r104148 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r104147‎ | r104148 | r104149 >
Date:09:50, 24 November 2011
Author:dantman
Status:ok
Tags:
Comment:
Update maintenance/dev/'s router, add handling for things like /wiki/File:Foo.png which end up with a SCRIPT_NAME but not SCRIPT_FILENAME.
Modified paths:
  • /trunk/phase3/maintenance/dev/router.php (modified) (history)

Diff [purge]

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

Status & tagging log