Update img_auth.php and WebRequest code to handle non index.php scripts like img_auth...
[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
14 * be displayed instead of just the 403 error (doesn't work on IE anyway),
15 * otherwise it will only appear in error logs
16 * - Set $wgImgAuthPublicTest false if you don't want to just check and see if all are public
17 * must be set to false if using specific restrictions such as LockDown or NSFileRepo
18 *
19 * For security reasons, you usually don't want your user to know *why* access was denied,
20 * just that it was. If you want to change this, you can set $wgImgAuthDetails to 'true'
21 * in localsettings.php and it will give the user the reason why access was denied.
22 *
23 * Your server needs to support PATH_INFO; CGI-based configurations usually don't.
24 *
25 * @file
26 *
27 **/
28
29 define( 'MW_NO_OUTPUT_COMPRESSION', 1 );
30 if ( isset( $_SERVER['MW_COMPILED'] ) ) {
31 require ( 'phase3/includes/WebStart.php' );
32 } else {
33 require ( dirname( __FILE__ ) . '/includes/WebStart.php' );
34 }
35 wfProfileIn( 'img_auth.php' );
36
37 # Set action base paths so that WebRequest::getPathInfo()
38 # recognizes the "X" as the 'title' in ../image_auth/X urls.
39 $wgArticlePath = false; # Don't let a "/*" article path clober our action path
40 $wgActionPaths = array( "$wgUploadPath/" );
41
42 wfImageAuthMain();
43 wfLogProfilingData();
44
45 function wfImageAuthMain() {
46 global $wgImgAuthPublicTest, $wgRequest, $wgUploadDirectory;
47
48 // See if this is a public Wiki (no protections).
49 if ( $wgImgAuthPublicTest
50 && in_array( 'read', User::getGroupPermissions( array( '*' ) ), true ) )
51 {
52 // This is a public wiki, so disable this script (for private wikis only)
53 wfForbidden( 'img-auth-accessdenied', 'img-auth-public' );
54 return;
55 }
56
57 // Get the requested file path (source file or thumbnail)
58 $matches = WebRequest::getPathInfo();
59 $path = $matches['title'];
60 if ( $path && $path[0] !== '/' ) {
61 // Make sure $path has a leading /
62 $path = "/" . $path;
63 }
64
65 // Check for bug 28235: QUERY_STRING overriding the correct extension
66 $whitelist = array();
67 $dotPos = strrpos( $path, '.' );
68 if ( $dotPos !== false ) {
69 $whitelist[] = substr( $path, $dotPos + 1 );
70 }
71 if ( !$wgRequest->checkUrlExtension( $whitelist ) ) {
72 return;
73 }
74
75 // Get the full file path
76 $filename = realpath( $wgUploadDirectory . $path );
77 $realUpload = realpath( $wgUploadDirectory );
78
79 // Basic directory traversal check
80 if ( substr( $filename, 0, strlen( $realUpload ) ) != $realUpload ) {
81 wfForbidden( 'img-auth-accessdenied', 'img-auth-notindir' );
82 return;
83 }
84
85 // Check to see if the file exists
86 if ( !file_exists( $filename ) ) {
87 wfForbidden( 'img-auth-accessdenied','img-auth-nofile', $filename );
88 return;
89 }
90
91 // Check to see if tried to access a directory
92 if ( is_dir( $filename ) ) {
93 wfForbidden( 'img-auth-accessdenied','img-auth-isdir', $filename );
94 return;
95 }
96
97 // Extract the file name and chop off the size specifier
98 // (e.g. 120px-Foo.png => Foo.png or page2-120px-Foo.png => Foo.png).
99 // This only applies to thumbnails, and all thumbnails have a -px specifier.
100 $name = wfBaseName( $path );
101 if ( preg_match( '!(?:[^-]*-)*?\d+px-(.*)!i', $name, $m ) ) {
102 $name = $m[1]; // this file is a thumbnail
103 }
104
105 $title = Title::makeTitleSafe( NS_FILE, $name );
106 if ( !$title instanceof Title ) { // files have valid titles
107 wfForbidden( 'img-auth-accessdenied', 'img-auth-badtitle', $name );
108 return;
109 }
110
111 // Run hook for extension authorization plugins
112 if ( !wfRunHooks( 'ImgAuthBeforeStream', array( &$title, &$path, &$name, &$result ) ) ) {
113 wfForbidden( $result[0], $result[1], array_slice( $result, 2 ) );
114 return;
115 }
116
117 // Check user authorization for this title
118 // UserCanRead Checks Whitelist too
119 if( !$title->userCanRead() ) {
120 wfForbidden( 'img-auth-accessdenied', 'img-auth-noread', $name );
121 return;
122 }
123
124 // Stream the requested file
125 wfDebugLog( 'img_auth', "Streaming `".$filename."`." );
126 StreamFile::stream( $filename, array( 'Cache-Control: private', 'Vary: Cookie' ) );
127 }
128
129 /**
130 * Issue a standard HTTP 403 Forbidden header ($msg1-a message index, not a message) and an
131 * error message ($msg2, also a message index), (both required) then end the script
132 * subsequent arguments to $msg2 will be passed as parameters only for replacing in $msg2
133 * @param $msg1
134 * @param $msg2
135 */
136 function wfForbidden( $msg1, $msg2 ) {
137 global $wgImgAuthDetails;
138
139 $args = func_get_args();
140 array_shift( $args );
141 array_shift( $args );
142
143 $msgHdr = htmlspecialchars( wfMsg( $msg1 ) );
144 $detailMsgKey = $wgImgAuthDetails ? $msg2 : 'badaccess-group0';
145 $detailMsg = htmlspecialchars( wfMsg( $detailMsgKey, $args ) );
146
147 wfDebugLog( 'img_auth',
148 "wfForbidden Hdr:" . wfMsgExt( $msg1, array( 'language' => 'en' ) ). " Msg: ".
149 wfMsgExt( $msg2, array( 'language' => 'en' ), $args )
150 );
151
152 header( 'HTTP/1.0 403 Forbidden' );
153 header( 'Cache-Control: no-cache' );
154 header( 'Content-Type: text/html; charset=utf-8' );
155 echo <<<ENDS
156 <html>
157 <body>
158 <h1>$msgHdr</h1>
159 <p>$detailMsg</p>
160 </body>
161 </html>
162 ENDS;
163 }