kUtRL/src/FrontendUrl.php

209 lines
7.0 KiB
PHP
Raw Normal View History

2023-08-21 14:55:06 +00:00
<?php
2023-10-19 18:32:57 +00:00
2023-08-21 14:55:06 +00:00
declare(strict_types=1);
namespace Dotclear\Plugin\kUtRL;
2023-10-19 18:32:57 +00:00
use Dotclear\App;
use Dotclear\Core\Frontend\Url;
2023-08-21 14:55:06 +00:00
use Dotclear\Helper\Html\Html;
use Dotclear\Helper\Network\Http;
2023-10-19 18:32:57 +00:00
/**
* @brief kUtRL frontend URL handler.
* @ingroup kUtRL
*
* @author Jean-Christian Denis (author)
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
class FrontendUrl extends Url
2023-08-21 14:55:06 +00:00
{
# Redirect !!! local !!! service only
public static function redirectUrl(?string $args): void
{
# Not active, go to default 404
if (!My::settings()->get('active')) {
self::p404();
return;
}
# Not a valid url, go to kutrl 404
if (!preg_match('#^(|(/(.*?)))$#', $args, $m)) {
self::kutrl404();
return;
}
2023-10-19 18:32:57 +00:00
$args = $m[3] ?? '';
App::frontend()->context()->kutrl_msg = '';
App::frontend()->context()->kutrl_hmf = FrontendUtils::create();
App::frontend()->context()->kutrl_hmfp = FrontendUtils::protect(App::frontend()->context()->kutrl_hmf);
2023-08-21 14:55:06 +00:00
$kut = new Service\ServiceLocal();
# Nothing on url
if ($m[1] == '/') {
2023-10-19 18:32:57 +00:00
App::frontend()->context()->kutrl_msg = 'No link given.';
2023-08-21 14:55:06 +00:00
}
# find suffix on redirect url
$suffix = '';
if (preg_match('@^([^?/#]+)(.*?)$@', $args, $more)) {
$args = $more[1];
$suffix = $more[2];
}
# No arg, go to kurtl page
if ($args == '') {
self::pageKutrl($kut);
return;
}
# Not find, go to kutrl 404
if (false === ($url = $kut->getUrl($args))) {
2023-10-19 18:32:57 +00:00
//App::frontend()->context()->kutrl_msg = 'Failed to find short link.';
2023-08-21 14:55:06 +00:00
//self::pageKutrl($kut);
self::kutrl404();
return;
}
# Removed (empty url), go to kutrl 404
if (!$url) {
self::kutrl404();
return;
}
2023-10-19 18:32:57 +00:00
App::blog()->triggerBlog();
2023-08-21 14:55:06 +00:00
Http::redirect($url . $suffix);
}
private static function pageKutrl(Service $kut): void
{
$s = My::settings();
# Not active, go to default 404
if (!$s->get('active')) {
self::p404();
return;
}
# Public page not active, go to kutrl 404
if (!$s->get('srv_local_public')) {
self::kutrl404();
return;
}
# Validation form
2023-10-19 18:32:57 +00:00
$url = !empty($_POST['longurl']) ? trim(App::con()->escapeStr((string) $_POST['longurl'])) : '';
2023-08-21 14:55:06 +00:00
if (!empty($url)) {
$hmf = !empty($_POST['hmf']) ? $_POST['hmf'] : '!';
$hmfu = !empty($_POST['hmfp']) ? FrontendUtils::unprotect($_POST['hmfp']) : '?';
$err = false;
if (!$err) {
if ($hmf != $hmfu) {
2023-10-19 18:32:57 +00:00
$err = true;
App::frontend()->context()->kutrl_msg = __('Failed to verify protected field.');
2023-08-21 14:55:06 +00:00
}
}
if (!$err) {
if (!$kut->testService()) {
2023-10-19 18:32:57 +00:00
$err = true;
App::frontend()->context()->kutrl_msg = __('Service is not well configured.');
2023-08-21 14:55:06 +00:00
}
}
if (!$err) {
if (!$kut->isValidUrl($url)) {
2023-10-19 18:32:57 +00:00
$err = true;
App::frontend()->context()->kutrl_msg = __('This string is not a valid URL.');
2023-08-21 14:55:06 +00:00
}
}
if (!$err) {
if (!$kut->isLongerUrl($url)) {
2023-10-19 18:32:57 +00:00
$err = true;
App::frontend()->context()->kutrl_msg = __('This link is too short.');
2023-08-21 14:55:06 +00:00
}
}
if (!$err) {
if (!$kut->isProtocolUrl($url)) {
2023-10-19 18:32:57 +00:00
$err = true;
App::frontend()->context()->kutrl_msg = __('This type of link is not allowed.');
2023-08-21 14:55:06 +00:00
}
}
if (!$err) {
if (!$kut->allow_external_url && !$kut->isBlogUrl($url)) {
2023-10-19 18:32:57 +00:00
$err = true;
App::frontend()->context()->kutrl_msg = __('Short links are limited to this blog URL.');
2023-08-21 14:55:06 +00:00
}
}
if (!$err) {
if ($kut->isServiceUrl($url)) {
2023-10-19 18:32:57 +00:00
$err = true;
App::frontend()->context()->kutrl_msg = __('This link is already a short link.');
2023-08-21 14:55:06 +00:00
}
}
if (!$err) {
if (false !== ($rs = $kut->isKnowUrl($url))) {
$err = true;
$url = $rs->url;
$new_url = $kut->url_base . $rs->hash;
2023-10-19 18:32:57 +00:00
App::frontend()->context()->kutrl_msg = sprintf(
2023-08-21 14:55:06 +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))) {
2023-10-19 18:32:57 +00:00
$err = true;
App::frontend()->context()->kutrl_msg = __('Failed to create short link.');
2023-08-21 14:55:06 +00:00
} else {
$url = $rs->url;
$new_url = $kut->url_base . $rs->hash;
2023-10-19 18:32:57 +00:00
App::frontend()->context()->kutrl_msg = sprintf(
2023-08-21 14:55:06 +00:00
__('Short link for %s is %s'),
Html::escapeHTML($url),
'<a href="' . $new_url . '">' . $new_url . '</a>'
);
2023-10-19 18:32:57 +00:00
App::blog()->triggerBlog();
2023-08-21 14:55:06 +00:00
# ex: Send new url to messengers
if (!empty($rs)) {
2023-10-19 18:32:57 +00:00
App::behavior()->callBehavior('publicAfterKutrlCreate', $rs, __('New public short URL'));
2023-08-21 14:55:06 +00:00
}
}
}
}
2023-10-19 18:32:57 +00:00
App::frontend()->template()->setPath(App::frontend()->template()->getPath(), My::path() . '/default-templates');
2023-08-21 14:55:06 +00:00
self::serveDocument('kutrl.html');
}
protected static function kutrl404(): void
{
if (!My::settigns()->get('srv_local_404_active')) {
self::p404();
return;
}
2023-10-19 18:32:57 +00:00
App::frontend()->template()->setPath(App::frontend()->template()->getPath(), My::path() . '/default-templates');
2023-08-21 14:55:06 +00:00
header('Content-Type: text/html; charset=UTF-8');
Http::head(404, 'Not Found');
2023-10-19 18:32:57 +00:00
App::url()->type = '404';
App::frontend()->context()->current_tpl = 'kutrl404.html';
App::frontend()->context()->content_type = 'text/html';
2023-08-21 14:55:06 +00:00
2023-10-19 18:32:57 +00:00
echo App::frontend()->template()->getData(App::frontend()->context()->current_tpl);
2023-08-21 14:55:06 +00:00
# --BEHAVIOR-- publicAfterDocumentV2
2023-10-19 18:32:57 +00:00
App::behavior()->callBehavior('publicAfterDocumentV2');
2023-08-21 14:55:06 +00:00
exit;
}
}