Orion PHP  0.11.12
The PHP5.3 framework
context.php
Go to the documentation of this file.
00001 <?php
00002 
00003 namespace Orion\Core;
00004 
00005 
00006 /**
00007  * \Orion\Core\Context
00008  * 
00009  * Orion context class.
00010  *
00011  * Handles static context variables such as URL and requests.
00012  * Support for URL generation, header definitions and more.
00013  *
00014  * @author Thibaut Despoulain
00015  * @license BSD 4-clauses
00016  * @version 0.2.11
00017  *
00018  * @static
00019  */
00020 class Context
00021 {
00022     /**
00023      * Current requested URI
00024      * @var string
00025      */
00026     public static $URI;
00027 
00028     /**
00029      * Base site directory, with starting and trailing slashes
00030      * @var string
00031      */
00032     public static $BASE_DIR;
00033 
00034     /**
00035      * Relative path to Orion's directories ('orion/' by default)
00036      * @var string
00037      */
00038     public static $PATH;
00039 
00040     /**
00041      * Current module name
00042      * @var string
00043      */
00044     public static $MODULE_NAME = null;
00045 
00046     /**
00047      * Module URI extension ('.o' by default)
00048      * @var string
00049      */
00050     public static $MODULE_EXT = null;
00051 
00052     /**
00053      * Modules sub URI used for in-module routing purpose
00054      * @var string
00055      */
00056     public static $MODULE_URI = '';
00057 
00058     /**
00059      * Module separator (after module slug and before sub URI)
00060      */
00061     public static $MODULE_SEP = '/';
00062 
00063     /**
00064      * Inits Orion's URI context.
00065      * Basically retreiving and parsing : base directory, requested URI, module URI, URI parameters, and mode.
00066      * 
00067      * Throw an Exception if no compatible URI is found.
00068      */
00069     public static function init( $path )
00070     {
00071         try
00072         {
00073             self::$URI = $_SERVER[ 'REQUEST_URI' ];
00074 
00075             if ( \Orion::config()->defined( 'BASE_DIR' ) )
00076                 self::$BASE_DIR = \Orion::config()->get( 'BASE_DIR' );
00077             else
00078                 self::$BASE_DIR = '';
00079 
00080             if ( \Orion::config()->defined( 'MODULE_SEPARATOR' ) )
00081                 self::$MODULE_SEP = \Orion::config()->get( 'MODULE_SEPARATOR' );
00082             else
00083                 self::$MODULE_SEP = '/';
00084 
00085             self::$PATH = $path;
00086             $uri = self::getRelativeURI();
00087             $modelist = \Orion::config()->get( 'MODE_LIST' );
00088 
00089             if ( $uri == '' )
00090             {
00091                 $mode = \Orion::config()->get( 'DEFAULT_MODE' );
00092 
00093                 if ( !array_key_exists( $mode, $modelist ) )
00094                     throw new Exception( "Default mode isn't registered in MODE_LIST", E_USER_ERROR, get_class() );
00095 
00096                 \Orion::setMode( $mode );
00097                 self::$MODULE_EXT = $modelist[ $mode ];
00098                 self::$MODULE_NAME = \Orion::config()->get( 'DEFAULT_MODULE' );
00099                 self::$MODULE_URI = '';
00100             }
00101             else
00102             {
00103                 foreach ( $modelist as $mode => $ext )
00104                 {
00105                     $matches = array( );
00106                     // Module-only URI type (ex: module.html)
00107                     if ( preg_match( '#^(\w+)' . Tools::escapeRegex( $ext ) . '$#', $uri, $matches ) )
00108                     {
00109                         \Orion::setMode( $mode );
00110                         self::$MODULE_EXT = $ext;
00111                         self::$MODULE_NAME = $matches[ 1 ];
00112                         self::$MODULE_URI = null;
00113                         break;
00114                     }
00115                     // Module+Parameters URI type (ex: module/some/more/parameters.html)
00116                     elseif ( preg_match( '#^(\w+)' . self::$MODULE_SEP . '(.*)' . Tools::escapeRegex( $ext ) . '$#', $uri, $matches ) )
00117                     {
00118                         \Orion::setMode( $mode );
00119                         self::$MODULE_EXT = $ext;
00120                         self::$MODULE_NAME = $matches[ 1 ];
00121                         self::$MODULE_URI = $matches[ 2 ];
00122                         break;
00123                     }
00124                 }
00125             }
00126 
00127             // No compatible URI found, redirecting.
00128             if ( self::$MODULE_NAME == null )
00129             {
00130                 Context::redirect( 404 );
00131             }
00132         }
00133         catch ( Exception $e )
00134         {
00135             throw $e;
00136         }
00137     }
00138 
00139     /**
00140      * Write redirect header
00141      * @param mixed $url Either a redirect code or an URL
00142      */
00143     public static function redirect( $url )
00144     {
00145         if ( $url == 404 )
00146         {
00147             if ( \Orion::isDebug() )
00148                 die( 'Requested URL failed. Components were : ' . self::$MODULE_EXT . ' : ' . self::$MODULE_NAME . ' : ' . self::$MODULE_URI );
00149             else
00150             {
00151                 $links = \Orion::config()->get( 'URL_404' );
00152                 if ( !array_key_exists( \Orion::getMode(), $links ) )
00153                     $mode = 'default';
00154                 else
00155                     $mode = \Orion::getMode();
00156                 self::setHeaderCode( 404 );
00157                 die( file_get_contents( $links[ $mode ] ) );
00158                 $exit( 1 );
00159             }
00160         }
00161         else
00162         {
00163             die( header( 'Location: ' . $url ) );
00164         }
00165     }
00166 
00167     /**
00168      * Generates an absolute URL (prevents URL rewrite issues)
00169      * @param string page url (ie: home/page-2.html)
00170      */
00171     public static function genURL( $page )
00172     {
00173         return \Orion::config()->get( 'BASE_URL' ) . $page;
00174     }
00175 
00176     /**
00177      * Generates an absolute module URL from a module name. 
00178      * For example: genModuleURL('home','/page-1','admin'); will generate 'http://mysite.com/pathtoapp/home/page-1.html'
00179      * @param string $module Example: home
00180      * @param string $uri Example: /page/1
00181      * @param string $mode Example admin
00182      * @return string
00183      */
00184     public static function genModuleURL( $module, $uri=null, $mode=null )
00185     {
00186         $modelist = \Orion::config()->get( 'MODE_LIST' );
00187 
00188         if ( ($mode) != null && array_key_exists( $mode, $modelist ) )
00189             $ext = $modelist[ $mode ];
00190         else
00191             $ext = self::$MODULE_EXT;
00192 
00193         if ( $uri == null || empty( $uri ) )
00194             $page = $module . $ext;
00195         else
00196             $page = $module . self::$MODULE_SEP . $uri . $ext;
00197 
00198         return Context::genURL( $page );
00199     }
00200 
00201     /**
00202      * Get the absolute URL based on an URL relative to the base URL
00203      * @param String Relative URL
00204      * @return String
00205      */
00206     public static function getAbsoluteURL( $relativeURL )
00207     {
00208         // if URL is already absolute
00209         if ( Tools::match( $relativeURL, '(https?|ftp|www\.)(.*)' ) )
00210             return $relativeURL;
00211 
00212         // removes any heading slash
00213         if ( $relativeURL{0} == DS )
00214             $relativeURL = substr( $relativeURL, 1 );
00215 
00216         return self::getBaseURL() . $relativeURL;
00217     }
00218 
00219     /**
00220      * Get Orion's base URL (this is an alias for Orion::config()->get('BASE_URL'))
00221      * @return string
00222      */
00223     public static function getBaseURL()
00224     {
00225         return \Orion::config()->get( 'BASE_URL' );
00226     }
00227 
00228     /**
00229      * Get current mode's default template from configuration
00230      * @return string template name
00231      */
00232     public static function getDefaultTemplate()
00233     {
00234         $template = \Orion::config()->get( strtoupper( \Orion::getMode() ) . '_TEMPLATE' );
00235 
00236         if ( is_null( $template ) )
00237             $template = \Orion::config()->get( 'DEFAULT_TEMPLATE' );
00238 
00239         return $template;
00240     }
00241 
00242     /**
00243      * Get default mode's extension from configuration file
00244      * @return string Extension
00245      */
00246     public static function getDefaultModeExtension()
00247     {
00248         $list = \Orion::config()->get( 'MODE_LIST' );
00249         return $list[ \Orion::config()->get( 'DEFAULT_MODE' ) ];
00250     }
00251 
00252     /**
00253      * Get the relative URI (Base URI minus BASE_DIR)
00254      * @return string The relative URI
00255      */
00256     public static function getRelativeURI()
00257     {
00258         if ( Tools::startWith( self::$URI, self::$BASE_DIR ) )
00259             return substr( self::$URI, strlen( self::$BASE_DIR ) );
00260         else
00261             return self::$URI;
00262     }
00263 
00264     /**
00265      * Get current module's relative URI (ex: 'home')
00266      * @return string
00267      */
00268     public static function getModuleURI()
00269     {
00270         return self::$MODULE_URI;
00271     }
00272 
00273     /**
00274      * Get current page complete relative URI (ex: 'module/method/params.html')
00275      * @return string
00276      */
00277     public static function getFullURI()
00278     {
00279         if ( empty( self::$MODULE_URI ) )
00280             return self::$MODULE_NAME . self::$MODULE_EXT;
00281         else
00282             return self::$MODULE_NAME . self::$MODULE_SEP . self::$MODULE_URI . self::$MODULE_EXT;
00283     }
00284 
00285     /**
00286      * Get current page complete absolute URL (ex: 'mysite.com/module/method/params.html')
00287      * @return string
00288      */
00289     public static function getFullURL()
00290     {
00291         return \Orion::config()->get( 'BASE_URL' ) . self::getFullURI();
00292     }
00293 
00294     /**
00295      * Get module's complete url
00296      * @param string $module Module name, if NULL, returns current module's URL
00297      * @return string
00298      */
00299     public static function getModuleURL( $module=null )
00300     {
00301         if ( $module == null )
00302             return self::genModuleURL( self::$MODULE_NAME );
00303         else
00304             return self::genModuleURL( $module );
00305     }
00306 
00307     /**
00308      * Get current module's path (with trailing slash)
00309      * @return string
00310      */
00311     public static function getGlobalModelPath( $model=null )
00312     {
00313         if ( $model == null )
00314             return \Orion::base() . \Orion::MODEL_PATH;
00315         else
00316             return \Orion::base() . \Orion::MODEL_PATH . $model . \Orion::MODEL_EXT . '.php';
00317     }
00318 
00319     /**
00320      * Get Orion's lib path or generate a filepath for a libs component.
00321      * @param String $file optionnal
00322      * @return String
00323      */
00324     public static function getLibsPath( $file='' )
00325     {
00326         return \Orion::base() . \Orion::LIBS_PATH . $file;
00327     }
00328 
00329     /**
00330      * Get Orion's absolute lib path or generate a filepath for a libs component.
00331      * @param String $file optionnal
00332      * @return String
00333      */
00334     public static function getLibsAbsolutePath( $file='' )
00335     {
00336         return \Orion::config()->get( 'BASE_URL' ) . self::getLibsPath( $file );
00337     }
00338 
00339     /**
00340      * Get current module's path (with trailing slash)
00341      * @return string
00342      */
00343     public static function getModulePath()
00344     {
00345         return \Orion::base() . \Orion::MODULE_PATH . self::$MODULE_NAME . DS;
00346     }
00347 
00348     /**
00349      * Get current module's absolute path (with trailing slash)
00350      * @return string
00351      */
00352     public static function getModuleAbsolutePath( $file='' )
00353     {
00354         return \Orion::config()->get( 'BASE_URL' ) . \Orion::base() . \Orion::MODULE_PATH . self::$MODULE_NAME . DS . $file;
00355     }
00356 
00357     /**
00358      * Get plugin path (with trailing slash)
00359      * @param the plugin folder, if null, returns the global plugins directory path
00360      * @return string
00361      */
00362     public static function getPluginPath( $plugin=null )
00363     {
00364         if ( $plugin == null )
00365             return \Orion::base() . \Orion::PLUGIN_PATH;
00366         else
00367             return \Orion::base() . \Orion::PLUGIN_PATH . $plugin . DS;
00368     }
00369 
00370     /**
00371      * Get plugin's complete url
00372      * @param string $plugin plugin file, if NULL, returns global plugin url
00373      * @return string
00374      */
00375     public static function getPluginURL( $plugin=null )
00376     {
00377         if ( $plugin == null )
00378             return \Orion::config()->get( 'BASE_URL' ) . \Orion::base() . \Orion::PLUGIN_PATH;
00379         else
00380             return \Orion::config()->get( 'BASE_URL' ) . \Orion::base() . \Orion::PLUGIN_PATH . $plugin . DS;
00381     }
00382 
00383     /**
00384      * Get the relative path to the provided template folder (with trailing slash)
00385      * @param string $template Template name
00386      * @return string
00387      */
00388     public static function getTemplatePath( $template )
00389     {
00390         return \Orion::config()->get( 'TEMPLATE_PATH' ) . $template . DS;
00391     }
00392 
00393     /**
00394      * Get the relative path of an uploaded file
00395      * @param string $file 
00396      */
00397     public static function getUploadRelativeFilePath( $file='' )
00398     {
00399         return \Orion::config()->get( 'UPLOAD_DIR' ) . \Orion::config()->get( 'FILE_UPLOAD_DIR' ) . $file;
00400     }
00401 
00402     /**
00403      * Get the absolute path of an uploaded file
00404      * @param string $file 
00405      */
00406     public static function getUploadPath( $file='' )
00407     {
00408         return self::getBaseURL() . \Orion::config()->get( 'UPLOAD_DIR' ) . $file;
00409     }
00410 
00411     /**
00412      * Get the absolute path of an uploaded file in the upload_file folder
00413      * @param string $file 
00414      */
00415     public static function getUploadFilePath( $file='' )
00416     {
00417         return self::getUploadPath( \Orion::config()->get( 'FILE_UPLOAD_DIR' ) . $file );
00418     }
00419 
00420     /**
00421      * Get the full path to the provided template folder
00422      * @param string $template Template name
00423      * @return string
00424      */
00425     public static function getTemplateAbsolutePath( $template )
00426     {
00427         return \Orion::config()->get( 'TEMPLATE_ABS_PATH' ) . $template . DS;
00428     }
00429 
00430     /**
00431      * Get the full path to the provided template file
00432      * @param string $template Template name
00433      * @return string
00434      */
00435     public static function getTemplateFilePath( $template )
00436     {
00437         return self::getTemplatePath( $template ) . $template . \Orion::TEMPLATE_EXT;
00438     }
00439 
00440     /**
00441      * Get a $_GET variable
00442      * @param $var the variable name
00443      * @param $exceptIfNotSet Set this to TRUE to throw an exception if the variable is not set
00444      * @param $preventInjection Set this to TRUE to escape variable content to prevent injections
00445      */
00446     public static function get( $var, $exceptIfNotSet, $preventInjection )
00447     {
00448         if ( !isset( $_GET[ $var ] ) )
00449         {
00450             if ( $exceptIfNotSet )
00451                 throw new Exception( '"' . Security::prevenInjection( $var ) . '" is not set in get data.' );
00452             else
00453                 return null;
00454         }
00455         else
00456         {
00457             return ($preventInjection ? Security::prevenInjection( $_GET[ $var ] ) : $_GET[ $var ]);
00458         }
00459     }
00460 
00461     /**
00462      * Get a $_POST variable
00463      * @param $var the variable name
00464      * @param $exceptIfNotSet Set this to TRUE to throw an exception if the variable is not set
00465      * @param $preventInjection Set this to TRUE to escape variable content to prevent injections
00466      */
00467     public static function post( $var, $exceptIfNotSet=false, $preventInjection=false )
00468     {
00469         if ( !isset( $_POST[ $var ] ) )
00470         {
00471             if ( $exceptIfNotSet )
00472                 throw new Exception( '"' . Security::preventInjection( $var ) . '" is not set in post data.' );
00473             else
00474                 return null;
00475         }
00476         else
00477         {
00478             return ($preventInjection ? Security::preventInjection( $_POST[ $var ] ) : $_POST[ $var ]);
00479         }
00480     }
00481 
00482     /**
00483      * Write provided status code and corresponding text into output headers.
00484      * Range of supported code is 100-504.
00485      * @param int $code 
00486      */
00487     public static function setHeaderCode( $code )
00488     {
00489         $http = array(
00490             100 => "HTTP/1.1 100 Continue",
00491             101 => "HTTP/1.1 101 Switching Protocols",
00492             200 => "HTTP/1.1 200 OK",
00493             201 => "HTTP/1.1 201 Created",
00494             202 => "HTTP/1.1 202 Accepted",
00495             203 => "HTTP/1.1 203 Non-Authoritative Information",
00496             204 => "HTTP/1.1 204 No Content",
00497             205 => "HTTP/1.1 205 Reset Content",
00498             206 => "HTTP/1.1 206 Partial Content",
00499             300 => "HTTP/1.1 300 Multiple Choices",
00500             301 => "HTTP/1.1 301 Moved Permanently",
00501             302 => "HTTP/1.1 302 Found",
00502             303 => "HTTP/1.1 303 See Other",
00503             304 => "HTTP/1.1 304 Not Modified",
00504             305 => "HTTP/1.1 305 Use Proxy",
00505             307 => "HTTP/1.1 307 Temporary Redirect",
00506             400 => "HTTP/1.1 400 Bad Request",
00507             401 => "HTTP/1.1 401 Unauthorized",
00508             402 => "HTTP/1.1 402 Payment Required",
00509             403 => "HTTP/1.1 403 Forbidden",
00510             404 => "HTTP/1.1 404 Not Found",
00511             405 => "HTTP/1.1 405 Method Not Allowed",
00512             406 => "HTTP/1.1 406 Not Acceptable",
00513             407 => "HTTP/1.1 407 Proxy Authentication Required",
00514             408 => "HTTP/1.1 408 Request Time-out",
00515             409 => "HTTP/1.1 409 Conflict",
00516             410 => "HTTP/1.1 410 Gone",
00517             411 => "HTTP/1.1 411 Length Required",
00518             412 => "HTTP/1.1 412 Precondition Failed",
00519             413 => "HTTP/1.1 413 Request Entity Too Large",
00520             414 => "HTTP/1.1 414 Request-URI Too Large",
00521             415 => "HTTP/1.1 415 Unsupported Media Type",
00522             416 => "HTTP/1.1 416 Requested range not satisfiable",
00523             417 => "HTTP/1.1 417 Expectation Failed",
00524             500 => "HTTP/1.1 500 Internal Server Error",
00525             501 => "HTTP/1.1 501 Not Implemented",
00526             502 => "HTTP/1.1 502 Bad Gateway",
00527             503 => "HTTP/1.1 503 Service Unavailable",
00528             504 => "HTTP/1.1 504 Gateway Time-out"
00529         );
00530         header( $http[ $code ] );
00531     }
00532 
00533 }
00534 
00535 ?>