szurubooru/src/Controllers/PostController.php

377 lines
11 KiB
PHP
Raw Normal View History

2013-10-05 12:55:03 +02:00
<?php
2014-05-16 21:38:33 +02:00
class PostController extends AbstractController
2013-10-05 12:55:03 +02:00
{
2014-05-02 13:49:31 +02:00
public function listView($query = null, $page = 1, $source = 'posts', $additionalInfo = null)
2013-10-05 12:55:03 +02:00
{
2014-05-15 10:32:53 +02:00
$context = Core::getContext();
2014-04-29 21:35:29 +02:00
$context->source = $source;
$context->additionalInfo = $additionalInfo;
2013-10-09 11:45:18 +02:00
$this->interceptErrors(function() use ($context, $query, $page, $source, $additionalInfo)
{
2014-05-16 21:38:33 +02:00
$query = trim($query);
$context->transport->searchQuery = $query;
$context->transport->lastSearchQuery = $query;
if ($source == 'mass-tag')
{
Access::assert(new Privilege(Privilege::MassTag));
$context->massTagTag = $additionalInfo;
$context->massTagQuery = $query;
if (!Access::check(new Privilege(Privilege::MassTag, 'all')))
$query = trim($query . ' submit:' . Auth::getCurrentUser()->getName());
}
$ret = Api::run(
new ListPostsJob(),
[
JobArgs::ARG_PAGE_NUMBER => $page,
JobArgs::ARG_QUERY => $query
]);
$context->transport->posts = $ret->entities;
$context->transport->paginator = $ret;
});
2014-05-16 21:38:33 +02:00
$this->renderView('post-list-wrapper');
2014-05-02 13:49:31 +02:00
}
public function listRedirectAction($source = 'posts')
{
$context = Core::getContext();
if ($source == 'mass-tag')
Access::assert(new Privilege(Privilege::MassTag));
$oldPage = intval(InputHelper::get('old-page'));
$oldQuery = trim(InputHelper::get('old-query'));
$query = trim(InputHelper::get('query'));
$additionalInfo = trim(InputHelper::get('tag'));
$context->transport->searchQuery = $query;
$context->transport->lastSearchQuery = $query;
if (strpos($query, '/') !== false)
throw new SimpleException('Search query contains invalid characters');
$params = [];
$params['source'] = $source;
if ($query)
$params['query'] = $query;
if ($additionalInfo)
$params['additionalInfo'] = $additionalInfo;
if ($oldPage != 0 and $oldQuery == $query)
$params['page'] = $oldPage;
else
$params['page'] = 1;
$url = Core::getRouter()->linkTo(['PostController', 'listView'], $params);
$this->redirect($url);
}
2014-05-02 13:49:31 +02:00
public function favoritesView($page = 1)
{
2014-05-16 21:38:33 +02:00
$this->listView('favmin:1', $page, 'favorites');
2014-05-02 13:49:31 +02:00
}
public function upvotedView($page = 1)
{
2014-05-16 21:38:33 +02:00
$this->listView('scoremin:1', $page, 'upvoted');
2014-05-02 13:49:31 +02:00
}
public function randomView($page = 1)
{
2014-05-16 21:38:33 +02:00
$this->listView('order:random', $page, 'random');
2013-10-05 19:24:08 +02:00
}
public function toggleTagAction($identifier, $tag, $enable)
{
2014-05-04 16:27:15 +02:00
Access::assert(new Privilege(
2014-04-30 08:08:24 +02:00
Privilege::MassTag,
Access::getIdentity(PostModel::getByIdOrName($identifier)->getUploader())));
$jobArgs =
[
JobArgs::ARG_TAG_NAME => $tag,
JobArgs::ARG_NEW_STATE => $enable,
];
2014-05-23 07:40:43 +02:00
$jobArgs = $this->appendPostIdentifierArgument($jobArgs, $identifier);
Api::run(new TogglePostTagJob(), $jobArgs);
2014-05-16 19:31:04 +02:00
2014-05-16 21:38:33 +02:00
if ($this->isAjax())
$this->renderAjax();
else
$this->redirectToLastVisitedUrl();
}
2014-05-03 14:20:48 +02:00
public function uploadView()
{
2014-05-16 21:38:33 +02:00
$this->renderView('post-upload');
2014-05-03 14:20:48 +02:00
}
2013-10-05 19:24:08 +02:00
public function uploadAction()
{
2014-05-03 14:20:48 +02:00
$jobArgs =
[
JobArgs::ARG_ANONYMOUS => InputHelper::get('anonymous'),
JobArgs::ARG_NEW_SAFETY => InputHelper::get('safety'),
JobArgs::ARG_NEW_TAG_NAMES => $this->splitTags(InputHelper::get('tags')),
JobArgs::ARG_NEW_SOURCE => InputHelper::get('source'),
2014-05-03 14:20:48 +02:00
];
if (!empty(InputHelper::get('url')))
{
$jobArgs[JobArgs::ARG_NEW_POST_CONTENT_URL] = InputHelper::get('url');
2014-05-03 14:20:48 +02:00
}
elseif (!empty($_FILES['file']['name']))
{
$file = $_FILES['file'];
TransferHelper::handleUploadErrors($file);
2013-10-07 00:44:17 +02:00
$jobArgs[JobArgs::ARG_NEW_POST_CONTENT] = new ApiFileInput(
2014-05-03 14:20:48 +02:00
$file['tmp_name'],
$file['name']);
}
2014-04-30 08:08:24 +02:00
2014-05-03 14:20:48 +02:00
Api::run(new AddPostJob(), $jobArgs);
2014-05-16 21:38:33 +02:00
if ($this->isAjax())
$this->renderAjax();
else
$this->redirectToPostList();
2013-10-05 19:24:08 +02:00
}
public function editView($identifier)
2013-10-13 12:28:16 +02:00
{
$jobArgs = [];
$jobArgs = $this->appendPostIdentifierArgument($jobArgs, $identifier);
$post = Api::run(new GetPostJob(), $jobArgs);
2014-05-04 16:27:15 +02:00
2014-05-15 10:32:53 +02:00
$context = Core::getContext()->transport->post = $post;
2014-05-16 21:38:33 +02:00
$this->renderView('post-edit');
2014-05-03 18:09:31 +02:00
}
2013-10-13 12:28:16 +02:00
public function editAction($identifier)
2014-05-03 18:09:31 +02:00
{
$post = PostModel::getByIdOrName($identifier);
2013-10-13 12:28:16 +02:00
2014-04-30 08:08:24 +02:00
$editToken = InputHelper::get('edit-token');
if ($editToken != $post->getEditToken())
throw new SimpleException('This post was already edited by someone else in the meantime');
2013-10-13 12:28:16 +02:00
2014-05-03 18:09:31 +02:00
$jobArgs =
[
JobArgs::ARG_NEW_SAFETY => InputHelper::get('safety'),
JobArgs::ARG_NEW_TAG_NAMES => $this->splitTags(InputHelper::get('tags')),
JobArgs::ARG_NEW_SOURCE => InputHelper::get('source'),
JobArgs::ARG_NEW_RELATED_POST_IDS => $this->splitPostIds(InputHelper::get('relations')),
2014-05-03 18:09:31 +02:00
];
$jobArgs = $this->appendPostIdentifierArgument($jobArgs, $identifier);
2013-10-30 20:20:01 +01:00
2014-05-03 18:09:31 +02:00
if (!empty(InputHelper::get('url')))
{
$jobArgs[JobArgs::ARG_NEW_POST_CONTENT_URL] = InputHelper::get('url');
2014-05-03 18:09:31 +02:00
}
elseif (!empty($_FILES['file']['name']))
{
$file = $_FILES['file'];
TransferHelper::handleUploadErrors($file);
$jobArgs[JobArgs::ARG_NEW_POST_CONTENT] = new ApiFileInput(
2014-05-03 18:09:31 +02:00
$file['tmp_name'],
$file['name']);
}
if (!empty($_FILES['thumbnail']['name']))
2014-05-03 18:09:31 +02:00
{
$file = $_FILES['thumbnail'];
2014-05-03 18:09:31 +02:00
TransferHelper::handleUploadErrors($file);
$jobArgs[JobArgs::ARG_NEW_THUMBNAIL_CONTENT] = new ApiFileInput(
2014-05-03 18:09:31 +02:00
$file['tmp_name'],
$file['name']);
}
Api::run(new EditPostJob(), $jobArgs);
2014-05-16 21:38:33 +02:00
if ($this->isAjax())
$this->renderAjax();
else
$this->redirectToGenericView($identifier);
2013-10-13 12:28:16 +02:00
}
public function flagAction($identifier)
2013-11-17 14:52:46 +01:00
{
$jobArgs = [];
$jobArgs = $this->appendPostIdentifierArgument($jobArgs, $identifier);
Api::run(new FlagPostJob(), $jobArgs);
2014-05-16 21:38:33 +02:00
if ($this->isAjax())
$this->renderAjax();
else
$this->redirectToGenericView($identifier);
2013-11-17 14:52:46 +01:00
}
public function hideAction($identifier)
2013-10-13 12:28:16 +02:00
{
$jobArgs = [JobArgs::ARG_NEW_STATE => false];
$jobArgs = $this->appendPostIdentifierArgument($jobArgs, $identifier);
Api::run(new TogglePostVisibilityJob(), $jobArgs);
$this->redirectToGenericView($identifier);
2013-10-13 12:28:16 +02:00
}
public function unhideAction($identifier)
2013-10-13 12:28:16 +02:00
{
$jobArgs = [JobArgs::ARG_NEW_STATE => true];
$jobArgs = $this->appendPostIdentifierArgument($jobArgs, $identifier);
Api::run(new TogglePostVisibilityJob(), $jobArgs);
$this->redirectToGenericView($identifier);
2013-10-13 12:28:16 +02:00
}
public function deleteAction($identifier)
2013-10-13 12:28:16 +02:00
{
$jobArgs = [];
$jobArgs = $this->appendPostIdentifierArgument($jobArgs, $identifier);
Api::run(new DeletePostJob(), $jobArgs);
2014-05-16 21:38:33 +02:00
$this->redirectToPostList();
2013-10-13 12:28:16 +02:00
}
public function addFavoriteAction($identifier)
2013-10-12 14:53:47 +02:00
{
$jobArgs = [JobArgs::ARG_NEW_STATE => true];
$jobArgs = $this->appendPostIdentifierArgument($jobArgs, $identifier);
Api::run(new TogglePostFavoriteJob(), $jobArgs);
$this->redirectToGenericView($identifier);
2013-10-12 14:53:47 +02:00
}
public function removeFavoriteAction($identifier)
2013-10-12 14:53:47 +02:00
{
$jobArgs = [JobArgs::ARG_NEW_STATE => false];
$jobArgs = $this->appendPostIdentifierArgument($jobArgs, $identifier);
Api::run(new TogglePostFavoriteJob(), $jobArgs);
$this->redirectToGenericView($identifier);
2013-10-12 14:53:47 +02:00
}
public function scoreAction($identifier, $score)
2013-11-10 12:23:59 +01:00
{
$jobArgs = [JobArgs::ARG_NEW_POST_SCORE => $score];
$jobArgs = $this->appendPostIdentifierArgument($jobArgs, $identifier);
Api::run(new ScorePostJob(), $jobArgs);
$this->redirectToGenericView($identifier);
2013-11-10 12:23:59 +01:00
}
public function featureAction($identifier)
2013-10-19 13:38:20 +02:00
{
$jobArgs = [];
$jobArgs = $this->appendPostIdentifierArgument($jobArgs, $identifier);
Api::run(new FeaturePostJob(), $jobArgs);
$this->redirectToGenericView($identifier);
2013-10-19 13:38:20 +02:00
}
public function genericView($identifier)
2013-10-05 19:24:08 +02:00
{
2014-05-15 10:32:53 +02:00
$context = Core::getContext();
2013-10-12 10:46:15 +02:00
$jobArgs = [];
$jobArgs = $this->appendPostIdentifierArgument($jobArgs, $identifier);
$post = Api::run(new GetPostJob(), $jobArgs);
try
{
2014-04-29 21:35:29 +02:00
$context->transport->lastSearchQuery = InputHelper::get('last-search-query');
list ($prevPostId, $nextPostId) =
PostSearchService::getPostIdsAround(
$context->transport->lastSearchQuery, $identifier);
}
#search for some reason was invalid, e.g. tag was deleted in the meantime
catch (Exception $e)
{
2014-04-29 21:35:29 +02:00
$context->transport->lastSearchQuery = '';
list ($prevPostId, $nextPostId) =
PostSearchService::getPostIdsAround($context->transport->lastSearchQuery, $identifier);
}
2013-10-13 12:28:16 +02:00
$isUserFavorite = Auth::getCurrentUser()->hasFavorited($post);
$userScore = Auth::getCurrentUser()->getScore($post);
$flagged = in_array(TextHelper::reprPost($post), SessionHelper::get('flagged', []));
$context->isUserFavorite = $isUserFavorite;
$context->userScore = $userScore;
2014-04-29 21:35:29 +02:00
$context->flagged = $flagged;
$context->transport->post = $post;
$context->transport->prevPostId = $prevPostId ? $prevPostId : null;
$context->transport->nextPostId = $nextPostId ? $nextPostId : null;
2014-05-16 21:38:33 +02:00
$this->renderView('post-view');
}
public function fileView($name)
2013-10-08 23:02:31 +02:00
{
$ret = Api::run(new GetPostContentJob(), [JobArgs::ARG_POST_NAME => $name]);
2013-10-08 23:02:31 +02:00
2014-05-15 10:32:53 +02:00
$context = Core::getContext();
$context->transport->cacheDaysToLive = 14;
$context->transport->customFileName = $ret->fileName;
$context->transport->mimeType = $ret->mimeType;
$context->transport->fileHash = 'post' . md5(substr($ret->fileContent, 0, 4096));
$context->transport->fileContent = $ret->fileContent;
$context->transport->lastModified = $ret->lastModified;
2014-05-16 21:38:33 +02:00
$this->renderFile();
2013-10-08 23:02:31 +02:00
}
public function thumbnailView($name)
{
$ret = Api::run(new GetPostThumbnailJob(), [JobArgs::ARG_POST_NAME => $name]);
2013-10-13 14:01:07 +02:00
2014-05-15 10:32:53 +02:00
$context = Core::getContext();
$context->transport->cacheDaysToLive = 365;
$context->transport->customFileName = $ret->fileName;
$context->transport->mimeType = 'image/jpeg';
$context->transport->fileHash = 'thumb' . md5(substr($ret->fileContent, 0, 4096));
$context->transport->fileContent = $ret->fileContent;
$context->transport->lastModified = $ret->lastModified;
2014-05-16 21:38:33 +02:00
$this->renderFile();
2013-10-05 12:55:03 +02:00
}
2014-05-07 21:39:41 +02:00
2014-05-16 21:38:33 +02:00
private function appendPostIdentifierArgument(array $arguments, $postIdentifier)
{
if (is_numeric($postIdentifier))
$arguments[JobArgs::ARG_POST_ID] = $postIdentifier;
else
$arguments[JobArgs::ARG_POST_NAME] = $postIdentifier;
return $arguments;
}
2014-05-16 19:31:04 +02:00
private function splitPostIds($string)
{
$ids = preg_split('/\D/', trim($string));
$ids = array_filter($ids, function($x) { return $x != ''; });
$ids = array_map('intval', $ids);
$ids = array_unique($ids);
return $ids;
}
2014-05-07 21:39:41 +02:00
2014-05-16 19:31:04 +02:00
private function splitTags($string)
2014-05-07 21:39:41 +02:00
{
$tags = preg_split('/[,;\s]+/', trim($string));
2014-05-07 21:39:41 +02:00
$tags = array_filter($tags, function($x) { return $x != ''; });
$tags = array_unique($tags);
return $tags;
}
2014-05-16 19:31:04 +02:00
2014-05-16 21:38:33 +02:00
private function redirectToPostList()
2014-05-16 19:31:04 +02:00
{
$this->redirect(Core::getRouter()->linkTo(['PostController', 'listView']));
2014-05-16 19:31:04 +02:00
}
private function redirectToGenericView($identifier)
2014-05-16 19:31:04 +02:00
{
$this->redirect(Core::getRouter()->linkTo(
2014-05-16 19:31:04 +02:00
['PostController', 'genericView'],
['identifier' => $identifier]));
2014-05-16 19:31:04 +02:00
}
2013-10-05 12:55:03 +02:00
}