From f893a8e1b48032af5bb13eeef72b624fe183d7f3 Mon Sep 17 00:00:00 2001 From: Jean-Christian Denis Date: Sat, 30 Oct 2021 17:47:53 +0200 Subject: [PATCH] partially restore bit.ly service --- CHANGELOG.md | 4 +- _prepend.php | 8 ++-- inc/services/class.bitly.service.php | 72 ++++++++-------------------- 3 files changed, 27 insertions(+), 57 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7b061d1..5fb2fc2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/_prepend.php b/_prepend.php index d28124e..9964869 100644 --- a/_prepend.php +++ b/_prepend.php @@ -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"]; } ); diff --git a/inc/services/class.bitly.service.php b/inc/services/class.bitly.service.php index 8083664..392228b 100644 --- a/inc/services/class.bitly.service.php +++ b/inc/services/class.bitly.service.php @@ -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 - '

' . - '

' . - sprintf(__('This is your login to sign up to %s'), $this->config['name']) . - '

' . '

' . '

' . sprintf(__('This is your personnal %s API key. You can find it on your account page.'), $this->config['name']) . - '

' . - '

' . - '

' . - __('This publish all short links on your bit.ly public page.') . '

'; } 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']; + } } \ No newline at end of file