2018-09-17 14:15:06 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Gallery;
|
|
|
|
|
2018-09-20 21:32:50 +01:00
|
|
|
use App\Album;
|
2018-09-18 14:28:59 +01:00
|
|
|
use App\Facade\Theme;
|
2018-09-17 14:15:06 +01:00
|
|
|
use App\Facade\UserConfig;
|
|
|
|
use App\Helpers\DbHelper;
|
2018-09-20 21:32:50 +01:00
|
|
|
use App\Helpers\PermissionsHelper;
|
2018-09-17 14:15:06 +01:00
|
|
|
use App\Http\Controllers\Controller;
|
2018-09-18 14:28:59 +01:00
|
|
|
use App\Http\Requests\StorePhotoCommentRequest;
|
2018-09-20 21:32:50 +01:00
|
|
|
use App\Mail\ModeratePhotoComment;
|
2018-10-05 21:08:14 +01:00
|
|
|
use App\Mail\PhotoCommentApproved;
|
|
|
|
use App\Mail\PhotoCommentApprovedUser;
|
2018-09-20 21:32:50 +01:00
|
|
|
use App\Permission;
|
2018-09-19 19:54:59 +01:00
|
|
|
use App\Photo;
|
2018-09-17 14:15:06 +01:00
|
|
|
use App\PhotoComment;
|
2018-09-19 20:23:02 +01:00
|
|
|
use App\User;
|
2018-09-17 14:15:06 +01:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Illuminate\Support\Facades\App;
|
|
|
|
use Illuminate\Support\Facades\Auth;
|
2018-09-19 19:54:59 +01:00
|
|
|
use Illuminate\Support\Facades\Gate;
|
2018-09-20 21:32:50 +01:00
|
|
|
use Illuminate\Support\Facades\Mail;
|
2018-09-18 15:50:12 +01:00
|
|
|
use Illuminate\Validation\ValidationException;
|
2018-09-17 14:15:06 +01:00
|
|
|
|
|
|
|
class PhotoCommentController extends Controller
|
|
|
|
{
|
2018-09-19 19:54:59 +01:00
|
|
|
public function moderate(Request $request, $albumUrlAlias, $photoFilename, $commentID)
|
2018-09-18 14:28:59 +01:00
|
|
|
{
|
2018-09-19 19:54:59 +01:00
|
|
|
$album = null;
|
|
|
|
|
|
|
|
/** @var Photo $photo */
|
|
|
|
$photo = null;
|
|
|
|
|
|
|
|
/** @var PhotoComment $comment */
|
|
|
|
$comment = null;
|
|
|
|
|
|
|
|
if (!$this->loadAlbumPhotoComment($albumUrlAlias, $photoFilename, $commentID, $album, $photo, $comment))
|
2018-09-18 14:28:59 +01:00
|
|
|
{
|
2018-09-19 20:23:02 +01:00
|
|
|
return null;
|
2018-09-19 19:54:59 +01:00
|
|
|
}
|
|
|
|
|
2018-10-05 21:08:14 +01:00
|
|
|
if (!User::currentOrAnonymous()->can('moderate-comments', $photo))
|
2018-09-19 19:54:59 +01:00
|
|
|
{
|
|
|
|
App::abort(403);
|
2018-09-18 14:28:59 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-09-19 19:54:59 +01:00
|
|
|
if (!$comment->isModerated())
|
|
|
|
{
|
|
|
|
if ($request->has('approve'))
|
|
|
|
{
|
|
|
|
$comment->approved_at = new \DateTime();
|
|
|
|
$comment->approved_user_id = $this->getUser()->id;
|
|
|
|
$comment->save();
|
2018-09-18 14:28:59 +01:00
|
|
|
|
2018-10-05 23:35:01 +01:00
|
|
|
$this->notifyAlbumOwnerAndPoster($album, $photo, $comment);
|
2018-09-19 19:54:59 +01:00
|
|
|
$request->getSession()->flash('success', trans('gallery.photo_comment_approved_successfully'));
|
|
|
|
}
|
|
|
|
else if ($request->has('reject'))
|
|
|
|
{
|
|
|
|
$comment->rejected_at = new \DateTime();
|
|
|
|
$comment->rejected_user_id = $this->getUser()->id;
|
|
|
|
$comment->save();
|
2018-09-18 14:28:59 +01:00
|
|
|
|
2018-09-19 19:54:59 +01:00
|
|
|
$request->getSession()->flash('success', trans('gallery.photo_comment_rejected_successfully'));
|
|
|
|
}
|
2018-09-18 14:28:59 +01:00
|
|
|
}
|
|
|
|
|
2018-09-19 19:54:59 +01:00
|
|
|
return redirect($photo->url());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function reply(Request $request, $albumUrlAlias, $photoFilename, $commentID)
|
|
|
|
{
|
|
|
|
$album = null;
|
|
|
|
|
|
|
|
/** @var Photo $photo */
|
|
|
|
$photo = null;
|
|
|
|
|
|
|
|
/** @var PhotoComment $comment */
|
|
|
|
$comment = null;
|
|
|
|
|
|
|
|
if (!$this->loadAlbumPhotoComment($albumUrlAlias, $photoFilename, $commentID, $album, $photo, $comment))
|
2018-09-18 14:28:59 +01:00
|
|
|
{
|
2018-09-19 20:23:02 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-09-20 14:38:34 +01:00
|
|
|
if (!User::currentOrAnonymous()->can('post-comment', $photo))
|
2018-09-19 20:23:02 +01:00
|
|
|
{
|
|
|
|
App::abort(403);
|
|
|
|
return null;
|
2018-09-18 14:28:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return Theme::render('partials.photo_comments_reply_form', [
|
|
|
|
'photo' => $photo,
|
|
|
|
'reply_comment' => $comment
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2018-09-18 15:50:12 +01:00
|
|
|
public function store(Request $request, $albumUrlAlias, $photoFilename)
|
2018-09-17 14:15:06 +01:00
|
|
|
{
|
2018-09-19 19:54:59 +01:00
|
|
|
$album = null;
|
2018-09-17 14:15:06 +01:00
|
|
|
|
2018-09-19 19:54:59 +01:00
|
|
|
/** @var Photo $photo */
|
|
|
|
$photo = null;
|
2018-09-17 14:15:06 +01:00
|
|
|
|
2018-09-19 19:54:59 +01:00
|
|
|
/** @var PhotoComment $comment */
|
|
|
|
$comment = null;
|
2018-09-17 14:15:06 +01:00
|
|
|
|
2018-09-19 19:54:59 +01:00
|
|
|
if (!$this->loadAlbumPhotoComment($albumUrlAlias, $photoFilename, 0, $album, $photo, $comment))
|
2018-09-17 14:15:06 +01:00
|
|
|
{
|
2018-09-19 20:23:02 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-09-20 14:38:34 +01:00
|
|
|
if (!User::currentOrAnonymous()->can('post-comment', $photo))
|
2018-09-19 20:23:02 +01:00
|
|
|
{
|
|
|
|
App::abort(403);
|
|
|
|
return null;
|
2018-09-17 14:15:06 +01:00
|
|
|
}
|
|
|
|
|
2018-09-18 14:28:59 +01:00
|
|
|
// Validate and link the parent comment, if provided
|
2018-09-18 15:50:12 +01:00
|
|
|
// We do this here so if the validation fails, we still have the parent comment available in the catch block
|
|
|
|
$parentComment = null;
|
2018-09-18 14:28:59 +01:00
|
|
|
if ($request->has('parent_comment_id'))
|
|
|
|
{
|
|
|
|
$parentComment = $photo->comments()->where('id', intval($request->get('parent_comment_id')))->first();
|
|
|
|
|
|
|
|
if (is_null($parentComment))
|
|
|
|
{
|
|
|
|
return redirect($photo->url());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-18 15:50:12 +01:00
|
|
|
try
|
2018-09-17 14:15:06 +01:00
|
|
|
{
|
2018-09-18 15:50:12 +01:00
|
|
|
$this->validate($request, [
|
2018-09-19 09:44:20 +01:00
|
|
|
'name' => 'required|max:255',
|
|
|
|
'email' => 'sometimes|max:255|email',
|
|
|
|
'comment' => 'required'
|
2018-09-18 15:50:12 +01:00
|
|
|
]);
|
|
|
|
|
2018-09-24 15:55:48 +01:00
|
|
|
$commentText = $this->stripDisallowedHtmlTags($request->get('comment'));
|
|
|
|
|
2018-09-18 15:50:12 +01:00
|
|
|
$comment = new PhotoComment();
|
|
|
|
$comment->photo_id = $photo->id;
|
2018-09-24 15:55:48 +01:00
|
|
|
$comment->fill($request->only(['name', 'email']));
|
|
|
|
$comment->comment = $commentText;
|
2018-09-18 15:50:12 +01:00
|
|
|
|
|
|
|
if (!is_null($parentComment))
|
|
|
|
{
|
|
|
|
$comment->parent_comment_id = $parentComment->id;
|
|
|
|
}
|
2018-09-17 14:15:06 +01:00
|
|
|
|
2018-09-19 19:54:59 +01:00
|
|
|
// Set the created user ID if we're logged in
|
2018-09-18 15:50:12 +01:00
|
|
|
$user = $this->getUser();
|
|
|
|
if (!is_null($user) && !$user->isAnonymous())
|
|
|
|
{
|
|
|
|
$comment->created_user_id = $user->id;
|
|
|
|
}
|
2018-09-17 14:15:06 +01:00
|
|
|
|
2018-09-19 19:54:59 +01:00
|
|
|
// Auto-approve the comment if we're allowed to moderate comments
|
|
|
|
$isAutoApproved = false;
|
2018-10-05 21:08:14 +01:00
|
|
|
if (User::currentOrAnonymous()->can('moderate-comments', $photo))
|
2018-09-19 19:54:59 +01:00
|
|
|
{
|
|
|
|
$comment->approved_at = new \DateTime();
|
|
|
|
$comment->approved_user_id = $user->id;
|
|
|
|
$isAutoApproved = true;
|
|
|
|
}
|
|
|
|
|
2018-09-19 20:35:43 +01:00
|
|
|
// Auto-approve the comment if settings allow
|
|
|
|
if ($user->isAnonymous() && !UserConfig::get('moderate_anonymous_users'))
|
|
|
|
{
|
|
|
|
$comment->approved_at = new \DateTime();
|
|
|
|
$comment->approved_user_id = null; // we don't have a user ID to set!
|
|
|
|
$isAutoApproved = true;
|
|
|
|
}
|
|
|
|
else if (!$user->isAnonymous() && !UserConfig::get('moderate_known_users'))
|
|
|
|
{
|
|
|
|
$comment->approved_at = new \DateTime();
|
|
|
|
$comment->approved_user_id = $user->id;
|
|
|
|
$isAutoApproved = true;
|
|
|
|
}
|
|
|
|
|
2018-10-05 21:08:14 +01:00
|
|
|
$comment->save();
|
|
|
|
|
2018-09-20 21:32:50 +01:00
|
|
|
// Send notification e-mails to moderators or album owner
|
|
|
|
if (!$isAutoApproved)
|
|
|
|
{
|
|
|
|
$this->notifyAlbumModerators($album, $photo, $comment);
|
2018-10-05 21:08:14 +01:00
|
|
|
$request->getSession()->flash('success', trans('gallery.photo_comment_posted_successfully_pending_moderation'));
|
2018-09-19 19:54:59 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-10-05 23:35:01 +01:00
|
|
|
$this->notifyAlbumOwnerAndPoster($album, $photo, $comment);
|
2018-10-05 21:08:14 +01:00
|
|
|
$request->getSession()->flash('success', trans('gallery.photo_comment_posted_successfully'));
|
2018-09-19 19:54:59 +01:00
|
|
|
}
|
2018-09-18 15:50:12 +01:00
|
|
|
|
|
|
|
if ($request->isXmlHttpRequest())
|
|
|
|
{
|
|
|
|
return response()->json(['redirect_url' => $photo->url()]);
|
2018-10-05 21:08:14 +01:00
|
|
|
}
|
|
|
|
else
|
2018-09-18 15:50:12 +01:00
|
|
|
{
|
|
|
|
return redirect($photo->url());
|
|
|
|
}
|
2018-09-18 14:28:59 +01:00
|
|
|
}
|
2018-09-18 15:50:12 +01:00
|
|
|
catch (ValidationException $e)
|
2018-09-18 14:28:59 +01:00
|
|
|
{
|
2018-09-18 15:50:12 +01:00
|
|
|
if (!is_null($parentComment))
|
|
|
|
{
|
2018-09-18 22:35:22 +01:00
|
|
|
return redirect()
|
|
|
|
->to($photo->replyToCommentFormUrl($parentComment->id))
|
|
|
|
->withErrors($e->errors())
|
|
|
|
->withInput($request->all());
|
2018-09-18 15:50:12 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-09-18 22:35:22 +01:00
|
|
|
return redirect()
|
|
|
|
->back()
|
|
|
|
->withErrors($e->errors())
|
|
|
|
->withInput($request->all());
|
2018-09-18 15:50:12 +01:00
|
|
|
}
|
2018-09-18 14:28:59 +01:00
|
|
|
}
|
2018-09-17 14:15:06 +01:00
|
|
|
}
|
2018-09-19 19:54:59 +01:00
|
|
|
|
|
|
|
private function loadAlbumPhotoComment($albumUrlAlias, $photoFilename, $commentID, &$album, &$photo, &$comment)
|
|
|
|
{
|
|
|
|
$album = DbHelper::getAlbumByPath($albumUrlAlias);
|
|
|
|
if (is_null($album))
|
|
|
|
{
|
|
|
|
App::abort(404);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->authorizeForUser($this->getUser(), 'view', $album);
|
|
|
|
|
|
|
|
$photo = PhotoController::loadPhotoByAlbumAndFilename($album, $photoFilename);
|
|
|
|
|
|
|
|
if (!UserConfig::get('allow_photo_comments'))
|
|
|
|
{
|
|
|
|
// Not allowed to post comments
|
|
|
|
App::abort(404);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (intval($commentID > 0))
|
|
|
|
{
|
|
|
|
$comment = $photo->comments()->where('id', $commentID)->first();
|
|
|
|
if (is_null($comment))
|
|
|
|
{
|
|
|
|
App::abort(404);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2018-09-20 21:32:50 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends an e-mail notification to an album's moderators that a comment is available to moderate.
|
|
|
|
* @param Album $album
|
|
|
|
* @param Photo $photo
|
|
|
|
* @param PhotoComment $comment
|
|
|
|
*/
|
|
|
|
private function notifyAlbumModerators(Album $album, Photo $photo, PhotoComment $comment)
|
|
|
|
{
|
|
|
|
// Get all users from the cache
|
|
|
|
$helper = new PermissionsHelper();
|
|
|
|
$moderators = $helper->usersWhoCan_Album($album, 'moderate-comments');
|
|
|
|
|
|
|
|
/** @var User $moderator */
|
|
|
|
foreach ($moderators as $moderator)
|
|
|
|
{
|
|
|
|
Mail::to($moderator)->send(new ModeratePhotoComment($moderator, $album, $photo, $comment));
|
|
|
|
}
|
|
|
|
}
|
2018-09-24 15:55:48 +01:00
|
|
|
|
2018-10-05 21:08:14 +01:00
|
|
|
/**
|
|
|
|
* Sends an e-mail notification to an album's owned that a comment has been posted/approved.
|
|
|
|
* @param Album $album
|
|
|
|
* @param Photo $photo
|
|
|
|
* @param PhotoComment $comment
|
|
|
|
*/
|
2018-10-05 23:35:01 +01:00
|
|
|
private function notifyAlbumOwnerAndPoster(Album $album, Photo $photo, PhotoComment $comment)
|
2018-10-05 21:08:14 +01:00
|
|
|
{
|
|
|
|
$owner = $album->user;
|
|
|
|
|
2018-10-05 23:35:01 +01:00
|
|
|
Mail::to($owner)->send(new PhotoCommentApproved($owner, $album, $photo, $comment));
|
|
|
|
|
|
|
|
// Also send a notification to the comment poster
|
2018-10-05 21:08:14 +01:00
|
|
|
$poster = new User();
|
|
|
|
$poster->name = $comment->authorDisplayName();
|
|
|
|
$poster->email = $comment->authorEmail();
|
|
|
|
|
|
|
|
Mail::to($poster)->send(new PhotoCommentApprovedUser($poster, $album, $photo, $comment));
|
2018-10-05 23:35:01 +01:00
|
|
|
|
|
|
|
// Send notification to the parent comment owner (if this is a reply)
|
|
|
|
if (!is_null($comment->parent_comment_id))
|
|
|
|
{
|
|
|
|
$parentComment = $this->loadCommentByID($comment->parent_comment_id);
|
|
|
|
|
|
|
|
if (is_null($parentComment))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$parentPoster = new User();
|
|
|
|
$parentPoster->name = $parentComment->authorDisplayName();
|
|
|
|
$parentPoster->email = $parentComment->authorEmail();
|
|
|
|
|
|
|
|
Mail::to($parentPoster)->send(new PhotoCommentRepliedTo($parentPoster, $album, $photo, $comment));
|
|
|
|
}
|
2018-10-05 21:08:14 +01:00
|
|
|
}
|
|
|
|
|
2018-09-24 15:55:48 +01:00
|
|
|
private function stripDisallowedHtmlTags($commentText)
|
|
|
|
{
|
|
|
|
$allowedHtmlTags = explode(',', UserConfig::get('photo_comments_allowed_html'));
|
|
|
|
$allowedHtmlTagsCleaned = [];
|
|
|
|
|
|
|
|
foreach ($allowedHtmlTags as $tag)
|
|
|
|
{
|
|
|
|
$allowedHtmlTagsCleaned[] = trim($tag);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Match any starting HTML tags
|
|
|
|
$regexMatchString = '/<(?!\/)([a-z]+)(?:\s.*)*>/Us';
|
|
|
|
|
|
|
|
$htmlTagMatches = [];
|
|
|
|
preg_match_all($regexMatchString, $commentText, $htmlTagMatches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER);
|
|
|
|
|
|
|
|
for ($index = 0; $index < count($htmlTagMatches); $index++)
|
|
|
|
{
|
|
|
|
$htmlTagMatch = $htmlTagMatches[$index];
|
|
|
|
|
|
|
|
$htmlTag = $htmlTagMatch[1][0]; // e.g. "p" for <p>
|
|
|
|
if (in_array($htmlTag, $allowedHtmlTagsCleaned))
|
|
|
|
{
|
|
|
|
// This tag is allowed - carry on
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This tag is not allowed - remove it from the string */
|
|
|
|
|
|
|
|
// Find the closing tag
|
|
|
|
$disallowedStringOffset = $htmlTagMatch[0][1];
|
|
|
|
$endingTagMatches = [];
|
|
|
|
preg_match(sprintf('/(<%1$s.*>)(.+)<\/%1$s>/Us', $htmlTag), $commentText, $endingTagMatches, 0, $disallowedStringOffset);
|
|
|
|
|
|
|
|
// Replace the matched string with the inner string
|
|
|
|
$commentText = substr_replace($commentText, $endingTagMatches[2], $disallowedStringOffset, strlen($endingTagMatches[0]));
|
|
|
|
|
|
|
|
// Adjust the offsets for strings after the one we're processing, so the offsets match up with the string correctly
|
|
|
|
for ($index2 = $index + 1; $index2 < count($htmlTagMatches); $index2++)
|
|
|
|
{
|
2018-09-26 16:51:17 +01:00
|
|
|
// If this string appears entirely BEFORE the next one starts, we need to subtract the entire length.
|
|
|
|
// Otherwise, we only need to substract the length of the start tag, as the next one starts within it.
|
|
|
|
$differenceAfterReplacement = strlen($endingTagMatches[1]);
|
|
|
|
|
|
|
|
if ($htmlTagMatch[0][1] + strlen($endingTagMatches[0]) < $htmlTagMatches[$index2][0][1])
|
|
|
|
{
|
|
|
|
$differenceAfterReplacement = strlen($endingTagMatches[0]) - strlen($endingTagMatches[2]);
|
|
|
|
}
|
|
|
|
|
2018-09-24 15:55:48 +01:00
|
|
|
$htmlTagMatches[$index2][0][1] -= $differenceAfterReplacement;
|
|
|
|
$htmlTagMatches[$index2][1][1] -= $differenceAfterReplacement;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $commentText;
|
|
|
|
}
|
2018-09-17 14:15:06 +01:00
|
|
|
}
|