szurubooru/src/Controllers/PostController.php

368 lines
9.4 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
2014-05-16 21:38:33 +02:00
try
{
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
catch (SimpleException $e)
{
2014-05-16 21:38:33 +02:00
Messenger::fail($e->getMessage());
}
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 = \Chibi\Router::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($id, $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::getById($id)->getUploader())));
2014-05-03 11:18:34 +02:00
Api::run(
new TogglePostTagJob(),
[
JobArgs::ARG_POST_ID => $id,
JobArgs::ARG_TAG_NAME => $tag,
JobArgs::ARG_NEW_STATE => $enable,
2014-05-03 11:18:34 +02:00
]);
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
}
2014-05-03 18:09:31 +02:00
public function editView($id)
2013-10-13 12:28:16 +02:00
{
2014-05-04 16:27:15 +02:00
$post = Api::run(new GetPostJob(), [
JobArgs::ARG_POST_ID => $id]);
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
2014-05-03 18:09:31 +02:00
public function editAction($id)
{
$post = PostModel::getByIdOrName($id);
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_POST_ID => $id,
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
];
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($id);
2013-10-13 12:28:16 +02:00
}
2013-11-17 14:52:46 +01:00
public function flagAction($id)
{
Api::run(new FlagPostJob(), [JobArgs::ARG_POST_ID => $id]);
2014-05-16 21:38:33 +02:00
if ($this->isAjax())
$this->renderAjax();
else
$this->redirectToGenericView($id);
2013-11-17 14:52:46 +01:00
}
2013-10-13 12:28:16 +02:00
public function hideAction($id)
{
2014-05-03 19:21:56 +02:00
Api::run(new TogglePostVisibilityJob(), [
JobArgs::ARG_POST_ID => $id,
JobArgs::ARG_NEW_STATE => false]);
2014-05-16 19:31:04 +02:00
$this->redirectToGenericView($id);
2013-10-13 12:28:16 +02:00
}
public function unhideAction($id)
{
2014-05-03 19:21:56 +02:00
Api::run(new TogglePostVisibilityJob(), [
JobArgs::ARG_POST_ID => $id,
JobArgs::ARG_NEW_STATE => true]);
2014-05-16 19:31:04 +02:00
$this->redirectToGenericView($id);
2013-10-13 12:28:16 +02:00
}
public function deleteAction($id)
{
2014-05-03 19:35:59 +02:00
Api::run(new DeletePostJob(), [
JobArgs::ARG_POST_ID => $id]);
2014-05-16 21:38:33 +02:00
$this->redirectToPostList();
2013-10-13 12:28:16 +02:00
}
2013-10-12 14:53:47 +02:00
public function addFavoriteAction($id)
{
2014-05-03 19:39:27 +02:00
Api::run(new TogglePostFavoriteJob(), [
JobArgs::ARG_POST_ID => $id,
JobArgs::ARG_NEW_STATE => true]);
2014-05-16 19:31:04 +02:00
$this->redirectToGenericView($id);
2013-10-12 14:53:47 +02:00
}
2014-04-29 21:35:29 +02:00
public function removeFavoriteAction($id)
2013-10-12 14:53:47 +02:00
{
2014-05-03 19:39:27 +02:00
Api::run(new TogglePostFavoriteJob(), [
JobArgs::ARG_POST_ID => $id,
JobArgs::ARG_NEW_STATE => false]);
2014-05-16 19:31:04 +02:00
$this->redirectToGenericView($id);
2013-10-12 14:53:47 +02:00
}
2013-11-10 12:23:59 +01:00
public function scoreAction($id, $score)
{
2014-05-03 19:50:35 +02:00
Api::run(new ScorePostJob(), [
JobArgs::ARG_POST_ID => $id,
JobArgs::ARG_NEW_POST_SCORE => $score]);
2014-05-16 19:31:04 +02:00
$this->redirectToGenericView($id);
2013-11-10 12:23:59 +01:00
}
2013-10-19 13:38:20 +02:00
public function featureAction($id)
{
2014-05-03 19:51:26 +02:00
Api::run(new FeaturePostJob(), [
JobArgs::ARG_POST_ID => $id]);
2014-05-16 19:31:04 +02:00
$this->redirectToGenericView($id);
2013-10-19 13:38:20 +02:00
}
2014-05-03 20:32:47 +02:00
public function genericView($id)
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
2014-05-03 20:32:47 +02:00
$post = Api::run(new GetPostJob(), [
JobArgs::ARG_POST_ID => $id]);
try
{
2014-04-29 21:35:29 +02:00
$context->transport->lastSearchQuery = InputHelper::get('last-search-query');
list ($prevPostId, $nextPostId) =
PostSearchService::getPostIdsAround(
2014-04-29 21:35:29 +02:00
$context->transport->lastSearchQuery, $id);
}
#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(
2014-04-29 21:35:29 +02:00
$context->transport->lastSearchQuery, $id);
}
2013-10-13 12:28:16 +02:00
2014-05-03 20:32:47 +02:00
//todo:
//move these to PostEntity when implementing ApiController
$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
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
{
2014-05-16 21:38:33 +02:00
$this->redirect(\Chibi\Router::linkTo(['PostController', 'listView']));
2014-05-16 19:31:04 +02:00
}
private function redirectToGenericView($id)
{
2014-05-16 21:38:33 +02:00
$this->redirect(\Chibi\Router::linkTo(
2014-05-16 19:31:04 +02:00
['PostController', 'genericView'],
['id' => $id]));
}
2013-10-05 12:55:03 +02:00
}