kUtRL/inc/class.kutrl.php

97 lines
2.5 KiB
PHP
Raw Normal View History

2021-08-25 23:06:22 +00:00
<?php
/**
* @brief kUtRL, a plugin for Dotclear 2
2021-11-06 15:43:02 +00:00
*
* @package Dotclear
* @subpackage Plugin
2021-11-06 15:43:02 +00:00
*
* @author Jean-Christian Denis and contributors
2021-11-06 15:43:02 +00:00
*
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
2021-08-27 22:06:46 +00:00
if (!defined('DC_RC_PATH')) {
return null;
}
2021-08-25 23:06:22 +00:00
# Generic class to play easily with services
class kUtRL
{
2021-08-27 22:06:46 +00:00
# Load services list from behavior
2022-11-20 16:15:36 +00:00
public static function getServices()
2021-08-27 22:06:46 +00:00
{
2022-11-20 16:15:36 +00:00
$list = dcCore::app()->getBehaviors('kutrlService');
2021-08-27 22:06:46 +00:00
if (empty($list)) {
return [];
}
2021-11-06 15:43:02 +00:00
$services = [];
foreach ($list as $k => $callback) {
2021-08-27 22:06:46 +00:00
try {
2022-11-20 16:15:36 +00:00
[$service_id, $service_class] = call_user_func($callback);
$services[(string) $service_id] = (string) $service_class;
2021-08-27 22:06:46 +00:00
} catch (Exception $e) {
}
}
2021-10-26 21:03:38 +00:00
2021-08-27 22:06:46 +00:00
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 = '')
{
try {
if (empty($id)) {
return null;
}
2022-11-20 16:15:36 +00:00
$services = self::getServices();
2021-08-27 22:06:46 +00:00
if (isset($services[$id])) {
2022-11-20 16:15:36 +00:00
return new $services[$id]();
2021-08-27 22:06:46 +00:00
}
2021-11-06 15:43:02 +00:00
} catch (Exception $e) {
2021-08-27 22:06:46 +00:00
}
2021-10-26 21:03:38 +00:00
2021-11-06 15:43:02 +00:00
return null;
2021-08-27 22:06:46 +00:00
}
# Silently try to load a service according to its place
# Return null on error else service on success
public static function quickPlace($place = 'plugin')
{
try {
if (!in_array($place, ['tpl', 'wiki', 'admin', 'plugin'])) {
return null;
}
2022-12-22 14:37:09 +00:00
$id = dcCore::app()->blog->settings->get(basename(dirname(__DIR__)))->get('kutrl_' . $place . '_service');
2021-08-27 22:06:46 +00:00
if (!empty($id)) {
return self::quickService($id);
}
2021-11-06 15:43:02 +00:00
} catch (Exception $e) {
2021-08-27 22:06:46 +00:00
}
2021-10-26 21:03:38 +00:00
2021-11-06 15:43:02 +00:00
return null;
2021-08-27 22:06:46 +00:00
}
2021-08-27 22:06:46 +00:00
# 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')
{
try {
$srv = self::quickPlace($place);
if (empty($srv)) {
return $url;
}
2021-11-06 15:43:02 +00:00
$rs = $srv->hash($url, $custom);
2021-08-27 22:06:46 +00:00
if (empty($rs)) {
return $url;
}
2021-11-06 15:43:02 +00:00
return $srv->url_base . $rs->hash;
} catch (Exception $e) {
2021-08-27 22:06:46 +00:00
}
2021-10-26 21:03:38 +00:00
2021-11-06 15:43:02 +00:00
return $url;
2021-08-27 22:06:46 +00:00
}
2021-11-06 15:43:02 +00:00
}