49d0d0818ca5abbfa82181d6a73ac05b246c87dd
3 # Router for the php cli-server built-in webserver
4 # http://ca2.php.net/manual/en/features.commandline.webserver.php
6 ini_set('display_errors', 1);
7 error_reporting(E_ALL
);
9 if ( isset( $_SERVER["SCRIPT_FILENAME"] ) ) {
10 # Known resource, sometimes a script sometimes a file
11 $file = $_SERVER["SCRIPT_FILENAME"];
12 } elseif ( isset( $_SERVER["SCRIPT_NAME"] ) ) {
13 # Usually unknown, document root relative rather than absolute
14 # Happens with some cases like /wiki/File:Image.png
15 if ( is_readable( $_SERVER['DOCUMENT_ROOT'] . $_SERVER["SCRIPT_NAME"] ) ) {
16 # Just in case this actually IS a file, set it here
17 $file = $_SERVER['DOCUMENT_ROOT'] . $_SERVER["SCRIPT_NAME"];
19 # Otherwise let's pretend that this is supposed to go to index.php
20 $file = $_SERVER['DOCUMENT_ROOT'] . '/index.php';
23 # Meh, we'll just give up
27 # And now do handling for that $file
29 if ( !is_readable( $file ) ) {
30 # Let the server throw the error if it doesn't exist
33 $ext = pathinfo( $file, PATHINFO_EXTENSION
);
34 if ( $ext == 'php' ||
$ext == 'php5' ) {
36 # We use require and return true here because when you return false
37 # the php webserver will discard post data and things like login
38 # will not function in the dev environment.
43 $lines = explode( "\n", file_get_contents( "includes/mime.types" ) );
44 foreach ( $lines as $line ) {
45 $exts = explode( " ", $line );
46 $mime = array_shift( $exts );
47 if ( in_array( $ext, $exts ) ) {
48 break; # this is the right value for $mime
53 $basename = basename( $file );
54 if ( $basename == strtoupper( $basename ) ) {
55 # IF it's something like README serve it as text
60 # Use custom handling to serve files with a known mime type
61 # This way we can serve things like .svg files that the built-in
62 # PHP webserver doesn't understand.
63 # ;) Nicely enough we just happen to bundle a mime.types file
64 $f = fopen($file, 'rb');
65 if ( preg_match( '^text/', $mime ) ) {
66 # Text should have a charset=UTF-8 (php's webserver does this too)
67 header("Content-Type: $mime; charset=UTF-8");
69 header("Content-Type: $mime");
71 header("Content-Length: " . filesize($file));
72 // Stream that out to the browser
77 # Let the php server handle things on it's own otherwise