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
|
|
|
{
|
2013-10-29 09:04:42 +01:00
|
|
|
public function listAction($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-04-29 21:35:29 +02:00
|
|
|
$url = \Chibi\Router::linkTo(['PostController', 'listAction'], [
|
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);
|
2013-10-05 22:52:55 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-11-18 14:00:54 +01:00
|
|
|
$query = trim($query);
|
2014-01-27 09:17:36 +01:00
|
|
|
$page = max(1, intval($page));
|
2014-04-29 21:35:29 +02:00
|
|
|
$postsPerPage = intval(getConfig()->browsing->postsPerPage);
|
|
|
|
$context->transport->searchQuery = $query;
|
|
|
|
$context->transport->lastSearchQuery = $query;
|
2014-04-29 23:52:17 +02:00
|
|
|
Access::assert(Privilege::ListPosts);
|
2013-10-29 09:04:42 +01:00
|
|
|
if ($source == 'mass-tag')
|
|
|
|
{
|
2014-04-29 23:52:17 +02:00
|
|
|
Access::assert(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-01 16:06:38 +02:00
|
|
|
if (!Access::check(Privilege::MassTag, 'all'))
|
2014-05-01 16:12:37 +02:00
|
|
|
$query = trim($query . ' submit:' . Auth::getCurrentUser()->name);
|
2013-10-29 09:04:42 +01:00
|
|
|
}
|
2013-10-07 20:44:14 +02:00
|
|
|
|
2013-12-18 15:10:53 +01:00
|
|
|
$posts = PostSearchService::getEntities($query, $postsPerPage, $page);
|
2014-02-16 18:18:33 +01:00
|
|
|
$postCount = PostSearchService::getEntityCount($query);
|
2013-10-09 11:45:18 +02:00
|
|
|
$pageCount = ceil($postCount / $postsPerPage);
|
2013-11-30 13:59:29 +01:00
|
|
|
$page = min($pageCount, $page);
|
2013-12-18 15:10:53 +01:00
|
|
|
PostModel::preloadTags($posts);
|
2013-10-09 11:45:18 +02:00
|
|
|
|
2014-04-29 21:35:29 +02:00
|
|
|
$context->transport->paginator = new StdClass;
|
|
|
|
$context->transport->paginator->page = $page;
|
|
|
|
$context->transport->paginator->pageCount = $pageCount;
|
|
|
|
$context->transport->paginator->entityCount = $postCount;
|
|
|
|
$context->transport->paginator->entities = $posts;
|
|
|
|
$context->transport->posts = $posts;
|
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-04-29 21:35:29 +02:00
|
|
|
$context = getContext();
|
2013-12-18 15:10:53 +01:00
|
|
|
$tagName = $tag;
|
|
|
|
$post = PostModel::findByIdOrName($id);
|
2014-04-29 21:35:29 +02:00
|
|
|
$context->transport->post = $post;
|
2013-11-21 21:06:18 +01:00
|
|
|
|
2014-04-30 08:08:24 +02:00
|
|
|
if (!InputHelper::get('submit'))
|
|
|
|
return;
|
2013-10-29 09:04:42 +01:00
|
|
|
|
2014-04-30 08:08:24 +02:00
|
|
|
Access::assert(
|
|
|
|
Privilege::MassTag,
|
|
|
|
Access::getIdentity($post->getUploader()));
|
2013-12-18 15:10:53 +01:00
|
|
|
|
2014-04-30 08:08:24 +02:00
|
|
|
$tags = $post->getTags();
|
2014-04-27 14:42:39 +02:00
|
|
|
|
2014-04-30 08:08:24 +02:00
|
|
|
if (!$enable)
|
|
|
|
{
|
|
|
|
foreach ($tags as $i => $tag)
|
|
|
|
if ($tag->name == $tagName)
|
|
|
|
unset($tags[$i]);
|
2013-12-18 15:10:53 +01:00
|
|
|
|
2014-04-30 08:08:24 +02:00
|
|
|
LogHelper::log('{user} untagged {post} with {tag}', [
|
|
|
|
'post' => TextHelper::reprPost($post),
|
|
|
|
'tag' => TextHelper::reprTag($tag)]);
|
|
|
|
}
|
|
|
|
elseif ($enable)
|
|
|
|
{
|
|
|
|
$tag = TagModel::findByName($tagName, false);
|
|
|
|
if ($tag === null)
|
|
|
|
{
|
|
|
|
$tag = TagModel::spawn();
|
|
|
|
$tag->name = $tagName;
|
|
|
|
TagModel::save($tag);
|
2013-11-16 21:21:43 +01:00
|
|
|
}
|
2013-10-29 09:04:42 +01:00
|
|
|
|
2014-04-30 08:08:24 +02:00
|
|
|
$tags []= $tag;
|
|
|
|
LogHelper::log('{user} tagged {post} with {tag}', [
|
|
|
|
'post' => TextHelper::reprPost($post),
|
|
|
|
'tag' => TextHelper::reprTag($tag)]);
|
2013-10-29 09:04:42 +01:00
|
|
|
}
|
2014-04-30 08:08:24 +02:00
|
|
|
|
|
|
|
$post->setTags($tags);
|
|
|
|
|
|
|
|
PostModel::save($post);
|
|
|
|
StatusHelper::success();
|
2013-10-29 09:04:42 +01:00
|
|
|
}
|
|
|
|
|
2013-10-13 12:28:16 +02:00
|
|
|
public function favoritesAction($page = 1)
|
|
|
|
{
|
|
|
|
$this->listAction('favmin:1', $page);
|
|
|
|
}
|
|
|
|
|
2014-03-03 21:56:10 +01:00
|
|
|
public function upvotedAction($page = 1)
|
|
|
|
{
|
|
|
|
$this->listAction('scoremin:1', $page);
|
|
|
|
}
|
2013-10-13 12:28:16 +02:00
|
|
|
|
2013-10-21 13:13:10 +02:00
|
|
|
public function randomAction($page = 1)
|
|
|
|
{
|
|
|
|
$this->listAction('order:random', $page);
|
|
|
|
}
|
|
|
|
|
2013-10-05 19:24:08 +02:00
|
|
|
public function uploadAction()
|
|
|
|
{
|
2014-04-29 21:35:29 +02:00
|
|
|
$context = getContext();
|
2014-04-29 23:52:17 +02:00
|
|
|
Access::assert(Privilege::UploadPost);
|
2014-04-29 21:35:29 +02:00
|
|
|
if (getConfig()->registration->needEmailForUploading)
|
2014-04-29 23:52:17 +02:00
|
|
|
Access::assertEmailConfirmation();
|
2013-10-07 00:44:17 +02:00
|
|
|
|
2014-04-30 08:08:24 +02:00
|
|
|
if (!InputHelper::get('submit'))
|
|
|
|
return;
|
|
|
|
|
|
|
|
\Chibi\Database::transaction(function() use ($context)
|
2013-10-07 00:44:17 +02:00
|
|
|
{
|
2014-04-30 08:08:24 +02:00
|
|
|
$post = PostModel::spawn();
|
|
|
|
LogHelper::bufferChanges();
|
2013-10-13 12:28:16 +02:00
|
|
|
|
2014-04-30 08:08:24 +02:00
|
|
|
//basic stuff
|
|
|
|
$anonymous = InputHelper::get('anonymous');
|
2014-05-01 16:12:37 +02:00
|
|
|
if (Auth::isLoggedIn() and !$anonymous)
|
|
|
|
$post->setUploader(Auth::getCurrentUser());
|
2013-10-13 12:28:16 +02:00
|
|
|
|
2014-04-30 08:08:24 +02:00
|
|
|
//store the post to get the ID in the logs
|
|
|
|
PostModel::forgeId($post);
|
2013-10-07 00:44:17 +02:00
|
|
|
|
2014-04-30 08:08:24 +02:00
|
|
|
//do the edits
|
|
|
|
$this->doEdit($post, true);
|
2013-11-22 21:20:56 +01:00
|
|
|
|
2014-04-30 08:08:24 +02:00
|
|
|
//this basically means that user didn't specify file nor url
|
|
|
|
if (empty($post->type))
|
|
|
|
throw new SimpleException('No post type detected; upload faled');
|
2013-10-07 00:44:17 +02:00
|
|
|
|
2014-04-30 08:08:24 +02:00
|
|
|
//clean edit log
|
|
|
|
LogHelper::setBuffer([]);
|
2013-11-23 13:35:23 +01:00
|
|
|
|
2014-04-30 08:08:24 +02:00
|
|
|
//log
|
|
|
|
$fmt = ($anonymous and !getConfig()->misc->logAnonymousUploads)
|
|
|
|
? '{anon}'
|
|
|
|
: '{user}';
|
|
|
|
$fmt .= ' added {post} (tags: {tags}, safety: {safety}, source: {source})';
|
|
|
|
LogHelper::log($fmt, [
|
|
|
|
'post' => TextHelper::reprPost($post),
|
|
|
|
'tags' => TextHelper::reprTags($post->getTags()),
|
|
|
|
'safety' => PostSafety::toString($post->safety),
|
|
|
|
'source' => $post->source]);
|
2013-11-22 21:20:56 +01:00
|
|
|
|
2014-04-30 08:08:24 +02:00
|
|
|
//finish
|
|
|
|
LogHelper::flush();
|
|
|
|
PostModel::save($post);
|
|
|
|
});
|
2013-11-16 21:21:43 +01:00
|
|
|
|
2014-04-30 08:08:24 +02:00
|
|
|
StatusHelper::success();
|
2013-10-05 19:24:08 +02:00
|
|
|
}
|
|
|
|
|
2013-10-13 12:28:16 +02:00
|
|
|
public function editAction($id)
|
|
|
|
{
|
2014-04-29 21:35:29 +02:00
|
|
|
$context = getContext();
|
2013-12-18 15:10:53 +01:00
|
|
|
$post = PostModel::findByIdOrName($id);
|
2014-04-29 21:35:29 +02:00
|
|
|
$context->transport->post = $post;
|
2013-10-13 12:28:16 +02:00
|
|
|
|
2014-04-30 08:08:24 +02:00
|
|
|
if (!InputHelper::get('submit'))
|
|
|
|
return;
|
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-04-30 08:08:24 +02:00
|
|
|
LogHelper::bufferChanges();
|
|
|
|
$this->doEdit($post, false);
|
|
|
|
LogHelper::flush();
|
2013-10-30 20:20:01 +01:00
|
|
|
|
2014-04-30 08:08:24 +02:00
|
|
|
PostModel::save($post);
|
|
|
|
TagModel::removeUnused();
|
|
|
|
|
|
|
|
StatusHelper::success();
|
2013-10-13 12:28:16 +02:00
|
|
|
}
|
|
|
|
|
2013-11-17 14:52:46 +01:00
|
|
|
public function flagAction($id)
|
|
|
|
{
|
2013-12-18 15:10:53 +01:00
|
|
|
$post = PostModel::findByIdOrName($id);
|
2014-04-29 23:52:17 +02:00
|
|
|
Access::assert(Privilege::FlagPost, Access::getIdentity($post->getUploader()));
|
2013-11-17 14:52:46 +01:00
|
|
|
|
2014-04-30 08:08:24 +02:00
|
|
|
if (!InputHelper::get('submit'))
|
|
|
|
return;
|
2013-11-17 14:52:46 +01:00
|
|
|
|
2014-04-30 08:08:24 +02:00
|
|
|
$key = TextHelper::reprPost($post);
|
2013-11-17 14:52:46 +01:00
|
|
|
|
2014-04-30 08:08:24 +02:00
|
|
|
$flagged = SessionHelper::get('flagged', []);
|
|
|
|
if (in_array($key, $flagged))
|
|
|
|
throw new SimpleException('You already flagged this post');
|
|
|
|
$flagged []= $key;
|
|
|
|
SessionHelper::set('flagged', $flagged);
|
|
|
|
|
|
|
|
LogHelper::log('{user} flagged {post} for moderator attention', ['post' => TextHelper::reprPost($post)]);
|
|
|
|
StatusHelper::success();
|
2013-11-17 14:52:46 +01:00
|
|
|
}
|
|
|
|
|
2013-10-13 12:28:16 +02:00
|
|
|
public function hideAction($id)
|
|
|
|
{
|
2013-12-18 15:10:53 +01:00
|
|
|
$post = PostModel::findByIdOrName($id);
|
2014-04-29 23:52:17 +02:00
|
|
|
Access::assert(Privilege::HidePost, Access::getIdentity($post->getUploader()));
|
2013-11-16 21:21:43 +01:00
|
|
|
|
2014-04-30 08:08:24 +02:00
|
|
|
if (!InputHelper::get('submit'))
|
|
|
|
return;
|
2013-11-16 21:21:43 +01:00
|
|
|
|
2014-04-30 08:08:24 +02:00
|
|
|
$post->setHidden(true);
|
|
|
|
PostModel::save($post);
|
|
|
|
|
|
|
|
LogHelper::log('{user} hidden {post}', ['post' => TextHelper::reprPost($post)]);
|
|
|
|
StatusHelper::success();
|
2013-10-13 12:28:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function unhideAction($id)
|
|
|
|
{
|
2013-12-18 15:10:53 +01:00
|
|
|
$post = PostModel::findByIdOrName($id);
|
2014-04-29 23:52:17 +02:00
|
|
|
Access::assert(Privilege::HidePost, Access::getIdentity($post->getUploader()));
|
2013-11-16 21:21:43 +01:00
|
|
|
|
2014-04-30 08:08:24 +02:00
|
|
|
if (!InputHelper::get('submit'))
|
|
|
|
return;
|
2013-11-16 21:21:43 +01:00
|
|
|
|
2014-04-30 08:08:24 +02:00
|
|
|
$post->setHidden(false);
|
|
|
|
PostModel::save($post);
|
|
|
|
|
|
|
|
LogHelper::log('{user} unhidden {post}', ['post' => TextHelper::reprPost($post)]);
|
|
|
|
StatusHelper::success();
|
2013-10-13 12:28:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function deleteAction($id)
|
|
|
|
{
|
2013-12-18 15:10:53 +01:00
|
|
|
$post = PostModel::findByIdOrName($id);
|
2014-04-29 23:52:17 +02:00
|
|
|
Access::assert(Privilege::DeletePost, Access::getIdentity($post->getUploader()));
|
2013-11-16 21:21:43 +01:00
|
|
|
|
2014-04-30 08:08:24 +02:00
|
|
|
if (!InputHelper::get('submit'))
|
|
|
|
return;
|
2013-11-16 21:21:43 +01:00
|
|
|
|
2014-04-30 08:08:24 +02:00
|
|
|
PostModel::remove($post);
|
|
|
|
|
|
|
|
LogHelper::log('{user} deleted {post}', ['post' => TextHelper::reprPost($id)]);
|
|
|
|
StatusHelper::success();
|
2013-10-13 12:28:16 +02:00
|
|
|
}
|
|
|
|
|
2013-10-12 14:53:47 +02:00
|
|
|
public function addFavoriteAction($id)
|
|
|
|
{
|
2014-04-29 21:35:29 +02:00
|
|
|
$context = getContext();
|
2013-12-18 15:10:53 +01:00
|
|
|
$post = PostModel::findByIdOrName($id);
|
2014-04-29 23:52:17 +02:00
|
|
|
Access::assert(Privilege::FavoritePost, Access::getIdentity($post->getUploader()));
|
2013-10-12 14:53:47 +02:00
|
|
|
|
2014-04-30 08:08:24 +02:00
|
|
|
if (!InputHelper::get('submit'))
|
|
|
|
return;
|
2013-10-12 14:53:47 +02:00
|
|
|
|
2014-05-01 16:12:37 +02:00
|
|
|
if (!Auth::isLoggedIn())
|
2014-04-30 08:08:24 +02:00
|
|
|
throw new SimpleException('Not logged in');
|
|
|
|
|
2014-05-01 16:12:37 +02:00
|
|
|
UserModel::updateUserScore(Auth::getCurrentUser(), $post, 1);
|
|
|
|
UserModel::addToUserFavorites(Auth::getCurrentUser(), $post);
|
2014-04-30 08:08:24 +02:00
|
|
|
StatusHelper::success();
|
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-04-29 21:35:29 +02:00
|
|
|
$context = getContext();
|
2013-12-18 15:10:53 +01:00
|
|
|
$post = PostModel::findByIdOrName($id);
|
2014-04-29 23:52:17 +02:00
|
|
|
Access::assert(Privilege::FavoritePost, Access::getIdentity($post->getUploader()));
|
2013-10-12 14:53:47 +02:00
|
|
|
|
2014-04-30 08:08:24 +02:00
|
|
|
if (!InputHelper::get('submit'))
|
|
|
|
return;
|
2013-10-12 14:53:47 +02:00
|
|
|
|
2014-05-01 16:12:37 +02:00
|
|
|
if (!Auth::isLoggedIn())
|
2014-04-30 08:08:24 +02:00
|
|
|
throw new SimpleException('Not logged in');
|
|
|
|
|
2014-05-01 16:12:37 +02:00
|
|
|
UserModel::removeFromUserFavorites(Auth::getCurrentUser(), $post);
|
2014-04-30 08:08:24 +02:00
|
|
|
StatusHelper::success();
|
2013-10-12 14:53:47 +02:00
|
|
|
}
|
|
|
|
|
2013-11-10 12:23:59 +01:00
|
|
|
public function scoreAction($id, $score)
|
|
|
|
{
|
2014-04-29 21:35:29 +02:00
|
|
|
$context = getContext();
|
2013-12-18 15:10:53 +01:00
|
|
|
$post = PostModel::findByIdOrName($id);
|
2014-04-29 23:52:17 +02:00
|
|
|
Access::assert(Privilege::ScorePost, Access::getIdentity($post->getUploader()));
|
2013-11-10 12:23:59 +01:00
|
|
|
|
2014-04-30 08:08:24 +02:00
|
|
|
if (!InputHelper::get('submit'))
|
|
|
|
return;
|
2013-11-10 12:23:59 +01:00
|
|
|
|
2014-05-01 16:12:37 +02:00
|
|
|
if (!Auth::isLoggedIn())
|
2014-04-30 08:08:24 +02:00
|
|
|
throw new SimpleException('Not logged in');
|
|
|
|
|
2014-05-01 16:12:37 +02:00
|
|
|
UserModel::updateUserScore(Auth::getCurrentUser(), $post, $score);
|
2014-04-30 08:08:24 +02:00
|
|
|
StatusHelper::success();
|
2013-11-10 12:23:59 +01:00
|
|
|
}
|
|
|
|
|
2013-10-19 13:38:20 +02:00
|
|
|
public function featureAction($id)
|
|
|
|
{
|
2014-04-29 21:35:29 +02:00
|
|
|
$context = getContext();
|
2013-12-18 15:10:53 +01:00
|
|
|
$post = PostModel::findByIdOrName($id);
|
2014-04-29 23:52:17 +02:00
|
|
|
Access::assert(Privilege::FeaturePost, Access::getIdentity($post->getUploader()));
|
2013-12-18 15:10:53 +01:00
|
|
|
PropertyModel::set(PropertyModel::FeaturedPostId, $post->id);
|
|
|
|
PropertyModel::set(PropertyModel::FeaturedPostDate, time());
|
2014-05-01 16:12:37 +02:00
|
|
|
PropertyModel::set(PropertyModel::FeaturedPostUserName, Auth::getCurrentUser()->name);
|
2013-11-16 18:40:26 +01:00
|
|
|
StatusHelper::success();
|
2013-11-22 23:32:56 +01:00
|
|
|
LogHelper::log('{user} featured {post} on main page', ['post' => TextHelper::reprPost($post)]);
|
2013-10-19 13:38:20 +02:00
|
|
|
}
|
|
|
|
|
2013-10-07 20:44:14 +02:00
|
|
|
public function viewAction($id)
|
2013-10-05 19:24:08 +02:00
|
|
|
{
|
2014-04-29 21:35:29 +02:00
|
|
|
$context = getContext();
|
2013-12-18 15:10:53 +01:00
|
|
|
$post = PostModel::findByIdOrName($id);
|
|
|
|
CommentModel::preloadCommenters($post->getComments());
|
2013-10-12 10:46:15 +02:00
|
|
|
|
2013-10-13 12:28:16 +02:00
|
|
|
if ($post->hidden)
|
2014-04-29 23:52:17 +02:00
|
|
|
Access::assert(Privilege::ViewPost, 'hidden');
|
|
|
|
Access::assert(Privilege::ViewPost);
|
|
|
|
Access::assert(Privilege::ViewPost, PostSafety::toString($post->safety));
|
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-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
|
|
|
}
|
|
|
|
|
2013-11-18 14:33:43 +01:00
|
|
|
public function thumbAction($name, $width = null, $height = null)
|
2013-10-08 23:02:31 +02:00
|
|
|
{
|
2014-04-29 21:35:29 +02:00
|
|
|
$context = getContext();
|
2013-12-18 15:10:53 +01:00
|
|
|
$path = PostModel::getThumbCustomPath($name, $width, $height);
|
2013-10-08 23:02:31 +02:00
|
|
|
if (!file_exists($path))
|
|
|
|
{
|
2013-12-18 15:10:53 +01:00
|
|
|
$path = PostModel::getThumbDefaultPath($name, $width, $height);
|
2013-11-22 21:20:56 +01:00
|
|
|
if (!file_exists($path))
|
2013-10-08 23:02:31 +02:00
|
|
|
{
|
2013-12-18 15:10:53 +01:00
|
|
|
$post = PostModel::findByIdOrName($name);
|
2014-04-29 23:52:17 +02:00
|
|
|
Access::assert(Privilege::ListPosts);
|
|
|
|
Access::assert(Privilege::ListPosts, PostSafety::toString($post->safety));
|
2014-04-30 09:54:04 +02:00
|
|
|
$post->generateThumb($width, $height);
|
2013-11-22 21:20:56 +01:00
|
|
|
if (!file_exists($path))
|
2014-04-29 21:35:29 +02:00
|
|
|
{
|
|
|
|
$path = getConfig()->main->mediaPath . DS . 'img' . DS . 'thumb.jpg';
|
|
|
|
$path = TextHelper::absolutePath($path);
|
|
|
|
}
|
2013-10-08 23:02:31 +02:00
|
|
|
}
|
|
|
|
}
|
2013-11-22 21:20:56 +01:00
|
|
|
|
2013-10-08 23:02:31 +02:00
|
|
|
if (!is_readable($path))
|
|
|
|
throw new SimpleException('Thumbnail file is not readable');
|
|
|
|
|
2014-04-29 21:35:29 +02:00
|
|
|
$context->layoutName = 'layout-file';
|
|
|
|
$context->transport->cacheDaysToLive = 365;
|
|
|
|
$context->transport->mimeType = 'image/jpeg';
|
|
|
|
$context->transport->fileHash = 'thumb' . md5($name . filemtime($path));
|
|
|
|
$context->transport->filePath = $path;
|
2013-10-08 23:02:31 +02:00
|
|
|
}
|
|
|
|
|
2013-10-07 23:17:33 +02:00
|
|
|
public function retrieveAction($name)
|
2013-10-07 20:44:14 +02:00
|
|
|
{
|
2013-12-18 15:10:53 +01:00
|
|
|
$post = PostModel::findByName($name, true);
|
2014-04-29 21:35:29 +02:00
|
|
|
$config = getConfig();
|
|
|
|
$context = getContext();
|
2013-10-07 20:44:14 +02:00
|
|
|
|
2014-04-29 23:52:17 +02:00
|
|
|
Access::assert(Privilege::RetrievePost);
|
|
|
|
Access::assert(Privilege::RetrievePost, PostSafety::toString($post->safety));
|
2013-10-07 20:44:14 +02:00
|
|
|
|
2014-04-29 21:35:29 +02:00
|
|
|
$path = $config->main->filesPath . DS . $post->name;
|
|
|
|
$path = TextHelper::absolutePath($path);
|
2013-10-07 20:44:14 +02:00
|
|
|
if (!file_exists($path))
|
2014-02-05 08:32:19 +01:00
|
|
|
throw new SimpleNotFoundException('Post file does not exist');
|
2013-10-07 20:44:14 +02:00
|
|
|
if (!is_readable($path))
|
|
|
|
throw new SimpleException('Post file is not readable');
|
|
|
|
|
2013-10-13 13:37:18 +02:00
|
|
|
$fn = sprintf('%s_%s_%s.%s',
|
2014-04-29 21:35:29 +02:00
|
|
|
$config->main->title,
|
2013-11-21 21:06:18 +01:00
|
|
|
$post->id,
|
2013-12-18 15:10:53 +01:00
|
|
|
join(',', array_map(function($tag) { return $tag->name; }, $post->getTags())),
|
2014-02-17 23:15:10 +01:00
|
|
|
TextHelper::resolveMimeType($post->mimeType) ?: 'dat');
|
2013-10-13 13:37:18 +02:00
|
|
|
$fn = preg_replace('/[[:^print:]]/', '', $fn);
|
|
|
|
|
2013-10-19 13:00:03 +02:00
|
|
|
$ttl = 60 * 60 * 24 * 14;
|
2013-10-13 14:01:07 +02:00
|
|
|
|
2014-04-29 21:35:29 +02:00
|
|
|
$context->layoutName = 'layout-file';
|
|
|
|
$context->transport->cacheDaysToLive = 14;
|
|
|
|
$context->transport->customFileName = $fn;
|
|
|
|
$context->transport->mimeType = $post->mimeType;
|
|
|
|
$context->transport->fileHash = 'post' . $post->fileHash;
|
|
|
|
$context->transport->filePath = $path;
|
2013-10-05 12:55:03 +02:00
|
|
|
}
|
2013-11-22 21:20:56 +01:00
|
|
|
|
|
|
|
private function doEdit($post, $isNew)
|
|
|
|
{
|
|
|
|
/* safety */
|
|
|
|
$suppliedSafety = InputHelper::get('safety');
|
|
|
|
if ($suppliedSafety !== null)
|
|
|
|
{
|
|
|
|
if (!$isNew)
|
2014-04-29 23:52:17 +02:00
|
|
|
Access::assert(Privilege::EditPostSafety, Access::getIdentity($post->getUploader()));
|
2013-11-22 21:20:56 +01:00
|
|
|
|
|
|
|
$oldSafety = $post->safety;
|
|
|
|
$post->setSafety($suppliedSafety);
|
|
|
|
$newSafety = $post->safety;
|
|
|
|
|
|
|
|
if ($oldSafety != $newSafety)
|
2014-04-30 00:11:13 +02:00
|
|
|
{
|
|
|
|
LogHelper::log('{user} changed safety of {post} to {safety}', [
|
|
|
|
'post' => TextHelper::reprPost($post),
|
|
|
|
'safety' => PostSafety::toString($post->safety)]);
|
|
|
|
}
|
2013-11-22 21:20:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* tags */
|
|
|
|
$suppliedTags = InputHelper::get('tags');
|
|
|
|
if ($suppliedTags !== null)
|
|
|
|
{
|
|
|
|
if (!$isNew)
|
2014-04-29 23:52:17 +02:00
|
|
|
Access::assert(Privilege::EditPostTags, Access::getIdentity($post->getUploader()));
|
2013-11-22 21:20:56 +01:00
|
|
|
|
2013-12-18 15:10:53 +01:00
|
|
|
$oldTags = array_map(function($tag) { return $tag->name; }, $post->getTags());
|
2013-11-22 21:20:56 +01:00
|
|
|
$post->setTagsFromText($suppliedTags);
|
2013-12-18 15:10:53 +01:00
|
|
|
$newTags = array_map(function($tag) { return $tag->name; }, $post->getTags());
|
2013-11-22 21:20:56 +01:00
|
|
|
|
|
|
|
foreach (array_diff($oldTags, $newTags) as $tag)
|
2014-04-30 00:11:13 +02:00
|
|
|
{
|
|
|
|
LogHelper::log('{user} untagged {post} with {tag}', [
|
|
|
|
'post' => TextHelper::reprPost($post),
|
|
|
|
'tag' => TextHelper::reprTag($tag)]);
|
|
|
|
}
|
2013-11-22 21:20:56 +01:00
|
|
|
|
|
|
|
foreach (array_diff($newTags, $oldTags) as $tag)
|
2014-04-30 00:11:13 +02:00
|
|
|
{
|
|
|
|
LogHelper::log('{user} tagged {post} with {tag}', [
|
|
|
|
'post' => TextHelper::reprPost($post),
|
|
|
|
'tag' => TextHelper::reprTag($tag)]);
|
|
|
|
}
|
2013-11-22 21:20:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* source */
|
|
|
|
$suppliedSource = InputHelper::get('source');
|
|
|
|
if ($suppliedSource !== null)
|
|
|
|
{
|
|
|
|
if (!$isNew)
|
2014-04-29 23:52:17 +02:00
|
|
|
Access::assert(Privilege::EditPostSource, Access::getIdentity($post->getUploader()));
|
2013-11-22 21:20:56 +01:00
|
|
|
|
|
|
|
$oldSource = $post->source;
|
|
|
|
$post->setSource($suppliedSource);
|
|
|
|
$newSource = $post->source;
|
|
|
|
|
|
|
|
if ($oldSource != $newSource)
|
2014-04-30 00:11:13 +02:00
|
|
|
{
|
|
|
|
LogHelper::log('{user} changed source of {post} to {source}', [
|
|
|
|
'post' => TextHelper::reprPost($post),
|
|
|
|
'source' => $post->source]);
|
|
|
|
}
|
2013-11-22 21:20:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* relations */
|
|
|
|
$suppliedRelations = InputHelper::get('relations');
|
|
|
|
if ($suppliedRelations !== null)
|
|
|
|
{
|
|
|
|
if (!$isNew)
|
2014-04-29 23:52:17 +02:00
|
|
|
Access::assert(Privilege::EditPostRelations, Access::getIdentity($post->getUploader()));
|
2013-11-22 21:20:56 +01:00
|
|
|
|
2013-12-18 15:10:53 +01:00
|
|
|
$oldRelatedIds = array_map(function($post) { return $post->id; }, $post->getRelations());
|
2013-11-22 21:20:56 +01:00
|
|
|
$post->setRelationsFromText($suppliedRelations);
|
2013-12-18 15:10:53 +01:00
|
|
|
$newRelatedIds = array_map(function($post) { return $post->id; }, $post->getRelations());
|
2013-11-22 21:20:56 +01:00
|
|
|
|
|
|
|
foreach (array_diff($oldRelatedIds, $newRelatedIds) as $post2id)
|
2014-04-30 00:11:13 +02:00
|
|
|
{
|
|
|
|
LogHelper::log('{user} removed relation between {post} and {post2}', [
|
|
|
|
'post' => TextHelper::reprPost($post),
|
|
|
|
'post2' => TextHelper::reprPost($post2id)]);
|
|
|
|
}
|
2013-11-22 21:20:56 +01:00
|
|
|
|
|
|
|
foreach (array_diff($newRelatedIds, $oldRelatedIds) as $post2id)
|
2014-04-30 00:11:13 +02:00
|
|
|
{
|
|
|
|
LogHelper::log('{user} added relation between {post} and {post2}', [
|
|
|
|
'post' => TextHelper::reprPost($post),
|
|
|
|
'post2' => TextHelper::reprPost($post2id)]);
|
|
|
|
}
|
2013-11-22 21:20:56 +01:00
|
|
|
}
|
|
|
|
|
2014-04-30 08:08:24 +02:00
|
|
|
/* file contents */
|
|
|
|
if (!empty($_FILES['file']['name']))
|
|
|
|
{
|
|
|
|
if (!$isNew)
|
|
|
|
Access::assert(Privilege::EditPostFile, Access::getIdentity($post->getUploader()));
|
|
|
|
|
|
|
|
$suppliedFile = $_FILES['file'];
|
|
|
|
TransferHelper::handleUploadErrors($suppliedFile);
|
|
|
|
|
|
|
|
$post->setContentFromPath($suppliedFile['tmp_name'], $suppliedFile['name']);
|
|
|
|
|
|
|
|
if (!$isNew)
|
|
|
|
LogHelper::log('{user} changed contents of {post}', ['post' => TextHelper::reprPost($post)]);
|
|
|
|
}
|
|
|
|
elseif (InputHelper::get('url'))
|
|
|
|
{
|
|
|
|
if (!$isNew)
|
|
|
|
Access::assert(Privilege::EditPostFile, Access::getIdentity($post->getUploader()));
|
|
|
|
|
|
|
|
$url = InputHelper::get('url');
|
|
|
|
$post->setContentFromUrl($url);
|
|
|
|
|
|
|
|
if (!$isNew)
|
|
|
|
LogHelper::log('{user} changed contents of {post}', ['post' => TextHelper::reprPost($post)]);
|
|
|
|
}
|
|
|
|
|
2013-11-22 21:20:56 +01:00
|
|
|
/* thumbnail */
|
|
|
|
if (!empty($_FILES['thumb']['name']))
|
|
|
|
{
|
|
|
|
if (!$isNew)
|
2014-04-29 23:52:17 +02:00
|
|
|
Access::assert(Privilege::EditPostThumb, Access::getIdentity($post->getUploader()));
|
2013-11-22 21:20:56 +01:00
|
|
|
|
|
|
|
$suppliedFile = $_FILES['thumb'];
|
2014-04-30 08:08:24 +02:00
|
|
|
TransferHelper::handleUploadErrors($suppliedFile);
|
2013-11-22 21:20:56 +01:00
|
|
|
|
2014-04-30 08:08:24 +02:00
|
|
|
$post->setCustomThumbnailFromPath($srcPath = $suppliedFile['tmp_name']);
|
2013-11-22 21:20:56 +01:00
|
|
|
|
2013-11-23 10:39:41 +01:00
|
|
|
LogHelper::log('{user} changed thumb of {post}', ['post' => TextHelper::reprPost($post)]);
|
2013-11-22 21:20:56 +01:00
|
|
|
}
|
|
|
|
}
|
2013-10-05 12:55:03 +02:00
|
|
|
}
|