From 861acb4f0904e1f9ebc36adec6c76b6300042940 Mon Sep 17 00:00:00 2001 From: Nicolas Barbey <223106@supinfo.com> Date: Wed, 17 Feb 2021 12:04:06 +0100 Subject: [PATCH 1/2] Fix preg_match --- Config/module.xml | 2 +- Model/RewriteurlRule.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Config/module.xml b/Config/module.xml index 98c8094..79c4a9e 100755 --- a/Config/module.xml +++ b/Config/module.xml @@ -7,7 +7,7 @@ Gérer la réécriture d'url - 1.5.9 + 1.5.10 Vincent Lopes, Gilles Bourgeat, Tom Pradat vlopes@openstudio.fr, gbourgeat@openstudio.fr, tpradat@openstudio.fr diff --git a/Model/RewriteurlRule.php b/Model/RewriteurlRule.php index bf51583..61f6932 100644 --- a/Model/RewriteurlRule.php +++ b/Model/RewriteurlRule.php @@ -32,7 +32,7 @@ public function isMatching($url, $getParamArray) { if ($this->getRuleType() == self::TYPE_REGEX) { if (!empty($this->getValue())) { - return preg_match("/" . $this->getValue() . "/", $url) === 1; + return preg_match("/" . str_replace('/', '.', $this->getValue()) . "/", $url) === 1; } } elseif ($this->getRuleType() == self::TYPE_GET_PARAMS) { if ($this->getRewriteUrlParamCollection()->count() > 0) { From 5b32e3adf1bb137c5b941ed240f818c5f8fcc387 Mon Sep 17 00:00:00 2001 From: Nicolas Barbey <223106@supinfo.com> Date: Wed, 17 Feb 2021 17:02:57 +0100 Subject: [PATCH 2/2] Add url rule type --- Controller/Admin/ModuleConfigController.php | 12 ++++--- Service/RewritingRouterFirst.php | 9 +++++ .../RewriteUrl/module-configuration-js.html | 34 +++++++++++++++---- .../RewriteUrl/module-configuration.html | 5 +++ .../default/RewriteUrl/tab-value-render.html | 4 +++ 5 files changed, 52 insertions(+), 12 deletions(-) diff --git a/Controller/Admin/ModuleConfigController.php b/Controller/Admin/ModuleConfigController.php index 4e9fe0a..5da459a 100644 --- a/Controller/Admin/ModuleConfigController.php +++ b/Controller/Admin/ModuleConfigController.php @@ -82,6 +82,7 @@ public function getDatatableRules() foreach ($searchArray as $row) { $id = $row['Id']; $isRegexSelected = $row['RuleType'] === 'regex' ? 'selected' : ''; + $isUrlSelected = $row['RuleType'] === 'url' ? 'selected' : ''; $isParamsSelected = $row['RuleType'] === 'params' ? 'selected' : ''; $isOnly404Checked = $row['Only404'] ? 'checked' : ''; $rewriteUrlRuleParams = RewriteurlRuleQuery::create()->findPk($row['Id'])->getRewriteUrlParamCollection(); @@ -89,6 +90,7 @@ public function getDatatableRules() 'Id' => $row['Id'], 'RuleType' => ' ' . Translator::getInstance()->trans("Regex", [], RewriteUrl::MODULE_DOMAIN) . ' + ' . Translator::getInstance()->trans("URL", [], RewriteUrl::MODULE_DOMAIN) . ' ' . Translator::getInstance()->trans("Get Params", [], RewriteUrl::MODULE_DOMAIN) . ' ', 'Value' => $this->renderRaw( @@ -227,13 +229,13 @@ public function moveRulePositionAction() protected function fillRuleObjectFields(RewriteurlRule $rule, $request) { $ruleType = $request->get("ruleType", null); - if ($ruleType !== "regex" && $ruleType !== "params") { + if ($ruleType !== "regex" && $ruleType !== "url" && $ruleType !== "params") { throw new \Exception(Translator::getInstance()->trans("Unknown rule type.", [], RewriteUrl::MODULE_DOMAIN)); } - $regexValue = $request->get("value", null); - if ($ruleType == "regex" && empty($regexValue)) { - throw new \Exception(Translator::getInstance()->trans("Regex value cannot be empty.", [], RewriteUrl::MODULE_DOMAIN)); + $value = $request->get("value", null); + if (empty($value) && in_array($ruleType, ["regex", "url"]) ) { + throw new \Exception(Translator::getInstance()->trans("Condition value cannot be empty.", [], RewriteUrl::MODULE_DOMAIN)); } $redirectUrl = $request->get("redirectUrl", null); @@ -250,7 +252,7 @@ protected function fillRuleObjectFields(RewriteurlRule $rule, $request) } $rule->setRuleType($ruleType); - $rule->setValue($regexValue); + $rule->setValue($value); $rule->setOnly404($request->get("only404", 1)); $rule->setRedirectUrl($redirectUrl); if (empty($rule->getPosition())) { diff --git a/Service/RewritingRouterFirst.php b/Service/RewritingRouterFirst.php index da8ca97..53d7732 100644 --- a/Service/RewritingRouterFirst.php +++ b/Service/RewritingRouterFirst.php @@ -32,6 +32,15 @@ public function matchRequest(Request $request) $pathInfo = $request instanceof TheliaRequest ? $request->getRealPathInfo() : $request->getPathInfo(); + $redirectByUrl = RewriteurlRuleQuery::create() + ->filterByRuleType('url') + ->filterByOnly404(0) + ->filterByValue(substr($pathInfo, 1)) + ->findOne(); + + if (null !== $redirectByUrl){ + $this->redirect($urlTool->absoluteUrl($redirectByUrl->getRedirectUrl()), 301); + } // Check RewriteUrl rules diff --git a/templates/backOffice/default/RewriteUrl/module-configuration-js.html b/templates/backOffice/default/RewriteUrl/module-configuration-js.html index 12ec175..02bbcb3 100644 --- a/templates/backOffice/default/RewriteUrl/module-configuration-js.html +++ b/templates/backOffice/default/RewriteUrl/module-configuration-js.html @@ -63,12 +63,22 @@ var $container = $("tr[data-idrule=\""+id_rule+"\"]"); - if (selectedRuleType == "regex"){ - $container.find(".js_regex_group").show(); - $container.find(".js_param_rule_group").hide(); - } else if (selectedRuleType == "params"){ - $container.find(".js_regex_group").hide(); - $container.find(".js_param_rule_group").show(); + switch (selectedRuleType){ + case 'regex': + $container.find(".js_regex_group").show(); + $container.find(".js_url_group").hide(); + $container.find(".js_param_rule_group").hide(); + break; + case 'url': + $container.find(".js_regex_group").hide(); + $container.find(".js_url_group").show(); + $container.find(".js_param_rule_group").hide(); + break; + case 'params': + $container.find(".js_regex_group").hide(); + $container.find(".js_url_group").hide(); + $container.find(".js_param_rule_group").show(); + break; } }); @@ -229,10 +239,20 @@ var $tr = $("tr[data-idrule=\""+ruleid+"\"]"); data.id = ruleid; data.ruleType = $tr.find(".js_rule_type").val(); - data.value = $tr.find(".js_regex").val(); data.only404 = $tr.find(".js_only404").prop("checked") ? 1 : 0; data.redirectUrl = $tr.find(".js_url_to_redirect").val(); + switch (data.ruleType){ + case 'regex': + data.value = $tr.find(".js_regex").val(); + break; + case 'url': + data.value = $tr.find(".js_url").val(); + break; + default: + data.value = ""; + } + data.paramRules = []; $tr.find(".js_param_rule").each(function(){ var idParamRule = this.dataset.idparamrule; diff --git a/templates/backOffice/default/RewriteUrl/module-configuration.html b/templates/backOffice/default/RewriteUrl/module-configuration.html index c55bb42..32de54f 100644 --- a/templates/backOffice/default/RewriteUrl/module-configuration.html +++ b/templates/backOffice/default/RewriteUrl/module-configuration.html @@ -71,6 +71,7 @@ {intl l="Regex"} + {intl l="URL"} {intl l="GET params"} @@ -81,6 +82,10 @@ / + + + + diff --git a/templates/backOffice/default/RewriteUrl/tab-value-render.html b/templates/backOffice/default/RewriteUrl/tab-value-render.html index b8e6462..7f9288e 100644 --- a/templates/backOffice/default/RewriteUrl/tab-value-render.html +++ b/templates/backOffice/default/RewriteUrl/tab-value-render.html @@ -4,6 +4,10 @@ / + + + + {foreach from=$REWRITE_URL_PARAMS item=ruleParam}