From efef44622088293a5bb038c902ee3ed4f32f41f7 Mon Sep 17 00:00:00 2001 From: Alma Armas Date: Thu, 20 Aug 2020 09:03:45 +0000 Subject: [PATCH 1/2] Make eventdb optional --- Web/Models/Repositories/Notifications.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Web/Models/Repositories/Notifications.php b/Web/Models/Repositories/Notifications.php index 53a4f1fc..4ae4806e 100644 --- a/Web/Models/Repositories/Notifications.php +++ b/Web/Models/Repositories/Notifications.php @@ -47,7 +47,7 @@ class Notifications { $db = $this->getEDB(false); if(!$db) - return 1; + return 0; $results = $db->query($this->getQuery($user, true, $offset, $archived)); @@ -56,6 +56,12 @@ class Notifications function getNotificationsByUser(User $user, int $offset, bool $archived = false, int $page = 1, ?int $perPage = NULL): \Traversable { + $db = $this->getEDB(false); + if(!$db) { + yield from []; + return; + } + $results = $this->getEDB()->query($this->getQuery($user, false, $offset, $archived, $page, $perPage)); foreach($results->fetchAll() as $notif) { $originModel = $this->getModel($notif->originModelType, $notif->originModelId); From fdb8b35609f47b3374f8fc95f85abbf60f4dff87 Mon Sep 17 00:00:00 2001 From: Alma Armas Date: Thu, 20 Aug 2020 12:58:40 +0000 Subject: [PATCH 2/2] Add post size limits --- Web/Models/Entities/Post.php | 2 ++ Web/Models/Entities/Traits/TRichText.php | 45 ++++++++++++++---------- Web/Presenters/WallPresenter.php | 10 +++--- openvk-example.yml | 5 +++ 4 files changed, 39 insertions(+), 23 deletions(-) diff --git a/Web/Models/Entities/Post.php b/Web/Models/Entities/Post.php index 781bbb56..baa93c3e 100644 --- a/Web/Models/Entities/Post.php +++ b/Web/Models/Entities/Post.php @@ -86,6 +86,8 @@ class Post extends Postable { if(ctype_space($content)) 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); } diff --git a/Web/Models/Entities/Traits/TRichText.php b/Web/Models/Entities/Traits/TRichText.php index 8026506a..6bb7f304 100644 --- a/Web/Models/Entities/Traits/TRichText.php +++ b/Web/Models/Entities/Traits/TRichText.php @@ -5,15 +5,18 @@ trait TRichText { 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); - $replaced = []; # OVK-113 + $replaced = []; # OVK-113 foreach($emojis as $emoji) { - $point = explode("-", strtolower($emoji["hex_str"]))[0]; - if(in_array($point, $replaced)) - continue; - else - $replaced[] = $point; - + $point = explode("-", strtolower($emoji["hex_str"]))[0]; + if(in_array($point, $replaced)) + continue; + else + $replaced[] = $point; + $image = "https://abs.twimg.com/emoji/v2/72x72/$point.png"; $image = "$emoji[emoji]getRecord()->content, ENT_DISALLOWED | ENT_XHTML); + $proc = iconv_strlen($this->getRecord()->content) <= OPENVK_ROOT_CONF["openvk"]["preferences"]["wall"]["postSizes"]["processingLimit"]; if($html) { - $rel = $this->isAd() ? "sponsored" : "ugc"; - $text = preg_replace( - "%((https?|ftp):\/\/(\S*?\.\S*?))([\s)\[\]{},;\"\':<]|\.\s|$)%", - "$3$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", "$3", $text); - $text = preg_replace("%(#([\p{L}_-]++[0-9]*[\p{L}_-]*))%Xu", "$1", $text); - $text = $this->formatEmojis($text); + if($proc) { + $rel = $this->isAd() ? "sponsored" : "ugc"; + $text = preg_replace( + "%((https?|ftp):\/\/(\S*?\.\S*?))([\s)\[\]{},;\"\':<]|\.\s|$)%", + "$3$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", "$3", $text); + $text = preg_replace("%(#([\p{L}_-]++[0-9]*[\p{L}_-]*))%Xu", "$1", $text); + $text = $this->formatEmojis($text); + } + $text = $this->removeZalgo($text); $text = nl2br($text); } diff --git a/Web/Presenters/WallPresenter.php b/Web/Presenters/WallPresenter.php index 28c74880..62383868 100644 --- a/Web/Presenters/WallPresenter.php +++ b/Web/Presenters/WallPresenter.php @@ -162,11 +162,13 @@ final class WallPresenter extends OpenVKPresenter $canPost = $wallOwner->canPost(); else $canPost = false; - if(!$canPost) $this->flashFail("err", "Ошибка доступа", "Вам нельзя писать на эту стену."); + if(iconv_strlen($this->postParam("text") ?? "") > 280) + $this->flashFail("err", "Не удалось опубликовать пост", "Пост слишком большой."); + $flags = 0; if($this->postParam("as_group") === "on") $flags |= 0b10000000; @@ -189,7 +191,7 @@ final class WallPresenter extends OpenVKPresenter (new Albums)->getUserWallAlbum($wallOwner)->addPhoto($photo); } } catch(ISE $ex) { - $this->flashFail("err", "Не удалось опубликовать пост", "Файл повреждён."); + $this->flashFail("err", "Не удалось опубликовать пост", "Файл изображения повреждён, слишком велик или одна сторона изображения в разы больше другой."); } $post = new Post; @@ -210,10 +212,10 @@ final class WallPresenter extends OpenVKPresenter $post->setFlags($flags); $post->save(); } catch(\LogicException $ex) { - $this->flashFail("err", "Не удалось опубликовать пост", "Нельзя опубликовать пустой пост."); + $this->flashFail("err", "Не удалось опубликовать пост", "Пост пустой или слишком большой."); } } else { - $this->flashFail("err", "Не удалось опубликовать пост", "Нельзя опубликовать пустой пост."); + $this->flashFail("err", "Не удалось опубликовать пост", "Пост пустой или слишком большой."); } if($wall > 0 && $wall !== $this->user->identity->getId()) diff --git a/openvk-example.yml b/openvk-example.yml index 74ac69da..b09a45d7 100644 --- a/openvk-example.yml +++ b/openvk-example.yml @@ -22,6 +22,11 @@ openvk: adminAccount: 1 # Change this ok messages: strict: false + wall: + postSizes: + maxSize: 60000 + processingLimit: 3000 + emojiProcessingLimit: 1000 menu: links: