thumb_handler.php doesn't seem to extract path_info correctly
[mediawiki.git] / thumb.php
blobb9826a78878f23ddd53810220e8e76ed65cb4ab7
1 <?php
2 /**
3 * PHP script to stream out an image thumbnail.
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.
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.
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
20 * @file
21 * @ingroup Media
24 define( 'MW_NO_OUTPUT_COMPRESSION', 1 );
25 require __DIR__ . '/includes/WebStart.php';
27 // Don't use fancy mime detection, just check the file extension for jpg/gif/png
28 $wgTrivialMimeDetection = true;
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();
38 wfLogProfilingData();
40 //--------------------------------------------------------------------------
42 /**
43 * Handle a thumbnail request via query parameters
45 * @return void
47 function wfThumbHandleRequest() {
48 $params = get_magic_quotes_gpc()
49 ? array_map( 'stripslashes', $_GET )
50 : $_GET;
52 wfStreamThumb( $params ); // stream the thumbnail
55 /**
56 * Handle a thumbnail request via thumbnail file URL
58 * @return void
60 function wfThumbHandle404() {
61 global $wgArticlePath;
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';
70 $matches = WebRequest::getPathInfo();
72 $wgArticlePath = $oldArticlePath;
74 if ( !isset( $matches['title'] ) ) {
75 wfThumbError( 404, 'Could not determine the name of the requested thumbnail.' );
76 return;
79 $params = wfExtractThumbParams( $matches['title'] ); // basic wiki URL param extracting
80 if ( $params == null ) {
81 wfThumbError( 400, 'The specified thumbnail parameters are not recognized.' );
82 return;
85 wfStreamThumb( $params ); // stream the thumbnail
88 /**
89 * Stream a thumbnail specified by parameters
91 * @param $params Array
92 * @return void
94 function wfStreamThumb( array $params ) {
95 global $wgVaryOnXFP;
97 wfProfileIn( __METHOD__ );
99 $headers = array(); // HTTP headers to send
101 $fileName = isset( $params['f'] ) ? $params['f'] : '';
102 unset( $params['f'] );
104 // Backwards compatibility parameters
105 if ( isset( $params['w'] ) ) {
106 $params['width'] = $params['w'];
107 unset( $params['w'] );
109 if ( isset( $params['p'] ) ) {
110 $params['page'] = $params['p'];
112 unset( $params['r'] ); // ignore 'r' because we unconditionally pass File::RENDER
114 // Is this a thumb of an archived file?
115 $isOld = ( isset( $params['archived'] ) && $params['archived'] );
116 unset( $params['archived'] ); // handlers don't care
118 // Is this a thumb of a temp file?
119 $isTemp = ( isset( $params['temp'] ) && $params['temp'] );
120 unset( $params['temp'] ); // handlers don't care
122 // Some basic input validation
123 $fileName = strtr( $fileName, '\\/', '__' );
125 // Actually fetch the image. Method depends on whether it is archived or not.
126 if ( $isTemp ) {
127 $repo = RepoGroup::singleton()->getLocalRepo()->getTempRepo();
128 $img = new UnregisteredLocalFile( null, $repo,
129 # Temp files are hashed based on the name without the timestamp.
130 # The thumbnails will be hashed based on the entire name however.
131 # @todo fix this convention to actually be reasonable.
132 $repo->getZonePath( 'public' ) . '/' . $repo->getTempHashPath( $fileName ) . $fileName
134 } elseif ( $isOld ) {
135 // Format is <timestamp>!<name>
136 $bits = explode( '!', $fileName, 2 );
137 if ( count( $bits ) != 2 ) {
138 wfThumbError( 404, wfMessage( 'badtitletext' )->text() );
139 wfProfileOut( __METHOD__ );
140 return;
142 $title = Title::makeTitleSafe( NS_FILE, $bits[1] );
143 if ( !$title ) {
144 wfThumbError( 404, wfMessage( 'badtitletext' )->text() );
145 wfProfileOut( __METHOD__ );
146 return;
148 $img = RepoGroup::singleton()->getLocalRepo()->newFromArchiveName( $title, $fileName );
149 } else {
150 $img = wfLocalFile( $fileName );
153 // Check the source file title
154 if ( !$img ) {
155 wfThumbError( 404, wfMessage( 'badtitletext' )->text() );
156 wfProfileOut( __METHOD__ );
157 return;
160 // Check permissions if there are read restrictions
161 $varyHeader = array();
162 if ( !in_array( 'read', User::getGroupPermissions( array( '*' ) ), true ) ) {
163 if ( !$img->getTitle() || !$img->getTitle()->userCan( 'read' ) ) {
164 wfThumbError( 403, 'Access denied. You do not have permission to access ' .
165 'the source file.' );
166 wfProfileOut( __METHOD__ );
167 return;
169 $headers[] = 'Cache-Control: private';
170 $varyHeader[] = 'Cookie';
173 // Check the source file storage path
174 if ( !$img->exists() ) {
175 wfThumbError( 404, "The source file '$fileName' does not exist." );
176 wfProfileOut( __METHOD__ );
177 return;
178 } elseif ( $img->getPath() === false ) {
179 wfThumbError( 500, "The source file '$fileName' is not locally accessible." );
180 wfProfileOut( __METHOD__ );
181 return;
184 // Check IMS against the source file
185 // This means that clients can keep a cached copy even after it has been deleted on the server
186 if ( !empty( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ) {
187 // Fix IE brokenness
188 $imsString = preg_replace( '/;.*$/', '', $_SERVER["HTTP_IF_MODIFIED_SINCE"] );
189 // Calculate time
190 wfSuppressWarnings();
191 $imsUnix = strtotime( $imsString );
192 wfRestoreWarnings();
193 if ( wfTimestamp( TS_UNIX, $img->getTimestamp() ) <= $imsUnix ) {
194 header( 'HTTP/1.1 304 Not Modified' );
195 wfProfileOut( __METHOD__ );
196 return;
200 // Get the normalized thumbnail name from the parameters...
201 try {
202 $thumbName = $img->thumbName( $params );
203 if ( !strlen( $thumbName ) ) { // invalid params?
204 wfThumbError( 400, 'The specified thumbnail parameters are not valid.' );
205 wfProfileOut( __METHOD__ );
206 return;
208 $thumbName2 = $img->thumbName( $params, File::THUMB_FULL_NAME ); // b/c; "long" style
209 } catch ( MWException $e ) {
210 wfThumbError( 500, $e->getHTML() );
211 wfProfileOut( __METHOD__ );
212 return;
215 // For 404 handled thumbnails, we only use the the base name of the URI
216 // for the thumb params and the parent directory for the source file name.
217 // Check that the zone relative path matches up so squid caches won't pick
218 // up thumbs that would not be purged on source file deletion (bug 34231).
219 if ( isset( $params['rel404'] ) ) { // thumbnail was handled via 404
220 if ( rawurldecode( $params['rel404'] ) === $img->getThumbRel( $thumbName ) ) {
221 // Request for the canonical thumbnail name
222 } elseif ( rawurldecode( $params['rel404'] ) === $img->getThumbRel( $thumbName2 ) ) {
223 // Request for the "long" thumbnail name; redirect to canonical name
224 $response = RequestContext::getMain()->getRequest()->response();
225 $response->header( "HTTP/1.1 301 " . HttpStatus::getMessage( 301 ) );
226 $response->header( 'Location: ' .
227 wfExpandUrl( $img->getThumbUrl( $thumbName ), PROTO_CURRENT ) );
228 $response->header( 'Expires: ' .
229 gmdate( 'D, d M Y H:i:s', time() + 7 * 86400 ) . ' GMT' );
230 if ( $wgVaryOnXFP ) {
231 $varyHeader[] = 'X-Forwarded-Proto';
233 if ( count( $varyHeader ) ) {
234 $response->header( 'Vary: ' . implode( ', ', $varyHeader ) );
236 wfProfileOut( __METHOD__ );
237 return;
238 } else {
239 wfThumbError( 404, "The given path of the specified thumbnail is incorrect;
240 expected '" . $img->getThumbRel( $thumbName ) . "' but got '" .
241 rawurldecode( $params['rel404'] ) . "'." );
242 wfProfileOut( __METHOD__ );
243 return;
247 // Suggest a good name for users downloading this thumbnail
248 $headers[] = "Content-Disposition: {$img->getThumbDisposition( $thumbName )}";
250 if ( count( $varyHeader ) ) {
251 $headers[] = 'Vary: ' . implode( ', ', $varyHeader );
254 // Stream the file if it exists already...
255 $thumbPath = $img->getThumbPath( $thumbName );
256 if ( $img->getRepo()->fileExists( $thumbPath ) ) {
257 $img->getRepo()->streamFile( $thumbPath, $headers );
258 wfProfileOut( __METHOD__ );
259 return;
262 // Thumbnail isn't already there, so create the new thumbnail...
263 try {
264 $thumb = $img->transform( $params, File::RENDER_NOW );
265 } catch ( Exception $ex ) {
266 // Tried to select a page on a non-paged file?
267 $thumb = false;
270 // Check for thumbnail generation errors...
271 $errorMsg = false;
272 $msg = wfMessage( 'thumbnail_error' );
273 if ( !$thumb ) {
274 $errorMsg = $msg->rawParams( 'File::transform() returned false' )->escaped();
275 } elseif ( $thumb->isError() ) {
276 $errorMsg = $thumb->getHtmlMsg();
277 } elseif ( !$thumb->hasFile() ) {
278 $errorMsg = $msg->rawParams( 'No path supplied in thumbnail object' )->escaped();
279 } elseif ( $thumb->fileIsSource() ) {
280 $errorMsg = $msg->
281 rawParams( 'Image was not scaled, is the requested width bigger than the source?' )->escaped();
284 if ( $errorMsg !== false ) {
285 wfThumbError( 500, $errorMsg );
286 } else {
287 // Stream the file if there were no errors
288 $thumb->streamFile( $headers );
291 wfProfileOut( __METHOD__ );
295 * Extract the required params for thumb.php from the thumbnail request URI.
296 * At least 'width' and 'f' should be set if the result is an array.
298 * @param $thumbRel String Thumbnail path relative to the thumb zone
299 * @return Array|null associative params array or null
301 function wfExtractThumbParams( $thumbRel ) {
302 $repo = RepoGroup::singleton()->getLocalRepo();
304 $hashDirReg = $subdirReg = '';
305 for ( $i = 0; $i < $repo->getHashLevels(); $i++ ) {
306 $subdirReg .= '[0-9a-f]';
307 $hashDirReg .= "$subdirReg/";
310 // Check if this is a thumbnail of an original in the local file repo
311 if ( preg_match( "!^((archive/)?$hashDirReg([^/]*)/([^/]*))$!", $thumbRel, $m ) ) {
312 list( /*all*/, $rel, $archOrTemp, $filename, $thumbname ) = $m;
313 // Check if this is a thumbnail of an temp file in the local file repo
314 } elseif ( preg_match( "!^(temp/)($hashDirReg([^/]*)/([^/]*))$!", $thumbRel, $m ) ) {
315 list( /*all*/, $archOrTemp, $rel, $filename, $thumbname ) = $m;
316 } else {
317 return null; // not a valid looking thumbnail request
320 $params = array( 'f' => $filename, 'rel404' => $rel );
321 if ( $archOrTemp === 'archive/' ) {
322 $params['archived'] = 1;
323 } elseif ( $archOrTemp === 'temp/' ) {
324 $params['temp'] = 1;
327 // Check hooks if parameters can be extracted
328 // Hooks return false if they manage to *resolve* the parameters
329 if ( !wfRunHooks( 'ExtractThumbParameters', array( $thumbname, &$params ) ) ) {
330 return $params; // valid thumbnail URL (via extension or config)
331 // Check if the parameters can be extracted from the thumbnail name...
332 } elseif ( preg_match( '!^(page(\d*)-)*(\d*)px-[^/]*$!', $thumbname, $matches ) ) {
333 list( /* all */, $pagefull, $pagenum, $size ) = $matches;
334 $params['width'] = $size;
335 if ( $pagenum ) {
336 $params['page'] = $pagenum;
338 return $params; // valid thumbnail URL
341 return null; // not a valid thumbnail URL
345 * Output a thumbnail generation error message
347 * @param $status integer
348 * @param $msg string
349 * @return void
351 function wfThumbError( $status, $msg ) {
352 global $wgShowHostnames;
354 header( 'Cache-Control: no-cache' );
355 header( 'Content-Type: text/html; charset=utf-8' );
356 if ( $status == 404 ) {
357 header( 'HTTP/1.1 404 Not found' );
358 } elseif ( $status == 403 ) {
359 header( 'HTTP/1.1 403 Forbidden' );
360 header( 'Vary: Cookie' );
361 } else {
362 header( 'HTTP/1.1 500 Internal server error' );
364 if ( $wgShowHostnames ) {
365 $url = htmlspecialchars( isset( $_SERVER['REQUEST_URI'] ) ? $_SERVER['REQUEST_URI'] : '' );
366 $hostname = htmlspecialchars( wfHostname() );
367 $debug = "<!-- $url -->\n<!-- $hostname -->\n";
368 } else {
369 $debug = '';
371 echo <<<EOT
372 <html><head><title>Error generating thumbnail</title></head>
373 <body>
374 <h1>Error generating thumbnail</h1>
376 $msg
377 </p>
378 $debug
379 </body>
380 </html>
382 EOT;