ff7f9153e94d61d7d9d52f1282fd3ba1b899b78e
[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
8 unset( $IP );
9 define( 'MEDIAWIKI', true );
10 require_once( './includes/Defines.php' );
11 require_once( './LocalSettings.php' );
12 require_once( 'Image.php' );
13 require_once( 'StreamFile.php' );
14
15 // Get input parameters
16
17 if ( get_magic_quotes_gpc() ) {
18 $fileName = stripslashes( $_REQUEST['f'] );
19 $width = stripslashes( $_REQUEST['w'] );
20 } else {
21 $fileName = $_REQUEST['f'];
22 $width = $_REQUEST['w'];
23 }
24
25 // Some basic input validation
26
27 $width = intval( $width );
28 $fileName = strtr( $fileName, '\\/', '__' );
29
30 // Work out paths, carefully avoiding constructing an Image object because that won't work yet
31
32 $imagePath = wfImageDir( $fileName ) . '/' . $fileName;
33 $thumbName = "{$width}px-$fileName";
34 if ( preg_match( '/\.svg$/', $fileName ) ) {
35 $thumbName .= '.png';
36 }
37 $thumbPath = wfImageThumbDir( $thumbName ) . '/' . $thumbName;
38
39 if ( file_exists( $thumbPath ) && filemtime( $thumbPath ) >= filemtime( $imagePath ) ) {
40 wfStreamFile( $thumbPath );
41 exit;
42 }
43
44 // OK, no valid thumbnail, time to get out the heavy machinery
45 require_once( 'Setup.php' );
46
47 // Force renderThumb() to actually do something
48 $wgThumbnailScriptPath = false;
49 $wgSharedThumbnailScriptPath = false;
50
51 $img = Image::newFromName( $fileName );
52 if ( $img ) {
53 $thumb = $img->renderThumb( $width );
54 } else {
55 $thumb = false;
56 }
57
58 if ( $thumb ) {
59 wfStreamFile( $thumb->path );
60 } else {
61 $badtitle = wfMsg( 'badtitle' );
62 $badtitletext = wfMsg( 'badtitletext' );
63 echo "<html><head>
64 <title>$badtitle</title>
65 <body>
66 <h1>$badtitle</h1>
67 <p>$badtitletext</p>
68 </body></html>";
69 }
70
71
72 ?>