Output what was asked for. Don't dirty up a clean API like thumb.php with arbitrary...
[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( "$IP/includes/GlobalFunctions.php" );
13 require_once( "$IP/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( "$IP/includes/StreamFile.php" );
18 require_once( "$IP/includes/AutoLoader.php" );
19
20 // Get input parameters
21 if ( get_magic_quotes_gpc() ) {
22 $params = array_map( 'stripslashes', $_REQUEST );
23 } else {
24 $params = $_REQUEST;
25 }
26
27 $fileName = isset( $params['f'] ) ? $params['f'] : '';
28 unset( $params['f'] );
29
30 // Backwards compatibility parameters
31 if ( isset( $params['w'] ) ) {
32 $params['width'] = $params['w'];
33 unset( $params['w'] );
34 }
35 if ( isset( $params['p'] ) ) {
36 $params['page'] = $params['p'];
37 }
38 unset( $params['r'] );
39
40 // Some basic input validation
41 $fileName = strtr( $fileName, '\\/', '__' );
42
43 // Work out paths, carefully avoiding constructing an Image object because that won't work yet
44 try {
45 $handler = thumbGetHandler( $fileName );
46 if ( $handler ) {
47 $imagePath = wfImageDir( $fileName ) . '/' . $fileName;
48 $thumbName = $handler->makeParamString( $params ) . "-$fileName";
49 $thumbPath = wfImageThumbDir( $fileName ) . '/' . $thumbName;
50
51 if ( is_file( $thumbPath ) && filemtime( $thumbPath ) >= filemtime( $imagePath ) ) {
52 wfStreamFile( $thumbPath );
53 // Can't log profiling data with no Setup.php
54 exit;
55 }
56 }
57 } catch ( MWException $e ) {
58 require_once( './includes/Setup.php' );
59 thumbInternalError( $e->getHTML() );
60 exit;
61 }
62
63
64 // OK, no valid thumbnail, time to get out the heavy machinery
65 wfProfileOut( 'thumb.php-start' );
66 require_once( './includes/Setup.php' );
67 wfProfileIn( 'thumb.php-render' );
68
69 $img = Image::newFromName( $fileName );
70 try {
71 if ( $img ) {
72 $thumb = $img->transform( $params, Image::RENDER_NOW );
73 } else {
74 $thumb = false;
75 }
76 } catch( Exception $ex ) {
77 // Tried to select a page on a non-paged file?
78 $thumb = false;
79 }
80
81 if ( $thumb && $thumb->getPath() && file_exists( $thumb->getPath() ) ) {
82 wfStreamFile( $thumb->getPath() );
83 } elseif ( $img ) {
84 if ( !$thumb ) {
85 $msg = wfMsgHtml( 'thumbnail_error', 'Image::transform() returned false' );
86 } elseif ( $thumb->isError() ) {
87 $msg = $thumb->getHtmlMsg();
88 } elseif ( !$thumb->getPath() ) {
89 $msg = wfMsgHtml( 'thumbnail_error', 'No path supplied in thumbnail object' );
90 } else {
91 $msg = wfMsgHtml( 'thumbnail_error', 'Output file missing' );
92 }
93 thumbInternalError( $msg );
94 } else {
95 $badtitle = wfMsg( 'badtitle' );
96 $badtitletext = wfMsg( 'badtitletext' );
97 header( 'Cache-Control: no-cache' );
98 header( 'Content-Type: text/html; charset=utf-8' );
99 header( 'HTTP/1.1 500 Internal server error' );
100 echo "<html><head>
101 <title>$badtitle</title>
102 <body>
103 <h1>$badtitle</h1>
104 <p>$badtitletext</p>
105 </body></html>
106 ";
107 }
108
109 wfProfileOut( 'thumb.php-render' );
110 wfProfileOut( 'thumb.php' );
111 wfLogProfilingData();
112
113 //--------------------------------------------------------------------------
114
115 function thumbGetHandler( $fileName ) {
116 // Determine type
117 $magic = MimeMagic::singleton();
118 $extPos = strrpos( $fileName, '.' );
119 if ( $extPos === false ) {
120 return false;
121 }
122 $mime = $magic->guessTypesForExtension( substr( $fileName, $extPos + 1 ) );
123 return MediaHandler::getHandler( $mime );
124 }
125
126 function thumbInternalError( $msg ) {
127 header( 'Cache-Control: no-cache' );
128 header( 'Content-Type: text/html; charset=utf-8' );
129 header( 'HTTP/1.1 500 Internal server error' );
130 echo <<<EOT
131 <html><head><title>Error generating thumbnail</title></head>
132 <body>
133 <h1>Error generating thumbnail</h1>
134 <p>
135 $msg
136 </p>
137 </body>
138 </html>
139
140 EOT;
141 }
142
143 ?>