cfe005e9369794ed6c4c7bbe20cee99530e8142c
[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 require_once( './includes/WebStart.php' );
11 wfProfileIn( 'img_auth.php' );
12 require_once( './includes/StreamFile.php' );
13
14 if( !isset( $_SERVER['PATH_INFO'] ) ) {
15 wfForbidden();
16 }
17
18 # Get filenames/directories
19 $filename = realpath( $wgUploadDirectory . $_SERVER['PATH_INFO'] );
20 $realUploadDirectory = realpath( $wgUploadDirectory );
21 $imageName = $wgContLang->getNsText( NS_IMAGE ) . ":" . wfBaseName( $_SERVER['PATH_INFO'] );
22
23 # Check if the filename is in the correct directory
24 if ( substr( $filename, 0, strlen( $realUploadDirectory ) ) != $realUploadDirectory ) {
25 wfForbidden();
26 }
27
28 if ( is_array( $wgWhitelistRead ) && !in_array( $imageName, $wgWhitelistRead ) && !$wgUser->getID() ) {
29 wfForbidden();
30 }
31
32 if( !file_exists( $filename ) ) {
33 wfForbidden();
34 }
35 if( is_dir( $filename ) ) {
36 wfForbidden();
37 }
38
39 # Write file
40 wfStreamFile( $filename );
41 wfLogProfilingData();
42
43 function wfForbidden() {
44 header( 'HTTP/1.0 403 Forbidden' );
45 print
46 "<html><body>
47 <h1>Access denied</h1>
48 <p>You need to log in to access files on this server</p>
49 </body></html>";
50 wfLogProfilingData();
51 exit;
52 }
53
54 ?>