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