* s~ +$~~
[lhc/web/wiklou.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 if ( isset( $_REQUEST['GLOBALS'] ) ) {
14 die( '<a href="http://www.hardened-php.net/index.76.html">$GLOBALS overwrite vulnerability</a>');
15 }
16
17 require_once( 'includes/Defines.php' );
18 require_once( './LocalSettings.php' );
19 require_once( 'includes/Setup.php' );
20 require_once( 'includes/StreamFile.php' );
21
22 if( !isset( $_SERVER['PATH_INFO'] ) ) {
23 wfForbidden();
24 }
25
26 # Get filenames/directories
27 $filename = realpath( $wgUploadDirectory . $_SERVER['PATH_INFO'] );
28 $realUploadDirectory = realpath( $wgUploadDirectory );
29 $imageName = $wgLang->getNsText( NS_IMAGE ) . ":" . basename( $_SERVER['PATH_INFO'] );
30
31 # Check if the filename is in the correct directory
32 if ( substr( $filename, 0, strlen( $realUploadDirectory ) ) != $realUploadDirectory ) {
33 wfForbidden();
34 }
35
36 if ( is_array( $wgWhitelistRead ) && !in_array( $imageName, $wgWhitelistRead ) && !$wgUser->getID() ) {
37 wfForbidden();
38 }
39
40 if( !file_exists( $filename ) ) {
41 wfForbidden();
42 }
43 if( is_dir( $filename ) ) {
44 wfForbidden();
45 }
46
47 # Write file
48 wfStreamFile( $filename );
49
50 function wfForbidden() {
51 header( 'HTTP/1.0 403 Forbidden' );
52 print
53 "<html><body>
54 <h1>Access denied</h1>
55 <p>You need to log in to access files on this server</p>
56 </body></html>";
57 exit;
58 }
59
60 ?>