From 7ba356269eaf9523acf41e4ef96f041d7d5f1994 Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Tue, 20 Jan 2026 18:23:22 +0100 Subject: [PATCH] wip #7868 @1 --- app/Fluidbook/Compiler/Links.php | 7 ++++ app/Fluidbook/LinkShortener.php | 34 ++++++++++++++++++- .../FluidbookAuditLinkCrudController.php | 28 +++++++++++++++ .../FluidbookCollectionCrudController.php | 1 + .../FluidbookPublicationCrudController.php | 1 + app/Models/Traits/PublicationSettings.php | 3 +- 6 files changed, 71 insertions(+), 3 deletions(-) create mode 100644 app/Http/Controllers/Admin/FluidbookAuditLinkCrudController.php diff --git a/app/Fluidbook/Compiler/Links.php b/app/Fluidbook/Compiler/Links.php index d256e9fc1..e7e323c94 100644 --- a/app/Fluidbook/Compiler/Links.php +++ b/app/Fluidbook/Compiler/Links.php @@ -529,5 +529,12 @@ trait Links $this->config->push('triggersLinks', ['page' => $page, 'link' => $link, 'delay' => $delay]); } + public function shortenURL($url) + { + if ($this->config->linkShortener === 'none' || !Url::isDistant($url)) { + return $url; + } + return $url; + } } diff --git a/app/Fluidbook/LinkShortener.php b/app/Fluidbook/LinkShortener.php index 3d89617bb..65102b52a 100644 --- a/app/Fluidbook/LinkShortener.php +++ b/app/Fluidbook/LinkShortener.php @@ -2,10 +2,42 @@ namespace App\Fluidbook; +use GuzzleHttp\Client; + class LinkShortener { + protected static $shorteners = ['l.fluidbook.com' => '324507fb8d']; + public static function getAvaiableShorteners() { - return ['l.fluidbook.com']; + return array_keys(self::$shorteners); + } + + public static function shorturl($url, $shortener) + { + return cache()->remember('shorturl_' . $shortener . '_' . $url, 3600, function () use ($url, $shortener) { + $res = self::_request('shorturl', ['url' => $url], $shortener); + if ($res->shorturl) { + return $res->shorturl; + } + }); + } + + protected static function _request($action, $data, $shortener, $format = 'json') + { + if (!isset(self::$shorteners[$shortener])) { + throw new \Exception("shortener " . $shortener . " not available"); + } + + $apiURL = 'https://' . $shortener . '/yourls-api.php'; + $data['action'] = $action; + $data['format'] = $format; + $data['hash'] = 'sha512'; + $data['timestamp'] = time(); + $data['signature'] = hash($data['hash'], $data['timestamp'] . self::$shorteners[$shortener]); + + $client = new Client(); + $response = $client->request('POST', $apiURL, ['form_params' => $data, 'http_errors' => false]); + return json_decode($response->getBody()); } } diff --git a/app/Http/Controllers/Admin/FluidbookAuditLinkCrudController.php b/app/Http/Controllers/Admin/FluidbookAuditLinkCrudController.php new file mode 100644 index 000000000..b3f13e132 --- /dev/null +++ b/app/Http/Controllers/Admin/FluidbookAuditLinkCrudController.php @@ -0,0 +1,28 @@ +_addSettingField('', FormSeparator::class); $this->_addSettingField('autolinkRule', SelectFromArray::class, $this->__('Règle pour l\'ajout des liens via excel'), ["default" => "web", 'options' => ['web' => 'Lien web', 'cart' => __('Lien panier'), 'steelite' => 'Steelite']]); $this->_addSettingField('', FormSeparator::class); - $shorteners = LinkShortener::getAvaiableShorteners(); $options = ['none' => $this->__('Aucun')]; - foreach ($shorteners as $shortener) { + foreach (LinkShortener::getAvaiableShorteners() as $shortener) { $options[$shortener] = $shortener; } $this->_addSettingField('linkShortener', SelectFromArray::class, $this->__('Raccourcisseur de liens'), ['default' => 'none', 'options' => $options]); -- 2.39.5