MediaWiki REL1_31
img_auth.php
Go to the documentation of this file.
1<?php
40define( 'MW_NO_OUTPUT_COMPRESSION', 1 );
41require __DIR__ . '/includes/WebStart.php';
42
43# Set action base paths so that WebRequest::getPathInfo()
44# recognizes the "X" as the 'title' in ../img_auth.php/X urls.
45$wgArticlePath = false; # Don't let a "/*" article path clober our action path
46$wgActionPaths = [ "$wgUploadPath/" ];
47
48wfImageAuthMain();
49
50$mediawiki = new MediaWiki();
51$mediawiki->doPostOutputShutdown( 'fast' );
52
53function wfImageAuthMain() {
54 global $wgImgAuthUrlPathMap;
55
56 $request = RequestContext::getMain()->getRequest();
57 $publicWiki = in_array( 'read', User::getGroupPermissions( [ '*' ] ), true );
58
59 // Get the requested file path (source file or thumbnail)
60 $matches = WebRequest::getPathInfo();
61 if ( !isset( $matches['title'] ) ) {
62 wfForbidden( 'img-auth-accessdenied', 'img-auth-nopathinfo' );
63 return;
64 }
65 $path = $matches['title'];
66 if ( $path && $path[0] !== '/' ) {
67 // Make sure $path has a leading /
68 $path = "/" . $path;
69 }
70
71 // Check for T30235: QUERY_STRING overriding the correct extension
72 $whitelist = [];
73 $extension = FileBackend::extensionFromPath( $path, 'rawcase' );
74 if ( $extension != '' ) {
75 $whitelist[] = $extension;
76 }
77 if ( !$request->checkUrlExtension( $whitelist ) ) {
78 return;
79 }
80
81 // Various extensions may have their own backends that need access.
82 // Check if there is a special backend and storage base path for this file.
83 foreach ( $wgImgAuthUrlPathMap as $prefix => $storageDir ) {
84 $prefix = rtrim( $prefix, '/' ) . '/'; // implicit trailing slash
85 if ( strpos( $path, $prefix ) === 0 ) {
86 $be = FileBackendGroup::singleton()->backendFromPath( $storageDir );
87 $filename = $storageDir . substr( $path, strlen( $prefix ) ); // strip prefix
88 // Check basic user authorization
89 if ( !RequestContext::getMain()->getUser()->isAllowed( 'read' ) ) {
90 wfForbidden( 'img-auth-accessdenied', 'img-auth-noread', $path );
91 return;
92 }
93 if ( $be->fileExists( [ 'src' => $filename ] ) ) {
94 wfDebugLog( 'img_auth', "Streaming `" . $filename . "`." );
95 $be->streamFile( [
96 'src' => $filename,
97 'headers' => [ 'Cache-Control: private', 'Vary: Cookie' ]
98 ] );
99 } else {
100 wfForbidden( 'img-auth-accessdenied', 'img-auth-nofile', $path );
101 }
102 return;
103 }
104 }
105
106 // Get the local file repository
107 $repo = RepoGroup::singleton()->getRepo( 'local' );
108 $zone = strstr( ltrim( $path, '/' ), '/', true );
109
110 // Get the full file storage path and extract the source file name.
111 // (e.g. 120px-Foo.png => Foo.png or page2-120px-Foo.png => Foo.png).
112 // This only applies to thumbnails/transcoded, and each of them should
113 // be under a folder that has the source file name.
114 if ( $zone === 'thumb' || $zone === 'transcoded' ) {
115 $name = wfBaseName( dirname( $path ) );
116 $filename = $repo->getZonePath( $zone ) . substr( $path, strlen( "/" . $zone ) );
117 // Check to see if the file exists
118 if ( !$repo->fileExists( $filename ) ) {
119 wfForbidden( 'img-auth-accessdenied', 'img-auth-nofile', $filename );
120 return;
121 }
122 } else {
123 $name = wfBaseName( $path ); // file is a source file
124 $filename = $repo->getZonePath( 'public' ) . $path;
125 // Check to see if the file exists and is not deleted
126 $bits = explode( '!', $name, 2 );
127 if ( substr( $path, 0, 9 ) === '/archive/' && count( $bits ) == 2 ) {
128 $file = $repo->newFromArchiveName( $bits[1], $name );
129 } else {
130 $file = $repo->newFile( $name );
131 }
132 if ( !$file->exists() || $file->isDeleted( File::DELETED_FILE ) ) {
133 wfForbidden( 'img-auth-accessdenied', 'img-auth-nofile', $filename );
134 return;
135 }
136 }
137
138 $headers = []; // extra HTTP headers to send
139
140 $title = Title::makeTitleSafe( NS_FILE, $name );
141
142 if ( !$publicWiki ) {
143 // For private wikis, run extra auth checks and set cache control headers
144 $headers['Cache-Control'] = 'private';
145 $headers['Vary'] = 'Cookie';
146
147 if ( !$title instanceof Title ) { // files have valid titles
148 wfForbidden( 'img-auth-accessdenied', 'img-auth-badtitle', $name );
149 return;
150 }
151
152 // Run hook for extension authorization plugins
154 $result = null;
155 if ( !Hooks::run( 'ImgAuthBeforeStream', [ &$title, &$path, &$name, &$result ] ) ) {
156 wfForbidden( $result[0], $result[1], array_slice( $result, 2 ) );
157 return;
158 }
159
160 // Check user authorization for this title
161 // Checks Whitelist too
162 if ( !$title->userCan( 'read' ) ) {
163 wfForbidden( 'img-auth-accessdenied', 'img-auth-noread', $name );
164 return;
165 }
166 }
167
168 if ( isset( $_SERVER['HTTP_RANGE'] ) ) {
169 $headers['Range'] = $_SERVER['HTTP_RANGE'];
170 }
171 if ( isset( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ) {
172 $headers['If-Modified-Since'] = $_SERVER['HTTP_IF_MODIFIED_SINCE'];
173 }
174
175 if ( $request->getCheck( 'download' ) ) {
176 $headers['Content-Disposition'] = 'attachment';
177 }
178
179 // Allow modification of headers before streaming a file
180 Hooks::run( 'ImgAuthModifyHeaders', [ $title->getTitleValue(), &$headers ] );
181
182 // Stream the requested file
183 list( $headers, $options ) = HTTPFileStreamer::preprocessHeaders( $headers );
184 wfDebugLog( 'img_auth', "Streaming `" . $filename . "`." );
185 $repo->streamFile( $filename, $headers, $options );
186}
187
195function wfForbidden( $msg1, $msg2 ) {
196 global $wgImgAuthDetails;
197
198 $args = func_get_args();
199 array_shift( $args );
200 array_shift( $args );
201 $args = ( isset( $args[0] ) && is_array( $args[0] ) ) ? $args[0] : $args;
202
203 $msgHdr = wfMessage( $msg1 )->escaped();
204 $detailMsgKey = $wgImgAuthDetails ? $msg2 : 'badaccess-group0';
205 $detailMsg = wfMessage( $detailMsgKey, $args )->escaped();
206
207 wfDebugLog( 'img_auth',
208 "wfForbidden Hdr: " . wfMessage( $msg1 )->inLanguage( 'en' )->text() . " Msg: " .
209 wfMessage( $msg2, $args )->inLanguage( 'en' )->text()
210 );
211
212 HttpStatus::header( 403 );
213 header( 'Cache-Control: no-cache' );
214 header( 'Content-Type: text/html; charset=utf-8' );
215 echo <<<ENDS
216<!DOCTYPE html>
217<html>
218<head>
219<meta charset="UTF-8" />
220<title>$msgHdr</title>
221</head>
222<body>
223<h1>$msgHdr</h1>
224<p>$detailMsg</p>
225</body>
226</html>
227ENDS;
228}
$wgArticlePath
Definition img_auth.php:45
Base interface for content objects.
Definition Content.php:34
you have access to all of the normal MediaWiki so you can get a DB use the cache