Update migrateUserGroup to deal with primary key issue
[mediawiki.git] / img_auth.php
blobd7125897c5f9941e629bb361aa961067ec3888ac
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 if ( isset( $_SERVER['MW_COMPILED'] ) ) {
44 require ( 'core/includes/WebStart.php' );
45 } else {
46 require ( dirname( __FILE__ ) . '/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 ../image_auth/X urls.
52 $wgArticlePath = false; # Don't let a "/*" article path clober our action path
53 $wgActionPaths = array( "$wgUploadPath/" );
55 wfImageAuthMain();
56 wfLogProfilingData();
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' );
67 return;
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' );
74 return;
76 $path = $matches['title'];
77 if ( $path && $path[0] !== '/' ) {
78 // Make sure $path has a leading /
79 $path = "/" . $path;
82 // Check for bug 28235: QUERY_STRING overriding the correct extension
83 $whitelist = array();
84 $dotPos = strrpos( $path, '.' );
85 if ( $dotPos !== false ) {
86 $whitelist[] = substr( $path, $dotPos + 1 );
88 if ( !$wgRequest->checkUrlExtension( $whitelist ) ) {
89 return;
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"
102 } else {
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 );
110 return;
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 );
116 return;
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 ) );
122 return;
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 );
129 return;
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
141 * @param $msg1
142 * @param $msg2
144 function wfForbidden( $msg1, $msg2 ) {
145 global $wgImgAuthDetails;
147 $args = func_get_args();
148 array_shift( $args );
149 array_shift( $args );
151 $msgHdr = htmlspecialchars( wfMsg( $msg1 ) );
152 $detailMsgKey = $wgImgAuthDetails ? $msg2 : 'badaccess-group0';
153 $detailMsg = htmlspecialchars( wfMsg( $detailMsgKey, $args ) );
155 wfDebugLog( 'img_auth',
156 "wfForbidden Hdr:" . wfMsgExt( $msg1, array( 'language' => 'en' ) ). " Msg: ".
157 wfMsgExt( $msg2, array( 'language' => 'en' ), $args )
160 header( 'HTTP/1.0 403 Forbidden' );
161 header( 'Cache-Control: no-cache' );
162 header( 'Content-Type: text/html; charset=utf-8' );
163 echo <<<ENDS
164 <html>
165 <body>
166 <h1>$msgHdr</h1>
167 <p>$detailMsg</p>
168 </body>
169 </html>
170 ENDS;