Add parser tests for bug 52468 and bug 52363.
[mediawiki.git] / img_auth.php
blobc8759ec82aa54c4e1e0e80ef740b074e5a138ad7
1 <?php
2 /**
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
10 * Optional Parameters
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
39 * @file
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/" );
51 wfImageAuthMain();
52 wfLogProfilingData();
54 function wfImageAuthMain() {
55 global $wgImgAuthPublicTest, $wgRequest;
57 // See if this is a public Wiki (no protections).
58 if ( $wgImgAuthPublicTest
59 && in_array( 'read', User::getGroupPermissions( array( '*' ) ), true )
60 ) {
61 // This is a public wiki, so disable this script (for private wikis only)
62 wfForbidden( 'img-auth-accessdenied', 'img-auth-public' );
63 return;
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' );
70 return;
72 $path = $matches['title'];
73 if ( $path && $path[0] !== '/' ) {
74 // Make sure $path has a leading /
75 $path = "/" . $path;
78 // Check for bug 28235: QUERY_STRING overriding the correct extension
79 $whitelist = array();
80 $dotPos = strrpos( $path, '.' );
81 if ( $dotPos !== false ) {
82 $whitelist[] = substr( $path, $dotPos + 1 );
84 if ( !$wgRequest->checkUrlExtension( $whitelist ) ) {
85 return;
88 // Get the local file repository
89 $repo = RepoGroup::singleton()->getRepo( 'local' );
91 // Get the full file storage path and extract the source file name.
92 // (e.g. 120px-Foo.png => Foo.png or page2-120px-Foo.png => Foo.png).
93 // This only applies to thumbnails, and all thumbnails should
94 // be under a folder that has the source file name.
95 if ( strpos( $path, '/thumb/' ) === 0 ) {
96 $name = wfBaseName( dirname( $path ) ); // file is a thumbnail
97 $filename = $repo->getZonePath( 'thumb' ) . substr( $path, 6 ); // strip "/thumb"
98 } else {
99 $name = wfBaseName( $path ); // file is a source file
100 $filename = $repo->getZonePath( 'public' ) . $path;
103 // Check to see if the file exists
104 if ( !$repo->fileExists( $filename ) ) {
105 wfForbidden( 'img-auth-accessdenied', 'img-auth-nofile', $filename );
106 return;
109 $title = Title::makeTitleSafe( NS_FILE, $name );
110 if ( !$title instanceof Title ) { // files have valid titles
111 wfForbidden( 'img-auth-accessdenied', 'img-auth-badtitle', $name );
112 return;
115 // Run hook for extension authorization plugins
116 /** @var $result array */
117 $result = null;
118 if ( !wfRunHooks( 'ImgAuthBeforeStream', array( &$title, &$path, &$name, &$result ) ) ) {
119 wfForbidden( $result[0], $result[1], array_slice( $result, 2 ) );
120 return;
123 // Check user authorization for this title
124 // Checks Whitelist too
125 if ( !$title->userCan( 'read' ) ) {
126 wfForbidden( 'img-auth-accessdenied', 'img-auth-noread', $name );
127 return;
130 // Stream the requested file
131 wfDebugLog( 'img_auth', "Streaming `" . $filename . "`." );
132 $repo->streamFile( $filename, array( 'Cache-Control: private', 'Vary: Cookie' ) );
136 * Issue a standard HTTP 403 Forbidden header ($msg1-a message index, not a message) and an
137 * error message ($msg2, also a message index), (both required) then end the script
138 * subsequent arguments to $msg2 will be passed as parameters only for replacing in $msg2
139 * @param $msg1
140 * @param $msg2
142 function wfForbidden( $msg1, $msg2 ) {
143 global $wgImgAuthDetails;
145 $args = func_get_args();
146 array_shift( $args );
147 array_shift( $args );
149 $msgHdr = wfMessage( $msg1 )->escaped();
150 $detailMsgKey = $wgImgAuthDetails ? $msg2 : 'badaccess-group0';
151 $detailMsg = wfMessage( $detailMsgKey, $args )->escaped();
153 wfDebugLog( 'img_auth',
154 "wfForbidden Hdr: " . wfMessage( $msg1 )->inLanguage( 'en' )->text() . " Msg: " .
155 wfMessage( $msg2, $args )->inLanguage( 'en' )->text()
158 header( 'HTTP/1.0 403 Forbidden' );
159 header( 'Cache-Control: no-cache' );
160 header( 'Content-Type: text/html; charset=utf-8' );
161 echo <<<ENDS
162 <html>
163 <body>
164 <h1>$msgHdr</h1>
165 <p>$detailMsg</p>
166 </body>
167 </html>
168 ENDS;