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