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