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