r100782 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r100781‎ | r100782 | r100783 >
Date:04:13, 26 October 2011
Author:aaron
Status:resolved
Tags:
Comment:
FU r100535:
* Added thumb-handler.php5 file
* Moved 404.php to thumb-handler.php as wfDisplay404Error()
* Removed excess short-circuiting error cases in thumb-handler.php
* Moved cURL extension check to the top of the file
* Improved sample $thgThumbHashFragment regex value
* Cleaned up wfStreamThumbViaCurl() error handling code a bit
Modified paths:
  • /trunk/phase3/404.php (deleted) (history)
  • /trunk/phase3/thumb-handler.php (modified) (history)
  • /trunk/phase3/thumb-handler.php5 (added) (history)
  • /trunk/phase3/thumb.config.sample (modified) (history)

Diff [purge]

Index: trunk/phase3/404.php
@@ -1,29 +0,0 @@
2 -<?php
3 -
4 -header( 'HTTP/1.1 404 Not Found' );
5 -header( 'Content-Type: text/html;charset=utf-8' );
6 -
7 -# $_SERVER['REQUEST_URI'] has two different definitions depending on PHP version
8 -if ( preg_match( '!^([a-z]*://)([a-z.]*)(/.*)$!', $_SERVER['REQUEST_URI'], $matches ) ) {
9 - $prot = $matches[1];
10 - $serv = $matches[2];
11 - $loc = $matches[3];
12 -} else {
13 - $prot = "http://";
14 - $serv = strlen( $_SERVER['HTTP_HOST'] ) ? $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME'];
15 - $loc = $_SERVER["REQUEST_URI"];
16 -}
17 -$encUrl = htmlspecialchars( $prot . $serv . $loc );
18 -
19 -// Looks like a typical apache2 error
20 -$standard_404 = <<<ENDTEXT
21 -<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
22 -<html><head>
23 -<title>404 Not Found</title>
24 -</head><body>
25 -<h1>Not Found</h1>
26 -<p>The requested URL $encUrl was not found on this server.</p>
27 -</body></html>
28 -ENDTEXT;
29 -
30 -echo $standard_404;
Index: trunk/phase3/thumb.config.sample
@@ -23,7 +23,7 @@
2424 $thgThumbFragment = "MW_trunk/images/thumb";
2525 # URL regex fragment correspond to the directory hashing of thumbnails.
2626 # This must correspond to $wgLocalFileRepo['hashLevels'].
27 -$thgThumbHashFragment = '\w/\w\w/'; // 2-level directory hashing
 27+$thgThumbHashFragment = '[0-9a-f]/[0-9a-f][0-9a-f]/'; // 2-level directory hashing
2828
2929 # The URL to thumb.php, accessible from the web server.
3030 $thgThumbScriptPath = "http://localhost/MW_trunk/thumb.php";
@@ -36,7 +36,7 @@
3737 $thgThumbCurlProxy = null; // proxy to thumb.php
3838
3939 # File path to a php file the gives a 404 error message
40 -$thgThumb404File = "404.php";
 40+$thgThumb404File = null;
4141
4242 # Custom functions for overriding aspects of thumb handling
4343 $thgThumbCallbacks = array();
Index: trunk/phase3/thumb-handler.php
@@ -8,10 +8,14 @@
99 $configPath = dirname( __FILE__ ) . "/thumb.config.php";
1010 if ( !file_exists( $configPath ) ) {
1111 die( "Thumb-handler.php is not enabled for this wiki.\n" );
 12+} elseif ( !extension_loaded( 'curl' ) ) {
 13+ die( "cURL is not enabled for PHP on this wiki.\n" ); // sanity
1214 }
1315 require( $configPath );
1416
15 -function wfHandleThumb404() {
 17+wfHandleThumb404Main();
 18+
 19+function wfHandleThumb404Main() {
1620 global $thgThumbCallbacks, $thgThumb404File;
1721
1822 # lighttpd puts the original request in REQUEST_URI, while
@@ -26,37 +30,24 @@
2731
2832 # Extract thumb.php params from the URI...
2933 if ( isset( $thgThumbCallbacks['extractParams'] )
30 - && is_callable( $thgThumbCallbacks['extractParams'] ) )
 34+ && is_callable( $thgThumbCallbacks['extractParams'] ) ) // overridden by configuration?
3135 {
32 - # Overridden by configuration
3336 $params = call_user_func_array( $thgThumbCallbacks['extractParams'], array( $uri ) );
3437 } else {
3538 $params = wfExtractThumbParams( $uri ); // basic wiki URL param extracting
3639 }
37 - if ( $params === null ) { // not a valid thumb request
 40+
 41+ # Show 404 error if this is not a valid thumb request...
 42+ if ( !is_array( $params ) ) {
3843 header( 'X-Debug: no regex match' ); // useful for debugging
39 - require_once( $thgThumb404File ); // standard 404 message
 44+ if ( $thgThumb404File ) { // overridden by configuration?
 45+ require( $thgThumb404File );
 46+ } else {
 47+ wfDisplay404Error(); // standard 404 message
 48+ }
4049 return;
4150 }
4251
43 - # Do some basic checks on the filename...
44 - if ( preg_match( '/[\x80-\xff]/', $uri ) ) {
45 - header( 'HTTP/1.0 400 Bad request' );
46 - header( 'Content-Type: text/html' );
47 - print "<html><head><title>Bad request</title></head><body>" .
48 - "The URI contained bytes with the high bit set, this is not allowed." .
49 - "</body></html>";
50 - return;
51 - } elseif ( strpos( $params['f'], '%20' ) !== false ) {
52 - header( 'HTTP/1.0 404 Not found' );
53 - header( 'Content-Type: text/html' );
54 - header( 'X-Debug: filename contains a space' ); // useful for debugging
55 - print "<html><head><title>Not found</title></head><body>" .
56 - "The URL contained spaces, we don't have any thumbnail files with spaces." .
57 - "</body></html>";
58 - return;
59 - }
60 -
6152 # Check any backend caches for the thumbnail...
6253 if ( isset( $thgThumbCallbacks['checkCache'] )
6354 && is_callable( $thgThumbCallbacks['checkCache'] ) )
@@ -83,7 +74,6 @@
8475 preg_quote( $thgThumbFragment ) . '(/archive|/temp|)/' .
8576 $thgThumbHashFragment . '([^/]*)/(page(\d*)-)*(\d*)px-[^/]*$!';
8677
87 - # Is this a thumbnail?
8878 if ( preg_match( $thumbRegex, $uri, $matches ) ) {
8979 list( $all, $archOrTemp, $filename, $pagefull, $pagenum, $size ) = $matches;
9080 $params = array( 'f' => $filename, 'width' => $size );
@@ -96,7 +86,7 @@
9787 $params['temp'] = 1;
9888 }
9989 } else {
100 - $params = null;
 90+ $params = null; // not a valid thumbnail URL
10191 }
10292
10393 return $params;
@@ -112,16 +102,6 @@
113103 function wfStreamThumbViaCurl( array $params, $uri ) {
114104 global $thgThumbCallbacks, $thgThumbScriptPath, $thgThumbCurlProxy, $thgThumbCurlTimeout;
115105
116 - if ( !function_exists( 'curl_init' ) ) {
117 - header( 'HTTP/1.0 404 Not found' );
118 - header( 'Content-Type: text/html' );
119 - header( 'X-Debug: cURL is not enabled' ); // useful for debugging
120 - print "<html><head><title>Not found</title></head><body>" .
121 - "cURL is not enabled for PHP on this wiki. Unable to send request thumb.php." .
122 - "</body></html>";
123 - return;
124 - }
125 -
126106 # Build up the request URL to use with CURL...
127107 $reqURL = "{$thgThumbScriptPath}?";
128108 $first = true;
@@ -166,29 +146,28 @@
167147 # Actually make the request
168148 $text = curl_exec( $ch );
169149
170 - # Send it on to the client
 150+ # Send it on to the client...
171151 $errno = curl_errno( $ch );
172152 $contentType = curl_getinfo( $ch, CURLINFO_CONTENT_TYPE );
173153 $httpCode = curl_getinfo( $ch, CURLINFO_HTTP_CODE );
174154 if ( $errno ) {
175155 header( 'HTTP/1.1 500 Internal server error' );
176156 header( 'Cache-Control: no-cache' );
177 - list( $text, $contentType ) = wfCurlErrorText( $ch );
178 - } elseif ( $httpCode == 304 ) {
 157+ $contentType = 'text/html';
 158+ $text = wfCurlErrorText( $ch );
 159+ } elseif ( $httpCode == 304 ) { // OK
179160 header( 'HTTP/1.1 304 Not modified' );
180161 $contentType = '';
181162 $text = '';
182163 } elseif ( strval( $text ) == '' ) {
183164 header( 'HTTP/1.1 500 Internal server error' );
184165 header( 'Cache-Control: no-cache' );
185 - list( $text, $contentType ) = wfCurlEmptyText( $ch );
 166+ $contentType = 'text/html';
 167+ $text = wfCurlEmptyText( $ch );
186168 } elseif ( $httpCode == 404 ) {
187169 header( 'HTTP/1.1 404 Not found' );
188170 header( 'Cache-Control: s-maxage=300, must-revalidate, max-age=0' );
189 - } elseif ( $httpCode != 200
190 - || substr( $contentType, 0, 9 ) == 'text/html'
191 - || substr( $text, 0, 5 ) == '<html' )
192 - {
 171+ } elseif ( $httpCode != 200 || substr( $contentType, 0, 9 ) == 'text/html' ) {
193172 # Error message, suppress cache
194173 header( 'HTTP/1.1 500 Internal server error' );
195174 header( 'Cache-Control: no-cache' );
@@ -213,40 +192,61 @@
214193 }
215194
216195 /**
217 - * Get error message and content type for when the cURL response is empty.
 196+ * Get error message HTML for when the cURL response is an error.
218197 *
219198 * @param $ch cURL handle
220 - * @return Array (error html, content type)
 199+ * @return string
221200 */
222201 function wfCurlErrorText( $ch ) {
223 - $contentType = 'text/html';
224202 $error = htmlspecialchars( curl_error( $ch ) );
225 - $text = <<<EOT
 203+ return <<<EOT
226204 <html>
227205 <head><title>Thumbnail error</title></head>
228206 <body>Error retrieving thumbnail from scaling server: $error</body>
229207 </html>
230208 EOT;
231 - return array( $text, $contentType );
232209 }
233210
234211 /**
235 - * Get error message and content type for when the cURL response is an error.
 212+ * Get error message HTML for when the cURL response is empty.
236213 *
237214 * @param $ch cURL handle
238 - * @return Array (error html, content type)
 215+ * @return string
239216 */
240217 function wfCurlEmptyText( $ch ) {
241 - $contentType = 'text/html';
242 - $error = htmlspecialchars( curl_error( $ch ) );
243 - $text = <<<EOT
 218+ return <<<EOT
244219 <html>
245220 <head><title>Thumbnail error</title></head>
246221 <body>Error retrieving thumbnail from scaling server: empty response</body>
247222 </html>
248223 EOT;
249 - return array( $text, $contentType );
250224 }
251225
252 -# Entry point
253 -wfHandleThumb404();
 226+/**
 227+ * Print out a generic 404 error message.
 228+ *
 229+ * @return void
 230+ */
 231+function wfDisplay404Error() {
 232+ header( 'HTTP/1.1 404 Not Found' );
 233+ header( 'Content-Type: text/html;charset=utf-8' );
 234+
 235+ $prot = isset( $_SERVER['HTTPS'] ) ? "https://" : "http://";
 236+ $serv = strlen( $_SERVER['HTTP_HOST'] ) ? $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME'];
 237+ $loc = $_SERVER["REQUEST_URI"];
 238+
 239+ $encUrl = htmlspecialchars( $prot . $serv . $loc );
 240+
 241+ // Looks like a typical apache2 error
 242+ $standard_404 = <<<ENDTEXT
 243+<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
 244+<html><head>
 245+<title>404 Not Found</title>
 246+</head><body>
 247+<h1>Not Found</h1>
 248+<p>The requested URL $encUrl was not found on this server.</p>
 249+</body></html>
 250+ENDTEXT;
 251+
 252+ print $standard_404;
 253+}
Index: trunk/phase3/thumb-handler.php5
@@ -0,0 +1 @@
 2+<?php require './thumb-handler.php';

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r100535Added a basic thumb-handler.php file, configured via thumb.config.php. The co...aaron09:36, 23 October 2011

Status & tagging log