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 @@ |
24 | 24 | $thgThumbFragment = "MW_trunk/images/thumb"; |
25 | 25 | # URL regex fragment correspond to the directory hashing of thumbnails. |
26 | 26 | # 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 |
28 | 28 | |
29 | 29 | # The URL to thumb.php, accessible from the web server. |
30 | 30 | $thgThumbScriptPath = "http://localhost/MW_trunk/thumb.php"; |
— | — | @@ -36,7 +36,7 @@ |
37 | 37 | $thgThumbCurlProxy = null; // proxy to thumb.php |
38 | 38 | |
39 | 39 | # File path to a php file the gives a 404 error message |
40 | | -$thgThumb404File = "404.php"; |
| 40 | +$thgThumb404File = null; |
41 | 41 | |
42 | 42 | # Custom functions for overriding aspects of thumb handling |
43 | 43 | $thgThumbCallbacks = array(); |
Index: trunk/phase3/thumb-handler.php |
— | — | @@ -8,10 +8,14 @@ |
9 | 9 | $configPath = dirname( __FILE__ ) . "/thumb.config.php"; |
10 | 10 | if ( !file_exists( $configPath ) ) { |
11 | 11 | 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 |
12 | 14 | } |
13 | 15 | require( $configPath ); |
14 | 16 | |
15 | | -function wfHandleThumb404() { |
| 17 | +wfHandleThumb404Main(); |
| 18 | + |
| 19 | +function wfHandleThumb404Main() { |
16 | 20 | global $thgThumbCallbacks, $thgThumb404File; |
17 | 21 | |
18 | 22 | # lighttpd puts the original request in REQUEST_URI, while |
— | — | @@ -26,37 +30,24 @@ |
27 | 31 | |
28 | 32 | # Extract thumb.php params from the URI... |
29 | 33 | if ( isset( $thgThumbCallbacks['extractParams'] ) |
30 | | - && is_callable( $thgThumbCallbacks['extractParams'] ) ) |
| 34 | + && is_callable( $thgThumbCallbacks['extractParams'] ) ) // overridden by configuration? |
31 | 35 | { |
32 | | - # Overridden by configuration |
33 | 36 | $params = call_user_func_array( $thgThumbCallbacks['extractParams'], array( $uri ) ); |
34 | 37 | } else { |
35 | 38 | $params = wfExtractThumbParams( $uri ); // basic wiki URL param extracting |
36 | 39 | } |
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 ) ) { |
38 | 43 | 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 | + } |
40 | 49 | return; |
41 | 50 | } |
42 | 51 | |
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 | | - |
61 | 52 | # Check any backend caches for the thumbnail... |
62 | 53 | if ( isset( $thgThumbCallbacks['checkCache'] ) |
63 | 54 | && is_callable( $thgThumbCallbacks['checkCache'] ) ) |
— | — | @@ -83,7 +74,6 @@ |
84 | 75 | preg_quote( $thgThumbFragment ) . '(/archive|/temp|)/' . |
85 | 76 | $thgThumbHashFragment . '([^/]*)/(page(\d*)-)*(\d*)px-[^/]*$!'; |
86 | 77 | |
87 | | - # Is this a thumbnail? |
88 | 78 | if ( preg_match( $thumbRegex, $uri, $matches ) ) { |
89 | 79 | list( $all, $archOrTemp, $filename, $pagefull, $pagenum, $size ) = $matches; |
90 | 80 | $params = array( 'f' => $filename, 'width' => $size ); |
— | — | @@ -96,7 +86,7 @@ |
97 | 87 | $params['temp'] = 1; |
98 | 88 | } |
99 | 89 | } else { |
100 | | - $params = null; |
| 90 | + $params = null; // not a valid thumbnail URL |
101 | 91 | } |
102 | 92 | |
103 | 93 | return $params; |
— | — | @@ -112,16 +102,6 @@ |
113 | 103 | function wfStreamThumbViaCurl( array $params, $uri ) { |
114 | 104 | global $thgThumbCallbacks, $thgThumbScriptPath, $thgThumbCurlProxy, $thgThumbCurlTimeout; |
115 | 105 | |
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 | | - |
126 | 106 | # Build up the request URL to use with CURL... |
127 | 107 | $reqURL = "{$thgThumbScriptPath}?"; |
128 | 108 | $first = true; |
— | — | @@ -166,29 +146,28 @@ |
167 | 147 | # Actually make the request |
168 | 148 | $text = curl_exec( $ch ); |
169 | 149 | |
170 | | - # Send it on to the client |
| 150 | + # Send it on to the client... |
171 | 151 | $errno = curl_errno( $ch ); |
172 | 152 | $contentType = curl_getinfo( $ch, CURLINFO_CONTENT_TYPE ); |
173 | 153 | $httpCode = curl_getinfo( $ch, CURLINFO_HTTP_CODE ); |
174 | 154 | if ( $errno ) { |
175 | 155 | header( 'HTTP/1.1 500 Internal server error' ); |
176 | 156 | 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 |
179 | 160 | header( 'HTTP/1.1 304 Not modified' ); |
180 | 161 | $contentType = ''; |
181 | 162 | $text = ''; |
182 | 163 | } elseif ( strval( $text ) == '' ) { |
183 | 164 | header( 'HTTP/1.1 500 Internal server error' ); |
184 | 165 | header( 'Cache-Control: no-cache' ); |
185 | | - list( $text, $contentType ) = wfCurlEmptyText( $ch ); |
| 166 | + $contentType = 'text/html'; |
| 167 | + $text = wfCurlEmptyText( $ch ); |
186 | 168 | } elseif ( $httpCode == 404 ) { |
187 | 169 | header( 'HTTP/1.1 404 Not found' ); |
188 | 170 | 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' ) { |
193 | 172 | # Error message, suppress cache |
194 | 173 | header( 'HTTP/1.1 500 Internal server error' ); |
195 | 174 | header( 'Cache-Control: no-cache' ); |
— | — | @@ -213,40 +192,61 @@ |
214 | 193 | } |
215 | 194 | |
216 | 195 | /** |
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. |
218 | 197 | * |
219 | 198 | * @param $ch cURL handle |
220 | | - * @return Array (error html, content type) |
| 199 | + * @return string |
221 | 200 | */ |
222 | 201 | function wfCurlErrorText( $ch ) { |
223 | | - $contentType = 'text/html'; |
224 | 202 | $error = htmlspecialchars( curl_error( $ch ) ); |
225 | | - $text = <<<EOT |
| 203 | + return <<<EOT |
226 | 204 | <html> |
227 | 205 | <head><title>Thumbnail error</title></head> |
228 | 206 | <body>Error retrieving thumbnail from scaling server: $error</body> |
229 | 207 | </html> |
230 | 208 | EOT; |
231 | | - return array( $text, $contentType ); |
232 | 209 | } |
233 | 210 | |
234 | 211 | /** |
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. |
236 | 213 | * |
237 | 214 | * @param $ch cURL handle |
238 | | - * @return Array (error html, content type) |
| 215 | + * @return string |
239 | 216 | */ |
240 | 217 | function wfCurlEmptyText( $ch ) { |
241 | | - $contentType = 'text/html'; |
242 | | - $error = htmlspecialchars( curl_error( $ch ) ); |
243 | | - $text = <<<EOT |
| 218 | + return <<<EOT |
244 | 219 | <html> |
245 | 220 | <head><title>Thumbnail error</title></head> |
246 | 221 | <body>Error retrieving thumbnail from scaling server: empty response</body> |
247 | 222 | </html> |
248 | 223 | EOT; |
249 | | - return array( $text, $contentType ); |
250 | 224 | } |
251 | 225 | |
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';
|