57 $services = \MediaWiki\MediaWikiServices::getInstance();
58 $permissionManager = $services->getPermissionManager();
60 $request = RequestContext::getMain()->getRequest();
61 $publicWiki = $services->getGroupPermissionsLookup()->groupHasPermission(
'*',
'read' );
64 $baseUrl = $services->getRepoGroup()->getLocalRepo()->getZoneUrl(
'public' );
65 if ( $baseUrl[0] ===
'/' ) {
68 $basePath = parse_url( $baseUrl, PHP_URL_PATH );
70 $path = WebRequest::getRequestPathSuffix( $basePath );
72 if (
$path ===
false ) {
75 $path = WebRequest::getRequestPathSuffix( $basePath );
78 if (
$path ===
false ) {
79 wfForbidden(
'img-auth-accessdenied',
'img-auth-notindir' );
88 $user = RequestContext::getMain()->getUser();
93 $prefix = rtrim( $prefix,
'/' ) .
'/';
94 if ( strpos(
$path, $prefix ) === 0 ) {
95 $be = $services->getFileBackendGroup()->backendFromPath( $storageDir );
96 $filename = $storageDir . substr(
$path, strlen( $prefix ) );
98 $isAllowedUser = $permissionManager->userHasRight( $user,
'read' );
99 if ( !$isAllowedUser ) {
103 if ( $be->fileExists( [
'src' => $filename ] ) ) {
104 wfDebugLog(
'img_auth',
"Streaming `" . $filename .
"`." );
107 'headers' => [
'Cache-Control: private',
'Vary: Cookie' ]
117 $repo = $services->getRepoGroup()->getRepo(
'local' );
118 $zone = strstr( ltrim(
$path,
'/' ),
'/',
true );
124 if ( $zone ===
'thumb' || $zone ===
'transcoded' ) {
126 $filename = $repo->getZonePath( $zone ) . substr(
$path, strlen(
"/" . $zone ) );
128 if ( !$repo->fileExists( $filename ) ) {
129 wfForbidden(
'img-auth-accessdenied',
'img-auth-nofile', $filename );
134 $filename = $repo->getZonePath(
'public' ) .
$path;
136 $bits = explode(
'!', $name, 2 );
137 if ( substr(
$path, 0, 9 ) ===
'/archive/' && count( $bits ) == 2 ) {
138 $file = $repo->newFromArchiveName( $bits[1], $name );
140 $file = $repo->newFile( $name );
142 if ( !
$file->exists() ||
$file->isDeleted( File::DELETED_FILE ) ) {
143 wfForbidden(
'img-auth-accessdenied',
'img-auth-nofile', $filename );
152 if ( !$publicWiki ) {
154 $headers[
'Cache-Control'] =
'private';
155 $headers[
'Vary'] =
'Cookie';
158 wfForbidden(
'img-auth-accessdenied',
'img-auth-badtitle', $name );
165 if ( !Hooks::runner()->onImgAuthBeforeStream(
$title,
$path, $name, $result ) ) {
166 wfForbidden( $result[0], $result[1], array_slice( $result, 2 ) );
173 if ( !$permissionManager->userCan(
'read', $user,
$title ) ) {
174 wfForbidden(
'img-auth-accessdenied',
'img-auth-noread', $name );
179 if ( isset( $_SERVER[
'HTTP_RANGE'] ) ) {
180 $headers[
'Range'] = $_SERVER[
'HTTP_RANGE'];
182 if ( isset( $_SERVER[
'HTTP_IF_MODIFIED_SINCE'] ) ) {
183 $headers[
'If-Modified-Since'] = $_SERVER[
'HTTP_IF_MODIFIED_SINCE'];
186 if ( $request->getCheck(
'download' ) ) {
187 $headers[
'Content-Disposition'] =
'attachment';
191 Hooks::runner()->onImgAuthModifyHeaders(
$title->getTitleValue(), $headers );
195 wfDebugLog(
'img_auth',
"Streaming `" . $filename .
"`." );
196 $repo->streamFileWithStatus( $filename, $headers, $options );
211 $args = ( isset( $args[0] ) && is_array( $args[0] ) ) ? $args[0] : $args;
215 $detailMsg =
wfMessage( $detailMsgKey, $args )->text();
218 "wfForbidden Hdr: " .
wfMessage( $msg1 )->inLanguage(
'en' )->text() .
" Msg: " .
219 wfMessage( $msg2, $args )->inLanguage(
'en' )->text()
222 HttpStatus::header( 403 );
223 header(
'Cache-Control: no-cache' );
224 header(
'Content-Type: text/html; charset=utf-8' );
228 'detailMsg' => $detailMsg,
wfDebugLog( $logGroup, $text, $dest='all', array $context=[])
Send a line to a supplementary debug log file, if configured, or main debug log if not.
wfForbidden( $msg1, $msg2,... $args)
Issue a standard HTTP 403 Forbidden header ($msg1-a message index, not a message) and an error messag...