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 googlKutrlService extends kutrlService
|
|
|
|
{
|
2021-11-06 15:43:02 +00:00
|
|
|
public $id = 'googl';
|
2021-08-27 23:52:03 +00:00
|
|
|
public $name = 'goo.gl';
|
|
|
|
public $home = 'http://goo.gl';
|
2021-08-25 23:06:22 +00:00
|
|
|
|
2021-11-06 15:43:02 +00:00
|
|
|
private $url_api = 'https://www.googleapis.com/urlshortener/v1/url';
|
2022-12-22 20:18:49 +00:00
|
|
|
private $url_test = 'https://github.com/JcDenis/kUtRL/releases';
|
2021-11-06 15:43:02 +00:00
|
|
|
private $args = [
|
2022-11-20 16:15:36 +00:00
|
|
|
'key' => '',
|
2021-08-27 23:52:03 +00:00
|
|
|
];
|
2021-11-06 15:43:02 +00:00
|
|
|
private $headers = ['Content-Type: application/json'];
|
2021-09-02 12:32:26 +00:00
|
|
|
|
2021-08-27 23:52:03 +00:00
|
|
|
protected function init()
|
|
|
|
{
|
2021-11-06 15:43:02 +00:00
|
|
|
$this->url_base = 'http://goo.gl/';
|
2021-08-27 23:52:03 +00:00
|
|
|
$this->url_min_length = 20;
|
|
|
|
}
|
2021-08-25 23:06:22 +00:00
|
|
|
|
2021-08-27 23:52:03 +00:00
|
|
|
public function testService()
|
|
|
|
{
|
2021-11-06 15:43:02 +00:00
|
|
|
$args = $this->args;
|
2021-08-27 23:52:03 +00:00
|
|
|
$args['shortUrl'] = $this->url_base . 'PLovn';
|
|
|
|
if (!($response = self::post($this->url_api, $args, true, true, $this->headers))) {
|
|
|
|
$this->error->add(__('Failed to call service.'));
|
2021-11-06 15:43:02 +00:00
|
|
|
|
2021-08-27 23:52:03 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-08-25 23:06:22 +00:00
|
|
|
|
2021-08-27 23:52:03 +00:00
|
|
|
$rsp = json_decode($response);
|
2021-08-25 23:06:22 +00:00
|
|
|
|
2021-08-27 23:52:03 +00:00
|
|
|
if (empty($rsp->status)) {
|
|
|
|
$this->error->add(__('An error occured'));
|
2021-11-06 15:43:02 +00:00
|
|
|
|
2021-08-27 23:52:03 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-11-06 15:43:02 +00:00
|
|
|
|
2021-08-27 23:52:03 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function createHash($url, $hash = null)
|
|
|
|
{
|
2021-11-06 15:43:02 +00:00
|
|
|
$args = $this->args;
|
2021-08-27 23:52:03 +00:00
|
|
|
$args['longUrl'] = $url;
|
2021-11-06 15:43:02 +00:00
|
|
|
$args = json_encode($args);
|
2021-08-27 23:52:03 +00:00
|
|
|
|
|
|
|
if (!($response = self::post($this->url_api, $args, true, false, $this->headers))) {
|
|
|
|
$this->error->add(__('Failed to call service.'));
|
2021-11-06 15:43:02 +00:00
|
|
|
|
2021-08-27 23:52:03 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$rsp = json_decode($response);
|
|
|
|
|
|
|
|
if (empty($rsp->id)) {
|
|
|
|
$this->error->add(__('An error occured'));
|
2021-11-06 15:43:02 +00:00
|
|
|
|
2021-08-27 23:52:03 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-11-06 15:43:02 +00:00
|
|
|
$rs = new ArrayObject();
|
2021-08-27 23:52:03 +00:00
|
|
|
$rs->hash = str_replace($this->url_base, '', $rsp->id);
|
2021-11-06 15:43:02 +00:00
|
|
|
$rs->url = $rsp->longUrl;
|
2021-08-27 23:52:03 +00:00
|
|
|
$rs->type = $this->id;
|
|
|
|
|
|
|
|
return $rs;
|
|
|
|
}
|
2021-11-06 15:43:02 +00:00
|
|
|
}
|