Merge "Set default for Block::appliesToPasswordReset to true"
[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 if ( !isset( $_SERVER['SCRIPT_FILENAME'] ) ) {
29 // Let built-in server handle error.
30 return false;
31 }
32
33 // The SCRIPT_FILENAME can be one of three things:
34 // 1. Absolute path to a file in the docroot associated with the
35 // path of the current request URL. PHP does this for any file path
36 // where it finds a matching file on disk. For both PHP files, and for
37 // static files.
38 // 2. Relative path to router.php (this file), for any unknown URL path
39 // that ends in ".php" or another extension that PHP would execute.
40 // 3. Absolute path to {docroot}/index.php, for any other unknown path.
41 // Effectively treating it as a 404 handler.
42 $file = $_SERVER['SCRIPT_FILENAME'];
43 if ( !is_readable( $file ) ) {
44 // Let built-in server handle error.
45 return false;
46 }
47
48 $ext = pathinfo( $file, PATHINFO_EXTENSION );
49 if ( $ext == 'php' ) {
50 // Let built-in server handle script inclusion.
51 return false;
52 } else {
53 // Serve static file with appropiate Content-Type headers.
54 // The built-in server for PHP 7.0+ supports most files already
55 // (contrary to PHP 5.2, which was supported when router.php was created).
56 // But it still doesn't support as many MIME types as MediaWiki (e.g. ".json")
57
58 // Fallback
59 $mime = 'text/plain';
60 // Borrow from MimeAnalyzer
61 $lines = explode( "\n", file_get_contents( "includes/libs/mime/mime.types" ) );
62 foreach ( $lines as $line ) {
63 $exts = explode( ' ', $line );
64 $type = array_shift( $exts );
65 if ( in_array( $ext, $exts ) ) {
66 $mime = $type;
67 break;
68 }
69 }
70
71 if ( preg_match( '#^text/#', $mime ) ) {
72 // Text should have a charset=UTF-8 (PHP's webserver does this too)
73 header( "Content-Type: $mime; charset=UTF-8" );
74 } else {
75 header( "Content-Type: $mime" );
76 }
77 header( "Content-Length: " . filesize( $file ) );
78 // Stream that out to the browser
79 $f = fopen( $file, 'rb' );
80 fpassthru( $f );
81
82 return true;
83 }