]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - thumb.php
MediaWiki 1.17.1-scripts
[autoinstalls/mediawiki.git] / thumb.php
1 <?php
2
3 /**
4  * PHP script to stream out an image thumbnail.
5  *
6  * @file
7  * @ingroup Media
8  */
9 define( 'MW_NO_OUTPUT_COMPRESSION', 1 );
10 require_once( './includes/WebStart.php' );
11
12 $wgTrivialMimeDetection = true; //don't use fancy mime detection, just check the file extension for jpg/gif/png.
13
14 require_once( "$IP/includes/StreamFile.php" );
15
16 wfThumbMain();
17 wfLogProfilingData();
18
19 //--------------------------------------------------------------------------
20
21 function wfThumbMain() {
22         wfProfileIn( __METHOD__ );
23
24         $headers = array();
25
26         // Get input parameters
27         if ( get_magic_quotes_gpc() ) {
28                 $params = array_map( 'stripslashes', $_REQUEST );
29         } else {
30                 $params = $_REQUEST;
31         }
32
33         $fileName = isset( $params['f'] ) ? $params['f'] : '';
34         unset( $params['f'] );
35
36         // Backwards compatibility parameters
37         if ( isset( $params['w'] ) ) {
38                 $params['width'] = $params['w'];
39                 unset( $params['w'] );
40         }
41         if ( isset( $params['p'] ) ) {
42                 $params['page'] = $params['p'];
43         }
44         unset( $params['r'] );
45
46         // Is this a thumb of an archived file?
47         $isOld = (isset( $params['archived'] ) && $params['archived']);
48         unset( $params['archived'] );
49
50         // Some basic input validation
51         $fileName = strtr( $fileName, '\\/', '__' );
52
53         // Actually fetch the image. Method depends on whether it is archived or not.
54         if( $isOld ) {
55                 // Format is <timestamp>!<name>
56                 $bits = explode( '!', $fileName, 2 );
57                 if( !isset($bits[1]) ) {
58                         wfThumbError( 404, wfMsg( 'badtitletext' ) );
59                         wfProfileOut( __METHOD__ );
60                         return;
61                 }
62                 $title = Title::makeTitleSafe( NS_FILE, $bits[1] );
63                 if( is_null($title) ) {
64                         wfThumbError( 404, wfMsg( 'badtitletext' ) );
65                         wfProfileOut( __METHOD__ );
66                         return;
67                 }
68                 $img = RepoGroup::singleton()->getLocalRepo()->newFromArchiveName( $title, $fileName );
69         } else {
70                 $img = wfLocalFile( $fileName );
71         }
72
73         // Check permissions if there are read restrictions
74         if ( !in_array( 'read', User::getGroupPermissions( array( '*' ) ), true ) ) {
75                 if ( !$img->getTitle()->userCanRead() ) {
76                         wfThumbError( 403, 'Access denied. You do not have permission to access ' . 
77                                 'the source file.' );
78                         wfProfileOut( __METHOD__ );
79                         return;
80                 }
81                 $headers[] = 'Cache-Control: private';
82                 $headers[] = 'Vary: Cookie';
83         }
84
85         if ( !$img ) {
86                 wfThumbError( 404, wfMsg( 'badtitletext' ) );
87                 wfProfileOut( __METHOD__ );
88                 return;
89         }
90         if ( !$img->exists() ) {
91                 wfThumbError( 404, 'The source file for the specified thumbnail does not exist.' );
92                 wfProfileOut( __METHOD__ );
93                 return;
94         }
95         $sourcePath = $img->getPath();
96         if ( $sourcePath === false ) {
97                 wfThumbError( 500, 'The source file is not locally accessible.' );
98                 wfProfileOut( __METHOD__ );
99                 return;
100         }
101
102         // Check IMS against the source file
103         // This means that clients can keep a cached copy even after it has been deleted on the server
104         if ( !empty( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ) {
105                 // Fix IE brokenness
106                 $imsString = preg_replace( '/;.*$/', '', $_SERVER["HTTP_IF_MODIFIED_SINCE"] );
107                 // Calculate time
108                 wfSuppressWarnings();
109                 $imsUnix = strtotime( $imsString );
110                 $stat = stat( $sourcePath );
111                 wfRestoreWarnings();
112                 if ( $stat['mtime'] <= $imsUnix ) {
113                         header( 'HTTP/1.1 304 Not Modified' );
114                         wfProfileOut( __METHOD__ );
115                         return;
116                 }
117         }
118
119         // Stream the file if it exists already
120         try {
121                 if ( false != ( $thumbName = $img->thumbName( $params ) ) ) {
122                         $thumbPath = $img->getThumbPath( $thumbName );
123
124                         if ( is_file( $thumbPath ) ) {
125                                 wfStreamFile( $thumbPath, $headers );
126                                 wfProfileOut( __METHOD__ );
127                                 return;
128                         }
129                 }
130         } catch ( MWException $e ) {
131                 wfThumbError( 500, $e->getHTML() );
132                 wfProfileOut( __METHOD__ );
133                 return;
134         }
135
136         try {
137                 $thumb = $img->transform( $params, File::RENDER_NOW );
138         } catch( Exception $ex ) {
139                 // Tried to select a page on a non-paged file?
140                 $thumb = false;
141         }
142
143         $errorMsg = false;
144         if ( !$thumb ) {
145                 $errorMsg = wfMsgHtml( 'thumbnail_error', 'File::transform() returned false' );
146         } elseif ( $thumb->isError() ) {
147                 $errorMsg = $thumb->getHtmlMsg();
148         } elseif ( !$thumb->getPath() ) {
149                 $errorMsg = wfMsgHtml( 'thumbnail_error', 'No path supplied in thumbnail object' );
150         } elseif ( $thumb->getPath() == $img->getPath() ) {
151                 $errorMsg = wfMsgHtml( 'thumbnail_error', 'Image was not scaled, ' .
152                         'is the requested width bigger than the source?' );
153         } else {
154                 wfStreamFile( $thumb->getPath(), $headers );
155         }
156         if ( $errorMsg !== false ) {
157                 wfThumbError( 500, $errorMsg );
158         }
159
160         wfProfileOut( __METHOD__ );
161 }
162
163 function wfThumbError( $status, $msg ) {
164         global $wgShowHostnames;
165         header( 'Cache-Control: no-cache' );
166         header( 'Content-Type: text/html; charset=utf-8' );
167         if ( $status == 404 ) {
168                 header( 'HTTP/1.1 404 Not found' );
169         } elseif ( $status == 403 ) {
170                 header( 'HTTP/1.1 403 Forbidden' );
171                 header( 'Vary: Cookie' );
172         } else {
173                 header( 'HTTP/1.1 500 Internal server error' );
174         }
175         if( $wgShowHostnames ) {
176                 $url = htmlspecialchars( isset( $_SERVER['REQUEST_URI'] ) ? $_SERVER['REQUEST_URI'] : '' );
177                 $hostname = htmlspecialchars( wfHostname() );
178                 $debug = "<!-- $url -->\n<!-- $hostname -->\n";
179         } else {
180                 $debug = "";
181         }
182         echo <<<EOT
183 <html><head><title>Error generating thumbnail</title></head>
184 <body>
185 <h1>Error generating thumbnail</h1>
186 <p>
187 $msg
188 </p>
189 $debug
190 </body>
191 </html>
192
193 EOT;
194 }
195