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-07 00:44:17 +02:00
|
|
|
public function workWrapper($callback)
|
|
|
|
{
|
2013-10-09 21:02:54 +02:00
|
|
|
$this->context->stylesheets []= '../lib/tagit/jquery.tagit.css';
|
|
|
|
$this->context->scripts []= '../lib/tagit/jquery.tagit.js';
|
2013-10-07 00:44:17 +02:00
|
|
|
$callback();
|
|
|
|
}
|
|
|
|
|
2013-11-22 21:20:56 +01:00
|
|
|
private static function serializePost($post)
|
2013-10-13 12:28:16 +02:00
|
|
|
{
|
|
|
|
$x = [];
|
|
|
|
foreach ($post->sharedTag as $tag)
|
2013-11-22 21:20:56 +01:00
|
|
|
$x []= TextHelper::reprTag($tag->name);
|
|
|
|
foreach ($post->via('crossref')->sharedPost as $relatedPost)
|
|
|
|
$x []= TextHelper::reprPost($relatedPost);
|
|
|
|
$x []= $post->safety;
|
|
|
|
$x []= $post->source;
|
|
|
|
$x []= $post->file_hash;
|
2013-10-13 12:28:16 +02:00
|
|
|
natcasesort($x);
|
2013-11-22 21:20:56 +01:00
|
|
|
$x = join(' ', $x);
|
2013-10-13 12:28:16 +02:00
|
|
|
return md5($x);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static function handleUploadErrors($file)
|
|
|
|
{
|
|
|
|
switch ($file['error'])
|
|
|
|
{
|
|
|
|
case UPLOAD_ERR_OK:
|
|
|
|
break;
|
|
|
|
case UPLOAD_ERR_INI_SIZE:
|
|
|
|
throw new SimpleException('File is too big (maximum size allowed: ' . ini_get('upload_max_filesize') . ')');
|
|
|
|
case UPLOAD_ERR_FORM_SIZE:
|
|
|
|
throw new SimpleException('File is too big than it was allowed in HTML form');
|
|
|
|
case UPLOAD_ERR_PARTIAL:
|
|
|
|
throw new SimpleException('File transfer was interrupted');
|
|
|
|
case UPLOAD_ERR_NO_FILE:
|
|
|
|
throw new SimpleException('No file was uploaded');
|
|
|
|
case UPLOAD_ERR_NO_TMP_DIR:
|
|
|
|
throw new SimpleException('Server misconfiguration error: missing temporary folder');
|
|
|
|
case UPLOAD_ERR_CANT_WRITE:
|
|
|
|
throw new SimpleException('Server misconfiguration error: cannot write to disk');
|
|
|
|
case UPLOAD_ERR_EXTENSION:
|
|
|
|
throw new SimpleException('Server misconfiguration error: upload was canceled by an extension');
|
|
|
|
default:
|
|
|
|
throw new SimpleException('Generic file upload error (id: ' . $file['error'] . ')');
|
|
|
|
}
|
|
|
|
if (!is_uploaded_file($file['tmp_name']))
|
|
|
|
throw new SimpleException('Generic file upload error');
|
|
|
|
}
|
|
|
|
|
2013-10-08 23:02:31 +02:00
|
|
|
|
|
|
|
|
2013-10-05 12:55:03 +02:00
|
|
|
/**
|
2013-10-29 09:04:42 +01:00
|
|
|
* @route /{source}
|
|
|
|
* @route /{source}/{page}
|
|
|
|
* @route /{source}/{query}/
|
|
|
|
* @route /{source}/{query}/{page}
|
|
|
|
* @route /{source}/{additionalInfo}/{query}/
|
|
|
|
* @route /{source}/{additionalInfo}/{query}/{page}
|
|
|
|
* @validate source posts|mass-tag
|
2013-10-09 11:45:18 +02:00
|
|
|
* @validate page \d*
|
|
|
|
* @validate query [^\/]*
|
2013-10-29 09:04:42 +01:00
|
|
|
* @validate additionalInfo [^\/]*
|
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
|
|
|
{
|
2013-11-24 21:50:46 +01:00
|
|
|
$this->context->viewName = 'post-list-wrapper';
|
2013-10-17 22:57:32 +02:00
|
|
|
$this->context->stylesheets []= 'post-small.css';
|
2013-10-09 11:45:18 +02:00
|
|
|
$this->context->stylesheets []= 'post-list.css';
|
2013-10-10 00:12:27 +02:00
|
|
|
$this->context->stylesheets []= 'paginator.css';
|
2013-11-24 21:50:46 +01:00
|
|
|
$this->context->scripts []= 'post-list.js';
|
2013-10-22 00:17:06 +02:00
|
|
|
if ($this->context->user->hasEnabledEndlessScrolling())
|
2013-10-10 00:12:27 +02:00
|
|
|
$this->context->scripts []= 'paginator-endless.js';
|
2013-10-29 09:04:42 +01:00
|
|
|
$this->context->source = $source;
|
|
|
|
$this->context->additionalInfo = $additionalInfo;
|
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
|
|
|
{
|
2013-10-13 22:20:06 +02:00
|
|
|
$this->context->transport->searchQuery = $formQuery;
|
2013-11-24 21:50:46 +01:00
|
|
|
$this->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');
|
2013-11-18 14:00:54 +01:00
|
|
|
$url = \Chibi\UrlHelper::route('post', 'list', ['source' => $source, 'additionalInfo' => $additionalInfo, 'query' => $formQuery]);
|
2013-10-05 22:52:55 +02:00
|
|
|
\Chibi\UrlHelper::forward($url);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-11-18 14:00:54 +01:00
|
|
|
$query = trim($query);
|
2013-10-09 11:45:18 +02:00
|
|
|
$page = intval($page);
|
|
|
|
$postsPerPage = intval($this->config->browsing->postsPerPage);
|
2013-10-20 00:36:50 +02:00
|
|
|
$this->context->subTitle = 'posts';
|
2013-10-13 13:17:23 +02:00
|
|
|
$this->context->transport->searchQuery = $query;
|
2013-11-24 21:50:46 +01:00
|
|
|
$this->context->transport->lastSearchQuery = $query;
|
2013-10-18 00:09:50 +02:00
|
|
|
PrivilegesHelper::confirmWithException(Privilege::ListPosts);
|
2013-10-29 09:04:42 +01:00
|
|
|
if ($source == 'mass-tag')
|
|
|
|
{
|
|
|
|
PrivilegesHelper::confirmWithException(Privilege::MassTag);
|
|
|
|
$this->context->massTagTag = $additionalInfo;
|
|
|
|
$this->context->massTagQuery = $query;
|
|
|
|
}
|
2013-10-07 20:44:14 +02:00
|
|
|
|
2013-10-28 11:19:15 +01:00
|
|
|
$postCount = Model_Post::getEntityCount($query);
|
2013-10-09 11:45:18 +02:00
|
|
|
$pageCount = ceil($postCount / $postsPerPage);
|
2013-10-13 22:20:06 +02:00
|
|
|
$page = max(1, min($pageCount, $page));
|
2013-11-30 00:10:31 +01:00
|
|
|
$posts = Model_Post::getEntitiesFast($query, $postsPerPage, $page);
|
2013-11-30 00:53:09 +01:00
|
|
|
R::preload($posts, 'sharedTag');
|
2013-10-09 11:45:18 +02:00
|
|
|
|
2013-10-16 13:07:01 +02:00
|
|
|
$this->context->transport->paginator = new StdClass;
|
|
|
|
$this->context->transport->paginator->page = $page;
|
|
|
|
$this->context->transport->paginator->pageCount = $pageCount;
|
|
|
|
$this->context->transport->paginator->entityCount = $postCount;
|
|
|
|
$this->context->transport->paginator->entities = $posts;
|
2013-10-07 20:44:14 +02:00
|
|
|
$this->context->transport->posts = $posts;
|
2013-10-05 19:24:08 +02:00
|
|
|
}
|
|
|
|
|
2013-10-08 23:02:31 +02:00
|
|
|
|
|
|
|
|
2013-10-29 09:04:42 +01:00
|
|
|
/**
|
2013-11-25 11:59:59 +01:00
|
|
|
* @route /post/{id}/toggle-tag/{tag}/{enable}
|
2013-10-29 09:04:42 +01:00
|
|
|
* @validate tag [^\/]*
|
2013-11-25 11:59:59 +01:00
|
|
|
* @validate enable 0|1
|
2013-10-29 09:04:42 +01:00
|
|
|
*/
|
2013-11-25 11:59:59 +01:00
|
|
|
public function toggleTagAction($id, $tag, $enable)
|
2013-10-29 09:04:42 +01:00
|
|
|
{
|
|
|
|
$post = Model_Post::locate($id);
|
|
|
|
$this->context->transport->post = $post;
|
2013-11-21 21:06:18 +01:00
|
|
|
|
|
|
|
$tagRow = Model_Tag::locate($tag, false);
|
|
|
|
if ($tagRow !== null)
|
|
|
|
$tag = $tagRow->name;
|
2013-10-29 09:04:42 +01:00
|
|
|
|
|
|
|
if (InputHelper::get('submit'))
|
|
|
|
{
|
|
|
|
PrivilegesHelper::confirmWithException(Privilege::MassTag);
|
|
|
|
$tags = array_map(function($x) { return $x->name; }, $post->sharedTag);
|
|
|
|
|
2013-11-25 11:59:59 +01:00
|
|
|
if (!$enable and in_array($tag, $tags))
|
2013-11-16 21:21:43 +01:00
|
|
|
{
|
2013-10-29 09:04:42 +01:00
|
|
|
$tags = array_diff($tags, [$tag]);
|
2013-11-22 23:32:56 +01:00
|
|
|
LogHelper::log('{user} untagged {post} with {tag}', ['post' => TextHelper::reprPost($post), 'tag' => TextHelper::reprTag($tag)]);
|
2013-11-16 21:21:43 +01:00
|
|
|
}
|
2013-11-25 11:59:59 +01:00
|
|
|
elseif ($enable)
|
2013-11-16 21:21:43 +01:00
|
|
|
{
|
2013-10-29 09:04:42 +01:00
|
|
|
$tags += [$tag];
|
2013-11-22 23:32:56 +01:00
|
|
|
LogHelper::log('{user} tagged {post} with {tag}', ['post' => TextHelper::reprPost($post), 'tag' => TextHelper::reprTag($tag)]);
|
2013-11-16 21:21:43 +01:00
|
|
|
}
|
2013-10-29 09:04:42 +01:00
|
|
|
|
|
|
|
$dbTags = Model_Tag::insertOrUpdate($tags);
|
|
|
|
$post->sharedTag = $dbTags;
|
|
|
|
|
2013-11-22 21:20:56 +01:00
|
|
|
Model_Post::save($post);
|
2013-11-16 18:40:26 +01:00
|
|
|
StatusHelper::success();
|
2013-10-29 09:04:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-10-13 12:28:16 +02:00
|
|
|
/**
|
|
|
|
* @route /favorites
|
|
|
|
* @route /favorites/{page}
|
|
|
|
* @validate page \d*
|
|
|
|
*/
|
|
|
|
public function favoritesAction($page = 1)
|
|
|
|
{
|
|
|
|
$this->listAction('favmin:1', $page);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-10-21 13:13:10 +02:00
|
|
|
/**
|
|
|
|
* @route /random
|
|
|
|
* @route /random/{page}
|
|
|
|
* @validate page \d*
|
|
|
|
*/
|
|
|
|
public function randomAction($page = 1)
|
|
|
|
{
|
|
|
|
$this->listAction('order:random', $page);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-10-05 19:24:08 +02:00
|
|
|
/**
|
|
|
|
* @route /post/upload
|
|
|
|
*/
|
|
|
|
public function uploadAction()
|
|
|
|
{
|
2013-10-07 00:44:17 +02:00
|
|
|
$this->context->stylesheets []= 'upload.css';
|
|
|
|
$this->context->scripts []= 'upload.js';
|
2013-10-05 21:22:28 +02:00
|
|
|
$this->context->subTitle = 'upload';
|
2013-10-18 00:09:50 +02:00
|
|
|
PrivilegesHelper::confirmWithException(Privilege::UploadPost);
|
2013-10-16 18:07:23 +02:00
|
|
|
if ($this->config->registration->needEmailForUploading)
|
|
|
|
PrivilegesHelper::confirmEmail($this->context->user);
|
2013-10-07 00:44:17 +02:00
|
|
|
|
2013-10-21 23:50:30 +02:00
|
|
|
if (InputHelper::get('submit'))
|
2013-10-07 00:44:17 +02:00
|
|
|
{
|
2013-11-22 21:20:56 +01:00
|
|
|
R::transaction(function()
|
2013-10-25 09:40:33 +02:00
|
|
|
{
|
2013-11-22 21:20:56 +01:00
|
|
|
$post = Model_Post::create();
|
2013-11-23 13:35:23 +01:00
|
|
|
LogHelper::bufferChanges();
|
2013-10-13 12:28:16 +02:00
|
|
|
|
2013-11-22 21:20:56 +01:00
|
|
|
//basic stuff
|
|
|
|
$anonymous = InputHelper::get('anonymous');
|
|
|
|
if ($this->context->loggedIn and !$anonymous)
|
|
|
|
$post->uploader = $this->context->user;
|
2013-10-13 12:28:16 +02:00
|
|
|
|
2013-11-22 21:20:56 +01:00
|
|
|
//store the post to get the ID in the logs
|
|
|
|
Model_Post::save($post);
|
2013-10-07 00:44:17 +02:00
|
|
|
|
2013-11-23 13:35:23 +01:00
|
|
|
//do the edits
|
2013-11-22 21:20:56 +01:00
|
|
|
$this->doEdit($post, true);
|
|
|
|
|
|
|
|
//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
|
|
|
|
2013-11-23 13:35:23 +01:00
|
|
|
//clean edit log
|
|
|
|
LogHelper::setBuffer([]);
|
|
|
|
|
|
|
|
//log
|
|
|
|
$fmt = ($anonymous and !$this->config->misc->logAnonymousUploads)
|
|
|
|
? '{anon}'
|
|
|
|
: '{user}';
|
|
|
|
$fmt .= ' added {post} (tags: {tags}, safety: {safety}, source: {source})';
|
|
|
|
LogHelper::log($fmt, [
|
|
|
|
'post' => TextHelper::reprPost($post),
|
|
|
|
'tags' => join(', ', array_map(['TextHelper', 'reprTag'], $post->sharedTag)),
|
|
|
|
'safety' => PostSafety::toString($post->safety),
|
|
|
|
'source' => $post->source]);
|
2013-11-22 21:20:56 +01:00
|
|
|
|
|
|
|
//finish
|
2013-11-23 13:35:23 +01:00
|
|
|
LogHelper::flush();
|
2013-11-22 21:20:56 +01:00
|
|
|
Model_Post::save($post);
|
|
|
|
});
|
2013-11-16 21:21:43 +01:00
|
|
|
|
2013-11-16 18:40:26 +01:00
|
|
|
StatusHelper::success();
|
2013-10-07 00:44:17 +02:00
|
|
|
}
|
2013-10-05 19:24:08 +02:00
|
|
|
}
|
|
|
|
|
2013-10-13 12:28:16 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
2013-10-17 22:57:32 +02:00
|
|
|
* @route /post/{id}/edit
|
2013-10-13 12:28:16 +02:00
|
|
|
*/
|
|
|
|
public function editAction($id)
|
|
|
|
{
|
2013-10-17 22:57:32 +02:00
|
|
|
$post = Model_Post::locate($id);
|
2013-10-15 13:14:48 +02:00
|
|
|
$this->context->transport->post = $post;
|
2013-10-13 12:28:16 +02:00
|
|
|
|
2013-10-21 23:50:30 +02:00
|
|
|
if (InputHelper::get('submit'))
|
2013-10-13 12:28:16 +02:00
|
|
|
{
|
2013-11-22 21:20:56 +01:00
|
|
|
$editToken = InputHelper::get('edit-token');
|
|
|
|
if ($editToken != self::serializePost($post))
|
|
|
|
throw new SimpleException('This post was already edited by someone else in the meantime');
|
2013-10-13 12:28:16 +02:00
|
|
|
|
2013-11-22 21:20:56 +01:00
|
|
|
LogHelper::bufferChanges();
|
|
|
|
$this->doEdit($post, false);
|
|
|
|
LogHelper::flush();
|
2013-10-13 12:28:16 +02:00
|
|
|
|
2013-11-22 21:20:56 +01:00
|
|
|
Model_Post::save($post);
|
2013-11-01 20:51:19 +01:00
|
|
|
Model_Tag::removeUnused();
|
2013-10-30 20:20:01 +01:00
|
|
|
|
2013-11-16 18:40:26 +01:00
|
|
|
StatusHelper::success();
|
2013-10-21 23:50:30 +02:00
|
|
|
}
|
2013-10-13 12:28:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-11-17 14:52:46 +01:00
|
|
|
/**
|
|
|
|
* @route /post/{id}/flag
|
|
|
|
*/
|
|
|
|
public function flagAction($id)
|
|
|
|
{
|
|
|
|
$post = Model_Post::locate($id);
|
|
|
|
PrivilegesHelper::confirmWithException(Privilege::FlagPost);
|
|
|
|
|
|
|
|
if (InputHelper::get('submit'))
|
|
|
|
{
|
2013-11-17 20:30:04 +01:00
|
|
|
$key = TextHelper::reprPost($post);
|
2013-11-17 14:52:46 +01:00
|
|
|
|
2013-11-17 20:30:04 +01:00
|
|
|
$flagged = SessionHelper::get('flagged', []);
|
|
|
|
if (in_array($key, $flagged))
|
2013-11-17 14:52:46 +01:00
|
|
|
throw new SimpleException('You already flagged this post');
|
2013-11-17 20:30:04 +01:00
|
|
|
$flagged []= $key;
|
|
|
|
SessionHelper::set('flagged', $flagged);
|
2013-11-17 14:52:46 +01:00
|
|
|
|
2013-11-22 23:32:56 +01:00
|
|
|
LogHelper::log('{user} flagged {post} for moderator attention', ['post' => TextHelper::reprPost($post)]);
|
2013-11-17 14:52:46 +01:00
|
|
|
StatusHelper::success();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-10-13 12:28:16 +02:00
|
|
|
/**
|
2013-10-17 22:57:32 +02:00
|
|
|
* @route /post/{id}/hide
|
2013-10-13 12:28:16 +02:00
|
|
|
*/
|
|
|
|
public function hideAction($id)
|
|
|
|
{
|
2013-10-17 22:57:32 +02:00
|
|
|
$post = Model_Post::locate($id);
|
2013-11-30 00:53:09 +01:00
|
|
|
R::preload($post, ['uploader' => 'user']);
|
|
|
|
|
2013-10-18 00:09:50 +02:00
|
|
|
PrivilegesHelper::confirmWithException(Privilege::HidePost, PrivilegesHelper::getIdentitySubPrivilege($post->uploader));
|
2013-11-16 21:21:43 +01:00
|
|
|
|
2013-10-21 23:50:30 +02:00
|
|
|
if (InputHelper::get('submit'))
|
|
|
|
{
|
2013-11-22 21:20:56 +01:00
|
|
|
$post->setHidden(true);
|
|
|
|
Model_Post::save($post);
|
2013-11-16 21:21:43 +01:00
|
|
|
|
2013-11-22 23:32:56 +01:00
|
|
|
LogHelper::log('{user} hidden {post}', ['post' => TextHelper::reprPost($post)]);
|
2013-11-16 18:40:26 +01:00
|
|
|
StatusHelper::success();
|
2013-10-21 23:50:30 +02:00
|
|
|
}
|
2013-10-13 12:28:16 +02:00
|
|
|
}
|
|
|
|
|
2013-11-16 21:21:43 +01:00
|
|
|
|
|
|
|
|
2013-10-13 12:28:16 +02:00
|
|
|
/**
|
2013-10-17 22:57:32 +02:00
|
|
|
* @route /post/{id}/unhide
|
2013-10-13 12:28:16 +02:00
|
|
|
*/
|
|
|
|
public function unhideAction($id)
|
|
|
|
{
|
2013-10-17 22:57:32 +02:00
|
|
|
$post = Model_Post::locate($id);
|
2013-11-30 00:53:09 +01:00
|
|
|
R::preload($post, ['uploader' => 'user']);
|
|
|
|
|
2013-10-18 00:09:50 +02:00
|
|
|
PrivilegesHelper::confirmWithException(Privilege::HidePost, PrivilegesHelper::getIdentitySubPrivilege($post->uploader));
|
2013-11-16 21:21:43 +01:00
|
|
|
|
2013-10-21 23:50:30 +02:00
|
|
|
if (InputHelper::get('submit'))
|
|
|
|
{
|
2013-11-22 21:20:56 +01:00
|
|
|
$post->setHidden(false);
|
|
|
|
Model_Post::save($post);
|
2013-11-16 21:21:43 +01:00
|
|
|
|
2013-11-22 23:32:56 +01:00
|
|
|
LogHelper::log('{user} unhidden {post}', ['post' => TextHelper::reprPost($post)]);
|
2013-11-16 18:40:26 +01:00
|
|
|
StatusHelper::success();
|
2013-10-21 23:50:30 +02:00
|
|
|
}
|
2013-10-13 12:28:16 +02:00
|
|
|
}
|
|
|
|
|
2013-11-16 21:21:43 +01:00
|
|
|
|
|
|
|
|
2013-10-13 12:28:16 +02:00
|
|
|
/**
|
2013-10-17 22:57:32 +02:00
|
|
|
* @route /post/{id}/delete
|
2013-10-13 12:28:16 +02:00
|
|
|
*/
|
|
|
|
public function deleteAction($id)
|
|
|
|
{
|
2013-10-17 22:57:32 +02:00
|
|
|
$post = Model_Post::locate($id);
|
2013-11-30 00:53:09 +01:00
|
|
|
R::preload($post, ['uploader' => 'user']);
|
|
|
|
|
2013-10-18 00:09:50 +02:00
|
|
|
PrivilegesHelper::confirmWithException(Privilege::DeletePost, PrivilegesHelper::getIdentitySubPrivilege($post->uploader));
|
2013-11-16 21:21:43 +01:00
|
|
|
|
2013-10-21 23:50:30 +02:00
|
|
|
if (InputHelper::get('submit'))
|
|
|
|
{
|
2013-11-22 21:20:56 +01:00
|
|
|
Model_Post::remove($post);
|
2013-11-16 21:21:43 +01:00
|
|
|
|
2013-11-22 23:32:56 +01:00
|
|
|
LogHelper::log('{user} deleted {post}', ['post' => TextHelper::reprPost($id)]);
|
2013-11-16 18:40:26 +01:00
|
|
|
StatusHelper::success();
|
2013-10-21 23:50:30 +02:00
|
|
|
}
|
2013-10-13 12:28:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-10-12 14:53:47 +02:00
|
|
|
/**
|
2013-10-17 22:57:32 +02:00
|
|
|
* @route /post/{id}/add-fav
|
|
|
|
* @route /post/{id}/fav-add
|
2013-10-12 14:53:47 +02:00
|
|
|
*/
|
|
|
|
public function addFavoriteAction($id)
|
|
|
|
{
|
2013-10-17 22:57:32 +02:00
|
|
|
$post = Model_Post::locate($id);
|
2013-10-21 23:50:30 +02:00
|
|
|
PrivilegesHelper::confirmWithException(Privilege::FavoritePost);
|
2013-10-12 14:53:47 +02:00
|
|
|
|
2013-10-21 23:50:30 +02:00
|
|
|
if (InputHelper::get('submit'))
|
|
|
|
{
|
|
|
|
if (!$this->context->loggedIn)
|
|
|
|
throw new SimpleException('Not logged in');
|
2013-10-12 14:53:47 +02:00
|
|
|
|
2013-11-22 21:20:56 +01:00
|
|
|
$this->context->user->addToFavorites($post);
|
|
|
|
Model_User::save($this->context->user);
|
2013-11-16 18:40:26 +01:00
|
|
|
StatusHelper::success();
|
2013-10-21 23:50:30 +02:00
|
|
|
}
|
2013-10-12 14:53:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-10-17 22:57:32 +02:00
|
|
|
* @route /post/{id}/rem-fav
|
|
|
|
* @route /post/{id}/fav-rem
|
2013-10-12 14:53:47 +02:00
|
|
|
*/
|
|
|
|
public function remFavoriteAction($id)
|
|
|
|
{
|
2013-10-17 22:57:32 +02:00
|
|
|
$post = Model_Post::locate($id);
|
2013-10-18 00:09:50 +02:00
|
|
|
PrivilegesHelper::confirmWithException(Privilege::FavoritePost);
|
2013-10-12 14:53:47 +02:00
|
|
|
|
2013-10-21 23:50:30 +02:00
|
|
|
if (InputHelper::get('submit'))
|
|
|
|
{
|
|
|
|
if (!$this->context->loggedIn)
|
|
|
|
throw new SimpleException('Not logged in');
|
2013-10-12 14:53:47 +02:00
|
|
|
|
2013-11-22 21:20:56 +01:00
|
|
|
$this->context->user->remFromFavorites($post);
|
|
|
|
Model_User::save($this->context->user);
|
2013-11-16 18:40:26 +01:00
|
|
|
StatusHelper::success();
|
2013-10-21 23:50:30 +02:00
|
|
|
}
|
2013-10-12 14:53:47 +02:00
|
|
|
}
|
|
|
|
|
2013-10-08 23:02:31 +02:00
|
|
|
|
|
|
|
|
2013-11-10 12:23:59 +01:00
|
|
|
/**
|
|
|
|
* @route /post/{id}/score/{score}
|
|
|
|
* @validate score -1|0|1
|
|
|
|
*/
|
|
|
|
public function scoreAction($id, $score)
|
|
|
|
{
|
|
|
|
$post = Model_Post::locate($id);
|
|
|
|
PrivilegesHelper::confirmWithException(Privilege::ScorePost);
|
|
|
|
|
|
|
|
if (InputHelper::get('submit'))
|
|
|
|
{
|
|
|
|
if (!$this->context->loggedIn)
|
|
|
|
throw new SimpleException('Not logged in');
|
|
|
|
|
2013-11-22 21:20:56 +01:00
|
|
|
$this->context->user->score($post, $score);
|
|
|
|
Model_User::save($this->context->user);
|
2013-11-16 18:40:26 +01:00
|
|
|
StatusHelper::success();
|
2013-11-10 12:23:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-10-19 13:38:20 +02:00
|
|
|
/**
|
|
|
|
* @route /post/{id}/feature
|
|
|
|
*/
|
|
|
|
public function featureAction($id)
|
|
|
|
{
|
|
|
|
$post = Model_Post::locate($id);
|
|
|
|
PrivilegesHelper::confirmWithException(Privilege::FeaturePost);
|
|
|
|
Model_Property::set(Model_Property::FeaturedPostId, $post->id);
|
|
|
|
Model_Property::set(Model_Property::FeaturedPostDate, time());
|
2013-11-23 15:57:38 +01:00
|
|
|
Model_Property::set(Model_Property::FeaturedPostUserName, $this->context->user->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-05 19:24:08 +02:00
|
|
|
/**
|
2013-10-07 20:44:14 +02:00
|
|
|
* Action that decorates the page containing the post.
|
2013-10-05 19:24:08 +02:00
|
|
|
* @route /post/{id}
|
|
|
|
*/
|
2013-10-07 20:44:14 +02:00
|
|
|
public function viewAction($id)
|
2013-10-05 19:24:08 +02:00
|
|
|
{
|
2013-10-17 22:57:32 +02:00
|
|
|
$post = Model_Post::locate($id);
|
|
|
|
R::preload($post, [
|
|
|
|
'tag',
|
2013-11-30 00:53:09 +01:00
|
|
|
'uploader' => 'user',
|
2013-10-17 22:57:32 +02:00
|
|
|
'ownComment.commenter' => 'user']);
|
2013-11-30 00:53:09 +01:00
|
|
|
R::preload($this->context->user, ['ownFavoritee']);
|
2013-10-12 10:46:15 +02:00
|
|
|
|
2013-11-24 21:50:46 +01:00
|
|
|
$this->context->transport->lastSearchQuery = InputHelper::get('last-search-query');
|
|
|
|
|
2013-10-13 12:28:16 +02:00
|
|
|
if ($post->hidden)
|
2013-10-18 00:09:50 +02:00
|
|
|
PrivilegesHelper::confirmWithException(Privilege::ViewPost, 'hidden');
|
|
|
|
PrivilegesHelper::confirmWithException(Privilege::ViewPost);
|
|
|
|
PrivilegesHelper::confirmWithException(Privilege::ViewPost, PostSafety::toString($post->safety));
|
2013-10-07 20:44:14 +02:00
|
|
|
|
2013-11-24 21:50:46 +01:00
|
|
|
Model_Post_QueryBuilder::enableTokenLimit(false);
|
|
|
|
$prevPostQuery = $this->context->transport->lastSearchQuery . ' prev:' . $id;
|
|
|
|
$nextPostQuery = $this->context->transport->lastSearchQuery . ' next:' . $id;
|
|
|
|
$prevPost = current(Model_Post::getEntities($prevPostQuery, 1, 1));
|
|
|
|
$nextPost = current(Model_Post::getEntities($nextPostQuery, 1, 1));
|
|
|
|
Model_Post_QueryBuilder::enableTokenLimit(true);
|
2013-10-13 12:28:16 +02:00
|
|
|
|
2013-11-22 21:20:56 +01:00
|
|
|
$favorite = $this->context->user->hasFavorited($post);
|
|
|
|
$score = $this->context->user->getScore($post);
|
2013-11-17 20:30:04 +01:00
|
|
|
$flagged = in_array(TextHelper::reprPost($post), SessionHelper::get('flagged', []));
|
|
|
|
|
2013-11-18 14:33:43 +01:00
|
|
|
$this->context->pageThumb = \Chibi\UrlHelper::route('post', 'thumb', ['name' => $post->name]);
|
2013-10-09 21:58:57 +02:00
|
|
|
$this->context->stylesheets []= 'post-view.css';
|
2013-10-17 22:57:32 +02:00
|
|
|
$this->context->stylesheets []= 'comment-small.css';
|
2013-10-12 14:53:47 +02:00
|
|
|
$this->context->scripts []= 'post-view.js';
|
2013-10-19 22:56:56 +02:00
|
|
|
$this->context->subTitle = 'showing @' . $post->id . ' – ' . join(', ', array_map(function($x) { return $x['name']; }, $post->sharedTag));
|
2013-10-12 14:53:47 +02:00
|
|
|
$this->context->favorite = $favorite;
|
2013-11-10 12:23:59 +01:00
|
|
|
$this->context->score = $score;
|
2013-11-17 20:30:04 +01:00
|
|
|
$this->context->flagged = $flagged;
|
2013-10-07 20:44:14 +02:00
|
|
|
$this->context->transport->post = $post;
|
2013-10-13 12:28:16 +02:00
|
|
|
$this->context->transport->prevPostId = $prevPost ? $prevPost['id'] : null;
|
|
|
|
$this->context->transport->nextPostId = $nextPost ? $nextPost['id'] : null;
|
2013-11-22 21:20:56 +01:00
|
|
|
$this->context->transport->editToken = self::serializePost($post);
|
2013-10-07 20:44:14 +02:00
|
|
|
}
|
|
|
|
|
2013-10-08 23:02:31 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Action that renders the thumbnail of the requested file and sends it to user.
|
2013-10-23 00:16:52 +02:00
|
|
|
* @route /post/{name}/thumb
|
2013-10-08 23:02:31 +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
|
|
|
{
|
2013-11-22 21:20:56 +01:00
|
|
|
$path = Model_Post::getThumbCustomPath($name, $width, $height);
|
2013-10-08 23:02:31 +02:00
|
|
|
if (!file_exists($path))
|
|
|
|
{
|
2013-11-22 21:20:56 +01:00
|
|
|
$path = Model_Post::getThumbDefaultPath($name, $width, $height);
|
|
|
|
if (!file_exists($path))
|
2013-10-08 23:02:31 +02:00
|
|
|
{
|
2013-11-22 21:20:56 +01:00
|
|
|
$post = Model_Post::locate($name);
|
|
|
|
PrivilegesHelper::confirmWithException(Privilege::ListPosts);
|
|
|
|
PrivilegesHelper::confirmWithException(Privilege::ListPosts, PostSafety::toString($post->safety));
|
|
|
|
$post->makeThumb($width, $height);
|
|
|
|
if (!file_exists($path))
|
2013-11-23 20:52:41 +01:00
|
|
|
$path = TextHelper::absolutePath($this->config->main->mediaPath . DS . 'img' . DS . 'thumb.jpg');
|
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');
|
|
|
|
|
2013-11-22 21:20:56 +01:00
|
|
|
$this->context->layoutName = 'layout-file';
|
2013-10-19 13:00:03 +02:00
|
|
|
$this->context->transport->cacheDaysToLive = 30;
|
2013-10-29 09:18:00 +01:00
|
|
|
$this->context->transport->mimeType = 'image/jpeg';
|
2013-10-23 00:16:52 +02:00
|
|
|
$this->context->transport->fileHash = 'thumb' . md5($name . filemtime($path));
|
2013-10-08 23:02:31 +02:00
|
|
|
$this->context->transport->filePath = $path;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-10-07 20:44:14 +02:00
|
|
|
/**
|
|
|
|
* Action that renders the requested file itself and sends it to user.
|
2013-10-17 22:57:32 +02:00
|
|
|
* @route /post/{name}/retrieve
|
2013-10-07 20:44:14 +02:00
|
|
|
*/
|
2013-10-07 23:17:33 +02:00
|
|
|
public function retrieveAction($name)
|
2013-10-07 20:44:14 +02:00
|
|
|
{
|
|
|
|
$this->context->layoutName = 'layout-file';
|
2013-10-17 22:57:32 +02:00
|
|
|
$post = Model_Post::locate($name, true);
|
2013-10-07 20:44:14 +02:00
|
|
|
|
2013-10-18 00:09:50 +02:00
|
|
|
PrivilegesHelper::confirmWithException(Privilege::RetrievePost);
|
|
|
|
PrivilegesHelper::confirmWithException(Privilege::RetrievePost, PostSafety::toString($post->safety));
|
2013-10-07 20:44:14 +02:00
|
|
|
|
2013-11-23 20:52:41 +01:00
|
|
|
$path = TextHelper::absolutePath($this->config->main->filesPath . DS . $post->name);
|
2013-10-07 20:44:14 +02:00
|
|
|
if (!file_exists($path))
|
|
|
|
throw new SimpleException('Post file does not exist');
|
|
|
|
if (!is_readable($path))
|
|
|
|
throw new SimpleException('Post file is not readable');
|
|
|
|
|
2013-10-13 13:37:18 +02:00
|
|
|
$ext = substr($post->orig_name, strrpos($post->orig_name, '.') + 1);
|
|
|
|
if (strpos($post->orig_name, '.') === false)
|
|
|
|
$ext = '.dat';
|
|
|
|
$fn = sprintf('%s_%s_%s.%s',
|
|
|
|
$this->config->main->title,
|
2013-11-21 21:06:18 +01:00
|
|
|
$post->id,
|
|
|
|
join(',', array_map(function($tag) { return $tag->name; }, $post->sharedTag)),
|
2013-10-13 13:37:18 +02:00
|
|
|
$ext);
|
|
|
|
$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
|
|
|
|
2013-10-19 13:00:03 +02:00
|
|
|
$this->context->transport->cacheDaysToLive = 14;
|
2013-10-13 13:37:18 +02:00
|
|
|
$this->context->transport->customFileName = $fn;
|
2013-10-07 20:44:14 +02:00
|
|
|
$this->context->transport->mimeType = $post->mimeType;
|
2013-10-19 13:00:03 +02:00
|
|
|
$this->context->transport->fileHash = 'post' . $post->file_hash;
|
2013-10-07 20:44:14 +02:00
|
|
|
$this->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)
|
|
|
|
{
|
2013-11-30 00:53:09 +01:00
|
|
|
if (!$isNew)
|
|
|
|
R::preload($post, ['uploader' => 'user']);
|
|
|
|
|
2013-11-22 21:20:56 +01:00
|
|
|
/* file contents */
|
2013-11-23 17:27:56 +01:00
|
|
|
if (!empty($_FILES['file']['name']))
|
2013-11-22 21:20:56 +01:00
|
|
|
{
|
|
|
|
if (!$isNew)
|
|
|
|
PrivilegesHelper::confirmWithException(Privilege::EditPostFile, PrivilegesHelper::getIdentitySubPrivilege($post->uploader));
|
|
|
|
|
|
|
|
$suppliedFile = $_FILES['file'];
|
|
|
|
self::handleUploadErrors($suppliedFile);
|
|
|
|
|
|
|
|
$srcPath = $suppliedFile['tmp_name'];
|
|
|
|
$post->setContentFromPath($srcPath);
|
|
|
|
|
|
|
|
if (!$isNew)
|
2013-11-22 23:32:56 +01:00
|
|
|
LogHelper::log('{user} changed contents of {post}', ['post' => TextHelper::reprPost($post)]);
|
2013-11-22 21:20:56 +01:00
|
|
|
}
|
|
|
|
elseif (InputHelper::get('url'))
|
|
|
|
{
|
|
|
|
if (!$isNew)
|
|
|
|
PrivilegesHelper::confirmWithException(Privilege::EditPostFile, PrivilegesHelper::getIdentitySubPrivilege($post->uploader));
|
|
|
|
|
|
|
|
$url = InputHelper::get('url');
|
|
|
|
$post->setContentFromUrl($url);
|
|
|
|
|
|
|
|
if (!$isNew)
|
2013-11-22 23:32:56 +01:00
|
|
|
LogHelper::log('{user} changed contents of {post}', ['post' => TextHelper::reprPost($post)]);
|
2013-11-22 21:20:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* safety */
|
|
|
|
$suppliedSafety = InputHelper::get('safety');
|
|
|
|
if ($suppliedSafety !== null)
|
|
|
|
{
|
|
|
|
if (!$isNew)
|
|
|
|
PrivilegesHelper::confirmWithException(Privilege::EditPostSafety, PrivilegesHelper::getIdentitySubPrivilege($post->uploader));
|
|
|
|
|
|
|
|
$oldSafety = $post->safety;
|
|
|
|
$post->setSafety($suppliedSafety);
|
|
|
|
$newSafety = $post->safety;
|
|
|
|
|
|
|
|
if ($oldSafety != $newSafety)
|
2013-11-23 10:39:41 +01: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)
|
|
|
|
PrivilegesHelper::confirmWithException(Privilege::EditPostTags, PrivilegesHelper::getIdentitySubPrivilege($post->uploader));
|
|
|
|
|
|
|
|
$oldTags = array_map(function($tag) { return $tag->name; }, $post->sharedTag);
|
|
|
|
$post->setTagsFromText($suppliedTags);
|
|
|
|
$newTags = array_map(function($tag) { return $tag->name; }, $post->sharedTag);
|
|
|
|
|
|
|
|
foreach (array_diff($oldTags, $newTags) as $tag)
|
2013-11-22 23:32:56 +01: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)
|
2013-11-22 23:32:56 +01: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)
|
|
|
|
PrivilegesHelper::confirmWithException(Privilege::EditPostSource, PrivilegesHelper::getIdentitySubPrivilege($post->uploader));
|
|
|
|
|
|
|
|
$oldSource = $post->source;
|
|
|
|
$post->setSource($suppliedSource);
|
|
|
|
$newSource = $post->source;
|
|
|
|
|
|
|
|
if ($oldSource != $newSource)
|
2013-11-23 10:39:41 +01: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)
|
|
|
|
PrivilegesHelper::confirmWithException(Privilege::EditPostRelations, PrivilegesHelper::getIdentitySubPrivilege($post->uploader));
|
|
|
|
|
|
|
|
$oldRelatedIds = array_map(function($post) { return $post->id; }, $post->via('crossref')->sharedPost);
|
|
|
|
$post->setRelationsFromText($suppliedRelations);
|
|
|
|
$newRelatedIds = array_map(function($post) { return $post->id; }, $post->via('crossref')->sharedPost);
|
|
|
|
|
|
|
|
foreach (array_diff($oldRelatedIds, $newRelatedIds) as $post2id)
|
2013-11-22 23:32:56 +01: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)
|
2013-11-22 23:32:56 +01: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
|
|
|
}
|
|
|
|
|
|
|
|
/* thumbnail */
|
|
|
|
if (!empty($_FILES['thumb']['name']))
|
|
|
|
{
|
|
|
|
if (!$isNew)
|
|
|
|
PrivilegesHelper::confirmWithException(Privilege::EditPostThumb, PrivilegesHelper::getIdentitySubPrivilege($post->uploader));
|
|
|
|
|
|
|
|
$suppliedFile = $_FILES['thumb'];
|
|
|
|
self::handleUploadErrors($suppliedFile);
|
|
|
|
|
|
|
|
$srcPath = $suppliedFile['tmp_name'];
|
|
|
|
$post->setCustomThumbnailFromPath($srcPath);
|
|
|
|
|
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
|
|
|
}
|