]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - img_auth.php
MediaWiki 1.5.8 (initial commit)
[autoinstalls/mediawiki.git] / img_auth.php
1 <?php
2 /**
3  * Image download authorisation script
4  * 
5  * To use, in LocalSettings.php set $wgUploadDirectory to point to a non-public
6  * directory, and $wgUploadPath to point to this file. Also set $wgWhitelistRead
7  * to an array of pages you want everyone to be able to access. Your server must
8  * support PATH_INFO, CGI-based configurations generally don't. 
9  */
10 # Valid web server entry point, enable includes
11 define( 'MEDIAWIKI', true );
12
13 require_once( 'includes/Defines.php' );
14 require_once( './LocalSettings.php' );
15 require_once( 'includes/Setup.php' );
16 require_once( 'includes/StreamFile.php' );
17
18 if( !isset( $_SERVER['PATH_INFO'] ) ) {
19         wfForbidden();
20 }
21
22 # Get filenames/directories
23 $filename = realpath( $wgUploadDirectory . $_SERVER['PATH_INFO'] );
24 $realUploadDirectory = realpath( $wgUploadDirectory );
25 $imageName = $wgLang->getNsText( NS_IMAGE ) . ":" . basename( $_SERVER['PATH_INFO'] );
26
27 # Check if the filename is in the correct directory
28 if ( substr( $filename, 0, strlen( $realUploadDirectory ) ) != $realUploadDirectory ) {
29         wfForbidden();
30 }
31
32 if ( is_array( $wgWhitelistRead ) && !in_array( $imageName, $wgWhitelistRead ) && !$wgUser->getID() ) {
33         wfForbidden();
34 }
35
36 if( !file_exists( $filename ) ) {
37         wfForbidden();
38 }
39 if( is_dir( $filename ) ) {
40         wfForbidden();
41 }
42
43 # Write file
44 wfStreamFile( $filename );
45
46 function wfForbidden() {
47         header( 'HTTP/1.0 403 Forbidden' );
48         print 
49 "<html><body>
50 <h1>Access denied</h1>
51 <p>You need to log in to access files on this server</p>
52 </body></html>";
53         exit;
54 }
55
56 ?>