Campustream 1.0
A social network MQP for WPI
core/lib/router.php
Go to the documentation of this file.
00001 <?php
00002 
00003 
00004 class Router {
00005         
00006         public static function process() {
00007 
00008                 if(isset($_GET['hub_uri']) && $_GET['hub_uri']) {
00009                         $uri = trim($_GET['hub_uri'], '/');
00010                 } else {
00011                         $uri = '_default';
00012                 }
00013                 
00014                 $routes = Router::loadRoutes();
00015                 
00016                 # STATIC URI MATCHING
00017                 if(isset($routes[$uri])) {
00018                         
00019                         $routed_uri = $routes[$uri];
00020                         
00021                 # REGEX URI MATCHING
00022                 } else {
00023                   
00024                         foreach($routes as $key=>$val) {
00025                                 
00026                                 if (preg_match('#^'.$key.'$#u', $uri)) {
00027                                         if (strpos($val, '$') !== FALSE) {
00028                                                 # Use regex routing
00029                                                 $routed_uri = preg_replace('#^'.$key.'$#u', $val, $uri);
00030                                         } else {
00031                                                 # Standard routing
00032                                                 $routed_uri = $val;
00033                                         }
00034 
00035                                         # A valid route has been found
00036                                         break;
00037                                 }
00038                                 
00039                         }
00040                 
00041                 }
00042 
00043                 if(isset($routed_uri)) {
00044 
00045                         $folders = explode( '.', $routed_uri );
00046                         
00047                         $path = null;
00048                         
00049                         if ( count($folders) > 1 ) {
00050                                 $routed_uri = array_pop($folders);
00051                                 $path = join('/', $folders );
00052                         }
00053                 
00054                         $uriparts = explode('/', $routed_uri);
00055                         $controller = $uriparts[0];
00056                         $method = ( $uriparts[1] != '' ? $uriparts[1] : 'index' );
00057                 
00058                         for($i=1;$i<=2;$i++) array_shift($uriparts);
00059                 
00060                         foreach($uriparts as $value) {
00061                                 $aparts = explode(':', $value);
00062                                 $args[$aparts[0]] = $aparts[1];
00063                         }
00064 
00065                         if(substr($method,0,1) == '_') {
00066                                 echo "Error - access to hidden methods not allowed";
00067                                 die();
00068                         }
00069                         
00070                         $router_args = array( 'path' => $path, 'controller' => $controller, 'method' => $method, 'args' => $args );
00071                         
00072                         self::statistics( $router_args );
00073 
00074                         Router::run( $router_args );
00075                 
00076                 } else {
00077 
00078                         # THROW AN ERROR (404)
00079                         Hub::send404();
00080                 }
00081                 
00082         }
00083         
00084         private static function statistics($info) {
00085                 // In order to avoid multiple views for the same page
00086                 if (Hub::is_ajax()) { return; }
00087                 
00088                 extract($info);
00089                 
00090                 $r = RedisManager::connection();
00091                 
00092                 $format = Hub::$response_format;
00093                 
00094                 $key = "stats:$controller:$method:$format:page_views";
00095                 if (!(bool)$r->exists($key)) {
00096                     $r->rpush("stats:by_method", serialize(array('controller' => $controller, 'method' => $method, 'format' => $format)));
00097                 }
00098                 
00099                 $r->incr($key);
00100                 $r->incr('stats:total_views');
00101                 $r->sadd("stats:ips", $_SERVER['REMOTE_ADDR']);
00102                 $r->incr("stats:by_ip:{$_SERVER['REMOTE_ADDR']}");
00103 
00104         }
00105         
00106         public static function run($args) {
00107 
00108                 $cname = $args['controller'] . '_Controller';
00109                 $mname = $args['method'];
00110                 
00111                 if ( $args['path'] !== null ) {
00112                         include "{$GLOBALS['APPROOT']}application/controllers/{$args['path']}/{$args['controller']}.php";
00113                 }
00114                 
00115                 // HACK HACK HACK HACK
00116                 // for backwards compatability, pass the format to the controller as an arg
00117                 $args['args']['format'] = Hub::$response_format;
00118                 // HACK HACK HACK HACK
00119                 
00120                 $controller = new $cname;
00121                 $controller->$mname($args['args']);
00122                 
00123         }
00124         
00125         public static function loadRoutes() {
00126                 require( $GLOBALS['APPROOT'] . 'application/config/routes.php' );
00127                 
00128                 return $config;
00129         }
00130         
00131 }