Handle missing width nicely in thumb.php
[lhc/web/wiklou.git] / thumb.php
1 <?php
2 /**
3 * PHP script to stream out an image thumbnail.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Media
22 */
23
24 define( 'MW_NO_OUTPUT_COMPRESSION', 1 );
25 require __DIR__ . '/includes/WebStart.php';
26
27 // Don't use fancy MIME detection, just check the file extension for jpg/gif/png
28 $wgTrivialMimeDetection = true;
29
30 if ( defined( 'THUMB_HANDLER' ) ) {
31 // Called from thumb_handler.php via 404; extract params from the URI...
32 wfThumbHandle404();
33 } else {
34 // Called directly, use $_GET params
35 wfStreamThumb( $_GET );
36 }
37
38 wfLogProfilingData();
39 // Commit and close up!
40 $factory = wfGetLBFactory();
41 $factory->commitMasterChanges();
42 $factory->shutdown();
43
44 //--------------------------------------------------------------------------
45
46 /**
47 * Handle a thumbnail request via thumbnail file URL
48 *
49 * @return void
50 */
51 function wfThumbHandle404() {
52 global $wgArticlePath;
53
54 # Set action base paths so that WebRequest::getPathInfo()
55 # recognizes the "X" as the 'title' in ../thumb_handler.php/X urls.
56 # Note: If Custom per-extension repo paths are set, this may break.
57 $repo = RepoGroup::singleton()->getLocalRepo();
58 $oldArticlePath = $wgArticlePath;
59 $wgArticlePath = $repo->getZoneUrl( 'thumb' ) . '/$1';
60
61 $matches = WebRequest::getPathInfo();
62
63 $wgArticlePath = $oldArticlePath;
64
65 if ( !isset( $matches['title'] ) ) {
66 wfThumbError( 404, 'Could not determine the name of the requested thumbnail.' );
67 return;
68 }
69
70 $params = wfExtractThumbRequestInfo( $matches['title'] ); // basic wiki URL param extracting
71 if ( $params == null ) {
72 wfThumbError( 400, 'The specified thumbnail parameters are not recognized.' );
73 return;
74 }
75
76 wfStreamThumb( $params ); // stream the thumbnail
77 }
78
79 /**
80 * Stream a thumbnail specified by parameters
81 *
82 * @param array $params List of thumbnailing parameters. In addition to parameters
83 * passed to the MediaHandler, this may also includes the keys:
84 * f (for filename), archived (if archived file), temp (if temp file),
85 * w (alias for width), p (alias for page), r (ignored; historical),
86 * rel404 (path for render on 404 to verify hash path correct),
87 * thumbName (thumbnail name to potentially extract more parameters from
88 * e.g. 'lossy-page1-120px-Foo.tiff' would add page, lossy and width
89 * to the parameters)
90 * @return void
91 */
92 function wfStreamThumb( array $params ) {
93 global $wgVaryOnXFP;
94
95
96 $headers = array(); // HTTP headers to send
97
98 $fileName = isset( $params['f'] ) ? $params['f'] : '';
99
100 // Backwards compatibility parameters
101 if ( isset( $params['w'] ) ) {
102 $params['width'] = $params['w'];
103 unset( $params['w'] );
104 }
105 if ( isset( $params['width'] ) && substr( $params['width'], -2 ) == 'px' ) {
106 // strip the px (pixel) suffix, if found
107 $params['width'] = substr( $params['width'], 0, -2 );
108 }
109 if ( isset( $params['p'] ) ) {
110 $params['page'] = $params['p'];
111 }
112
113 // Is this a thumb of an archived file?
114 $isOld = ( isset( $params['archived'] ) && $params['archived'] );
115 unset( $params['archived'] ); // handlers don't care
116
117 // Is this a thumb of a temp file?
118 $isTemp = ( isset( $params['temp'] ) && $params['temp'] );
119 unset( $params['temp'] ); // handlers don't care
120
121 // Some basic input validation
122 $fileName = strtr( $fileName, '\\/', '__' );
123
124 // Actually fetch the image. Method depends on whether it is archived or not.
125 if ( $isTemp ) {
126 $repo = RepoGroup::singleton()->getLocalRepo()->getTempRepo();
127 $img = new UnregisteredLocalFile( null, $repo,
128 # Temp files are hashed based on the name without the timestamp.
129 # The thumbnails will be hashed based on the entire name however.
130 # @todo fix this convention to actually be reasonable.
131 $repo->getZonePath( 'public' ) . '/' . $repo->getTempHashPath( $fileName ) . $fileName
132 );
133 } elseif ( $isOld ) {
134 // Format is <timestamp>!<name>
135 $bits = explode( '!', $fileName, 2 );
136 if ( count( $bits ) != 2 ) {
137 wfThumbError( 404, wfMessage( 'badtitletext' )->parse() );
138 return;
139 }
140 $title = Title::makeTitleSafe( NS_FILE, $bits[1] );
141 if ( !$title ) {
142 wfThumbError( 404, wfMessage( 'badtitletext' )->parse() );
143 return;
144 }
145 $img = RepoGroup::singleton()->getLocalRepo()->newFromArchiveName( $title, $fileName );
146 } else {
147 $img = wfLocalFile( $fileName );
148 }
149
150 // Check the source file title
151 if ( !$img ) {
152 wfThumbError( 404, wfMessage( 'badtitletext' )->parse() );
153 return;
154 }
155
156 // Check permissions if there are read restrictions
157 $varyHeader = array();
158 if ( !in_array( 'read', User::getGroupPermissions( array( '*' ) ), true ) ) {
159 if ( !$img->getTitle() || !$img->getTitle()->userCan( 'read' ) ) {
160 wfThumbError( 403, 'Access denied. You do not have permission to access ' .
161 'the source file.' );
162 return;
163 }
164 $headers[] = 'Cache-Control: private';
165 $varyHeader[] = 'Cookie';
166 }
167
168 // Check if the file is hidden
169 if ( $img->isDeleted( File::DELETED_FILE ) ) {
170 wfThumbError( 404, "The source file '$fileName' does not exist." );
171 return;
172 }
173
174 // Do rendering parameters extraction from thumbnail name.
175 if ( isset( $params['thumbName'] ) ) {
176 $params = wfExtractThumbParams( $img, $params );
177 }
178 if ( $params == null ) {
179 wfThumbError( 400, 'The specified thumbnail parameters are not recognized.' );
180 return;
181 }
182
183 // Check the source file storage path
184 if ( !$img->exists() ) {
185 $redirectedLocation = false;
186 if ( !$isTemp ) {
187 // Check for file redirect
188 // Since redirects are associated with pages, not versions of files,
189 // we look for the most current version to see if its a redirect.
190 $possRedirFile = RepoGroup::singleton()->getLocalRepo()->findFile( $img->getName() );
191 if ( $possRedirFile && !is_null( $possRedirFile->getRedirected() ) ) {
192 $redirTarget = $possRedirFile->getName();
193 $targetFile = wfLocalFile( Title::makeTitleSafe( NS_FILE, $redirTarget ) );
194 if ( $targetFile->exists() ) {
195 $newThumbName = $targetFile->thumbName( $params );
196 if ( $isOld ) {
197 $newThumbUrl = $targetFile->getArchiveThumbUrl(
198 $bits[0] . '!' . $targetFile->getName(), $newThumbName );
199 } else {
200 $newThumbUrl = $targetFile->getThumbUrl( $newThumbName );
201 }
202 $redirectedLocation = wfExpandUrl( $newThumbUrl, PROTO_CURRENT );
203 }
204 }
205 }
206
207 if ( $redirectedLocation ) {
208 // File has been moved. Give redirect.
209 $response = RequestContext::getMain()->getRequest()->response();
210 $response->header( "HTTP/1.1 302 " . HttpStatus::getMessage( 302 ) );
211 $response->header( 'Location: ' . $redirectedLocation );
212 $response->header( 'Expires: ' .
213 gmdate( 'D, d M Y H:i:s', time() + 12 * 3600 ) . ' GMT' );
214 if ( $wgVaryOnXFP ) {
215 $varyHeader[] = 'X-Forwarded-Proto';
216 }
217 if ( count( $varyHeader ) ) {
218 $response->header( 'Vary: ' . implode( ', ', $varyHeader ) );
219 }
220 return;
221 }
222
223 // If its not a redirect that has a target as a local file, give 404.
224 wfThumbError( 404, "The source file '$fileName' does not exist." );
225 return;
226 } elseif ( $img->getPath() === false ) {
227 wfThumbError( 500, "The source file '$fileName' is not locally accessible." );
228 return;
229 }
230
231 // Check IMS against the source file
232 // This means that clients can keep a cached copy even after it has been deleted on the server
233 if ( !empty( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ) {
234 // Fix IE brokenness
235 $imsString = preg_replace( '/;.*$/', '', $_SERVER["HTTP_IF_MODIFIED_SINCE"] );
236 // Calculate time
237 wfSuppressWarnings();
238 $imsUnix = strtotime( $imsString );
239 wfRestoreWarnings();
240 if ( wfTimestamp( TS_UNIX, $img->getTimestamp() ) <= $imsUnix ) {
241 header( 'HTTP/1.1 304 Not Modified' );
242 return;
243 }
244 }
245
246 $rel404 = isset( $params['rel404'] ) ? $params['rel404'] : null;
247 unset( $params['r'] ); // ignore 'r' because we unconditionally pass File::RENDER
248 unset( $params['f'] ); // We're done with 'f' parameter.
249 unset( $params['rel404'] ); // moved to $rel404
250
251 // Get the normalized thumbnail name from the parameters...
252 try {
253 $thumbName = $img->thumbName( $params );
254 if ( !strlen( $thumbName ) ) { // invalid params?
255 throw new MediaTransformInvalidParametersException( 'Empty return from File::thumbName' );
256 }
257 $thumbName2 = $img->thumbName( $params, File::THUMB_FULL_NAME ); // b/c; "long" style
258 } catch ( MediaTransformInvalidParametersException $e ) {
259 wfThumbError( 400, 'The specified thumbnail parameters are not valid: ' . $e->getMessage() );
260 return;
261 } catch ( MWException $e ) {
262 wfThumbError( 500, $e->getHTML() );
263 return;
264 }
265
266 // For 404 handled thumbnails, we only use the base name of the URI
267 // for the thumb params and the parent directory for the source file name.
268 // Check that the zone relative path matches up so squid caches won't pick
269 // up thumbs that would not be purged on source file deletion (bug 34231).
270 if ( $rel404 !== null ) { // thumbnail was handled via 404
271 if ( rawurldecode( $rel404 ) === $img->getThumbRel( $thumbName ) ) {
272 // Request for the canonical thumbnail name
273 } elseif ( rawurldecode( $rel404 ) === $img->getThumbRel( $thumbName2 ) ) {
274 // Request for the "long" thumbnail name; redirect to canonical name
275 $response = RequestContext::getMain()->getRequest()->response();
276 $response->header( "HTTP/1.1 301 " . HttpStatus::getMessage( 301 ) );
277 $response->header( 'Location: ' .
278 wfExpandUrl( $img->getThumbUrl( $thumbName ), PROTO_CURRENT ) );
279 $response->header( 'Expires: ' .
280 gmdate( 'D, d M Y H:i:s', time() + 7 * 86400 ) . ' GMT' );
281 if ( $wgVaryOnXFP ) {
282 $varyHeader[] = 'X-Forwarded-Proto';
283 }
284 if ( count( $varyHeader ) ) {
285 $response->header( 'Vary: ' . implode( ', ', $varyHeader ) );
286 }
287 return;
288 } else {
289 wfThumbError( 404, "The given path of the specified thumbnail is incorrect;
290 expected '" . $img->getThumbRel( $thumbName ) . "' but got '" .
291 rawurldecode( $rel404 ) . "'." );
292 return;
293 }
294 }
295
296 $dispositionType = isset( $params['download'] ) ? 'attachment' : 'inline';
297
298 // Suggest a good name for users downloading this thumbnail
299 $headers[] = "Content-Disposition: {$img->getThumbDisposition( $thumbName, $dispositionType )}";
300
301 if ( count( $varyHeader ) ) {
302 $headers[] = 'Vary: ' . implode( ', ', $varyHeader );
303 }
304
305 // Stream the file if it exists already...
306 $thumbPath = $img->getThumbPath( $thumbName );
307 if ( $img->getRepo()->fileExists( $thumbPath ) ) {
308 $img->getRepo()->streamFile( $thumbPath, $headers );
309 return;
310 }
311
312 $user = RequestContext::getMain()->getUser();
313 if ( !wfThumbIsStandard( $img, $params ) && $user->pingLimiter( 'renderfile-nonstandard' ) ) {
314 wfThumbError( 500, wfMessage( 'actionthrottledtext' )->parse() );
315 return;
316 } elseif ( $user->pingLimiter( 'renderfile' ) ) {
317 wfThumbError( 500, wfMessage( 'actionthrottledtext' )->parse() );
318 return;
319 }
320
321 // Actually generate a new thumbnail
322 list( $thumb, $errorMsg ) = wfGenerateThumbnail( $img, $params, $thumbName, $thumbPath );
323
324 // Check for thumbnail generation errors...
325 $msg = wfMessage( 'thumbnail_error' );
326 $errorCode = 500;
327 if ( !$thumb ) {
328 $errorMsg = $errorMsg ?: $msg->rawParams( 'File::transform() returned false' )->escaped();
329 } elseif ( $thumb->isError() ) {
330 $errorMsg = $thumb->getHtmlMsg();
331 } elseif ( !$thumb->hasFile() ) {
332 $errorMsg = $msg->rawParams( 'No path supplied in thumbnail object' )->escaped();
333 } elseif ( $thumb->fileIsSource() ) {
334 $errorMsg = $msg->
335 rawParams( 'Image was not scaled, is the requested width bigger than the source?' )->escaped();
336 $errorCode = 400;
337 }
338
339 if ( $errorMsg !== false ) {
340 wfThumbError( $errorCode, $errorMsg );
341 } else {
342 // Stream the file if there were no errors
343 $thumb->streamFile( $headers );
344 }
345 }
346
347 /**
348 * Actually try to generate a new thumbnail
349 *
350 * @param File $file
351 * @param array $params
352 * @param string $thumbName
353 * @param string $thumbPath
354 * @return array (MediaTransformOutput|bool, string|bool error message HTML)
355 */
356 function wfGenerateThumbnail( File $file, array $params, $thumbName, $thumbPath ) {
357 global $wgMemc, $wgAttemptFailureEpoch;
358
359 $key = wfMemcKey( 'attempt-failures', $wgAttemptFailureEpoch,
360 $file->getRepo()->getName(), $file->getSha1(), md5( $thumbName ) );
361
362 // Check if this file keeps failing to render
363 if ( $wgMemc->get( $key ) >= 4 ) {
364 return array( false, wfMessage( 'thumbnail_image-failure-limit', 4 ) );
365 }
366
367 $done = false;
368 // Record failures on PHP fatals in addition to caching exceptions
369 register_shutdown_function( function () use ( &$done, $key ) {
370 if ( !$done ) { // transform() gave a fatal
371 global $wgMemc;
372 // Randomize TTL to reduce stampedes
373 $wgMemc->incrWithInit( $key, 3600 + mt_rand( 0, 300 ) );
374 }
375 } );
376
377 $thumb = false;
378 $errorHtml = false;
379
380 // guard thumbnail rendering with PoolCounter to avoid stampedes
381 // expensive files use a separate PoolCounter config so it is possible
382 // to set up a global limit on them
383 if ( $file->isExpensiveToThumbnail() ) {
384 $poolCounterType = 'FileRenderExpensive';
385 } else {
386 $poolCounterType = 'FileRender';
387 }
388
389 // Thumbnail isn't already there, so create the new thumbnail...
390 try {
391 $work = new PoolCounterWorkViaCallback( $poolCounterType, sha1( $file->getName() ),
392 array(
393 'doWork' => function () use ( $file, $params ) {
394 return $file->transform( $params, File::RENDER_NOW );
395 },
396 'getCachedWork' => function () use ( $file, $params, $thumbPath ) {
397 // If the worker that finished made this thumbnail then use it.
398 // Otherwise, it probably made a different thumbnail for this file.
399 return $file->getRepo()->fileExists( $thumbPath )
400 ? $file->transform( $params, File::RENDER_NOW )
401 : false; // retry once more in exclusive mode
402 },
403 'fallback' => function () {
404 return wfMessage( 'generic-pool-error' )->parse();
405 },
406 'error' => function ( $status ) {
407 return $status->getHTML();
408 }
409 )
410 );
411 $result = $work->execute();
412 if ( $result instanceof MediaTransformOutput ) {
413 $thumb = $result;
414 } elseif ( is_string( $result ) ) { // error
415 $errorHtml = $result;
416 }
417 } catch ( Exception $e ) {
418 // Tried to select a page on a non-paged file?
419 }
420
421 $done = true; // no PHP fatal occured
422
423 if ( !$thumb || $thumb->isError() ) {
424 // Randomize TTL to reduce stampedes
425 $wgMemc->incrWithInit( $key, 3600 + mt_rand( 0, 300 ) );
426 }
427
428 return array( $thumb, $errorHtml );
429 }
430
431 /**
432 * Returns true if this thumbnail is one that MediaWiki generates
433 * links to on file description pages and possibly parser output.
434 *
435 * $params is considered non-standard if they involve a non-standard
436 * width or any non-default parameters aside from width and page number.
437 * The number of possible files with standard parameters is far less than
438 * that of all combinations; rate-limiting for them can thus be more generious.
439 *
440 * @param File $file
441 * @param array $params
442 * @return bool
443 */
444 function wfThumbIsStandard( File $file, array $params ) {
445 global $wgThumbLimits, $wgImageLimits;
446
447 $handler = $file->getHandler();
448 if ( !$handler || !isset( $params['width'] ) ) {
449 return false;
450 }
451
452 $basicParams = array();
453 if ( isset( $params['page'] ) ) {
454 $basicParams['page'] = $params['page'];
455 }
456
457 // Check if the width matches one of $wgThumbLimits
458 if ( in_array( $params['width'], $wgThumbLimits ) ) {
459 $normalParams = $basicParams + array( 'width' => $params['width'] );
460 // Append any default values to the map (e.g. "lossy", "lossless", ...)
461 $handler->normaliseParams( $file, $normalParams );
462 } else {
463 // If not, then check if the width matchs one of $wgImageLimits
464 $match = false;
465 foreach ( $wgImageLimits as $pair ) {
466 $normalParams = $basicParams + array( 'width' => $pair[0], 'height' => $pair[1] );
467 // Decide whether the thumbnail should be scaled on width or height.
468 // Also append any default values to the map (e.g. "lossy", "lossless", ...)
469 $handler->normaliseParams( $file, $normalParams );
470 // Check if this standard thumbnail size maps to the given width
471 if ( $normalParams['width'] == $params['width'] ) {
472 $match = true;
473 break;
474 }
475 }
476 if ( !$match ) {
477 return false; // not standard for description pages
478 }
479 }
480
481 // Check that the given values for non-page, non-width, params are just defaults
482 foreach ( $params as $key => $value ) {
483 if ( !isset( $normalParams[$key] ) || $normalParams[$key] != $value ) {
484 return false;
485 }
486 }
487
488 return true;
489 }
490
491 /**
492 * Convert pathinfo type parameter, into normal request parameters
493 *
494 * So for example, if the request was redirected from
495 * /w/images/thumb/a/ab/Foo.png/120px-Foo.png. The $thumbRel parameter
496 * of this function would be set to "a/ab/Foo.png/120px-Foo.png".
497 * This method is responsible for turning that into an array
498 * with the folowing keys:
499 * * f => the filename (Foo.png)
500 * * rel404 => the whole thing (a/ab/Foo.png/120px-Foo.png)
501 * * archived => 1 (If the request is for an archived thumb)
502 * * temp => 1 (If the file is in the "temporary" zone)
503 * * thumbName => the thumbnail name, including parameters (120px-Foo.png)
504 *
505 * Transform specific parameters are set later via wfExtractThumbParams().
506 *
507 * @param string $thumbRel Thumbnail path relative to the thumb zone
508 * @return array|null Associative params array or null
509 */
510 function wfExtractThumbRequestInfo( $thumbRel ) {
511 $repo = RepoGroup::singleton()->getLocalRepo();
512
513 $hashDirReg = $subdirReg = '';
514 $hashLevels = $repo->getHashLevels();
515 for ( $i = 0; $i < $hashLevels; $i++ ) {
516 $subdirReg .= '[0-9a-f]';
517 $hashDirReg .= "$subdirReg/";
518 }
519
520 // Check if this is a thumbnail of an original in the local file repo
521 if ( preg_match( "!^((archive/)?$hashDirReg([^/]*)/([^/]*))$!", $thumbRel, $m ) ) {
522 list( /*all*/, $rel, $archOrTemp, $filename, $thumbname ) = $m;
523 // Check if this is a thumbnail of an temp file in the local file repo
524 } elseif ( preg_match( "!^(temp/)($hashDirReg([^/]*)/([^/]*))$!", $thumbRel, $m ) ) {
525 list( /*all*/, $archOrTemp, $rel, $filename, $thumbname ) = $m;
526 } else {
527 return null; // not a valid looking thumbnail request
528 }
529
530 $params = array( 'f' => $filename, 'rel404' => $rel );
531 if ( $archOrTemp === 'archive/' ) {
532 $params['archived'] = 1;
533 } elseif ( $archOrTemp === 'temp/' ) {
534 $params['temp'] = 1;
535 }
536
537 $params['thumbName'] = $thumbname;
538 return $params;
539 }
540
541 /**
542 * Convert a thumbnail name (122px-foo.png) to parameters, using
543 * file handler.
544 *
545 * @param File $file File object for file in question
546 * @param array $params Array of parameters so far
547 * @return array Parameters array with more parameters
548 */
549 function wfExtractThumbParams( $file, $params ) {
550 if ( !isset( $params['thumbName'] ) ) {
551 throw new MWException( "No thumbnail name passed to wfExtractThumbParams" );
552 }
553
554 $thumbname = $params['thumbName'];
555 unset( $params['thumbName'] );
556
557 // Do the hook first for older extensions that rely on it.
558 if ( !wfRunHooks( 'ExtractThumbParameters', array( $thumbname, &$params ) ) ) {
559 // Check hooks if parameters can be extracted
560 // Hooks return false if they manage to *resolve* the parameters
561 // This hook should be considered deprecated
562 wfDeprecated( 'ExtractThumbParameters', '1.22' );
563 return $params; // valid thumbnail URL (via extension or config)
564 }
565
566 // FIXME: Files in the temp zone don't set a MIME type, which means
567 // they don't have a handler. Which means we can't parse the param
568 // string. However, not a big issue as what good is a param string
569 // if you have no handler to make use of the param string and
570 // actually generate the thumbnail.
571 $handler = $file->getHandler();
572
573 // Based on UploadStash::parseKey
574 $fileNamePos = strrpos( $thumbname, $params['f'] );
575 if ( $fileNamePos === false ) {
576 // Maybe using a short filename? (see FileRepo::nameForThumb)
577 $fileNamePos = strrpos( $thumbname, 'thumbnail' );
578 }
579
580 if ( $handler && $fileNamePos !== false ) {
581 $paramString = substr( $thumbname, 0, $fileNamePos - 1 );
582 $extraParams = $handler->parseParamString( $paramString );
583 if ( $extraParams !== false ) {
584 return $params + $extraParams;
585 }
586 }
587
588 // As a last ditch fallback, use the traditional common parameters
589 if ( preg_match( '!^(page(\d*)-)*(\d*)px-[^/]*$!', $thumbname, $matches ) ) {
590 list( /* all */, $pagefull, $pagenum, $size ) = $matches;
591 $params['width'] = $size;
592 if ( $pagenum ) {
593 $params['page'] = $pagenum;
594 }
595 return $params; // valid thumbnail URL
596 }
597 return null;
598 }
599
600 /**
601 * Output a thumbnail generation error message
602 *
603 * @param int $status
604 * @param string $msg HTML
605 * @return void
606 */
607 function wfThumbError( $status, $msg ) {
608 global $wgShowHostnames;
609
610 header( 'Cache-Control: no-cache' );
611 header( 'Content-Type: text/html; charset=utf-8' );
612 if ( $status == 400 ) {
613 header( 'HTTP/1.1 400 Bad request' );
614 } elseif ( $status == 404 ) {
615 header( 'HTTP/1.1 404 Not found' );
616 } elseif ( $status == 403 ) {
617 header( 'HTTP/1.1 403 Forbidden' );
618 header( 'Vary: Cookie' );
619 } else {
620 header( 'HTTP/1.1 500 Internal server error' );
621 }
622 if ( $wgShowHostnames ) {
623 header( 'X-MW-Thumbnail-Renderer: ' . wfHostname() );
624 $url = htmlspecialchars( isset( $_SERVER['REQUEST_URI'] ) ? $_SERVER['REQUEST_URI'] : '' );
625 $hostname = htmlspecialchars( wfHostname() );
626 $debug = "<!-- $url -->\n<!-- $hostname -->\n";
627 } else {
628 $debug = '';
629 }
630 echo <<<EOT
631 <html><head><title>Error generating thumbnail</title></head>
632 <body>
633 <h1>Error generating thumbnail</h1>
634 <p>
635 $msg
636 </p>
637 $debug
638 </body>
639 </html>
640
641 EOT;
642 }