php configs array apps array members profiler bets bukmeyker defaultAp

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
<?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>");
}
}