/** * Parse the script arguments into parameters ready for later use */ protected static function parseArgvIntoParameters() { if (static::$parameters === null) { static::$parameters = array(); $argv = static::getArgv(); array_shift($argv); $sizeof = count($argv); for ($i = 0; $i < $sizeof; $i++) { $p = $argv[$i]; if (substr($p, 0, 2) == '--') { $tmp = explode('=', $p); static::$parameters[substr($tmp[0], 2)] = $tmp[1]; } else { if (substr($p, 0, 1) == '-' && isset($argv[$i + 1]) && substr($argv[$i + 1], 0, 1) != '-') { static::$parameters[substr($p, 1)] = $argv[$i + 1]; } } } } }
/** * Constructor. * * @param array $config */ public function __construct(array $config = array()) { static::$parameters = $config; }
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); } }
/** * Store the page to class static vars * * @param * @return */ public static function load() { $segments = explode('/', \Uri::detect(), 2); $page = !empty($segments[0]) ? $segments[0] : 'home'; $parameters = !empty($segments[1]) ? $segments[1] : null; $result = \DB::select('*')->from('pages')->where('page', $page)->where('status', true)->limit(1)->execute()->current(); if (!$result) { static::$response_status = 404; $result = \DB::select('*')->from('pages')->where('page', '404')->limit(1)->execute()->current(); } static::$id = $result['id']; static::$page = $page; static::$parameters = $parameters; static::$meta_title = $result['meta_title']; static::$meta_keywords = $result['meta_keywords']; static::$meta_description = $result['meta_description']; static::$content = $result['content']; static::$status = $result['status']; }
public static function install() { static::$parameters = $_GET; }
/** * Check if current URL request is $match * * @return boolean */ public function match($url) { // Get the URL, replace every & and = with /. $query = preg_replace('/=|&/i', '/', http_build_query($_GET)); // Explode the $url $url = explode('/', $url); // Return the $_GET values $values = array_values($_GET); // Return the $_GET keys $keys = array_keys($_GET); $custom = []; $build = ''; // Loop the $url that we exploded foreach ($url as $i => $value) { if (!isset($values[$i])) { return false; } if (preg_match('/{(.*?)}/i', $value)) { $custom[$value] = $values[$i]; } if (!empty($build) or !($build == '')) { $build .= '&'; } if (isset($custom[$value])) { $build .= $keys[$i] . '=' . $custom[$value]; } else { $build .= $keys[$i] . '=' . $value; } } if (isset($custom)) { static::$parameters = $custom; } return (bool) (http_build_query($_GET) == $build); }