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