Merge "Drop -{webkit,moz}-box-shadow"
[lhc/web/wiklou.git] / img_auth.php
1 <?php
2 /**
3 * Image authorisation script
4 *
5 * To use this, see https://www.mediawiki.org/wiki/Manual:Image_Authorization
6 *
7 * - Set $wgUploadDirectory to a non-public directory (not web accessible)
8 * - Set $wgUploadPath to point to this file
9 *
10 * Optional Parameters
11 *
12 * - Set $wgImgAuthDetails = true if you want the reason the access was denied messages to
13 * be displayed instead of just the 403 error (doesn't work on IE anyway),
14 * otherwise it 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,
19 * just that it was. If you want to change this, you can set $wgImgAuthDetails to 'true'
20 * in localsettings.php and it will give the user the reason why access was denied.
21 *
22 * Your server needs to support PATH_INFO; CGI-based configurations usually don't.
23 *
24 * This program is free software; you can redistribute it and/or modify
25 * it under the terms of the GNU General Public License as published by
26 * the Free Software Foundation; either version 2 of the License, or
27 * (at your option) any later version.
28 *
29 * This program is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 * GNU General Public License for more details.
33 *
34 * You should have received a copy of the GNU General Public License along
35 * with this program; if not, write to the Free Software Foundation, Inc.,
36 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
37 * http://www.gnu.org/copyleft/gpl.html
38 *
39 * @file
40 */
41
42 define( 'MW_NO_OUTPUT_COMPRESSION', 1 );
43 require __DIR__ . '/includes/WebStart.php';
44 wfProfileIn( 'img_auth.php' );
45
46 # Set action base paths so that WebRequest::getPathInfo()
47 # recognizes the "X" as the 'title' in ../img_auth.php/X urls.
48 $wgArticlePath = false; # Don't let a "/*" article path clober our action path
49 $wgActionPaths = array( "$wgUploadPath/" );
50
51 wfImageAuthMain();
52 wfLogProfilingData();
53 // Commit and close up!
54 $factory = wfGetLBFactory();
55 $factory->commitMasterChanges();
56 $factory->shutdown();
57
58 function wfImageAuthMain() {
59 global $wgImgAuthPublicTest, $wgImgAuthUrlPathMap;
60
61 $request = RequestContext::getMain()->getRequest();
62 $publicWiki = in_array( 'read', User::getGroupPermissions( array( '*' ) ), true );
63
64 // See if this is a public Wiki (no protections).
65 if ( $wgImgAuthPublicTest && $publicWiki ) {
66 // This is a public wiki, so disable this script (for private wikis only)
67 wfForbidden( 'img-auth-accessdenied', 'img-auth-public' );
68 return;
69 }
70
71 // Get the requested file path (source file or thumbnail)
72 $matches = WebRequest::getPathInfo();
73 if ( !isset( $matches['title'] ) ) {
74 wfForbidden( 'img-auth-accessdenied', 'img-auth-nopathinfo' );
75 return;
76 }
77 $path = $matches['title'];
78 if ( $path && $path[0] !== '/' ) {
79 // Make sure $path has a leading /
80 $path = "/" . $path;
81 }
82
83 // Check for bug 28235: QUERY_STRING overriding the correct extension
84 $whitelist = array();
85 $extension = FileBackend::extensionFromPath( $path );
86 if ( $extension != '' ) {
87 $whitelist[] = $extension;
88 }
89 if ( !$request->checkUrlExtension( $whitelist ) ) {
90 return;
91 }
92
93 // Various extensions may have their own backends that need access.
94 // Check if there is a special backend and storage base path for this file.
95 foreach ( $wgImgAuthUrlPathMap as $prefix => $storageDir ) {
96 $prefix = rtrim( $prefix, '/' ) . '/'; // implicit trailing slash
97 if ( strpos( $path, $prefix ) === 0 ) {
98 $be = FileBackendGroup::singleton()->backendFromPath( $storageDir );
99 $filename = $storageDir . substr( $path, strlen( $prefix ) ); // strip prefix
100 // Check basic user authorization
101 if ( !RequestContext::getMain()->getUser()->isAllowed( 'read' ) ) {
102 wfForbidden( 'img-auth-accessdenied', 'img-auth-noread', $path );
103 return;
104 }
105 if ( $be->fileExists( array( 'src' => $filename ) ) ) {
106 wfDebugLog( 'img_auth', "Streaming `" . $filename . "`." );
107 $be->streamFile( array( 'src' => $filename ),
108 array( 'Cache-Control: private', 'Vary: Cookie' ) );
109 } else {
110 wfForbidden( 'img-auth-accessdenied', 'img-auth-nofile', $path );
111 }
112 return;
113 }
114 }
115
116 // Get the local file repository
117 $repo = RepoGroup::singleton()->getRepo( 'local' );
118
119 // Get the full file storage path and extract the source file name.
120 // (e.g. 120px-Foo.png => Foo.png or page2-120px-Foo.png => Foo.png).
121 // This only applies to thumbnails, and all thumbnails should
122 // be under a folder that has the source file name.
123 if ( strpos( $path, '/thumb/' ) === 0 ) {
124 $name = wfBaseName( dirname( $path ) ); // file is a thumbnail
125 $filename = $repo->getZonePath( 'thumb' ) . substr( $path, 6 ); // strip "/thumb"
126 // Check to see if the file exists
127 if ( !$repo->fileExists( $filename ) ) {
128 wfForbidden( 'img-auth-accessdenied', 'img-auth-nofile', $filename );
129 return;
130 }
131 } else {
132 $name = wfBaseName( $path ); // file is a source file
133 $filename = $repo->getZonePath( 'public' ) . $path;
134 // Check to see if the file exists and is not deleted
135 $bits = explode( '!', $name, 2 );
136 if ( substr( $path, 0, 9 ) === '/archive/' && count( $bits ) == 2 ) {
137 $file = $repo->newFromArchiveName( $bits[1], $name );
138 } else {
139 $file = $repo->newFile( $name );
140 }
141 if ( !$file->exists() || $file->isDeleted( File::DELETED_FILE ) ) {
142 wfForbidden( 'img-auth-accessdenied', 'img-auth-nofile', $filename );
143 return;
144 }
145 }
146
147 $headers = array(); // extra HTTP headers to send
148
149 if ( !$publicWiki ) {
150 // For private wikis, run extra auth checks and set cache control headers
151 $headers[] = 'Cache-Control: private';
152 $headers[] = 'Vary: Cookie';
153
154 $title = Title::makeTitleSafe( NS_FILE, $name );
155 if ( !$title instanceof Title ) { // files have valid titles
156 wfForbidden( 'img-auth-accessdenied', 'img-auth-badtitle', $name );
157 return;
158 }
159
160 // Run hook for extension authorization plugins
161 /** @var $result array */
162 $result = null;
163 if ( !wfRunHooks( 'ImgAuthBeforeStream', array( &$title, &$path, &$name, &$result ) ) ) {
164 wfForbidden( $result[0], $result[1], array_slice( $result, 2 ) );
165 return;
166 }
167
168 // Check user authorization for this title
169 // Checks Whitelist too
170 if ( !$title->userCan( 'read' ) ) {
171 wfForbidden( 'img-auth-accessdenied', 'img-auth-noread', $name );
172 return;
173 }
174 }
175
176 if ( $request->getCheck( 'download' ) ) {
177 $headers[] = 'Content-Disposition: attachment';
178 }
179
180 // Stream the requested file
181 wfDebugLog( 'img_auth', "Streaming `" . $filename . "`." );
182 $repo->streamFile( $filename, $headers );
183 }
184
185 /**
186 * Issue a standard HTTP 403 Forbidden header ($msg1-a message index, not a message) and an
187 * error message ($msg2, also a message index), (both required) then end the script
188 * subsequent arguments to $msg2 will be passed as parameters only for replacing in $msg2
189 * @param string $msg1
190 * @param string $msg2
191 */
192 function wfForbidden( $msg1, $msg2 ) {
193 global $wgImgAuthDetails;
194
195 $args = func_get_args();
196 array_shift( $args );
197 array_shift( $args );
198 $args = ( isset( $args[0] ) && is_array( $args[0] ) ) ? $args[0] : $args;
199
200 $msgHdr = wfMessage( $msg1 )->escaped();
201 $detailMsgKey = $wgImgAuthDetails ? $msg2 : 'badaccess-group0';
202 $detailMsg = wfMessage( $detailMsgKey, $args )->escaped();
203
204 wfDebugLog( 'img_auth',
205 "wfForbidden Hdr: " . wfMessage( $msg1 )->inLanguage( 'en' )->text() . " Msg: " .
206 wfMessage( $msg2, $args )->inLanguage( 'en' )->text()
207 );
208
209 header( 'HTTP/1.0 403 Forbidden' );
210 header( 'Cache-Control: no-cache' );
211 header( 'Content-Type: text/html; charset=utf-8' );
212 echo <<<ENDS
213 <html>
214 <body>
215 <h1>$msgHdr</h1>
216 <p>$detailMsg</p>
217 </body>
218 </html>
219 ENDS;
220 }