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