2013-12-18 15:10:53 +01:00
|
|
|
<?php
|
2014-02-28 20:35:12 +01:00
|
|
|
use \Chibi\Sql as Sql;
|
|
|
|
use \Chibi\Database as Database;
|
|
|
|
|
2013-12-18 15:10:53 +01:00
|
|
|
class PostEntity extends AbstractEntity
|
|
|
|
{
|
|
|
|
public $type;
|
|
|
|
public $name;
|
|
|
|
public $origName;
|
|
|
|
public $fileHash;
|
|
|
|
public $fileSize;
|
|
|
|
public $mimeType;
|
|
|
|
public $safety;
|
|
|
|
public $hidden;
|
|
|
|
public $uploadDate;
|
|
|
|
public $imageWidth;
|
|
|
|
public $imageHeight;
|
|
|
|
public $uploaderId;
|
|
|
|
public $source;
|
2013-12-23 10:10:03 +01:00
|
|
|
public $commentCount;
|
|
|
|
public $favCount;
|
|
|
|
public $score;
|
2013-12-18 15:10:53 +01:00
|
|
|
|
|
|
|
public function getUploader()
|
|
|
|
{
|
|
|
|
if ($this->hasCache('uploader'))
|
|
|
|
return $this->getCache('uploader');
|
|
|
|
$uploader = UserModel::findById($this->uploaderId, false);
|
|
|
|
$this->setCache('uploader', $uploader);
|
|
|
|
return $uploader;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setUploader($user)
|
|
|
|
{
|
|
|
|
$this->uploaderId = $user->id;
|
|
|
|
$this->setCache('uploader', $user);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getComments()
|
|
|
|
{
|
|
|
|
if ($this->hasCache('comments'))
|
|
|
|
return $this->getCache('comments');
|
|
|
|
$comments = CommentModel::findAllByPostId($this->id);
|
|
|
|
$this->setCache('comments', $comments);
|
|
|
|
return $comments;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getFavorites()
|
|
|
|
{
|
|
|
|
if ($this->hasCache('favoritee'))
|
|
|
|
return $this->getCache('favoritee');
|
2014-02-28 20:35:12 +01:00
|
|
|
$stmt = new Sql\SelectStatement();
|
2014-02-22 19:21:32 +01:00
|
|
|
$stmt->setColumn('user.*');
|
|
|
|
$stmt->setTable('user');
|
2014-02-28 20:35:12 +01:00
|
|
|
$stmt->addInnerJoin('favoritee', new Sql\EqualsFunctor('favoritee.user_id', 'user.id'));
|
|
|
|
$stmt->setCriterion(new Sql\EqualsFunctor('favoritee.post_id', new Sql\Binding($this->id)));
|
2014-02-22 19:21:32 +01:00
|
|
|
$rows = Database::fetchAll($stmt);
|
2013-12-18 15:10:53 +01:00
|
|
|
$favorites = UserModel::convertRows($rows);
|
|
|
|
$this->setCache('favoritee', $favorites);
|
|
|
|
return $favorites;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getRelations()
|
|
|
|
{
|
|
|
|
if ($this->hasCache('relations'))
|
|
|
|
return $this->getCache('relations');
|
|
|
|
|
2014-02-28 20:35:12 +01:00
|
|
|
$stmt = new Sql\SelectStatement();
|
2014-02-22 19:21:32 +01:00
|
|
|
$stmt->setColumn('post.*');
|
|
|
|
$stmt->setTable('post');
|
2014-02-28 20:35:12 +01:00
|
|
|
$binding = new Sql\Binding($this->id);
|
|
|
|
$stmt->addInnerJoin('crossref', (new Sql\DisjunctionFunctor)
|
2014-02-22 19:21:32 +01:00
|
|
|
->add(
|
2014-02-28 20:35:12 +01:00
|
|
|
(new Sql\ConjunctionFunctor)
|
|
|
|
->add(new Sql\EqualsFunctor('post.id', 'crossref.post2_id'))
|
|
|
|
->add(new Sql\EqualsFunctor('crossref.post_id', $binding)))
|
2014-02-22 19:21:32 +01:00
|
|
|
->add(
|
2014-02-28 20:35:12 +01:00
|
|
|
(new Sql\ConjunctionFunctor)
|
|
|
|
->add(new Sql\EqualsFunctor('post.id', 'crossref.post_id'))
|
|
|
|
->add(new Sql\EqualsFunctor('crossref.post2_id', $binding))));
|
2014-02-22 19:21:32 +01:00
|
|
|
$rows = Database::fetchAll($stmt);
|
2013-12-18 15:10:53 +01:00
|
|
|
$posts = PostModel::convertRows($rows);
|
|
|
|
$this->setCache('relations', $posts);
|
|
|
|
return $posts;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setRelations(array $relations)
|
|
|
|
{
|
|
|
|
foreach ($relations as $relatedPost)
|
|
|
|
if (!$relatedPost->id)
|
|
|
|
throw new Exception('All related posts must be saved');
|
|
|
|
$uniqueRelations = [];
|
|
|
|
foreach ($relations as $relatedPost)
|
|
|
|
$uniqueRelations[$relatedPost->id] = $relatedPost;
|
|
|
|
$relations = array_values($uniqueRelations);
|
|
|
|
$this->setCache('relations', $relations);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setRelationsFromText($relationsText)
|
|
|
|
{
|
2014-04-29 21:35:29 +02:00
|
|
|
$config = getConfig();
|
2013-12-18 15:10:53 +01:00
|
|
|
$relatedIds = array_filter(preg_split('/\D/', $relationsText));
|
|
|
|
|
|
|
|
$relatedPosts = [];
|
|
|
|
foreach ($relatedIds as $relatedId)
|
|
|
|
{
|
|
|
|
if ($relatedId == $this->id)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (count($relatedPosts) > $config->browsing->maxRelatedPosts)
|
2014-04-30 00:11:13 +02:00
|
|
|
{
|
|
|
|
throw new SimpleException(
|
|
|
|
'Too many related posts (maximum: %d)',
|
|
|
|
$config->browsing->maxRelatedPosts);
|
|
|
|
}
|
2013-12-18 15:10:53 +01:00
|
|
|
|
|
|
|
$relatedPosts []= PostModel::findById($relatedId);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->setRelations($relatedPosts);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getTags()
|
|
|
|
{
|
|
|
|
if ($this->hasCache('tags'))
|
|
|
|
return $this->getCache('tags');
|
|
|
|
$tags = TagModel::findAllByPostId($this->id);
|
|
|
|
$this->setCache('tags', $tags);
|
|
|
|
return $tags;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setTags(array $tags)
|
|
|
|
{
|
|
|
|
foreach ($tags as $tag)
|
|
|
|
if (!$tag->id)
|
|
|
|
throw new Exception('All tags must be saved');
|
|
|
|
$uniqueTags = [];
|
|
|
|
foreach ($tags as $tag)
|
|
|
|
$uniqueTags[$tag->id] = $tag;
|
|
|
|
$tags = array_values($uniqueTags);
|
|
|
|
$this->setCache('tags', $tags);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setTagsFromText($tagsText)
|
|
|
|
{
|
|
|
|
$tagNames = TagModel::validateTags($tagsText);
|
|
|
|
$tags = [];
|
|
|
|
foreach ($tagNames as $tagName)
|
|
|
|
{
|
|
|
|
$tag = TagModel::findByName($tagName, false);
|
|
|
|
if (!$tag)
|
|
|
|
{
|
|
|
|
$tag = TagModel::spawn();
|
|
|
|
$tag->name = $tagName;
|
|
|
|
TagModel::save($tag);
|
|
|
|
}
|
|
|
|
$tags []= $tag;
|
|
|
|
}
|
|
|
|
$this->setTags($tags);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function isTaggedWith($tagName)
|
|
|
|
{
|
|
|
|
$tagName = trim(strtolower($tagName));
|
|
|
|
foreach ($this->getTags() as $tag)
|
|
|
|
if (trim(strtolower($tag->name)) == $tagName)
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setHidden($hidden)
|
|
|
|
{
|
|
|
|
$this->hidden = boolval($hidden);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setSafety($safety)
|
|
|
|
{
|
|
|
|
$this->safety = PostModel::validateSafety($safety);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setSource($source)
|
|
|
|
{
|
|
|
|
$this->source = PostModel::validateSource($source);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getThumbCustomPath($width = null, $height = null)
|
|
|
|
{
|
|
|
|
return PostModel::getThumbCustomPath($this->name, $width, $height);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getThumbDefaultPath($width = null, $height = null)
|
|
|
|
{
|
|
|
|
return PostModel::getThumbDefaultPath($this->name, $width, $height);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getFullPath()
|
|
|
|
{
|
|
|
|
return PostModel::getFullPath($this->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function hasCustomThumb($width = null, $height = null)
|
|
|
|
{
|
|
|
|
$thumbPath = $this->getThumbCustomPath($width, $height);
|
|
|
|
return file_exists($thumbPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setCustomThumbnailFromPath($srcPath)
|
|
|
|
{
|
2014-04-29 21:35:29 +02:00
|
|
|
$config = getConfig();
|
2013-12-18 15:10:53 +01:00
|
|
|
|
|
|
|
$mimeType = mime_content_type($srcPath);
|
|
|
|
if (!in_array($mimeType, ['image/gif', 'image/png', 'image/jpeg']))
|
2014-04-27 14:42:39 +02:00
|
|
|
throw new SimpleException('Invalid thumbnail type "%s"', $mimeType);
|
2013-12-18 15:10:53 +01:00
|
|
|
|
|
|
|
list ($imageWidth, $imageHeight) = getimagesize($srcPath);
|
2014-04-30 08:08:24 +02:00
|
|
|
if ($imageWidth != $config->browsing->thumbWidth
|
|
|
|
or $imageHeight != $config->browsing->thumbHeight)
|
|
|
|
{
|
|
|
|
throw new SimpleException(
|
|
|
|
'Invalid thumbnail size (should be %dx%d)',
|
|
|
|
$config->browsing->thumbWidth,
|
|
|
|
$config->browsing->thumbHeight);
|
|
|
|
}
|
2013-12-18 15:10:53 +01:00
|
|
|
|
|
|
|
$dstPath = $this->getThumbCustomPath();
|
|
|
|
|
2014-04-30 08:08:24 +02:00
|
|
|
TransferHelper::moveUpload($srcPath, $dstPath);
|
2013-12-18 15:10:53 +01:00
|
|
|
}
|
|
|
|
|
2014-04-30 09:54:04 +02:00
|
|
|
public function generateThumb($width = null, $height = null)
|
2013-12-18 15:10:53 +01:00
|
|
|
{
|
|
|
|
list ($width, $height) = PostModel::validateThumbSize($width, $height);
|
|
|
|
$srcPath = $this->getFullPath();
|
2014-04-30 09:54:04 +02:00
|
|
|
$dstPath = $this->getThumbDefaultPath($width, $height);
|
2013-12-18 15:10:53 +01:00
|
|
|
|
|
|
|
if ($this->type == PostType::Youtube)
|
|
|
|
{
|
2014-04-30 09:54:04 +02:00
|
|
|
return ThumbnailHelper::generateFromUrl(
|
|
|
|
'http://img.youtube.com/vi/' . $this->fileHash . '/mqdefault.jpg',
|
|
|
|
$dstPath,
|
|
|
|
$width,
|
|
|
|
$height);
|
2013-12-18 15:10:53 +01:00
|
|
|
}
|
2014-04-30 09:54:04 +02:00
|
|
|
else
|
2013-12-18 15:10:53 +01:00
|
|
|
{
|
2014-04-30 09:54:04 +02:00
|
|
|
return ThumbnailHelper::generateFromPath($srcPath, $dstPath, $width, $height);
|
2013-12-18 15:10:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-30 08:08:24 +02:00
|
|
|
public function setContentFromPath($srcPath, $origName)
|
2013-12-18 15:10:53 +01:00
|
|
|
{
|
|
|
|
$this->fileSize = filesize($srcPath);
|
|
|
|
$this->fileHash = md5_file($srcPath);
|
2014-04-30 08:08:24 +02:00
|
|
|
$this->origName = $origName;
|
2013-12-18 15:10:53 +01:00
|
|
|
|
|
|
|
if ($this->fileSize == 0)
|
|
|
|
throw new SimpleException('Specified file is empty');
|
|
|
|
|
|
|
|
$this->mimeType = mime_content_type($srcPath);
|
|
|
|
switch ($this->mimeType)
|
|
|
|
{
|
|
|
|
case 'image/gif':
|
|
|
|
case 'image/png':
|
|
|
|
case 'image/jpeg':
|
|
|
|
list ($imageWidth, $imageHeight) = getimagesize($srcPath);
|
|
|
|
$this->type = PostType::Image;
|
|
|
|
$this->imageWidth = $imageWidth;
|
|
|
|
$this->imageHeight = $imageHeight;
|
|
|
|
break;
|
|
|
|
case 'application/x-shockwave-flash':
|
|
|
|
list ($imageWidth, $imageHeight) = getimagesize($srcPath);
|
|
|
|
$this->type = PostType::Flash;
|
|
|
|
$this->imageWidth = $imageWidth;
|
|
|
|
$this->imageHeight = $imageHeight;
|
|
|
|
break;
|
2014-04-08 16:54:36 +02:00
|
|
|
case 'video/webm':
|
|
|
|
case 'video/mp4':
|
|
|
|
case 'video/ogg':
|
|
|
|
case 'application/ogg':
|
|
|
|
case 'video/x-flv':
|
|
|
|
case 'video/3gpp':
|
|
|
|
list ($imageWidth, $imageHeight) = getimagesize($srcPath);
|
|
|
|
$this->type = PostType::Video;
|
|
|
|
$this->imageWidth = $imageWidth;
|
|
|
|
$this->imageHeight = $imageHeight;
|
|
|
|
break;
|
2013-12-18 15:10:53 +01:00
|
|
|
default:
|
2014-04-27 14:42:39 +02:00
|
|
|
throw new SimpleException('Invalid file type "%s"', $this->mimeType);
|
2013-12-18 15:10:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$duplicatedPost = PostModel::findByHash($this->fileHash, false);
|
|
|
|
if ($duplicatedPost !== null and (!$this->id or $this->id != $duplicatedPost->id))
|
2014-04-30 00:11:13 +02:00
|
|
|
{
|
|
|
|
throw new SimpleException(
|
|
|
|
'Duplicate upload: %s',
|
|
|
|
TextHelper::reprPost($duplicatedPost));
|
|
|
|
}
|
2013-12-18 15:10:53 +01:00
|
|
|
|
|
|
|
$dstPath = $this->getFullPath();
|
|
|
|
|
2014-04-30 08:08:24 +02:00
|
|
|
TransferHelper::moveUpload($srcPath, $dstPath);
|
2013-12-18 15:10:53 +01:00
|
|
|
|
|
|
|
$thumbPath = $this->getThumbDefaultPath();
|
|
|
|
if (file_exists($thumbPath))
|
|
|
|
unlink($thumbPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setContentFromUrl($srcUrl)
|
|
|
|
{
|
|
|
|
if (!preg_match('/^https?:\/\//', $srcUrl))
|
2014-04-27 14:42:39 +02:00
|
|
|
throw new SimpleException('Invalid URL "%s"', $srcUrl);
|
2013-12-18 15:10:53 +01:00
|
|
|
|
2014-04-30 08:08:24 +02:00
|
|
|
$this->origName = $srcUrl;
|
|
|
|
|
2013-12-18 15:10:53 +01:00
|
|
|
if (preg_match('/youtube.com\/watch.*?=([a-zA-Z0-9_-]+)/', $srcUrl, $matches))
|
|
|
|
{
|
2014-02-17 23:11:00 +01:00
|
|
|
$youtubeId = $matches[1];
|
2013-12-18 15:10:53 +01:00
|
|
|
$this->type = PostType::Youtube;
|
|
|
|
$this->mimeType = null;
|
|
|
|
$this->fileSize = null;
|
2014-02-17 23:11:00 +01:00
|
|
|
$this->fileHash = $youtubeId;
|
2013-12-18 15:10:53 +01:00
|
|
|
$this->imageWidth = null;
|
|
|
|
$this->imageHeight = null;
|
|
|
|
|
|
|
|
$thumbPath = $this->getThumbDefaultPath();
|
|
|
|
if (file_exists($thumbPath))
|
|
|
|
unlink($thumbPath);
|
|
|
|
|
2014-02-17 23:11:00 +01:00
|
|
|
$duplicatedPost = PostModel::findByHash($youtubeId, false);
|
2013-12-18 15:10:53 +01:00
|
|
|
if ($duplicatedPost !== null and (!$this->id or $this->id != $duplicatedPost->id))
|
2014-04-30 00:11:13 +02:00
|
|
|
{
|
|
|
|
throw new SimpleException(
|
|
|
|
'Duplicate upload: %s',
|
|
|
|
TextHelper::reprPost($duplicatedPost));
|
|
|
|
}
|
2013-12-18 15:10:53 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$srcPath = tempnam(sys_get_temp_dir(), 'upload') . '.dat';
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2014-04-30 08:08:24 +02:00
|
|
|
$maxBytes = TextHelper::stripBytesUnits(ini_get('upload_max_filesize'));
|
2013-12-18 15:10:53 +01:00
|
|
|
|
2014-04-30 08:08:24 +02:00
|
|
|
TransferHelper::download($srcUrl, $srcPath, $maxBytes);
|
|
|
|
|
|
|
|
$this->setContentFromPath($srcPath, basename($srcUrl));
|
2013-12-18 15:10:53 +01:00
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
if (file_exists($srcPath))
|
|
|
|
unlink($srcPath);
|
|
|
|
}
|
|
|
|
}
|
2014-01-25 15:09:20 +01:00
|
|
|
|
|
|
|
public function getEditToken()
|
|
|
|
{
|
|
|
|
$x = [];
|
|
|
|
foreach ($this->getTags() as $tag)
|
|
|
|
$x []= TextHelper::reprTag($tag->name);
|
|
|
|
foreach ($this->getRelations() as $relatedPost)
|
|
|
|
$x []= TextHelper::reprPost($relatedPost);
|
|
|
|
$x []= $this->safety;
|
|
|
|
$x []= $this->source;
|
|
|
|
$x []= $this->fileHash;
|
|
|
|
natcasesort($x);
|
|
|
|
$x = join(' ', $x);
|
|
|
|
return md5($x);
|
|
|
|
}
|
2013-12-18 15:10:53 +01:00
|
|
|
}
|