Merge "Follow-up I774a89d6 (2fabea7): use $this->msg() in HistoryAction"
[mediawiki.git] / maintenance / dev / includes / router.php
blob95bb1faf6e0d6053e3a12de94f234687bec9efff
1 <?php
3 # Router for the php cli-server built-in webserver
4 # http://ca2.php.net/manual/en/features.commandline.webserver.php
6 if ( php_sapi_name() != 'cli-server' ) {
7 die( "This script can only be run by php's cli-server sapi." );
10 ini_set('display_errors', 1);
11 error_reporting(E_ALL);
13 if ( isset( $_SERVER["SCRIPT_FILENAME"] ) ) {
14 # Known resource, sometimes a script sometimes a file
15 $file = $_SERVER["SCRIPT_FILENAME"];
16 } elseif ( isset( $_SERVER["SCRIPT_NAME"] ) ) {
17 # Usually unknown, document root relative rather than absolute
18 # Happens with some cases like /wiki/File:Image.png
19 if ( is_readable( $_SERVER['DOCUMENT_ROOT'] . $_SERVER["SCRIPT_NAME"] ) ) {
20 # Just in case this actually IS a file, set it here
21 $file = $_SERVER['DOCUMENT_ROOT'] . $_SERVER["SCRIPT_NAME"];
22 } else {
23 # Otherwise let's pretend that this is supposed to go to index.php
24 $file = $_SERVER['DOCUMENT_ROOT'] . '/index.php';
26 } else {
27 # Meh, we'll just give up
28 return false;
31 # And now do handling for that $file
33 if ( !is_readable( $file ) ) {
34 # Let the server throw the error if it doesn't exist
35 return false;
37 $ext = pathinfo( $file, PATHINFO_EXTENSION );
38 if ( $ext == 'php' || $ext == 'php5' ) {
39 # Execute php files
40 # We use require and return true here because when you return false
41 # the php webserver will discard post data and things like login
42 # will not function in the dev environment.
43 require( $file );
44 return true;
46 $mime = false;
47 $lines = explode( "\n", file_get_contents( "includes/mime.types" ) );
48 foreach ( $lines as $line ) {
49 $exts = explode( " ", $line );
50 $mime = array_shift( $exts );
51 if ( in_array( $ext, $exts ) ) {
52 break; # this is the right value for $mime
54 $mime = false;
56 if ( !$mime ) {
57 $basename = basename( $file );
58 if ( $basename == strtoupper( $basename ) ) {
59 # IF it's something like README serve it as text
60 $mime = "text/plain";
63 if ( $mime ) {
64 # Use custom handling to serve files with a known mime type
65 # This way we can serve things like .svg files that the built-in
66 # PHP webserver doesn't understand.
67 # ;) Nicely enough we just happen to bundle a mime.types file
68 $f = fopen($file, 'rb');
69 if ( preg_match( '#^text/#', $mime ) ) {
70 # Text should have a charset=UTF-8 (php's webserver does this too)
71 header("Content-Type: $mime; charset=UTF-8");
72 } else {
73 header("Content-Type: $mime");
75 header("Content-Length: " . filesize($file));
76 // Stream that out to the browser
77 fpassthru($f);
78 return true;
81 # Let the php server handle things on it's own otherwise
82 return false;