kUtRL/inc/lib.wiki.kutrl.php

75 lines
2.1 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-25 23:06:22 +00:00
# This file contents class to shorten url pass through wiki
2021-08-27 22:06:46 +00:00
if (!defined('DC_RC_PATH')) {
return null;
}
2021-08-25 23:06:22 +00:00
class kutrlWiki
{
2021-08-27 22:06:46 +00:00
public static function coreInitWiki($wiki2xhtml)
{
# Do nothing on comment preview and post preview
2021-11-06 15:43:02 +00:00
if (!empty($_POST['preview'])
2022-11-20 16:15:36 +00:00
|| isset(dcCore::app()->ctx) && dcCore::app()->ctx->preview
2022-12-22 14:37:09 +00:00
|| !dcCore::app()->blog->settings->get(basename(dirname(__DIR__)))->get('kutrl_active')
2021-10-26 21:03:38 +00:00
) {
2021-08-27 22:06:46 +00:00
return null;
}
2022-11-20 16:15:36 +00:00
if (null === ($kut = kUtRL::quickPlace('wiki'))) {
2021-08-27 22:06:46 +00:00
return null;
}
2021-11-06 15:43:02 +00:00
foreach ($kut->allow_protocols as $protocol) {
2021-08-27 22:06:46 +00:00
$wiki2xhtml->registerFunction(
'url:' . $protocol,
['kutrlWiki', 'transform']
);
}
}
2021-08-25 23:06:22 +00:00
2021-08-27 22:06:46 +00:00
public static function transform($url, $content)
{
2022-12-22 14:37:09 +00:00
if (!dcCore::app()->blog->settings->get(basename(dirname(__DIR__)))->get('kutrl_active')) {
2021-08-27 22:06:46 +00:00
return null;
}
2022-11-20 16:15:36 +00:00
if (null === ($kut = kUtRL::quickPlace('wiki'))) {
2021-08-27 22:06:46 +00:00
return [];
}
# Test if long url exists
$is_new = false;
2021-11-06 15:43:02 +00:00
$rs = $kut->isKnowUrl($url);
2021-08-27 22:06:46 +00:00
if (!$rs) {
$is_new = true;
2021-11-06 15:43:02 +00:00
$rs = $kut->hash($url);
2021-08-27 22:06:46 +00:00
}
if (!$rs) {
return [];
2021-11-06 15:43:02 +00:00
}
$res = [];
$testurl = strlen($rs->url) > 35 ? substr($rs->url, 0, 35) . '...' : $rs->url;
$res['url'] = $kut->url_base . $rs->hash;
$res['title'] = sprintf(__('%s (Shorten with %s)'), $rs->url, __($kut->name));
if ($testurl == $content) {
$res['content'] = $res['url'];
}
2021-08-27 22:06:46 +00:00
2021-11-06 15:43:02 +00:00
# ex: Send new url to messengers
if (!empty($rs)) {
2022-11-20 16:15:36 +00:00
dcCore::app()->callBehavior('wikiAfterKutrlCreate', $rs, __('New short URL'));
2021-08-27 22:06:46 +00:00
}
2021-11-06 15:43:02 +00:00
return $res;
2021-08-27 22:06:46 +00:00
}
2021-11-06 15:43:02 +00:00
}