Campustream 1.0
A social network MQP for WPI
|
00001 <?php 00002 00003 class Hub { 00004 00005 public static $activecontroller = NULL; 00006 public static $response_format = NULL; 00007 00008 public static $mime_types = array( 'xml' => 'text/xml', 'html' => 'text/html', 'json' => 'application/json', 'rss' => 'application/rss+xml', 'jsx' => 'application/javascript', 'js' => 'text/javascript' ); 00009 00010 00011 public static function run() { 00012 00013 Hub::setup(); 00014 00015 if(!iprules::allowed()) { 00016 return Hub::http_error(403, "Forbidden"); 00017 } 00018 00019 Hub::determine_response_format(); 00020 Router::process(); 00021 00022 } 00023 00024 // determines the type of response the requestor wants based on the extension 00025 // of the url. then does some path mangling so it's transparent to the router 00026 00027 public static function determine_response_format() { 00028 // try to get the position of the extension, if it exists 00029 $ext_pos = strrpos( $_GET['hub_uri'], '.' ); 00030 00031 // set the response type as html by default 00032 if ( $ext_pos === false ) { 00033 self::$response_format = 'html'; 00034 return; 00035 } 00036 00037 // grab everything after the "." in the uri 00038 self::$response_format = substr( $_GET['hub_uri'], $ext_pos+1 ); 00039 00040 // hack to make .do extensions compatible with this kind of api 00041 if ( self::$response_format == 'do' ) self::$response_format = 'html'; 00042 00043 // remove the extension from the uri, so the router doesn't have to worry about it 00044 $_GET['hub_uri'] = substr( $_GET['hub_uri'], 0, $ext_pos ); 00045 00046 } 00047 00048 public static function setup() { 00049 00050 spl_autoload_register(array('Hub', 'auto_load')); 00051 set_error_handler(array('Hub', 'error_handler')); 00052 set_exception_handler(array('Hub', 'exception_handler')); 00053 00054 } 00055 00056 public static function redirect($url) { 00057 header("Location: $url"); 00058 } 00059 00060 public static function send404() { 00061 header("HTTP/1.0 404 Not Found"); 00062 } 00063 00064 function browserInfo($agent = null) { 00065 // Declare known browsers to look for 00066 $known = array('msie', 'firefox', 'safari', 'webkit', 'opera', 'netscape', 'konqueror', 'gecko'); 00067 00068 // Clean up agent and build regex that matches phrases for known browsers 00069 // (e.g. "Firefox/2.0" or "MSIE 6.0" (This only matches the major and minor 00070 // version numbers. E.g. "2.0.0.6" is parsed as simply "2.0" 00071 $agent = strtolower($agent ? $agent : $_SERVER['HTTP_USER_AGENT']); 00072 $pattern = '#(?<browser>' . join('|', $known) . ')[/ ]+(?<version>[0-9]+(?:\.[0-9]+)?)#'; 00073 00074 // Find all phrases (or return empty array if none found) 00075 if (!preg_match_all($pattern, $agent, $matches)) return array(); 00076 00077 // Since some UAs have more than one phrase (e.g Firefox has a Gecko phrase, 00078 // Opera 7,8 have a MSIE phrase), use the last one found (the right-most one 00079 // in the UA). That's usually the most correct. 00080 $i = count($matches['browser'])-1; 00081 00082 return array('name' => $matches['browser'][$i], 'version' => $matches['version'][$i]); 00083 } 00084 00085 public static function is_ajax() { 00086 return $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest'; 00087 } 00088 00089 public static function cache_headers( $unique_id ) { 00090 header("Last-Modified: ".gmdate("D, d M Y H:i:s", time()-10000)." GMT"); 00091 header("Etag: {$unique_id}"); 00092 00093 if ( stripos( $_SERVER['REQUEST_METHOD'], 'HEAD' ) === true ) { 00094 header('HTTP/1.1 304 Not Modified'); 00095 exit(); 00096 } 00097 00098 header("Cache-Control: maxage=10000"); 00099 header('Expires: ' . gmdate('D, d M Y H:i:s', time()+10000) . ' GMT'); 00100 header('Pragma: public'); 00101 } 00102 00103 public static function auto_load($class) { 00104 #echo "<br>Loading: $class<br>"; 00105 00106 $lib = true; 00107 if(strpos($class,'_') !== false) $lib = false; 00108 00109 if($lib) { 00110 00111 $classfilename = strtolower($class) . '.php'; 00112 00113 if(file_exists($GLOBALS['APPROOT'] . 'core/lib/' . $classfilename )) { 00114 00115 include($GLOBALS['APPROOT'] . 'core/lib/' . $classfilename); 00116 return; 00117 00118 } elseif(file_exists($GLOBALS['APPROOT'] . 'application/lib/' . $classfilename )) { 00119 00120 include($GLOBALS['APPROOT'] . 'application/lib/' . $classfilename); 00121 return; 00122 00123 } 00124 00125 } else { 00126 00127 $cparts = explode('_', $class); 00128 $type = strtolower($cparts[1]); 00129 $classfilename = strtolower($cparts[0]) . '.php'; 00130 00131 if($type == 'controller') { 00132 00133 include($GLOBALS['APPROOT'] . 'application/controllers/' . $classfilename); 00134 return; 00135 00136 } elseif($type == 'view') { 00137 00138 include($GLOBALS['APPROOT'] . 'application/views/' . $classfilename); 00139 return; 00140 00141 } elseif($type == 'model') { 00142 00143 include($GLOBALS['APPROOT'] . 'application/models/' . $classfilename); 00144 return; 00145 00146 } 00147 00148 } 00149 00150 00151 } 00152 00153 public static function http_error( $code, $message=false ) { 00154 00155 switch ( $code ) { 00156 case 400: 00157 header( "HTTP/1.1 400 Bad Request" ); 00158 break; 00159 case 401: 00160 header( "HTTP/1.1 401 Unauthorized" ); 00161 break; 00162 case 403: 00163 header( "HTTP/1.1 403 Forbidden" ); 00164 break; 00165 case 404: 00166 header( "HTTP/1.1 404 Not Found" ); 00167 break; 00168 } 00169 00170 View::respond_to( 'html', function() { 00171 00172 $view = new View( 'error/404_general' ); 00173 00174 if ( Hub::$activecontroller !== null ) { 00175 Hub::$activecontroller->template->content = $view->render(); 00176 Hub::$activecontroller->template->page_title = "Page could not be found - Twitpic"; 00177 echo Hub::$activecontroller->template->render(); 00178 } else { 00179 echo $view->render(); 00180 } 00181 00182 }); 00183 00184 View::respond_to( array('json','jsonp','xml'), function($format) use ($code, $message) { 00185 00186 $response = new stdClass; 00187 00188 $response->code = $code; 00189 $response->message = $message; 00190 00191 echo View::factory( "error/error.{$format}" )->set( "response", $response )->render(); 00192 00193 }); 00194 00195 return Hub::shutdown(); 00196 00197 } 00198 00199 public static function exception_handler( $exception ) { 00200 header('HTTP/1.1 500 Internal Server Error'); 00201 Logger::debug("--> ERROR: " . $exception->getMessage()); 00202 include $GLOBALS['APPROOT'] . 'www/50x.html'; 00203 } 00204 00205 public static function error_handler($errno, $errstr, $errfile, $errline) { 00206 //Logger::debug("--> ERROR: $errstr"); 00207 return true; 00208 } 00209 00210 public static function shutdown() { 00211 // future cleanup code- ideally, all requests should end here... 00212 exit; 00213 } 00214 00215 00216 }