Add post size limits

This commit is contained in:
Alma Armas 2020-08-20 12:58:40 +00:00
parent 524d531c4f
commit fdb8b35609
4 changed files with 39 additions and 23 deletions

View file

@ -86,6 +86,8 @@ class Post extends Postable
{ {
if(ctype_space($content)) if(ctype_space($content))
throw new \LengthException("Content length must be at least 1 character (not counting whitespaces)."); throw new \LengthException("Content length must be at least 1 character (not counting whitespaces).");
else if(iconv_strlen($content) > OPENVK_ROOT_CONF["openvk"]["preferences"]["wall"]["postSizes"]["maxSize"])
throw new \LengthException("Content is too large.");
$this->stateChanges("content", $content); $this->stateChanges("content", $content);
} }

View file

@ -5,15 +5,18 @@ trait TRichText
{ {
private function formatEmojis(string $text): string private function formatEmojis(string $text): string
{ {
if(iconv_strlen($this->getRecord()->content) > OPENVK_ROOT_CONF["openvk"]["preferences"]["wall"]["postSizes"]["emojiProcessingLimit"])
return $text;
$emojis = \Emoji\detect_emoji($text); $emojis = \Emoji\detect_emoji($text);
$replaced = []; # OVK-113 $replaced = []; # OVK-113
foreach($emojis as $emoji) { foreach($emojis as $emoji) {
$point = explode("-", strtolower($emoji["hex_str"]))[0]; $point = explode("-", strtolower($emoji["hex_str"]))[0];
if(in_array($point, $replaced)) if(in_array($point, $replaced))
continue; continue;
else else
$replaced[] = $point; $replaced[] = $point;
$image = "https://abs.twimg.com/emoji/v2/72x72/$point.png"; $image = "https://abs.twimg.com/emoji/v2/72x72/$point.png";
$image = "<img src='$image' alt='$emoji[emoji]' "; $image = "<img src='$image' alt='$emoji[emoji]' ";
$image .= "style='max-height:12px; padding-left: 2pt; padding-right: 2pt; vertical-align: bottom;' />"; $image .= "style='max-height:12px; padding-left: 2pt; padding-right: 2pt; vertical-align: bottom;' />";
@ -21,7 +24,7 @@ trait TRichText
$text = str_replace($emoji["emoji"], $image, $text); $text = str_replace($emoji["emoji"], $image, $text);
} }
return $text; return $text;
} }
private function removeZalgo(string $text): string private function removeZalgo(string $text): string
@ -32,18 +35,22 @@ trait TRichText
function getText(bool $html = true): string function getText(bool $html = true): string
{ {
$text = htmlentities($this->getRecord()->content, ENT_DISALLOWED | ENT_XHTML); $text = htmlentities($this->getRecord()->content, ENT_DISALLOWED | ENT_XHTML);
$proc = iconv_strlen($this->getRecord()->content) <= OPENVK_ROOT_CONF["openvk"]["preferences"]["wall"]["postSizes"]["processingLimit"];
if($html) { if($html) {
$rel = $this->isAd() ? "sponsored" : "ugc"; if($proc) {
$text = preg_replace( $rel = $this->isAd() ? "sponsored" : "ugc";
"%((https?|ftp):\/\/(\S*?\.\S*?))([\s)\[\]{},;\"\':<]|\.\s|$)%", $text = preg_replace(
"<a href='$1' rel='$rel' target='_blank'>$3</a>$4", "%((https?|ftp):\/\/(\S*?\.\S*?))([\s)\[\]{},;\"\':<]|\.\s|$)%",
$text "<a href='$1' rel='$rel' target='_blank'>$3</a>$4",
); $text
$text = preg_replace("%@(id|club)([0-9]++) \(([\p{L} 0-9]+)\)%Xu", "[$1$2|$3]", $text); );
$text = preg_replace("%@(id|club)([0-9]++)%Xu", "[$1$2|@$1$2]", $text); $text = preg_replace("%@(id|club)([0-9]++) \(([\p{L} 0-9]+)\)%Xu", "[$1$2|$3]", $text);
$text = preg_replace("%\[(id|club)([0-9]++)\|([\p{L} 0-9@]+)\]%Xu", "<a href='/$1$2'>$3</a>", $text); $text = preg_replace("%@(id|club)([0-9]++)%Xu", "[$1$2|@$1$2]", $text);
$text = preg_replace("%(#([\p{L}_-]++[0-9]*[\p{L}_-]*))%Xu", "<a href='/feed/hashtag/$2'>$1</a>", $text); $text = preg_replace("%\[(id|club)([0-9]++)\|([\p{L} 0-9@]+)\]%Xu", "<a href='/$1$2'>$3</a>", $text);
$text = $this->formatEmojis($text); $text = preg_replace("%(#([\p{L}_-]++[0-9]*[\p{L}_-]*))%Xu", "<a href='/feed/hashtag/$2'>$1</a>", $text);
$text = $this->formatEmojis($text);
}
$text = $this->removeZalgo($text); $text = $this->removeZalgo($text);
$text = nl2br($text); $text = nl2br($text);
} }

View file

@ -162,11 +162,13 @@ final class WallPresenter extends OpenVKPresenter
$canPost = $wallOwner->canPost(); $canPost = $wallOwner->canPost();
else else
$canPost = false; $canPost = false;
if(!$canPost) if(!$canPost)
$this->flashFail("err", "Ошибка доступа", "Вам нельзя писать на эту стену."); $this->flashFail("err", "Ошибка доступа", "Вам нельзя писать на эту стену.");
if(iconv_strlen($this->postParam("text") ?? "") > 280)
$this->flashFail("err", "Не удалось опубликовать пост", "Пост слишком большой.");
$flags = 0; $flags = 0;
if($this->postParam("as_group") === "on") if($this->postParam("as_group") === "on")
$flags |= 0b10000000; $flags |= 0b10000000;
@ -189,7 +191,7 @@ final class WallPresenter extends OpenVKPresenter
(new Albums)->getUserWallAlbum($wallOwner)->addPhoto($photo); (new Albums)->getUserWallAlbum($wallOwner)->addPhoto($photo);
} }
} catch(ISE $ex) { } catch(ISE $ex) {
$this->flashFail("err", "Не удалось опубликовать пост", "Файл повреждён."); $this->flashFail("err", "Не удалось опубликовать пост", "Файл изображения повреждён, слишком велик или одна сторона изображения в разы больше другой.");
} }
$post = new Post; $post = new Post;
@ -210,10 +212,10 @@ final class WallPresenter extends OpenVKPresenter
$post->setFlags($flags); $post->setFlags($flags);
$post->save(); $post->save();
} catch(\LogicException $ex) { } catch(\LogicException $ex) {
$this->flashFail("err", "Не удалось опубликовать пост", "Нельзя опубликовать пустой пост."); $this->flashFail("err", "Не удалось опубликовать пост", "Пост пустой или слишком большой.");
} }
} else { } else {
$this->flashFail("err", "Не удалось опубликовать пост", "Нельзя опубликовать пустой пост."); $this->flashFail("err", "Не удалось опубликовать пост", "Пост пустой или слишком большой.");
} }
if($wall > 0 && $wall !== $this->user->identity->getId()) if($wall > 0 && $wall !== $this->user->identity->getId())

View file

@ -22,6 +22,11 @@ openvk:
adminAccount: 1 # Change this ok adminAccount: 1 # Change this ok
messages: messages:
strict: false strict: false
wall:
postSizes:
maxSize: 60000
processingLimit: 3000
emojiProcessingLimit: 1000
menu: menu:
links: links: