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-08-27 23:52:03 +00:00
|
|
|
if (!defined('DC_RC_PATH')) {
|
|
|
|
return null;
|
|
|
|
}
|
2021-08-25 23:06:22 +00:00
|
|
|
|
|
|
|
class bitlyKutrlService extends kutrlService
|
|
|
|
{
|
2021-08-27 23:52:03 +00:00
|
|
|
protected $config = [
|
2022-12-22 14:37:09 +00:00
|
|
|
'id' => 'bitly',
|
|
|
|
'name' => 'bit.ly',
|
|
|
|
'home' => 'https://bit.ly',
|
2021-09-02 12:32:26 +00:00
|
|
|
|
2022-12-22 14:37:09 +00:00
|
|
|
'url_api' => 'https://api-ssl.bitly.com/v4/',
|
|
|
|
'url_base' => 'https://bit.ly/',
|
|
|
|
'url_min_len' => 25,
|
2021-10-30 15:47:53 +00:00
|
|
|
|
2021-11-06 15:43:02 +00:00
|
|
|
'allow_protocols' => ['http://', 'https://'],
|
2021-08-27 23:52:03 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
private $args = [
|
2022-11-20 16:15:36 +00:00
|
|
|
'apiKey' => '',
|
2021-08-27 23:52:03 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
protected function init()
|
|
|
|
{
|
2021-11-06 15:43:02 +00:00
|
|
|
$this->args['apiKey'] = $this->settings->kutrl_srv_bitly_apikey;
|
2021-08-27 23:52:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function saveSettings()
|
|
|
|
{
|
|
|
|
$this->settings->put('kutrl_srv_bitly_apikey', $_POST['kutrl_srv_bitly_apikey']);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function settingsForm()
|
|
|
|
{
|
2021-11-06 15:43:02 +00:00
|
|
|
echo
|
|
|
|
'<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']) .
|
2021-08-27 23:52:03 +00:00
|
|
|
'</p>';
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testService()
|
|
|
|
{
|
2021-10-30 15:47:53 +00:00
|
|
|
if (empty($this->args['apiKey'])) {
|
2021-08-27 23:52:03 +00:00
|
|
|
$this->error->add(__('Service is not well configured.'));
|
2021-10-26 21:03:38 +00:00
|
|
|
|
2021-08-27 23:52:03 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-10-30 15:47:53 +00:00
|
|
|
$args = json_encode(['domain' => 'bit.ly', 'bitlink_id' => 'bit.ly/WP9vc'], JSON_UNESCAPED_SLASHES);
|
|
|
|
if (!($response = self::post($this->url_api . 'expand', $args, true, false, $this->headers()))) {
|
2021-08-27 23:52:03 +00:00
|
|
|
$this->error->add(__('Failed to call service.'));
|
2021-10-26 21:03:38 +00:00
|
|
|
|
2021-08-27 23:52:03 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function createHash($url, $hash = null)
|
|
|
|
{
|
2021-10-30 15:47:53 +00:00
|
|
|
$args = json_encode(['domain' => 'bit.ly', 'long_url' => $url]);
|
2021-08-27 23:52:03 +00:00
|
|
|
|
2021-10-30 15:47:53 +00:00
|
|
|
if (!($response = self::post($this->url_api . 'shorten', $args, true, false, $this->headers()))) {
|
2021-08-27 23:52:03 +00:00
|
|
|
$this->error->add(__('Failed to call service.'));
|
2021-10-26 21:03:38 +00:00
|
|
|
|
2021-08-27 23:52:03 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-10-30 15:47:53 +00:00
|
|
|
$rsp = json_decode($response);
|
2021-08-27 23:52:03 +00:00
|
|
|
|
2021-11-06 15:43:02 +00:00
|
|
|
$rs = new ArrayObject();
|
2021-10-30 15:47:53 +00:00
|
|
|
$rs->hash = str_replace($this->url_base, '', (string) $rsp->link);
|
|
|
|
$rs->url = (string) $rsp->long_url;
|
2021-08-27 23:52:03 +00:00
|
|
|
$rs->type = $this->id;
|
|
|
|
|
|
|
|
return $rs;
|
|
|
|
}
|
2021-10-30 15:47:53 +00:00
|
|
|
|
|
|
|
private function headers()
|
|
|
|
{
|
|
|
|
return ['Authorization: Bearer ' . $this->args['apiKey'], 'Content-Type: application/json'];
|
|
|
|
}
|
2021-11-06 15:43:02 +00:00
|
|
|
}
|