cleanup public file

master
Jean-Christian Paul Denis 2021-08-27 23:13:43 +02:00
parent be3d060e51
commit d01496f5cf
1 changed files with 527 additions and 583 deletions

View File

@ -10,38 +10,40 @@
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
# -- END LICENSE BLOCK ------------------------------------ # -- END LICENSE BLOCK ------------------------------------
if (!defined('DC_RC_PATH')){return;} if (!defined('DC_RC_PATH')) {
return null;
}
require_once dirname(__FILE__).'/_widgets.php'; require_once dirname(__FILE__) . '/_widgets.php';
$core->tpl->setPath($core->tpl->getPath(),dirname(__FILE__).'/default-templates'); $core->tpl->setPath($core->tpl->getPath(), dirname(__FILE__) . '/default-templates');
$core->addBehavior('publicBeforeDocument',array('pubKutrl','publicBeforeDocument')); $core->addBehavior('publicBeforeDocument', ['pubKutrl', 'publicBeforeDocument']);
$core->addBehavior('publicHeadContent',array('pubKutrl','publicHeadContent')); $core->addBehavior('publicHeadContent', ['pubKutrl', 'publicHeadContent']);
$core->addBehavior('publicBeforeContentFilter',array('pubKutrl','publicBeforeContentFilter')); $core->addBehavior('publicBeforeContentFilter', ['pubKutrl', 'publicBeforeContentFilter']);
$core->addBehavior('templateBeforeValue',array('pubKutrl','templateBeforeValue')); $core->addBehavior('templateBeforeValue', ['pubKutrl', 'templateBeforeValue']);
$core->addBehavior('templateAfterValue',array('pubKutrl','templateAfterValue')); $core->addBehavior('templateAfterValue', ['pubKutrl', 'templateAfterValue']);
$core->tpl->addBlock('kutrlPageIf',array('tplKutrl','pageIf')); $core->tpl->addBlock('kutrlPageIf', ['tplKutrl', 'pageIf']);
$core->tpl->addBlock('kutrlMsgIf',array('tplKutrl','pageMsgIf')); $core->tpl->addBlock('kutrlMsgIf', ['tplKutrl', 'pageMsgIf']);
$core->tpl->addValue('kutrlPageURL',array('tplKutrl','pageURL')); $core->tpl->addValue('kutrlPageURL', ['tplKutrl', 'pageURL']);
$core->tpl->addValue('kutrlMsg',array('tplKutrl','pageMsg')); $core->tpl->addValue('kutrlMsg', ['tplKutrl', 'pageMsg']);
$core->tpl->addValue('kutrlHumanField',array('tplKutrl','humanField')); $core->tpl->addValue('kutrlHumanField', ['tplKutrl', 'humanField']);
$core->tpl->addValue('kutrlHumanFieldProtect',array('tplKutrl','humanFieldProtect')); $core->tpl->addValue('kutrlHumanFieldProtect', ['tplKutrl', 'humanFieldProtect']);
$core->tpl->addBlock('AttachmentKutrlIf',array('tplKutrl','AttachmentKutrlIf')); $core->tpl->addBlock('AttachmentKutrlIf', ['tplKutrl', 'AttachmentKutrlIf']);
$core->tpl->addValue('AttachmentKutrl',array('tplKutrl','AttachmentKutrl')); $core->tpl->addValue('AttachmentKutrl', ['tplKutrl', 'AttachmentKutrl']);
$core->tpl->addBlock('MediaKutrlIf',array('tplKutrl','MediaKutrlIf')); $core->tpl->addBlock('MediaKutrlIf', ['tplKutrl', 'MediaKutrlIf']);
$core->tpl->addValue('MediaKutrl',array('tplKutrl','MediaKutrl')); $core->tpl->addValue('MediaKutrl', ['tplKutrl', 'MediaKutrl']);
$core->tpl->addBlock('EntryAuthorKutrlIf',array('tplKutrl','EntryAuthorKutrlIf')); $core->tpl->addBlock('EntryAuthorKutrlIf', ['tplKutrl', 'EntryAuthorKutrlIf']);
$core->tpl->addValue('EntryAuthorKutrl',array('tplKutrl','EntryAuthorKutrl')); $core->tpl->addValue('EntryAuthorKutrl', ['tplKutrl', 'EntryAuthorKutrl']);
$core->tpl->addBlock('EntryKutrlIf',array('tplKutrl','EntryKutrlIf')); $core->tpl->addBlock('EntryKutrlIf', ['tplKutrl', 'EntryKutrlIf']);
$core->tpl->addValue('EntryKutrl',array('tplKutrl','EntryKutrl')); $core->tpl->addValue('EntryKutrl', ['tplKutrl', 'EntryKutrl']);
$core->tpl->addBlock('CommentAuthorKutrlIf',array('tplKutrl','CommentAuthorKutrlIf')); $core->tpl->addBlock('CommentAuthorKutrlIf', ['tplKutrl', 'CommentAuthorKutrlIf']);
$core->tpl->addValue('CommentAuthorKutrl',array('tplKutrl','CommentAuthorKutrl')); $core->tpl->addValue('CommentAuthorKutrl', ['tplKutrl', 'CommentAuthorKutrl']);
$core->tpl->addBlock('CommentPostKutrlIf',array('tplKutrl','CommentPostKutrlIf')); $core->tpl->addBlock('CommentPostKutrlIf', ['tplKutrl', 'CommentPostKutrlIf']);
$core->tpl->addValue('CommentPostKutrl',array('tplKutrl','CommentPostKutrl')); $core->tpl->addValue('CommentPostKutrl', ['tplKutrl', 'CommentPostKutrl']);
class urlKutrl extends dcUrlHandlers class urlKutrl extends dcUrlHandlers
{ {
@ -52,16 +54,14 @@ class urlKutrl extends dcUrlHandlers
$s = $core->blog->settings->kUtRL; $s = $core->blog->settings->kUtRL;
# 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; 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; return null;
} }
$args = isset($m[3]) ? $m[3] : ''; $args = isset($m[3]) ? $m[3] : '';
@ -72,42 +72,36 @@ class urlKutrl extends dcUrlHandlers
$kut = new localKutrlService($core); $kut = new localKutrlService($core);
# Nothing on url # Nothing on url
if ($m[1] == '/') if ($m[1] == '/') {
{
$_ctx->kutrl_msg = 'No link given.'; $_ctx->kutrl_msg = 'No link given.';
} }
# find suffix on redirect url # find suffix on redirect url
$suffix = ''; $suffix = '';
if (preg_match('@^([^?/#]+)(.*?)$@',$args,$more)) if (preg_match('@^([^?/#]+)(.*?)$@', $args, $more)) {
{
$args = $more[1]; $args = $more[1];
$suffix = $more[2]; $suffix = $more[2];
} }
# No arg, go to kurtl page # No arg, go to kurtl page
if ($args == '') if ($args == '') {
{
self::pageKutrl($kut); self::pageKutrl($kut);
return; return null;
} }
# Not find, go to kutrl 404 # Not find, go to kutrl 404
if (false === ($url = $kut->getUrl($args))) if (false === ($url = $kut->getUrl($args))) {
{
//$_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; 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; return null;
} }
$core->blog->triggerBlog(); $core->blog->triggerBlog();
http::redirect($url.$suffix); http::redirect($url . $suffix);
return; return null;
} }
private static function pageKutrl($kut) private static function pageKutrl($kut)
@ -116,147 +110,122 @@ class urlKutrl extends dcUrlHandlers
$s = $core->blog->settings->kUtRL; $s = $core->blog->settings->kUtRL;
# 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; 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; return null;
} }
# Validation form # Validation form
$url = !empty($_POST['longurl']) ? trim($core->con->escape($_POST['longurl'])) : ''; $url = !empty($_POST['longurl']) ? trim($core->con->escape($_POST['longurl'])) : '';
if (!empty($url)) if (!empty($url)) {
{
$hmf = !empty($_POST['hmf']) ? $_POST['hmf'] : '!'; $hmf = !empty($_POST['hmf']) ? $_POST['hmf'] : '!';
$hmfu = !empty($_POST['hmfp']) ? hmfKutrl::unprotect($_POST['hmfp']) : '?'; $hmfu = !empty($_POST['hmfp']) ? hmfKutrl::unprotect($_POST['hmfp']) : '?';
$err = false; $err = false;
if (!$err) if (!$err) {
{ if ($hmf != $hmfu) {
if ($hmf != $hmfu)
{
$err = true; $err = true;
$_ctx->kutrl_msg = __('Failed to verify protected field.'); $_ctx->kutrl_msg = __('Failed to verify protected field.');
} }
} }
if (!$err) if (!$err) {
{ if (!$kut->testService()) {
if (!$kut->testService())
{
$err = true; $err = true;
$_ctx->kutrl_msg = __('Service is not well configured.'); $_ctx->kutrl_msg = __('Service is not well configured.');
} }
} }
if (!$err) if (!$err) {
{ if (!$kut->isValidUrl($url)) {
if (!$kut->isValidUrl($url))
{
$err = true; $err = true;
$_ctx->kutrl_msg = __('This string is not a valid URL.'); $_ctx->kutrl_msg = __('This string is not a valid URL.');
} }
} }
if (!$err) if (!$err) {
{ if (!$kut->isLongerUrl($url)) {
if (!$kut->isLongerUrl($url))
{
$err = true; $err = true;
$_ctx->kutrl_msg = __('This link is too short.'); $_ctx->kutrl_msg = __('This link is too short.');
} }
} }
if (!$err) if (!$err) {
{ if (!$kut->isProtocolUrl($url)) {
if (!$kut->isProtocolUrl($url))
{
$err = true; $err = true;
$_ctx->kutrl_msg = __('This type of link is not allowed.'); $_ctx->kutrl_msg = __('This type of link is not allowed.');
} }
} }
if (!$err) if (!$err) {
{ if (!$kut->allow_external_url && !$kut->isBlogUrl($url)) {
if (!$kut->allow_external_url && !$kut->isBlogUrl($url))
{
$err = true; $err = true;
$_ctx->kutrl_msg = __('Short links are limited to this blog URL.'); $_ctx->kutrl_msg = __('Short links are limited to this blog URL.');
} }
} }
if (!$err) if (!$err) {
{ if ($kut->isServiceUrl($url)) {
if ($kut->isServiceUrl($url))
{
$err = true; $err = true;
$_ctx->kutrl_msg = __('This link is already a short link.'); $_ctx->kutrl_msg = __('This link is already a short link.');
} }
} }
if (!$err) if (!$err) {
{ if (false !== ($rs = $kut->isKnowUrl($url))) {
if (false !== ($rs = $kut->isKnowUrl($url)))
{
$err = true; $err = true;
$url = $rs->url; $url = $rs->url;
$new_url = $kut->url_base.$rs->hash; $new_url = $kut->url_base . $rs->hash;
$_ctx->kutrl_msg = sprintf( $_ctx->kutrl_msg = sprintf(
__('Short link for %s is %s'), __('Short link for %s is %s'),
html::escapeHTML($url), html::escapeHTML($url),
'<a href="'.$new_url.'">'.$new_url.'</a>' '<a href="' . $new_url . '">' . $new_url . '</a>'
); );
} }
} }
if (!$err) if (!$err) {
{ if (false === ($rs = $kut->hash($url))) {
if (false === ($rs = $kut->hash($url)))
{
$err = true; $err = true;
$_ctx->kutrl_msg = __('Failed to create short link.'); $_ctx->kutrl_msg = __('Failed to create short link.');
} } else {
else
{
$url = $rs->url; $url = $rs->url;
$new_url = $kut->url_base.$rs->hash; $new_url = $kut->url_base . $rs->hash;
$_ctx->kutrl_msg = sprintf( $_ctx->kutrl_msg = sprintf(
__('Short link for %s is %s'), __('Short link for %s is %s'),
html::escapeHTML($url), html::escapeHTML($url),
'<a href="'.$new_url.'">'.$new_url.'</a>' '<a href="' . $new_url . '">' . $new_url . '</a>'
); );
$core->blog->triggerBlog(); $core->blog->triggerBlog();
# ex: Send new url to messengers # ex: Send new url to messengers
if (!empty($rs)) if (!empty($rs)) {
{ $core->callBehavior('publicAfterKutrlCreate', $core, $rs, __('New public short URL'));
$core->callBehavior('publicAfterKutrlCreate',$core,$rs,__('New public short URL'));
} }
} }
} }
} }
$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; return null;
} }
protected static function kutrl404() protected static function kutrl404()
{ {
global $core; global $core;
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; return null;
} }
$core->tpl->setPath($core->tpl->getPath(),dirname(__FILE__).'/default-templates'); $core->tpl->setPath($core->tpl->getPath(), dirname(__FILE__) . '/default-templates');
$_ctx =& $GLOBALS['_ctx']; $_ctx =& $GLOBALS['_ctx'];
$core = $GLOBALS['core']; $core = $GLOBALS['core'];
header('Content-Type: text/html; charset=UTF-8'); header('Content-Type: text/html; charset=UTF-8');
http::head(404,'Not Found'); http::head(404, 'Not Found');
$core->url->type = '404'; $core->url->type = '404';
$_ctx->current_tpl = 'kutrl404.html'; $_ctx->current_tpl = 'kutrl404.html';
$_ctx->content_type = 'text/html'; $_ctx->content_type = 'text/html';
@ -264,7 +233,7 @@ class urlKutrl extends dcUrlHandlers
echo $core->tpl->getData($_ctx->current_tpl); echo $core->tpl->getData($_ctx->current_tpl);
# --BEHAVIOR-- publicAfterDocument # --BEHAVIOR-- publicAfterDocument
$core->callBehavior('publicAfterDocument',$core); $core->callBehavior('publicAfterDocument', $core);
exit; exit;
} }
} }
@ -272,7 +241,7 @@ class urlKutrl extends dcUrlHandlers
class pubKutrl class pubKutrl
{ {
# List of template tag which content URL that can be shortenn # List of template tag which content URL that can be shortenn
public static $know_tags = array( public static $know_tags = [
'AttachmentURL', 'AttachmentURL',
'CategoryURL', 'CategoryURL',
'MediaURL', 'MediaURL',
@ -281,60 +250,57 @@ class pubKutrl
'EntryCategoryURL', 'EntryCategoryURL',
'CommentAuthorURL', 'CommentAuthorURL',
'CommentPostURL' 'CommentPostURL'
); ];
# Disable URL shoretning on filtered tag # Disable URL shoretning on filtered tag
public static function templateBeforeValue($core,$tag,$attr) public static function templateBeforeValue($core, $tag, $attr)
{
if (!empty($attr['disable_kutrl']) && in_array($tag,pubKutrl::$know_tags))
{ {
if (!empty($attr['disable_kutrl']) && in_array($tag,pubKutrl::$know_tags)) {
return '<?php $GLOBALS["disable_kutrl"] = true; ?>'; return '<?php $GLOBALS["disable_kutrl"] = true; ?>';
} }
return; return null;
} }
# Re unable it after tag # Re unable it after tag
public static function templateAfterValue($core,$tag,$attr) public static function templateAfterValue($core, $tag, $attr)
{
if (!empty($attr['disable_kutrl']) && in_array($tag,pubKutrl::$know_tags))
{ {
if (!empty($attr['disable_kutrl']) && in_array($tag,pubKutrl::$know_tags)) {
return '<?php $GLOBALS["disable_kutrl"] = false; ?>'; return '<?php $GLOBALS["disable_kutrl"] = false; ?>';
} }
return; return null;
} }
# Replace long urls on the fly (on filter) for default tags # Replace long urls on the fly (on filter) for default tags
public static function publicBeforeContentFilter($core,$tag,$args) public static function publicBeforeContentFilter($core, $tag, $args)
{ {
# Unknow tag # Unknow tag
if (!in_array($tag,pubKutrl::$know_tags)) return; if (!in_array($tag,pubKutrl::$know_tags)) {
return null;
}
# URL shortening is disabled by tag attribute # URL shortening is disabled by tag attribute
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) return; || !$core->blog->settings->kUtRL->kutrl_tpl_active) {
return null;
}
global $_ctx; global $_ctx;
# Oups # Oups
if (!$_ctx->exists('kutrl')) return; if (!$_ctx->exists('kutrl')) {
return null;
# Existing
if (false !== ($kutrl_rs = $_ctx->kutrl->isKnowUrl($args[0])))
{
$args[0] = $_ctx->kutrl->url_base.$kutrl_rs->hash;
} }
# Existing
if (false !== ($kutrl_rs = $_ctx->kutrl->isKnowUrl($args[0]))) {
$args[0] = $_ctx->kutrl->url_base . $kutrl_rs->hash;
# New # New
elseif (false !== ($kutrl_rs = $_ctx->kutrl->hash($args[0]))) } elseif (false !== ($kutrl_rs = $_ctx->kutrl->hash($args[0]))) {
{ $args[0] = $_ctx->kutrl->url_base . $kutrl_rs->hash;
$args[0] = $_ctx->kutrl->url_base.$kutrl_rs->hash;
# ex: Send new url to messengers # ex: Send new url to messengers
if (!empty($kutrl_rs)) if (!empty($kutrl_rs)) {
{ $core->callBehavior('publicAfterKutrlCreate', $core, $kutrl_rs, __('New public short URL'));
$core->callBehavior('publicAfterKutrlCreate',$core,$kutrl_rs,__('New public short URL'));
} }
} }
} }
@ -348,22 +314,23 @@ class pubKutrl
# Passive : all kutrl tag return long url # Passive : all kutrl tag return long url
$_ctx->kutrl_passive = (boolean) $s->kutrl_tpl_passive; $_ctx->kutrl_passive = (boolean) $s->kutrl_tpl_passive;
if (!$s->kutrl_active || !$s->kutrl_tpl_service) return; if (!$s->kutrl_active || !$s->kutrl_tpl_service) {
return null;
if (null === ($kut = kutrl::quickPlace('tpl'))) return; }
if (null === ($kut = kutrl::quickPlace('tpl'))) {
return null;
}
$_ctx->kutrl = $kut; $_ctx->kutrl = $kut;
} }
public static function publicHeadContent($core) public static function publicHeadContent($core)
{ {
$css = $core->blog->settings->kUtRL->kutrl_srv_local_css; $css = $core->blog->settings->kUtRL->kutrl_srv_local_css;
if ($css) if ($css) {
{
echo echo
"\n<!-- CSS for kUtRL --> \n". "\n<!-- CSS for kUtRL --> \n" .
"<style type=\"text/css\"> \n". "<style type=\"text/css\"> \n" .
html::escapeHTML($css)."\n". html::escapeHTML($css) . "\n" .
"</style>\n"; "</style>\n";
} }
} }
@ -374,48 +341,42 @@ class tplKutrl
public static function pageURL($attr) public static function pageURL($attr)
{ {
$f = $GLOBALS['core']->tpl->getFilters($attr); $f = $GLOBALS['core']->tpl->getFilters($attr);
return '<?php echo '.sprintf($f,'$core->blog->url.$core->url->getBase("kutrl")').'; ?>'; return '<?php echo ' . sprintf($f, '$core->blog->url.$core->url->getBase("kutrl")') . '; ?>';
} }
public static function pageIf($attr,$content) public static function pageIf($attr, $content)
{ {
$operator = isset($attr['operator']) ? self::getOperator($attr['operator']) : '&&'; $operator = isset($attr['operator']) ? self::getOperator($attr['operator']) : '&&';
if (isset($attr['is_active'])) if (isset($attr['is_active'])) {
{
$sign = (boolean) $attr['is_active'] ? '' : '!'; $sign = (boolean) $attr['is_active'] ? '' : '!';
$if[] = $sign.'$core->blog->settings->kUtRL->kutrl_srv_local_public'; $if[] = $sign . '$core->blog->settings->kUtRL->kutrl_srv_local_public';
} }
if (empty($if)) {
if (empty($if))
{
return $content; return $content;
} }
return return
"<?php if(".implode(' '.$operator.' ',$if).") : ?>\n". "<?php if(" . implode(' ' . $operator . ' ', $if) . ") : ?>\n" .
$content. $content .
"<?php endif; unset(\$s);?>\n"; "<?php endif; unset(\$s);?>\n";
} }
public static function pageMsgIf($attr,$content) public static function pageMsgIf($attr, $content)
{ {
$operator = isset($attr['operator']) ? self::getOperator($attr['operator']) : '&&'; $operator = isset($attr['operator']) ? self::getOperator($attr['operator']) : '&&';
if (isset($attr['has_message'])) if (isset($attr['has_message'])) {
{
$sign = (boolean) $attr['has_message'] ? '!' : '='; $sign = (boolean) $attr['has_message'] ? '!' : '=';
$if[] = '"" '.$sign.'= $_ctx->kutrl_msg'; $if[] = '"" ' . $sign . '= $_ctx->kutrl_msg';
} }
if (empty($if)) {
if (empty($if))
{
return $content; return $content;
} }
return return
"<?php if(".implode(' '.$operator.' ',$if).") : ?>\n". "<?php if(" . implode(' ' . $operator . ' ', $if) . ") : ?>\n" .
$content. $content .
"<?php endif; ?>\n"; "<?php endif; ?>\n";
} }
@ -436,138 +397,130 @@ class tplKutrl
"<?php echo \$core->formNonce(); ?>"; "<?php echo \$core->formNonce(); ?>";
} }
public static function AttachmentKutrlIf($attr,$content) public static function AttachmentKutrlIf($attr, $content)
{ {
return self::genericKutrlIf('$attach_f->file_url',$attr,$content); return self::genericKutrlIf('$attach_f->file_url', $attr, $content);
} }
public static function AttachmentKutrl($attr) public static function AttachmentKutrl($attr)
{ {
return self::genericKutrl('$attach_f->file_url',$attr); return self::genericKutrl('$attach_f->file_url', $attr);
} }
public static function MediaKutrlIf($attr,$content) public static function MediaKutrlIf($attr, $content)
{ {
return self::genericKutrlIf('$_ctx->file_url',$attr,$content); return self::genericKutrlIf('$_ctx->file_url', $attr, $content);
} }
public static function MediaKutrl($attr) public static function MediaKutrl($attr)
{ {
return self::genericKutrl('$_ctx->file_url',$attr); return self::genericKutrl('$_ctx->file_url', $attr);
} }
public static function EntryAuthorKutrlIf($attr,$content) public static function EntryAuthorKutrlIf($attr, $content)
{ {
return self::genericKutrlIf('$_ctx->posts->user_url',$attr,$content); return self::genericKutrlIf('$_ctx->posts->user_url', $attr, $content);
} }
public static function EntryAuthorKutrl($attr) public static function EntryAuthorKutrl($attr)
{ {
return self::genericKutrl('$_ctx->posts->user_url',$attr); return self::genericKutrl('$_ctx->posts->user_url', $attr);
} }
public static function EntryKutrlIf($attr,$content) public static function EntryKutrlIf($attr,$content)
{ {
return self::genericKutrlIf('$_ctx->posts->getURL()',$attr,$content); return self::genericKutrlIf('$_ctx->posts->getURL()', $attr, $content);
} }
public static function EntryKutrl($attr) public static function EntryKutrl($attr)
{ {
return self::genericKutrl('$_ctx->posts->getURL()',$attr); return self::genericKutrl('$_ctx->posts->getURL()', $attr);
} }
public static function CommentAuthorKutrlIf($attr,$content) public static function CommentAuthorKutrlIf($attr, $content)
{ {
return self::genericKutrlIf('$_ctx->comments->getAuthorURL()',$attr,$content); return self::genericKutrlIf('$_ctx->comments->getAuthorURL()', $attr, $content);
} }
public static function CommentAuthorKutrl($attr) public static function CommentAuthorKutrl($attr)
{ {
return self::genericKutrl('$_ctx->comments->getAuthorURL()',$attr); return self::genericKutrl('$_ctx->comments->getAuthorURL()', $attr);
} }
public static function CommentPostKutrlIf($attr,$content) public static function CommentPostKutrlIf($attr, $content)
{ {
return self::genericKutrlIf('$_ctx->comments->getPostURL()',$attr,$content); return self::genericKutrlIf('$_ctx->comments->getPostURL()', $attr, $content);
} }
public static function CommentPostKutrl($attr) public static function CommentPostKutrl($attr)
{ {
return self::genericKutrl('$_ctx->comments->getPostURL()',$attr); return self::genericKutrl('$_ctx->comments->getPostURL()', $attr);
} }
protected static function genericKutrlIf($str,$attr,$content) protected static function genericKutrlIf($str, $attr, $content)
{ {
$operator = isset($attr['operator']) ? self::getOperator($attr['operator']) : '&&'; $operator = isset($attr['operator']) ? self::getOperator($attr['operator']) : '&&';
if (isset($attr['is_active'])) if (isset($attr['is_active'])) {
{
$sign = (boolean) $attr['is_active'] ? '' : '!'; $sign = (boolean) $attr['is_active'] ? '' : '!';
$if[] = $sign.'$_ctx->exists("kutrl")'; $if[] = $sign . '$_ctx->exists("kutrl")';
} }
if (isset($attr['passive_mode'])) {
if (isset($attr['passive_mode']))
{
$sign = (boolean) $attr['passive_mode'] ? '' : '!'; $sign = (boolean) $attr['passive_mode'] ? '' : '!';
$if[] = $sign.'$_ctx->kutrl_passive'; $if[] = $sign . '$_ctx->kutrl_passive';
} }
if (isset($attr['has_kutrl'])) {
if (isset($attr['has_kutrl']))
{
$sign = (boolean) $attr['has_kutrl'] ? '!' : '='; $sign = (boolean) $attr['has_kutrl'] ? '!' : '=';
$if[] = '($_ctx->exists("kutrl") && false '.$sign.'== $_ctx->kutrl->select('.$str.',null,null,"kutrl"))'; $if[] = '($_ctx->exists("kutrl") && false ' . $sign . '== $_ctx->kutrl->select(' . $str . ',null,null,"kutrl"))';
} }
if (empty($if)) {
if (empty($if))
{
return $content; return $content;
} }
return return
"<?php if(".implode(' '.$operator.' ',$if).") : ?>\n". "<?php if(" . implode(' ' . $operator . ' ', $if) . ") : ?>\n" .
$content. $content .
"<?php endif; ?>\n"; "<?php endif; ?>\n";
} }
protected static function genericKutrl($str,$attr) protected static function genericKutrl($str, $attr)
{ {
$f = $GLOBALS['core']->tpl->getFilters($attr); $f = $GLOBALS['core']->tpl->getFilters($attr);
return return
"<?php \n". "<?php \n" .
# Preview # Preview
"if (\$_ctx->preview) { \n". "if (\$_ctx->preview) { \n" .
" echo ".sprintf($f,$str)."; ". " echo " . sprintf($f, $str) . "; " .
"} else { \n". "} else { \n" .
# Disable # Disable
"if (!\$_ctx->exists('kutrl')) { \n". "if (!\$_ctx->exists('kutrl')) { \n" .
# Passive mode # Passive mode
" if (\$_ctx->kutrl_passive) { ". " if (\$_ctx->kutrl_passive) { " .
" echo ".sprintf($f,$str)."; ". " echo " . sprintf($f, $str) . "; " .
" } \n". " } \n" .
"} else { \n". "} else { \n" .
# Existing # Existing
" if (false !== (\$kutrl_rs = \$_ctx->kutrl->isKnowUrl(".$str."))) { ". " if (false !== (\$kutrl_rs = \$_ctx->kutrl->isKnowUrl(" . $str . "))) { " .
" echo ".sprintf($f,'$_ctx->kutrl->url_base.$kutrl_rs->hash')."; ". " echo " . sprintf($f, '$_ctx->kutrl->url_base.$kutrl_rs->hash') . "; " .
" } \n". " } \n" .
# New # New
" elseif (false !== (\$kutrl_rs = \$_ctx->kutrl->hash(".$str."))) { ". " elseif (false !== (\$kutrl_rs = \$_ctx->kutrl->hash(" . $str . "))) { " .
" echo ".sprintf($f,'$_ctx->kutrl->url_base.$kutrl_rs->hash')."; ". " echo " . sprintf($f, '$_ctx->kutrl->url_base.$kutrl_rs->hash') . "; " .
# ex: Send new url to messengers # ex: Send new url to messengers
" if (!empty(\$kutrl_rs)) { ". " if (!empty(\$kutrl_rs)) { " .
" \$core->callBehavior('publicAfterKutrlCreate',\$core,\$kutrl_rs,__('New public short URL')); ". " \$core->callBehavior('publicAfterKutrlCreate',\$core,\$kutrl_rs,__('New public short URL')); " .
" } \n". " } \n" .
" } \n". " } \n" .
" unset(\$kutrl_rs); \n". " unset(\$kutrl_rs); \n" .
"} \n". "} \n" .
"} \n". "} \n" .
"?>\n"; "?>\n";
} }
protected static function getOperator($op) protected static function getOperator($op)
{ {
switch (strtolower($op)) switch (strtolower($op)) {
{
case 'or': case 'or':
case '||': case '||':
return '||'; return '||';
@ -587,12 +540,9 @@ class hmfKutrl
{ {
$res = ''; $res = '';
$chars = self::$chars; $chars = self::$chars;
for($i = 0; $i < $len; $i++) {
for($i = 0;$i < $len; $i++) $res .= $chars[rand(0, strlen($chars)-1)];
{
$res .= $chars[rand(0,strlen($chars)-1)];
} }
return $res; return $res;
} }
@ -600,26 +550,20 @@ class hmfKutrl
{ {
$res = ''; $res = '';
$chars = self::$chars; $chars = self::$chars;
for($i = 0; $i < strlen($str); $i++)
for($i = 0; $i < strlen($str);$i++)
{ {
$res .= $chars[rand(0,strlen($chars)-1)].$str[$i]; $res .= $chars[rand(0, strlen($chars)-1)] . $str[$i];
} }
return $res; return $res;
} }
public static function unprotect($str) public static function unprotect($str)
{ {
$res = ''; $res = '';
for($i = 0; $i < strlen($str); $i++) {
for($i = 0; $i < strlen($str);$i++)
{
$i++; $i++;
$res .= $str[$i]; $res .= $str[$i];
} }
return $res; return $res;
} }
} }
?>