| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
namespace MediaWiki\FileRepo; |
| 27 |
|
| 28 |
use File; |
| 29 |
use MediaWiki\HookContainer\HookRunner; |
| 30 |
use MediaWiki\Html\TemplateParser; |
| 31 |
use MediaWiki\MainConfigNames; |
| 32 |
use MediaWiki\MediaWikiEntryPoint; |
| 33 |
use MediaWiki\Title\Title; |
| 34 |
use Wikimedia\FileBackend\HTTPFileStreamer; |
| 35 |
|
| 36 |
class AuthenticatedFileEntryPoint extends MediaWikiEntryPoint { |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
public function execute() { |
| 42 |
$services = $this->getServiceContainer(); |
| 43 |
$permissionManager = $services->getPermissionManager(); |
| 44 |
|
| 45 |
$request = $this->getRequest(); |
| 46 |
$publicWiki = $services->getGroupPermissionsLookup()->groupHasPermission( '*', 'read' ); |
| 47 |
|
| 48 |
|
| 49 |
$baseUrl = $services->getRepoGroup()->getLocalRepo()->getZoneUrl( 'public' ); |
| 50 |
if ( $baseUrl[0] === '/' ) { |
| 51 |
$basePath = $baseUrl; |
| 52 |
} else { |
| 53 |
$basePath = parse_url( $baseUrl, PHP_URL_PATH ); |
| 54 |
} |
| 55 |
$path = $this->getRequestPathSuffix( "$basePath" ); |
| 56 |
|
| 57 |
if ( $path === false ) { |
| 58 |
|
| 59 |
$basePath = $this->getConfig( MainConfigNames::ImgAuthPath ) |
| 60 |
?: $this->getConfig( MainConfigNames::ScriptPath ) . '/img_auth.php'; |
| 61 |
$path = $this->getRequestPathSuffix( $basePath ); |
| 62 |
} |
| 63 |
|
| 64 |
if ( $path === false ) { |
| 65 |
$this->forbidden( 'img-auth-accessdenied', 'img-auth-notindir' ); |
| 66 |
return; |
| 67 |
} |
| 68 |
|
| 69 |
if ( $path === '' || $path[0] !== '/' ) { |
| 70 |
|
| 71 |
$path = "/" . $path; |
| 72 |
} |
| 73 |
|
| 74 |
$user = $this->getContext()->getUser(); |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
$pathMap = $this->getConfig( MainConfigNames::ImgAuthUrlPathMap ); |
| 79 |
foreach ( $pathMap as $prefix => $storageDir ) { |
| 80 |
$prefix = rtrim( $prefix, '/' ) . '/'; |
| 81 |
if ( strpos( $path, $prefix ) === 0 ) { |
| 82 |
$be = $services->getFileBackendGroup()->backendFromPath( $storageDir ); |
| 83 |
$filename = $storageDir . substr( $path, strlen( $prefix ) ); |
| 84 |
|
| 85 |
$isAllowedUser = $permissionManager->userHasRight( $user, 'read' ); |
| 86 |
if ( !$isAllowedUser ) { |
| 87 |
$this->forbidden( 'img-auth-accessdenied', 'img-auth-noread', $path ); |
| 88 |
return; |
| 89 |
} |
| 90 |
if ( $be && $be->fileExists( [ 'src' => $filename ] ) ) { |
| 91 |
wfDebugLog( 'img_auth', "Streaming `" . $filename . "`." ); |
| 92 |
$be->streamFile( [ |
| 93 |
'src' => $filename, |
| 94 |
'headers' => [ 'Cache-Control: private', 'Vary: Cookie' ] |
| 95 |
] ); |
| 96 |
} else { |
| 97 |
$this->forbidden( 'img-auth-accessdenied', 'img-auth-nofile', $path ); |
| 98 |
} |
| 99 |
|
| 100 |
return; |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
$repo = $services->getRepoGroup()->getLocalRepo(); |
| 106 |
$zone = strstr( ltrim( $path, '/' ), '/', true ); |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
if ( $zone === 'thumb' || $zone === 'transcoded' ) { |
| 113 |
$name = wfBaseName( dirname( $path ) ); |
| 114 |
$filename = $repo->getZonePath( $zone ) . substr( $path, strlen( "/" . $zone ) ); |
| 115 |
|
| 116 |
if ( !$repo->fileExists( $filename ) ) { |
| 117 |
$this->forbidden( 'img-auth-accessdenied', 'img-auth-nofile', $filename ); |
| 118 |
return; |
| 119 |
} |
| 120 |
} else { |
| 121 |
$name = wfBaseName( $path ); |
| 122 |
$filename = $repo->getZonePath( 'public' ) . $path; |
| 123 |
|
| 124 |
$bits = explode( '!', $name, 2 ); |
| 125 |
if ( str_starts_with( $path, '/archive/' ) && count( $bits ) == 2 ) { |
| 126 |
$file = $repo->newFromArchiveName( $bits[1], $name ); |
| 127 |
} else { |
| 128 |
$file = $repo->newFile( $name ); |
| 129 |
} |
| 130 |
if ( !$file || !$file->exists() || $file->isDeleted( File::DELETED_FILE ) ) { |
| 131 |
$this->forbidden( 'img-auth-accessdenied', 'img-auth-nofile', $filename ); |
| 132 |
return; |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
$headers = []; |
| 137 |
|
| 138 |
$title = Title::makeTitleSafe( NS_FILE, $name ); |
| 139 |
|
| 140 |
$hookRunner = new HookRunner( $services->getHookContainer() ); |
| 141 |
if ( !$publicWiki ) { |
| 142 |
|
| 143 |
$headers['Cache-Control'] = 'private'; |
| 144 |
$headers['Vary'] = 'Cookie'; |
| 145 |
|
| 146 |
if ( !$title instanceof Title ) { |
| 147 |
$this->forbidden( 'img-auth-accessdenied', 'img-auth-badtitle', $name ); |
| 148 |
return; |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
$authResult = []; |
| 153 |
if ( !$hookRunner->onImgAuthBeforeStream( $title, $path, $name, $authResult ) ) { |
| 154 |
$this->forbidden( $authResult[0], $authResult[1], array_slice( $authResult, 2 ) ); |
| 155 |
return; |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
if ( !$permissionManager->userCan( 'read', $user, $title ) ) { |
| 162 |
$this->forbidden( 'img-auth-accessdenied', 'img-auth-noread', $name ); |
| 163 |
return; |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
$range = $this->environment->getServerInfo( 'HTTP_RANGE' ); |
| 168 |
$ims = $this->environment->getServerInfo( 'HTTP_IF_MODIFIED_SINCE' ); |
| 169 |
|
| 170 |
if ( $range !== null ) { |
| 171 |
$headers['Range'] = $range; |
| 172 |
} |
| 173 |
if ( $ims !== null ) { |
| 174 |
$headers['If-Modified-Since'] = $ims; |
| 175 |
} |
| 176 |
|
| 177 |
if ( $request->getCheck( 'download' ) ) { |
| 178 |
$headers['Content-Disposition'] = 'attachment'; |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
$hookRunner->onImgAuthModifyHeaders( $title->getTitleValue(), $headers ); |
| 183 |
|
| 184 |
|
| 185 |
$this->prepareForOutput(); |
| 186 |
|
| 187 |
[ $headers, $options ] = HTTPFileStreamer::preprocessHeaders( $headers ); |
| 188 |
wfDebugLog( 'img_auth', "Streaming `" . $filename . "`." ); |
| 189 |
$repo->streamFileWithStatus( $filename, $headers, $options ); |
| 190 |
|
| 191 |
$this->enterPostSendMode(); |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
private function forbidden( $msg1, $msg2, ...$args ) { |
| 205 |
$args = ( isset( $args[0] ) && is_array( $args[0] ) ) ? $args[0] : $args; |
| 206 |
$context = $this->getContext(); |
| 207 |
|
| 208 |
$msgHdr = $context->msg( $msg1 )->text(); |
| 209 |
$detailMsg = $this->getConfig( MainConfigNames::ImgAuthDetails ) |
| 210 |
? $context->msg( $msg2, $args )->text() |
| 211 |
: $context->msg( 'badaccess-group0' )->text(); |
| 212 |
|
| 213 |
wfDebugLog( |
| 214 |
'img_auth', |
| 215 |
"wfForbidden Hdr: " . $context->msg( $msg1 )->inLanguage( 'en' )->text() |
| 216 |
. " Msg: " . $context->msg( $msg2, $args )->inLanguage( 'en' )->text() |
| 217 |
); |
| 218 |
|
| 219 |
$this->status( 403 ); |
| 220 |
$this->header( 'Cache-Control: no-cache' ); |
| 221 |
$this->header( 'Content-Type: text/html; charset=utf-8' ); |
| 222 |
$language = $context->getLanguage(); |
| 223 |
$lang = $language->getHtmlCode(); |
| 224 |
$this->header( "Content-Language: $lang" ); |
| 225 |
$templateParser = new TemplateParser(); |
| 226 |
$this->print( |
| 227 |
$templateParser->processTemplate( 'ImageAuthForbidden', [ |
| 228 |
'dir' => $language->getDir(), |
| 229 |
'lang' => $lang, |
| 230 |
'msgHdr' => $msgHdr, |
| 231 |
'detailMsg' => $detailMsg, |
| 232 |
] ) |
| 233 |
); |
| 234 |
} |
| 235 |
} |