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

128 lines
3.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-11-06 15:43:02 +00:00
if (!defined('DC_RC_PATH')) {
return;
}
2021-08-25 23:06:22 +00:00
class trimKutrlService extends kutrlService
{
2021-11-06 15:43:02 +00:00
protected $config = [
'id' => 'trim',
'name' => 'tr.im',
'home' => 'http://tr.im',
2021-11-06 15:43:02 +00:00
'url_api' => 'http://api.tr.im/v1/',
'url_base' => 'http://tr.im/',
'url_min_len' => 25
2021-11-06 15:43:02 +00:00
];
2021-11-06 15:43:02 +00:00
private $args = [
'username' => '',
'password' => ''
2021-11-06 15:43:02 +00:00
];
private $api_rate_time = 0;
protected function init()
{
$this->args['username'] = $this->settings->kutrl_srv_trim_username;
$this->args['password'] = $this->settings->kutrl_srv_trim_password;
2021-11-06 15:43:02 +00:00
$this->api_rate_time = (int) $this->settings->kutrl_srv_trim_apiratetime;
}
public function saveSettings()
{
2021-11-06 15:43:02 +00:00
$this->settings->put('kutrl_srv_trim_username', $_POST['kutrl_srv_trim_username']);
$this->settings->put('kutrl_srv_trim_password', $_POST['kutrl_srv_trim_password']);
}
public function settingsForm()
{
2021-11-06 15:43:02 +00:00
echo
'<p><label class="classic">' . __('Login:') . '<br />' .
form::field(['kutrl_srv_trim_username'], 50, 255, $this->settings->kutrl_srv_trim_username) .
'</label></p>' .
'<p class="form-note">' .
__('This is your login to sign up to tr.im.') .
'</p>' .
'<p><label class="classic">' . __('Password:') . '<br />' .
form::field(['kutrl_srv_trim_password'], 50, 255, $this->settings->kutrl_srv_trim_password) .
'</label></p>' .
'<p class="form-note">' .
__('This is your password to sign up to tr.im.') .
'</p>';
}
public function testService()
{
2021-11-06 15:43:02 +00:00
if (empty($this->args['username']) || empty($this->args['password'])) {
$this->error->add(__('Service is not well configured.'));
2021-11-06 15:43:02 +00:00
return false;
}
2021-11-06 15:43:02 +00:00
if (time() < $this->api_rate_time + 300) { // bloc service within 5min on API rate limit
$this->error->add(__('Prevent service rate limit.'));
2021-11-06 15:43:02 +00:00
return false;
}
2021-11-06 15:43:02 +00:00
if (!($rsp = self::post($this->url_api . 'verify.xml', $this->args, true, true))) {
$this->error->add(__('Service is unavailable.'));
2021-11-06 15:43:02 +00:00
return false;
}
$r = simplexml_load_string($rsp);
2021-11-06 15:43:02 +00:00
if ($r['code'] == 200) {
return true;
}
$this->error->add(__('Authentication to service failed.'));
2021-11-06 15:43:02 +00:00
return false;
}
2021-11-06 15:43:02 +00:00
public function createHash($url, $hash = null)
{
2021-11-06 15:43:02 +00:00
$arg = $this->args;
$arg['url'] = $url;
2021-11-06 15:43:02 +00:00
if (!($rsp = self::post($this->url_api . 'trim_url.xml', $arg, true, true))) {
$this->error->add(__('Service is unavailable.'));
2021-11-06 15:43:02 +00:00
return false;
}
$r = simplexml_load_string($rsp);
# API rate limit
2021-11-06 15:43:02 +00:00
if ($r['code'] == 425) {
$this->settings->put('kutrl_srv_trim_apiratetime', time());
$this->error->add(__('Service rate limit exceeded.'));
2021-11-06 15:43:02 +00:00
return false;
}
2021-11-06 15:43:02 +00:00
if (isset($r->trimpath)) {
$rs = new ArrayObject();
$rs->hash = $r->trimpath;
2021-11-06 15:43:02 +00:00
$rs->url = $url;
$rs->type = $this->id;
return $rs;
}
$this->error->add(__('Unreadable service response.'));
2021-11-06 15:43:02 +00:00
return false;
}
2021-11-06 15:43:02 +00:00
}