diff --git a/system/includes/dispatch.php b/system/includes/dispatch.php index f3d5b02..2b45b03 100644 --- a/system/includes/dispatch.php +++ b/system/includes/dispatch.php @@ -1,531 +1,531 @@ 0) { - foreach ($sym as $s) { - $s = substr($s, 1); - if (isset($cb_map[$s]) && isset($cb_or_val[$s])) - call_user_func($cb_map[$s], $cb_or_val[$s]); + static $cb_map = array(); + + if (is_callable($cb_or_val)) { + $cb_map[$sym] = $cb_or_val; + return; + } + + if (is_array($sym) && count($sym) > 0) { + foreach ($sym as $s) { + $s = substr($s, 1); + if (isset($cb_map[$s]) && isset($cb_or_val[$s])) + call_user_func($cb_map[$s], $cb_or_val[$s]); + } + return; } - return; - } - error(500, 'bad call to filter()'); + error(500, 'bad call to filter()'); } function route_to_regex($route) { - $route = preg_replace_callback('@:[\w]+@i', function ($matches) { - $token = str_replace(':', '', $matches[0]); - return '(?P<'.$token.'>[a-z0-9_\0-\.]+)'; - }, $route); - return '@^'.rtrim($route, '/').'$@i'; + $route = preg_replace_callback('@:[\w]+@i', function ($matches) { + $token = str_replace(':', '', $matches[0]); + return '(?P<' . $token . '>[a-z0-9_\0-\.]+)'; + }, $route); + return '@^' . rtrim($route, '/') . '$@i'; } function route($method, $pattern, $callback = null) { - // callback map by request type - static $route_map = array( - 'GET' => array(), - 'POST' => array() - ); + // callback map by request type + static $route_map = array( + 'GET' => array(), + 'POST' => array() + ); - $method = strtoupper($method); + $method = strtoupper($method); - if (!in_array($method, array('GET', 'POST'))) - error(500, 'Only GET and POST are supported'); + if (!in_array($method, array('GET', 'POST'))) + error(500, 'Only GET and POST are supported'); - // a callback was passed, so we create a route defiition - if ($callback !== null) { + // a callback was passed, so we create a route defiition + if ($callback !== null) { - // create a route entry for this pattern - $route_map[$method][$pattern] = array( - 'xp' => route_to_regex($pattern), - 'cb' => $callback - ); + // create a route entry for this pattern + $route_map[$method][$pattern] = array( + 'xp' => route_to_regex($pattern), + 'cb' => $callback + ); - } else { + } else { - // callback is null, so this is a route invokation. look up the callback. - foreach ($route_map[$method] as $pat => $obj) { + // callback is null, so this is a route invokation. look up the callback. + foreach ($route_map[$method] as $pat => $obj) { - // if the requested uri ($pat) has a matching route, let's invoke the cb - if (!preg_match($obj['xp'], $pattern, $vals)) - continue; + // if the requested uri ($pat) has a matching route, let's invoke the cb + if (!preg_match($obj['xp'], $pattern, $vals)) + continue; - // call middleware - middleware($pattern); + // call middleware + middleware($pattern); - // construct the params for the callback - array_shift($vals); - preg_match_all('@:([\w]+)@', $pat, $keys, PREG_PATTERN_ORDER); - $keys = array_shift($keys); - $argv = array(); + // construct the params for the callback + array_shift($vals); + preg_match_all('@:([\w]+)@', $pat, $keys, PREG_PATTERN_ORDER); + $keys = array_shift($keys); + $argv = array(); - foreach ($keys as $index => $id) { - $id = substr($id, 1); - if (isset($vals[$id])) { - array_push($argv, trim(urldecode($vals[$id]))); - } - } + foreach ($keys as $index => $id) { + $id = substr($id, 1); + if (isset($vals[$id])) { + array_push($argv, trim(urldecode($vals[$id]))); + } + } - // call filters if we have symbols - if (count($keys)) { - filter(array_values($keys), $vals); - } + // call filters if we have symbols + if (count($keys)) { + filter(array_values($keys), $vals); + } - // if cb found, invoke it - if (is_callable($obj['cb'])) { - call_user_func_array($obj['cb'], $argv); - } + // if cb found, invoke it + if (is_callable($obj['cb'])) { + call_user_func_array($obj['cb'], $argv); + } - // leave after first match - break; + // leave after first match + break; + } } - } } function get($path, $cb) { - route('GET', $path, $cb); + route('GET', $path, $cb); } function post($path, $cb) { - route('POST', $path, $cb); + route('POST', $path, $cb); } function flash($key, $msg = null, $now = false) { - static $x = array(), - $f = null; + static $x = array(), + $f = null; - $f = (config('cookies.flash') ? config('cookies.flash') : '_F'); + $f = (config('cookies.flash') ? config('cookies.flash') : '_F'); - if ($c = get_cookie($f)) - $c = json_decode($c, true); - else - $c = array(); + if ($c = get_cookie($f)) + $c = json_decode($c, true); + else + $c = array(); - if ($msg == null) { + if ($msg == null) { - if (isset($c[$key])) { - $x[$key] = $c[$key]; - unset($c[$key]); - set_cookie($f, json_encode($c)); - } + if (isset($c[$key])) { + $x[$key] = $c[$key]; + unset($c[$key]); + set_cookie($f, json_encode($c)); + } - return (isset($x[$key]) ? $x[$key] : null); - } + return (isset($x[$key]) ? $x[$key] : null); + } - if (!$now) { - $c[$key] = $msg; - set_cookie($f, json_encode($c)); - } + if (!$now) { + $c[$key] = $msg; + set_cookie($f, json_encode($c)); + } - $x[$key] = $msg; + $x[$key] = $msg; } function dispatch() { - $path = urldecode($_SERVER['REQUEST_URI']); + $path = urldecode($_SERVER['REQUEST_URI']); - if (config('site.url') !== null) - $path = preg_replace('@^'.preg_quote(site_path()).'@', '', $path); + if (config('site.url') !== null) + $path = preg_replace('@^' . preg_quote(site_path()) . '@', '', $path); - $parts = preg_split('/\?/', $path, -1, PREG_SPLIT_NO_EMPTY); + $parts = preg_split('/\?/', $path, -1, PREG_SPLIT_NO_EMPTY); - $uri = trim($parts[0], '/'); - $uri = strlen($uri) ? $uri : 'index'; + $uri = trim($parts[0], '/'); + $uri = strlen($uri) ? $uri : 'index'; - route(method(), "/{$uri}"); + route(method(), "/{$uri}"); } diff --git a/system/upgrade/run.php b/system/upgrade/run.php index cc9d0ef..2d4ed76 100644 --- a/system/upgrade/run.php +++ b/system/upgrade/run.php @@ -2,20 +2,22 @@ $updater = new Kanti\HubUpdater("danpros/htmly"); $info = $updater->getCurrentInfo(); -$versionNumber = substr($info['tag_name'],1); +$versionNumber = substr($info['tag_name'], 1); -function isGraterThan($string){ +function isGraterThan($string) +{ global $versionNumber; - return (version_compare($versionNumber,$string) > 0); + return (version_compare($versionNumber, $string) > 0); } // http://stackoverflow.com/questions/3338123/how-do-i-recursively-delete-a-directory-and-its-entire-contents-files-sub-dir -function rrmdir($dir) { +function rrmdir($dir) +{ if (is_dir($dir)) { $objects = scandir($dir); foreach ($objects as $object) { if ($object != "." && $object != "..") { - if (filetype($dir."/".$object) == "dir") rrmdir($dir."/".$object); else unlink($dir."/".$object); + if (filetype($dir . "/" . $object) == "dir") rrmdir($dir . "/" . $object); else unlink($dir . "/" . $object); } } reset($objects); @@ -24,13 +26,11 @@ function rrmdir($dir) { } //run upgrade specific stuff -if(isGraterThan("2.3")) {// 2.4, 2.5, ... - if(file_exists("vendor/")){ +if (isGraterThan("2.3")) {// 2.4, 2.5, ... + if (file_exists("vendor/")) { rrmdir("vendor/"); } } -if(isGraterThan("2.3")) { - file_put_contents("index.php",file_get_contents("system/upgrade/index.php")); - rrmdir("system/upgrade/"); -} \ No newline at end of file +file_put_contents("index.php", file_get_contents("system/upgrade/index.php")); +rrmdir("system/upgrade/");