* (bug 29531) r89628 breaks img_auth.php
[lhc/web/wiklou.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 if ( isset( $_SERVER['MW_COMPILED'] ) ) {
30 require ( 'phase3/includes/WebStart.php' );
31 } else {
32 require ( dirname( __FILE__ ) . '/includes/WebStart.php' );
33 }
34 wfProfileIn( 'img_auth.php' );
35 require_once( dirname( __FILE__ ) . '/includes/StreamFile.php' );
36
37 $wgActionPaths[] = $_SERVER['SCRIPT_NAME'];
38 // See if this is a public Wiki (no protections)
39 if ( $wgImgAuthPublicTest
40 && in_array( 'read', User::getGroupPermissions( array( '*' ) ), true ) )
41 {
42 wfForbidden('img-auth-accessdenied','img-auth-public');
43 }
44
45 $matches = WebRequest::getPathInfo();
46 $path = $matches['title'];
47
48 // Check for bug 28235: QUERY_STRING overriding the correct extension
49 $dotPos = strrpos( $path, '.' );
50 $whitelist = array();
51 if ( $dotPos !== false ) {
52 $whitelist[] = substr( $path, $dotPos + 1 );
53 }
54 if ( !$wgRequest->checkUrlExtension( $whitelist ) )
55 {
56 return;
57 }
58
59 $filename = realpath( $wgUploadDirectory . $path );
60 $realUpload = realpath( $wgUploadDirectory );
61
62 // Basic directory traversal check
63 if( substr( $filename, 0, strlen( $realUpload ) ) != $realUpload )
64 wfForbidden('img-auth-accessdenied','img-auth-notindir');
65
66 // Extract the file name and chop off the size specifier
67 // (e.g. 120px-Foo.png => Foo.png)
68 $name = wfBaseName( $path );
69 if( preg_match( '!\d+px-(.*)!i', $name, $m ) )
70 $name = $m[1];
71
72 // Check to see if the file exists
73 if( !file_exists( $filename ) )
74 wfForbidden('img-auth-accessdenied','img-auth-nofile',$filename);
75
76 // Check to see if tried to access a directory
77 if( is_dir( $filename ) )
78 wfForbidden('img-auth-accessdenied','img-auth-isdir',$filename);
79
80
81 $title = Title::makeTitleSafe( NS_FILE, $name );
82
83 // See if could create the title object
84 if( !$title instanceof Title )
85 wfForbidden('img-auth-accessdenied','img-auth-badtitle',$name);
86
87 // Run hook
88 if (!wfRunHooks( 'ImgAuthBeforeStream', array( &$title, &$path, &$name, &$result ) ) )
89 wfForbidden($result[0],$result[1],array_slice($result,2));
90
91 // Check user authorization for this title
92 // UserCanRead Checks Whitelist too
93 if( !$title->userCanRead() )
94 wfForbidden('img-auth-accessdenied','img-auth-noread',$name);
95
96 // Stream the requested file
97 wfDebugLog( 'img_auth', "Streaming `".$filename."`." );
98 wfStreamFile( $filename, array( 'Cache-Control: private', 'Vary: Cookie' ) );
99 wfLogProfilingData();
100
101 /**
102 * Issue a standard HTTP 403 Forbidden header ($msg1-a message index, not a message) and an
103 * error message ($msg2, also a message index), (both required) then end the script
104 * subsequent arguments to $msg2 will be passed as parameters only for replacing in $msg2
105 */
106 function wfForbidden($msg1,$msg2) {
107 global $wgImgAuthDetails;
108 $args = func_get_args();
109 array_shift( $args );
110 array_shift( $args );
111 $MsgHdr = htmlspecialchars(wfMsg($msg1));
112 $detailMsg = (htmlspecialchars(wfMsg(($wgImgAuthDetails ? $msg2 : 'badaccess-group0'),$args)));
113 wfDebugLog('img_auth', "wfForbidden Hdr:".wfMsgExt( $msg1, array('language' => 'en'))." Msg: ".
114 wfMsgExt($msg2,array('language' => 'en'),$args));
115 header( 'HTTP/1.0 403 Forbidden' );
116 header( 'Cache-Control: no-cache' );
117 header( 'Content-Type: text/html; charset=utf-8' );
118 echo <<<ENDS
119 <html>
120 <body>
121 <h1>$MsgHdr</h1>
122 <p>$detailMsg</p>
123 </body>
124 </html>
125 ENDS;
126 wfLogProfilingData();
127 exit();
128 }