Merge "Make EditPage robust against null content."
[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 the source file title
157 if ( !$img ) {
158 wfThumbError( 404, wfMessage( 'badtitletext' )->text() );
159 wfProfileOut( __METHOD__ );
160 return;
161 }
162
163 // Check permissions if there are read restrictions
164 $varyHeader = array();
165 if ( !in_array( 'read', User::getGroupPermissions( array( '*' ) ), true ) ) {
166 if ( !$img->getTitle() || !$img->getTitle()->userCan( 'read' ) ) {
167 wfThumbError( 403, 'Access denied. You do not have permission to access ' .
168 'the source file.' );
169 wfProfileOut( __METHOD__ );
170 return;
171 }
172 $headers[] = 'Cache-Control: private';
173 $varyHeader[] = 'Cookie';
174 }
175
176 // Check the source file storage path
177 if ( !$img->exists() ) {
178 wfThumbError( 404, 'The source file for the specified thumbnail does not exist.' );
179 wfProfileOut( __METHOD__ );
180 return;
181 }
182 $sourcePath = $img->getPath();
183 if ( $sourcePath === false ) {
184 wfThumbError( 500, 'The source file is not locally accessible.' );
185 wfProfileOut( __METHOD__ );
186 return;
187 }
188
189 // Check IMS against the source file
190 // This means that clients can keep a cached copy even after it has been deleted on the server
191 if ( !empty( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ) {
192 // Fix IE brokenness
193 $imsString = preg_replace( '/;.*$/', '', $_SERVER["HTTP_IF_MODIFIED_SINCE"] );
194 // Calculate time
195 wfSuppressWarnings();
196 $imsUnix = strtotime( $imsString );
197 wfRestoreWarnings();
198 $sourceTsUnix = wfTimestamp( TS_UNIX, $img->getTimestamp() );
199 if ( $sourceTsUnix <= $imsUnix ) {
200 header( 'HTTP/1.1 304 Not Modified' );
201 wfProfileOut( __METHOD__ );
202 return;
203 }
204 }
205
206 $thumbName = $img->thumbName( $params );
207 if ( !strlen( $thumbName ) ) { // invalid params?
208 wfThumbError( 400, 'The specified thumbnail parameters are not valid.' );
209 wfProfileOut( __METHOD__ );
210 return;
211 }
212
213 $disposition = $img->getThumbDisposition( $thumbName );
214 $headers[] = "Content-Disposition: $disposition";
215
216 // Stream the file if it exists already...
217 try {
218 $thumbName2 = $img->thumbName( $params, File::THUMB_FULL_NAME ); // b/c; "long" style
219 // For 404 handled thumbnails, we only use the the base name of the URI
220 // for the thumb params and the parent directory for the source file name.
221 // Check that the zone relative path matches up so squid caches won't pick
222 // up thumbs that would not be purged on source file deletion (bug 34231).
223 if ( isset( $params['rel404'] ) ) { // thumbnail was handled via 404
224 if ( urldecode( $params['rel404'] ) === $img->getThumbRel( $thumbName ) ) {
225 // Request for the canonical thumbnail name
226 } elseif ( urldecode( $params['rel404'] ) === $img->getThumbRel( $thumbName2 ) ) {
227 // Request for the "long" thumbnail name; redirect to canonical name
228 $response = RequestContext::getMain()->getRequest()->response();
229 $response->header( "HTTP/1.1 301 " . HttpStatus::getMessage( 301 ) );
230 $response->header( 'Location: ' . wfExpandUrl( $img->getThumbUrl( $thumbName ), PROTO_CURRENT ) );
231 $response->header( 'Expires: ' .
232 gmdate( 'D, d M Y H:i:s', time() + 7*86400 ) . ' GMT' );
233 if ( $wgVaryOnXFP ) {
234 $varyHeader[] = 'X-Forwarded-Proto';
235 }
236 if ( count( $varyHeader ) ) {
237 $response->header( 'Vary: ' . implode( ', ', $varyHeader ) );
238 }
239 wfProfileOut( __METHOD__ );
240 return;
241 } else {
242 wfThumbError( 404, 'The given path of the specified thumbnail is incorrect.' );
243 wfProfileOut( __METHOD__ );
244 return;
245 }
246 }
247 $thumbPath = $img->getThumbPath( $thumbName );
248 if ( $img->getRepo()->fileExists( $thumbPath ) ) {
249 if ( count( $varyHeader ) ) {
250 $headers[] = 'Vary: ' . implode( ', ', $varyHeader );
251 }
252 $img->getRepo()->streamFile( $thumbPath, $headers );
253 wfProfileOut( __METHOD__ );
254 return;
255 }
256 } catch ( MWException $e ) {
257 wfThumbError( 500, $e->getHTML() );
258 wfProfileOut( __METHOD__ );
259 return;
260 }
261
262 if ( count( $varyHeader ) ) {
263 $headers[] = 'Vary: ' . implode( ', ', $varyHeader );
264 }
265
266 // Thumbnail isn't already there, so create the new thumbnail...
267 try {
268 $thumb = $img->transform( $params, File::RENDER_NOW );
269 } catch ( Exception $ex ) {
270 // Tried to select a page on a non-paged file?
271 $thumb = false;
272 }
273
274 // Check for thumbnail generation errors...
275 $errorMsg = false;
276 $msg = wfMessage( 'thumbnail_error' );
277 if ( !$thumb ) {
278 $errorMsg = $msg->rawParams( 'File::transform() returned false' )->escaped();
279 } elseif ( $thumb->isError() ) {
280 $errorMsg = $thumb->getHtmlMsg();
281 } elseif ( !$thumb->hasFile() ) {
282 $errorMsg = $msg->rawParams( 'No path supplied in thumbnail object' )->escaped();
283 } elseif ( $thumb->fileIsSource() ) {
284 $errorMsg = $msg->
285 rawParams( 'Image was not scaled, is the requested width bigger than the source?' )->escaped();
286 }
287
288 if ( $errorMsg !== false ) {
289 wfThumbError( 500, $errorMsg );
290 } else {
291 // Stream the file if there were no errors
292 $thumb->streamFile( $headers );
293 }
294
295 wfProfileOut( __METHOD__ );
296 }
297
298 /**
299 * Extract the required params for thumb.php from the thumbnail request URI.
300 * At least 'width' and 'f' should be set if the result is an array.
301 *
302 * @param $uriPath String Thumbnail request URI path
303 * @return Array|null associative params array or null
304 */
305 function wfExtractThumbParams( $uriPath ) {
306 $repo = RepoGroup::singleton()->getLocalRepo();
307
308 // Zone URL might be relative ("/images") or protocol-relative ("//lang.site/image")
309 $zoneUriPath = $repo->getZoneHandlerUrl( 'thumb' )
310 ? $repo->getZoneHandlerUrl( 'thumb' ) // custom URL
311 : $repo->getZoneUrl( 'thumb' ); // default to main URL
312 $bits = wfParseUrl( wfExpandUrl( $zoneUriPath, PROTO_INTERNAL ) );
313 if ( $bits && isset( $bits['path'] ) ) {
314 $zoneUriPath = $bits['path'];
315 } else {
316 return null; // not a valid thumbnail URL
317 }
318
319 $hashDirReg = $subdirReg = '';
320 for ( $i = 0; $i < $repo->getHashLevels(); $i++ ) {
321 $subdirReg .= '[0-9a-f]';
322 $hashDirReg .= "$subdirReg/";
323 }
324 $zoneReg = preg_quote( $zoneUriPath ); // regex for thumb zone URI
325
326 // Check if this is a thumbnail of an original in the local file repo
327 if ( preg_match( "!^$zoneReg/((archive/)?$hashDirReg([^/]*)/([^/]*))$!", $uriPath, $m ) ) {
328 list( /*all*/, $rel, $archOrTemp, $filename, $thumbname ) = $m;
329 // Check if this is a thumbnail of an temp file in the local file repo
330 } elseif ( preg_match( "!^$zoneReg/(temp/)($hashDirReg([^/]*)/([^/]*))$!", $uriPath, $m ) ) {
331 list( /*all*/, $archOrTemp, $rel, $filename, $thumbname ) = $m;
332 } else {
333 return null; // not a valid looking thumbnail request
334 }
335
336 $filename = urldecode( $filename );
337 $thumbname = urldecode( $thumbname );
338
339 $params = array( 'f' => $filename, 'rel404' => $rel );
340 if ( $archOrTemp === 'archive/' ) {
341 $params['archived'] = 1;
342 } elseif ( $archOrTemp === 'temp/' ) {
343 $params['temp'] = 1;
344 }
345
346 // Check if the parameters can be extracted from the thumbnail name...
347 if ( preg_match( '!^(page(\d*)-)*(\d*)px-[^/]*$!', $thumbname, $matches ) ) {
348 list( /* all */, $pagefull, $pagenum, $size ) = $matches;
349 $params['width'] = $size;
350 if ( $pagenum ) {
351 $params['page'] = $pagenum;
352 }
353 return $params; // valid thumbnail URL
354 // Hooks return false if they manage to *resolve* the parameters
355 } elseif ( !wfRunHooks( 'ExtractThumbParameters', array( $thumbname, &$params ) ) ) {
356 return $params; // valid thumbnail URL (via extension or config)
357 }
358
359 return null; // not a valid thumbnail URL
360 }
361
362 /**
363 * Output a thumbnail generation error message
364 *
365 * @param $status integer
366 * @param $msg string
367 * @return void
368 */
369 function wfThumbError( $status, $msg ) {
370 global $wgShowHostnames;
371
372 header( 'Cache-Control: no-cache' );
373 header( 'Content-Type: text/html; charset=utf-8' );
374 if ( $status == 404 ) {
375 header( 'HTTP/1.1 404 Not found' );
376 } elseif ( $status == 403 ) {
377 header( 'HTTP/1.1 403 Forbidden' );
378 header( 'Vary: Cookie' );
379 } else {
380 header( 'HTTP/1.1 500 Internal server error' );
381 }
382 if ( $wgShowHostnames ) {
383 $url = htmlspecialchars( isset( $_SERVER['REQUEST_URI'] ) ? $_SERVER['REQUEST_URI'] : '' );
384 $hostname = htmlspecialchars( wfHostname() );
385 $debug = "<!-- $url -->\n<!-- $hostname -->\n";
386 } else {
387 $debug = "";
388 }
389 echo <<<EOT
390 <html><head><title>Error generating thumbnail</title></head>
391 <body>
392 <h1>Error generating thumbnail</h1>
393 <p>
394 $msg
395 </p>
396 $debug
397 </body>
398 </html>
399
400 EOT;
401 }