4 * PHP script to stream out an image thumbnail.
9 define( 'MW_NO_OUTPUT_COMPRESSION', 1 );
10 require_once( './includes/WebStart.php' );
12 $wgTrivialMimeDetection = true; //don't use fancy mime detection, just check the file extension for jpg/gif/png.
14 require_once( "$IP/includes/StreamFile.php" );
19 //--------------------------------------------------------------------------
21 function wfThumbMain() {
22 wfProfileIn( __METHOD__
);
23 // Get input parameters
24 if ( get_magic_quotes_gpc() ) {
25 $params = array_map( 'stripslashes', $_REQUEST );
30 $fileName = isset( $params['f'] ) ?
$params['f'] : '';
31 unset( $params['f'] );
33 // Backwards compatibility parameters
34 if ( isset( $params['w'] ) ) {
35 $params['width'] = $params['w'];
36 unset( $params['w'] );
38 if ( isset( $params['p'] ) ) {
39 $params['page'] = $params['p'];
41 unset( $params['r'] );
43 // Is this a thumb of an archived file?
44 $isOld = (isset( $params['archived'] ) && $params['archived']);
45 unset( $params['archived'] );
47 // Some basic input validation
48 $fileName = strtr( $fileName, '\\/', '__' );
50 // Actually fetch the image. Method depends on whether it is archived or not.
52 // Format is <timestamp>!<name>
53 $bits = explode( '!', $fileName, 2 );
54 if( !isset($bits[1]) ) {
55 wfThumbError( 404, wfMsg( 'badtitletext' ) );
58 $title = Title
::makeTitleSafe( NS_FILE
, $bits[1] );
59 if( is_null($title) ) {
60 wfThumbError( 404, wfMsg( 'badtitletext' ) );
63 $img = RepoGroup
::singleton()->getLocalRepo()->newFromArchiveName( $title, $fileName );
65 $img = wfLocalFile( $fileName );
69 wfThumbError( 404, wfMsg( 'badtitletext' ) );
72 if ( !$img->exists() ) {
73 wfThumbError( 404, 'The source file for the specified thumbnail does not exist.' );
76 $sourcePath = $img->getPath();
77 if ( $sourcePath === false ) {
78 wfThumbError( 500, 'The source file is not locally accessible.' );
82 // Check IMS against the source file
83 // This means that clients can keep a cached copy even after it has been deleted on the server
84 if ( !empty( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ) {
86 $imsString = preg_replace( '/;.*$/', '', $_SERVER["HTTP_IF_MODIFIED_SINCE"] );
89 $imsUnix = strtotime( $imsString );
91 $stat = @stat
( $sourcePath );
92 if ( $stat['mtime'] <= $imsUnix ) {
93 header( 'HTTP/1.1 304 Not Modified' );
98 // Stream the file if it exists already
100 if ( false != ( $thumbName = $img->thumbName( $params ) ) ) {
101 $thumbPath = $img->getThumbPath( $thumbName );
103 if ( is_file( $thumbPath ) ) {
104 wfStreamFile( $thumbPath );
108 } catch ( MWException
$e ) {
109 wfThumbError( 500, $e->getHTML() );
114 $thumb = $img->transform( $params, File
::RENDER_NOW
);
115 } catch( Exception
$ex ) {
116 // Tried to select a page on a non-paged file?
122 $errorMsg = wfMsgHtml( 'thumbnail_error', 'File::transform() returned false' );
123 } elseif ( $thumb->isError() ) {
124 $errorMsg = $thumb->getHtmlMsg();
125 } elseif ( !$thumb->getPath() ) {
126 $errorMsg = wfMsgHtml( 'thumbnail_error', 'No path supplied in thumbnail object' );
127 } elseif ( $thumb->getPath() == $img->getPath() ) {
128 $errorMsg = wfMsgHtml( 'thumbnail_error', 'Image was not scaled, ' .
129 'is the requested width bigger than the source?' );
131 wfStreamFile( $thumb->getPath() );
133 if ( $errorMsg !== false ) {
134 wfThumbError( 500, $errorMsg );
137 wfProfileOut( __METHOD__
);
140 function wfThumbError( $status, $msg ) {
141 global $wgShowHostnames;
142 header( 'Cache-Control: no-cache' );
143 header( 'Content-Type: text/html; charset=utf-8' );
144 if ( $status == 404 ) {
145 header( 'HTTP/1.1 404 Not found' );
147 header( 'HTTP/1.1 500 Internal server error' );
149 if( $wgShowHostnames ) {
150 $url = htmlspecialchars( @$_SERVER['REQUEST_URI'] );
151 $hostname = htmlspecialchars( wfHostname() );
152 $debug = "<!-- $url -->\n<!-- $hostname -->\n";
157 <html><head><title>Error generating thumbnail</title></head>
159 <h1>Error generating thumbnail</h1>