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
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
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.
22 * Your server needs to support PATH_INFO; CGI-based configurations usually don't.
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.
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.
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
42 define( 'MW_NO_OUTPUT_COMPRESSION', 1 );
43 require __DIR__
. '/includes/WebStart.php';
44 wfProfileIn( 'img_auth.php' );
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/" );
53 // Commit and close up!
54 $factory = wfGetLBFactory();
55 $factory->commitMasterChanges();
58 function wfImageAuthMain() {
59 global $wgImgAuthPublicTest, $wgImgAuthUrlPathMap;
61 $request = RequestContext
::getMain()->getRequest();
62 $publicWiki = in_array( 'read', User
::getGroupPermissions( array( '*' ) ), true );
64 // See if this is a public Wiki (no protections).
65 if ( $wgImgAuthPublicTest && $publicWiki ) {
66 // This is a public wiki, so disable this script (for private wikis only)
67 wfForbidden( 'img-auth-accessdenied', 'img-auth-public' );
71 // Get the requested file path (source file or thumbnail)
72 $matches = WebRequest
::getPathInfo();
73 if ( !isset( $matches['title'] ) ) {
74 wfForbidden( 'img-auth-accessdenied', 'img-auth-nopathinfo' );
77 $path = $matches['title'];
78 if ( $path && $path[0] !== '/' ) {
79 // Make sure $path has a leading /
83 // Check for bug 28235: QUERY_STRING overriding the correct extension
85 $extension = FileBackend
::extensionFromPath( $path );
86 if ( $extension != '' ) {
87 $whitelist[] = $extension;
89 if ( !$request->checkUrlExtension( $whitelist ) ) {
93 // Various extensions may have their own backends that need access.
94 // Check if there is a special backend and storage base path for this file.
95 foreach ( $wgImgAuthUrlPathMap as $prefix => $storageDir ) {
96 $prefix = rtrim( $prefix, '/' ) . '/'; // implicit trailing slash
97 if ( strpos( $path, $prefix ) === 0 ) {
98 $be = FileBackendGroup
::singleton()->backendFromPath( $storageDir );
99 $filename = $storageDir . substr( $path, strlen( $prefix ) ); // strip prefix
100 // Check basic user authorization
101 if ( !RequestContext
::getMain()->getUser()->isAllowed( 'read' ) ) {
102 wfForbidden( 'img-auth-accessdenied', 'img-auth-noread', $path );
105 if ( $be->fileExists( array( 'src' => $filename ) ) ) {
106 wfDebugLog( 'img_auth', "Streaming `" . $filename . "`." );
107 $be->streamFile( array( 'src' => $filename ),
108 array( 'Cache-Control: private', 'Vary: Cookie' ) );
110 wfForbidden( 'img-auth-accessdenied', 'img-auth-nofile', $path );
116 // Get the local file repository
117 $repo = RepoGroup
::singleton()->getRepo( 'local' );
119 // Get the full file storage path and extract the source file name.
120 // (e.g. 120px-Foo.png => Foo.png or page2-120px-Foo.png => Foo.png).
121 // This only applies to thumbnails, and all thumbnails should
122 // be under a folder that has the source file name.
123 if ( strpos( $path, '/thumb/' ) === 0 ) {
124 $name = wfBaseName( dirname( $path ) ); // file is a thumbnail
125 $filename = $repo->getZonePath( 'thumb' ) . substr( $path, 6 ); // strip "/thumb"
126 // Check to see if the file exists
127 if ( !$repo->fileExists( $filename ) ) {
128 wfForbidden( 'img-auth-accessdenied', 'img-auth-nofile', $filename );
132 $name = wfBaseName( $path ); // file is a source file
133 $filename = $repo->getZonePath( 'public' ) . $path;
134 // Check to see if the file exists and is not deleted
135 $bits = explode( '!', $name, 2 );
136 if ( substr( $path, 0, 9 ) === '/archive/' && count( $bits ) == 2 ) {
137 $file = $repo->newFromArchiveName( $bits[1], $name );
139 $file = $repo->newFile( $name );
141 if ( !$file->exists() ||
$file->isDeleted( File
::DELETED_FILE
) ) {
142 wfForbidden( 'img-auth-accessdenied', 'img-auth-nofile', $filename );
147 $headers = array(); // extra HTTP headers to send
149 if ( !$publicWiki ) {
150 // For private wikis, run extra auth checks and set cache control headers
151 $headers[] = 'Cache-Control: private';
152 $headers[] = 'Vary: Cookie';
154 $title = Title
::makeTitleSafe( NS_FILE
, $name );
155 if ( !$title instanceof Title
) { // files have valid titles
156 wfForbidden( 'img-auth-accessdenied', 'img-auth-badtitle', $name );
160 // Run hook for extension authorization plugins
161 /** @var $result array */
163 if ( !wfRunHooks( 'ImgAuthBeforeStream', array( &$title, &$path, &$name, &$result ) ) ) {
164 wfForbidden( $result[0], $result[1], array_slice( $result, 2 ) );
168 // Check user authorization for this title
169 // Checks Whitelist too
170 if ( !$title->userCan( 'read' ) ) {
171 wfForbidden( 'img-auth-accessdenied', 'img-auth-noread', $name );
176 if ( $request->getCheck( 'download' ) ) {
177 $headers[] = 'Content-Disposition: attachment';
180 // Stream the requested file
181 wfDebugLog( 'img_auth', "Streaming `" . $filename . "`." );
182 $repo->streamFile( $filename, $headers );
186 * Issue a standard HTTP 403 Forbidden header ($msg1-a message index, not a message) and an
187 * error message ($msg2, also a message index), (both required) then end the script
188 * subsequent arguments to $msg2 will be passed as parameters only for replacing in $msg2
189 * @param string $msg1
190 * @param string $msg2
192 function wfForbidden( $msg1, $msg2 ) {
193 global $wgImgAuthDetails;
195 $args = func_get_args();
196 array_shift( $args );
197 array_shift( $args );
198 $args = ( isset( $args[0] ) && is_array( $args[0] ) ) ?
$args[0] : $args;
200 $msgHdr = wfMessage( $msg1 )->escaped();
201 $detailMsgKey = $wgImgAuthDetails ?
$msg2 : 'badaccess-group0';
202 $detailMsg = wfMessage( $detailMsgKey, $args )->escaped();
204 wfDebugLog( 'img_auth',
205 "wfForbidden Hdr: " . wfMessage( $msg1 )->inLanguage( 'en' )->text() . " Msg: " .
206 wfMessage( $msg2, $args )->inLanguage( 'en' )->text()
209 header( 'HTTP/1.0 403 Forbidden' );
210 header( 'Cache-Control: no-cache' );
211 header( 'Content-Type: text/html; charset=utf-8' );