cosmetics

This commit is contained in:
Jean-Christian 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
/**
* @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')) {
return;

View file

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

View file

@ -14,11 +14,6 @@
if (!defined('DC_RC_PATH')) {
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
if (!defined('SHORTEN_SERVICE_NAME')) {

View file

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

View file

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

View file

@ -20,7 +20,7 @@ class kutrlLog
public $blog;
public $con;
public function __construct($core)
public function __construct(dcCore $core)
{
$this->core = $core;
$this->table = $core->prefix . 'kutrl';
@ -65,6 +65,7 @@ class kutrlLog
$this->con->unlock();
throw $e;
}
return false;
}
@ -124,6 +125,7 @@ class kutrlLog
$this->con->unlock();
throw $e;
}
return false;
}
@ -152,12 +154,14 @@ class kutrlLog
$counter = $rs->isEmpty() ? 0 : $rs->kut_counter;
if ('get' == $do) {
return $counter;
} elseif ('up' == $do) {
$counter += 1;
} elseif ('reset' == $do) {
$counter = 0;
} else {
return 0;
}
@ -253,6 +257,7 @@ class kutrlLog
if (!$count_only && !empty($p['limit'])) {
$r .= $this->con->limit($p['limit']);
}
return $this->con->select($r);
}
}

View file

@ -121,9 +121,11 @@ class kutrlService
continue;
}
if (strpos($url,$protocol) === 0) {
return true;
}
}
return false;
}
@ -199,6 +201,7 @@ class kutrlService
}
$this->deleteUrl($url);
$this->log->delete($rs->id);
return true;
}
@ -238,6 +241,7 @@ class kutrlService
if ($verbose) {
return $client->getContent();
}
return true;
}
}

View file

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

View file

@ -62,14 +62,17 @@ class bilbolinksKutrlService extends kutrlService
{
if (empty($this->url_base)) {
$this->error->add(__('Service is not well configured.'));
return false;
}
$arg = ['longurl' => urlencode($this->url_test)];
if (!self::post($this->url_api, $arg, true,true)) {
$this->error->add(__('Service is unavailable.'));
return false;
}
return true;
}
@ -79,10 +82,12 @@ class bilbolinksKutrlService extends kutrlService
if (!($response = self::post($this->url_api, $arg, true, true))) {
$this->error->add(__('Service is unavailable.'));
return false;
}
if ($response == 'You are too speed!') {
$this->error->add(__('Service rate limit exceeded.'));
return false;
}
$rs = new ArrayObject();

View file

@ -76,6 +76,7 @@ class bitlyKutrlService extends kutrlService
{
if (empty($this->args['login']) || empty($this->args['apiKey'])) {
$this->error->add(__('Service is not well configured.'));
return false;
}
@ -83,6 +84,7 @@ class bitlyKutrlService extends kutrlService
$args['hash'] = 'WP9vc';
if (!($response = self::post($this->url_api . 'expand', $args, true))) {
$this->error->add(__('Failed to call service.'));
return false;
}
@ -92,18 +94,20 @@ class bitlyKutrlService extends kutrlService
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 = $this->args;
$args['longUrl'] = $url;
$args = array_merge($this->args, ['longUrl' => $url]);
if (!($response = self::post($this->url_api . 'shorten', $args, true))) {
$this->error->add(__('Failed to call service.'));
return false;
}
@ -113,6 +117,7 @@ class bitlyKutrlService extends kutrlService
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;
}

View file

@ -25,8 +25,7 @@ class customKutrlService extends kutrlService
protected function init()
{
$config = unserialize(base64_decode($this->settings->kutrl_srv_custom));
if (!is_array($config))
{
if (!is_array($config)) {
$config = [];
}
@ -89,21 +88,24 @@ class customKutrlService extends kutrlService
public function testService()
{
if (empty($this->url_api)) {
return false;
}
$url = $this->url_encode ? urlencode($this->url_test) : $this->url_test;
$arg = [$this->url_param => $url];
if (!self::post($this->url_api, $arg, true, true)) {
$this->error->add(__('Service is unavailable.'));
return false;
}
return true;
}
public function createHash($url, $hash = null)
{
$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))) {
$this->error->add(__('Service is unavailable.'));

View file

@ -62,22 +62,25 @@ class defaultKutrlService extends kutrlService
public function testService()
{
$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)) {
$this->error->add(__('Service is unavailable.'));
return false;
}
return true;
}
public function createHash($url, $hash = null)
{
$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))) {
$this->error->add(__('Service is unavailable.'));
return false;
}

View file

@ -30,8 +30,10 @@ class isgdKutrlService extends kutrlService
$arg = ['longurl' => urlencode($this->url_test)];
if (!self::post($this->url_api, $arg, true, true)) {
$this->error->add(__('Service is unavailable.'));
return false;
}
return true;
}
@ -41,6 +43,7 @@ class isgdKutrlService extends kutrlService
if (!($response = self::post($this->url_api, $arg, true, true))) {
$this->error->add(__('Service is unavailable.'));
return false;
}

View file

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

View file

@ -78,6 +78,7 @@ class yourlsKutrlService extends kutrlService
{
if (empty($this->url_api)) {
$this->error->add(__('Service is not well configured.'));
return false;
}
@ -86,24 +87,27 @@ class yourlsKutrlService extends kutrlService
if (!($response = self::post($this->url_api, $this->args, true))) {
$this->error->add(__('Service is unavailable.'));
return false;
}
$rsp = @simplexml_load_string($response);
if ($rsp && $rsp->status == 'success') {
return true;
}
$this->error->add(__('Authentication to service failed.'));
return false;
}
public function createHash($url, $hash = null)
{
$args = $this->args;
$args['url'] = $url;
$args = array_merge($this->args, ['url' => $url]);
if (!($response = self::post($this->url_api, $args, true))) {
$this->error->add(__('Service is unavailable.'));
return false;
}
@ -118,6 +122,7 @@ class yourlsKutrlService extends kutrlService
return $rs;
}
$this->error->add(__('Unreadable service response.'));
return false;
}
}