diff --git a/inc/class.kutrl.php b/inc/class.kutrl.php index cfbade9..b227a66 100644 --- a/inc/class.kutrl.php +++ b/inc/class.kutrl.php @@ -10,95 +10,92 @@ # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html # -- END LICENSE BLOCK ------------------------------------ -if (!defined('DC_RC_PATH')){return;} +if (!defined('DC_RC_PATH')) { + return null; +} # Generic class to play easily with services class kUtRL { - # Load services list from behavior - public static function getServices($core) - { - $list = $core->getBehaviors('kutrlService'); - - if (empty($list)) return array(); - - $service = array(); - foreach($list as $k => $callback) - { - try - { - list($service_id,$service_class) = call_user_func($callback); - $services[(string) $service_id] = (string) $service_class; - } - catch (Exception $e) {} - } - return $services; - } - - # Silently try to load a service according to its id - # Return null on error else service on success - public static function quickService($id='') - { - global $core; - - try - { - if (empty($id)) { - return null; - } - $services = self::getServices($core); - if (isset($services[$id])) { - return new $services[$id]($core); - } - } - catch(Exception $e) { } - - return null; - } - - # Silently try to load a service according to its place - # Return null on error else service on success - public static function quickPlace($place='plugin') - { - global $core; - - try - { - if (!in_array($place,array('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; - } - - # Silently try to reduce url (using 'plugin' place) - # return long url on error else short url on success - public static function quickReduce($url,$custom=null,$place='plugin') - { - global $core; - - 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; - } -} -?> \ No newline at end of file + # Load services list from behavior + public static function getServices($core) + { + $list = $core->getBehaviors('kutrlService'); + + if (empty($list)) { + return []; + } + $service = []; + foreach($list as $k => $callback) { + try { + list($service_id,$service_class) = call_user_func($callback); + $services[(string) $service_id] = (string) $service_class; + } catch (Exception $e) { + + } + } + return $services; + } + + # Silently try to load a service according to its id + # Return null on error else service on success + public static function quickService($id = '') + { + global $core; + + try { + if (empty($id)) { + return null; + } + $services = self::getServices($core); + if (isset($services[$id])) { + return new $services[$id]($core); + } + } catch(Exception $e) { + + } + return null; + } + + # Silently try to load a service according to its place + # Return null on error else service on success + public static function quickPlace($place = 'plugin') + { + global $core; + + 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; + } + + # Silently try to reduce url (using 'plugin' place) + # return long url on error else short url on success + public static function quickReduce($url, $custom = null, $place = 'plugin') + { + global $core; + + 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; + } +} \ No newline at end of file diff --git a/inc/index.link.php b/inc/index.link.php index d2bd1a1..1283dc9 100644 --- a/inc/index.link.php +++ b/inc/index.link.php @@ -12,142 +12,128 @@ # This file manage admin link creation of kUtRL (called from index.php) -if (!defined('DC_CONTEXT_ADMIN')){return;} +if (!defined('DC_CONTEXT_ADMIN')) { + return null; +} $kut = kutrl::quickPlace('admin'); # Create a new link if ($action == 'createlink') { + try { + if (null === $kut) { + throw new Exception('Unknow service'); + } + $url = trim($core->con->escape($_POST['str'])); + $hash = empty($_POST['custom']) ? null : $_POST['custom']; + + if (empty($url)) { + throw new Exception(__('There is nothing to shorten.')); + } + if (!$kut->testService()) { + throw new Exception(__('Service is not well configured.')); + } + if (null !== $hash && !$kut->allow_custom_hash) { + throw new Exception(__('This service does not allowed custom hash.')); + } + if (!$kut->isValidUrl($url)) { + throw new Exception(__('This link is not a valid URL.')); + } + if (!$kut->isLongerUrl($url)) { + throw new Exception(__('This link is too short.')); + } + if (!$kut->isProtocolUrl($url)) { + throw new Exception(__('This type of link is not allowed.')); + } + if (!$kut->allow_external_url && !$kut->isBlogUrl($url)) { + throw new Exception(__('Short links are limited to this blog URL.')); + } + if ($kut->isServiceUrl($url)) { + throw new Exception(__('This link is already a short link.')); + } + if (null !== $hash && false !== ($rs = $kut->isKnowHash($hash))) { + throw new Exception(__('This custom short url is already taken.')); + } + if (false !== ($rs = $kut->isKnowUrl($url))) { + $url = $rs->url; + $new_url = $kut->url_base .$rs->hash; + $msg = + '
'; + } else { + if (false === ($rs = $kut->hash($url, $hash))) { + if ($kut->error->flag()) { + throw new Exception($kut->error->toHTML()); + } + throw new Exception(__('Failed to create short link. This could be caused by a service failure.')); + } else { + $url = $rs->url; + $new_url = $kut->url_base . $rs->hash; + $msg = + ' '; - try - { - if (null === $kut) - throw new Exception('Unknow service'); - - $url = trim($core->con->escape($_POST['str'])); - $hash = empty($_POST['custom']) ? null : $_POST['custom']; - - if (empty($url)) - throw new Exception(__('There is nothing to shorten.')); - - if (!$kut->testService()) - throw new Exception(__('Service is not well configured.')); - - if (null !== $hash && !$kut->allow_custom_hash) - throw new Exception(__('This service does not allowed custom hash.')); - - if (!$kut->isValidUrl($url)) - throw new Exception(__('This link is not a valid URL.')); - - if (!$kut->isLongerUrl($url)) - throw new Exception(__('This link is too short.')); - - if (!$kut->isProtocolUrl($url)) - throw new Exception(__('This type of link is not allowed.')); - - if (!$kut->allow_external_url && !$kut->isBlogUrl($url)) - throw new Exception(__('Short links are limited to this blog URL.')); - - if ($kut->isServiceUrl($url)) - throw new Exception(__('This link is already a short link.')); - - if (null !== $hash && false !== ($rs = $kut->isKnowHash($hash))) - throw new Exception(__('This custom short url is already taken.')); - - if (false !== ($rs = $kut->isKnowUrl($url))) - { - $url = $rs->url; - $new_url = $kut->url_base.$rs->hash; - $msg = - ' '; - } - else - { - if (false === ($rs = $kut->hash($url,$hash))) - { - if ($kut->error->flag()) - { - throw new Exception($kut->error->toHTML()); - } - throw new Exception(__('Failed to create short link. This could be caused by a service failure.')); - } - else - { - $url = $rs->url; - $new_url = $kut->url_base.$rs->hash; - $msg = - ' '; - - # ex: Send new url to messengers - if (!empty($rs)) - { - $core->callBehavior('adminAfterKutrlCreate',$core,$rs,__('New short URL')); - } - } - } - } - catch (Exception $e) - { - $core->error->add($e->getMessage()); - } + # ex: Send new url to messengers + if (!empty($rs)) { + $core->callBehavior('adminAfterKutrlCreate', $core, $rs,__('New short URL')); + } + } + } + } catch (Exception $e) { + $core->error->add($e->getMessage()); + } } echo ' -'.__('You must set an admin service.').'
'; -} -else -{ - echo ' - '; +if (null === $kut) { + echo '' . __('You must set an admin service.') . '
'; +} else { + echo ' + '; } dcPage::helpBlock('kUtRL'); -echo $footer.''; -?> \ No newline at end of file +echo $footer . '