* (bug 32414) Empty page get a empty bytes attribute in Export/Dump
[lhc/web/wiklou.git] / maintenance / dev / router.php
1 <?php
2
3 # Router for the php cli-server built-in webserver
4 # http://ca2.php.net/manual/en/features.commandline.webserver.php
5
6 ini_set('display_errors', 1);
7 error_reporting(E_ALL);
8
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"];
18 } else {
19 # Otherwise let's pretend that this is supposed to go to index.php
20 $file = $_SERVER['DOCUMENT_ROOT'] . '/index.php';
21 }
22 } else {
23 # Meh, we'll just give up
24 return false;
25 }
26
27 # And now do handling for that $file
28
29 if ( !is_readable( $file ) ) {
30 # Let the server throw the error if it doesn't exist
31 return false;
32 }
33 $ext = pathinfo( $file, PATHINFO_EXTENSION );
34 if ( $ext == 'php' || $ext == 'php5' ) {
35 # Execute php files
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.
39 require( $file );
40 return true;
41 }
42 $mime = false;
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
49 }
50 $mime = false;
51 }
52 if ( !$mime ) {
53 $basename = basename( $file );
54 if ( $basename == strtoupper( $basename ) ) {
55 # IF it's something like README serve it as text
56 $mime = "text/plain";
57 }
58 }
59 if ( $mime ) {
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");
68 } else {
69 header("Content-Type: $mime");
70 }
71 header("Content-Length: " . filesize($file));
72 // Stream that out to the browser
73 fpassthru($f);
74 return true;
75 }
76
77 # Let the php server handle things on it's own otherwise
78 return false;