protected static function route() { $root = static::$pageRoot; $dir = ''; $redirect = false; $status = 200; $request = static::removePrefix(static::$request, static::$script ?: ''); $request = static::removePrefix($request, static::$baseUrl); if (static::$original === null) { static::$original = $request; } $questionMarkPosition = strpos($request, '?'); $hasGet = $questionMarkPosition !== false; if ($hasGet) { $request = substr($request, 0, $questionMarkPosition); } $parts = explode('/', $request); for ($i = count($parts); $i >= 0; $i--) { if ($i == 0) { $dir = ''; } else { $dir = implode('/', array_slice($parts, 0, $i)) . '/'; } if (file_exists($root . $dir) && is_dir($root . $dir)) { $parameters = array_slice($parts, $i, count($parts) - $i); if (count($parameters)) { $part = array_shift($parameters); $matches = glob($root . $dir . $part . '(*).phtml'); if (count($matches) == 0) { array_unshift($parameters, $part); $matches = glob($root . $dir . 'index(*).phtml'); } } else { $matches = glob($root . $dir . 'index(*).phtml'); } $csrfOk = static::$method == 'GET' ? true : Session::checkCsrfToken(); if (!$csrfOk) { $status = 403; $matches = glob($root . 'error/forbidden(*).phtml'); $dir = ''; $i = count($parts); } if (!static::$allowGet && $hasGet) { $status = 405; $matches = glob($root . 'error/method_not_allowed(*).phtml'); $dir = ''; $i = count($parts); } if (count($matches) == 0) { $status = 404; $matches = glob($root . 'error/not_found(*).phtml'); $dir = ''; $i = count($parts); } if (count($matches) == 0) { static::error('Could not find 404'); } if (count($matches) > 1) { static::error('Mutiple views matched: ' . implode(', ', $matches)); } list($view, $template) = static::extractParts($matches[0], $root, $dir); static::$url = $dir . $view; static::$view = static::$pageRoot . $dir . $view . '(' . $template . ').phtml'; static::$template = $template != 'none' ? static::$templateRoot . $template : false; $matches = glob($root . $dir . $view . '().php'); if (count($matches) == 0) { $matches = glob($root . $dir . $view . '($*).php'); } if (count($matches) == 0) { static::$action = false; } if (count($matches) > 1) { static::error('Mutiple actions matched: ' . implode(', ', $matches)); } static::$parameters = array(); if (count($matches) == 1) { static::$action = $matches[0]; $parameterNames = static::extractParameterNames($matches[0], $root, $dir, $view); if (substr(static::$url, -7) == '//index') { $redirect = substr(static::$url, 0, -7); } if (substr(static::$original, -6) == '/index') { $redirect = substr(static::$url, 0, -6); } if (count($parameters) > count($parameterNames)) { if (substr(static::$url, -6) == '/index') { static::$url = substr(static::$url, 0, -6); } if (count($parameterNames)) { $redirect = static::$url . '/' . implode('/', array_slice($parameters, 0, count($parameterNames))); } else { $redirect = static::$url; } } $parameters = array_map('urldecode', $parameters); if (count($parameters) < count($parameterNames)) { for ($i = count($parameters); $i < count($parameterNames); $i++) { array_push($parameters, null); } } if (!$redirect && count($parameterNames)) { static::$parameters = array_combine($parameterNames, $parameters); } } break; } } if (Debugger::$enabled) { $method = static::$method; $request = '/' . static::$original; $url = '/' . static::$url; $viewFile = static::$view; $actionFile = static::$action; $templateFile = static::$template; $parameters = array(); $parameters['url'] = static::$parameters; $parameters['get'] = $_GET; $parameters['post'] = $_POST; Debugger::set('router', compact('method', 'csrfOk', 'request', 'url', 'dir', 'view', 'template', 'viewFile', 'actionFile', 'templateFile', 'parameters')); Debugger::set('status', $status); } if ($redirect) { static::redirect($redirect); } }