Worked around hash path inconsistency to unbreak stash file thumbs.
[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 if ( isset( $_SERVER['MW_COMPILED'] ) ) {
26 require( 'core/includes/WebStart.php' );
27 } else {
28 require( __DIR__ . '/includes/WebStart.php' );
29 }
30
31 // Don't use fancy mime detection, just check the file extension for jpg/gif/png
32 $wgTrivialMimeDetection = true;
33
34 if ( defined( 'THUMB_HANDLER' ) ) {
35 // Called from thumb_handler.php via 404; extract params from the URI...
36 wfThumbHandle404();
37 } else {
38 // Called directly, use $_REQUEST params
39 wfThumbHandleRequest();
40 }
41 wfLogProfilingData();
42
43 //--------------------------------------------------------------------------
44
45 /**
46 * Handle a thumbnail request via query parameters
47 *
48 * @return void
49 */
50 function wfThumbHandleRequest() {
51 $params = get_magic_quotes_gpc()
52 ? array_map( 'stripslashes', $_REQUEST )
53 : $_REQUEST;
54
55 wfStreamThumb( $params ); // stream the thumbnail
56 }
57
58 /**
59 * Handle a thumbnail request via thumbnail file URL
60 *
61 * @return void
62 */
63 function wfThumbHandle404() {
64 # lighttpd puts the original request in REQUEST_URI, while sjs sets
65 # that to the 404 handler, and puts the original request in REDIRECT_URL.
66 if ( isset( $_SERVER['REDIRECT_URL'] ) ) {
67 # The URL is un-encoded, so put it back how it was
68 $uriPath = str_replace( "%2F", "/", urlencode( $_SERVER['REDIRECT_URL'] ) );
69 } else {
70 $uriPath = $_SERVER['REQUEST_URI'];
71 }
72 # Just get the URI path (REDIRECT_URL/REQUEST_URI is either a full URL or a path)
73 if ( substr( $uriPath, 0, 1 ) !== '/' ) {
74 $bits = wfParseUrl( $uriPath );
75 if ( $bits && isset( $bits['path'] ) ) {
76 $uriPath = $bits['path'];
77 } else {
78 wfThumbError( 404, 'The source file for the specified thumbnail does not exist.' );
79 return;
80 }
81 }
82
83 $params = wfExtractThumbParams( $uriPath ); // basic wiki URL param extracting
84 if ( $params == null ) {
85 wfThumbError( 404, 'The source file for the specified thumbnail does not exist.' );
86 return;
87 }
88
89 wfStreamThumb( $params ); // stream the thumbnail
90 }
91
92 /**
93 * Stream a thumbnail specified by parameters
94 *
95 * @param $params Array
96 * @return void
97 */
98 function wfStreamThumb( array $params ) {
99 global $wgVaryOnXFP;
100 wfProfileIn( __METHOD__ );
101
102 $headers = array(); // HTTP headers to send
103
104 $fileName = isset( $params['f'] ) ? $params['f'] : '';
105 unset( $params['f'] );
106
107 // Backwards compatibility parameters
108 if ( isset( $params['w'] ) ) {
109 $params['width'] = $params['w'];
110 unset( $params['w'] );
111 }
112 if ( isset( $params['p'] ) ) {
113 $params['page'] = $params['p'];
114 }
115 unset( $params['r'] ); // ignore 'r' because we unconditionally pass File::RENDER
116
117 // Is this a thumb of an archived file?
118 $isOld = ( isset( $params['archived'] ) && $params['archived'] );
119 unset( $params['archived'] ); // handlers don't care
120
121 // Is this a thumb of a temp file?
122 $isTemp = ( isset( $params['temp'] ) && $params['temp'] );
123 unset( $params['temp'] ); // handlers don't care
124
125 // Some basic input validation
126 $fileName = strtr( $fileName, '\\/', '__' );
127
128 // Actually fetch the image. Method depends on whether it is archived or not.
129 if ( $isTemp ) {
130 $repo = RepoGroup::singleton()->getLocalRepo()->getTempRepo();
131 $img = new UnregisteredLocalFile( null, $repo,
132 # Temp files are hashed based on the name without the timestamp.
133 # The thumbnails will be hashed based on the entire name however.
134 # @TODO: fix this convention to actually be reasonable.
135 $repo->getZonePath( 'public' ) . '/' . $repo->getTempHashPath( $fileName ) . $fileName
136 );
137 } elseif ( $isOld ) {
138 // Format is <timestamp>!<name>
139 $bits = explode( '!', $fileName, 2 );
140 if ( count( $bits ) != 2 ) {
141 wfThumbError( 404, wfMessage( 'badtitletext' )->text() );
142 wfProfileOut( __METHOD__ );
143 return;
144 }
145 $title = Title::makeTitleSafe( NS_FILE, $bits[1] );
146 if ( !$title ) {
147 wfThumbError( 404, wfMessage( 'badtitletext' )->text() );
148 wfProfileOut( __METHOD__ );
149 return;
150 }
151 $img = RepoGroup::singleton()->getLocalRepo()->newFromArchiveName( $title, $fileName );
152 } else {
153 $img = wfLocalFile( $fileName );
154 }
155
156 // Check permissions if there are read restrictions
157 $varyHeader = array();
158 if ( !in_array( 'read', User::getGroupPermissions( array( '*' ) ), true ) ) {
159 if ( !$img->getTitle() || !$img->getTitle()->userCan( 'read' ) ) {
160 wfThumbError( 403, 'Access denied. You do not have permission to access ' .
161 'the source file.' );
162 wfProfileOut( __METHOD__ );
163 return;
164 }
165 $headers[] = 'Cache-Control: private';
166 $varyHeader[] = 'Cookie';
167 }
168
169 // Check the source file storage path
170 if ( !$img ) {
171 wfThumbError( 404, wfMessage( 'badtitletext' )->text() );
172 wfProfileOut( __METHOD__ );
173 return;
174 }
175 if ( !$img->exists() ) {
176 wfThumbError( 404, 'The source file for the specified thumbnail does not exist.' );
177 wfProfileOut( __METHOD__ );
178 return;
179 }
180 $sourcePath = $img->getPath();
181 if ( $sourcePath === false ) {
182 wfThumbError( 500, 'The source file is not locally accessible.' );
183 wfProfileOut( __METHOD__ );
184 return;
185 }
186
187 // Check IMS against the source file
188 // This means that clients can keep a cached copy even after it has been deleted on the server
189 if ( !empty( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ) {
190 // Fix IE brokenness
191 $imsString = preg_replace( '/;.*$/', '', $_SERVER["HTTP_IF_MODIFIED_SINCE"] );
192 // Calculate time
193 wfSuppressWarnings();
194 $imsUnix = strtotime( $imsString );
195 wfRestoreWarnings();
196 $sourceTsUnix = wfTimestamp( TS_UNIX, $img->getTimestamp() );
197 if ( $sourceTsUnix <= $imsUnix ) {
198 header( 'HTTP/1.1 304 Not Modified' );
199 wfProfileOut( __METHOD__ );
200 return;
201 }
202 }
203
204 $thumbName = $img->thumbName( $params );
205 if ( !strlen( $thumbName ) ) { // invalid params?
206 wfThumbError( 400, 'The specified thumbnail parameters are not valid.' );
207 wfProfileOut( __METHOD__ );
208 return;
209 }
210
211 $disposition = $img->getThumbDisposition( $thumbName );
212 $headers[] = "Content-Disposition: $disposition";
213
214 // Stream the file if it exists already...
215 try {
216 $thumbName2 = $img->thumbName( $params, File::THUMB_FULL_NAME ); // b/c; "long" style
217 // For 404 handled thumbnails, we only use the the base name of the URI
218 // for the thumb params and the parent directory for the source file name.
219 // Check that the zone relative path matches up so squid caches won't pick
220 // up thumbs that would not be purged on source file deletion (bug 34231).
221 if ( isset( $params['rel404'] ) ) { // thumbnail was handled via 404
222 if ( urldecode( $params['rel404'] ) === $img->getThumbRel( $thumbName ) ) {
223 // Request for the canonical thumbnail name
224 } elseif ( urldecode( $params['rel404'] ) === $img->getThumbRel( $thumbName2 ) ) {
225 // Request for the "long" thumbnail name; redirect to canonical name
226 $response = RequestContext::getMain()->getRequest()->response();
227 $response->header( "HTTP/1.1 301 " . HttpStatus::getMessage( 301 ) );
228 $response->header( 'Location: ' . wfExpandUrl( $img->getThumbUrl( $thumbName ), PROTO_CURRENT ) );
229 $response->header( 'Expires: ' .
230 gmdate( 'D, d M Y H:i:s', time() + 7*86400 ) . ' GMT' );
231 if ( $wgVaryOnXFP ) {
232 $varyHeader[] = 'X-Forwarded-Proto';
233 }
234 $response->header( 'Vary: ' . implode( ', ', $varyHeader ) );
235 wfProfileOut( __METHOD__ );
236 return;
237 } else {
238 wfThumbError( 404, 'The given path of the specified thumbnail is incorrect.' );
239 wfProfileOut( __METHOD__ );
240 return;
241 }
242 }
243 $thumbPath = $img->getThumbPath( $thumbName );
244 if ( $img->getRepo()->fileExists( $thumbPath ) ) {
245 $headers[] = 'Vary: ' . implode( ', ', $varyHeader );
246 $img->getRepo()->streamFile( $thumbPath, $headers );
247 wfProfileOut( __METHOD__ );
248 return;
249 }
250 } catch ( MWException $e ) {
251 wfThumbError( 500, $e->getHTML() );
252 wfProfileOut( __METHOD__ );
253 return;
254 }
255 $headers[] = 'Vary: ' . implode( ', ', $varyHeader );
256
257 // Thumbnail isn't already there, so create the new thumbnail...
258 try {
259 $thumb = $img->transform( $params, File::RENDER_NOW );
260 } catch ( Exception $ex ) {
261 // Tried to select a page on a non-paged file?
262 $thumb = false;
263 }
264
265 // Check for thumbnail generation errors...
266 $errorMsg = false;
267 $msg = wfMessage( 'thumbnail_error' );
268 if ( !$thumb ) {
269 $errorMsg = $msg->rawParams( 'File::transform() returned false' )->escaped();
270 } elseif ( $thumb->isError() ) {
271 $errorMsg = $thumb->getHtmlMsg();
272 } elseif ( !$thumb->hasFile() ) {
273 $errorMsg = $msg->rawParams( 'No path supplied in thumbnail object' )->escaped();
274 } elseif ( $thumb->fileIsSource() ) {
275 $errorMsg = $msg->
276 rawParams( 'Image was not scaled, is the requested width bigger than the source?' )->escaped();
277 }
278
279 if ( $errorMsg !== false ) {
280 wfThumbError( 500, $errorMsg );
281 } else {
282 // Stream the file if there were no errors
283 $thumb->streamFile( $headers );
284 }
285
286 wfProfileOut( __METHOD__ );
287 }
288
289 /**
290 * Extract the required params for thumb.php from the thumbnail request URI.
291 * At least 'width' and 'f' should be set if the result is an array.
292 *
293 * @param $uriPath String Thumbnail request URI path
294 * @return Array|null associative params array or null
295 */
296 function wfExtractThumbParams( $uriPath ) {
297 $repo = RepoGroup::singleton()->getLocalRepo();
298
299 // Zone URL might be relative ("/images") or protocol-relative ("//lang.site/image")
300 $zoneUriPath = $repo->getZoneHandlerUrl( 'thumb' )
301 ? $repo->getZoneHandlerUrl( 'thumb' ) // custom URL
302 : $repo->getZoneUrl( 'thumb' ); // default to main URL
303 $bits = wfParseUrl( wfExpandUrl( $zoneUriPath, PROTO_INTERNAL ) );
304 if ( $bits && isset( $bits['path'] ) ) {
305 $zoneUriPath = $bits['path'];
306 } else {
307 return null; // not a valid thumbnail URL
308 }
309
310 $hashDirReg = $subdirReg = '';
311 for ( $i = 0; $i < $repo->getHashLevels(); $i++ ) {
312 $subdirReg .= '[0-9a-f]';
313 $hashDirReg .= "$subdirReg/";
314 }
315 $zoneReg = preg_quote( $zoneUriPath ); // regex for thumb zone URI
316
317 // Check if this is a thumbnail of an original in the local file repo
318 if ( preg_match( "!^$zoneReg/((archive/)?$hashDirReg([^/]*)/([^/]*))$!", $uriPath, $m ) ) {
319 list( /*all*/, $rel, $archOrTemp, $filename, $thumbname ) = $m;
320 // Check if this is a thumbnail of an temp file in the local file repo
321 } elseif ( preg_match( "!^$zoneReg/(temp/)($hashDirReg([^/]*)/([^/]*))$!", $uriPath, $m ) ) {
322 list( /*all*/, $archOrTemp, $rel, $filename, $thumbname ) = $m;
323 } else {
324 return null; // not a valid looking thumbnail request
325 }
326
327 $filename = urldecode( $filename );
328 $thumbname = urldecode( $thumbname );
329
330 $params = array( 'f' => $filename, 'rel404' => $rel );
331 if ( $archOrTemp === 'archive/' ) {
332 $params['archived'] = 1;
333 } elseif ( $archOrTemp === 'temp/' ) {
334 $params['temp'] = 1;
335 }
336
337 // Check if the parameters can be extracted from the thumbnail name...
338 if ( preg_match( '!^(page(\d*)-)*(\d*)px-[^/]*$!', $thumbname, $matches ) ) {
339 list( /* all */, $pagefull, $pagenum, $size ) = $matches;
340 $params['width'] = $size;
341 if ( $pagenum ) {
342 $params['page'] = $pagenum;
343 }
344 return $params; // valid thumbnail URL
345 // Hooks return false if they manage to *resolve* the parameters
346 } elseif ( !wfRunHooks( 'ExtractThumbParameters', array( $thumbname, &$params ) ) ) {
347 return $params; // valid thumbnail URL (via extension or config)
348 }
349
350 return null; // not a valid thumbnail URL
351 }
352
353 /**
354 * Output a thumbnail generation error message
355 *
356 * @param $status integer
357 * @param $msg string
358 * @return void
359 */
360 function wfThumbError( $status, $msg ) {
361 global $wgShowHostnames;
362
363 header( 'Cache-Control: no-cache' );
364 header( 'Content-Type: text/html; charset=utf-8' );
365 if ( $status == 404 ) {
366 header( 'HTTP/1.1 404 Not found' );
367 } elseif ( $status == 403 ) {
368 header( 'HTTP/1.1 403 Forbidden' );
369 header( 'Vary: Cookie' );
370 } else {
371 header( 'HTTP/1.1 500 Internal server error' );
372 }
373 if ( $wgShowHostnames ) {
374 $url = htmlspecialchars( isset( $_SERVER['REQUEST_URI'] ) ? $_SERVER['REQUEST_URI'] : '' );
375 $hostname = htmlspecialchars( wfHostname() );
376 $debug = "<!-- $url -->\n<!-- $hostname -->\n";
377 } else {
378 $debug = "";
379 }
380 echo <<<EOT
381 <html><head><title>Error generating thumbnail</title></head>
382 <body>
383 <h1>Error generating thumbnail</h1>
384 <p>
385 $msg
386 </p>
387 $debug
388 </body>
389 </html>
390
391 EOT;
392 }