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 if ( isset( $_SERVER['MW_COMPILED'] ) ) {
44 require ( 'core/includes/WebStart.php' );
46 require ( __DIR__
. '/includes/WebStart.php' );
48 wfProfileIn( 'img_auth.php' );
50 # Set action base paths so that WebRequest::getPathInfo()
51 # recognizes the "X" as the 'title' in ../img_auth.php/X urls.
52 $wgArticlePath = false; # Don't let a "/*" article path clober our action path
53 $wgActionPaths = array( "$wgUploadPath/" );
58 function wfImageAuthMain() {
59 global $wgImgAuthPublicTest, $wgRequest;
61 // See if this is a public Wiki (no protections).
62 if ( $wgImgAuthPublicTest
63 && in_array( 'read', User
::getGroupPermissions( array( '*' ) ), true )
65 // This is a public wiki, so disable this script (for private wikis only)
66 wfForbidden( 'img-auth-accessdenied', 'img-auth-public' );
70 // Get the requested file path (source file or thumbnail)
71 $matches = WebRequest
::getPathInfo();
72 if ( !isset( $matches['title'] ) ) {
73 wfForbidden( 'img-auth-accessdenied', 'img-auth-nopathinfo' );
76 $path = $matches['title'];
77 if ( $path && $path[0] !== '/' ) {
78 // Make sure $path has a leading /
82 // Check for bug 28235: QUERY_STRING overriding the correct extension
84 $dotPos = strrpos( $path, '.' );
85 if ( $dotPos !== false ) {
86 $whitelist[] = substr( $path, $dotPos +
1 );
88 if ( !$wgRequest->checkUrlExtension( $whitelist ) ) {
92 // Get the local file repository
93 $repo = RepoGroup
::singleton()->getRepo( 'local' );
95 // Get the full file storage path and extract the source file name.
96 // (e.g. 120px-Foo.png => Foo.png or page2-120px-Foo.png => Foo.png).
97 // This only applies to thumbnails, and all thumbnails should
98 // be under a folder that has the source file name.
99 if ( strpos( $path, '/thumb/' ) === 0 ) {
100 $name = wfBaseName( dirname( $path ) ); // file is a thumbnail
101 $filename = $repo->getZonePath( 'thumb' ) . substr( $path, 6 ); // strip "/thumb"
103 $name = wfBaseName( $path ); // file is a source file
104 $filename = $repo->getZonePath( 'public' ) . $path;
107 // Check to see if the file exists
108 if ( !$repo->fileExists( $filename ) ) {
109 wfForbidden( 'img-auth-accessdenied', 'img-auth-nofile', $filename );
113 $title = Title
::makeTitleSafe( NS_FILE
, $name );
114 if ( !$title instanceof Title
) { // files have valid titles
115 wfForbidden( 'img-auth-accessdenied', 'img-auth-badtitle', $name );
119 // Run hook for extension authorization plugins
120 if ( !wfRunHooks( 'ImgAuthBeforeStream', array( &$title, &$path, &$name, &$result ) ) ) {
121 wfForbidden( $result[0], $result[1], array_slice( $result, 2 ) );
125 // Check user authorization for this title
126 // Checks Whitelist too
127 if ( !$title->userCan( 'read' ) ) {
128 wfForbidden( 'img-auth-accessdenied', 'img-auth-noread', $name );
132 // Stream the requested file
133 wfDebugLog( 'img_auth', "Streaming `" . $filename . "`." );
134 $repo->streamFile( $filename, array( 'Cache-Control: private', 'Vary: Cookie' ) );
138 * Issue a standard HTTP 403 Forbidden header ($msg1-a message index, not a message) and an
139 * error message ($msg2, also a message index), (both required) then end the script
140 * subsequent arguments to $msg2 will be passed as parameters only for replacing in $msg2
144 function wfForbidden( $msg1, $msg2 ) {
145 global $wgImgAuthDetails;
147 $args = func_get_args();
148 array_shift( $args );
149 array_shift( $args );
151 $msgHdr = wfMessage( $msg1 )->escaped();
152 $detailMsgKey = $wgImgAuthDetails ?
$msg2 : 'badaccess-group0';
153 $detailMsg = wfMessage( $detailMsgKey, $args )->escaped();
155 wfDebugLog( 'img_auth',
156 "wfForbidden Hdr: " . wfMessage( $msg1 )->inLanguage( 'en' )->text() . " Msg: " .
157 wfMessage( $msg2, $args )->inLanguage( 'en' )->text()
160 header( 'HTTP/1.0 403 Forbidden' );
161 header( 'Cache-Control: no-cache' );
162 header( 'Content-Type: text/html; charset=utf-8' );