add some debugging output to img_auth.php
[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 wfDebugLog( 'img_auth', "missing PATH_INFO" );
16 wfForbidden();
17 }
18
19 # Get filenames/directories
20 wfDebugLog( 'img_auth', "PATH_INFO is: " . $_SERVER['PATH_INFO'] );
21 $filename = realpath( $wgUploadDirectory . $_SERVER['PATH_INFO'] );
22 $realUploadDirectory = realpath( $wgUploadDirectory );
23 $imageName = $wgContLang->getNsText( NS_IMAGE ) . ":" . wfBaseName( $_SERVER['PATH_INFO'] );
24
25 # Check if the filename is in the correct directory
26 if ( substr( $filename, 0, strlen( $realUploadDirectory ) ) != $realUploadDirectory ) {
27 wfDebugLog( 'img_auth', "requested path not in upload dir: $filename" );
28 wfForbidden();
29 }
30
31 if ( is_array( $wgWhitelistRead ) && !in_array( $imageName, $wgWhitelistRead ) && !$wgUser->getID() ) {
32 wfDebugLog( 'img_auth', "not logged in and requested file not in whitelist: $imageName" );
33 wfForbidden();
34 }
35
36 if( !file_exists( $filename ) ) {
37 wfDebugLog( 'img_auth', "requested file does not exist: $filename" );
38 wfForbidden();
39 }
40 if( is_dir( $filename ) ) {
41 wfDebugLog( 'img_auth', "requested file is a directory: $filename" );
42 wfForbidden();
43 }
44
45 # Write file
46 wfDebugLog( 'img_auth', "streaming file: $filename" );
47 wfStreamFile( $filename );
48 wfLogProfilingData();
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 wfLogProfilingData();
58 exit;
59 }
60
61 ?>