2021-08-25 23:06:22 +00:00
|
|
|
<?php
|
2021-09-02 12:32:26 +00:00
|
|
|
/**
|
|
|
|
* @brief kUtRL, a plugin for Dotclear 2
|
2021-11-06 15:43:02 +00:00
|
|
|
*
|
2021-09-02 12:32:26 +00:00
|
|
|
* @package Dotclear
|
|
|
|
* @subpackage Plugin
|
2021-11-06 15:43:02 +00:00
|
|
|
*
|
2021-09-02 12:32:26 +00:00
|
|
|
* @author Jean-Christian Denis and contributors
|
2021-11-06 15:43:02 +00:00
|
|
|
*
|
2021-09-02 12:32:26 +00:00
|
|
|
* @copyright Jean-Christian Denis
|
|
|
|
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
|
|
|
|
*/
|
2021-08-27 21:13:43 +00:00
|
|
|
if (!defined('DC_RC_PATH')) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
require_once dirname(__FILE__) . '/_widgets.php';
|
|
|
|
|
|
|
|
$core->tpl->setPath($core->tpl->getPath(), dirname(__FILE__) . '/default-templates');
|
|
|
|
|
|
|
|
$core->addBehavior('publicBeforeDocument', ['pubKutrl', 'publicBeforeDocument']);
|
|
|
|
$core->addBehavior('publicHeadContent', ['pubKutrl', 'publicHeadContent']);
|
|
|
|
$core->addBehavior('publicBeforeContentFilter', ['pubKutrl', 'publicBeforeContentFilter']);
|
|
|
|
$core->addBehavior('templateBeforeValue', ['pubKutrl', 'templateBeforeValue']);
|
|
|
|
$core->addBehavior('templateAfterValue', ['pubKutrl', 'templateAfterValue']);
|
|
|
|
|
|
|
|
$core->tpl->addBlock('kutrlPageIf', ['tplKutrl', 'pageIf']);
|
|
|
|
$core->tpl->addBlock('kutrlMsgIf', ['tplKutrl', 'pageMsgIf']);
|
|
|
|
|
|
|
|
$core->tpl->addValue('kutrlPageURL', ['tplKutrl', 'pageURL']);
|
|
|
|
$core->tpl->addValue('kutrlMsg', ['tplKutrl', 'pageMsg']);
|
|
|
|
$core->tpl->addValue('kutrlHumanField', ['tplKutrl', 'humanField']);
|
|
|
|
$core->tpl->addValue('kutrlHumanFieldProtect', ['tplKutrl', 'humanFieldProtect']);
|
|
|
|
|
|
|
|
$core->tpl->addBlock('AttachmentKutrlIf', ['tplKutrl', 'AttachmentKutrlIf']);
|
|
|
|
$core->tpl->addValue('AttachmentKutrl', ['tplKutrl', 'AttachmentKutrl']);
|
|
|
|
$core->tpl->addBlock('MediaKutrlIf', ['tplKutrl', 'MediaKutrlIf']);
|
|
|
|
$core->tpl->addValue('MediaKutrl', ['tplKutrl', 'MediaKutrl']);
|
|
|
|
$core->tpl->addBlock('EntryAuthorKutrlIf', ['tplKutrl', 'EntryAuthorKutrlIf']);
|
|
|
|
$core->tpl->addValue('EntryAuthorKutrl', ['tplKutrl', 'EntryAuthorKutrl']);
|
|
|
|
$core->tpl->addBlock('EntryKutrlIf', ['tplKutrl', 'EntryKutrlIf']);
|
|
|
|
$core->tpl->addValue('EntryKutrl', ['tplKutrl', 'EntryKutrl']);
|
|
|
|
$core->tpl->addBlock('CommentAuthorKutrlIf', ['tplKutrl', 'CommentAuthorKutrlIf']);
|
|
|
|
$core->tpl->addValue('CommentAuthorKutrl', ['tplKutrl', 'CommentAuthorKutrl']);
|
|
|
|
$core->tpl->addBlock('CommentPostKutrlIf', ['tplKutrl', 'CommentPostKutrlIf']);
|
|
|
|
$core->tpl->addValue('CommentPostKutrl', ['tplKutrl', 'CommentPostKutrl']);
|
2021-08-25 23:06:22 +00:00
|
|
|
|
|
|
|
class urlKutrl extends dcUrlHandlers
|
|
|
|
{
|
2021-08-27 21:13:43 +00:00
|
|
|
# Redirect !!! local !!! service only
|
|
|
|
public static function redirectUrl($args)
|
|
|
|
{
|
|
|
|
global $core, $_ctx;
|
|
|
|
$s = $core->blog->settings->kUtRL;
|
|
|
|
|
|
|
|
# Not active, go to default 404
|
|
|
|
if (!$s->kutrl_active) {
|
|
|
|
self::p404();
|
2021-10-26 21:03:38 +00:00
|
|
|
|
2021-08-27 21:13:43 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
# Not a valid url, go to kutrl 404
|
|
|
|
if (!preg_match('#^(|(/(.*?)))$#', $args, $m)) {
|
|
|
|
self::kutrl404();
|
2021-10-26 21:03:38 +00:00
|
|
|
|
2021-08-27 21:13:43 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-11-06 15:43:02 +00:00
|
|
|
$args = $m[3] ?? '';
|
|
|
|
$_ctx->kutrl_msg = '';
|
|
|
|
$_ctx->kutrl_hmf = hmfKutrl::create();
|
2021-08-27 21:13:43 +00:00
|
|
|
$_ctx->kutrl_hmfp = hmfKutrl::protect($_ctx->kutrl_hmf);
|
|
|
|
|
|
|
|
$kut = new localKutrlService($core);
|
|
|
|
|
|
|
|
# Nothing on url
|
|
|
|
if ($m[1] == '/') {
|
|
|
|
$_ctx->kutrl_msg = 'No link given.';
|
|
|
|
}
|
|
|
|
# find suffix on redirect url
|
|
|
|
$suffix = '';
|
|
|
|
if (preg_match('@^([^?/#]+)(.*?)$@', $args, $more)) {
|
2021-11-06 15:43:02 +00:00
|
|
|
$args = $more[1];
|
2021-08-27 21:13:43 +00:00
|
|
|
$suffix = $more[2];
|
|
|
|
}
|
|
|
|
# No arg, go to kurtl page
|
|
|
|
if ($args == '') {
|
|
|
|
self::pageKutrl($kut);
|
2021-10-26 21:03:38 +00:00
|
|
|
|
2021-08-27 21:13:43 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
# Not find, go to kutrl 404
|
|
|
|
if (false === ($url = $kut->getUrl($args))) {
|
|
|
|
//$_ctx->kutrl_msg = 'Failed to find short link.';
|
|
|
|
//self::pageKutrl($kut);
|
|
|
|
self::kutrl404();
|
2021-10-26 21:03:38 +00:00
|
|
|
|
2021-08-27 21:13:43 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
# Removed (empty url), go to kutrl 404
|
|
|
|
if (!$url) {
|
|
|
|
self::kutrl404();
|
2021-10-26 21:03:38 +00:00
|
|
|
|
2021-08-27 21:13:43 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$core->blog->triggerBlog();
|
|
|
|
http::redirect($url . $suffix);
|
2021-10-26 21:03:38 +00:00
|
|
|
|
2021-08-27 21:13:43 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static function pageKutrl($kut)
|
|
|
|
{
|
|
|
|
global $core, $_ctx;
|
|
|
|
$s = $core->blog->settings->kUtRL;
|
2021-09-02 12:32:26 +00:00
|
|
|
|
2021-08-27 21:13:43 +00:00
|
|
|
# Not active, go to default 404
|
|
|
|
if (!$s->kutrl_active) {
|
|
|
|
self::p404();
|
2021-10-26 21:03:38 +00:00
|
|
|
|
2021-08-27 21:13:43 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
# Public page not active, go to kutrl 404
|
|
|
|
if (!$s->kutrl_srv_local_public) {
|
|
|
|
self::kutrl404();
|
2021-10-26 21:03:38 +00:00
|
|
|
|
2021-08-27 21:13:43 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
# Validation form
|
|
|
|
$url = !empty($_POST['longurl']) ? trim($core->con->escape($_POST['longurl'])) : '';
|
|
|
|
if (!empty($url)) {
|
2021-11-06 15:43:02 +00:00
|
|
|
$hmf = !empty($_POST['hmf']) ? $_POST['hmf'] : '!';
|
2021-08-27 21:13:43 +00:00
|
|
|
$hmfu = !empty($_POST['hmfp']) ? hmfKutrl::unprotect($_POST['hmfp']) : '?';
|
|
|
|
|
|
|
|
$err = false;
|
|
|
|
if (!$err) {
|
|
|
|
if ($hmf != $hmfu) {
|
2021-11-06 15:43:02 +00:00
|
|
|
$err = true;
|
2021-08-27 21:13:43 +00:00
|
|
|
$_ctx->kutrl_msg = __('Failed to verify protected field.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!$err) {
|
|
|
|
if (!$kut->testService()) {
|
2021-11-06 15:43:02 +00:00
|
|
|
$err = true;
|
2021-08-27 21:13:43 +00:00
|
|
|
$_ctx->kutrl_msg = __('Service is not well configured.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!$err) {
|
|
|
|
if (!$kut->isValidUrl($url)) {
|
2021-11-06 15:43:02 +00:00
|
|
|
$err = true;
|
2021-08-27 21:13:43 +00:00
|
|
|
$_ctx->kutrl_msg = __('This string is not a valid URL.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!$err) {
|
|
|
|
if (!$kut->isLongerUrl($url)) {
|
2021-11-06 15:43:02 +00:00
|
|
|
$err = true;
|
2021-08-27 21:13:43 +00:00
|
|
|
$_ctx->kutrl_msg = __('This link is too short.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!$err) {
|
|
|
|
if (!$kut->isProtocolUrl($url)) {
|
2021-11-06 15:43:02 +00:00
|
|
|
$err = true;
|
2021-08-27 21:13:43 +00:00
|
|
|
$_ctx->kutrl_msg = __('This type of link is not allowed.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$err) {
|
|
|
|
if (!$kut->allow_external_url && !$kut->isBlogUrl($url)) {
|
2021-11-06 15:43:02 +00:00
|
|
|
$err = true;
|
2021-08-27 21:13:43 +00:00
|
|
|
$_ctx->kutrl_msg = __('Short links are limited to this blog URL.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!$err) {
|
|
|
|
if ($kut->isServiceUrl($url)) {
|
2021-11-06 15:43:02 +00:00
|
|
|
$err = true;
|
2021-08-27 21:13:43 +00:00
|
|
|
$_ctx->kutrl_msg = __('This link is already a short link.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!$err) {
|
|
|
|
if (false !== ($rs = $kut->isKnowUrl($url))) {
|
|
|
|
$err = true;
|
|
|
|
|
2021-11-06 15:43:02 +00:00
|
|
|
$url = $rs->url;
|
2021-08-27 21:13:43 +00:00
|
|
|
$new_url = $kut->url_base . $rs->hash;
|
|
|
|
|
|
|
|
$_ctx->kutrl_msg = sprintf(
|
|
|
|
__('Short link for %s is %s'),
|
|
|
|
html::escapeHTML($url),
|
|
|
|
'<a href="' . $new_url . '">' . $new_url . '</a>'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!$err) {
|
|
|
|
if (false === ($rs = $kut->hash($url))) {
|
2021-11-06 15:43:02 +00:00
|
|
|
$err = true;
|
2021-08-27 21:13:43 +00:00
|
|
|
$_ctx->kutrl_msg = __('Failed to create short link.');
|
|
|
|
} else {
|
2021-11-06 15:43:02 +00:00
|
|
|
$url = $rs->url;
|
2021-08-27 21:13:43 +00:00
|
|
|
$new_url = $kut->url_base . $rs->hash;
|
|
|
|
|
|
|
|
$_ctx->kutrl_msg = sprintf(
|
|
|
|
__('Short link for %s is %s'),
|
|
|
|
html::escapeHTML($url),
|
|
|
|
'<a href="' . $new_url . '">' . $new_url . '</a>'
|
|
|
|
);
|
|
|
|
$core->blog->triggerBlog();
|
|
|
|
|
|
|
|
# ex: Send new url to messengers
|
|
|
|
if (!empty($rs)) {
|
|
|
|
$core->callBehavior('publicAfterKutrlCreate', $core, $rs, __('New public short URL'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$core->tpl->setPath($core->tpl->getPath(), dirname(__FILE__) . '/default-templates');
|
|
|
|
self::serveDocument('kutrl.html');
|
2021-10-26 21:03:38 +00:00
|
|
|
|
2021-08-27 21:13:43 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected static function kutrl404()
|
|
|
|
{
|
|
|
|
global $core;
|
2021-09-02 12:32:26 +00:00
|
|
|
|
2021-08-27 21:13:43 +00:00
|
|
|
if (!$core->blog->settings->kUtRL->kutrl_srv_local_404_active) {
|
|
|
|
self::p404();
|
2021-10-26 21:03:38 +00:00
|
|
|
|
2021-08-27 21:13:43 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$core->tpl->setPath($core->tpl->getPath(), dirname(__FILE__) . '/default-templates');
|
2021-11-06 15:43:02 +00:00
|
|
|
$_ctx = & $GLOBALS['_ctx'];
|
2021-08-27 21:13:43 +00:00
|
|
|
$core = $GLOBALS['core'];
|
|
|
|
|
|
|
|
header('Content-Type: text/html; charset=UTF-8');
|
|
|
|
http::head(404, 'Not Found');
|
2021-11-06 15:43:02 +00:00
|
|
|
$core->url->type = '404';
|
|
|
|
$_ctx->current_tpl = 'kutrl404.html';
|
2021-08-27 21:13:43 +00:00
|
|
|
$_ctx->content_type = 'text/html';
|
|
|
|
|
|
|
|
echo $core->tpl->getData($_ctx->current_tpl);
|
|
|
|
|
|
|
|
# --BEHAVIOR-- publicAfterDocument
|
|
|
|
$core->callBehavior('publicAfterDocument', $core);
|
|
|
|
exit;
|
|
|
|
}
|
2021-08-25 23:06:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class pubKutrl
|
|
|
|
{
|
2021-08-27 21:13:43 +00:00
|
|
|
# List of template tag which content URL that can be shortenn
|
|
|
|
public static $know_tags = [
|
|
|
|
'AttachmentURL',
|
|
|
|
'CategoryURL',
|
|
|
|
'MediaURL',
|
|
|
|
'EntryAuthorURL',
|
|
|
|
'EntryURL',
|
|
|
|
'EntryCategoryURL',
|
|
|
|
'CommentAuthorURL',
|
|
|
|
'CommentPostURL'
|
|
|
|
];
|
|
|
|
|
|
|
|
# Disable URL shoretning on filtered tag
|
|
|
|
public static function templateBeforeValue($core, $tag, $attr)
|
|
|
|
{
|
2021-11-06 15:43:02 +00:00
|
|
|
if (!empty($attr['disable_kutrl']) && in_array($tag, pubKutrl::$know_tags)) {
|
2021-08-27 21:13:43 +00:00
|
|
|
return '<?php $GLOBALS["disable_kutrl"] = true; ?>';
|
|
|
|
}
|
2021-11-06 15:43:02 +00:00
|
|
|
|
2021-08-27 21:13:43 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
# Re unable it after tag
|
|
|
|
public static function templateAfterValue($core, $tag, $attr)
|
|
|
|
{
|
2021-11-06 15:43:02 +00:00
|
|
|
if (!empty($attr['disable_kutrl']) && in_array($tag, pubKutrl::$know_tags)) {
|
2021-08-27 21:13:43 +00:00
|
|
|
return '<?php $GLOBALS["disable_kutrl"] = false; ?>';
|
|
|
|
}
|
2021-11-06 15:43:02 +00:00
|
|
|
|
2021-08-27 21:13:43 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
# Replace long urls on the fly (on filter) for default tags
|
|
|
|
public static function publicBeforeContentFilter($core, $tag, $args)
|
|
|
|
{
|
|
|
|
# Unknow tag
|
2021-11-06 15:43:02 +00:00
|
|
|
if (!in_array($tag, pubKutrl::$know_tags)) {
|
2021-08-27 21:13:43 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
# URL shortening is disabled by tag attribute
|
|
|
|
if (empty($GLOBALS['disable_kutrl'])) {
|
|
|
|
# kUtRL is not activated
|
2021-11-06 15:43:02 +00:00
|
|
|
if (!$core->blog->settings->kUtRL->kutrl_active
|
2021-10-26 21:03:38 +00:00
|
|
|
|| !$core->blog->settings->kUtRL->kutrl_tpl_active
|
|
|
|
) {
|
2021-08-27 21:13:43 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
global $_ctx;
|
|
|
|
|
|
|
|
# Oups
|
|
|
|
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;
|
|
|
|
# New
|
|
|
|
} elseif (false !== ($kutrl_rs = $_ctx->kutrl->hash($args[0]))) {
|
|
|
|
$args[0] = $_ctx->kutrl->url_base . $kutrl_rs->hash;
|
|
|
|
|
|
|
|
# ex: Send new url to messengers
|
|
|
|
if (!empty($kutrl_rs)) {
|
|
|
|
$core->callBehavior('publicAfterKutrlCreate', $core, $kutrl_rs, __('New public short URL'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function publicBeforeDocument($core)
|
|
|
|
{
|
|
|
|
global $_ctx;
|
|
|
|
$s = $core->blog->settings->kUtRL;
|
|
|
|
|
|
|
|
# Passive : all kutrl tag return long url
|
2021-11-06 15:43:02 +00:00
|
|
|
$_ctx->kutrl_passive = (bool) $s->kutrl_tpl_passive;
|
2021-08-27 21:13:43 +00:00
|
|
|
|
|
|
|
if (!$s->kutrl_active || !$s->kutrl_tpl_service) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (null === ($kut = kutrl::quickPlace('tpl'))) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$_ctx->kutrl = $kut;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function publicHeadContent($core)
|
|
|
|
{
|
|
|
|
$css = $core->blog->settings->kUtRL->kutrl_srv_local_css;
|
|
|
|
if ($css) {
|
2021-11-06 15:43:02 +00:00
|
|
|
echo
|
2021-08-27 21:13:43 +00:00
|
|
|
"\n<!-- CSS for kUtRL --> \n" .
|
|
|
|
"<style type=\"text/css\"> \n" .
|
|
|
|
html::escapeHTML($css) . "\n" .
|
|
|
|
"</style>\n";
|
|
|
|
}
|
|
|
|
}
|
2021-08-25 23:06:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class tplKutrl
|
|
|
|
{
|
2021-08-27 21:13:43 +00:00
|
|
|
public static function pageURL($attr)
|
|
|
|
{
|
|
|
|
$f = $GLOBALS['core']->tpl->getFilters($attr);
|
2021-11-06 15:43:02 +00:00
|
|
|
|
2021-08-27 21:13:43 +00:00
|
|
|
return '<?php echo ' . sprintf($f, '$core->blog->url.$core->url->getBase("kutrl")') . '; ?>';
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function pageIf($attr, $content)
|
|
|
|
{
|
|
|
|
$operator = isset($attr['operator']) ? self::getOperator($attr['operator']) : '&&';
|
2021-09-02 12:32:26 +00:00
|
|
|
|
2021-08-27 21:13:43 +00:00
|
|
|
if (isset($attr['is_active'])) {
|
2021-11-06 15:43:02 +00:00
|
|
|
$sign = (bool) $attr['is_active'] ? '' : '!';
|
2021-08-27 21:13:43 +00:00
|
|
|
$if[] = $sign . '$core->blog->settings->kUtRL->kutrl_srv_local_public';
|
|
|
|
}
|
|
|
|
if (empty($if)) {
|
|
|
|
return $content;
|
|
|
|
}
|
|
|
|
|
2021-11-06 15:43:02 +00:00
|
|
|
return
|
|
|
|
'<?php if(' . implode(' ' . $operator . ' ', $if) . ") : ?>\n" .
|
2021-08-27 21:13:43 +00:00
|
|
|
$content .
|
|
|
|
"<?php endif; unset(\$s);?>\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function pageMsgIf($attr, $content)
|
|
|
|
{
|
|
|
|
$operator = isset($attr['operator']) ? self::getOperator($attr['operator']) : '&&';
|
|
|
|
|
|
|
|
if (isset($attr['has_message'])) {
|
2021-11-06 15:43:02 +00:00
|
|
|
$sign = (bool) $attr['has_message'] ? '!' : '=';
|
2021-08-27 21:13:43 +00:00
|
|
|
$if[] = '"" ' . $sign . '= $_ctx->kutrl_msg';
|
|
|
|
}
|
|
|
|
if (empty($if)) {
|
|
|
|
return $content;
|
|
|
|
}
|
|
|
|
|
2021-11-06 15:43:02 +00:00
|
|
|
return
|
|
|
|
'<?php if(' . implode(' ' . $operator . ' ', $if) . ") : ?>\n" .
|
2021-08-27 21:13:43 +00:00
|
|
|
$content .
|
|
|
|
"<?php endif; ?>\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function pageMsg($attr)
|
|
|
|
{
|
|
|
|
return '<?php echo $_ctx->kutrl_msg; ?>';
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function humanField($attr)
|
|
|
|
{
|
2021-08-28 19:48:42 +00:00
|
|
|
return "<?php echo sprintf(__('Confirm by writing \"%s\" in next field:'),\$_ctx->kutrl_hmf); ?>";
|
2021-08-27 21:13:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function humanFieldProtect($attr)
|
|
|
|
{
|
2021-11-06 15:43:02 +00:00
|
|
|
return
|
|
|
|
'<input type="hidden" name="hmfp" id="hmfp" value="<?php echo $_ctx->kutrl_hmfp; ?>" />' .
|
|
|
|
'<?php echo $core->formNonce(); ?>';
|
2021-08-27 21:13:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function AttachmentKutrlIf($attr, $content)
|
|
|
|
{
|
|
|
|
return self::genericKutrlIf('$attach_f->file_url', $attr, $content);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function AttachmentKutrl($attr)
|
|
|
|
{
|
|
|
|
return self::genericKutrl('$attach_f->file_url', $attr);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function MediaKutrlIf($attr, $content)
|
|
|
|
{
|
|
|
|
return self::genericKutrlIf('$_ctx->file_url', $attr, $content);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function MediaKutrl($attr)
|
|
|
|
{
|
|
|
|
return self::genericKutrl('$_ctx->file_url', $attr);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function EntryAuthorKutrlIf($attr, $content)
|
|
|
|
{
|
|
|
|
return self::genericKutrlIf('$_ctx->posts->user_url', $attr, $content);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function EntryAuthorKutrl($attr)
|
|
|
|
{
|
|
|
|
return self::genericKutrl('$_ctx->posts->user_url', $attr);
|
|
|
|
}
|
|
|
|
|
2021-11-06 15:43:02 +00:00
|
|
|
public static function EntryKutrlIf($attr, $content)
|
2021-08-27 21:13:43 +00:00
|
|
|
{
|
|
|
|
return self::genericKutrlIf('$_ctx->posts->getURL()', $attr, $content);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function EntryKutrl($attr)
|
|
|
|
{
|
|
|
|
return self::genericKutrl('$_ctx->posts->getURL()', $attr);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function CommentAuthorKutrlIf($attr, $content)
|
|
|
|
{
|
|
|
|
return self::genericKutrlIf('$_ctx->comments->getAuthorURL()', $attr, $content);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function CommentAuthorKutrl($attr)
|
|
|
|
{
|
|
|
|
return self::genericKutrl('$_ctx->comments->getAuthorURL()', $attr);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function CommentPostKutrlIf($attr, $content)
|
|
|
|
{
|
|
|
|
return self::genericKutrlIf('$_ctx->comments->getPostURL()', $attr, $content);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function CommentPostKutrl($attr)
|
|
|
|
{
|
|
|
|
return self::genericKutrl('$_ctx->comments->getPostURL()', $attr);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected static function genericKutrlIf($str, $attr, $content)
|
|
|
|
{
|
|
|
|
$operator = isset($attr['operator']) ? self::getOperator($attr['operator']) : '&&';
|
|
|
|
|
|
|
|
if (isset($attr['is_active'])) {
|
2021-11-06 15:43:02 +00:00
|
|
|
$sign = (bool) $attr['is_active'] ? '' : '!';
|
2021-08-27 21:13:43 +00:00
|
|
|
$if[] = $sign . '$_ctx->exists("kutrl")';
|
|
|
|
}
|
|
|
|
if (isset($attr['passive_mode'])) {
|
2021-11-06 15:43:02 +00:00
|
|
|
$sign = (bool) $attr['passive_mode'] ? '' : '!';
|
2021-08-27 21:13:43 +00:00
|
|
|
$if[] = $sign . '$_ctx->kutrl_passive';
|
|
|
|
}
|
|
|
|
if (isset($attr['has_kutrl'])) {
|
2021-11-06 15:43:02 +00:00
|
|
|
$sign = (bool) $attr['has_kutrl'] ? '!' : '=';
|
2021-08-27 21:13:43 +00:00
|
|
|
$if[] = '($_ctx->exists("kutrl") && false ' . $sign . '== $_ctx->kutrl->select(' . $str . ',null,null,"kutrl"))';
|
|
|
|
}
|
|
|
|
if (empty($if)) {
|
|
|
|
return $content;
|
|
|
|
}
|
2021-11-06 15:43:02 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
'<?php if(' . implode(' ' . $operator . ' ', $if) . ") : ?>\n" .
|
2021-08-27 21:13:43 +00:00
|
|
|
$content .
|
|
|
|
"<?php endif; ?>\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
protected static function genericKutrl($str, $attr)
|
|
|
|
{
|
|
|
|
$f = $GLOBALS['core']->tpl->getFilters($attr);
|
2021-11-06 15:43:02 +00:00
|
|
|
|
|
|
|
return
|
2021-08-27 21:13:43 +00:00
|
|
|
"<?php \n" .
|
|
|
|
# Preview
|
|
|
|
"if (\$_ctx->preview) { \n" .
|
2021-11-06 15:43:02 +00:00
|
|
|
' echo ' . sprintf($f, $str) . '; ' .
|
2021-08-27 21:13:43 +00:00
|
|
|
"} else { \n" .
|
|
|
|
# Disable
|
|
|
|
"if (!\$_ctx->exists('kutrl')) { \n" .
|
|
|
|
# Passive mode
|
2021-11-06 15:43:02 +00:00
|
|
|
' if ($_ctx->kutrl_passive) { ' .
|
|
|
|
' echo ' . sprintf($f, $str) . '; ' .
|
2021-08-27 21:13:43 +00:00
|
|
|
" } \n" .
|
|
|
|
"} else { \n" .
|
|
|
|
# Existing
|
2021-11-06 15:43:02 +00:00
|
|
|
' if (false !== ($kutrl_rs = $_ctx->kutrl->isKnowUrl(' . $str . '))) { ' .
|
|
|
|
' echo ' . sprintf($f, '$_ctx->kutrl->url_base.$kutrl_rs->hash') . '; ' .
|
2021-08-27 21:13:43 +00:00
|
|
|
" } \n" .
|
|
|
|
# New
|
2021-11-06 15:43:02 +00:00
|
|
|
' elseif (false !== ($kutrl_rs = $_ctx->kutrl->hash(' . $str . '))) { ' .
|
|
|
|
' echo ' . sprintf($f, '$_ctx->kutrl->url_base.$kutrl_rs->hash') . '; ' .
|
2021-08-27 21:13:43 +00:00
|
|
|
|
|
|
|
# ex: Send new url to messengers
|
2021-11-06 15:43:02 +00:00
|
|
|
' if (!empty($kutrl_rs)) { ' .
|
2021-08-27 21:13:43 +00:00
|
|
|
" \$core->callBehavior('publicAfterKutrlCreate',\$core,\$kutrl_rs,__('New public short URL')); " .
|
|
|
|
" } \n" .
|
2021-09-02 12:32:26 +00:00
|
|
|
|
2021-08-27 21:13:43 +00:00
|
|
|
" } \n" .
|
|
|
|
" unset(\$kutrl_rs); \n" .
|
|
|
|
"} \n" .
|
|
|
|
"} \n" .
|
|
|
|
"?>\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
protected static function getOperator($op)
|
|
|
|
{
|
|
|
|
switch (strtolower($op)) {
|
|
|
|
case 'or':
|
|
|
|
case '||':
|
|
|
|
return '||';
|
|
|
|
case 'and':
|
|
|
|
case '&&':
|
|
|
|
default:
|
|
|
|
return '&&';
|
|
|
|
}
|
|
|
|
}
|
2021-08-25 23:06:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class hmfKutrl
|
|
|
|
{
|
2021-08-27 21:13:43 +00:00
|
|
|
public static $chars = 'abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789';
|
|
|
|
|
2021-11-06 15:43:02 +00:00
|
|
|
public static function create($len = 6)
|
2021-08-27 21:13:43 +00:00
|
|
|
{
|
2021-11-06 15:43:02 +00:00
|
|
|
$res = '';
|
2021-08-27 21:13:43 +00:00
|
|
|
$chars = self::$chars;
|
2021-11-06 15:43:02 +00:00
|
|
|
for ($i = 0; $i < $len; $i++) {
|
|
|
|
$res .= $chars[rand(0, strlen($chars) - 1)];
|
2021-08-27 21:13:43 +00:00
|
|
|
}
|
2021-11-06 15:43:02 +00:00
|
|
|
|
2021-08-27 21:13:43 +00:00
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function protect($str)
|
|
|
|
{
|
2021-11-06 15:43:02 +00:00
|
|
|
$res = '';
|
2021-08-27 21:13:43 +00:00
|
|
|
$chars = self::$chars;
|
2021-11-06 15:43:02 +00:00
|
|
|
for ($i = 0; $i < strlen($str); $i++) {
|
|
|
|
$res .= $chars[rand(0, strlen($chars) - 1)] . $str[$i];
|
2021-08-27 21:13:43 +00:00
|
|
|
}
|
2021-11-06 15:43:02 +00:00
|
|
|
|
2021-08-27 21:13:43 +00:00
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function unprotect($str)
|
|
|
|
{
|
|
|
|
$res = '';
|
2021-11-06 15:43:02 +00:00
|
|
|
for ($i = 0; $i < strlen($str); $i++) {
|
2021-08-27 21:13:43 +00:00
|
|
|
$i++;
|
|
|
|
$res .= $str[$i];
|
|
|
|
}
|
2021-11-06 15:43:02 +00:00
|
|
|
|
2021-08-27 21:13:43 +00:00
|
|
|
return $res;
|
|
|
|
}
|
2021-11-06 15:43:02 +00:00
|
|
|
}
|