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;
|
|
|
|
|
2014-05-09 17:54:52 +02:00
|
|
|
final class PostEntity extends AbstractEntity implements IValidatable
|
2013-12-18 15:10:53 +01:00
|
|
|
{
|
2014-05-04 19:06:40 +02:00
|
|
|
protected $type;
|
2014-05-04 20:09:03 +02:00
|
|
|
protected $name;
|
2014-05-07 18:41:13 +02:00
|
|
|
protected $origName;
|
2014-05-07 18:47:45 +02:00
|
|
|
protected $fileHash;
|
|
|
|
protected $fileSize;
|
|
|
|
protected $mimeType;
|
2014-05-04 19:06:40 +02:00
|
|
|
protected $safety;
|
2014-05-07 18:00:05 +02:00
|
|
|
protected $hidden;
|
2014-05-07 20:33:52 +02:00
|
|
|
protected $uploadDate;
|
2014-05-07 17:57:17 +02:00
|
|
|
protected $imageWidth;
|
|
|
|
protected $imageHeight;
|
2014-05-07 18:39:14 +02:00
|
|
|
protected $uploaderId;
|
2014-05-07 17:52:57 +02:00
|
|
|
protected $source;
|
2014-05-07 20:33:52 +02:00
|
|
|
|
2014-05-09 17:54:52 +02:00
|
|
|
public function fillNew()
|
|
|
|
{
|
|
|
|
$this->setSafety(new PostSafety(PostSafety::Safe));
|
|
|
|
$this->setHidden(false);
|
|
|
|
$this->setCreationTime(time());
|
|
|
|
do
|
|
|
|
{
|
|
|
|
$this->setName(md5(mt_rand() . uniqid()));
|
|
|
|
}
|
|
|
|
while (file_exists($this->getFullPath()));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function fillFromDatabase($row)
|
|
|
|
{
|
|
|
|
$this->id = (int) $row['id'];
|
|
|
|
$this->name = $row['name'];
|
|
|
|
$this->origName = $row['orig_name'];
|
|
|
|
$this->fileHash = $row['file_hash'];
|
|
|
|
$this->fileSize = (int) $row['file_size'];
|
|
|
|
$this->mimeType = $row['mime_type'];
|
|
|
|
$this->hidden = (bool) $row['hidden'];
|
|
|
|
$this->uploadDate = $row['upload_date'];
|
|
|
|
$this->imageWidth = (int) $row['image_width'];
|
|
|
|
$this->imageHeight = (int) $row['image_height'];
|
|
|
|
$this->uploaderId = (int) $row['uploader_id'];
|
|
|
|
$this->source = $row['source'];
|
|
|
|
$this->setCache('comment_count', $row['comment_count']);
|
|
|
|
$this->setCache('fav_count', $row['fav_count']);
|
|
|
|
$this->setCache('score', $row['score']);
|
|
|
|
$this->setType(new PostType($row['type']));
|
|
|
|
$this->setSafety(new PostSafety($row['safety']));
|
|
|
|
}
|
2013-12-18 15:10:53 +01:00
|
|
|
|
2014-05-05 09:36:08 +02:00
|
|
|
public function validate()
|
|
|
|
{
|
|
|
|
if (empty($this->getType()))
|
|
|
|
throw new SimpleException('No post type detected');
|
2014-05-07 17:52:57 +02:00
|
|
|
|
2014-05-07 21:39:41 +02:00
|
|
|
if (empty($this->getTags()))
|
|
|
|
throw new SimpleException('No tags set');
|
|
|
|
|
2014-05-07 17:52:57 +02:00
|
|
|
$this->getType()->validate();
|
|
|
|
|
|
|
|
$this->getSafety()->validate();
|
|
|
|
|
|
|
|
$maxSourceLength = getConfig()->posts->maxSourceLength;
|
|
|
|
if (strlen($this->getSource()) > $maxSourceLength)
|
|
|
|
throw new SimpleException('Source must have at most %d characters', $maxSourceLength);
|
2014-05-05 09:36:08 +02:00
|
|
|
}
|
|
|
|
|
2013-12-18 15:10:53 +01:00
|
|
|
public function getUploader()
|
|
|
|
{
|
|
|
|
if ($this->hasCache('uploader'))
|
|
|
|
return $this->getCache('uploader');
|
2014-05-07 18:39:14 +02:00
|
|
|
if (!$this->uploaderId)
|
|
|
|
return null;
|
2014-05-08 08:54:08 +02:00
|
|
|
$uploader = UserModel::tryGetById($this->uploaderId);
|
2013-12-18 15:10:53 +01:00
|
|
|
$this->setCache('uploader', $uploader);
|
|
|
|
return $uploader;
|
|
|
|
}
|
|
|
|
|
2014-05-07 18:39:14 +02:00
|
|
|
public function getUploaderId()
|
|
|
|
{
|
|
|
|
return $this->uploaderId;
|
|
|
|
}
|
|
|
|
|
2013-12-18 15:10:53 +01:00
|
|
|
public function setUploader($user)
|
|
|
|
{
|
2014-05-07 18:39:14 +02:00
|
|
|
$this->uploaderId = $user !== null ? $user->getId() : null;
|
2013-12-18 15:10:53 +01:00
|
|
|
$this->setCache('uploader', $user);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getComments()
|
|
|
|
{
|
|
|
|
if ($this->hasCache('comments'))
|
|
|
|
return $this->getCache('comments');
|
2014-05-08 08:54:08 +02:00
|
|
|
$comments = CommentModel::getAllByPostId($this->getId());
|
2013-12-18 15:10:53 +01:00
|
|
|
$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'));
|
2014-05-05 21:20:40 +02:00
|
|
|
$stmt->setCriterion(new Sql\EqualsFunctor('favoritee.post_id', new Sql\Binding($this->getId())));
|
2014-02-22 19:21:32 +01:00
|
|
|
$rows = Database::fetchAll($stmt);
|
2014-05-09 17:54:52 +02:00
|
|
|
$favorites = UserModel::spawnFromDatabaseRows($rows);
|
2013-12-18 15:10:53 +01:00
|
|
|
$this->setCache('favoritee', $favorites);
|
|
|
|
return $favorites;
|
|
|
|
}
|
|
|
|
|
2014-05-09 13:46:29 +02:00
|
|
|
public function getScore()
|
|
|
|
{
|
2014-05-09 17:54:52 +02:00
|
|
|
return (int) $this->getColumnWithCache('score');
|
2014-05-09 13:46:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getCommentCount()
|
|
|
|
{
|
2014-05-09 17:54:52 +02:00
|
|
|
return (int) $this->getColumnWithCache('comment_count');
|
2014-05-09 13:46:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getFavoriteCount()
|
|
|
|
{
|
2014-05-09 17:54:52 +02:00
|
|
|
return (int) $this->getColumnWithCache('fav_count');
|
2014-05-09 13:46:29 +02:00
|
|
|
}
|
|
|
|
|
2013-12-18 15:10:53 +01:00
|
|
|
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-05-05 21:20:40 +02:00
|
|
|
$binding = new Sql\Binding($this->getId());
|
2014-02-28 20:35:12 +01:00
|
|
|
$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);
|
2014-05-09 17:54:52 +02:00
|
|
|
$posts = PostModel::spawnFromDatabaseRows($rows);
|
2013-12-18 15:10:53 +01:00
|
|
|
$this->setCache('relations', $posts);
|
|
|
|
return $posts;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setRelations(array $relations)
|
|
|
|
{
|
|
|
|
foreach ($relations as $relatedPost)
|
2014-05-05 21:20:40 +02:00
|
|
|
if (!$relatedPost->getId())
|
2013-12-18 15:10:53 +01:00
|
|
|
throw new Exception('All related posts must be saved');
|
|
|
|
$uniqueRelations = [];
|
|
|
|
foreach ($relations as $relatedPost)
|
2014-05-05 21:20:40 +02:00
|
|
|
$uniqueRelations[$relatedPost->getId()] = $relatedPost;
|
2013-12-18 15:10:53 +01:00
|
|
|
$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)
|
|
|
|
{
|
2014-05-05 21:20:40 +02:00
|
|
|
if ($relatedId == $this->getId())
|
2013-12-18 15:10:53 +01:00
|
|
|
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
|
|
|
|
2014-05-08 08:54:08 +02:00
|
|
|
$relatedPosts []= PostModel::getById($relatedId);
|
2013-12-18 15:10:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->setRelations($relatedPosts);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getTags()
|
|
|
|
{
|
|
|
|
if ($this->hasCache('tags'))
|
|
|
|
return $this->getCache('tags');
|
2014-05-08 08:54:08 +02:00
|
|
|
$tags = TagModel::getAllByPostId($this->getId());
|
2013-12-18 15:10:53 +01:00
|
|
|
$this->setCache('tags', $tags);
|
|
|
|
return $tags;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setTags(array $tags)
|
|
|
|
{
|
|
|
|
foreach ($tags as $tag)
|
2014-05-05 21:20:40 +02:00
|
|
|
if (!$tag->getId())
|
2013-12-18 15:10:53 +01:00
|
|
|
throw new Exception('All tags must be saved');
|
|
|
|
$uniqueTags = [];
|
|
|
|
foreach ($tags as $tag)
|
2014-05-05 21:20:40 +02:00
|
|
|
$uniqueTags[$tag->getId()] = $tag;
|
2013-12-18 15:10:53 +01:00
|
|
|
$tags = array_values($uniqueTags);
|
|
|
|
$this->setCache('tags', $tags);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function isTaggedWith($tagName)
|
|
|
|
{
|
|
|
|
$tagName = trim(strtolower($tagName));
|
|
|
|
foreach ($this->getTags() as $tag)
|
2014-05-04 20:09:03 +02:00
|
|
|
if (trim(strtolower($tag->getName())) == $tagName)
|
2013-12-18 15:10:53 +01:00
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-05-07 18:00:05 +02:00
|
|
|
public function isHidden()
|
|
|
|
{
|
|
|
|
return $this->hidden;
|
|
|
|
}
|
|
|
|
|
2013-12-18 15:10:53 +01:00
|
|
|
public function setHidden($hidden)
|
|
|
|
{
|
|
|
|
$this->hidden = boolval($hidden);
|
|
|
|
}
|
|
|
|
|
2014-05-07 20:33:52 +02:00
|
|
|
public function getCreationTime()
|
|
|
|
{
|
|
|
|
return $this->uploadDate;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setCreationTime($unixTime)
|
|
|
|
{
|
|
|
|
$this->uploadDate = $unixTime;
|
|
|
|
}
|
|
|
|
|
2014-05-07 17:57:17 +02:00
|
|
|
public function getImageWidth()
|
|
|
|
{
|
|
|
|
return $this->imageWidth;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setImageWidth($imageWidth)
|
|
|
|
{
|
|
|
|
$this->imageWidth = $imageWidth;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getImageHeight()
|
|
|
|
{
|
|
|
|
return $this->imageHeight;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setImageHeight($imageHeight)
|
|
|
|
{
|
|
|
|
$this->imageHeight = $imageHeight;
|
|
|
|
}
|
|
|
|
|
2014-05-04 20:09:03 +02:00
|
|
|
public function getName()
|
|
|
|
{
|
|
|
|
return $this->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setName($name)
|
|
|
|
{
|
|
|
|
$this->name = $name;
|
|
|
|
}
|
|
|
|
|
2014-05-07 18:41:13 +02:00
|
|
|
public function getOriginalName()
|
|
|
|
{
|
|
|
|
return $this->origName;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setOriginalName($origName)
|
|
|
|
{
|
|
|
|
$this->origName = $origName;
|
|
|
|
}
|
|
|
|
|
2014-05-07 18:47:45 +02:00
|
|
|
public function getFileHash()
|
|
|
|
{
|
|
|
|
return $this->fileHash;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setFileHash($fileHash)
|
|
|
|
{
|
|
|
|
$this->fileHash = $fileHash;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getFileSize()
|
|
|
|
{
|
|
|
|
return $this->fileSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setFileSize($fileSize)
|
|
|
|
{
|
|
|
|
$this->fileSize = $fileSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getMimeType()
|
|
|
|
{
|
|
|
|
return $this->mimeType;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setMimeType($mimeType)
|
|
|
|
{
|
|
|
|
$this->mimeType = $mimeType;
|
|
|
|
}
|
|
|
|
|
2014-05-04 19:06:40 +02:00
|
|
|
public function getType()
|
2013-12-18 15:10:53 +01:00
|
|
|
{
|
2014-05-04 19:06:40 +02:00
|
|
|
return $this->type;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setType(PostType $type)
|
|
|
|
{
|
|
|
|
$this->type = $type;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getSafety()
|
|
|
|
{
|
|
|
|
return $this->safety;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setSafety(PostSafety $safety)
|
|
|
|
{
|
|
|
|
$this->safety = $safety;
|
2013-12-18 15:10:53 +01:00
|
|
|
}
|
|
|
|
|
2014-05-07 17:52:57 +02:00
|
|
|
public function getSource()
|
|
|
|
{
|
|
|
|
return $this->source;
|
|
|
|
}
|
|
|
|
|
2013-12-18 15:10:53 +01:00
|
|
|
public function setSource($source)
|
|
|
|
{
|
2014-05-07 17:52:57 +02:00
|
|
|
$this->source = trim($source);
|
2013-12-18 15:10:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getThumbCustomPath($width = null, $height = null)
|
|
|
|
{
|
2014-05-04 20:09:03 +02:00
|
|
|
return PostModel::getThumbCustomPath($this->getName(), $width, $height);
|
2013-12-18 15:10:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getThumbDefaultPath($width = null, $height = null)
|
|
|
|
{
|
2014-05-04 20:09:03 +02:00
|
|
|
return PostModel::getThumbDefaultPath($this->getName(), $width, $height);
|
2013-12-18 15:10:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getFullPath()
|
|
|
|
{
|
2014-05-04 20:09:03 +02:00
|
|
|
return PostModel::getFullPath($this->getName());
|
2013-12-18 15:10:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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-05-06 15:49:02 +02:00
|
|
|
TransferHelper::copy($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
|
|
|
|
2014-05-04 19:06:40 +02:00
|
|
|
if ($this->getType()->toInteger() == PostType::Youtube)
|
2013-12-18 15:10:53 +01:00
|
|
|
{
|
2014-04-30 09:54:04 +02:00
|
|
|
return ThumbnailHelper::generateFromUrl(
|
2014-05-07 18:47:45 +02:00
|
|
|
'http://img.youtube.com/vi/' . $this->getFileHash() . '/mqdefault.jpg',
|
2014-04-30 09:54:04 +02:00
|
|
|
$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
|
|
|
{
|
2014-05-07 18:47:45 +02:00
|
|
|
$this->setFileSize(filesize($srcPath));
|
|
|
|
$this->setFileHash(md5_file($srcPath));
|
2014-05-07 18:41:13 +02:00
|
|
|
$this->setOriginalName($origName);
|
2013-12-18 15:10:53 +01:00
|
|
|
|
2014-05-07 18:47:45 +02:00
|
|
|
if ($this->getFileSize() == 0)
|
2013-12-18 15:10:53 +01:00
|
|
|
throw new SimpleException('Specified file is empty');
|
|
|
|
|
2014-05-07 18:47:45 +02:00
|
|
|
$this->setMimeType(mime_content_type($srcPath));
|
|
|
|
switch ($this->getMimeType())
|
2013-12-18 15:10:53 +01:00
|
|
|
{
|
|
|
|
case 'image/gif':
|
|
|
|
case 'image/png':
|
|
|
|
case 'image/jpeg':
|
|
|
|
list ($imageWidth, $imageHeight) = getimagesize($srcPath);
|
2014-05-04 19:06:40 +02:00
|
|
|
$this->setType(new PostType(PostType::Image));
|
2014-05-07 17:57:17 +02:00
|
|
|
$this->setImageWidth($imageWidth);
|
|
|
|
$this->setImageHeight($imageHeight);
|
2013-12-18 15:10:53 +01:00
|
|
|
break;
|
|
|
|
case 'application/x-shockwave-flash':
|
|
|
|
list ($imageWidth, $imageHeight) = getimagesize($srcPath);
|
2014-05-04 19:06:40 +02:00
|
|
|
$this->setType(new PostType(PostType::Flash));
|
2014-05-07 17:57:17 +02:00
|
|
|
$this->setImageWidth($imageWidth);
|
|
|
|
$this->setImageHeight($imageHeight);
|
2013-12-18 15:10:53 +01:00
|
|
|
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);
|
2014-05-04 19:06:40 +02:00
|
|
|
$this->setType(new PostType(PostType::Video));
|
2014-05-07 17:57:17 +02:00
|
|
|
$this->setImageWidth($imageWidth);
|
|
|
|
$this->setImageHeight($imageHeight);
|
2014-04-08 16:54:36 +02:00
|
|
|
break;
|
2013-12-18 15:10:53 +01:00
|
|
|
default:
|
2014-05-07 18:47:45 +02:00
|
|
|
throw new SimpleException('Invalid file type "%s"', $this->getMimeType());
|
2013-12-18 15:10:53 +01:00
|
|
|
}
|
|
|
|
|
2014-05-08 08:54:08 +02:00
|
|
|
$duplicatedPost = PostModel::tryGetByHash($this->getFileHash());
|
2014-05-05 21:20:40 +02:00
|
|
|
if ($duplicatedPost !== null and (!$this->getId() or $this->getId() != $duplicatedPost->getId()))
|
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-05-06 15:49:02 +02:00
|
|
|
TransferHelper::copy($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-05-07 18:41:13 +02:00
|
|
|
$this->setOriginalName($srcUrl);
|
2014-04-30 08:08:24 +02:00
|
|
|
|
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];
|
2014-05-04 19:06:40 +02:00
|
|
|
$this->setType(new PostType(PostType::Youtube));
|
2014-05-07 18:47:45 +02:00
|
|
|
$this->setMimeType(null);
|
|
|
|
$this->setFileSize(null);
|
|
|
|
$this->setFileHash($youtubeId);
|
2014-05-07 17:57:17 +02:00
|
|
|
$this->setImageWidth(null);
|
|
|
|
$this->setImageHeight(null);
|
2013-12-18 15:10:53 +01:00
|
|
|
|
|
|
|
$thumbPath = $this->getThumbDefaultPath();
|
|
|
|
if (file_exists($thumbPath))
|
|
|
|
unlink($thumbPath);
|
|
|
|
|
2014-05-08 08:54:08 +02:00
|
|
|
$duplicatedPost = PostModel::tryGetByHash($youtubeId);
|
2014-05-05 21:20:40 +02:00
|
|
|
if ($duplicatedPost !== null and (!$this->getId() or $this->getId() != $duplicatedPost->getId()))
|
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;
|
|
|
|
}
|
|
|
|
|
2014-05-06 15:49:02 +02:00
|
|
|
$tmpPath = tempnam(sys_get_temp_dir(), 'upload') . '.dat';
|
2013-12-18 15:10:53 +01:00
|
|
|
|
|
|
|
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-05-06 15:49:02 +02:00
|
|
|
TransferHelper::download($srcUrl, $tmpPath, $maxBytes);
|
2014-04-30 08:08:24 +02:00
|
|
|
|
2014-05-06 15:49:02 +02:00
|
|
|
$this->setContentFromPath($tmpPath, basename($srcUrl));
|
2013-12-18 15:10:53 +01:00
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
2014-05-06 15:49:02 +02:00
|
|
|
if (file_exists($tmpPath))
|
|
|
|
unlink($tmpPath);
|
2013-12-18 15:10:53 +01:00
|
|
|
}
|
|
|
|
}
|
2014-01-25 15:09:20 +01:00
|
|
|
|
|
|
|
public function getEditToken()
|
|
|
|
{
|
|
|
|
$x = [];
|
2014-05-07 17:52:57 +02:00
|
|
|
|
2014-01-25 15:09:20 +01:00
|
|
|
foreach ($this->getTags() as $tag)
|
2014-05-04 20:09:03 +02:00
|
|
|
$x []= TextHelper::reprTag($tag->getName());
|
2014-05-07 17:52:57 +02:00
|
|
|
|
2014-01-25 15:09:20 +01:00
|
|
|
foreach ($this->getRelations() as $relatedPost)
|
|
|
|
$x []= TextHelper::reprPost($relatedPost);
|
2014-05-07 17:52:57 +02:00
|
|
|
|
2014-05-04 19:06:40 +02:00
|
|
|
$x []= $this->getSafety()->toInteger();
|
2014-05-07 17:52:57 +02:00
|
|
|
$x []= $this->getSource();
|
2014-05-07 18:47:45 +02:00
|
|
|
$x []= $this->getFileHash();
|
2014-05-07 17:52:57 +02:00
|
|
|
|
2014-01-25 15:09:20 +01:00
|
|
|
natcasesort($x);
|
2014-05-07 17:52:57 +02:00
|
|
|
|
2014-01-25 15:09:20 +01:00
|
|
|
$x = join(' ', $x);
|
|
|
|
return md5($x);
|
|
|
|
}
|
2013-12-18 15:10:53 +01:00
|
|
|
}
|