From 9040c611bcdda1f56f6617e8ec452712c98fed92 Mon Sep 17 00:00:00 2001 From: Avatar4eg Date: Thu, 11 Aug 2016 13:54:43 +0300 Subject: [PATCH] Strip newlines in static context (some socials convert \n to
and displays them). --- src/Listener/AddClientAssets.php | 6 ++++++ src/Listener/AddHeadData.php | 34 +++++++++++++++++++++++++++----- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/src/Listener/AddClientAssets.php b/src/Listener/AddClientAssets.php index db95789..e55d9e2 100644 --- a/src/Listener/AddClientAssets.php +++ b/src/Listener/AddClientAssets.php @@ -17,6 +17,9 @@ public function subscribe(Dispatcher $events) $events->listen(ConfigureLocales::class, [$this, 'addLocales']); } + /** + * @param ConfigureClientView $event + */ public function addAssets(ConfigureClientView $event) { if($event->isForum()) { @@ -35,6 +38,9 @@ public function addAssets(ConfigureClientView $event) } } + /** + * @param ConfigureLocales $event + */ public function addLocales(ConfigureLocales $event) { foreach (new DirectoryIterator(__DIR__ .'/../../locale') as $file) { diff --git a/src/Listener/AddHeadData.php b/src/Listener/AddHeadData.php index 38b8cf2..b10652e 100644 --- a/src/Listener/AddHeadData.php +++ b/src/Listener/AddHeadData.php @@ -41,12 +41,18 @@ public function __construct(SettingsRepositoryInterface $settings, UrlGenerator $this->urlGenerator = $urlGenerator; } + /** + * @param Dispatcher $events + */ public function subscribe(Dispatcher $events) { $events->listen(ConfigureClientView::class, [$this, 'addMetaTags']); $events->listen(PrepareApiData::class, [$this, 'addDiscussionMetaTags']); } - + + /** + * @param ConfigureClientView $event + */ public function addMetaTags(ConfigureClientView $event) { if ($event->isForum()) { @@ -63,7 +69,7 @@ public function addMetaTags(ConfigureClientView $event) if ($this->openGraph || $this->twitterCard) { $dataTitle = htmlspecialchars($this->settings->get('welcome_title'), ENT_QUOTES|ENT_HTML5|ENT_DISALLOWED|ENT_SUBSTITUTE, 'UTF-8'); $dataUrl = $this->urlGenerator->toBase(); - $dataDescription = htmlspecialchars($this->settings->get('forum_description'), ENT_QUOTES|ENT_HTML5|ENT_DISALLOWED|ENT_SUBSTITUTE, 'UTF-8'); + $dataDescription = $this->plainText($this->settings->get('forum_description'), 150); } if ($this->openGraph) { @@ -82,6 +88,9 @@ public function addMetaTags(ConfigureClientView $event) } } + /** + * @param PrepareApiData $event + */ public function addDiscussionMetaTags(PrepareApiData $event) { if ($this->clientView && $event->isController(ShowDiscussionController::class)) { @@ -91,9 +100,7 @@ public function addDiscussionMetaTags(PrepareApiData $event) $dataUrl = $this->urlGenerator->toRoute('discussion', ['id' => $event->data->id]); } if ($event->data->startPost) { - $dataDescription = strip_tags($event->data->startPost->content); - $dataDescription = strlen($dataDescription) > 150 ? substr($dataDescription, 0, 150) . '...' : $dataDescription; - $dataDescription = htmlspecialchars($dataDescription, ENT_QUOTES|ENT_HTML5|ENT_DISALLOWED|ENT_SUBSTITUTE, 'UTF-8'); + $dataDescription = $this->plainText($event->data->startPost->content, 150); } //$this->clientView->addHeadString('', 'description'); @@ -109,4 +116,21 @@ public function addDiscussionMetaTags(PrepareApiData $event) } } } + + /** + * @param string $text + * @param int|null $length + * @return string + */ + protected function plainText($text, $length = null) + { + $text = strip_tags($text); + $text = preg_replace('/\s+/', ' ', $text); + $text = htmlspecialchars($text, ENT_QUOTES|ENT_HTML5|ENT_DISALLOWED|ENT_SUBSTITUTE, 'UTF-8'); + if ($length !== null) { + $text = strlen($text) > $length ? substr($text, 0, $length) . '...' : $text; + } + + return $text; + } }