Merge "Added a separate error message for mkdir failures"
[lhc/web/wiklou.git] / maintenance / dev / includes / router.php
1 <?php
2 /**
3 * Router for the php cli-server built-in webserver.
4 * https://secure.php.net/manual/en/features.commandline.webserver.php
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
20 *
21 * @file
22 */
23
24 if ( PHP_SAPI != 'cli-server' ) {
25 die( "This script can only be run by php's cli-server sapi." );
26 }
27
28 ini_set( 'display_errors', 1 );
29 error_reporting( E_ALL );
30
31 if ( isset( $_SERVER["SCRIPT_FILENAME"] ) ) {
32 # Known resource, sometimes a script sometimes a file
33 $file = $_SERVER["SCRIPT_FILENAME"];
34 } elseif ( isset( $_SERVER["SCRIPT_NAME"] ) ) {
35 # Usually unknown, document root relative rather than absolute
36 # Happens with some cases like /wiki/File:Image.png
37 if ( is_readable( $_SERVER['DOCUMENT_ROOT'] . $_SERVER["SCRIPT_NAME"] ) ) {
38 # Just in case this actually IS a file, set it here
39 $file = $_SERVER['DOCUMENT_ROOT'] . $_SERVER["SCRIPT_NAME"];
40 } else {
41 # Otherwise let's pretend that this is supposed to go to index.php
42 $file = $_SERVER['DOCUMENT_ROOT'] . '/index.php';
43 }
44 } else {
45 # Meh, we'll just give up
46 return false;
47 }
48
49 # And now do handling for that $file
50
51 if ( !is_readable( $file ) ) {
52 # Let the server throw the error if it doesn't exist
53 return false;
54 }
55 $ext = pathinfo( $file, PATHINFO_EXTENSION );
56 if ( $ext == 'php' || $ext == 'php5' ) {
57 # Execute php files
58 # We use require and return true here because when you return false
59 # the php webserver will discard post data and things like login
60 # will not function in the dev environment.
61 require $file;
62
63 return true;
64 }
65 $mime = false;
66 // Borrow mime type file from MimeAnalyzer
67 $lines = explode( "\n", file_get_contents( "includes/libs/mime/mime.types" ) );
68 foreach ( $lines as $line ) {
69 $exts = explode( " ", $line );
70 $mime = array_shift( $exts );
71 if ( in_array( $ext, $exts ) ) {
72 break; # this is the right value for $mime
73 }
74 $mime = false;
75 }
76 if ( !$mime ) {
77 $basename = basename( $file );
78 if ( $basename == strtoupper( $basename ) ) {
79 # IF it's something like README serve it as text
80 $mime = "text/plain";
81 }
82 }
83 if ( $mime ) {
84 # Use custom handling to serve files with a known MIME type
85 # This way we can serve things like .svg files that the built-in
86 # PHP webserver doesn't understand.
87 # ;) Nicely enough we just happen to bundle a mime.types file
88 $f = fopen( $file, 'rb' );
89 if ( preg_match( '#^text/#', $mime ) ) {
90 # Text should have a charset=UTF-8 (php's webserver does this too)
91 header( "Content-Type: $mime; charset=UTF-8" );
92 } else {
93 header( "Content-Type: $mime" );
94 }
95 header( "Content-Length: " . filesize( $file ) );
96 // Stream that out to the browser
97 fpassthru( $f );
98
99 return true;
100 }
101
102 # Let the php server handle things on its own otherwise
103 return false;