MediaWiki  REL1_22
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     return true;
00063 }
00064 $mime = false;
00065 $lines = explode( "\n", file_get_contents( "includes/mime.types" ) );
00066 foreach ( $lines as $line ) {
00067     $exts = explode( " ", $line );
00068     $mime = array_shift( $exts );
00069     if ( in_array( $ext, $exts ) ) {
00070         break; # this is the right value for $mime
00071     }
00072     $mime = false;
00073 }
00074 if ( !$mime ) {
00075     $basename = basename( $file );
00076     if ( $basename == strtoupper( $basename ) ) {
00077         # IF it's something like README serve it as text
00078         $mime = "text/plain";
00079     }
00080 }
00081 if ( $mime ) {
00082     # Use custom handling to serve files with a known mime type
00083     # This way we can serve things like .svg files that the built-in
00084     # PHP webserver doesn't understand.
00085     # ;) Nicely enough we just happen to bundle a mime.types file
00086     $f = fopen( $file, 'rb' );
00087     if ( preg_match( '#^text/#', $mime ) ) {
00088         # Text should have a charset=UTF-8 (php's webserver does this too)
00089         header( "Content-Type: $mime; charset=UTF-8" );
00090     } else {
00091         header( "Content-Type: $mime" );
00092     }
00093     header( "Content-Length: " . filesize( $file ) );
00094     // Stream that out to the browser
00095     fpassthru( $f );
00096     return true;
00097 }
00098 
00099 # Let the php server handle things on it's own otherwise
00100 return false;