From 65017c5d91658eebd5b722fe8aba050c4b0f5153 Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Fri, 30 Jan 2026 17:14:31 +0100 Subject: [PATCH] wip #7868 @0.5 --- app/Fluidbook/Compiler/Links.php | 4 +- app/Fluidbook/LinkShortener.php | 46 ------------------ app/Models/LinkShortener.php | 57 ++++++++++++++++++++++- app/Models/Traits/PublicationSettings.php | 4 +- 4 files changed, 57 insertions(+), 54 deletions(-) delete mode 100644 app/Fluidbook/LinkShortener.php diff --git a/app/Fluidbook/Compiler/Links.php b/app/Fluidbook/Compiler/Links.php index 35e78b02e..4ee4a8750 100644 --- a/app/Fluidbook/Compiler/Links.php +++ b/app/Fluidbook/Compiler/Links.php @@ -2,11 +2,9 @@ namespace App\Fluidbook\Compiler; -use App\Fluidbook\Farm; use App\Fluidbook\Link\Link; use App\Fluidbook\Link\LinksData; -use App\Fluidbook\LinkShortener; -use App\Jobs\OCR; +use App\Models\LinkShortener; use App\SubForms\Link\Base; use Cubist\Util\Text; use Cubist\Util\Url; diff --git a/app/Fluidbook/LinkShortener.php b/app/Fluidbook/LinkShortener.php deleted file mode 100644 index cc14aec9e..000000000 --- a/app/Fluidbook/LinkShortener.php +++ /dev/null @@ -1,46 +0,0 @@ -id] = $server->url; - } - return $res; - } - - 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/Models/LinkShortener.php b/app/Models/LinkShortener.php index c7e219134..ce41540f6 100644 --- a/app/Models/LinkShortener.php +++ b/app/Models/LinkShortener.php @@ -4,19 +4,22 @@ namespace App\Models; use App\Models\Base\ToolboxModel; use Cubist\Backpack\Magic\Fields\Text; +use GuzzleHttp\Client; class LinkShortener extends ToolboxModel { protected $table = 'link_shortener'; protected $_options = ['name' => 'linkshortener', - 'singular' => 'serveur de liens courts', - 'plural' => 'serveurs de liens courts',]; + 'singular' => 'raccourcisseur de liens', + 'plural' => 'raccourcisseurs de liens',]; protected $_enableRevisions = false; protected static $_permissionBase = 'linkshortener'; protected static $_ownerAttribute = 'owner'; + protected static $_cache = []; + public function setFields() { parent::setFields(); @@ -24,6 +27,56 @@ class LinkShortener extends ToolboxModel $this->addField('url', Text::class, __('URL'), ['column' => true]); $this->addOwnerField(); $this->addField('api_key', Text::class, __('Clé d\'API')); + } + + public static function getAvaiableShorteners() + { + $res = []; + foreach (self::all() as $server) { + $res[$server->id] = $server->url; + } + return $res; + } + + public static function getShortener($id) + { + if (!isset(self::$_cache[$id])) { + $s = self::withoutGlobalScopes()->find($id); + if ($s === null) { + self::$_cache[$id] = false; + } else { + self::$_cache[$id] = ['key' => $s->api_key, 'url' => $s->url]; + } + } + return self::$_cache[$id]; + } + + 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') + { + $s = self::getShortener($shortener); + if (!$s) { + throw new \Exception("shortener " . $shortener . " not available"); + } + + $apiURL = 'https://' . $s['url'] . '/yourls-api.php'; + $data['action'] = $action; + $data['format'] = $format; + $data['hash'] = 'sha512'; + $data['timestamp'] = time(); + $data['signature'] = hash($data['hash'], $data['timestamp'] . $s['key']); + $client = new Client(); + $response = $client->request('POST', $apiURL, ['form_params' => $data, 'http_errors' => false]); + return json_decode($response->getBody()); } } diff --git a/app/Models/Traits/PublicationSettings.php b/app/Models/Traits/PublicationSettings.php index 21595a181..650ff5b7f 100644 --- a/app/Models/Traits/PublicationSettings.php +++ b/app/Models/Traits/PublicationSettings.php @@ -4,14 +4,12 @@ namespace App\Models\Traits; use App\Fields\FluidbookDevelopmentVersion; use App\Fields\FluidbookFont; -use App\Fields\FluidbookLocale; use App\Fields\FluidbookLocaleCurrent; use App\Fields\FluidbookSignature; use App\Fields\FluidbookTTSVoice; use App\Fields\SCORMVersion; -use App\Fluidbook\LinkShortener; -use App\Models\File; use App\Models\FluidbookExternalInstallServer; +use App\Models\LinkShortener; use Cubist\Backpack\Magic\Fields\Checkbox; use Cubist\Backpack\Magic\Fields\Code; use Cubist\Backpack\Magic\Fields\Email; -- 2.39.5