From 39a933e5a18fff1b4603ab431a640bfd843270be Mon Sep 17 00:00:00 2001 From: Jean-Christian Denis Date: Sun, 30 Apr 2023 23:44:37 +0200 Subject: [PATCH] add visible redirection option --- src/Alias.php | 7 +++++-- src/Install.php | 1 + src/Manage.php | 14 +++++++++++--- src/Prepend.php | 12 ++++++++++-- 4 files changed, 27 insertions(+), 7 deletions(-) diff --git a/src/Alias.php b/src/Alias.php index f7c90c6..98c254f 100644 --- a/src/Alias.php +++ b/src/Alias.php @@ -51,6 +51,7 @@ class Alias 'alias_url', 'alias_destination', 'alias_position', + 'alias_redirect' ]) ->where('blog_id = ' . $sql->quote((string) dcCore::app()->blog->id)) ->order('alias_position ASC') @@ -81,7 +82,7 @@ class Alias $this->deleteAliases(); foreach ($aliases as $k => $v) { if (!empty($v['alias_url']) && !empty($v['alias_destination'])) { - $this->createAlias($v['alias_url'], $v['alias_destination'], $k + 1); + $this->createAlias($v['alias_url'], $v['alias_destination'], $k + 1, !empty($v['alias_redirect'])); } } @@ -99,8 +100,9 @@ class Alias * @param string $url The URL * @param string $destination The destination * @param int $position The position + * @param bool $position Do redirection */ - public function createAlias(string $url, string $destination, int $position): void + public function createAlias(string $url, string $destination, int $position, bool $redirect): void { // nullsafe PHP < 8.0 if (is_null(dcCore::app()->blog)) { @@ -122,6 +124,7 @@ class Alias $cur->setField('alias_url', (string) $url); $cur->setField('alias_destination', (string) $destination); $cur->setField('alias_position', abs((int) $position)); + $cur->setField('alias_redirect', (int) $redirect); $cur->insert(); } diff --git a/src/Install.php b/src/Install.php index 4554525..b4d9115 100644 --- a/src/Install.php +++ b/src/Install.php @@ -41,6 +41,7 @@ class Install extends dcNsProcess ->alias_url('varchar', 255, false) ->alias_destination('varchar', 255, false) ->alias_position('smallint', 0, false, 1) + ->alias_redirect('smallint', 0, false, 0) ->primary('pk_alias', 'blog_id', 'alias_url') diff --git a/src/Manage.php b/src/Manage.php index e321379..454dbbf 100644 --- a/src/Manage.php +++ b/src/Manage.php @@ -18,6 +18,7 @@ use dcCore; use dcNsProcess; use dcPage; use Dotclear\Helper\Html\Form\{ + Checkbox, Div, Form, Hidden, @@ -80,7 +81,7 @@ class Manage extends dcNsProcess # New alias if (isset($_POST['alias_url'])) { try { - $alias->createAlias($_POST['alias_url'], $_POST['alias_destination'], count($aliases) + 1); + $alias->createAlias($_POST['alias_url'], $_POST['alias_destination'], count($aliases) + 1, !empty($_POST['alias_redirect'])); dcPage::addSuccessNotice(__('Alias successfully created.')); dcCore::app()->adminurl->redirect('admin.plugin.' . My::id()); } catch (Exception $e) { @@ -128,6 +129,10 @@ class Manage extends dcNsProcess (new Input('alias_destination'))->size(50)->maxlenght(255), ]), (new Note())->class('form-note')->text(sprintf(__('Do not put blog URL "%s" in fields.'), dcCore::app()->blog->url)), + (new Para())->items([ + (new Checkbox('alias_redirect', false))->value(1), + (new Label(__('Do visible redirection to destination'), Label::OUTSIDE_LABEL_AFTER))->for('alias_redirect')->class('classic'), + ]), (new Para())->items([ dcCore::app()->formNonce(false), (new Hidden('part', 'new')), @@ -160,18 +165,21 @@ class Manage extends dcNsProcess '' . __('Alias URL') . '' . '' . __('Alias destination') . '' . '' . __('Alias position') . '' . + '' . __('Redrection') . '' . '' . ''; foreach ($aliases as $k => $v) { echo '' . - '' . + '' . (new Input(['a[' . $k . '][alias_url]']))->size(50)->maxlenght(255)->value(Html::escapeHTML($v['alias_url']))->render() . '' . - '' . + '' . (new Input(['a[' . $k . '][alias_destination]']))->size(50)->maxlenght(255)->value(Html::escapeHTML($v['alias_destination']))->render() . '' . '' . (new Number(['a[' . $k . '][alias_position]']))->min(1)->max(count($aliases))->default((int) $v['alias_position'])->class('position')->title(sprintf(__('position of %s'), Html::escapeHTML($v['alias_url'])))->render() . '' . + '' . + (new Checkbox(['a[' . $k . '][alias_redirect]'], (bool) $v['alias_redirect']))->title(sprintf(__('visible redirection to %s'), Html::escapeHTML(dcCore::app()->blog->url . $v['alias_destination'])))->render() . '' . ''; } diff --git a/src/Prepend.php b/src/Prepend.php index 6d1381a..4fc3f7d 100644 --- a/src/Prepend.php +++ b/src/Prepend.php @@ -17,6 +17,7 @@ namespace Dotclear\Plugin\alias; use dcCore; use dcNsProcess; use dcUrlHandlers; +use Dotclear\Helper\Network\Http; class Prepend extends dcNsProcess { @@ -34,18 +35,21 @@ class Prepend extends dcNsProcess } dcCore::app()->addBehavior('urlHandlerGetArgsDocument', function (dcUrlHandlers $handler): void { - $found = false; + $found = $redir = false; $type = 'alias'; $part = $args = $_SERVER['URL_REQUEST_PART']; + foreach ((new Alias())->getAliases() as $v) { if (@preg_match('#^/.*/$#', $v['alias_url']) && @preg_match($v['alias_url'], $args)) { $part = preg_replace($v['alias_url'], $v['alias_destination'], $args); $found = true; + $redir = !empty($v['alias_redirect']); break; } elseif ($v['alias_url'] == $args) { $part = $v['alias_destination']; $found = true; + $redir = !empty($v['alias_redirect']); break; } @@ -55,7 +59,11 @@ class Prepend extends dcNsProcess return; } - dcCore::app()->url->unregister('alias'); + if ($redir) { + Http::redirect(dcCore::app()->blog->url . $part); + } + + $_SERVER['URL_REQUEST_PART'] = $part; dcCore::app()->url->getArgs($part, $type, $args); if (!$type) {