cosmetics

This commit is contained in:
Jean-Christian Paul Denis 2021-10-26 23:03:38 +02:00
parent 9e9bb94f1d
commit 79a77474f0
Signed by: JcDenis
GPG Key ID: 1B5B8C5B90B6C951
17 changed files with 187 additions and 120 deletions

View File

@ -1,4 +1,15 @@
<?php <?php
/**
* @brief kUtRL, a plugin for Dotclear 2
*
* @package Dotclear
* @subpackage Plugin
*
* @author Jean-Christian Denis and contributors
*
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
if (!defined('DC_CONTEXT_ADMIN')) { if (!defined('DC_CONTEXT_ADMIN')) {
return; return;

View File

@ -21,11 +21,11 @@ $this->registerModule(
'Jean-Christian Denis and contributors', 'Jean-Christian Denis and contributors',
'2021.09.16', '2021.09.16',
[ [
'requires' => [['core', '2.19']], 'requires' => [['core', '2.19']],
'permissions' => 'usage,contentadmin,admin', 'permissions' => 'usage,contentadmin,admin',
'type' => 'plugin', 'type' => 'plugin',
'support' => 'https://github.com/JcDenis/kUtRL', 'support' => 'https://github.com/JcDenis/kUtRL',
'details' => 'http://plugins.dotaddict.org/dc2/details/kUtRL', 'details' => 'http://plugins.dotaddict.org/dc2/details/kUtRL',
'repository' => 'https://raw.githubusercontent.com/JcDenis/kUtRL/master/dcstore.xml' 'repository' => 'https://raw.githubusercontent.com/JcDenis/kUtRL/master/dcstore.xml'
] ]
); );

View File

@ -21,15 +21,11 @@ $old_version = $core->getVersion('kUtRL');
# Compare versions # Compare versions
if (version_compare($old_version, $new_version, '>=')) { if (version_compare($old_version, $new_version, '>=')) {
return; return null;
} }
# Install or update # Install or update
try { try {
if (version_compare(str_replace("-r", "-p", DC_VERSION), '2.2-alpha', '<')) {
throw new Exception('kUtRL requires Dotclear 2.2');
}
# Table # Table
$t = new dbStruct($core->con, $core->prefix); $t = new dbStruct($core->con, $core->prefix);
$t->kutrl $t->kutrl
@ -89,5 +85,6 @@ try {
return true; return true;
} catch (Exception $e) { } catch (Exception $e) {
$core->error->add($e->getMessage()); $core->error->add($e->getMessage());
return false; }
}
return false;

View File

@ -14,11 +14,6 @@
if (!defined('DC_RC_PATH')) { if (!defined('DC_RC_PATH')) {
return; return;
} }
if (version_compare(str_replace("-r", "-p", DC_VERSION), '2.2-alpha', '<')) {
return;
}
global $__autoload, $core;
# Set a URL shortener for quick get request # Set a URL shortener for quick get request
if (!defined('SHORTEN_SERVICE_NAME')) { if (!defined('SHORTEN_SERVICE_NAME')) {
@ -39,9 +34,9 @@ if (!defined('SHORTEN_SERVICE_ENCODE')) {
# Main class # Main class
$d = dirname(__FILE__) . '/inc/'; $d = dirname(__FILE__) . '/inc/';
$__autoload['kutrl'] = $d . 'class.kutrl.php'; $__autoload['kutrl'] = $d . 'class.kutrl.php';
$__autoload['kutrlService'] = $d . 'lib.kutrl.srv.php'; $__autoload['kutrlService'] = $d . 'lib.kutrl.srv.php';
$__autoload['kutrlLog'] = $d . 'lib.kutrl.log.php'; $__autoload['kutrlLog'] = $d . 'lib.kutrl.log.php';
$__autoload['kutrlLinksList'] = $d . 'lib.kutrl.lst.php'; $__autoload['kutrlLinksList'] = $d . 'lib.kutrl.lst.php';
# Services # Services

View File

@ -57,11 +57,13 @@ class urlKutrl extends dcUrlHandlers
# Not active, go to default 404 # Not active, go to default 404
if (!$s->kutrl_active) { if (!$s->kutrl_active) {
self::p404(); self::p404();
return null; return null;
} }
# Not a valid url, go to kutrl 404 # Not a valid url, go to kutrl 404
if (!preg_match('#^(|(/(.*?)))$#', $args, $m)) { if (!preg_match('#^(|(/(.*?)))$#', $args, $m)) {
self::kutrl404(); self::kutrl404();
return null; return null;
} }
@ -85,6 +87,7 @@ class urlKutrl extends dcUrlHandlers
# No arg, go to kurtl page # No arg, go to kurtl page
if ($args == '') { if ($args == '') {
self::pageKutrl($kut); self::pageKutrl($kut);
return null; return null;
} }
# Not find, go to kutrl 404 # Not find, go to kutrl 404
@ -92,16 +95,19 @@ class urlKutrl extends dcUrlHandlers
//$_ctx->kutrl_msg = 'Failed to find short link.'; //$_ctx->kutrl_msg = 'Failed to find short link.';
//self::pageKutrl($kut); //self::pageKutrl($kut);
self::kutrl404(); self::kutrl404();
return null; return null;
} }
# Removed (empty url), go to kutrl 404 # Removed (empty url), go to kutrl 404
if (!$url) { if (!$url) {
self::kutrl404(); self::kutrl404();
return null; return null;
} }
$core->blog->triggerBlog(); $core->blog->triggerBlog();
http::redirect($url . $suffix); http::redirect($url . $suffix);
return null; return null;
} }
@ -113,11 +119,13 @@ class urlKutrl extends dcUrlHandlers
# Not active, go to default 404 # Not active, go to default 404
if (!$s->kutrl_active) { if (!$s->kutrl_active) {
self::p404(); self::p404();
return null; return null;
} }
# Public page not active, go to kutrl 404 # Public page not active, go to kutrl 404
if (!$s->kutrl_srv_local_public) { if (!$s->kutrl_srv_local_public) {
self::kutrl404(); self::kutrl404();
return null; return null;
} }
# Validation form # Validation form
@ -209,6 +217,7 @@ class urlKutrl extends dcUrlHandlers
$core->tpl->setPath($core->tpl->getPath(), dirname(__FILE__) . '/default-templates'); $core->tpl->setPath($core->tpl->getPath(), dirname(__FILE__) . '/default-templates');
self::serveDocument('kutrl.html'); self::serveDocument('kutrl.html');
return null; return null;
} }
@ -218,6 +227,7 @@ class urlKutrl extends dcUrlHandlers
if (!$core->blog->settings->kUtRL->kutrl_srv_local_404_active) { if (!$core->blog->settings->kUtRL->kutrl_srv_local_404_active) {
self::p404(); self::p404();
return null; return null;
} }
@ -282,7 +292,8 @@ class pubKutrl
if (empty($GLOBALS['disable_kutrl'])) { if (empty($GLOBALS['disable_kutrl'])) {
# kUtRL is not activated # kUtRL is not activated
if (!$core->blog->settings->kUtRL->kutrl_active if (!$core->blog->settings->kUtRL->kutrl_active
|| !$core->blog->settings->kUtRL->kutrl_tpl_active) { || !$core->blog->settings->kUtRL->kutrl_tpl_active
) {
return null; return null;
} }

View File

@ -19,11 +19,12 @@ if (!defined('DC_RC_PATH')) {
class kUtRL class kUtRL
{ {
# Load services list from behavior # Load services list from behavior
public static function getServices($core) public static function getServices(dcCore $core)
{ {
$list = $core->getBehaviors('kutrlService'); $list = $core->getBehaviors('kutrlService');
if (empty($list)) { if (empty($list)) {
return []; return [];
} }
$services = []; $services = [];
@ -35,6 +36,7 @@ class kUtRL
} }
} }
return $services; return $services;
} }
@ -46,15 +48,17 @@ class kUtRL
try { try {
if (empty($id)) { if (empty($id)) {
return null; return null;
} }
$services = self::getServices($core); $services = self::getServices($core);
if (isset($services[$id])) { if (isset($services[$id])) {
return new $services[$id]($core); return new $services[$id]($core);
} }
} catch(Exception $e) { } catch(Exception $e) {
} }
return null; return null;
} }
@ -66,15 +70,17 @@ class kUtRL
try { try {
if (!in_array($place, ['tpl', 'wiki', 'admin', 'plugin'])) { if (!in_array($place, ['tpl', 'wiki', 'admin', 'plugin'])) {
return null; return null;
} }
$id = $core->blog->settings->kUtRL->get('kutrl_' . $place .'_service'); $id = $core->blog->settings->kUtRL->get('kutrl_' . $place .'_service');
if (!empty($id)) { if (!empty($id)) {
return self::quickService($id); return self::quickService($id);
} }
} catch(Exception $e) { } catch(Exception $e) {
} }
return null; return null;
} }
@ -87,16 +93,18 @@ class kUtRL
try { try {
$srv = self::quickPlace($place); $srv = self::quickPlace($place);
if (empty($srv)) { if (empty($srv)) {
return $url; return $url;
} }
$rs = $srv->hash($url,$custom); $rs = $srv->hash($url,$custom);
if (empty($rs)) { if (empty($rs)) {
return $url; return $url;
} }
return $srv->url_base.$rs->hash; return $srv->url_base.$rs->hash;
} catch(Exception $e) { } catch(Exception $e) {
} }
return $url; return $url;
} }
} }

View File

@ -20,12 +20,12 @@ class kutrlLog
public $blog; public $blog;
public $con; public $con;
public function __construct($core) public function __construct(dcCore $core)
{ {
$this->core = $core; $this->core = $core;
$this->table = $core->prefix . 'kutrl'; $this->table = $core->prefix . 'kutrl';
$this->blog = $core->con->escape($core->blog->id); $this->blog = $core->con->escape($core->blog->id);
$this->con = $core->con; $this->con = $core->con;
} }
public function nextId() public function nextId()
@ -41,23 +41,23 @@ class kutrlLog
$this->con->writeLock($this->table); $this->con->writeLock($this->table);
try { try {
$cur->kut_id = $this->nextId(); $cur->kut_id = $this->nextId();
$cur->blog_id = $this->blog; $cur->blog_id = $this->blog;
$cur->kut_url = (string) $url; $cur->kut_url = (string) $url;
$cur->kut_hash = (string) $hash; $cur->kut_hash = (string) $hash;
$cur->kut_type = (string) $type; $cur->kut_type = (string) $type;
$cur->kut_service = (string) $service; $cur->kut_service = (string) $service;
$cur->kut_dt = date('Y-m-d H:i:s'); $cur->kut_dt = date('Y-m-d H:i:s');
$cur->kut_counter = 0; $cur->kut_counter = 0;
$cur->insert(); $cur->insert();
$this->con->unlock(); $this->con->unlock();
return [ return [
'id' => $cur->kut_id, 'id' => $cur->kut_id,
'url' => $url, 'url' => $url,
'hash' => $hash, 'hash' => $hash,
'type' => $type, 'type' => $type,
'service' => $service, 'service' => $service,
'counter '=> 0 'counter '=> 0
]; ];
@ -65,6 +65,7 @@ class kutrlLog
$this->con->unlock(); $this->con->unlock();
throw $e; throw $e;
} }
return false; return false;
} }
@ -109,8 +110,8 @@ class kutrlLog
$this->con->writeLock($this->table); $this->con->writeLock($this->table);
try { try {
$cur->kut_url = ''; $cur->kut_url = '';
$cur->kut_dt = date('Y-m-d H:i:s'); $cur->kut_dt = date('Y-m-d H:i:s');
$cur->kut_counter = 0; $cur->kut_counter = 0;
$cur->update( $cur->update(
@ -124,6 +125,7 @@ class kutrlLog
$this->con->unlock(); $this->con->unlock();
throw $e; throw $e;
} }
return false; return false;
} }
@ -152,12 +154,14 @@ class kutrlLog
$counter = $rs->isEmpty() ? 0 : $rs->kut_counter; $counter = $rs->isEmpty() ? 0 : $rs->kut_counter;
if ('get' == $do) { if ('get' == $do) {
return $counter; return $counter;
} elseif ('up' == $do) { } elseif ('up' == $do) {
$counter += 1; $counter += 1;
} elseif ('reset' == $do) { } elseif ('reset' == $do) {
$counter = 0; $counter = 0;
} else { } else {
return 0; return 0;
} }
@ -253,6 +257,7 @@ class kutrlLog
if (!$count_only && !empty($p['limit'])) { if (!$count_only && !empty($p['limit'])) {
$r .= $this->con->limit($p['limit']); $r .= $this->con->limit($p['limit']);
} }
return $this->con->select($r); return $this->con->select($r);
} }
} }

View File

@ -60,8 +60,8 @@ class kutrlLinkslist
} }
$cols = [ $cols = [
'kut_url' => '<th colspan="2" class="first">' . __('Link') . '</th>', 'kut_url' => '<th colspan="2" class="first">' . __('Link') . '</th>',
'kut_hash' => '<th scope="col">' . __('Hash') . '</th>', 'kut_hash' => '<th scope="col">' . __('Hash') . '</th>',
'kut_dt' => '<th scope="col">' . __('Date') . '</th>', 'kut_dt' => '<th scope="col">' . __('Date') . '</th>',
'kut_service' => '<th scope="col">' . __('Service') . '</th>' 'kut_service' => '<th scope="col">' . __('Service') . '</th>'
]; ];

View File

@ -24,10 +24,10 @@ class kutrlService
public function __construct($core) public function __construct($core)
{ {
$this->core = $core; $this->core = $core;
$this->settings = $core->blog->settings->kUtRL; $this->settings = $core->blog->settings->kUtRL;
$this->log = new kutrlLog($core); $this->log = new kutrlLog($core);
$this->error = new dcError(); $this->error = new dcError();
$this->error->setHTMLFormat('%s', "%s\n"); $this->error->setHTMLFormat('%s', "%s\n");
$this->init(); $this->init();
@ -39,18 +39,18 @@ class kutrlService
$this->config = array_merge( $this->config = array_merge(
[ [
'id' => 'undefined', 'id' => 'undefined',
'name' => 'undefined', 'name' => 'undefined',
'home' => '', 'home' => '',
'allow_external_url' => true, 'allow_external_url' => true,
'allow_custom_hash' => false, 'allow_custom_hash' => false,
'allow_protocols' => ['http://'], 'allow_protocols' => ['http://'],
'url_test' => 'http://dotclear.jcdenis.com/go/kUtRL', 'url_test' => 'http://dotclear.jcdenis.com/go/kUtRL',
'url_api' => '', 'url_api' => '',
'url_base' => '', 'url_base' => '',
'url_min_len' => 0 'url_min_len' => 0
], ],
$this->config $this->config
); );
@ -121,9 +121,11 @@ class kutrlService
continue; continue;
} }
if (strpos($url,$protocol) === 0) { if (strpos($url,$protocol) === 0) {
return true; return true;
} }
} }
return false; return false;
} }
@ -199,6 +201,7 @@ class kutrlService
} }
$this->deleteUrl($url); $this->deleteUrl($url);
$this->log->delete($rs->id); $this->log->delete($rs->id);
return true; return true;
} }
@ -238,6 +241,7 @@ class kutrlService
if ($verbose) { if ($verbose) {
return $client->getContent(); return $client->getContent();
} }
return true; return true;
} }
} }

View File

@ -26,8 +26,9 @@ class kutrlWiki
# Do nothing on comment preview and post preview # Do nothing on comment preview and post preview
if (!empty($_POST['preview']) if (!empty($_POST['preview'])
|| !empty($GLOBALS['_ctx']) && $GLOBALS['_ctx']->preview || !empty($GLOBALS['_ctx']) && $GLOBALS['_ctx']->preview
|| !$s->kutrl_active) { || !$s->kutrl_active
) {
return null; return null;
} }
if (null === ($kut = kutrl::quickPlace('wiki'))) { if (null === ($kut = kutrl::quickPlace('wiki'))) {

View File

@ -18,7 +18,7 @@ if (!defined('DC_RC_PATH')) {
class bilbolinksKutrlService extends kutrlService class bilbolinksKutrlService extends kutrlService
{ {
protected $config = [ protected $config = [
'id' => 'bilbolinks', 'id' => 'bilbolinks',
'name' => 'BilboLinks', 'name' => 'BilboLinks',
'home' => 'http://www.tux-planet.fr/bilbobox/' 'home' => 'http://www.tux-planet.fr/bilbobox/'
]; ];
@ -62,14 +62,17 @@ class bilbolinksKutrlService extends kutrlService
{ {
if (empty($this->url_base)) { if (empty($this->url_base)) {
$this->error->add(__('Service is not well configured.')); $this->error->add(__('Service is not well configured.'));
return false; return false;
} }
$arg = ['longurl' => urlencode($this->url_test)]; $arg = ['longurl' => urlencode($this->url_test)];
if (!self::post($this->url_api, $arg, true,true)) { if (!self::post($this->url_api, $arg, true,true)) {
$this->error->add(__('Service is unavailable.')); $this->error->add(__('Service is unavailable.'));
return false; return false;
} }
return true; return true;
} }
@ -79,15 +82,17 @@ class bilbolinksKutrlService extends kutrlService
if (!($response = self::post($this->url_api, $arg, true, true))) { if (!($response = self::post($this->url_api, $arg, true, true))) {
$this->error->add(__('Service is unavailable.')); $this->error->add(__('Service is unavailable.'));
return false; return false;
} }
if ($response == 'You are too speed!') { if ($response == 'You are too speed!') {
$this->error->add(__('Service rate limit exceeded.')); $this->error->add(__('Service rate limit exceeded.'));
return false; return false;
} }
$rs = new ArrayObject(); $rs = new ArrayObject();
$rs->hash = str_replace($this->url_base, '', $response); $rs->hash = str_replace($this->url_base, '', $response);
$rs->url = $url; $rs->url = $url;
$rs->type = $this->id; $rs->type = $this->id;
return $rs; return $rs;

View File

@ -18,26 +18,26 @@ if (!defined('DC_RC_PATH')) {
class bitlyKutrlService extends kutrlService class bitlyKutrlService extends kutrlService
{ {
protected $config = [ protected $config = [
'id' => 'bitly', 'id' => 'bitly',
'name' => 'bit.ly', 'name' => 'bit.ly',
'home' => 'http://bit.ly', 'home' => 'http://bit.ly',
'url_api' => 'http://api.bit.ly/v3/', 'url_api' => 'http://api.bit.ly/v3/',
'url_base' => 'http://bit.ly/', 'url_base' => 'http://bit.ly/',
'url_min_len' => 25 'url_min_len' => 25
]; ];
private $args = [ private $args = [
'format' => 'xml', 'format' => 'xml',
'login' => '', 'login' => '',
'apiKey' => '', 'apiKey' => '',
'history' => 0 'history' => 0
]; ];
protected function init() protected function init()
{ {
$this->args['login'] = $this->settings->kutrl_srv_bitly_login; $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; $this->args['history'] = $this->settings->kutrl_srv_bitly_history ? 1 : 0;
} }
@ -76,6 +76,7 @@ class bitlyKutrlService extends kutrlService
{ {
if (empty($this->args['login']) || empty($this->args['apiKey'])) { if (empty($this->args['login']) || empty($this->args['apiKey'])) {
$this->error->add(__('Service is not well configured.')); $this->error->add(__('Service is not well configured.'));
return false; return false;
} }
@ -83,6 +84,7 @@ class bitlyKutrlService extends kutrlService
$args['hash'] = 'WP9vc'; $args['hash'] = 'WP9vc';
if (!($response = self::post($this->url_api . 'expand', $args, true))) { 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;
} }
@ -92,18 +94,20 @@ class bitlyKutrlService extends kutrlService
if ($err_msg != 'OK') { if ($err_msg != 'OK') {
$err_no = (integer) $rsp->status_code; $err_no = (integer) $rsp->status_code;
$this->error->add(sprintf(__('An error occured with code %s and message "%s"'), $err_no, $err_msg)); $this->error->add(sprintf(__('An error occured with code %s and message "%s"'), $err_no, $err_msg));
return false; return false;
} }
return true; return true;
} }
public function createHash($url, $hash = null) public function createHash($url, $hash = null)
{ {
$args = $this->args; $args = array_merge($this->args, ['longUrl' => $url]);
$args['longUrl'] = $url;
if (!($response = self::post($this->url_api . 'shorten', $args, true))) { if (!($response = self::post($this->url_api . 'shorten', $args, true))) {
$this->error->add(__('Failed to call service.')); $this->error->add(__('Failed to call service.'));
return false; return false;
} }
@ -113,12 +117,13 @@ class bitlyKutrlService extends kutrlService
if ($err_msg != 'OK') { if ($err_msg != 'OK') {
$err_no = (integer) $rsp->status_code; $err_no = (integer) $rsp->status_code;
$this->error->add(sprintf(__('An error occured with code %s and message "%s"'), $err_no, $err_msg)); $this->error->add(sprintf(__('An error occured with code %s and message "%s"'), $err_no, $err_msg));
return false; return false;
} }
$rs = new ArrayObject(); $rs = new ArrayObject();
$rs->hash = (string) $rsp->data[0]->hash; $rs->hash = (string) $rsp->data[0]->hash;
$rs->url = (string) $rsp->data[0]->long_url; $rs->url = (string) $rsp->data[0]->long_url;
$rs->type = $this->id; $rs->type = $this->id;
return $rs; return $rs;

View File

@ -18,22 +18,21 @@ if (!defined('DC_RC_PATH')) {
class customKutrlService extends kutrlService class customKutrlService extends kutrlService
{ {
protected $config = [ protected $config = [
'id' => 'custom', 'id' => 'custom',
'name' => 'Custom' 'name' => 'Custom'
]; ];
protected function init() protected function init()
{ {
$config = unserialize(base64_decode($this->settings->kutrl_srv_custom)); $config = unserialize(base64_decode($this->settings->kutrl_srv_custom));
if (!is_array($config)) if (!is_array($config)) {
{
$config = []; $config = [];
} }
$this->config['url_api'] = !empty($config['url_api']) ? $config['url_api'] : ''; $this->config['url_api'] = !empty($config['url_api']) ? $config['url_api'] : '';
$this->config['url_base'] = !empty($config['url_base']) ? $config['url_base'] : ''; $this->config['url_base'] = !empty($config['url_base']) ? $config['url_base'] : '';
$this->config['url_param'] = !empty($config['url_param']) ? $config['url_param'] : ''; $this->config['url_param'] = !empty($config['url_param']) ? $config['url_param'] : '';
$this->config['url_encode'] = !empty($config['url_api']); $this->config['url_encode'] = !empty($config['url_api']);
$this->config['url_min_length'] = strlen($this->url_base) + 2; $this->config['url_min_length'] = strlen($this->url_base) + 2;
} }
@ -41,9 +40,9 @@ class customKutrlService extends kutrlService
public function saveSettings() public function saveSettings()
{ {
$config = [ $config = [
'url_api' => $_POST['kutrl_srv_custom_url_api'], 'url_api' => $_POST['kutrl_srv_custom_url_api'],
'url_base' => $_POST['kutrl_srv_custom_url_base'], 'url_base' => $_POST['kutrl_srv_custom_url_base'],
'url_param' => $_POST['kutrl_srv_custom_url_param'], 'url_param' => $_POST['kutrl_srv_custom_url_param'],
'url_encode' => !empty($_POST['kutrl_srv_custom_url_encode']) 'url_encode' => !empty($_POST['kutrl_srv_custom_url_encode'])
]; ];
$this->settings->put('kutrl_srv_custom', base64_encode(serialize($config))); $this->settings->put('kutrl_srv_custom', base64_encode(serialize($config)));
@ -52,9 +51,9 @@ class customKutrlService extends kutrlService
public function settingsForm() public function settingsForm()
{ {
$default = [ $default = [
'url_api' => '', 'url_api' => '',
'url_base' => '', 'url_base' => '',
'url_param' => '', 'url_param' => '',
'url_encode' => true 'url_encode' => true
]; ];
$config = unserialize(base64_decode($this->settings->kutrl_srv_custom)); $config = unserialize(base64_decode($this->settings->kutrl_srv_custom));
@ -89,21 +88,24 @@ class customKutrlService extends kutrlService
public function testService() public function testService()
{ {
if (empty($this->url_api)) { if (empty($this->url_api)) {
return false; return false;
} }
$url = $this->url_encode ? urlencode($this->url_test) : $this->url_test; $url = $this->url_encode ? urlencode($this->url_test) : $this->url_test;
$arg = [$this->url_param => $url]; $arg = [$this->url_param => $url];
if (!self::post($this->url_api, $arg, true, true)) { if (!self::post($this->url_api, $arg, true, true)) {
$this->error->add(__('Service is unavailable.')); $this->error->add(__('Service is unavailable.'));
return false; return false;
} }
return true; return true;
} }
public function createHash($url, $hash = null) public function createHash($url, $hash = null)
{ {
$enc = $this->url_encode ? urlencode($url) : $url; $enc = $this->url_encode ? urlencode($url) : $url;
$arg = array($this->url_param => $enc); $arg = [$this->url_param => $enc];
if (!($response = self::post($this->url_api, $arg, true, true))) { if (!($response = self::post($this->url_api, $arg, true, true))) {
$this->error->add(__('Service is unavailable.')); $this->error->add(__('Service is unavailable.'));
@ -111,7 +113,7 @@ class customKutrlService extends kutrlService
} }
$rs = new ArrayObject(); $rs = new ArrayObject();
$rs->hash = str_replace($this->url_base, '', $response); $rs->hash = str_replace($this->url_base, '', $response);
$rs->url = $url; $rs->url = $url;
$rs->type = $this->id; $rs->type = $this->id;
return $rs; return $rs;

View File

@ -25,16 +25,16 @@ class defaultKutrlService extends kutrlService
protected function init() protected function init()
{ {
$this->config = [ $this->config = [
'id' => 'default', 'id' => 'default',
'name' => 'Default', 'name' => 'Default',
'home' => '', 'home' => '',
'url_api' => SHORTEN_SERVICE_API, 'url_api' => SHORTEN_SERVICE_API,
'url_base' => SHORTEN_SERVICE_BASE, 'url_base' => SHORTEN_SERVICE_BASE,
'url_min_len' => strlen(SHORTEN_SERVICE_BASE) + 2, 'url_min_len' => strlen(SHORTEN_SERVICE_BASE) + 2,
'url_param' => SHORTEN_SERVICE_PARAM, 'url_param' => SHORTEN_SERVICE_PARAM,
'url_encode' => SHORTEN_SERVICE_ENCODE 'url_encode' => SHORTEN_SERVICE_ENCODE
]; ];
} }
@ -62,28 +62,31 @@ class defaultKutrlService extends kutrlService
public function testService() public function testService()
{ {
$url = $this->url_encode ? urlencode($this->url_test) : $this->url_test; $url = $this->url_encode ? urlencode($this->url_test) : $this->url_test;
$arg = array($this->url_param => urlencode($this->url_test)); $arg = [$this->url_param => urlencode($this->url_test)];
if (!self::post($this->url_api, $arg, true, true)) { if (!self::post($this->url_api, $arg, true, true)) {
$this->error->add(__('Service is unavailable.')); $this->error->add(__('Service is unavailable.'));
return false; return false;
} }
return true; return true;
} }
public function createHash($url, $hash = null) public function createHash($url, $hash = null)
{ {
$enc = $this->url_encode ? urlencode($url) : $url; $enc = $this->url_encode ? urlencode($url) : $url;
$arg = array($this->url_param => $url); $arg = [$this->url_param => $url];
if (!($response = self::post($this->url_api, $arg, true, true))) { if (!($response = self::post($this->url_api, $arg, true, true))) {
$this->error->add(__('Service is unavailable.')); $this->error->add(__('Service is unavailable.'));
return false; return false;
} }
$rs = new ArrayObject(); $rs = new ArrayObject();
$rs->hash = str_replace($this->url_base, '', $response); $rs->hash = str_replace($this->url_base, '', $response);
$rs->url = $url; $rs->url = $url;
$rs->type = $this->id; $rs->type = $this->id;
return $rs; return $rs;

View File

@ -16,12 +16,12 @@ if (!defined('DC_RC_PATH')){return;}
class isgdKutrlService extends kutrlService class isgdKutrlService extends kutrlService
{ {
protected $config = [ protected $config = [
'id' => 'isgd', 'id' => 'isgd',
'name' => 'is.gd', 'name' => 'is.gd',
'home' => 'http://is.gd/', 'home' => 'http://is.gd/',
'url_api' => 'http://is.gd/api.php', 'url_api' => 'http://is.gd/api.php',
'url_base' => 'http://is.gd/', 'url_base' => 'http://is.gd/',
'url_min_length' => 25 'url_min_length' => 25
]; ];
@ -30,8 +30,10 @@ class isgdKutrlService extends kutrlService
$arg = ['longurl' => urlencode($this->url_test)]; $arg = ['longurl' => urlencode($this->url_test)];
if (!self::post($this->url_api, $arg, true, true)) { if (!self::post($this->url_api, $arg, true, true)) {
$this->error->add(__('Service is unavailable.')); $this->error->add(__('Service is unavailable.'));
return false; return false;
} }
return true; return true;
} }
@ -41,12 +43,13 @@ class isgdKutrlService extends kutrlService
if (!($response = self::post($this->url_api, $arg, true, true))) { if (!($response = self::post($this->url_api, $arg, true, true))) {
$this->error->add(__('Service is unavailable.')); $this->error->add(__('Service is unavailable.'));
return false; return false;
} }
$rs = new ArrayObject(); $rs = new ArrayObject();
$rs->hash = str_replace($this->url_base, '', $response); $rs->hash = str_replace($this->url_base, '', $response);
$rs->url = $url; $rs->url = $url;
$rs->type = $this->id; $rs->type = $this->id;
return $rs; return $rs;

View File

@ -18,9 +18,9 @@ if (!defined('DC_RC_PATH')) {
class localKutrlService extends kutrlService class localKutrlService extends kutrlService
{ {
protected $config = [ protected $config = [
'id' => 'local', 'id' => 'local',
'name' => 'kUtRL', 'name' => 'kUtRL',
'home' => 'https://github.com/JcDenis/kUtRL', 'home' => 'https://github.com/JcDenis/kUtRL',
'allow_custom_hash' => true 'allow_custom_hash' => true
]; ];
@ -104,9 +104,11 @@ class localKutrlService extends kutrlService
{ {
$ap = $this->allow_protocols; $ap = $this->allow_protocols;
if (!empty($ap)) { if (!empty($ap)) {
return true; return true;
} else { } else {
$this->error->add(__('Service is not well configured.')); $this->error->add(__('Service is not well configured.'));
return false; return false;
} }
} }
@ -116,7 +118,7 @@ class localKutrlService extends kutrlService
# Create response object # Create response object
$rs = new ArrayObject(); $rs = new ArrayObject();
$rs->type = 'local'; $rs->type = 'local';
$rs->url = $url; $rs->url = $url;
# Normal link # Normal link
if ($hash === null) { if ($hash === null) {
@ -132,6 +134,7 @@ class localKutrlService extends kutrlService
} elseif (preg_match('/^[A-Za-z0-9\.\-\_]{2,}$/', $hash)) { } elseif (preg_match('/^[A-Za-z0-9\.\-\_]{2,}$/', $hash)) {
if (false !== $this->log->select(null, $hash, null, 'local')) { if (false !== $this->log->select(null, $hash, null, 'local')) {
$this->error->add(__('Custom short link is already taken.')); $this->error->add(__('Custom short link is already taken.'));
return false; return false;
} }
$type = 'localcustom'; $type = 'localcustom';
@ -140,23 +143,25 @@ class localKutrlService extends kutrlService
# Wrong char in custom hash # Wrong char in custom hash
} else { } else {
$this->error->add(__('Custom short link is not valid.')); $this->error->add(__('Custom short link is not valid.'));
return false; return false;
} }
# Save link # Save link
try { try {
$this->log->insert($rs->url, $rs->hash, $type, $rs->type); $this->log->insert($rs->url, $rs->hash, $type, $rs->type);
return $rs; return $rs;
} catch (Exception $e) { } catch (Exception $e) {
$this->error->add(__('Failed to save link.')); $this->error->add(__('Failed to save link.'));
} }
return false; return false;
} }
protected function last($type) protected function last($type)
{ {
return return false === ($rs = $this->log->select(null, null, $type, 'local')) ?
false === ($rs = $this->log->select(null, null, $type, 'local')) ?
-1 : $rs->hash; -1 : $rs->hash;
} }
@ -177,9 +182,9 @@ class localKutrlService extends kutrlService
$next_id = $this->append($last_id); $next_id = $this->append($last_id);
} }
} }
return
false === $this->log->select(null,$prefix . $next_id, null, 'local') ? return false === $this->log->select(null,$prefix . $next_id, null, 'local') ?
$next_id : $this->next($next_id, $prefix); $next_id : $this->next($next_id, $prefix);
} }
protected function append($id) protected function append($id)
@ -188,6 +193,7 @@ class localKutrlService extends kutrlService
for ($x = 0; $x < count($id); $x++) { for ($x = 0; $x < count($id); $x++) {
$id[$x] = 0; $id[$x] = 0;
} }
return implode($id) . '0'; return implode($id) . '0';
} }
@ -208,24 +214,29 @@ class localKutrlService extends kutrlService
$id[$x] = 0; $id[$x] = 0;
} }
} }
return implode($id); return implode($id);
} }
public function getUrl($hash) public function getUrl($hash)
{ {
if (false === ($rs = $this->log->select(null, $hash, null, 'local'))) { if (false === ($rs = $this->log->select(null, $hash, null, 'local'))) {
return false; return false;
} }
if (!$rs->url) { //previously removed url if (!$rs->url) { //previously removed url
return false; return false;
} }
$this->log->counter($rs->id, 'up'); $this->log->counter($rs->id, 'up');
return $rs->url; return $rs->url;
} }
public function deleteUrl($url, $delete = false) public function deleteUrl($url, $delete = false)
{ {
if (false === ($rs = $this->log->select($url, null, null, 'local'))) { if (false === ($rs = $this->log->select($url, null, null, 'local'))) {
return false; return false;
} }
if ($delete) { if ($delete) {
@ -233,6 +244,7 @@ class localKutrlService extends kutrlService
} else { } else {
$this->log->clear($rs->id, ''); $this->log->clear($rs->id, '');
} }
return true; return true;
} }
} }

View File

@ -18,7 +18,7 @@ if (!defined('DC_RC_PATH')) {
class yourlsKutrlService extends kutrlService class yourlsKutrlService extends kutrlService
{ {
protected $config = [ protected $config = [
'id' => 'yourls', 'id' => 'yourls',
'name' => 'YOURLS', 'name' => 'YOURLS',
'home' => 'http://yourls.org' 'home' => 'http://yourls.org'
]; ];
@ -26,8 +26,8 @@ class yourlsKutrlService extends kutrlService
private $args = [ private $args = [
'username' => '', 'username' => '',
'password' => '', 'password' => '',
'format' => 'xml', 'format' => 'xml',
'action' => 'shorturl' 'action' => 'shorturl'
]; ];
protected function init() protected function init()
@ -38,8 +38,8 @@ class yourlsKutrlService extends kutrlService
$base = (string) $this->settings->kutrl_srv_yourls_base; $base = (string) $this->settings->kutrl_srv_yourls_base;
//if (!empty($base) && substr($base,-1,1) != '/') $base .= '/'; //if (!empty($base) && substr($base,-1,1) != '/') $base .= '/';
$this->config['url_api'] = $base; $this->config['url_api'] = $base;
$this->config['url_base'] = $base; $this->config['url_base'] = $base;
$this->config['url_min_len'] = strlen($base)+3; $this->config['url_min_len'] = strlen($base)+3;
} }
@ -78,6 +78,7 @@ class yourlsKutrlService extends kutrlService
{ {
if (empty($this->url_api)) { if (empty($this->url_api)) {
$this->error->add(__('Service is not well configured.')); $this->error->add(__('Service is not well configured.'));
return false; return false;
} }
@ -86,24 +87,27 @@ class yourlsKutrlService extends kutrlService
if (!($response = self::post($this->url_api, $this->args, true))) { if (!($response = self::post($this->url_api, $this->args, true))) {
$this->error->add(__('Service is unavailable.')); $this->error->add(__('Service is unavailable.'));
return false; return false;
} }
$rsp = @simplexml_load_string($response); $rsp = @simplexml_load_string($response);
if ($rsp && $rsp->status == 'success') { if ($rsp && $rsp->status == 'success') {
return true; return true;
} }
$this->error->add(__('Authentication to service failed.')); $this->error->add(__('Authentication to service failed.'));
return false; return false;
} }
public function createHash($url, $hash = null) public function createHash($url, $hash = null)
{ {
$args = $this->args; $args = array_merge($this->args, ['url' => $url]);
$args['url'] = $url;
if (!($response = self::post($this->url_api, $args, true))) { if (!($response = self::post($this->url_api, $args, true))) {
$this->error->add(__('Service is unavailable.')); $this->error->add(__('Service is unavailable.'));
return false; return false;
} }
@ -112,12 +116,13 @@ class yourlsKutrlService extends kutrlService
if ($rsp && $rsp->status == 'success') { if ($rsp && $rsp->status == 'success') {
$rs = new ArrayObject(); $rs = new ArrayObject();
$rs->hash = $rsp->url[0]->keyword; $rs->hash = $rsp->url[0]->keyword;
$rs->url = $url; $rs->url = $url;
$rs->type = $this->id; $rs->type = $this->id;
return $rs; return $rs;
} }
$this->error->add(__('Unreadable service response.')); $this->error->add(__('Unreadable service response.'));
return false; return false;
} }
} }