]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - img_auth.php
MediaWiki 1.17.2
[autoinstallsdev/mediawiki.git] / img_auth.php
1 <?php
2
3 /**
4  * Image authorisation script
5  *
6  * To use this, see http://www.mediawiki.org/wiki/Manual:Image_Authorization
7  *
8  * - Set $wgUploadDirectory to a non-public directory (not web accessible)
9  * - Set $wgUploadPath to point to this file
10  *
11  * Optional Parameters
12  *
13  * - Set $wgImgAuthDetails = true if you want the reason the access was denied messages to be displayed 
14  *       instead of just the 403 error (doesn't work on IE anyway),  otherwise 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
17  *
18  *  For security reasons, you usually don't want your user to know *why* access was denied, just that it was.
19  *  If you want to change this, you can set $wgImgAuthDetails to 'true' in localsettings.php and it will give the user the reason
20  *  why access was denied.
21  *
22  * Your server needs to support PATH_INFO; CGI-based configurations usually don't.
23  *
24  * @file
25  *
26  **/
27
28 define( 'MW_NO_OUTPUT_COMPRESSION', 1 );
29 require_once( dirname( __FILE__ ) . '/includes/WebStart.php' );
30 wfProfileIn( 'img_auth.php' );
31 require_once( dirname( __FILE__ ) . '/includes/StreamFile.php' );
32
33 // See if this is a public Wiki (no protections)
34 if ( $wgImgAuthPublicTest 
35         && in_array( 'read', User::getGroupPermissions( array( '*' ) ), true ) )
36 {
37         wfForbidden('img-auth-accessdenied','img-auth-public');
38 }
39
40 // Extract path and image information
41 if( !isset( $_SERVER['PATH_INFO'] ) ) {
42         $path = $wgRequest->getText( 'path' );
43         if( !$path ) {
44         wfForbidden( 'img-auth-accessdenied', 'img-auth-nopathinfo' );
45         }
46         $path = "/$path";
47 } else {
48         $path = $_SERVER['PATH_INFO'];
49 }
50
51 // Check for bug 28235: QUERY_STRING overriding the correct extension
52 $dotPos = strrpos( $path, '.' );
53 $whitelist = array();
54 if ( $dotPos !== false ) {
55         $whitelist[] = substr( $path, $dotPos + 1 );
56 }
57 if ( !$wgRequest->checkUrlExtension( $whitelist ) )
58 {
59         return;
60 }
61
62 $filename = realpath( $wgUploadDirectory . $path );
63 $realUpload = realpath( $wgUploadDirectory );
64
65 // Basic directory traversal check
66 if( substr( $filename, 0, strlen( $realUpload ) ) != $realUpload )
67         wfForbidden('img-auth-accessdenied','img-auth-notindir');
68
69 // Extract the file name and chop off the size specifier
70 // (e.g. 120px-Foo.png => Foo.png)
71 $name = wfBaseName( $path );
72 if( preg_match( '!\d+px-(.*)!i', $name, $m ) )
73         $name = $m[1];
74
75 // Check to see if the file exists
76 if( !file_exists( $filename ) )
77         wfForbidden('img-auth-accessdenied','img-auth-nofile',$filename);
78
79 // Check to see if tried to access a directory
80 if( is_dir( $filename ) )
81         wfForbidden('img-auth-accessdenied','img-auth-isdir',$filename);
82
83
84 $title = Title::makeTitleSafe( NS_FILE, $name );
85
86 // See if could create the title object
87 if( !$title instanceof Title ) 
88         wfForbidden('img-auth-accessdenied','img-auth-badtitle',$name);
89
90 // Run hook
91 if (!wfRunHooks( 'ImgAuthBeforeStream', array( &$title, &$path, &$name, &$result ) ) )
92         wfForbidden($result[0],$result[1],array_slice($result,2));
93         
94 //  Check user authorization for this title
95 //  UserCanRead Checks Whitelist too
96 if( !$title->userCanRead() ) 
97         wfForbidden('img-auth-accessdenied','img-auth-noread',$name);
98
99 // Stream the requested file
100 wfDebugLog( 'img_auth', "Streaming `".$filename."`." );
101 wfStreamFile( $filename, array( 'Cache-Control: private', 'Vary: Cookie' ) );
102 wfLogProfilingData();
103
104 /**
105  * Issue a standard HTTP 403 Forbidden header ($msg1-a message index, not a message) and an
106  * error message ($msg2, also a message index), (both required) then end the script
107  * subsequent arguments to $msg2 will be passed as parameters only for replacing in $msg2 
108  */
109 function wfForbidden($msg1,$msg2) {
110         global $wgImgAuthDetails;
111         $args = func_get_args();
112         array_shift( $args );
113         array_shift( $args );
114         $MsgHdr = htmlspecialchars(wfMsg($msg1));
115         $detailMsg = (htmlspecialchars(wfMsg(($wgImgAuthDetails ? $msg2 : 'badaccess-group0'),$args)));
116         wfDebugLog('img_auth', "wfForbidden Hdr:".wfMsgExt( $msg1, array('language' => 'en'))." Msg: ".
117                                 wfMsgExt($msg2,array('language' => 'en'),$args));
118         header( 'HTTP/1.0 403 Forbidden' );
119         header( 'Cache-Control: no-cache' );
120         header( 'Content-Type: text/html; charset=utf-8' );
121         echo <<<ENDS
122 <html>
123 <body>
124 <h1>$MsgHdr</h1>
125 <p>$detailMsg</p>
126 </body>
127 </html>
128 ENDS;
129         wfLogProfilingData();
130         exit();
131 }