+ foreach ($keys as $k) {
+ if ($k == "#final#") {
+ continue;
+ }
+ $lev = levenshtein($key, $k);
+ if ((!isset($val) || $lev < $val) && $lev <= (strlen($k)*2)/3) {
+ $val = $lev;
+ $best = $k;
+ }
+ }
+ if (!isset($best) && $has_end) {
+ return "#final#";
+ } else {
+ return $best;
+ }
+ return null;
+ }
+
+ public function near_hook()
+ {
+ $hooks = array();
+ foreach ($this->__hooks as $hook=>$handler) {
+ if (!empty($handler['perms']) && $handler['perms'] != S::v('perms')) {
+ continue;
+ }
+ $parts = split('/', $hook);
+ $place =& $hooks;
+ foreach ($parts as $part) {
+ if (!isset($place[$part])) {
+ $place[$part] = array();
+ }
+ $place =& $place[$part];
+ }
+ $place["#final#"] = array();
+ }
+
+ $p = split('/', $this->path);
+ $place =& $hooks;
+ $link = '';
+ foreach ($p as $k) {
+ if (!isset($ended)) {
+ $key = $this->find_nearest_key($k, $place);
+ } else {
+ $key = $k;
+ }
+ if ($key == "#final#") {
+ if (!array_key_exists($link, $this->__hooks)) {
+ return null;
+ }
+ $key = $k;
+ $ended = true;
+ }
+ if (!is_null($key)) {
+ if (!empty($link)) {
+ $link .= '/';
+ }
+ $link .= $key;
+ $place =& $place[$key];
+ } else {
+ return null;
+ }
+ }
+ if ($link != $this->path) {
+ return $link;
+ }
+ return null;
+ }
+
+ protected function check_perms($perms)
+ {
+ if (!$perms) { // No perms, no check
+ return true;
+ }
+ $s_perms = S::v('perms');
+
+ // hook perms syntax is
+ $perms = explode(',', $perms);
+ foreach ($perms as $perm)
+ {
+ $ok = true;
+ $rights = explode(':', $perm);
+ foreach ($rights as $right) {
+ if (($right{0} == '!' && $s_perms->hasFlag(substr($right, 1))) || !$s_perms->hasFlag($right)) {
+ $ok = false;
+ }
+ }
+ if ($ok) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ private function call_hook(PlatalPage &$page)
+ {
+ $hook = $this->find_hook();
+ if (empty($hook)) {