3 * Image authorisation script
5 * To use this, see https://www.mediawiki.org/wiki/Manual:Image_Authorization
7 * - Set $wgUploadDirectory to a non-public directory (not web accessible)
8 * - Set $wgUploadPath to point to this file
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
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.
20 * Your server needs to support PATH_INFO; CGI-based configurations usually don't.
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.
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.
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
40 define( 'MW_NO_OUTPUT_COMPRESSION', 1 );
41 require __DIR__
. '/includes/WebStart.php';
42 wfProfileIn( 'img_auth.php' );
44 # Set action base paths so that WebRequest::getPathInfo()
45 # recognizes the "X" as the 'title' in ../img_auth.php/X urls.
46 $wgArticlePath = false; # Don't let a "/*" article path clober our action path
47 $wgActionPaths = array( "$wgUploadPath/" );
51 // Commit and close up!
52 $factory = wfGetLBFactory();
53 $factory->commitMasterChanges();
56 function wfImageAuthMain() {
57 global $wgImgAuthUrlPathMap;
59 $request = RequestContext
::getMain()->getRequest();
60 $publicWiki = in_array( 'read', User
::getGroupPermissions( array( '*' ) ), true );
62 // Get the requested file path (source file or thumbnail)
63 $matches = WebRequest
::getPathInfo();
64 if ( !isset( $matches['title'] ) ) {
65 wfForbidden( 'img-auth-accessdenied', 'img-auth-nopathinfo' );
68 $path = $matches['title'];
69 if ( $path && $path[0] !== '/' ) {
70 // Make sure $path has a leading /
74 // Check for bug 28235: QUERY_STRING overriding the correct extension
76 $extension = FileBackend
::extensionFromPath( $path, 'rawcase' );
77 if ( $extension != '' ) {
78 $whitelist[] = $extension;
80 if ( !$request->checkUrlExtension( $whitelist ) ) {
84 // Various extensions may have their own backends that need access.
85 // Check if there is a special backend and storage base path for this file.
86 foreach ( $wgImgAuthUrlPathMap as $prefix => $storageDir ) {
87 $prefix = rtrim( $prefix, '/' ) . '/'; // implicit trailing slash
88 if ( strpos( $path, $prefix ) === 0 ) {
89 $be = FileBackendGroup
::singleton()->backendFromPath( $storageDir );
90 $filename = $storageDir . substr( $path, strlen( $prefix ) ); // strip prefix
91 // Check basic user authorization
92 if ( !RequestContext
::getMain()->getUser()->isAllowed( 'read' ) ) {
93 wfForbidden( 'img-auth-accessdenied', 'img-auth-noread', $path );
96 if ( $be->fileExists( array( 'src' => $filename ) ) ) {
97 wfDebugLog( 'img_auth', "Streaming `" . $filename . "`." );
98 $be->streamFile( array( 'src' => $filename ),
99 array( 'Cache-Control: private', 'Vary: Cookie' ) );
101 wfForbidden( 'img-auth-accessdenied', 'img-auth-nofile', $path );
107 // Get the local file repository
108 $repo = RepoGroup
::singleton()->getRepo( 'local' );
109 $zone = strstr( ltrim( $path, '/' ), '/', true );
111 // Get the full file storage path and extract the source file name.
112 // (e.g. 120px-Foo.png => Foo.png or page2-120px-Foo.png => Foo.png).
113 // This only applies to thumbnails/transcoded, and each of them should
114 // be under a folder that has the source file name.
115 if ( $zone === 'thumb' ||
$zone === 'transcoded' ) {
116 $name = wfBaseName( dirname( $path ) );
117 $filename = $repo->getZonePath( $zone ) . substr( $path, strlen( "/".$zone ) );
118 // Check to see if the file exists
119 if ( !$repo->fileExists( $filename ) ) {
120 wfForbidden( 'img-auth-accessdenied', 'img-auth-nofile', $filename );
124 $name = wfBaseName( $path ); // file is a source file
125 $filename = $repo->getZonePath( 'public' ) . $path;
126 // Check to see if the file exists and is not deleted
127 $bits = explode( '!', $name, 2 );
128 if ( substr( $path, 0, 9 ) === '/archive/' && count( $bits ) == 2 ) {
129 $file = $repo->newFromArchiveName( $bits[1], $name );
131 $file = $repo->newFile( $name );
133 if ( !$file->exists() ||
$file->isDeleted( File
::DELETED_FILE
) ) {
134 wfForbidden( 'img-auth-accessdenied', 'img-auth-nofile', $filename );
139 $headers = array(); // extra HTTP headers to send
141 if ( !$publicWiki ) {
142 // For private wikis, run extra auth checks and set cache control headers
143 $headers[] = 'Cache-Control: private';
144 $headers[] = 'Vary: Cookie';
146 $title = Title
::makeTitleSafe( NS_FILE
, $name );
147 if ( !$title instanceof Title
) { // files have valid titles
148 wfForbidden( 'img-auth-accessdenied', 'img-auth-badtitle', $name );
152 // Run hook for extension authorization plugins
153 /** @var $result array */
155 if ( !wfRunHooks( 'ImgAuthBeforeStream', array( &$title, &$path, &$name, &$result ) ) ) {
156 wfForbidden( $result[0], $result[1], array_slice( $result, 2 ) );
160 // Check user authorization for this title
161 // Checks Whitelist too
162 if ( !$title->userCan( 'read' ) ) {
163 wfForbidden( 'img-auth-accessdenied', 'img-auth-noread', $name );
168 if ( $request->getCheck( 'download' ) ) {
169 $headers[] = 'Content-Disposition: attachment';
172 // Stream the requested file
173 wfDebugLog( 'img_auth', "Streaming `" . $filename . "`." );
174 $repo->streamFile( $filename, $headers );
178 * Issue a standard HTTP 403 Forbidden header ($msg1-a message index, not a message) and an
179 * error message ($msg2, also a message index), (both required) then end the script
180 * subsequent arguments to $msg2 will be passed as parameters only for replacing in $msg2
181 * @param string $msg1
182 * @param string $msg2
184 function wfForbidden( $msg1, $msg2 ) {
185 global $wgImgAuthDetails;
187 $args = func_get_args();
188 array_shift( $args );
189 array_shift( $args );
190 $args = ( isset( $args[0] ) && is_array( $args[0] ) ) ?
$args[0] : $args;
192 $msgHdr = wfMessage( $msg1 )->escaped();
193 $detailMsgKey = $wgImgAuthDetails ?
$msg2 : 'badaccess-group0';
194 $detailMsg = wfMessage( $detailMsgKey, $args )->escaped();
196 wfDebugLog( 'img_auth',
197 "wfForbidden Hdr: " . wfMessage( $msg1 )->inLanguage( 'en' )->text() . " Msg: " .
198 wfMessage( $msg2, $args )->inLanguage( 'en' )->text()
201 header( 'HTTP/1.0 403 Forbidden' );
202 header( 'Cache-Control: no-cache' );
203 header( 'Content-Type: text/html; charset=utf-8' );