partially restore bit.ly service

This commit is contained in:
Jean-Christian Paul Denis 2021-10-30 17:47:53 +02:00
parent ab707498b9
commit f893a8e1b4
Signed by: JcDenis
GPG Key ID: 1B5B8C5B90B6C951
3 changed files with 27 additions and 57 deletions

View File

@ -1,7 +1,9 @@
xxx.xx.xx
- Never fix breaking comments or complexe public url
- [ ] Fix bit.ly multi domain for short links
- [ ] Add bit.ly custom domain
- [ ] Add public page of the list of know urls and in/visible status
- [ ] Add passworded links
- Never fix breaking comments or complexe public url (add warning)
- fix permissions
- fix js load
- add user pref on table cols

View File

@ -54,10 +54,10 @@ if (!defined('SHORTEN_SERVICE_DISABLE_BILBOLINKS')) {
$__autoload['bilbolinksKutrlService'] = $d . 'services/class.bilbolinks.service.php';
$core->addBehavior('kutrlService', function() { return ["bilbolinks","bilbolinksKutrlService"]; } );
}
//if (!defined('SHORTEN_SERVICE_DISABLE_BITLY')) {
//$__autoload['bitlyKutrlService'] = $d . 'services/class.bitly.service.php';
//$core->addBehavior('kutrlService', function() { return ["bitly","bitlyKutrlService"]; } );
//}
if (!defined('SHORTEN_SERVICE_DISABLE_BITLY')) {
$__autoload['bitlyKutrlService'] = $d . 'services/class.bitly.service.php';
$core->addBehavior('kutrlService', function() { return ["bitly","bitlyKutrlService"]; } );
}
//if (!defined('SHORTEN_SERVICE_DISABLE_GOOGL')) {
// $__autoload['googlKutrlService'] = $d . 'services/class.googl.service.php';
// $core->addBehavior('kutrlService', function() { return ["googl","googlKutrlService"]; } );

View File

@ -20,112 +20,80 @@ class bitlyKutrlService extends kutrlService
protected $config = [
'id' => 'bitly',
'name' => 'bit.ly',
'home' => 'http://bit.ly',
'home' => 'https://bit.ly',
'url_api' => 'http://api.bit.ly/v3/',
'url_base' => 'http://bit.ly/',
'url_min_len' => 25
'url_api' => 'https://api-ssl.bitly.com/v4/',
'url_base' => 'https://bit.ly/',
'url_min_len' => 25,
'allow_protocols' => ['http://', 'https://'],
];
private $args = [
'format' => 'xml',
'login' => '',
'apiKey' => '',
'history' => 0
'apiKey' => ''
];
protected function init()
{
$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'])) {
if (empty($this->args['apiKey'])) {
$this->error->add(__('Service is not well configured.'));
return false;
}
$args = $this->args;
$args['hash'] = 'WP9vc';
if (!($response = self::post($this->url_api . 'expand', $args, true))) {
$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()))) {
$this->error->add(__('Failed to call service.'));
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));
return false;
}
return true;
}
public function createHash($url, $hash = null)
{
$args = array_merge($this->args, ['longUrl' => $url]);
$args = json_encode(['domain' => 'bit.ly', 'long_url' => $url]);
if (!($response = self::post($this->url_api . 'shorten', $args, true))) {
if (!($response = self::post($this->url_api . 'shorten', $args, true, false, $this->headers()))) {
$this->error->add(__('Failed to call service.'));
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));
return false;
}
$rsp = json_decode($response);
$rs = new ArrayObject();
$rs->hash = (string) $rsp->data[0]->hash;
$rs->url = (string) $rsp->data[0]->long_url;
$rs->hash = str_replace($this->url_base, '', (string) $rsp->link);
$rs->url = (string) $rsp->long_url;
$rs->type = $this->id;
return $rs;
}
private function headers()
{
return ['Authorization: Bearer ' . $this->args['apiKey'], 'Content-Type: application/json'];
}
}