<?php
$configs = array (
'apps' => array(
'members' => 'profiler',
'bets' => 'bukmeyker',
),
'defaultApp' => 'pages',
'defaultAppParams' => array(
'name' => 'about',
),
);
/* Match URI string */
//on htaccess isn't in use
if( strpos( $_SERVER['REQUEST_URI'], $_SERVER['SCRIPT_NAME'] ) !== false )
self::$uri = substr( $_SERVER['REQUEST_URI'], strlen( $_SERVER['SCRIPT_NAME'] )+1 );
//:TRICKY:
else
self::$uri = substr( $_SERVER['REQUEST_URI'], strlen(substr(ROOT, strlen($_SERVER['DOCUMENT_ROOT']))) );
//add `/` to the end of URI for router
self::$uri .= substr(self::$uri, -1) == '/'? '':'/';
/* Match APP */
if( isset( self::$_configs['_system'][strtolower(substr( self::$uri, 0, strpos(self::$uri, '/') ))] ) )
{
self::$_app = self::$_configs['_system'][substr( self::$uri, 0, strpos(self::$uri, '/') )];
self::$uri = substr( self::$uri, strpos(self::$uri, '/')+1 );
}
else
self::$_app = self::$_configs['_system']['defaultApp'];
class Router
{
static public $route;
static public $ctrl = false;
static public $act = false;
static private $params;
public static function matchRoute($uri, $params)
{
//Clean $_GET array
$_GET = array();
//check page var
if(stripos($uri, 'page/') !== false AND is_numeric(substr($uri, stripos($uri, 'page/')+5, -1)))
{
$_GET['page'] = substr($uri, stripos($uri, 'page/')+5, -1);
$uri = substr($uri, 0, -(strlen($_GET['page'])+6));
}
foreach( $params['rules'] as $template=>$route )
{
if( preg_match(self::makePattern($template),$uri,$matches) )
{
self::$route = $route;
break;
}
}
if(self::$route !== false)
{
self::$ctrl = substr($route, 0, strpos($route,'/'));
self::$act = substr($route, strpos($route,'/')+1);
}
foreach(self::$params as $key=>$value)
$_GET[$key] = $matches[$key];
}
static private function makePattern($pattern)
{
if(preg_match_all('/<(\w+):?(.*?)?>/',$pattern,$matches))
self::$params=array_combine($matches[1],$matches[2]);
else
self::$params=array();
$p=rtrim($pattern,'*');
$append=$p!==$pattern;
$p=trim($p,'/');
$template=preg_replace('/<(\w+):?.*?>/','<$1>',$p);
if(($pos=strpos($p,'<'))!==false)
$signature=substr($p,0,$pos);
else
$signature=$p;
$tr['/']='\\/';
foreach(self::$params as $key=>$value)
$tr["<$key>"]="(?P<$key>".($value!==''?$value:'[^\/]+').')';
$pattern='/^'.strtr($template,$tr).'\/';
if($append)
$pattern.='/u';
else
$pattern.='$/u';
return $pattern."is";
}
public static function httpError($code)
{
header("HTTP/1.0 {$code} Not Found");
die("<h1>{$code} Error</h1>\n<p>Not Found</p>");
}
}