Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix preg_match in RewriteururlRule model #33

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Config/module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<descriptive locale="fr_FR">
<title>Gérer la réécriture d'url</title>
</descriptive>
<version>1.5.9</version>
<version>1.5.10</version>
<author>
<name>Vincent Lopes, Gilles Bourgeat, Tom Pradat</name>
<email>[email protected], [email protected], [email protected]</email>
Expand Down
12 changes: 7 additions & 5 deletions Controller/Admin/ModuleConfigController.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,15 @@ 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();
$resultsArray[] = [
'Id' => $row['Id'],
'RuleType' => '<select class="js_rule_type form-control" data-idrule="' . $id . '" required>
<option value="regex" ' . $isRegexSelected . '>' . Translator::getInstance()->trans("Regex", [], RewriteUrl::MODULE_DOMAIN) . '</option>
<option value="url" ' . $isUrlSelected . '>' . Translator::getInstance()->trans("URL", [], RewriteUrl::MODULE_DOMAIN) . '</option>
<option value="params" ' . $isParamsSelected . '>' . Translator::getInstance()->trans("Get Params", [], RewriteUrl::MODULE_DOMAIN) . '</option>
</select>',
'Value' => $this->renderRaw(
Expand Down Expand Up @@ -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);
Expand All @@ -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())) {
Expand Down
2 changes: 1 addition & 1 deletion Model/RewriteurlRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function isMatching($url, $getParamArray)
{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Il manque la gestion de la règle URL

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) {
Expand Down
9 changes: 9 additions & 0 deletions Service/RewritingRouterFirst.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
});

Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
<td class="text-center">
<select class="js_rule_type form-control" data-idrule="new" required>
<option value="regex">{intl l="Regex"}</option>
<option value="url">{intl l="URL"}</option>
<option value="params">{intl l="GET params"}</option>
</select>
</td>
Expand All @@ -81,6 +82,10 @@
<span class="input-group-addon">/</span>
</div>

<div class="js_url_group input-group col-md-12">
<input class="js_url form-control" type="text"/>
</div>

<div class="js_param_rule_group">

</div>
Expand Down
4 changes: 4 additions & 0 deletions templates/backOffice/default/RewriteUrl/tab-value-render.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
<span class="input-group-addon" style="width: 1%;">/</span>
</div>

<div class="js_url_group input-group col-md-12">
<input class="js_url form-control" type="text" value="{$VALUE}"/>
</div>

<div class="js_param_rule_group">
{foreach from=$REWRITE_URL_PARAMS item=ruleParam}
<div class="js_param_rule" data-idparamrule="{$ruleParam->getId()}" data-idrule="{$ID}">
Expand Down