Merge "Avoid image table updates on file upload failure"
[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 permissions if there are read restrictions
163 $varyHeader = array();
164 if ( !in_array( 'read', User::getGroupPermissions( array( '*' ) ), true ) ) {
165 if ( !$img->getTitle() || !$img->getTitle()->userCan( 'read' ) ) {
166 wfThumbError( 403, 'Access denied. You do not have permission to access ' .
167 'the source file.' );
168 return;
169 }
170 $headers[] = 'Cache-Control: private';
171 $varyHeader[] = 'Cookie';
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 wfThumbError( 400, 'The specified thumbnail parameters are not valid.' );
256 return;
257 }
258 $thumbName2 = $img->thumbName( $params, File::THUMB_FULL_NAME ); // b/c; "long" style
259 } catch ( MWException $e ) {
260 wfThumbError( 500, $e->getHTML() );
261 return;
262 }
263
264 // For 404 handled thumbnails, we only use the the base name of the URI
265 // for the thumb params and the parent directory for the source file name.
266 // Check that the zone relative path matches up so squid caches won't pick
267 // up thumbs that would not be purged on source file deletion (bug 34231).
268 if ( $rel404 !== null ) { // thumbnail was handled via 404
269 if ( rawurldecode( $rel404 ) === $img->getThumbRel( $thumbName ) ) {
270 // Request for the canonical thumbnail name
271 } elseif ( rawurldecode( $rel404 ) === $img->getThumbRel( $thumbName2 ) ) {
272 // Request for the "long" thumbnail name; redirect to canonical name
273 $response = RequestContext::getMain()->getRequest()->response();
274 $response->header( "HTTP/1.1 301 " . HttpStatus::getMessage( 301 ) );
275 $response->header( 'Location: ' .
276 wfExpandUrl( $img->getThumbUrl( $thumbName ), PROTO_CURRENT ) );
277 $response->header( 'Expires: ' .
278 gmdate( 'D, d M Y H:i:s', time() + 7 * 86400 ) . ' GMT' );
279 if ( $wgVaryOnXFP ) {
280 $varyHeader[] = 'X-Forwarded-Proto';
281 }
282 if ( count( $varyHeader ) ) {
283 $response->header( 'Vary: ' . implode( ', ', $varyHeader ) );
284 }
285 return;
286 } else {
287 wfThumbError( 404, "The given path of the specified thumbnail is incorrect;
288 expected '" . $img->getThumbRel( $thumbName ) . "' but got '" .
289 rawurldecode( $rel404 ) . "'." );
290 return;
291 }
292 }
293
294 $dispositionType = isset( $params['download'] ) ? 'attachment' : 'inline';
295
296 // Suggest a good name for users downloading this thumbnail
297 $headers[] = "Content-Disposition: {$img->getThumbDisposition( $thumbName, $dispositionType )}";
298
299 if ( count( $varyHeader ) ) {
300 $headers[] = 'Vary: ' . implode( ', ', $varyHeader );
301 }
302
303 // Stream the file if it exists already...
304 $thumbPath = $img->getThumbPath( $thumbName );
305 if ( $img->getRepo()->fileExists( $thumbPath ) ) {
306 $img->getRepo()->streamFile( $thumbPath, $headers );
307 return;
308 }
309
310 $user = RequestContext::getMain()->getUser();
311 if ( !wfThumbIsStandard( $img, $params ) && $user->pingLimiter( 'renderfile-nonstandard' ) ) {
312 wfThumbError( 500, wfMessage( 'actionthrottledtext' ) );
313 return;
314 } elseif ( $user->pingLimiter( 'renderfile' ) ) {
315 wfThumbError( 500, wfMessage( 'actionthrottledtext' ) );
316 return;
317 } elseif ( wfThumbIsAttemptThrottled( $img, $thumbName, 5 ) ) {
318 wfThumbError( 500, wfMessage( 'thumbnail_image-failure-limit', 5 ) );
319 return;
320 }
321
322 // Thumbnail isn't already there, so create the new thumbnail...
323 $thumb = null;
324 try {
325 // Record failures on PHP fatals too
326 register_shutdown_function( function() use ( &$thumb, $img, $thumbName ) {
327 if ( $thumb === null ) { // transform() gave a fatal
328 wfThumbIncrAttemptFailures( $img, $thumbName );
329 }
330 } );
331 $thumb = $img->transform( $params, File::RENDER_NOW );
332 } catch ( Exception $ex ) {
333 // Tried to select a page on a non-paged file?
334 $thumb = false;
335 }
336
337 // Check for thumbnail generation errors...
338 $errorMsg = false;
339 $msg = wfMessage( 'thumbnail_error' );
340 if ( !$thumb ) {
341 $errorMsg = $msg->rawParams( 'File::transform() returned false' )->escaped();
342 } elseif ( $thumb->isError() ) {
343 $errorMsg = $thumb->getHtmlMsg();
344 } elseif ( !$thumb->hasFile() ) {
345 $errorMsg = $msg->rawParams( 'No path supplied in thumbnail object' )->escaped();
346 } elseif ( $thumb->fileIsSource() ) {
347 $errorMsg = $msg->
348 rawParams( 'Image was not scaled, is the requested width bigger than the source?' )->escaped();
349 }
350
351 if ( $errorMsg !== false ) {
352 wfThumbIncrAttemptFailures( $img, $thumbName );
353 wfThumbError( 500, $errorMsg );
354 } else {
355 // Stream the file if there were no errors
356 $thumb->streamFile( $headers );
357 }
358 }
359
360 /**
361 * Returns true if this thumbnail is one that MediaWiki generates
362 * links to on file description pages and possibly parser output.
363 *
364 * $params is considered non-standard if they involve a non-standard
365 * width or any parameter aside from width and page number. The number
366 * of possible files with standard parameters is far less than that of all
367 * possible combinations; rate-limiting for them can thus be more generious.
368 *
369 * @param File $img
370 * @param array $params
371 * @return bool
372 */
373 function wfThumbIsStandard( File $img, array $params ) {
374 global $wgThumbLimits, $wgImageLimits;
375 // @TODO: use polymorphism with media handler here
376 if ( array_diff( array_keys( $params ), array( 'width', 'page' ) ) ) {
377 return false; // extra parameters present
378 }
379 if ( isset( $params['width'] ) ) {
380 $widths = $wgThumbLimits;
381 foreach ( $wgImageLimits as $pair ) {
382 $widths[] = $pair[0];
383 }
384 if ( !in_array( $params['width'], $widths ) ) {
385 return false;
386 }
387 }
388 return true;
389 }
390
391 /**
392 * @param File $img
393 * @param string $thumbName
394 * @param int $limit
395 * @return int|bool
396 */
397 function wfThumbIsAttemptThrottled( File $img, $thumbName, $limit ) {
398 global $wgMemc;
399
400 return ( $wgMemc->get( wfThumbAttemptKey( $img, $thumbName ) ) >= $limit );
401 }
402
403 /**
404 * @param File $img
405 * @param string $thumbName
406 */
407 function wfThumbIncrAttemptFailures( File $img, $thumbName ) {
408 global $wgMemc;
409
410 $key = wfThumbAttemptKey( $img, $thumbName );
411 if ( !$wgMemc->incr( $key, 1 ) ) {
412 if ( !$wgMemc->add( $key, 1, 3600 ) ) {
413 $wgMemc->incr( $key, 1 );
414 }
415 }
416 }
417
418 /**
419 * @param File $img
420 * @param string $thumbName
421 * @return string
422 */
423 function wfThumbAttemptKey( File $img, $thumbName ) {
424 global $wgAttemptFailureEpoch;
425
426 return wfMemcKey( 'attempt-failures', $wgAttemptFailureEpoch,
427 $img->getRepo()->getName(), md5( $img->getName() ), md5( $thumbName ) );
428 }
429
430 /**
431 * Convert pathinfo type parameter, into normal request parameters
432 *
433 * So for example, if the request was redirected from
434 * /w/images/thumb/a/ab/Foo.png/120px-Foo.png. The $thumbRel parameter
435 * of this function would be set to "a/ab/Foo.png/120px-Foo.png".
436 * This method is responsible for turning that into an array
437 * with the folowing keys:
438 * * f => the filename (Foo.png)
439 * * rel404 => the whole thing (a/ab/Foo.png/120px-Foo.png)
440 * * archived => 1 (If the request is for an archived thumb)
441 * * temp => 1 (If the file is in the "temporary" zone)
442 * * thumbName => the thumbnail name, including parameters (120px-Foo.png)
443 *
444 * Transform specific parameters are set later via wfExtractThumbParams().
445 *
446 * @param string $thumbRel Thumbnail path relative to the thumb zone
447 * @return array|null Associative params array or null
448 */
449 function wfExtractThumbRequestInfo( $thumbRel ) {
450 $repo = RepoGroup::singleton()->getLocalRepo();
451
452 $hashDirReg = $subdirReg = '';
453 for ( $i = 0; $i < $repo->getHashLevels(); $i++ ) {
454 $subdirReg .= '[0-9a-f]';
455 $hashDirReg .= "$subdirReg/";
456 }
457
458 // Check if this is a thumbnail of an original in the local file repo
459 if ( preg_match( "!^((archive/)?$hashDirReg([^/]*)/([^/]*))$!", $thumbRel, $m ) ) {
460 list( /*all*/, $rel, $archOrTemp, $filename, $thumbname ) = $m;
461 // Check if this is a thumbnail of an temp file in the local file repo
462 } elseif ( preg_match( "!^(temp/)($hashDirReg([^/]*)/([^/]*))$!", $thumbRel, $m ) ) {
463 list( /*all*/, $archOrTemp, $rel, $filename, $thumbname ) = $m;
464 } else {
465 return null; // not a valid looking thumbnail request
466 }
467
468 $params = array( 'f' => $filename, 'rel404' => $rel );
469 if ( $archOrTemp === 'archive/' ) {
470 $params['archived'] = 1;
471 } elseif ( $archOrTemp === 'temp/' ) {
472 $params['temp'] = 1;
473 }
474
475 $params['thumbName'] = $thumbname;
476 return $params;
477 }
478
479 /**
480 * Convert a thumbnail name (122px-foo.png) to parameters, using
481 * file handler.
482 *
483 * @param File $file File object for file in question
484 * @param array $param Array of parameters so far
485 * @return array Parameters array with more parameters
486 */
487 function wfExtractThumbParams( $file, $params ) {
488 if ( !isset( $params['thumbName'] ) ) {
489 throw new MWException( "No thumbnail name passed to wfExtractThumbParams" );
490 }
491
492 $thumbname = $params['thumbName'];
493 unset( $params['thumbName'] );
494
495 // Do the hook first for older extensions that rely on it.
496 if ( !wfRunHooks( 'ExtractThumbParameters', array( $thumbname, &$params ) ) ) {
497 // Check hooks if parameters can be extracted
498 // Hooks return false if they manage to *resolve* the parameters
499 // This hook should be considered deprecated
500 wfDeprecated( 'ExtractThumbParameters', '1.22' );
501 return $params; // valid thumbnail URL (via extension or config)
502 }
503
504 // FIXME: Files in the temp zone don't set a mime type, which means
505 // they don't have a handler. Which means we can't parse the param
506 // string. However, not a big issue as what good is a param string
507 // if you have no handler to make use of the param string and
508 // actually generate the thumbnail.
509 $handler = $file->getHandler();
510
511 // Based on UploadStash::parseKey
512 $fileNamePos = strrpos( $thumbname, $params['f'] );
513 if ( $fileNamePos === false ) {
514 // Maybe using a short filename? (see FileRepo::nameForThumb)
515 $fileNamePos = strrpos( $thumbname, 'thumbnail' );
516 }
517
518 if ( $handler && $fileNamePos !== false ) {
519 $paramString = substr( $thumbname, 0, $fileNamePos - 1 );
520 $extraParams = $handler->parseParamString( $paramString );
521 if ( $extraParams !== false ) {
522 return $params + $extraParams;
523 }
524 }
525
526 // As a last ditch fallback, use the traditional common parameters
527 if ( preg_match( '!^(page(\d*)-)*(\d*)px-[^/]*$!', $thumbname, $matches ) ) {
528 list( /* all */, $pagefull, $pagenum, $size ) = $matches;
529 $params['width'] = $size;
530 if ( $pagenum ) {
531 $params['page'] = $pagenum;
532 }
533 return $params; // valid thumbnail URL
534 }
535 return null;
536 }
537
538 /**
539 * Output a thumbnail generation error message
540 *
541 * @param int $status
542 * @param string $msg
543 * @return void
544 */
545 function wfThumbError( $status, $msg ) {
546 global $wgShowHostnames;
547
548 header( 'Cache-Control: no-cache' );
549 header( 'Content-Type: text/html; charset=utf-8' );
550 if ( $status == 404 ) {
551 header( 'HTTP/1.1 404 Not found' );
552 } elseif ( $status == 403 ) {
553 header( 'HTTP/1.1 403 Forbidden' );
554 header( 'Vary: Cookie' );
555 } else {
556 header( 'HTTP/1.1 500 Internal server error' );
557 }
558 if ( $wgShowHostnames ) {
559 header( 'X-MW-Thumbnail-Renderer: ' . wfHostname() );
560 $url = htmlspecialchars( isset( $_SERVER['REQUEST_URI'] ) ? $_SERVER['REQUEST_URI'] : '' );
561 $hostname = htmlspecialchars( wfHostname() );
562 $debug = "<!-- $url -->\n<!-- $hostname -->\n";
563 } else {
564 $debug = '';
565 }
566 echo <<<EOT
567 <html><head><title>Error generating thumbnail</title></head>
568 <body>
569 <h1>Error generating thumbnail</h1>
570 <p>
571 $msg
572 </p>
573 $debug
574 </body>
575 </html>
576
577 EOT;
578 }