MediaWiki  REL1_19
router.php
Go to the documentation of this file.
00001 <?php
00002 
00003 # Router for the php cli-server built-in webserver
00004 # http://ca2.php.net/manual/en/features.commandline.webserver.php
00005 
00006 if ( php_sapi_name() != 'cli-server' ) {
00007         die( "This script can only be run by php's cli-server sapi." );
00008 }
00009 
00010 ini_set('display_errors', 1);
00011 error_reporting(E_ALL);
00012 
00013 if ( isset( $_SERVER["SCRIPT_FILENAME"] ) ) {
00014         # Known resource, sometimes a script sometimes a file
00015         $file = $_SERVER["SCRIPT_FILENAME"];
00016 } elseif ( isset( $_SERVER["SCRIPT_NAME"] ) ) {
00017         # Usually unknown, document root relative rather than absolute
00018         # Happens with some cases like /wiki/File:Image.png
00019         if ( is_readable( $_SERVER['DOCUMENT_ROOT'] . $_SERVER["SCRIPT_NAME"] ) ) {
00020                 # Just in case this actually IS a file, set it here
00021                 $file = $_SERVER['DOCUMENT_ROOT'] . $_SERVER["SCRIPT_NAME"];
00022         } else {
00023                 # Otherwise let's pretend that this is supposed to go to index.php
00024                 $file = $_SERVER['DOCUMENT_ROOT'] . '/index.php';
00025         }
00026 } else {
00027         # Meh, we'll just give up
00028         return false;
00029 }
00030 
00031 # And now do handling for that $file
00032 
00033 if ( !is_readable( $file ) ) {
00034         # Let the server throw the error if it doesn't exist
00035         return false;
00036 }
00037 $ext = pathinfo( $file, PATHINFO_EXTENSION );
00038 if ( $ext == 'php' || $ext == 'php5' ) {
00039         # Execute php files
00040         # We use require and return true here because when you return false
00041         # the php webserver will discard post data and things like login
00042         # will not function in the dev environment.
00043         require( $file );
00044         return true;
00045 }
00046 $mime = false;
00047 $lines = explode( "\n", file_get_contents( "includes/mime.types" ) );
00048 foreach ( $lines as $line ) {
00049         $exts = explode( " ", $line );
00050         $mime = array_shift( $exts );
00051         if ( in_array( $ext, $exts ) ) {
00052                 break; # this is the right value for $mime
00053         }
00054         $mime = false;
00055 }
00056 if ( !$mime ) {
00057         $basename = basename( $file );
00058         if ( $basename == strtoupper( $basename ) ) {
00059                 # IF it's something like README serve it as text
00060                 $mime = "text/plain";
00061         }
00062 }
00063 if ( $mime ) {
00064         # Use custom handling to serve files with a known mime type
00065         # This way we can serve things like .svg files that the built-in
00066         # PHP webserver doesn't understand.
00067         # ;) Nicely enough we just happen to bundle a mime.types file
00068         $f = fopen($file, 'rb');
00069         if ( preg_match( '^text/', $mime ) ) {
00070                 # Text should have a charset=UTF-8 (php's webserver does this too)
00071                 header("Content-Type: $mime; charset=UTF-8");
00072         } else {
00073                 header("Content-Type: $mime");
00074         }
00075         header("Content-Length: " . filesize($file));
00076         // Stream that out to the browser
00077         fpassthru($f);
00078         return true;
00079 }
00080 
00081 # Let the php server handle things on it's own otherwise
00082 return false;