kUtRL/inc/services/class.default.service.php

94 lines
2.6 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
*/
if (!defined('DC_RC_PATH')) {
return null;
}
2021-08-25 23:06:22 +00:00
# nb: "default" ne veut pas dire service par d<>faut
# mais service simple et rapide configur<75> par des constantes
2021-11-06 15:43:02 +00:00
# cela permet de configurer ces constantes dans le fichier
2021-08-25 23:06:22 +00:00
# config de Dotclear pour une plateforme compl<70>te.
class defaultKutrlService extends kutrlService
{
protected function init()
{
$this->config = [
2021-11-06 15:43:02 +00:00
'id' => 'default',
'name' => 'Default',
'home' => '',
2021-10-26 21:03:38 +00:00
'url_api' => SHORTEN_SERVICE_API,
'url_base' => SHORTEN_SERVICE_BASE,
'url_min_len' => strlen(SHORTEN_SERVICE_BASE) + 2,
2021-11-06 15:43:02 +00:00
'url_param' => SHORTEN_SERVICE_PARAM,
'url_encode' => SHORTEN_SERVICE_ENCODE
];
}
public function settingsForm()
{
2021-11-06 15:43:02 +00:00
echo
'<p class="form-note">' .
__('There is nothing to configure for this service.') .
2021-11-06 15:43:02 +00:00
'</p>' .
'<p>' . __('This service is set to:') . '</p>' .
2021-11-06 15:43:02 +00:00
'<dl>' .
'<dt>' . __('Service name:') . '</dt>' .
'<dd>' . SHORTEN_SERVICE_NAME . '</dd>' .
'<dt>' . __('Full API URL:') . '</dt>' .
'<dd>' . SHORTEN_SERVICE_API . '</dd>' .
'<dt>' . __('Query param:') . '</dt>' .
'<dd>' . SHORTEN_SERVICE_PARAM . '</dd>' .
'<dt>' . __('Short URL domain:') . '</dt>' .
'<dd>' . SHORTEN_SERVICE_BASE . '</dd>' .
'<dt>' . __('Encode URL:') . '</dt>' .
'<dd>' . (SHORTEN_SERVICE_ENCODE ? __('yes') : __('no')) . '</dd>' .
'</dl>';
}
public function testService()
{
$url = $this->url_encode ? urlencode($this->url_test) : $this->url_test;
2021-10-26 21:03:38 +00:00
$arg = [$this->url_param => urlencode($this->url_test)];
if (!self::post($this->url_api, $arg, true, true)) {
$this->error->add(__('Service is unavailable.'));
2021-10-26 21:03:38 +00:00
return false;
}
2021-10-26 21:03:38 +00:00
return true;
}
public function createHash($url, $hash = null)
{
$enc = $this->url_encode ? urlencode($url) : $url;
2021-10-26 21:03:38 +00:00
$arg = [$this->url_param => $url];
if (!($response = self::post($this->url_api, $arg, true, true))) {
$this->error->add(__('Service is unavailable.'));
2021-10-26 21:03:38 +00:00
return false;
}
2021-11-06 15:43:02 +00:00
$rs = new ArrayObject();
$rs->hash = str_replace($this->url_base, '', $response);
2021-10-26 21:03:38 +00:00
$rs->url = $url;
$rs->type = $this->id;
return $rs;
}
2021-11-06 15:43:02 +00:00
}