From ad2b90daac0aa6ffed0b13105d9270ac5623007d Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Wed, 8 Feb 2023 19:39:18 +0700 Subject: [PATCH] Update code --- extend.php | 4 +- less/forum.less | 6 ++- src/Api/Controllers/ScrapperController.php | 47 ++++++++++------------ 3 files changed, 28 insertions(+), 29 deletions(-) diff --git a/extend.php b/extend.php index 56af847..93bfbc6 100644 --- a/extend.php +++ b/extend.php @@ -11,8 +11,8 @@ namespace Datlechin\LinkPreview; +use Datlechin\LinkPreview\Api\Controllers\ScrapperController; use Flarum\Extend; -use Flarum\Settings\Event\Deserializing; return [ (new Extend\Frontend('forum')) @@ -25,7 +25,7 @@ new Extend\Locales(__DIR__ . '/locale'), (new Extend\Routes('api')) - ->get('/datlechin-link-preview', 'datlechin-link-preview', Api\Controllers\ScrapperController::class), + ->get('/datlechin-link-preview', 'datlechin-link-preview', ScrapperController::class), (new Extend\Settings()) ->default('datlechin-link-preview.blacklist', '') diff --git a/less/forum.less b/less/forum.less index fe3e327..133e923 100644 --- a/less/forum.less +++ b/less/forum.less @@ -12,11 +12,15 @@ &-image { display: flex; - align-items: flex-start; + align-items: center; justify-content: center; width: 60px; padding: 5px 0; + @media @phone { + align-items: center; + } + img { width: 100%; height: auto; diff --git a/src/Api/Controllers/ScrapperController.php b/src/Api/Controllers/ScrapperController.php index c0feea1..ffd5f25 100644 --- a/src/Api/Controllers/ScrapperController.php +++ b/src/Api/Controllers/ScrapperController.php @@ -45,19 +45,17 @@ public function __construct( $this->translator = $translator; $this->cache = $cache; $cacheTime = $settings->get('datlechin-link-preview.cache_time'); - if (!is_numeric($cacheTime)) { + + if (! is_numeric($cacheTime)) { $cacheTime = 60; } + $this->cacheTime = intval($cacheTime); $this->settings = $settings; $this->blacklist = $this->getMultiDimensionalSetting('datlechin-link-preview.blacklist'); $this->whitelist = $this->getMultiDimensionalSetting('datlechin-link-preview.whitelist'); } - /* - * @param Request $request - * @return Response - */ public function handle(Request $request): Response { $url = $request->getQueryParams()['url'] ?? ''; @@ -69,6 +67,7 @@ public function handle(Request $request): Response } $normalizedUrl = preg_replace('/^https?:\/\/(.+?)\/?$/i', '$1', $url); + if ( ($this->whitelist && ! $this->inList($normalizedUrl, $this->whitelist)) || ($this->blacklist && $this->inList($normalizedUrl, $this->blacklist)) @@ -81,7 +80,8 @@ public function handle(Request $request): Response if ($this->cacheTime) { $cacheKey = 'datlechin-link-preview:' . md5($normalizedUrl); $data = $this->cache->get($cacheKey); - if (null !== $data) { + + if ($data) { return new JsonResponse($data); } } @@ -94,13 +94,14 @@ public function handle(Request $request): Response } try { - $this->web->go($url); + $web = $this->web; + $web->go($url); $data = [ - 'site_name' => $this->web->openGraph['og:site_name'] ?? $this->web->twitterCard['twitter:site'] ?? null, - 'title' => $this->web->title ?? $this->web->openGraph['og:title'] ?? $this->web->twitterCard['twitter:title'] ?? null, - 'description' => $this->web->description ?? $this->web->openGraph['og:description'] ?? $this->web->twitterCard['twitter:description'] ?? null, - 'image' => $this->web->image ?? $this->web->openGraph['og:image'] ?? $this->web->twitterCard['twitter:image'] ?? null, + 'site_name' => $web->openGraph['og:site_name'] ?? $web->twitterCard['twitter:site'] ?? null, + 'title' => $web->title ?? $web->openGraph['og:title'] ?? $web->twitterCard['twitter:title'] ?? null, + 'description' => $web->description ?? $web->openGraph['og:description'] ?? $web->twitterCard['twitter:description'] ?? null, + 'image' => $web->image ?? $web->openGraph['og:image'] ?? $web->twitterCard['twitter:image'] ?? null, 'accessed' => time(), ]; @@ -116,40 +117,34 @@ public function handle(Request $request): Response } } - /** - * @param string $setting - * @return array - */ private function getMultiDimensionalSetting(string $setting): array { $items = preg_split('/[,\\n]/', $this->settings->get($setting) ?? '') ?: []; + return array_filter(array_map('trim', $items)); } - /** - * @param string $needle - * @param array $haystack - */ private function inList(string $needle, array $haystack): bool { if (! $haystack) { return false; } + if (in_array($needle, $haystack, true)) { return true; } + foreach ($haystack as $item) { - $quoted = strtr( - preg_quote($item, '/'), - [ - '\\*' => '.*', - '\\?' => '.', - ] - ); + $quoted = strtr(preg_quote($item, '/'), [ + '\\*' => '.*', + '\\?' => '.', + ]); + if (preg_match("/$quoted/i", $needle)) { return true; } } + return false; } }