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