54 $services = \MediaWiki\MediaWikiServices::getInstance();
55 $permissionManager = $services->getPermissionManager();
57 $request = RequestContext::getMain()->getRequest();
58 $publicWiki = in_array(
'read', $permissionManager->getGroupPermissions( [
'*' ] ),
true );
61 $baseUrl = $services->getRepoGroup()->getLocalRepo()->getZoneUrl(
'public' );
62 if ( $baseUrl[0] ===
'/' ) {
65 $basePath = parse_url( $baseUrl, PHP_URL_PATH );
69 if (
$path ===
false ) {
75 if (
$path ===
false ) {
76 wfForbidden(
'img-auth-accessdenied',
'img-auth-notindir' );
85 $user = RequestContext::getMain()->getUser();
90 $prefix = rtrim( $prefix,
'/' ) .
'/';
91 if ( strpos(
$path, $prefix ) === 0 ) {
92 $be = $services->getFileBackendGroup()->backendFromPath( $storageDir );
93 $filename = $storageDir . substr(
$path, strlen( $prefix ) );
95 $isAllowedUser = $permissionManager->userHasRight( $user,
'read' );
96 if ( !$isAllowedUser ) {
100 if ( $be->fileExists( [
'src' => $filename ] ) ) {
101 wfDebugLog(
'img_auth',
"Streaming `" . $filename .
"`." );
104 'headers' => [
'Cache-Control: private',
'Vary: Cookie' ]
114 $repo = $services->getRepoGroup()->getRepo(
'local' );
115 $zone = strstr( ltrim(
$path,
'/' ),
'/',
true );
121 if ( $zone ===
'thumb' || $zone ===
'transcoded' ) {
123 $filename = $repo->getZonePath( $zone ) . substr(
$path, strlen(
"/" . $zone ) );
125 if ( !$repo->fileExists( $filename ) ) {
126 wfForbidden(
'img-auth-accessdenied',
'img-auth-nofile', $filename );
131 $filename = $repo->getZonePath(
'public' ) .
$path;
133 $bits = explode(
'!', $name, 2 );
134 if ( substr(
$path, 0, 9 ) ===
'/archive/' && count( $bits ) == 2 ) {
135 $file = $repo->newFromArchiveName( $bits[1], $name );
137 $file = $repo->newFile( $name );
139 if ( !
$file->exists() ||
$file->isDeleted( File::DELETED_FILE ) ) {
140 wfForbidden(
'img-auth-accessdenied',
'img-auth-nofile', $filename );
149 if ( !$publicWiki ) {
151 $headers[
'Cache-Control'] =
'private';
152 $headers[
'Vary'] =
'Cookie';
155 wfForbidden(
'img-auth-accessdenied',
'img-auth-badtitle', $name );
162 if ( !Hooks::runner()->onImgAuthBeforeStream(
$title,
$path, $name, $result ) ) {
163 wfForbidden( $result[0], $result[1], array_slice( $result, 2 ) );
170 if ( !$permissionManager->userCan(
'read', $user,
$title ) ) {
171 wfForbidden(
'img-auth-accessdenied',
'img-auth-noread', $name );
176 if ( isset( $_SERVER[
'HTTP_RANGE'] ) ) {
177 $headers[
'Range'] = $_SERVER[
'HTTP_RANGE'];
179 if ( isset( $_SERVER[
'HTTP_IF_MODIFIED_SINCE'] ) ) {
180 $headers[
'If-Modified-Since'] = $_SERVER[
'HTTP_IF_MODIFIED_SINCE'];
183 if ( $request->getCheck(
'download' ) ) {
184 $headers[
'Content-Disposition'] =
'attachment';
188 Hooks::runner()->onImgAuthModifyHeaders(
$title->getTitleValue(), $headers );
192 wfDebugLog(
'img_auth',
"Streaming `" . $filename .
"`." );
193 $repo->streamFileWithStatus( $filename, $headers, $options );
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...