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

131 lines
4.0 KiB
PHP
Raw Normal View History

2021-08-25 23:06:22 +00:00
<?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
*/
2021-08-25 23:06:22 +00:00
if (!defined('DC_RC_PATH')) {
return null;
}
2021-08-25 23:06:22 +00:00
class bitlyKutrlService extends kutrlService
{
protected $config = [
2021-10-26 21:03:38 +00:00
'id' => 'bitly',
'name' => 'bit.ly',
'home' => 'http://bit.ly',
2021-10-26 21:03:38 +00:00
'url_api' => 'http://api.bit.ly/v3/',
'url_base' => 'http://bit.ly/',
'url_min_len' => 25
];
private $args = [
2021-10-26 21:03:38 +00:00
'format' => 'xml',
'login' => '',
'apiKey' => '',
'history' => 0
];
protected function init()
{
2021-10-26 21:03:38 +00:00
$this->args['login'] = $this->settings->kutrl_srv_bitly_login;
$this->args['apiKey'] = $this->settings->kutrl_srv_bitly_apikey;
$this->args['history'] = $this->settings->kutrl_srv_bitly_history ? 1 : 0;
}
public function saveSettings()
{
$this->settings->put('kutrl_srv_bitly_login', $_POST['kutrl_srv_bitly_login']);
$this->settings->put('kutrl_srv_bitly_apikey', $_POST['kutrl_srv_bitly_apikey']);
$this->settings->put('kutrl_srv_bitly_history', isset($_POST['kutrl_srv_bitly_history']));
}
public function settingsForm()
{
echo
'<p><label class="classic">' . __('Login:') . '<br />' .
form::field(['kutrl_srv_bitly_login'], 50, 255, $this->settings->kutrl_srv_bitly_login) .
'</label></p>' .
'<p class="form-note">' .
sprintf(__('This is your login to sign up to %s'), $this->config['name']) .
'</p>' .
'<p><label class="classic">' . __('API Key:') . '<br />' .
form::field(['kutrl_srv_bitly_apikey'], 50, 255, $this->settings->kutrl_srv_bitly_apikey) .
'</label></p>' .
'<p class="form-note">' .
sprintf(__('This is your personnal %s API key. You can find it on your account page.'), $this->config['name']) .
'</p>' .
'<p><label class="classic">' .
form::checkbox(['kutrl_srv_bitly_history'], '1', $this->settings->kutrl_srv_bitly_history) . ' ' .
__('Publish history') .
'</label></p>' .
'<p class="form-note">' .
__('This publish all short links on your bit.ly public page.') .
'</p>';
}
public function testService()
{
if (empty($this->args['login']) || empty($this->args['apiKey'])) {
$this->error->add(__('Service is not well configured.'));
2021-10-26 21:03:38 +00:00
return false;
}
$args = $this->args;
$args['hash'] = 'WP9vc';
if (!($response = self::post($this->url_api . 'expand', $args, true))) {
$this->error->add(__('Failed to call service.'));
2021-10-26 21:03:38 +00:00
return false;
}
$rsp = simplexml_load_string($response);
$err_msg = (string) $rsp->status_txt;
if ($err_msg != 'OK') {
$err_no = (integer) $rsp->status_code;
$this->error->add(sprintf(__('An error occured with code %s and message "%s"'), $err_no, $err_msg));
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)
{
2021-10-26 21:03:38 +00:00
$args = array_merge($this->args, ['longUrl' => $url]);
if (!($response = self::post($this->url_api . 'shorten', $args, true))) {
$this->error->add(__('Failed to call service.'));
2021-10-26 21:03:38 +00:00
return false;
}
$rsp = simplexml_load_string($response);
$err_msg = (string) $rsp->status_txt;
if ($err_msg != 'OK') {
$err_no = (integer) $rsp->status_code;
$this->error->add(sprintf(__('An error occured with code %s and message "%s"'), $err_no, $err_msg));
2021-10-26 21:03:38 +00:00
return false;
}
$rs = new ArrayObject();
$rs->hash = (string) $rsp->data[0]->hash;
2021-10-26 21:03:38 +00:00
$rs->url = (string) $rsp->data[0]->long_url;
$rs->type = $this->id;
return $rs;
}
}