Add support for Number grouping(commafy) based on CLDR number grouping patterns like...
[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 ( get_magic_quotes_gpc() ) {
30 $params = array_map( 'stripslashes', $_REQUEST );
31 } else {
32 $params = $_REQUEST;
33 }
34
35 $fileName = isset( $params['f'] ) ? $params['f'] : '';
36 unset( $params['f'] );
37
38 // Backwards compatibility parameters
39 if ( isset( $params['w'] ) ) {
40 $params['width'] = $params['w'];
41 unset( $params['w'] );
42 }
43 if ( isset( $params['p'] ) ) {
44 $params['page'] = $params['p'];
45 }
46 unset( $params['r'] ); // ignore 'r' because we unconditionally pass File::RENDER
47
48 // Is this a thumb of an archived file?
49 $isOld = (isset( $params['archived'] ) && $params['archived']);
50 unset( $params['archived'] );
51
52 // Some basic input validation
53 $fileName = strtr( $fileName, '\\/', '__' );
54
55 // Actually fetch the image. Method depends on whether it is archived or not.
56 if( $isOld ) {
57 // Format is <timestamp>!<name>
58 $bits = explode( '!', $fileName, 2 );
59 if( !isset($bits[1]) ) {
60 wfThumbError( 404, wfMsg( 'badtitletext' ) );
61 wfProfileOut( __METHOD__ );
62 return;
63 }
64 $title = Title::makeTitleSafe( NS_FILE, $bits[1] );
65 if( is_null($title) ) {
66 wfThumbError( 404, wfMsg( 'badtitletext' ) );
67 wfProfileOut( __METHOD__ );
68 return;
69 }
70 $img = RepoGroup::singleton()->getLocalRepo()->newFromArchiveName( $title, $fileName );
71 } else {
72 $img = wfLocalFile( $fileName );
73 }
74
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 ' .
79 'the source file.' );
80 wfProfileOut( __METHOD__ );
81 return;
82 }
83 $headers[] = 'Cache-Control: private';
84 $headers[] = 'Vary: Cookie';
85 }
86
87 if ( !$img ) {
88 wfThumbError( 404, wfMsg( 'badtitletext' ) );
89 wfProfileOut( __METHOD__ );
90 return;
91 }
92 if ( !$img->exists() ) {
93 wfThumbError( 404, 'The source file for the specified thumbnail does not exist.' );
94 wfProfileOut( __METHOD__ );
95 return;
96 }
97 $sourcePath = $img->getPath();
98 if ( $sourcePath === false ) {
99 wfThumbError( 500, 'The source file is not locally accessible.' );
100 wfProfileOut( __METHOD__ );
101 return;
102 }
103
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'] ) ) {
107 // Fix IE brokenness
108 $imsString = preg_replace( '/;.*$/', '', $_SERVER["HTTP_IF_MODIFIED_SINCE"] );
109 // Calculate time
110 wfSuppressWarnings();
111 $imsUnix = strtotime( $imsString );
112 $stat = stat( $sourcePath );
113 wfRestoreWarnings();
114 if ( $stat['mtime'] <= $imsUnix ) {
115 header( 'HTTP/1.1 304 Not Modified' );
116 wfProfileOut( __METHOD__ );
117 return;
118 }
119 }
120
121 // Stream the file if it exists already
122 try {
123 if ( false != ( $thumbName = $img->thumbName( $params ) ) ) {
124 $thumbPath = $img->getThumbPath( $thumbName );
125
126 if ( is_file( $thumbPath ) ) {
127 StreamFile::stream( $thumbPath, $headers );
128 wfProfileOut( __METHOD__ );
129 return;
130 }
131 }
132 } catch ( MWException $e ) {
133 wfThumbError( 500, $e->getHTML() );
134 wfProfileOut( __METHOD__ );
135 return;
136 }
137
138 try {
139 $thumb = $img->transform( $params, File::RENDER_NOW );
140 } catch( Exception $ex ) {
141 // Tried to select a page on a non-paged file?
142 $thumb = false;
143 }
144
145 $errorMsg = false;
146 if ( !$thumb ) {
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?' );
155 } else {
156 StreamFile::stream( $thumb->getPath(), $headers );
157 }
158 if ( $errorMsg !== false ) {
159 wfThumbError( 500, $errorMsg );
160 }
161
162 wfProfileOut( __METHOD__ );
163 }
164
165 function wfThumbError( $status, $msg ) {
166 global $wgShowHostnames;
167 header( 'Cache-Control: no-cache' );
168 header( 'Content-Type: text/html; charset=utf-8' );
169 if ( $status == 404 ) {
170 header( 'HTTP/1.1 404 Not found' );
171 } elseif ( $status == 403 ) {
172 header( 'HTTP/1.1 403 Forbidden' );
173 header( 'Vary: Cookie' );
174 } else {
175 header( 'HTTP/1.1 500 Internal server error' );
176 }
177 if( $wgShowHostnames ) {
178 $url = htmlspecialchars( isset( $_SERVER['REQUEST_URI'] ) ? $_SERVER['REQUEST_URI'] : '' );
179 $hostname = htmlspecialchars( wfHostname() );
180 $debug = "<!-- $url -->\n<!-- $hostname -->\n";
181 } else {
182 $debug = "";
183 }
184 echo <<<EOT
185 <html><head><title>Error generating thumbnail</title></head>
186 <body>
187 <h1>Error generating thumbnail</h1>
188 <p>
189 $msg
190 </p>
191 $debug
192 </body>
193 </html>
194
195 EOT;
196 }
197