MediaWiki REL1_40
img_auth.php
Go to the documentation of this file.
1<?php
44
45define( 'MW_NO_OUTPUT_COMPRESSION', 1 );
46define( 'MW_ENTRY_POINT', 'img_auth' );
47require __DIR__ . '/includes/WebStart.php';
48
50
52$mediawiki->doPostOutputShutdown();
53
54function wfImageAuthMain() {
56
57 $services = \MediaWiki\MediaWikiServices::getInstance();
58 $permissionManager = $services->getPermissionManager();
59
60 $request = RequestContext::getMain()->getRequest();
61 $publicWiki = $services->getGroupPermissionsLookup()->groupHasPermission( '*', 'read' );
62
63 // Find the path assuming the request URL is relative to the local public zone URL
64 $baseUrl = $services->getRepoGroup()->getLocalRepo()->getZoneUrl( 'public' );
65 if ( $baseUrl[0] === '/' ) {
66 $basePath = $baseUrl;
67 } else {
68 $basePath = parse_url( $baseUrl, PHP_URL_PATH );
69 }
70 $path = WebRequest::getRequestPathSuffix( $basePath );
71
72 if ( $path === false ) {
73 // Try instead assuming img_auth.php is the base path
74 $basePath = $wgImgAuthPath ?: "$wgScriptPath/img_auth.php";
75 $path = WebRequest::getRequestPathSuffix( $basePath );
76 }
77
78 if ( $path === false ) {
79 wfForbidden( 'img-auth-accessdenied', 'img-auth-notindir' );
80 return;
81 }
82
83 if ( $path === '' || $path[0] !== '/' ) {
84 // Make sure $path has a leading /
85 $path = "/" . $path;
86 }
87
88 $user = RequestContext::getMain()->getUser();
89
90 // Various extensions may have their own backends that need access.
91 // Check if there is a special backend and storage base path for this file.
92 foreach ( $wgImgAuthUrlPathMap as $prefix => $storageDir ) {
93 $prefix = rtrim( $prefix, '/' ) . '/'; // implicit trailing slash
94 if ( strpos( $path, $prefix ) === 0 ) {
95 $be = $services->getFileBackendGroup()->backendFromPath( $storageDir );
96 $filename = $storageDir . substr( $path, strlen( $prefix ) ); // strip prefix
97 // Check basic user authorization
98 $isAllowedUser = $permissionManager->userHasRight( $user, 'read' );
99 if ( !$isAllowedUser ) {
100 wfForbidden( 'img-auth-accessdenied', 'img-auth-noread', $path );
101 return;
102 }
103 if ( $be->fileExists( [ 'src' => $filename ] ) ) {
104 wfDebugLog( 'img_auth', "Streaming `" . $filename . "`." );
105 $be->streamFile( [
106 'src' => $filename,
107 'headers' => [ 'Cache-Control: private', 'Vary: Cookie' ]
108 ] );
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 = $services->getRepoGroup()->getRepo( 'local' );
118 $zone = strstr( ltrim( $path, '/' ), '/', true );
119
120 // Get the full file storage path and extract the source file name.
121 // (e.g. 120px-Foo.png => Foo.png or page2-120px-Foo.png => Foo.png).
122 // This only applies to thumbnails/transcoded, and each of them should
123 // be under a folder that has the source file name.
124 if ( $zone === 'thumb' || $zone === 'transcoded' ) {
125 $name = wfBaseName( dirname( $path ) );
126 $filename = $repo->getZonePath( $zone ) . substr( $path, strlen( "/" . $zone ) );
127 // Check to see if the file exists
128 if ( !$repo->fileExists( $filename ) ) {
129 wfForbidden( 'img-auth-accessdenied', 'img-auth-nofile', $filename );
130 return;
131 }
132 } else {
133 $name = wfBaseName( $path ); // file is a source file
134 $filename = $repo->getZonePath( 'public' ) . $path;
135 // Check to see if the file exists and is not deleted
136 $bits = explode( '!', $name, 2 );
137 if ( substr( $path, 0, 9 ) === '/archive/' && count( $bits ) == 2 ) {
138 $file = $repo->newFromArchiveName( $bits[1], $name );
139 } else {
140 $file = $repo->newFile( $name );
141 }
142 if ( !$file->exists() || $file->isDeleted( File::DELETED_FILE ) ) {
143 wfForbidden( 'img-auth-accessdenied', 'img-auth-nofile', $filename );
144 return;
145 }
146 }
147
148 $headers = []; // extra HTTP headers to send
149
150 $title = Title::makeTitleSafe( NS_FILE, $name );
151
152 if ( !$publicWiki ) {
153 // For private wikis, run extra auth checks and set cache control headers
154 $headers['Cache-Control'] = 'private';
155 $headers['Vary'] = 'Cookie';
156
157 if ( !$title instanceof Title ) { // files have valid titles
158 wfForbidden( 'img-auth-accessdenied', 'img-auth-badtitle', $name );
159 return;
160 }
161
162 // Run hook for extension authorization plugins
164 $result = null;
165 if ( !Hooks::runner()->onImgAuthBeforeStream( $title, $path, $name, $result ) ) {
166 wfForbidden( $result[0], $result[1], array_slice( $result, 2 ) );
167 return;
168 }
169
170 // Check user authorization for this title
171 // Checks Whitelist too
172
173 if ( !$permissionManager->userCan( 'read', $user, $title ) ) {
174 wfForbidden( 'img-auth-accessdenied', 'img-auth-noread', $name );
175 return;
176 }
177 }
178
179 if ( isset( $_SERVER['HTTP_RANGE'] ) ) {
180 $headers['Range'] = $_SERVER['HTTP_RANGE'];
181 }
182 if ( isset( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ) {
183 $headers['If-Modified-Since'] = $_SERVER['HTTP_IF_MODIFIED_SINCE'];
184 }
185
186 if ( $request->getCheck( 'download' ) ) {
187 $headers['Content-Disposition'] = 'attachment';
188 }
189
190 // Allow modification of headers before streaming a file
191 Hooks::runner()->onImgAuthModifyHeaders( $title->getTitleValue(), $headers );
192
193 // Stream the requested file
194 [ $headers, $options ] = HTTPFileStreamer::preprocessHeaders( $headers );
195 wfDebugLog( 'img_auth', "Streaming `" . $filename . "`." );
196 $repo->streamFileWithStatus( $filename, $headers, $options );
197}
198
208function wfForbidden( $msg1, $msg2, ...$args ) {
209 global $wgImgAuthDetails;
210
211 $args = ( isset( $args[0] ) && is_array( $args[0] ) ) ? $args[0] : $args;
212
213 $msgHdr = wfMessage( $msg1 )->text();
214 $detailMsgKey = $wgImgAuthDetails ? $msg2 : 'badaccess-group0';
215 $detailMsg = wfMessage( $detailMsgKey, $args )->text();
216
217 wfDebugLog( 'img_auth',
218 "wfForbidden Hdr: " . wfMessage( $msg1 )->inLanguage( 'en' )->text() . " Msg: " .
219 wfMessage( $msg2, $args )->inLanguage( 'en' )->text()
220 );
221
222 HttpStatus::header( 403 );
223 header( 'Cache-Control: no-cache' );
224 header( 'Content-Type: text/html; charset=utf-8' );
226 echo $templateParser->processTemplate( 'ImageAuthForbidden', [
227 'msgHdr' => $msgHdr,
228 'detailMsg' => $detailMsg,
229 ] );
230}
const NS_FILE
Definition Defines.php:70
wfBaseName( $path, $suffix='')
Return the final portion of a pathname.
wfDebugLog( $logGroup, $text, $dest='all', array $context=[])
Send a line to a supplementary debug log file, if configured, or main debug log if not.
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
$templateParser
static preprocessHeaders( $headers)
Takes HTTP headers in a name => value format and converts them to the weird format expected by stream...
Represents a title within MediaWiki.
Definition Title.php:82
$wgImgAuthPath
Config variable stub for the ImgAuthPath setting, for use by phpdoc and IDEs.
$wgImgAuthDetails
Config variable stub for the ImgAuthDetails setting, for use by phpdoc and IDEs.
$wgImgAuthUrlPathMap
Config variable stub for the ImgAuthUrlPathMap setting, for use by phpdoc and IDEs.
$wgScriptPath
Config variable stub for the ScriptPath setting, for use by phpdoc and IDEs.
wfForbidden( $msg1, $msg2,... $args)
Issue a standard HTTP 403 Forbidden header ($msg1-a message index, not a message) and an error messag...
Definition img_auth.php:208
wfImageAuthMain()
Definition img_auth.php:54
$mediawiki
Definition img_auth.php:51
A helper class for throttling authentication attempts.
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition router.php:42