kUtRL/inc/class.kutrl.php

103 lines
2.5 KiB
PHP

<?php
/**
* @brief kUtRL, a plugin for Dotclear 2
*
* @package Dotclear
* @subpackage Plugin
*
* @author Jean-Christian Denis and contributors
*
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
if (!defined('DC_RC_PATH')) {
return null;
}
# Generic class to play easily with services
class kUtRL
{
# Load services list from behavior
public static function getServices(dcCore $core)
{
$list = $core->getBehaviors('kutrlService');
if (empty($list)) {
return [];
}
$services = [];
foreach ($list as $k => $callback) {
try {
list($service_id, $service_class) = call_user_func($callback);
$services[(string) $service_id] = (string) $service_class;
} catch (Exception $e) {
}
}
return $services;
}
# Silently try to load a service according to its id
# Return null on error else service on success
public static function quickService($id = '')
{
global $core;
try {
if (empty($id)) {
return null;
}
$services = self::getServices($core);
if (isset($services[$id])) {
return new $services[$id]($core);
}
} catch (Exception $e) {
}
return null;
}
# Silently try to load a service according to its place
# Return null on error else service on success
public static function quickPlace($place = 'plugin')
{
global $core;
try {
if (!in_array($place, ['tpl', 'wiki', 'admin', 'plugin'])) {
return null;
}
$id = $core->blog->settings->kUtRL->get('kutrl_' . $place . '_service');
if (!empty($id)) {
return self::quickService($id);
}
} catch (Exception $e) {
}
return null;
}
# Silently try to reduce url (using 'plugin' place)
# return long url on error else short url on success
public static function quickReduce($url, $custom = null, $place = 'plugin')
{
global $core;
try {
$srv = self::quickPlace($place);
if (empty($srv)) {
return $url;
}
$rs = $srv->hash($url, $custom);
if (empty($rs)) {
return $url;
}
return $srv->url_base . $rs->hash;
} catch (Exception $e) {
}
return $url;
}
}