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-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 20:18:49 +00:00
|
|
|
|| !dcCore::app()->blog->settings->get(basename(dirname(__DIR__)))->get('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 20:18:49 +00:00
|
|
|
if (!dcCore::app()->blog->settings->get(basename(dirname(__DIR__)))->get('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
|
|
|
}
|