In thumb.php:
[lhc/web/wiklou.git] / thumb.php
1 <?php
2
3 /**
4 * PHP script to stream out an image thumbnail.
5 *
6 * @addtogroup Media
7 */
8 define( 'MW_NO_OUTPUT_COMPRESSION', 1 );
9 require_once( './includes/WebStart.php' );
10
11 $wgTrivialMimeDetection = true; //don't use fancy mime detection, just check the file extension for jpg/gif/png.
12
13 require_once( "$IP/includes/StreamFile.php" );
14
15 wfThumbMain();
16 wfLogProfilingData();
17
18 //--------------------------------------------------------------------------
19
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;
27 }
28
29 $fileName = isset( $params['f'] ) ? $params['f'] : '';
30 unset( $params['f'] );
31
32 // Backwards compatibility parameters
33 if ( isset( $params['w'] ) ) {
34 $params['width'] = $params['w'];
35 unset( $params['w'] );
36 }
37 if ( isset( $params['p'] ) ) {
38 $params['page'] = $params['p'];
39 }
40 unset( $params['r'] );
41
42 // Some basic input validation
43 $fileName = strtr( $fileName, '\\/', '__' );
44
45 $img = wfLocalFile( $fileName );
46 if ( !$img ) {
47 wfThumbError( 404, wfMsg( 'badtitletext' ) );
48 return;
49 }
50 if ( !$img->exists() ) {
51 wfThumbError( 404, 'The source file for the specified thumbnail does not exist.' );
52 return;
53 }
54 $sourcePath = $img->getPath();
55 if ( $sourcePath === false ) {
56 wfThumbError( 500, 'The source file is not locally accessible.' );
57 return;
58 }
59
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;
73 }
74 }
75
76 // Stream the file if it exists already
77 try {
78 if ( false != ( $thumbName = $img->thumbName( $params ) ) ) {
79 $thumbPath = $img->getThumbPath( $thumbName );
80
81 if ( is_file( $thumbPath ) ) {
82 wfStreamFile( $thumbPath );
83 return;
84 }
85 }
86 } catch ( MWException $e ) {
87 wfThumbError( 500, $e->getHTML() );
88 return;
89 }
90
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;
96 }
97
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() );
110 }
111 if ( $errorMsg !== false ) {
112 wfThumbError( 500, $errorMsg );
113 }
114
115 wfProfileOut( __METHOD__ );
116 }
117
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' );
125 }
126 echo <<<EOT
127 <html><head><title>Error generating thumbnail</title></head>
128 <body>
129 <h1>Error generating thumbnail</h1>
130 <p>
131 $msg
132 </p>
133 </body>
134 </html>
135
136 EOT;
137 }
138