* Add 'charset' to Content-Type headers on various HTTP error responses
[lhc/web/wiklou.git] / thumb.php
1 <?php
2
3 /**
4 * PHP script to stream out an image thumbnail.
5 * If the file exists, we make do with abridged MediaWiki initialisation.
6 */
7 define( 'MW_NO_SETUP', 1 );
8 define( 'MW_NO_OUTPUT_COMPRESSION', 1 );
9 require_once( './includes/WebStart.php' );
10 wfProfileIn( 'thumb.php' );
11 wfProfileIn( 'thumb.php-start' );
12 require_once( './includes/GlobalFunctions.php' );
13 require_once( './includes/ImageFunctions.php' );
14
15 $wgTrivialMimeDetection = true; //don't use fancy mime detection, just check the file extension for jpg/gif/png.
16
17 require_once( './includes/Image.php' );
18 require_once( './includes/StreamFile.php' );
19
20 // Get input parameters
21 $fileName = isset( $_REQUEST['f'] ) ? $_REQUEST['f'] : '';
22 $width = isset( $_REQUEST['w'] ) ? intval( $_REQUEST['w'] ) : 0;
23 $page = isset( $_REQUEST['p'] ) ? intval( $_REQUEST['p'] ) : null;
24
25 if ( get_magic_quotes_gpc() ) {
26 $fileName = stripslashes( $fileName );
27 }
28
29 $pre_render= isset($_REQUEST['r']) && $_REQUEST['r']!="0";
30
31 // Some basic input validation
32 $fileName = strtr( $fileName, '\\/', '__' );
33
34 // Work out paths, carefully avoiding constructing an Image object because that won't work yet
35
36 $imagePath = wfImageDir( $fileName ) . '/' . $fileName;
37 $thumbName = "{$width}px-$fileName";
38 if ( ! is_null( $page ) ) {
39 $thumbName = 'page' . $page . '-' . $thumbName;
40 }
41 if ( $pre_render ) {
42 $thumbName .= '.png';
43 }
44 $thumbPath = wfImageThumbDir( $fileName ) . '/' . $thumbName;
45
46 if ( is_file( $thumbPath ) && filemtime( $thumbPath ) >= filemtime( $imagePath ) ) {
47 wfStreamFile( $thumbPath );
48 // Can't log profiling data with no Setup.php
49 exit;
50 }
51
52 // OK, no valid thumbnail, time to get out the heavy machinery
53 wfProfileOut( 'thumb.php-start' );
54 require_once( './includes/Setup.php' );
55 wfProfileIn( 'thumb.php-render' );
56
57 $img = Image::newFromName( $fileName );
58 try {
59 if ( $img ) {
60 if ( ! is_null( $page ) ) {
61 $img->selectPage( $page );
62 }
63 $thumb = $img->renderThumb( $width, false );
64 } else {
65 $thumb = false;
66 }
67 } catch( Exception $ex ) {
68 // Tried to select a page on a non-paged file?
69 $thumb = false;
70 }
71
72 if ( $thumb && $thumb->path ) {
73 wfStreamFile( $thumb->path );
74 } else {
75 $badtitle = wfMsg( 'badtitle' );
76 $badtitletext = wfMsg( 'badtitletext' );
77 header( 'Cache-Control: no-cache' );
78 header( 'Content-Type: text/html; charset=utf-8' );
79 echo "<html><head>
80 <title>$badtitle</title>
81 <body>
82 <h1>$badtitle</h1>
83 <p>$badtitletext</p>
84 </body></html>
85 ";
86 }
87
88 wfProfileOut( 'thumb.php-render' );
89 wfProfileOut( 'thumb.php' );
90 wfLogProfilingData();
91
92 ?>