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