+++ /dev/null
-<?php
-
-namespace App\Fluidbook;
-
-use GuzzleHttp\Client;
-
-class LinkShortener
-{
-
- public static function getAvaiableShorteners()
- {
- $res = [];
- foreach (\App\Models\LinkShortener::all() as $server) {
- $res[$server->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());
- }
-}
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();
$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());
}
}
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;