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 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 public page of the list of know urls and in/visible status
- [ ] Add passworded links - [ ] Add passworded links
- Never fix breaking comments or complexe public url (add warning)
- fix permissions - fix permissions
- fix js load - fix js load
- add user pref on table cols - 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'; $__autoload['bilbolinksKutrlService'] = $d . 'services/class.bilbolinks.service.php';
$core->addBehavior('kutrlService', function() { return ["bilbolinks","bilbolinksKutrlService"]; } ); $core->addBehavior('kutrlService', function() { return ["bilbolinks","bilbolinksKutrlService"]; } );
} }
//if (!defined('SHORTEN_SERVICE_DISABLE_BITLY')) { if (!defined('SHORTEN_SERVICE_DISABLE_BITLY')) {
//$__autoload['bitlyKutrlService'] = $d . 'services/class.bitly.service.php'; $__autoload['bitlyKutrlService'] = $d . 'services/class.bitly.service.php';
//$core->addBehavior('kutrlService', function() { return ["bitly","bitlyKutrlService"]; } ); $core->addBehavior('kutrlService', function() { return ["bitly","bitlyKutrlService"]; } );
//} }
//if (!defined('SHORTEN_SERVICE_DISABLE_GOOGL')) { //if (!defined('SHORTEN_SERVICE_DISABLE_GOOGL')) {
// $__autoload['googlKutrlService'] = $d . 'services/class.googl.service.php'; // $__autoload['googlKutrlService'] = $d . 'services/class.googl.service.php';
// $core->addBehavior('kutrlService', function() { return ["googl","googlKutrlService"]; } ); // $core->addBehavior('kutrlService', function() { return ["googl","googlKutrlService"]; } );

View File

@ -20,112 +20,80 @@ class bitlyKutrlService extends kutrlService
protected $config = [ protected $config = [
'id' => 'bitly', 'id' => 'bitly',
'name' => 'bit.ly', 'name' => 'bit.ly',
'home' => 'http://bit.ly', 'home' => 'https://bit.ly',
'url_api' => 'http://api.bit.ly/v3/', 'url_api' => 'https://api-ssl.bitly.com/v4/',
'url_base' => 'http://bit.ly/', 'url_base' => 'https://bit.ly/',
'url_min_len' => 25 'url_min_len' => 25,
'allow_protocols' => ['http://', 'https://'],
]; ];
private $args = [ private $args = [
'format' => 'xml', 'apiKey' => ''
'login' => '',
'apiKey' => '',
'history' => 0
]; ];
protected function init() protected function init()
{ {
$this->args['login'] = $this->settings->kutrl_srv_bitly_login;
$this->args['apiKey'] = $this->settings->kutrl_srv_bitly_apikey; $this->args['apiKey'] = $this->settings->kutrl_srv_bitly_apikey;
$this->args['history'] = $this->settings->kutrl_srv_bitly_history ? 1 : 0;
} }
public function saveSettings() 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_apikey', $_POST['kutrl_srv_bitly_apikey']);
$this->settings->put('kutrl_srv_bitly_history', isset($_POST['kutrl_srv_bitly_history']));
} }
public function settingsForm() public function settingsForm()
{ {
echo 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 />' . '<p><label class="classic">' . __('API Key:') . '<br />' .
form::field(['kutrl_srv_bitly_apikey'], 50, 255, $this->settings->kutrl_srv_bitly_apikey) . form::field(['kutrl_srv_bitly_apikey'], 50, 255, $this->settings->kutrl_srv_bitly_apikey) .
'</label></p>' . '</label></p>' .
'<p class="form-note">' . '<p class="form-note">' .
sprintf(__('This is your personnal %s API key. You can find it on your account page.'), $this->config['name']) . 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>'; '</p>';
} }
public function testService() 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.')); $this->error->add(__('Service is not well configured.'));
return false; return false;
} }
$args = $this->args; $args = json_encode(['domain' => 'bit.ly', 'bitlink_id' => 'bit.ly/WP9vc'], JSON_UNESCAPED_SLASHES);
$args['hash'] = 'WP9vc'; if (!($response = self::post($this->url_api . 'expand', $args, true, false, $this->headers()))) {
if (!($response = self::post($this->url_api . 'expand', $args, true))) {
$this->error->add(__('Failed to call service.')); $this->error->add(__('Failed to call service.'));
return false; 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; return true;
} }
public function createHash($url, $hash = null) 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.')); $this->error->add(__('Failed to call service.'));
return false; return false;
} }
$rsp = simplexml_load_string($response); $rsp = json_decode($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;
}
$rs = new ArrayObject(); $rs = new ArrayObject();
$rs->hash = (string) $rsp->data[0]->hash; $rs->hash = str_replace($this->url_base, '', (string) $rsp->link);
$rs->url = (string) $rsp->data[0]->long_url; $rs->url = (string) $rsp->long_url;
$rs->type = $this->id; $rs->type = $this->id;
return $rs; return $rs;
} }
private function headers()
{
return ['Authorization: Bearer ' . $this->args['apiKey'], 'Content-Type: application/json'];
}
} }