4 * PHP script to stream out an image thumbnail.
9 define( 'MW_NO_OUTPUT_COMPRESSION', 1 );
10 if ( isset( $_SERVER['MW_COMPILED'] ) ) {
11 require ( 'phase3/includes/WebStart.php' );
13 require ( dirname( __FILE__
) . '/includes/WebStart.php' );
16 $wgTrivialMimeDetection = true; //don't use fancy mime detection, just check the file extension for jpg/gif/png.
21 //--------------------------------------------------------------------------
23 function wfThumbMain() {
24 wfProfileIn( __METHOD__
);
28 // Get input parameters
29 if ( get_magic_quotes_gpc() ) {
30 $params = array_map( 'stripslashes', $_REQUEST );
35 $fileName = isset( $params['f'] ) ?
$params['f'] : '';
36 unset( $params['f'] );
38 // Backwards compatibility parameters
39 if ( isset( $params['w'] ) ) {
40 $params['width'] = $params['w'];
41 unset( $params['w'] );
43 if ( isset( $params['p'] ) ) {
44 $params['page'] = $params['p'];
46 unset( $params['r'] ); // ignore 'r' because we unconditionally pass File::RENDER
48 // Is this a thumb of an archived file?
49 $isOld = (isset( $params['archived'] ) && $params['archived']);
50 unset( $params['archived'] );
52 // Some basic input validation
53 $fileName = strtr( $fileName, '\\/', '__' );
55 // Actually fetch the image. Method depends on whether it is archived or not.
57 // Format is <timestamp>!<name>
58 $bits = explode( '!', $fileName, 2 );
59 if( !isset($bits[1]) ) {
60 wfThumbError( 404, wfMsg( 'badtitletext' ) );
61 wfProfileOut( __METHOD__
);
64 $title = Title
::makeTitleSafe( NS_FILE
, $bits[1] );
65 if( is_null($title) ) {
66 wfThumbError( 404, wfMsg( 'badtitletext' ) );
67 wfProfileOut( __METHOD__
);
70 $img = RepoGroup
::singleton()->getLocalRepo()->newFromArchiveName( $title, $fileName );
72 $img = wfLocalFile( $fileName );
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 ' .
80 wfProfileOut( __METHOD__
);
83 $headers[] = 'Cache-Control: private';
84 $headers[] = 'Vary: Cookie';
88 wfThumbError( 404, wfMsg( 'badtitletext' ) );
89 wfProfileOut( __METHOD__
);
92 if ( !$img->exists() ) {
93 wfThumbError( 404, 'The source file for the specified thumbnail does not exist.' );
94 wfProfileOut( __METHOD__
);
97 $sourcePath = $img->getPath();
98 if ( $sourcePath === false ) {
99 wfThumbError( 500, 'The source file is not locally accessible.' );
100 wfProfileOut( __METHOD__
);
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'] ) ) {
108 $imsString = preg_replace( '/;.*$/', '', $_SERVER["HTTP_IF_MODIFIED_SINCE"] );
110 wfSuppressWarnings();
111 $imsUnix = strtotime( $imsString );
112 $stat = stat( $sourcePath );
114 if ( $stat['mtime'] <= $imsUnix ) {
115 header( 'HTTP/1.1 304 Not Modified' );
116 wfProfileOut( __METHOD__
);
121 // Stream the file if it exists already
123 if ( false != ( $thumbName = $img->thumbName( $params ) ) ) {
124 $thumbPath = $img->getThumbPath( $thumbName );
126 if ( is_file( $thumbPath ) ) {
127 StreamFile
::stream( $thumbPath, $headers );
128 wfProfileOut( __METHOD__
);
132 } catch ( MWException
$e ) {
133 wfThumbError( 500, $e->getHTML() );
134 wfProfileOut( __METHOD__
);
139 $thumb = $img->transform( $params, File
::RENDER_NOW
);
140 } catch( Exception
$ex ) {
141 // Tried to select a page on a non-paged file?
147 $errorMsg = wfMsgHtml( 'thumbnail_error', 'File::transform() returned false' );
148 } elseif ( $thumb->isError() ) {
149 $errorMsg = $thumb->getHtmlMsg();
150 } elseif ( !$thumb->getPath() ) {
151 $errorMsg = wfMsgHtml( 'thumbnail_error', 'No path supplied in thumbnail object' );
152 } elseif ( $thumb->getPath() == $img->getPath() ) {
153 $errorMsg = wfMsgHtml( 'thumbnail_error', 'Image was not scaled, ' .
154 'is the requested width bigger than the source?' );
156 StreamFile
::stream( $thumb->getPath(), $headers );
158 if ( $errorMsg !== false ) {
159 wfThumbError( 500, $errorMsg );
162 wfProfileOut( __METHOD__
);
169 function wfThumbError( $status, $msg ) {
170 global $wgShowHostnames;
171 header( 'Cache-Control: no-cache' );
172 header( 'Content-Type: text/html; charset=utf-8' );
173 if ( $status == 404 ) {
174 header( 'HTTP/1.1 404 Not found' );
175 } elseif ( $status == 403 ) {
176 header( 'HTTP/1.1 403 Forbidden' );
177 header( 'Vary: Cookie' );
179 header( 'HTTP/1.1 500 Internal server error' );
181 if( $wgShowHostnames ) {
182 $url = htmlspecialchars( isset( $_SERVER['REQUEST_URI'] ) ?
$_SERVER['REQUEST_URI'] : '' );
183 $hostname = htmlspecialchars( wfHostname() );
184 $debug = "<!-- $url -->\n<!-- $hostname -->\n";
189 <html><head><title>Error generating thumbnail</title></head>
191 <h1>Error generating thumbnail</h1>