Add noratelimit right to list of core rights, was missing.
[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 require_once( './includes/WebStart.php' );
11
12 $wgTrivialMimeDetection = true; //don't use fancy mime detection, just check the file extension for jpg/gif/png.
13
14 require_once( "$IP/includes/StreamFile.php" );
15
16 wfThumbMain();
17 wfLogProfilingData();
18
19 //--------------------------------------------------------------------------
20
21 function wfThumbMain() {
22 wfProfileIn( __METHOD__ );
23 // Get input parameters
24 if ( get_magic_quotes_gpc() ) {
25 $params = array_map( 'stripslashes', $_REQUEST );
26 } else {
27 $params = $_REQUEST;
28 }
29
30 $fileName = isset( $params['f'] ) ? $params['f'] : '';
31 unset( $params['f'] );
32
33 // Backwards compatibility parameters
34 if ( isset( $params['w'] ) ) {
35 $params['width'] = $params['w'];
36 unset( $params['w'] );
37 }
38 if ( isset( $params['p'] ) ) {
39 $params['page'] = $params['p'];
40 }
41 unset( $params['r'] );
42
43 // Some basic input validation
44 $fileName = strtr( $fileName, '\\/', '__' );
45
46 $img = wfLocalFile( $fileName );
47 if ( !$img ) {
48 wfThumbError( 404, wfMsg( 'badtitletext' ) );
49 return;
50 }
51 if ( !$img->exists() ) {
52 wfThumbError( 404, 'The source file for the specified thumbnail does not exist.' );
53 return;
54 }
55 $sourcePath = $img->getPath();
56 if ( $sourcePath === false ) {
57 wfThumbError( 500, 'The source file is not locally accessible.' );
58 return;
59 }
60
61 // Check IMS against the source file
62 // This means that clients can keep a cached copy even after it has been deleted on the server
63 if ( !empty( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ) {
64 // Fix IE brokenness
65 $imsString = preg_replace( '/;.*$/', '', $_SERVER["HTTP_IF_MODIFIED_SINCE"] );
66 // Calculate time
67 wfSuppressWarnings();
68 $imsUnix = strtotime( $imsString );
69 wfRestoreWarnings();
70 $stat = @stat( $sourcePath );
71 if ( $stat['mtime'] <= $imsUnix ) {
72 header( 'HTTP/1.1 304 Not Modified' );
73 return;
74 }
75 }
76
77 // Stream the file if it exists already
78 try {
79 if ( false != ( $thumbName = $img->thumbName( $params ) ) ) {
80 $thumbPath = $img->getThumbPath( $thumbName );
81
82 if ( is_file( $thumbPath ) ) {
83 wfStreamFile( $thumbPath );
84 return;
85 }
86 }
87 } catch ( MWException $e ) {
88 wfThumbError( 500, $e->getHTML() );
89 return;
90 }
91
92 try {
93 $thumb = $img->transform( $params, File::RENDER_NOW );
94 } catch( Exception $ex ) {
95 // Tried to select a page on a non-paged file?
96 $thumb = false;
97 }
98
99 $errorMsg = false;
100 if ( !$thumb ) {
101 $errorMsg = wfMsgHtml( 'thumbnail_error', 'File::transform() returned false' );
102 } elseif ( $thumb->isError() ) {
103 $errorMsg = $thumb->getHtmlMsg();
104 } elseif ( !$thumb->getPath() ) {
105 $errorMsg = wfMsgHtml( 'thumbnail_error', 'No path supplied in thumbnail object' );
106 } elseif ( $thumb->getPath() == $img->getPath() ) {
107 $errorMsg = wfMsgHtml( 'thumbnail_error', 'Image was not scaled, ' .
108 'is the requested width bigger than the source?' );
109 } else {
110 wfStreamFile( $thumb->getPath() );
111 }
112 if ( $errorMsg !== false ) {
113 wfThumbError( 500, $errorMsg );
114 }
115
116 wfProfileOut( __METHOD__ );
117 }
118
119 function wfThumbError( $status, $msg ) {
120 global $wgShowHostnames;
121 header( 'Cache-Control: no-cache' );
122 header( 'Content-Type: text/html; charset=utf-8' );
123 if ( $status == 404 ) {
124 header( 'HTTP/1.1 404 Not found' );
125 } else {
126 header( 'HTTP/1.1 500 Internal server error' );
127 }
128 if( $wgShowHostnames ) {
129 $url = htmlspecialchars( @$_SERVER['REQUEST_URI'] );
130 $hostname = htmlspecialchars( wfHostname() );
131 $debug = "<!-- $url -->\n<!-- $hostname -->\n";
132 } else {
133 $debug = "";
134 }
135 echo <<<EOT
136 <html><head><title>Error generating thumbnail</title></head>
137 <body>
138 <h1>Error generating thumbnail</h1>
139 <p>
140 $msg
141 </p>
142 $debug
143 </body>
144 </html>
145
146 EOT;
147 }
148