c67468b694ed116efa0a7eee3d97b053b6ab9c27
[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 $handler = thumbGetHandler( $fileName );
45 if ( $handler ) {
46 $imagePath = wfImageDir( $fileName ) . '/' . $fileName;
47 $thumbName = $handler->makeParamString( $params ) . "-$fileName";
48 $thumbPath = wfImageThumbDir( $fileName ) . '/' . $thumbName;
49
50 if ( is_file( $thumbPath ) && filemtime( $thumbPath ) >= filemtime( $imagePath ) ) {
51 wfStreamFile( $thumbPath );
52 // Can't log profiling data with no Setup.php
53 exit;
54 }
55 }
56
57 // OK, no valid thumbnail, time to get out the heavy machinery
58 wfProfileOut( 'thumb.php-start' );
59 require_once( './includes/Setup.php' );
60 wfProfileIn( 'thumb.php-render' );
61
62 $img = Image::newFromName( $fileName );
63 try {
64 if ( $img ) {
65 $thumb = $img->transform( $params, Image::RENDER_NOW );
66 } else {
67 $thumb = false;
68 }
69 } catch( Exception $ex ) {
70 // Tried to select a page on a non-paged file?
71 $thumb = false;
72 }
73
74 if ( $thumb && $thumb->getPath() ) {
75 wfStreamFile( $thumb->getPath() );
76 } elseif ( $img ) {
77 header( 'Cache-Control: no-cache' );
78 header( 'Content-Type: text/html; charset=utf-8' );
79 header( 'HTTP/1.1 500 Internal server error' );
80 if ( !$thumb ) {
81 $msg = wfMsgHtml( 'thumbnail_error', 'Image::transform() returned false' );
82 } elseif ( $thumb->isError() ) {
83 $msg = $thumb->toHtml();
84 } else {
85 $msg = wfMsgHtml( 'thumbnail_error', 'No path supplied in thumbnail object' );
86 }
87 echo <<<EOT
88 <html><head><title>Error generating thumbnail</title></head>
89 <body>
90 $msg
91 </body>
92 </html>
93
94 EOT;
95 } else {
96 $badtitle = wfMsg( 'badtitle' );
97 $badtitletext = wfMsg( 'badtitletext' );
98 header( 'Cache-Control: no-cache' );
99 header( 'Content-Type: text/html; charset=utf-8' );
100 header( 'HTTP/1.1 500 Internal server error' );
101 echo "<html><head>
102 <title>$badtitle</title>
103 <body>
104 <h1>$badtitle</h1>
105 <p>$badtitletext</p>
106 </body></html>
107 ";
108 }
109
110 wfProfileOut( 'thumb.php-render' );
111 wfProfileOut( 'thumb.php' );
112 wfLogProfilingData();
113
114 //--------------------------------------------------------------------------
115
116 function thumbGetHandler( $fileName ) {
117 // Determine type
118 $magic = MimeMagic::singleton();
119 $extPos = strrpos( $fileName, '.' );
120 if ( $extPos === false ) {
121 return false;
122 }
123 $mime = $magic->guessTypesForExtension( substr( $fileName, $extPos + 1 ) );
124 return MediaHandler::getHandler( $mime );
125 }
126
127 ?>