* Update docs/skin.txt.
[mediawiki.git] / thumb.php
blob62c338200209a5b97877232bfc2c8f5c842b2fb3
1 <?php
3 /**
4 * PHP script to stream out an image thumbnail.
6 * @addtogroup Media
7 */
8 define( 'MW_NO_OUTPUT_COMPRESSION', 1 );
9 require_once( './includes/WebStart.php' );
11 $wgTrivialMimeDetection = true; //don't use fancy mime detection, just check the file extension for jpg/gif/png.
13 require_once( "$IP/includes/StreamFile.php" );
15 wfThumbMain();
16 wfLogProfilingData();
18 //--------------------------------------------------------------------------
20 function wfThumbMain() {
21 wfProfileIn( __METHOD__ );
22 // Get input parameters
23 if ( get_magic_quotes_gpc() ) {
24 $params = array_map( 'stripslashes', $_REQUEST );
25 } else {
26 $params = $_REQUEST;
29 $fileName = isset( $params['f'] ) ? $params['f'] : '';
30 unset( $params['f'] );
32 // Backwards compatibility parameters
33 if ( isset( $params['w'] ) ) {
34 $params['width'] = $params['w'];
35 unset( $params['w'] );
37 if ( isset( $params['p'] ) ) {
38 $params['page'] = $params['p'];
40 unset( $params['r'] );
42 // Some basic input validation
43 $fileName = strtr( $fileName, '\\/', '__' );
45 $img = wfLocalFile( $fileName );
46 if ( !$img ) {
47 wfThumbError( 404, wfMsg( 'badtitletext' ) );
48 return;
50 if ( !$img->exists() ) {
51 wfThumbError( 404, 'The source file for the specified thumbnail does not exist.' );
52 return;
54 $sourcePath = $img->getPath();
55 if ( $sourcePath === false ) {
56 wfThumbError( 500, 'The source file is not locally accessible.' );
57 return;
60 // Check IMS against the source file
61 // This means that clients can keep a cached copy even after it has been deleted on the server
62 if ( !empty( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ) {
63 // Fix IE brokenness
64 $imsString = preg_replace( '/;.*$/', '', $_SERVER["HTTP_IF_MODIFIED_SINCE"] );
65 // Calculate time
66 wfSuppressWarnings();
67 $imsUnix = strtotime( $imsString );
68 wfRestoreWarnings();
69 $stat = @stat( $sourcePath );
70 if ( $stat['mtime'] <= $imsUnix ) {
71 header( 'HTTP/1.1 304 Not Modified' );
72 return;
76 // Stream the file if it exists already
77 try {
78 if ( false != ( $thumbName = $img->thumbName( $params ) ) ) {
79 $thumbPath = $img->getThumbPath( $thumbName );
81 if ( is_file( $thumbPath ) ) {
82 wfStreamFile( $thumbPath );
83 return;
86 } catch ( MWException $e ) {
87 wfThumbError( 500, $e->getHTML() );
88 return;
91 try {
92 $thumb = $img->transform( $params, File::RENDER_NOW );
93 } catch( Exception $ex ) {
94 // Tried to select a page on a non-paged file?
95 $thumb = false;
98 $errorMsg = false;
99 if ( !$thumb ) {
100 $errorMsg = wfMsgHtml( 'thumbnail_error', 'File::transform() returned false' );
101 } elseif ( $thumb->isError() ) {
102 $errorMsg = $thumb->getHtmlMsg();
103 } elseif ( !$thumb->getPath() ) {
104 $errorMsg = wfMsgHtml( 'thumbnail_error', 'No path supplied in thumbnail object' );
105 } elseif ( $thumb->getPath() == $img->getPath() ) {
106 $errorMsg = wfMsgHtml( 'thumbnail_error', 'Image was not scaled, ' .
107 'is the requested width bigger than the source?' );
108 } else {
109 wfStreamFile( $thumb->getPath() );
111 if ( $errorMsg !== false ) {
112 wfThumbError( 500, $errorMsg );
115 wfProfileOut( __METHOD__ );
118 function wfThumbError( $status, $msg ) {
119 header( 'Cache-Control: no-cache' );
120 header( 'Content-Type: text/html; charset=utf-8' );
121 if ( $status == 404 ) {
122 header( 'HTTP/1.1 404 Not found' );
123 } else {
124 header( 'HTTP/1.1 500 Internal server error' );
126 echo <<<EOT
127 <html><head><title>Error generating thumbnail</title></head>
128 <body>
129 <h1>Error generating thumbnail</h1>
131 $msg
132 </p>
133 </body>
134 </html>
136 EOT;