kUtRL/_public.php

579 lines
19 KiB
PHP
Raw Normal View History

2021-08-25 23:06:22 +00:00
<?php
/**
* @brief kUtRL, a plugin for Dotclear 2
2021-11-06 15:43:02 +00:00
*
* @package Dotclear
* @subpackage Plugin
2021-11-06 15:43:02 +00:00
*
* @author Jean-Christian Denis and contributors
2021-11-06 15:43:02 +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;
}
2022-11-20 16:15:36 +00:00
require_once __DIR__ . '/_widgets.php';
dcCore::app()->tpl->setPath(dcCore::app()->tpl->getPath(), __DIR__ . '/default-templates');
dcCore::app()->addBehavior('publicBeforeDocumentV2', ['pubKutrl', 'publicBeforeDocument']);
dcCore::app()->addBehavior('publicHeadContent', ['pubKutrl', 'publicHeadContent']);
dcCore::app()->addBehavior('publicBeforeContentFilterV2', ['pubKutrl', 'publicBeforeContentFilter']);
dcCore::app()->addBehavior('templateBeforeValueV2', ['pubKutrl', 'templateBeforeValue']);
dcCore::app()->addBehavior('templateAfterValueV2', ['pubKutrl', 'templateAfterValue']);
dcCore::app()->tpl->addBlock('kutrlPageIf', ['tplKutrl', 'pageIf']);
dcCore::app()->tpl->addBlock('kutrlMsgIf', ['tplKutrl', 'pageMsgIf']);
dcCore::app()->tpl->addValue('kutrlPageURL', ['tplKutrl', 'pageURL']);
dcCore::app()->tpl->addValue('kutrlMsg', ['tplKutrl', 'pageMsg']);
dcCore::app()->tpl->addValue('kutrlHumanField', ['tplKutrl', 'humanField']);
dcCore::app()->tpl->addValue('kutrlHumanFieldProtect', ['tplKutrl', 'humanFieldProtect']);
dcCore::app()->tpl->addBlock('AttachmentKutrlIf', ['tplKutrl', 'AttachmentKutrlIf']);
dcCore::app()->tpl->addValue('AttachmentKutrl', ['tplKutrl', 'AttachmentKutrl']);
dcCore::app()->tpl->addBlock('MediaKutrlIf', ['tplKutrl', 'MediaKutrlIf']);
dcCore::app()->tpl->addValue('MediaKutrl', ['tplKutrl', 'MediaKutrl']);
dcCore::app()->tpl->addBlock('EntryAuthorKutrlIf', ['tplKutrl', 'EntryAuthorKutrlIf']);
dcCore::app()->tpl->addValue('EntryAuthorKutrl', ['tplKutrl', 'EntryAuthorKutrl']);
dcCore::app()->tpl->addBlock('EntryKutrlIf', ['tplKutrl', 'EntryKutrlIf']);
dcCore::app()->tpl->addValue('EntryKutrl', ['tplKutrl', 'EntryKutrl']);
dcCore::app()->tpl->addBlock('CommentAuthorKutrlIf', ['tplKutrl', 'CommentAuthorKutrlIf']);
dcCore::app()->tpl->addValue('CommentAuthorKutrl', ['tplKutrl', 'CommentAuthorKutrl']);
dcCore::app()->tpl->addBlock('CommentPostKutrlIf', ['tplKutrl', 'CommentPostKutrlIf']);
dcCore::app()->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)
{
2022-11-20 16:15:36 +00:00
$s = dcCore::app()->blog->settings->kUtRL;
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;
}
# 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;
}
2022-11-20 16:15:36 +00:00
$args = $m[3] ?? '';
dcCore::app()->ctx->kutrl_msg = '';
dcCore::app()->ctx->kutrl_hmf = hmfKutrl::create();
dcCore::app()->ctx->kutrl_hmfp = hmfKutrl::protect(dcCore::app()->ctx->kutrl_hmf);
2021-08-27 21:13:43 +00:00
2022-11-20 16:15:36 +00:00
$kut = new localKutrlService();
2021-08-27 21:13:43 +00:00
# Nothing on url
if ($m[1] == '/') {
2022-11-20 16:15:36 +00:00
dcCore::app()->ctx->kutrl_msg = 'No link given.';
2021-08-27 21:13:43 +00:00
}
# 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;
}
2022-11-20 16:15:36 +00:00
dcCore::app()->blog->triggerBlog();
2021-08-27 21:13:43 +00:00
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)
{
2022-11-20 16:15:36 +00:00
$s = dcCore::app()->blog->settings->kUtRL;
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
2022-11-20 16:15:36 +00:00
$url = !empty($_POST['longurl']) ? trim(dcCore::app()->con->escape($_POST['longurl'])) : '';
2021-08-27 21:13:43 +00:00
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) {
2022-11-20 16:15:36 +00:00
$err = true;
dcCore::app()->ctx->kutrl_msg = __('Failed to verify protected field.');
2021-08-27 21:13:43 +00:00
}
}
if (!$err) {
if (!$kut->testService()) {
2022-11-20 16:15:36 +00:00
$err = true;
dcCore::app()->ctx->kutrl_msg = __('Service is not well configured.');
2021-08-27 21:13:43 +00:00
}
}
if (!$err) {
if (!$kut->isValidUrl($url)) {
2022-11-20 16:15:36 +00:00
$err = true;
dcCore::app()->ctx->kutrl_msg = __('This string is not a valid URL.');
2021-08-27 21:13:43 +00:00
}
}
if (!$err) {
if (!$kut->isLongerUrl($url)) {
2022-11-20 16:15:36 +00:00
$err = true;
dcCore::app()->ctx->kutrl_msg = __('This link is too short.');
2021-08-27 21:13:43 +00:00
}
}
if (!$err) {
if (!$kut->isProtocolUrl($url)) {
2022-11-20 16:15:36 +00:00
$err = true;
dcCore::app()->ctx->kutrl_msg = __('This type of link is not allowed.');
2021-08-27 21:13:43 +00:00
}
}
if (!$err) {
if (!$kut->allow_external_url && !$kut->isBlogUrl($url)) {
2022-11-20 16:15:36 +00:00
$err = true;
dcCore::app()->ctx->kutrl_msg = __('Short links are limited to this blog URL.');
2021-08-27 21:13:43 +00:00
}
}
if (!$err) {
if ($kut->isServiceUrl($url)) {
2022-11-20 16:15:36 +00:00
$err = true;
dcCore::app()->ctx->kutrl_msg = __('This link is already a short link.');
2021-08-27 21:13:43 +00:00
}
}
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;
2022-11-20 16:15:36 +00:00
dcCore::app()->ctx->kutrl_msg = sprintf(
2021-08-27 21:13:43 +00:00
__('Short link for %s is %s'),
html::escapeHTML($url),
'<a href="' . $new_url . '">' . $new_url . '</a>'
);
}
}
if (!$err) {
if (false === ($rs = $kut->hash($url))) {
2022-11-20 16:15:36 +00:00
$err = true;
dcCore::app()->ctx->kutrl_msg = __('Failed to create short link.');
2021-08-27 21:13:43 +00:00
} 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;
2022-11-20 16:15:36 +00:00
dcCore::app()->ctx->kutrl_msg = sprintf(
2021-08-27 21:13:43 +00:00
__('Short link for %s is %s'),
html::escapeHTML($url),
'<a href="' . $new_url . '">' . $new_url . '</a>'
);
2022-11-20 16:15:36 +00:00
dcCore::app()->blog->triggerBlog();
2021-08-27 21:13:43 +00:00
# ex: Send new url to messengers
if (!empty($rs)) {
2022-11-20 16:15:36 +00:00
dcCore::app()->callBehavior('publicAfterKutrlCreate', $rs, __('New public short URL'));
2021-08-27 21:13:43 +00:00
}
}
}
}
2022-11-20 16:15:36 +00:00
dcCore::app()->tpl->setPath(dcCore::app()->tpl->getPath(), __DIR__ . '/default-templates');
2021-08-27 21:13:43 +00:00
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()
{
2022-11-20 16:15:36 +00:00
if (!dcCore::app()->blog->settings->kUtRL->kutrl_srv_local_404_active) {
2021-08-27 21:13:43 +00:00
self::p404();
2021-10-26 21:03:38 +00:00
2021-08-27 21:13:43 +00:00
return null;
}
2022-11-20 16:15:36 +00:00
dcCore::app()->tpl->setPath(dcCore::app()->tpl->getPath(), __DIR__ . '/default-templates');
2021-08-27 21:13:43 +00:00
header('Content-Type: text/html; charset=UTF-8');
http::head(404, 'Not Found');
2022-11-20 16:15:36 +00:00
dcCore::app()->url->type = '404';
dcCore::app()->ctx->current_tpl = 'kutrl404.html';
dcCore::app()->ctx->content_type = 'text/html';
2021-08-27 21:13:43 +00:00
2022-11-20 16:15:36 +00:00
echo dcCore::app()->tpl->getData(dcCore::app()->ctx->current_tpl);
2021-08-27 21:13:43 +00:00
# --BEHAVIOR-- publicAfterDocument
2022-11-20 16:15:36 +00:00
dcCore::app()->callBehavior('publicAfterDocumentV2');
2021-08-27 21:13:43 +00:00
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',
2022-11-20 16:15:36 +00:00
'CommentPostURL',
2021-08-27 21:13:43 +00:00
];
# Disable URL shoretning on filtered tag
2022-11-20 16:15:36 +00:00
public static function templateBeforeValue($tag, $attr)
2021-08-27 21:13:43 +00:00
{
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
2022-11-20 16:15:36 +00:00
public static function templateAfterValue($tag, $attr)
2021-08-27 21:13:43 +00:00
{
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
2022-11-20 16:15:36 +00:00
public static function publicBeforeContentFilter($tag, $args)
2021-08-27 21:13:43 +00:00
{
# 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
2022-11-20 16:15:36 +00:00
if (!dcCore::app()->blog->settings->kUtRL->kutrl_active
|| !dcCore::app()->blog->settings->kUtRL->kutrl_tpl_active
2021-10-26 21:03:38 +00:00
) {
2021-08-27 21:13:43 +00:00
return null;
}
# Oups
2022-11-20 16:15:36 +00:00
if (!dcCore::app()->ctx->exists('kutrl')) {
2021-08-27 21:13:43 +00:00
return null;
}
# Existing
2022-11-20 16:15:36 +00:00
if (false !== ($kutrl_rs = dcCore::app()->ctx->kutrl->isKnowUrl($args[0]))) {
$args[0] = dcCore::app()->ctx->kutrl->url_base . $kutrl_rs->hash;
2021-08-27 21:13:43 +00:00
# New
2022-11-20 16:15:36 +00:00
} elseif (false !== ($kutrl_rs = dcCore::app()->ctx->kutrl->hash($args[0]))) {
$args[0] = dcCore::app()->ctx->kutrl->url_base . $kutrl_rs->hash;
2021-08-27 21:13:43 +00:00
# ex: Send new url to messengers
if (!empty($kutrl_rs)) {
2022-11-20 16:15:36 +00:00
dcCore::app()->callBehavior('publicAfterKutrlCreate', $kutrl_rs, __('New public short URL'));
2021-08-27 21:13:43 +00:00
}
}
}
}
2022-11-20 16:15:36 +00:00
public static function publicBeforeDocument()
2021-08-27 21:13:43 +00:00
{
2022-11-20 16:15:36 +00:00
$s = dcCore::app()->blog->settings->kUtRL;
2021-08-27 21:13:43 +00:00
# Passive : all kutrl tag return long url
2022-11-20 16:15:36 +00:00
dcCore::app()->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;
}
2022-11-20 16:15:36 +00:00
if (null === ($kut = kUtRL::quickPlace('tpl'))) {
2021-08-27 21:13:43 +00:00
return null;
}
2022-11-20 16:15:36 +00:00
dcCore::app()->ctx->kutrl = $kut;
2021-08-27 21:13:43 +00:00
}
public static function publicHeadContent($core)
{
2022-11-20 16:15:36 +00:00
$css = dcCore::app()->blog->settings->kUtRL->kutrl_srv_local_css;
2021-08-27 21:13:43 +00:00
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)
{
2022-11-20 16:15:36 +00:00
$f = dcCore::app()->tpl->getFilters($attr);
2021-11-06 15:43:02 +00:00
2022-11-20 16:15:36 +00:00
return '<?php echo ' . sprintf($f, 'dcCore::app()->blog->url.dcCore::app()->url->getBase("kutrl")') . '; ?>';
2021-08-27 21:13:43 +00:00
}
public static function pageIf($attr, $content)
{
$operator = isset($attr['operator']) ? self::getOperator($attr['operator']) : '&&';
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'] ? '' : '!';
2022-11-20 16:15:36 +00:00
$if[] = $sign . 'dcCore::app()->blog->settings->kUtRL->kutrl_srv_local_public';
2021-08-27 21:13:43 +00:00
}
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'] ? '!' : '=';
2022-11-20 16:15:36 +00:00
$if[] = '"" ' . $sign . '= dcCore::app()->ctx->kutrl_msg';
2021-08-27 21:13:43 +00:00
}
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)
{
2022-11-20 16:15:36 +00:00
return '<?php echo dcCore::app()->ctx->kutrl_msg; ?>';
2021-08-27 21:13:43 +00:00
}
public static function humanField($attr)
{
2022-11-20 16:15:36 +00:00
return "<?php echo sprintf(__('Confirm by writing \"%s\" in next field:'),dcCore::app()->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
2022-11-20 16:15:36 +00:00
'<input type="hidden" name="hmfp" id="hmfp" value="<?php echo dcCore::app()->ctx->kutrl_hmfp; ?>" />' .
'<?php echo dcCore::app()->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)
{
2022-11-20 16:15:36 +00:00
return self::genericKutrlIf('dcCore::app()->ctx->file_url', $attr, $content);
2021-08-27 21:13:43 +00:00
}
public static function MediaKutrl($attr)
{
2022-11-20 16:15:36 +00:00
return self::genericKutrl('dcCore::app()->ctx->file_url', $attr);
2021-08-27 21:13:43 +00:00
}
public static function EntryAuthorKutrlIf($attr, $content)
{
2022-11-20 16:15:36 +00:00
return self::genericKutrlIf('dcCore::app()->ctx->posts->user_url', $attr, $content);
2021-08-27 21:13:43 +00:00
}
public static function EntryAuthorKutrl($attr)
{
2022-11-20 16:15:36 +00:00
return self::genericKutrl('dcCore::app()->ctx->posts->user_url', $attr);
2021-08-27 21:13:43 +00:00
}
2021-11-06 15:43:02 +00:00
public static function EntryKutrlIf($attr, $content)
2021-08-27 21:13:43 +00:00
{
2022-11-20 16:15:36 +00:00
return self::genericKutrlIf('dcCore::app()->ctx->posts->getURL()', $attr, $content);
2021-08-27 21:13:43 +00:00
}
public static function EntryKutrl($attr)
{
2022-11-20 16:15:36 +00:00
return self::genericKutrl('dcCore::app()->ctx->posts->getURL()', $attr);
2021-08-27 21:13:43 +00:00
}
public static function CommentAuthorKutrlIf($attr, $content)
{
2022-11-20 16:15:36 +00:00
return self::genericKutrlIf('dcCore::app()->ctx->comments->getAuthorURL()', $attr, $content);
2021-08-27 21:13:43 +00:00
}
public static function CommentAuthorKutrl($attr)
{
2022-11-20 16:15:36 +00:00
return self::genericKutrl('dcCore::app()->ctx->comments->getAuthorURL()', $attr);
2021-08-27 21:13:43 +00:00
}
public static function CommentPostKutrlIf($attr, $content)
{
2022-11-20 16:15:36 +00:00
return self::genericKutrlIf('dcCore::app()->ctx->comments->getPostURL()', $attr, $content);
2021-08-27 21:13:43 +00:00
}
public static function CommentPostKutrl($attr)
{
2022-11-20 16:15:36 +00:00
return self::genericKutrl('dcCore::app()->ctx->comments->getPostURL()', $attr);
2021-08-27 21:13:43 +00:00
}
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'] ? '' : '!';
2022-11-20 16:15:36 +00:00
$if[] = $sign . 'dcCore::app()->ctx->exists("kutrl")';
2021-08-27 21:13:43 +00:00
}
if (isset($attr['passive_mode'])) {
2021-11-06 15:43:02 +00:00
$sign = (bool) $attr['passive_mode'] ? '' : '!';
2022-11-20 16:15:36 +00:00
$if[] = $sign . 'dcCore::app()->ctx->kutrl_passive';
2021-08-27 21:13:43 +00:00
}
if (isset($attr['has_kutrl'])) {
2021-11-06 15:43:02 +00:00
$sign = (bool) $attr['has_kutrl'] ? '!' : '=';
2022-11-20 16:15:36 +00:00
$if[] = '(dcCore::app()->ctx->exists("kutrl") && false ' . $sign . '== dcCore::app()->ctx->kutrl->select(' . $str . ',null,null,"kutrl"))';
2021-08-27 21:13:43 +00:00
}
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)
{
2022-11-20 16:15:36 +00:00
$f = dcCore::app()->tpl->getFilters($attr);
2021-11-06 15:43:02 +00:00
return
2021-08-27 21:13:43 +00:00
"<?php \n" .
# Preview
2022-11-20 16:15:36 +00:00
"if (dcCore::app()->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
2022-11-20 16:15:36 +00:00
"if (!dcCore::app()->ctx->exists('kutrl')) { \n" .
2021-08-27 21:13:43 +00:00
# Passive mode
2022-11-20 16:15:36 +00:00
' if (dcCore::app()->ctx->kutrl_passive) { ' .
2021-11-06 15:43:02 +00:00
' echo ' . sprintf($f, $str) . '; ' .
2021-08-27 21:13:43 +00:00
" } \n" .
"} else { \n" .
# Existing
2022-11-20 16:15:36 +00:00
' if (false !== ($kutrl_rs = dcCore::app()->ctx->kutrl->isKnowUrl(' . $str . '))) { ' .
' echo ' . sprintf($f, 'dcCore::app()->ctx->kutrl->url_base.$kutrl_rs->hash') . '; ' .
2021-08-27 21:13:43 +00:00
" } \n" .
# New
2022-11-20 16:15:36 +00:00
' elseif (false !== ($kutrl_rs = dcCore::app()->ctx->kutrl->hash(' . $str . '))) { ' .
' echo ' . sprintf($f, 'dcCore::app()->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)) { ' .
2022-11-20 16:15:36 +00:00
" dcCore::app()->callBehavior('publicAfterKutrlCreate',\$kutrl_rs,__('New public short URL')); " .
2021-08-27 21:13:43 +00:00
" } \n" .
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
}