Stylize
[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 should
100 // be under a folder that has the source file name.
101 $name = wfBaseName( $path );
102 if ( strpos( $path, '/thumb/' ) === 0 ) {
103 $name = wfBaseName( dirname( $path ) ); // this file is a thumbnail
104 }
105
106 $title = Title::makeTitleSafe( NS_FILE, $name );
107 if ( !$title instanceof Title ) { // files have valid titles
108 wfForbidden( 'img-auth-accessdenied', 'img-auth-badtitle', $name );
109 return;
110 }
111
112 // Run hook for extension authorization plugins
113 if ( !wfRunHooks( 'ImgAuthBeforeStream', array( &$title, &$path, &$name, &$result ) ) ) {
114 wfForbidden( $result[0], $result[1], array_slice( $result, 2 ) );
115 return;
116 }
117
118 // Check user authorization for this title
119 // Checks Whitelist too
120 if ( !$title->userCan( 'read' ) ) {
121 wfForbidden( 'img-auth-accessdenied', 'img-auth-noread', $name );
122 return;
123 }
124
125 // Stream the requested file
126 wfDebugLog( 'img_auth', "Streaming `".$filename."`." );
127 StreamFile::stream( $filename, array( 'Cache-Control: private', 'Vary: Cookie' ) );
128 }
129
130 /**
131 * Issue a standard HTTP 403 Forbidden header ($msg1-a message index, not a message) and an
132 * error message ($msg2, also a message index), (both required) then end the script
133 * subsequent arguments to $msg2 will be passed as parameters only for replacing in $msg2
134 * @param $msg1
135 * @param $msg2
136 */
137 function wfForbidden( $msg1, $msg2 ) {
138 global $wgImgAuthDetails;
139
140 $args = func_get_args();
141 array_shift( $args );
142 array_shift( $args );
143
144 $msgHdr = htmlspecialchars( wfMsg( $msg1 ) );
145 $detailMsgKey = $wgImgAuthDetails ? $msg2 : 'badaccess-group0';
146 $detailMsg = htmlspecialchars( wfMsg( $detailMsgKey, $args ) );
147
148 wfDebugLog( 'img_auth',
149 "wfForbidden Hdr:" . wfMsgExt( $msg1, array( 'language' => 'en' ) ). " Msg: ".
150 wfMsgExt( $msg2, array( 'language' => 'en' ), $args )
151 );
152
153 header( 'HTTP/1.0 403 Forbidden' );
154 header( 'Cache-Control: no-cache' );
155 header( 'Content-Type: text/html; charset=utf-8' );
156 echo <<<ENDS
157 <html>
158 <body>
159 <h1>$msgHdr</h1>
160 <p>$detailMsg</p>
161 </body>
162 </html>
163 ENDS;
164 }