Merge "Pass the 'returntoquery' parameter on cookie check."
[lhc/web/wiklou.git] / img_auth.php
1 <?php
2 /**
3 * Image authorisation script
4 *
5 * To use this, see http://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 if ( isset( $_SERVER['MW_COMPILED'] ) ) {
44 require ( 'core/includes/WebStart.php' );
45 } else {
46 require ( dirname( __FILE__ ) . '/includes/WebStart.php' );
47 }
48 wfProfileIn( 'img_auth.php' );
49
50 # Set action base paths so that WebRequest::getPathInfo()
51 # recognizes the "X" as the 'title' in ../image_auth/X urls.
52 $wgArticlePath = false; # Don't let a "/*" article path clober our action path
53 $wgActionPaths = array( "$wgUploadPath/" );
54
55 wfImageAuthMain();
56 wfLogProfilingData();
57
58 function wfImageAuthMain() {
59 global $wgImgAuthPublicTest, $wgRequest;
60
61 // See if this is a public Wiki (no protections).
62 if ( $wgImgAuthPublicTest
63 && in_array( 'read', User::getGroupPermissions( array( '*' ) ), true ) )
64 {
65 // This is a public wiki, so disable this script (for private wikis only)
66 wfForbidden( 'img-auth-accessdenied', 'img-auth-public' );
67 return;
68 }
69
70 // Get the requested file path (source file or thumbnail)
71 $matches = WebRequest::getPathInfo();
72 if ( !isset( $matches['title'] ) ) {
73 wfForbidden( 'img-auth-accessdenied', 'img-auth-nopathinfo' );
74 return;
75 }
76 $path = $matches['title'];
77 if ( $path && $path[0] !== '/' ) {
78 // Make sure $path has a leading /
79 $path = "/" . $path;
80 }
81
82 // Check for bug 28235: QUERY_STRING overriding the correct extension
83 $whitelist = array();
84 $dotPos = strrpos( $path, '.' );
85 if ( $dotPos !== false ) {
86 $whitelist[] = substr( $path, $dotPos + 1 );
87 }
88 if ( !$wgRequest->checkUrlExtension( $whitelist ) ) {
89 return;
90 }
91
92 // Get the local file repository
93 $repo = RepoGroup::singleton()->getRepo( 'local' );
94
95 // Get the full file storage path and extract the source file name.
96 // (e.g. 120px-Foo.png => Foo.png or page2-120px-Foo.png => Foo.png).
97 // This only applies to thumbnails, and all thumbnails should
98 // be under a folder that has the source file name.
99 if ( strpos( $path, '/thumb/' ) === 0 ) {
100 $name = wfBaseName( dirname( $path ) ); // file is a thumbnail
101 $filename = $repo->getZonePath( 'thumb' ) . substr( $path, 6 ); // strip "/thumb"
102 } else {
103 $name = wfBaseName( $path ); // file is a source file
104 $filename = $repo->getZonePath( 'public' ) . $path;
105 }
106
107 // Check to see if the file exists
108 if ( !$repo->fileExists( $filename ) ) {
109 wfForbidden( 'img-auth-accessdenied','img-auth-nofile', $filename );
110 return;
111 }
112
113 $title = Title::makeTitleSafe( NS_FILE, $name );
114 if ( !$title instanceof Title ) { // files have valid titles
115 wfForbidden( 'img-auth-accessdenied', 'img-auth-badtitle', $name );
116 return;
117 }
118
119 // Run hook for extension authorization plugins
120 if ( !wfRunHooks( 'ImgAuthBeforeStream', array( &$title, &$path, &$name, &$result ) ) ) {
121 wfForbidden( $result[0], $result[1], array_slice( $result, 2 ) );
122 return;
123 }
124
125 // Check user authorization for this title
126 // Checks Whitelist too
127 if ( !$title->userCan( 'read' ) ) {
128 wfForbidden( 'img-auth-accessdenied', 'img-auth-noread', $name );
129 return;
130 }
131
132 // Stream the requested file
133 wfDebugLog( 'img_auth', "Streaming `".$filename."`." );
134 $repo->streamFile( $filename, array( 'Cache-Control: private', 'Vary: Cookie' ) );
135 }
136
137 /**
138 * Issue a standard HTTP 403 Forbidden header ($msg1-a message index, not a message) and an
139 * error message ($msg2, also a message index), (both required) then end the script
140 * subsequent arguments to $msg2 will be passed as parameters only for replacing in $msg2
141 * @param $msg1
142 * @param $msg2
143 */
144 function wfForbidden( $msg1, $msg2 ) {
145 global $wgImgAuthDetails;
146
147 $args = func_get_args();
148 array_shift( $args );
149 array_shift( $args );
150
151 $msgHdr = htmlspecialchars( wfMsg( $msg1 ) );
152 $detailMsgKey = $wgImgAuthDetails ? $msg2 : 'badaccess-group0';
153 $detailMsg = htmlspecialchars( wfMsg( $detailMsgKey, $args ) );
154
155 wfDebugLog( 'img_auth',
156 "wfForbidden Hdr:" . wfMsgExt( $msg1, array( 'language' => 'en' ) ). " Msg: ".
157 wfMsgExt( $msg2, array( 'language' => 'en' ), $args )
158 );
159
160 header( 'HTTP/1.0 403 Forbidden' );
161 header( 'Cache-Control: no-cache' );
162 header( 'Content-Type: text/html; charset=utf-8' );
163 echo <<<ENDS
164 <html>
165 <body>
166 <h1>$msgHdr</h1>
167 <p>$detailMsg</p>
168 </body>
169 </html>
170 ENDS;
171 }