2013-10-05 12:55:03 +02:00
|
|
|
<?php
|
2013-10-05 21:24:20 +02:00
|
|
|
class PostController
|
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-04-29 21:35:29 +02:00
|
|
|
$context = getContext();
|
|
|
|
$context->viewName = 'post-list-wrapper';
|
|
|
|
$context->source = $source;
|
|
|
|
$context->additionalInfo = $additionalInfo;
|
|
|
|
$context->handleExceptions = true;
|
2013-10-09 11:45:18 +02:00
|
|
|
|
2013-10-14 00:25:40 +02:00
|
|
|
//redirect requests in form of /posts/?query=... to canonical address
|
2013-10-05 22:52:55 +02:00
|
|
|
$formQuery = InputHelper::get('query');
|
2013-10-13 22:20:06 +02:00
|
|
|
if ($formQuery !== null)
|
2013-10-05 22:52:55 +02:00
|
|
|
{
|
2014-04-29 21:35:29 +02:00
|
|
|
$context->transport->searchQuery = $formQuery;
|
|
|
|
$context->transport->lastSearchQuery = $formQuery;
|
2013-10-13 22:20:06 +02:00
|
|
|
if (strpos($formQuery, '/') !== false)
|
2013-10-14 00:25:40 +02:00
|
|
|
throw new SimpleException('Search query contains invalid characters');
|
2014-04-27 14:42:39 +02:00
|
|
|
|
2014-05-02 13:49:31 +02:00
|
|
|
$url = \Chibi\Router::linkTo(['PostController', 'listView'], [
|
2014-04-27 14:42:39 +02:00
|
|
|
'source' => $source,
|
|
|
|
'additionalInfo' => $additionalInfo,
|
|
|
|
'query' => $formQuery]);
|
2014-04-29 21:35:29 +02:00
|
|
|
\Chibi\Util\Url::forward($url);
|
2014-05-03 23:27:00 +02:00
|
|
|
exit;
|
2013-10-05 22:52:55 +02:00
|
|
|
}
|
|
|
|
|
2013-11-18 14:00:54 +01:00
|
|
|
$query = trim($query);
|
2014-04-29 21:35:29 +02:00
|
|
|
$context->transport->searchQuery = $query;
|
|
|
|
$context->transport->lastSearchQuery = $query;
|
2013-10-29 09:04:42 +01:00
|
|
|
if ($source == 'mass-tag')
|
|
|
|
{
|
2014-05-04 16:27:15 +02:00
|
|
|
Access::assert(new Privilege(Privilege::MassTag));
|
2014-04-29 21:35:29 +02:00
|
|
|
$context->massTagTag = $additionalInfo;
|
|
|
|
$context->massTagQuery = $query;
|
2014-02-20 18:44:51 +01:00
|
|
|
|
2014-05-04 16:27:15 +02:00
|
|
|
if (!Access::check(new Privilege(Privilege::MassTag, 'all')))
|
2014-05-04 20:09:03 +02:00
|
|
|
$query = trim($query . ' submit:' . Auth::getCurrentUser()->getName());
|
2013-10-29 09:04:42 +01:00
|
|
|
}
|
2013-10-07 20:44:14 +02:00
|
|
|
|
2014-05-02 13:49:31 +02:00
|
|
|
$ret = Api::run(
|
|
|
|
new ListPostsJob(),
|
|
|
|
[
|
2014-05-03 11:43:29 +02:00
|
|
|
ListPostsJob::PAGE_NUMBER => $page,
|
|
|
|
ListPostsJob::QUERY => $query
|
2014-05-02 13:49:31 +02:00
|
|
|
]);
|
2013-10-09 11:45:18 +02:00
|
|
|
|
2014-05-04 09:11:08 +02:00
|
|
|
$context->transport->posts = $ret->entities;
|
|
|
|
$context->transport->paginator = $ret;
|
2014-05-02 13:49:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function favoritesView($page = 1)
|
|
|
|
{
|
|
|
|
$this->listView('favmin:1', $page);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function upvotedView($page = 1)
|
|
|
|
{
|
|
|
|
$this->listView('scoremin:1', $page);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function randomView($page = 1)
|
|
|
|
{
|
|
|
|
$this->listView('order:random', $page);
|
2013-10-05 19:24:08 +02:00
|
|
|
}
|
|
|
|
|
2013-11-25 11:59:59 +01:00
|
|
|
public function toggleTagAction($id, $tag, $enable)
|
2013-10-29 09:04:42 +01:00
|
|
|
{
|
2014-05-04 16:27:15 +02:00
|
|
|
Access::assert(new Privilege(
|
2014-04-30 08:08:24 +02:00
|
|
|
Privilege::MassTag,
|
2014-05-08 08:54:08 +02:00
|
|
|
Access::getIdentity(PostModel::getById($id)->getUploader())));
|
2013-12-18 15:10:53 +01:00
|
|
|
|
2014-05-03 11:18:34 +02:00
|
|
|
Api::run(
|
|
|
|
new TogglePostTagJob(),
|
|
|
|
[
|
2014-05-03 11:43:29 +02:00
|
|
|
TogglePostTagJob::POST_ID => $id,
|
|
|
|
TogglePostTagJob::TAG_NAME => $tag,
|
|
|
|
TogglePostTagJob::STATE => $enable,
|
2014-05-03 11:18:34 +02:00
|
|
|
]);
|
2013-10-29 09:04:42 +01:00
|
|
|
}
|
|
|
|
|
2014-05-03 14:20:48 +02:00
|
|
|
public function uploadView()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-10-05 19:24:08 +02:00
|
|
|
public function uploadAction()
|
|
|
|
{
|
2014-05-03 14:20:48 +02:00
|
|
|
$jobArgs =
|
|
|
|
[
|
|
|
|
AddPostJob::ANONYMOUS => InputHelper::get('anonymous'),
|
|
|
|
EditPostSafetyJob::SAFETY => InputHelper::get('safety'),
|
2014-05-07 21:39:41 +02:00
|
|
|
EditPostTagsJob::TAG_NAMES => $this->splitTags(InputHelper::get('tags')),
|
2014-05-03 14:20:48 +02:00
|
|
|
EditPostSourceJob::SOURCE => InputHelper::get('source'),
|
|
|
|
];
|
|
|
|
|
|
|
|
if (!empty(InputHelper::get('url')))
|
|
|
|
{
|
2014-05-06 15:49:02 +02:00
|
|
|
$jobArgs[EditPostContentJob::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
|
|
|
|
2014-05-03 22:14:00 +02:00
|
|
|
$jobArgs[EditPostContentJob::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);
|
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(), [
|
|
|
|
GetPostJob::POST_ID => $id]);
|
|
|
|
|
2014-05-03 18:09:31 +02:00
|
|
|
$context = getContext()->transport->post = $post;
|
|
|
|
}
|
2013-10-13 12:28:16 +02:00
|
|
|
|
2014-05-03 18:09:31 +02:00
|
|
|
public function editAction($id)
|
|
|
|
{
|
2014-05-08 08:54:08 +02:00
|
|
|
$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 =
|
|
|
|
[
|
|
|
|
EditPostJob::POST_ID => $id,
|
|
|
|
EditPostSafetyJob::SAFETY => InputHelper::get('safety'),
|
2014-05-07 21:39:41 +02:00
|
|
|
EditPostTagsJob::TAG_NAMES => $this->splitTags(InputHelper::get('tags')),
|
2014-05-03 18:09:31 +02:00
|
|
|
EditPostSourceJob::SOURCE => InputHelper::get('source'),
|
|
|
|
EditPostRelationsJob::RELATED_POST_IDS => InputHelper::get('relations'),
|
|
|
|
];
|
2013-10-30 20:20:01 +01:00
|
|
|
|
2014-05-03 18:09:31 +02:00
|
|
|
if (!empty(InputHelper::get('url')))
|
|
|
|
{
|
2014-05-06 15:49:02 +02:00
|
|
|
$jobArgs[EditPostContentJob::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);
|
|
|
|
|
2014-05-03 22:14:00 +02:00
|
|
|
$jobArgs[EditPostContentJob::POST_CONTENT] = new ApiFileInput(
|
2014-05-03 18:09:31 +02:00
|
|
|
$file['tmp_name'],
|
|
|
|
$file['name']);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($_FILES['thumb']['name']))
|
|
|
|
{
|
|
|
|
$file = $_FILES['thumb'];
|
|
|
|
TransferHelper::handleUploadErrors($file);
|
|
|
|
|
2014-05-03 22:14:00 +02:00
|
|
|
$jobArgs[EditPostThumbJob::THUMB_CONTENT] = new ApiFileInput(
|
2014-05-03 18:09:31 +02:00
|
|
|
$file['tmp_name'],
|
|
|
|
$file['name']);
|
|
|
|
}
|
|
|
|
|
|
|
|
Api::run(new EditPostJob(), $jobArgs);
|
2013-10-13 12:28:16 +02:00
|
|
|
}
|
|
|
|
|
2013-11-17 14:52:46 +01:00
|
|
|
public function flagAction($id)
|
|
|
|
{
|
2014-05-03 19:08:58 +02:00
|
|
|
Api::run(new FlagPostJob(), [FlagPostJob::POST_ID => $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(), [
|
|
|
|
TogglePostVisibilityJob::POST_ID => $id,
|
|
|
|
TogglePostVisibilityJob::STATE => false]);
|
2013-10-13 12:28:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function unhideAction($id)
|
|
|
|
{
|
2014-05-03 19:21:56 +02:00
|
|
|
Api::run(new TogglePostVisibilityJob(), [
|
|
|
|
TogglePostVisibilityJob::POST_ID => $id,
|
|
|
|
TogglePostVisibilityJob::STATE => true]);
|
2013-10-13 12:28:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function deleteAction($id)
|
|
|
|
{
|
2014-05-03 19:35:59 +02:00
|
|
|
Api::run(new DeletePostJob(), [
|
|
|
|
DeletePostJob::POST_ID => $id]);
|
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(), [
|
|
|
|
TogglePostFavoriteJob::POST_ID => $id,
|
|
|
|
TogglePostFavoriteJob::STATE => true]);
|
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(), [
|
|
|
|
TogglePostFavoriteJob::POST_ID => $id,
|
|
|
|
TogglePostFavoriteJob::STATE => false]);
|
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(), [
|
|
|
|
ScorePostJob::POST_ID => $id,
|
|
|
|
ScorePostJob::SCORE => $score]);
|
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(), [
|
|
|
|
FeaturePostJob::POST_ID => $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-04-29 21:35:29 +02:00
|
|
|
$context = getContext();
|
2014-05-03 20:32:47 +02:00
|
|
|
$context->viewName = 'post-view';
|
2013-10-12 10:46:15 +02:00
|
|
|
|
2014-05-03 20:32:47 +02:00
|
|
|
$post = Api::run(new GetPostJob(), [
|
|
|
|
GetPostJob::POST_ID => $id]);
|
2013-10-07 20:44:14 +02:00
|
|
|
|
2013-12-01 14:47:35 +01:00
|
|
|
try
|
|
|
|
{
|
2014-04-29 21:35:29 +02:00
|
|
|
$context->transport->lastSearchQuery = InputHelper::get('last-search-query');
|
2014-02-22 19:21:32 +01:00
|
|
|
list ($prevPostId, $nextPostId) =
|
|
|
|
PostSearchService::getPostIdsAround(
|
2014-04-29 21:35:29 +02:00
|
|
|
$context->transport->lastSearchQuery, $id);
|
2013-12-01 14:47:35 +01:00
|
|
|
}
|
|
|
|
#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 = '';
|
2014-02-22 19:21:32 +01:00
|
|
|
list ($prevPostId, $nextPostId) =
|
|
|
|
PostSearchService::getPostIdsAround(
|
2014-04-29 21:35:29 +02:00
|
|
|
$context->transport->lastSearchQuery, $id);
|
2013-12-01 14:47:35 +01:00
|
|
|
}
|
2013-10-13 12:28:16 +02:00
|
|
|
|
2014-05-03 20:32:47 +02:00
|
|
|
//todo:
|
|
|
|
//move these to PostEntity when implementing ApiController
|
2014-05-01 16:12:37 +02:00
|
|
|
$favorite = Auth::getCurrentUser()->hasFavorited($post);
|
|
|
|
$score = Auth::getCurrentUser()->getScore($post);
|
2013-11-17 20:30:04 +01:00
|
|
|
$flagged = in_array(TextHelper::reprPost($post), SessionHelper::get('flagged', []));
|
|
|
|
|
2014-04-29 21:35:29 +02:00
|
|
|
$context->favorite = $favorite;
|
|
|
|
$context->score = $score;
|
|
|
|
$context->flagged = $flagged;
|
|
|
|
$context->transport->post = $post;
|
|
|
|
$context->transport->prevPostId = $prevPostId ? $prevPostId : null;
|
|
|
|
$context->transport->nextPostId = $nextPostId ? $nextPostId : null;
|
2013-10-07 20:44:14 +02:00
|
|
|
}
|
|
|
|
|
2014-05-03 22:14:00 +02:00
|
|
|
public function fileView($name)
|
2013-10-08 23:02:31 +02:00
|
|
|
{
|
2014-05-03 22:14:00 +02:00
|
|
|
$ret = Api::run(new GetPostContentJob(), [GetPostContentJob::POST_NAME => $name]);
|
2013-10-08 23:02:31 +02:00
|
|
|
|
2014-05-03 22:14:00 +02:00
|
|
|
$context = 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-04-29 21:35:29 +02:00
|
|
|
$context->layoutName = 'layout-file';
|
2013-10-08 23:02:31 +02:00
|
|
|
}
|
|
|
|
|
2014-05-03 22:14:00 +02:00
|
|
|
public function thumbView($name, $width = null, $height = null)
|
2013-10-07 20:44:14 +02:00
|
|
|
{
|
2014-05-03 22:14:00 +02:00
|
|
|
$ret = Api::run(new GetPostThumbJob(), [
|
|
|
|
GetPostThumbJob::POST_NAME => $name,
|
|
|
|
GetPostThumbJob::WIDTH => $width,
|
|
|
|
GetPostThumbJob::HEIGHT => $height]);
|
2013-10-13 14:01:07 +02:00
|
|
|
|
2014-05-03 22:14:00 +02:00
|
|
|
$context = 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-04-29 21:35:29 +02:00
|
|
|
$context->layoutName = 'layout-file';
|
2013-10-05 12:55:03 +02:00
|
|
|
}
|
2014-05-07 21:39:41 +02:00
|
|
|
|
|
|
|
|
|
|
|
protected function splitTags($string)
|
|
|
|
{
|
|
|
|
$tags = trim($string);
|
|
|
|
$tags = preg_split('/[,;\s]+/', $tags);
|
|
|
|
$tags = array_filter($tags, function($x) { return $x != ''; });
|
|
|
|
$tags = array_unique($tags);
|
|
|
|
return $tags;
|
|
|
|
}
|
2013-10-05 12:55:03 +02:00
|
|
|
}
|