2021-08-25 23:06:22 +00:00
|
|
|
<?php
|
2021-09-02 12:32:26 +00:00
|
|
|
/**
|
|
|
|
* @brief kUtRL, a plugin for Dotclear 2
|
2021-11-06 15:43:02 +00:00
|
|
|
*
|
2021-09-02 12:32:26 +00:00
|
|
|
* @package Dotclear
|
|
|
|
* @subpackage Plugin
|
2021-11-06 15:43:02 +00:00
|
|
|
*
|
2021-09-02 12:32:26 +00:00
|
|
|
* @author Jean-Christian Denis and contributors
|
2021-11-06 15:43:02 +00:00
|
|
|
*
|
2021-09-02 12:32:26 +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 = [
|
2022-12-22 14:37:09 +00:00
|
|
|
'id' => 'trim',
|
|
|
|
'name' => 'tr.im',
|
|
|
|
'home' => 'http://tr.im',
|
2021-09-02 12:32:26 +00:00
|
|
|
|
2021-11-06 15:43:02 +00:00
|
|
|
'url_api' => 'http://api.tr.im/v1/',
|
|
|
|
'url_base' => 'http://tr.im/',
|
2022-11-20 16:15:36 +00:00
|
|
|
'url_min_len' => 25,
|
2021-11-06 15:43:02 +00:00
|
|
|
];
|
2021-09-02 12:32:26 +00:00
|
|
|
|
2021-11-06 15:43:02 +00:00
|
|
|
private $args = [
|
2021-09-02 12:32:26 +00:00
|
|
|
'username' => '',
|
2022-11-20 16:15:36 +00:00
|
|
|
'password' => '',
|
2021-11-06 15:43:02 +00:00
|
|
|
];
|
2021-09-02 12:32:26 +00:00
|
|
|
|
|
|
|
private $api_rate_time = 0;
|
|
|
|
|
|
|
|
protected function init()
|
|
|
|
{
|
2022-12-22 20:18:49 +00:00
|
|
|
$this->args['username'] = $this->settings->get('srv_trim_username');
|
|
|
|
$this->args['password'] = $this->settings->get('srv_trim_password');
|
2021-09-02 12:32:26 +00:00
|
|
|
|
2022-12-22 20:18:49 +00:00
|
|
|
$this->api_rate_time = (int) $this->settings->get('srv_trim_apiratetime');
|
2021-09-02 12:32:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function saveSettings()
|
|
|
|
{
|
2022-12-22 20:18:49 +00:00
|
|
|
$this->settings->put('srv_trim_username', $_POST['kutrl_srv_trim_username']);
|
|
|
|
$this->settings->put('srv_trim_password', $_POST['kutrl_srv_trim_password']);
|
2021-09-02 12:32:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function settingsForm()
|
|
|
|
{
|
2021-11-06 15:43:02 +00:00
|
|
|
echo
|
|
|
|
'<p><label class="classic">' . __('Login:') . '<br />' .
|
2022-12-22 20:18:49 +00:00
|
|
|
form::field(['kutrl_srv_trim_username'], 50, 255, $this->settings->get('srv_trim_username')) .
|
2021-11-06 15:43:02 +00:00
|
|
|
'</label></p>' .
|
|
|
|
'<p class="form-note">' .
|
|
|
|
__('This is your login to sign up to tr.im.') .
|
|
|
|
'</p>' .
|
|
|
|
'<p><label class="classic">' . __('Password:') . '<br />' .
|
2022-12-22 20:18:49 +00:00
|
|
|
form::field(['kutrl_srv_trim_password'], 50, 255, $this->settings->get('srv_trim_password')) .
|
2021-11-06 15:43:02 +00:00
|
|
|
'</label></p>' .
|
|
|
|
'<p class="form-note">' .
|
|
|
|
__('This is your password to sign up to tr.im.') .
|
2021-09-02 12:32:26 +00:00
|
|
|
'</p>';
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testService()
|
|
|
|
{
|
2021-11-06 15:43:02 +00:00
|
|
|
if (empty($this->args['username']) || empty($this->args['password'])) {
|
2021-09-02 12:32:26 +00:00
|
|
|
$this->error->add(__('Service is not well configured.'));
|
2021-11-06 15:43:02 +00:00
|
|
|
|
2021-09-02 12:32:26 +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
|
2021-09-02 12:32:26 +00:00
|
|
|
$this->error->add(__('Prevent service rate limit.'));
|
2021-11-06 15:43:02 +00:00
|
|
|
|
|
|
|
return false;
|
2021-09-02 12:32:26 +00:00
|
|
|
}
|
2021-11-06 15:43:02 +00:00
|
|
|
if (!($rsp = self::post($this->url_api . 'verify.xml', $this->args, true, true))) {
|
2021-09-02 12:32:26 +00:00
|
|
|
$this->error->add(__('Service is unavailable.'));
|
2021-11-06 15:43:02 +00:00
|
|
|
|
2021-09-02 12:32:26 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$r = simplexml_load_string($rsp);
|
|
|
|
|
2021-11-06 15:43:02 +00:00
|
|
|
if ($r['code'] == 200) {
|
2021-09-02 12:32:26 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
$this->error->add(__('Authentication to service failed.'));
|
2021-11-06 15:43:02 +00:00
|
|
|
|
2021-09-02 12:32:26 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-11-06 15:43:02 +00:00
|
|
|
public function createHash($url, $hash = null)
|
2021-09-02 12:32:26 +00:00
|
|
|
{
|
2021-11-06 15:43:02 +00:00
|
|
|
$arg = $this->args;
|
2021-09-02 12:32:26 +00:00
|
|
|
$arg['url'] = $url;
|
|
|
|
|
2021-11-06 15:43:02 +00:00
|
|
|
if (!($rsp = self::post($this->url_api . 'trim_url.xml', $arg, true, true))) {
|
2021-09-02 12:32:26 +00:00
|
|
|
$this->error->add(__('Service is unavailable.'));
|
2021-11-06 15:43:02 +00:00
|
|
|
|
2021-09-02 12:32:26 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$r = simplexml_load_string($rsp);
|
|
|
|
|
|
|
|
# API rate limit
|
2021-11-06 15:43:02 +00:00
|
|
|
if ($r['code'] == 425) {
|
2022-12-22 20:18:49 +00:00
|
|
|
$this->settings->put('srv_trim_apiratetime', time());
|
2021-09-02 12:32:26 +00:00
|
|
|
|
|
|
|
$this->error->add(__('Service rate limit exceeded.'));
|
2021-11-06 15:43:02 +00:00
|
|
|
|
2021-09-02 12:32:26 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-11-06 15:43:02 +00:00
|
|
|
if (isset($r->trimpath)) {
|
|
|
|
$rs = new ArrayObject();
|
2021-09-02 12:32:26 +00:00
|
|
|
$rs->hash = $r->trimpath;
|
2021-11-06 15:43:02 +00:00
|
|
|
$rs->url = $url;
|
2021-09-02 12:32:26 +00:00
|
|
|
$rs->type = $this->id;
|
|
|
|
|
|
|
|
return $rs;
|
|
|
|
}
|
|
|
|
$this->error->add(__('Unreadable service response.'));
|
2021-11-06 15:43:02 +00:00
|
|
|
|
2021-09-02 12:32:26 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-11-06 15:43:02 +00:00
|
|
|
}
|