Update maintenance/dev/'s router, add handling for things like /wiki/File:Foo.png...
[mediawiki.git] / img_auth.php
blob64910e349c06083ec97c56c57903411695ee383f
1 <?php
3 /**
4 * Image authorisation script
6 * To use this, see http://www.mediawiki.org/wiki/Manual:Image_Authorization
8 * - Set $wgUploadDirectory to a non-public directory (not web accessible)
9 * - Set $wgUploadPath to point to this file
11 * Optional Parameters
13 * - Set $wgImgAuthDetails = true if you want the reason the access was denied messages to
14 * be displayed instead of just the 403 error (doesn't work on IE anyway),
15 * otherwise it will only appear in error logs
16 * - Set $wgImgAuthPublicTest false if you don't want to just check and see if all are public
17 * must be set to false if using specific restrictions such as LockDown or NSFileRepo
19 * For security reasons, you usually don't want your user to know *why* access was denied,
20 * just that it was. If you want to change this, you can set $wgImgAuthDetails to 'true'
21 * in localsettings.php and it will give the user the reason why access was denied.
23 * Your server needs to support PATH_INFO; CGI-based configurations usually don't.
25 * @file
27 **/
29 define( 'MW_NO_OUTPUT_COMPRESSION', 1 );
30 if ( isset( $_SERVER['MW_COMPILED'] ) ) {
31 require ( 'phase3/includes/WebStart.php' );
32 } else {
33 require ( dirname( __FILE__ ) . '/includes/WebStart.php' );
35 wfProfileIn( 'img_auth.php' );
37 # Set action base paths so that WebRequest::getPathInfo()
38 # recognizes the "X" as the 'title' in ../image_auth/X urls.
39 $wgActionPaths[] = $_SERVER['SCRIPT_NAME'];
41 wfImageAuthMain();
42 wfLogProfilingData();
44 function wfImageAuthMain() {
45 global $wgImgAuthPublicTest, $wgRequest, $wgUploadDirectory;
47 // See if this is a public Wiki (no protections).
48 if ( $wgImgAuthPublicTest
49 && in_array( 'read', User::getGroupPermissions( array( '*' ) ), true ) )
51 // This is a public wiki, so disable this script (for private wikis only)
52 wfForbidden( 'img-auth-accessdenied', 'img-auth-public' );
53 return;
56 // Get the requested file path (source file or thumbnail)
57 $matches = WebRequest::getPathInfo();
58 $path = $matches['title']; // path with leading '/'
60 // Check for bug 28235: QUERY_STRING overriding the correct extension
61 $whitelist = array();
62 $dotPos = strrpos( $path, '.' );
63 if ( $dotPos !== false ) {
64 $whitelist[] = substr( $path, $dotPos + 1 );
66 if ( !$wgRequest->checkUrlExtension( $whitelist ) ) {
67 return;
70 // Get the full file path
71 $filename = realpath( $wgUploadDirectory . $path );
72 $realUpload = realpath( $wgUploadDirectory );
74 // Basic directory traversal check
75 if ( substr( $filename, 0, strlen( $realUpload ) ) != $realUpload ) {
76 wfForbidden( 'img-auth-accessdenied', 'img-auth-notindir' );
77 return;
80 // Check to see if the file exists
81 if ( !file_exists( $filename ) ) {
82 wfForbidden( 'img-auth-accessdenied','img-auth-nofile', $filename );
83 return;
86 // Check to see if tried to access a directory
87 if ( is_dir( $filename ) ) {
88 wfForbidden( 'img-auth-accessdenied','img-auth-isdir', $filename );
89 return;
92 // Extract the file name and chop off the size specifier
93 // (e.g. 120px-Foo.png => Foo.png or page2-120px-Foo.png => Foo.png).
94 // This only applies to thumbnails, and all thumbnails have a -px specifier.
95 $name = wfBaseName( $path );
96 if ( preg_match( '!(?:[^-]*-)*?\d+px-(.*)!i', $name, $m ) ) {
97 $name = $m[1]; // this file is a thumbnail
100 $title = Title::makeTitleSafe( NS_FILE, $name );
101 if ( !$title instanceof Title ) { // files have valid titles
102 wfForbidden( 'img-auth-accessdenied', 'img-auth-badtitle', $name );
103 return;
106 // Run hook for extension authorization plugins
107 if ( !wfRunHooks( 'ImgAuthBeforeStream', array( &$title, &$path, &$name, &$result ) ) ) {
108 wfForbidden( $result[0], $result[1], array_slice( $result, 2 ) );
109 return;
112 // Check user authorization for this title
113 // UserCanRead Checks Whitelist too
114 if( !$title->userCanRead() ) {
115 wfForbidden( 'img-auth-accessdenied', 'img-auth-noread', $name );
116 return;
119 // Stream the requested file
120 wfDebugLog( 'img_auth', "Streaming `".$filename."`." );
121 StreamFile::stream( $filename, array( 'Cache-Control: private', 'Vary: Cookie' ) );
125 * Issue a standard HTTP 403 Forbidden header ($msg1-a message index, not a message) and an
126 * error message ($msg2, also a message index), (both required) then end the script
127 * subsequent arguments to $msg2 will be passed as parameters only for replacing in $msg2
128 * @param $msg1
129 * @param $msg2
131 function wfForbidden( $msg1, $msg2 ) {
132 global $wgImgAuthDetails;
134 $args = func_get_args();
135 array_shift( $args );
136 array_shift( $args );
138 $msgHdr = htmlspecialchars( wfMsg( $msg1 ) );
139 $detailMsgKey = $wgImgAuthDetails ? $msg2 : 'badaccess-group0';
140 $detailMsg = htmlspecialchars( wfMsg( $detailMsgKey, $args ) );
142 wfDebugLog( 'img_auth',
143 "wfForbidden Hdr:" . wfMsgExt( $msg1, array( 'language' => 'en' ) ). " Msg: ".
144 wfMsgExt( $msg2, array( 'language' => 'en' ), $args )
147 header( 'HTTP/1.0 403 Forbidden' );
148 header( 'Cache-Control: no-cache' );
149 header( 'Content-Type: text/html; charset=utf-8' );
150 echo <<<ENDS
151 <html>
152 <body>
153 <h1>$msgHdr</h1>
154 <p>$detailMsg</p>
155 </body>
156 </html>
157 ENDS;