From 6d407683b0fcb3bd2550f7554c486df08b9dabcd Mon Sep 17 00:00:00 2001 From: Florent Bruneau Date: Sat, 15 Sep 2007 15:11:11 +0200 Subject: [PATCH] -> Add a search in the URL tree leafs to guess the valid URL. -> Stricter limits for valid URL proposal With this patch: /antispam => guess /emails/antispam /spam => guess /emails/antispam /anti-spam => guess /emails/antispam /redir => guess /emails/redirect /archives/promo2003 => guess /lists/archives/promo2003 and so on... this closes #558 Signed-off-by: Florent Bruneau --- classes/platal.php | 47 ++++++++++++++++++++++++++++++++++++++++++----- templates/core/404.tpl | 2 +- 2 files changed, 43 insertions(+), 6 deletions(-) diff --git a/classes/platal.php b/classes/platal.php index e307782..24a0066 100644 --- a/classes/platal.php +++ b/classes/platal.php @@ -117,7 +117,8 @@ class Platal } $lev = levenshtein($key, $k); - if ((!isset($val) || $lev < $val) && $lev <= (strlen($k)*2)/3) { + if ((!isset($val) || $lev < $val) + && ($lev <= strlen($k)/2 || strpos($k, $key) !== false || strpos($key, $k) !== false)) { $val = $lev; $best = $k; } @@ -133,6 +134,7 @@ class Platal public function near_hook() { $hooks = array(); + $leafs = array(); foreach ($this->__hooks as $hook=>$handler) { if (!$this->check_perms($handler['perms'])) { continue; @@ -145,9 +147,18 @@ class Platal } $place =& $place[$part]; } + $leaf = $parts[count($parts)-1]; + if (!isset($leafs[$leaf])) { + $leafs[$leaf] = $hook; + } else if (is_array($leafs[$leaf])) { + $leafs[$leaf][] = $hook; + } else { + $leafs[$leaf] = array($hook, $leafs[$leaf]); + } $place["#final#"] = array(); } + // search for the nearest full path $p = split('/', $this->path); $place =& $hooks; $link = ''; @@ -159,7 +170,8 @@ class Platal } if ($key == "#final#") { if (!array_key_exists($link, $this->__hooks)) { - return null; + $link = ''; + break; } $key = $k; $ended = true; @@ -171,13 +183,38 @@ class Platal $link .= $key; $place =& $place[$key]; } else { - return null; + $link = ''; + break; } } - if ($link != $this->path) { + if ($link == $this->path) { + $link = ''; + } + if ($link && levenshtein($link, $this->path) < strlen($link)/3) { return $link; } - return null; + + // search for missing namespace (the given name is a leaf) + $leaf = array_shift($p); + $args = count($p) ? '/' . implode('/', $p) : ''; + if (isset($leafs[$leaf]) && !is_array($leafs[$leaf]) && $leafs[$leaf] != $this->path) { + return $leafs[$leaf] . $args; + } + unset($val); + $best = null; + foreach ($leafs as $k=>&$path) { + if (is_array($path)) { + continue; + } + $lev = levenshtein($leaf, $k); + + if ((!isset($val) || $lev < $val) + && ($lev <= strlen($k)/2 || strpos($k, $leaf) !== false || strpos($leaf, $k) !== false)) { + $val = $lev; + $best = $path; + } + } + return $best == null ? ( $link ? $link : null ) : $best . $args; } protected function check_perms($perms) diff --git a/templates/core/404.tpl b/templates/core/404.tpl index 97d57da..b78d9af 100644 --- a/templates/core/404.tpl +++ b/templates/core/404.tpl @@ -24,7 +24,7 @@ {if $near}

L'adresse suivante semble correspondre à ta demande :
-{$globals->baseurl}/{$near} +{icon name="lightbulb" title="Cherchais-tu ?"} {$globals->baseurl}/{$near}

{/if} {* vim:set et sw=2 sts=2 sws=2 enc=utf-8: *} -- 2.1.4