Fix undefined variable in SpecialMergeHistory
[lhc/web/wiklou.git] / img_auth.php
1 <?php
2 /**
3 * Image authorisation script
4 *
5 * To use this, see https://www.mediawiki.org/wiki/Manual:Image_Authorization
6 *
7 * - Set $wgUploadDirectory to a non-public directory (not web accessible)
8 * - Set $wgUploadPath to point to this file
9 *
10 * Optional Parameters
11 *
12 * - Set $wgImgAuthDetails = true if you want the reason the access was denied messages to
13 * be displayed instead of just the 403 error (doesn't work on IE anyway),
14 * otherwise it will only appear in error logs
15 *
16 * For security reasons, you usually don't want your user to know *why* access was denied,
17 * just that it was. If you want to change this, you can set $wgImgAuthDetails to 'true'
18 * in localsettings.php and it will give the user the reason why access was denied.
19 *
20 * Your server needs to support PATH_INFO; CGI-based configurations usually don't.
21 *
22 * This program is free software; you can redistribute it and/or modify
23 * it under the terms of the GNU General Public License as published by
24 * the Free Software Foundation; either version 2 of the License, or
25 * (at your option) any later version.
26 *
27 * This program is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 * GNU General Public License for more details.
31 *
32 * You should have received a copy of the GNU General Public License along
33 * with this program; if not, write to the Free Software Foundation, Inc.,
34 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
35 * http://www.gnu.org/copyleft/gpl.html
36 *
37 * @file
38 */
39
40 define( 'MW_NO_OUTPUT_COMPRESSION', 1 );
41 require __DIR__ . '/includes/WebStart.php';
42
43 # Set action base paths so that WebRequest::getPathInfo()
44 # recognizes the "X" as the 'title' in ../img_auth.php/X urls.
45 $wgArticlePath = false; # Don't let a "/*" article path clober our action path
46 $wgActionPaths = array( "$wgUploadPath/" );
47
48 wfImageAuthMain();
49 wfLogProfilingData();
50 // Commit and close up!
51 $factory = wfGetLBFactory();
52 $factory->commitMasterChanges();
53 $factory->shutdown();
54
55 function wfImageAuthMain() {
56 global $wgImgAuthUrlPathMap;
57
58 $request = RequestContext::getMain()->getRequest();
59 $publicWiki = in_array( 'read', User::getGroupPermissions( array( '*' ) ), true );
60
61 // Get the requested file path (source file or thumbnail)
62 $matches = WebRequest::getPathInfo();
63 if ( !isset( $matches['title'] ) ) {
64 wfForbidden( 'img-auth-accessdenied', 'img-auth-nopathinfo' );
65 return;
66 }
67 $path = $matches['title'];
68 if ( $path && $path[0] !== '/' ) {
69 // Make sure $path has a leading /
70 $path = "/" . $path;
71 }
72
73 // Check for bug 28235: QUERY_STRING overriding the correct extension
74 $whitelist = array();
75 $extension = FileBackend::extensionFromPath( $path, 'rawcase' );
76 if ( $extension != '' ) {
77 $whitelist[] = $extension;
78 }
79 if ( !$request->checkUrlExtension( $whitelist ) ) {
80 return;
81 }
82
83 // Various extensions may have their own backends that need access.
84 // Check if there is a special backend and storage base path for this file.
85 foreach ( $wgImgAuthUrlPathMap as $prefix => $storageDir ) {
86 $prefix = rtrim( $prefix, '/' ) . '/'; // implicit trailing slash
87 if ( strpos( $path, $prefix ) === 0 ) {
88 $be = FileBackendGroup::singleton()->backendFromPath( $storageDir );
89 $filename = $storageDir . substr( $path, strlen( $prefix ) ); // strip prefix
90 // Check basic user authorization
91 if ( !RequestContext::getMain()->getUser()->isAllowed( 'read' ) ) {
92 wfForbidden( 'img-auth-accessdenied', 'img-auth-noread', $path );
93 return;
94 }
95 if ( $be->fileExists( array( 'src' => $filename ) ) ) {
96 wfDebugLog( 'img_auth', "Streaming `" . $filename . "`." );
97 $be->streamFile( array( 'src' => $filename ),
98 array( 'Cache-Control: private', 'Vary: Cookie' ) );
99 } else {
100 wfForbidden( 'img-auth-accessdenied', 'img-auth-nofile', $path );
101 }
102 return;
103 }
104 }
105
106 // Get the local file repository
107 $repo = RepoGroup::singleton()->getRepo( 'local' );
108 $zone = strstr( ltrim( $path, '/' ), '/', true );
109
110 // Get the full file storage path and extract the source file name.
111 // (e.g. 120px-Foo.png => Foo.png or page2-120px-Foo.png => Foo.png).
112 // This only applies to thumbnails/transcoded, and each of them should
113 // be under a folder that has the source file name.
114 if ( $zone === 'thumb' || $zone === 'transcoded' ) {
115 $name = wfBaseName( dirname( $path ) );
116 $filename = $repo->getZonePath( $zone ) . substr( $path, strlen( "/" . $zone ) );
117 // Check to see if the file exists
118 if ( !$repo->fileExists( $filename ) ) {
119 wfForbidden( 'img-auth-accessdenied', 'img-auth-nofile', $filename );
120 return;
121 }
122 } else {
123 $name = wfBaseName( $path ); // file is a source file
124 $filename = $repo->getZonePath( 'public' ) . $path;
125 // Check to see if the file exists and is not deleted
126 $bits = explode( '!', $name, 2 );
127 if ( substr( $path, 0, 9 ) === '/archive/' && count( $bits ) == 2 ) {
128 $file = $repo->newFromArchiveName( $bits[1], $name );
129 } else {
130 $file = $repo->newFile( $name );
131 }
132 if ( !$file->exists() || $file->isDeleted( File::DELETED_FILE ) ) {
133 wfForbidden( 'img-auth-accessdenied', 'img-auth-nofile', $filename );
134 return;
135 }
136 }
137
138 $headers = array(); // extra HTTP headers to send
139
140 if ( !$publicWiki ) {
141 // For private wikis, run extra auth checks and set cache control headers
142 $headers[] = 'Cache-Control: private';
143 $headers[] = 'Vary: Cookie';
144
145 $title = Title::makeTitleSafe( NS_FILE, $name );
146 if ( !$title instanceof Title ) { // files have valid titles
147 wfForbidden( 'img-auth-accessdenied', 'img-auth-badtitle', $name );
148 return;
149 }
150
151 // Run hook for extension authorization plugins
152 /** @var $result array */
153 $result = null;
154 if ( !wfRunHooks( 'ImgAuthBeforeStream', array( &$title, &$path, &$name, &$result ) ) ) {
155 wfForbidden( $result[0], $result[1], array_slice( $result, 2 ) );
156 return;
157 }
158
159 // Check user authorization for this title
160 // Checks Whitelist too
161 if ( !$title->userCan( 'read' ) ) {
162 wfForbidden( 'img-auth-accessdenied', 'img-auth-noread', $name );
163 return;
164 }
165 }
166
167 if ( $request->getCheck( 'download' ) ) {
168 $headers[] = 'Content-Disposition: attachment';
169 }
170
171 // Stream the requested file
172 wfDebugLog( 'img_auth', "Streaming `" . $filename . "`." );
173 $repo->streamFile( $filename, $headers );
174 }
175
176 /**
177 * Issue a standard HTTP 403 Forbidden header ($msg1-a message index, not a message) and an
178 * error message ($msg2, also a message index), (both required) then end the script
179 * subsequent arguments to $msg2 will be passed as parameters only for replacing in $msg2
180 * @param string $msg1
181 * @param string $msg2
182 */
183 function wfForbidden( $msg1, $msg2 ) {
184 global $wgImgAuthDetails;
185
186 $args = func_get_args();
187 array_shift( $args );
188 array_shift( $args );
189 $args = ( isset( $args[0] ) && is_array( $args[0] ) ) ? $args[0] : $args;
190
191 $msgHdr = wfMessage( $msg1 )->escaped();
192 $detailMsgKey = $wgImgAuthDetails ? $msg2 : 'badaccess-group0';
193 $detailMsg = wfMessage( $detailMsgKey, $args )->escaped();
194
195 wfDebugLog( 'img_auth',
196 "wfForbidden Hdr: " . wfMessage( $msg1 )->inLanguage( 'en' )->text() . " Msg: " .
197 wfMessage( $msg2, $args )->inLanguage( 'en' )->text()
198 );
199
200 header( 'HTTP/1.0 403 Forbidden' );
201 header( 'Cache-Control: no-cache' );
202 header( 'Content-Type: text/html; charset=utf-8' );
203 echo <<<ENDS
204 <!DOCTYPE html>
205 <html>
206 <head>
207 <meta charset="UTF-8" />
208 <title>$msgHdr</title>
209 </head>
210 <body>
211 <h1>$msgHdr</h1>
212 <p>$detailMsg</p>
213 </body>
214 </html>
215 ENDS;
216 }