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,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')) {

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,7 +20,7 @@ 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';
@ -65,6 +65,7 @@ class kutrlLog
$this->con->unlock(); $this->con->unlock();
throw $e; throw $e;
} }
return false; return false;
} }
@ -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

@ -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

@ -27,7 +27,8 @@ 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

@ -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,10 +82,12 @@ 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();

View File

@ -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,6 +117,7 @@ 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;
} }

View File

@ -25,8 +25,7 @@ class customKutrlService extends kutrlService
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 = [];
} }
@ -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.'));

View File

@ -62,22 +62,25 @@ 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;
} }

View File

@ -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,6 +43,7 @@ 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;
} }

View File

@ -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;
} }
} }
@ -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,8 +182,8 @@ 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);
} }
@ -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

@ -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;
} }
@ -118,6 +122,7 @@ class yourlsKutrlService extends kutrlService
return $rs; return $rs;
} }
$this->error->add(__('Unreadable service response.')); $this->error->add(__('Unreadable service response.'));
return false; return false;
} }
} }