3 * Image authorisation script
5 * To use this, see http://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/" );
54 function wfImageAuthMain() {
55 global $wgImgAuthPublicTest, $wgImgAuthUrlPathMap, $wgRequest;
57 // See if this is a public Wiki (no protections).
58 if ( $wgImgAuthPublicTest
59 && in_array( 'read', User
::getGroupPermissions( array( '*' ) ), true )
61 // This is a public wiki, so disable this script (for private wikis only)
62 wfForbidden( 'img-auth-accessdenied', 'img-auth-public' );
66 // Get the requested file path (source file or thumbnail)
67 $matches = WebRequest
::getPathInfo();
68 if ( !isset( $matches['title'] ) ) {
69 wfForbidden( 'img-auth-accessdenied', 'img-auth-nopathinfo' );
72 $path = $matches['title'];
73 if ( $path && $path[0] !== '/' ) {
74 // Make sure $path has a leading /
78 // Check for bug 28235: QUERY_STRING overriding the correct extension
80 $extension = FileBackend
::extensionFromPath( $path );
81 if ( $extension != '' ) {
82 $whitelist[] = $extension;
84 if ( !$wgRequest->checkUrlExtension( $whitelist ) ) {
88 // Various extensions may have their own backends that need access.
89 // Check if there is a special backend and storage base path for this file.
90 foreach ( $wgImgAuthUrlPathMap as $prefix => $storageDir ) {
91 $prefix = rtrim( $prefix, '/' ) . '/'; // implicit trailing slash
92 if ( strpos( $path, $prefix ) === 0 ) {
93 $be = FileBackendGroup
::singleton()->backendFromPath( $storageDir );
94 $filename = $storageDir . substr( $path, strlen( $prefix ) ); // strip prefix
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' ) );
100 wfForbidden( 'img-auth-accessdenied', 'img-auth-nofile', $filename );
106 // Get the local file repository
107 $repo = RepoGroup
::singleton()->getRepo( 'local' );
109 // Get the full file storage path and extract the source file name.
110 // (e.g. 120px-Foo.png => Foo.png or page2-120px-Foo.png => Foo.png).
111 // This only applies to thumbnails, and all thumbnails should
112 // be under a folder that has the source file name.
113 if ( strpos( $path, '/thumb/' ) === 0 ) {
114 $name = wfBaseName( dirname( $path ) ); // file is a thumbnail
115 $filename = $repo->getZonePath( 'thumb' ) . substr( $path, 6 ); // strip "/thumb"
117 $name = wfBaseName( $path ); // file is a source file
118 $filename = $repo->getZonePath( 'public' ) . $path;
121 // Check to see if the file exists
122 if ( !$repo->fileExists( $filename ) ) {
123 wfForbidden( 'img-auth-accessdenied', 'img-auth-nofile', $filename );
127 $title = Title
::makeTitleSafe( NS_FILE
, $name );
128 if ( !$title instanceof Title
) { // files have valid titles
129 wfForbidden( 'img-auth-accessdenied', 'img-auth-badtitle', $name );
133 // Run hook for extension authorization plugins
134 /** @var $result array */
136 if ( !wfRunHooks( 'ImgAuthBeforeStream', array( &$title, &$path, &$name, &$result ) ) ) {
137 wfForbidden( $result[0], $result[1], array_slice( $result, 2 ) );
141 // Check user authorization for this title
142 // Checks Whitelist too
143 if ( !$title->userCan( 'read' ) ) {
144 wfForbidden( 'img-auth-accessdenied', 'img-auth-noread', $name );
148 // Stream the requested file
149 wfDebugLog( 'img_auth', "Streaming `" . $filename . "`." );
150 $repo->streamFile( $filename, array( 'Cache-Control: private', 'Vary: Cookie' ) );
154 * Issue a standard HTTP 403 Forbidden header ($msg1-a message index, not a message) and an
155 * error message ($msg2, also a message index), (both required) then end the script
156 * subsequent arguments to $msg2 will be passed as parameters only for replacing in $msg2
160 function wfForbidden( $msg1, $msg2 ) {
161 global $wgImgAuthDetails;
163 $args = func_get_args();
164 array_shift( $args );
165 array_shift( $args );
166 $args = ( isset( $args[0] ) && is_array( $args[0] ) ) ?
$args[0] : $args;
168 $msgHdr = wfMessage( $msg1 )->escaped();
169 $detailMsgKey = $wgImgAuthDetails ?
$msg2 : 'badaccess-group0';
170 $detailMsg = wfMessage( $detailMsgKey, $args )->escaped();
172 wfDebugLog( 'img_auth',
173 "wfForbidden Hdr: " . wfMessage( $msg1 )->inLanguage( 'en' )->text() . " Msg: " .
174 wfMessage( $msg2, $args )->inLanguage( 'en' )->text()
177 header( 'HTTP/1.0 403 Forbidden' );
178 header( 'Cache-Control: no-cache' );
179 header( 'Content-Type: text/html; charset=utf-8' );