| 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 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
namespace MediaWiki\FileRepo; |
| 32 |
|
| 33 |
use Exception; |
| 34 |
use File; |
| 35 |
use InvalidArgumentException; |
| 36 |
use MediaTransformError; |
| 37 |
use MediaTransformInvalidParametersException; |
| 38 |
use MediaTransformOutput; |
| 39 |
use MediaWiki\Logger\LoggerFactory; |
| 40 |
use MediaWiki\MainConfigNames; |
| 41 |
use MediaWiki\MediaWikiEntryPoint; |
| 42 |
use MediaWiki\MediaWikiServices; |
| 43 |
use MediaWiki\Message\Message; |
| 44 |
use MediaWiki\Permissions\PermissionStatus; |
| 45 |
use MediaWiki\PoolCounter\PoolCounterWorkViaCallback; |
| 46 |
use MediaWiki\Profiler\ProfilingContext; |
| 47 |
use MediaWiki\Request\HeaderCallback; |
| 48 |
use MediaWiki\Status\Status; |
| 49 |
use MediaWiki\Title\Title; |
| 50 |
use RepoGroup; |
| 51 |
use UnregisteredLocalFile; |
| 52 |
use Wikimedia\AtEase\AtEase; |
| 53 |
use Wikimedia\Message\MessageSpecifier; |
| 54 |
|
| 55 |
class ThumbnailEntryPoint extends MediaWikiEntryPoint { |
| 56 |
|
| 57 |
|
| 58 |
private $varyHeader = []; |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
public function execute() { |
| 64 |
global $wgTrivialMimeDetection; |
| 65 |
|
| 66 |
ProfilingContext::singleton()->init( |
| 67 |
MW_ENTRY_POINT, |
| 68 |
'stream' |
| 69 |
); |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
$wgTrivialMimeDetection = true; |
| 76 |
|
| 77 |
$this->handleRequest(); |
| 78 |
} |
| 79 |
|
| 80 |
protected function doPrepareForOutput() { |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
} |
| 85 |
|
| 86 |
protected function handleRequest() { |
| 87 |
$this->streamThumb( $this->getRequest()->getQueryValuesOnly() ); |
| 88 |
} |
| 89 |
|
| 90 |
private function getRepoGroup(): RepoGroup { |
| 91 |
return $this->getServiceContainer()->getRepoGroup(); |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
protected function streamThumb( array $params ) { |
| 109 |
$headers = []; |
| 110 |
|
| 111 |
$fileName = $params['f'] ?? ''; |
| 112 |
|
| 113 |
|
| 114 |
if ( isset( $params['w'] ) ) { |
| 115 |
$params['width'] = $params['w']; |
| 116 |
unset( $params['w'] ); |
| 117 |
} |
| 118 |
if ( isset( $params['width'] ) && substr( $params['width'], -2 ) == 'px' ) { |
| 119 |
|
| 120 |
$params['width'] = substr( $params['width'], 0, -2 ); |
| 121 |
} |
| 122 |
if ( isset( $params['p'] ) ) { |
| 123 |
$params['page'] = $params['p']; |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
$isOld = ( isset( $params['archived'] ) && $params['archived'] ); |
| 128 |
unset( $params['archived'] ); |
| 129 |
|
| 130 |
|
| 131 |
$isTemp = ( isset( $params['temp'] ) && $params['temp'] ); |
| 132 |
unset( $params['temp'] ); |
| 133 |
|
| 134 |
$services = $this->getServiceContainer(); |
| 135 |
|
| 136 |
|
| 137 |
$fileName = strtr( $fileName, '\\/', '__' ); |
| 138 |
$localRepo = $services->getRepoGroup()->getLocalRepo(); |
| 139 |
$archiveTimestamp = null; |
| 140 |
|
| 141 |
|
| 142 |
if ( $isTemp ) { |
| 143 |
$repo = $localRepo->getTempRepo(); |
| 144 |
$img = new UnregisteredLocalFile( false, $repo, |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
$repo->getZonePath( 'public' ) . '/' . $repo->getTempHashPath( $fileName ) . $fileName |
| 149 |
); |
| 150 |
} elseif ( $isOld ) { |
| 151 |
|
| 152 |
$bits = explode( '!', $fileName, 2 ); |
| 153 |
if ( count( $bits ) != 2 ) { |
| 154 |
$this->thumbError( 404, $this->getContext()->msg( 'badtitletext' )->parse() ); |
| 155 |
return; |
| 156 |
} |
| 157 |
$archiveTimestamp = $bits[0]; |
| 158 |
$title = Title::makeTitleSafe( NS_FILE, $bits[1] ); |
| 159 |
if ( !$title ) { |
| 160 |
$this->thumbError( 404, $this->getContext()->msg( 'badtitletext' )->parse() ); |
| 161 |
return; |
| 162 |
} |
| 163 |
$img = $localRepo->newFromArchiveName( $title, $fileName ); |
| 164 |
} else { |
| 165 |
$img = $localRepo->newFile( $fileName ); |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
if ( !$img ) { |
| 170 |
$this->thumbError( 404, $this->getContext()->msg( 'badtitletext' )->parse() ); |
| 171 |
return; |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
if ( $this->maybeDenyAccess( $img ) ) { |
| 176 |
return; |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
if ( $img->isDeleted( File::DELETED_FILE ) ) { |
| 181 |
$this->thumbErrorText( 404, "The source file '$fileName' does not exist." ); |
| 182 |
return; |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
if ( isset( $params['thumbName'] ) ) { |
| 187 |
$params = $this->extractThumbParams( $img, $params ); |
| 188 |
} |
| 189 |
if ( $params == null ) { |
| 190 |
$this->thumbErrorText( 400, 'The specified thumbnail parameters are not recognized.' ); |
| 191 |
return; |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
if ( !$img->exists() ) { |
| 196 |
$redirectedHasTarget = $this->maybeDoRedirect( |
| 197 |
$img, |
| 198 |
$params, |
| 199 |
$isTemp, |
| 200 |
$isOld, |
| 201 |
$archiveTimestamp |
| 202 |
); |
| 203 |
|
| 204 |
if ( !$redirectedHasTarget ) { |
| 205 |
|
| 206 |
$this->thumbErrorText( |
| 207 |
404, |
| 208 |
"The source file '$fileName' does not exist." |
| 209 |
); |
| 210 |
} |
| 211 |
|
| 212 |
$this->applyVaryHeader(); |
| 213 |
|
| 214 |
return; |
| 215 |
} elseif ( $img->getPath() === false ) { |
| 216 |
$this->thumbErrorText( 400, "The source file '$fileName' is not locally accessible." ); |
| 217 |
return; |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
if ( $this->maybeNotModified( $img ) ) { |
| 223 |
return; |
| 224 |
} |
| 225 |
|
| 226 |
$rel404 = $params['rel404'] ?? null; |
| 227 |
unset( $params['r'] ); |
| 228 |
unset( $params['f'] ); |
| 229 |
unset( $params['rel404'] ); |
| 230 |
|
| 231 |
|
| 232 |
try { |
| 233 |
$thumbName = $img->thumbName( $params ); |
| 234 |
if ( !strlen( $thumbName ?? '' ) ) { |
| 235 |
throw new MediaTransformInvalidParametersException( |
| 236 |
'Empty return from File::thumbName' |
| 237 |
); |
| 238 |
} |
| 239 |
$thumbName2 = $img->thumbName( $params, File::THUMB_FULL_NAME ); |
| 240 |
} catch ( MediaTransformInvalidParametersException $e ) { |
| 241 |
$this->thumbErrorText( |
| 242 |
400, |
| 243 |
'The specified thumbnail parameters are not valid: ' . $e->getMessage() |
| 244 |
); |
| 245 |
|
| 246 |
return; |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
if ( $rel404 !== null ) { |
| 254 |
if ( |
| 255 |
$this->maybeNormalizeRel404Path( |
| 256 |
$img, |
| 257 |
$rel404, |
| 258 |
$thumbName, |
| 259 |
$thumbName2 |
| 260 |
) |
| 261 |
) { |
| 262 |
$this->applyVaryHeader(); |
| 263 |
|
| 264 |
return; |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
$dispositionType = isset( $params['download'] ) ? 'attachment' : 'inline'; |
| 269 |
|
| 270 |
|
| 271 |
$headers[] = |
| 272 |
'Content-Disposition: ' . $img->getThumbDisposition( $thumbName, $dispositionType ); |
| 273 |
|
| 274 |
$this->applyVaryHeader(); |
| 275 |
|
| 276 |
|
| 277 |
$thumbPath = $img->getThumbPath( $thumbName ); |
| 278 |
|
| 279 |
if ( $this->maybeStreamExistingThumbnail( $img, $thumbName, $thumbPath, $headers ) ) { |
| 280 |
return; |
| 281 |
} |
| 282 |
|
| 283 |
if ( $this->maybeEnforceRateLimits( $img, $params ) ) { |
| 284 |
return; |
| 285 |
} |
| 286 |
|
| 287 |
$thumbProxyUrl = $img->getRepo()->getThumbProxyUrl(); |
| 288 |
|
| 289 |
if ( strlen( $thumbProxyUrl ?? '' ) ) { |
| 290 |
$this->proxyThumbnailRequest( $img, $thumbName ); |
| 291 |
|
| 292 |
return; |
| 293 |
} else { |
| 294 |
|
| 295 |
[ $thumb, $errorMsg, $errorCode ] = $this->generateThumbnail( $img, $params, $thumbName, $thumbPath ); |
| 296 |
} |
| 297 |
|
| 298 |
$this->prepareForOutput(); |
| 299 |
|
| 300 |
if ( !$thumb ) { |
| 301 |
$errorMsg ??= 'unknown error'; |
| 302 |
$this->thumbError( $errorCode, $errorMsg, null, [ 'file' => $thumbName, 'path' => $thumbPath ] ); |
| 303 |
} else { |
| 304 |
|
| 305 |
|
| 306 |
'@phan-var MediaTransformOutput $thumb'; |
| 307 |
$status = $thumb->streamFileWithStatus( $headers ); |
| 308 |
if ( !$status->isOK() ) { |
| 309 |
$this->thumbError( 500, 'Could not stream the file', $status->getWikiText( false, false, 'en' ), [ |
| 310 |
'file' => $thumbName, 'path' => $thumbPath, |
| 311 |
'error' => $status->getWikiText( false, false, 'en' ) ] ); |
| 312 |
} |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
private function proxyThumbnailRequest( $img, $thumbName ) { |
| 323 |
$thumbProxyUrl = $img->getRepo()->getThumbProxyUrl(); |
| 324 |
|
| 325 |
|
| 326 |
$thumbProxiedUrl = $thumbProxyUrl . $img->getThumbRel( $thumbName ); |
| 327 |
|
| 328 |
$req = MediaWikiServices::getInstance()->getHttpRequestFactory()->create( $thumbProxiedUrl, [], __METHOD__ ); |
| 329 |
$secret = $img->getRepo()->getThumbProxySecret(); |
| 330 |
|
| 331 |
|
| 332 |
if ( strlen( $secret ?? '' ) ) { |
| 333 |
$req->setHeader( 'X-Swift-Secret', $secret ); |
| 334 |
} |
| 335 |
|
| 336 |
|
| 337 |
$req->execute(); |
| 338 |
|
| 339 |
HeaderCallback::warnIfHeadersSent(); |
| 340 |
|
| 341 |
|
| 342 |
$this->header( 'HTTP/1.1 ' . $req->getStatus() ); |
| 343 |
|
| 344 |
$headers = $req->getResponseHeaders(); |
| 345 |
|
| 346 |
foreach ( $headers as $key => $values ) { |
| 347 |
foreach ( $values as $value ) { |
| 348 |
$this->header( $key . ': ' . $value, false ); |
| 349 |
} |
| 350 |
} |
| 351 |
|
| 352 |
$this->print( $req->getContent() ); |
| 353 |
} |
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
protected function generateThumbnail( File $file, array $params, $thumbName, $thumbPath ) { |
| 367 |
$attemptFailureEpoch = $this->getConfig( MainConfigNames::AttemptFailureEpoch ); |
| 368 |
|
| 369 |
$services = MediaWikiServices::getInstance()->getObjectCacheFactory(); |
| 370 |
|
| 371 |
$cache = $services->getLocalClusterInstance(); |
| 372 |
$key = $cache->makeKey( |
| 373 |
'attempt-failures', |
| 374 |
$attemptFailureEpoch, |
| 375 |
$file->getRepo()->getName(), |
| 376 |
$file->getSha1(), |
| 377 |
md5( $thumbName ) |
| 378 |
); |
| 379 |
|
| 380 |
|
| 381 |
if ( $cache->get( $key ) >= 4 ) { |
| 382 |
return [ |
| 383 |
null, |
| 384 |
$this->getContext()->msg( 'thumbnail_image-failure-limit', 4 )->escaped(), |
| 385 |
500, |
| 386 |
]; |
| 387 |
} |
| 388 |
|
| 389 |
$done = false; |
| 390 |
|
| 391 |
register_shutdown_function( static function () use ( $cache, &$done, $key ) { |
| 392 |
if ( !$done ) { |
| 393 |
|
| 394 |
$cache->incrWithInit( $key, $cache::TTL_HOUR + mt_rand( 0, 300 ) ); |
| 395 |
} |
| 396 |
} ); |
| 397 |
|
| 398 |
|
| 399 |
$thumb = null; |
| 400 |
$errorHtml = null; |
| 401 |
'@phan-var MediaTransformOutput $thumb|false'; |
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
if ( $file->isExpensiveToThumbnail() ) { |
| 407 |
$poolCounterType = 'FileRenderExpensive'; |
| 408 |
} else { |
| 409 |
$poolCounterType = 'FileRender'; |
| 410 |
} |
| 411 |
|
| 412 |
|
| 413 |
try { |
| 414 |
$work = new PoolCounterWorkViaCallback( $poolCounterType, sha1( $file->getName() ), |
| 415 |
[ |
| 416 |
'doWork' => static function () use ( $file, $params ) { |
| 417 |
return $file->transform( $params, File::RENDER_NOW ); |
| 418 |
}, |
| 419 |
'doCachedWork' => static function () use ( $file, $params, $thumbPath ) { |
| 420 |
|
| 421 |
|
| 422 |
return $file->getRepo()->fileExists( $thumbPath ) |
| 423 |
? $file->transform( $params, File::RENDER_NOW ) |
| 424 |
: false; |
| 425 |
}, |
| 426 |
'error' => function ( Status $status ) { |
| 427 |
return $this->getContext()->msg( 'generic-pool-error' )->parse() . '<hr>' . $status->getHTML(); |
| 428 |
} |
| 429 |
] |
| 430 |
); |
| 431 |
$result = $work->execute(); |
| 432 |
if ( $result instanceof MediaTransformOutput ) { |
| 433 |
$thumb = $result; |
| 434 |
} elseif ( is_string( $result ) ) { |
| 435 |
$errorHtml = $result; |
| 436 |
} |
| 437 |
} catch ( Exception $e ) { |
| 438 |
|
| 439 |
} |
| 440 |
|
| 441 |
|
| 442 |
$done = true; |
| 443 |
|
| 444 |
if ( !$thumb || $thumb->isError() ) { |
| 445 |
|
| 446 |
$cache->incrWithInit( $key, $cache::TTL_HOUR + mt_rand( 0, 300 ) ); |
| 447 |
} |
| 448 |
|
| 449 |
|
| 450 |
$msg = $this->getContext()->msg( 'thumbnail_error' ); |
| 451 |
$errorCode = null; |
| 452 |
|
| 453 |
if ( !$thumb ) { |
| 454 |
$errorHtml = $errorHtml ?: $msg->rawParams( 'File::transform() returned false' ); |
| 455 |
$errorCode = 500; |
| 456 |
} elseif ( $thumb instanceof MediaTransformError ) { |
| 457 |
$errorHtml = $thumb->getMsg(); |
| 458 |
$errorCode = $thumb->getHttpStatusCode(); |
| 459 |
} elseif ( !$thumb->hasFile() ) { |
| 460 |
$errorHtml = $msg->rawParams( 'No path supplied in thumbnail object' ); |
| 461 |
$errorCode = 500; |
| 462 |
} elseif ( $thumb->fileIsSource() ) { |
| 463 |
$errorHtml = $msg |
| 464 |
->rawParams( 'Image was not scaled, is the requested width bigger than the source?' ); |
| 465 |
$errorCode = 400; |
| 466 |
} |
| 467 |
|
| 468 |
if ( $errorCode && $errorHtml ) { |
| 469 |
if ( $errorHtml instanceof MessageSpecifier && |
| 470 |
$errorHtml->getKey() === 'thumbnail_image-failure-limit' |
| 471 |
) { |
| 472 |
$errorCode = 429; |
| 473 |
} |
| 474 |
|
| 475 |
if ( $errorHtml instanceof Message ) { |
| 476 |
$errorHtml = $errorHtml->escaped(); |
| 477 |
} |
| 478 |
|
| 479 |
return [ null, $errorHtml, $errorCode ]; |
| 480 |
} |
| 481 |
|
| 482 |
return [ $thumb, null, 200 ]; |
| 483 |
} |
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
private function extractThumbParams( $file, $params ) { |
| 494 |
if ( !isset( $params['thumbName'] ) ) { |
| 495 |
throw new InvalidArgumentException( "No thumbnail name passed to extractThumbParams" ); |
| 496 |
} |
| 497 |
|
| 498 |
$thumbname = $params['thumbName']; |
| 499 |
unset( $params['thumbName'] ); |
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
$handler = $file->getHandler(); |
| 507 |
|
| 508 |
|
| 509 |
$fileNamePos = strrpos( $thumbname, $params['f'] ); |
| 510 |
if ( $fileNamePos === false ) { |
| 511 |
|
| 512 |
$fileNamePos = strrpos( $thumbname, 'thumbnail' ); |
| 513 |
} |
| 514 |
|
| 515 |
if ( $handler && $fileNamePos !== false ) { |
| 516 |
$paramString = substr( $thumbname, 0, $fileNamePos - 1 ); |
| 517 |
$extraParams = $handler->parseParamString( $paramString ); |
| 518 |
if ( $extraParams !== false ) { |
| 519 |
return $params + $extraParams; |
| 520 |
} |
| 521 |
} |
| 522 |
|
| 523 |
|
| 524 |
if ( preg_match( '!^(page(\d*)-)*(\d*)px-[^/]*$!', $thumbname, $matches ) ) { |
| 525 |
[ , , $pagenum, $size ] = $matches; |
| 526 |
$params['width'] = $size; |
| 527 |
if ( $pagenum ) { |
| 528 |
$params['page'] = $pagenum; |
| 529 |
} |
| 530 |
return $params; |
| 531 |
} |
| 532 |
return null; |
| 533 |
} |
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
protected function thumbErrorText( $status, $msgText ) { |
| 543 |
$this->thumbError( $status, htmlspecialchars( $msgText, ENT_NOQUOTES ) ); |
| 544 |
} |
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
protected function thumbError( $status, $msgHtml, $msgText = null, $context = [] ) { |
| 557 |
$showHostnames = $this->getConfig( MainConfigNames::ShowHostnames ); |
| 558 |
|
| 559 |
HeaderCallback::warnIfHeadersSent(); |
| 560 |
|
| 561 |
if ( $this->getResponse()->headersSent() ) { |
| 562 |
LoggerFactory::getInstance( 'thumbnail' )->error( |
| 563 |
'Error after output had been started. Output may be corrupt or truncated. ' . |
| 564 |
'Original error: ' . ( $msgText ?: $msgHtml ) . " (Status $status)", |
| 565 |
$context |
| 566 |
); |
| 567 |
return; |
| 568 |
} |
| 569 |
|
| 570 |
$this->header( 'Cache-Control: no-cache' ); |
| 571 |
$this->header( 'Content-Type: text/html; charset=utf-8' ); |
| 572 |
if ( $status == 400 || $status == 404 || $status == 429 ) { |
| 573 |
$this->status( $status ); |
| 574 |
} elseif ( $status == 403 ) { |
| 575 |
$this->status( 403 ); |
| 576 |
$this->header( 'Vary: Cookie' ); |
| 577 |
} else { |
| 578 |
LoggerFactory::getInstance( 'thumbnail' )->error( $msgText ?: $msgHtml, $context ); |
| 579 |
$this->status( 500 ); |
| 580 |
} |
| 581 |
if ( $showHostnames ) { |
| 582 |
$this->header( 'X-MW-Thumbnail-Renderer: ' . wfHostname() ); |
| 583 |
$url = htmlspecialchars( |
| 584 |
$this->getServerInfo( 'REQUEST_URI' ) ?? '', |
| 585 |
ENT_NOQUOTES |
| 586 |
); |
| 587 |
$hostname = htmlspecialchars( wfHostname(), ENT_NOQUOTES ); |
| 588 |
$debug = "<!-- $url -->\n<!-- $hostname -->\n"; |
| 589 |
} else { |
| 590 |
$debug = ''; |
| 591 |
} |
| 592 |
$content = <<<EOT |
| 593 |
<!DOCTYPE html> |
| 594 |
<html><head> |
| 595 |
<meta charset="UTF-8" /> |
| 596 |
<meta name="color-scheme" content="light dark" /> |
| 597 |
<title>Error generating thumbnail</title> |
| 598 |
</head> |
| 599 |
<body> |
| 600 |
<h1>Error generating thumbnail</h1> |
| 601 |
<p> |
| 602 |
$msgHtml |
| 603 |
</p> |
| 604 |
$debug |
| 605 |
</body> |
| 606 |
</html> |
| 607 |
|
| 608 |
EOT; |
| 609 |
$this->header( 'Content-Length: ' . strlen( $content ) ); |
| 610 |
$this->print( $content ); |
| 611 |
} |
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
private function maybeDoRedirect( |
| 617 |
File $img, |
| 618 |
array $params, |
| 619 |
bool $isTemp, |
| 620 |
bool $isOld, |
| 621 |
?string $archiveTimestamp |
| 622 |
): bool { |
| 623 |
$varyOnXFP = $this->getConfig( MainConfigNames::VaryOnXFP ); |
| 624 |
|
| 625 |
$redirectedLocation = false; |
| 626 |
if ( !$isTemp ) { |
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
$localRepo = $this->getRepoGroup()->getLocalRepo(); |
| 631 |
$possRedirFile = $localRepo->findFile( $img->getName() ); |
| 632 |
if ( $possRedirFile && $possRedirFile->getRedirected() !== null ) { |
| 633 |
$redirTarget = $possRedirFile->getName(); |
| 634 |
$targetFile = $localRepo->newFile( |
| 635 |
Title::makeTitleSafe( |
| 636 |
NS_FILE, |
| 637 |
$redirTarget |
| 638 |
) |
| 639 |
); |
| 640 |
if ( $targetFile->exists() ) { |
| 641 |
|
| 642 |
try { |
| 643 |
$newThumbName = $targetFile->thumbName( $params ); |
| 644 |
} catch ( MediaTransformInvalidParametersException $e ) { |
| 645 |
$this->thumbErrorText( |
| 646 |
400, |
| 647 |
'The specified thumbnail parameters are not valid: ' . $e->getMessage() |
| 648 |
); |
| 649 |
|
| 650 |
return true; |
| 651 |
} |
| 652 |
if ( $isOld ) { |
| 653 |
$newThumbUrl = $targetFile->getArchiveThumbUrl( |
| 654 |
$archiveTimestamp . '!' . $targetFile->getName(), |
| 655 |
$newThumbName |
| 656 |
); |
| 657 |
} else { |
| 658 |
$newThumbUrl = $targetFile->getThumbUrl( $newThumbName ); |
| 659 |
} |
| 660 |
$redirectedLocation = $this->getUrlUtils()->expand( |
| 661 |
$newThumbUrl, |
| 662 |
PROTO_CURRENT |
| 663 |
) ?? false; |
| 664 |
} |
| 665 |
} |
| 666 |
} |
| 667 |
|
| 668 |
if ( $redirectedLocation ) { |
| 669 |
|
| 670 |
$response = $this->getResponse(); |
| 671 |
$response->statusHeader( 302 ); |
| 672 |
$response->header( 'Location: ' . $redirectedLocation ); |
| 673 |
$response->header( |
| 674 |
'Expires: ' . gmdate( 'D, d M Y H:i:s', time() + 12 * 3600 ) . ' GMT' |
| 675 |
); |
| 676 |
if ( $varyOnXFP ) { |
| 677 |
$this->vary( 'X-Forwarded-Proto' ); |
| 678 |
} |
| 679 |
$response->header( 'Content-Length: 0' ); |
| 680 |
|
| 681 |
return true; |
| 682 |
} |
| 683 |
|
| 684 |
return false; |
| 685 |
} |
| 686 |
|
| 687 |
private function vary( $header ) { |
| 688 |
$this->varyHeader[] = $header; |
| 689 |
} |
| 690 |
|
| 691 |
private function applyVaryHeader() { |
| 692 |
if ( count( $this->varyHeader ) ) { |
| 693 |
$this->header( 'Vary: ' . implode( ', ', $this->varyHeader ) ); |
| 694 |
} |
| 695 |
} |
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
private function maybeDenyAccess( File $img ): bool { |
| 701 |
$permissionLookup = $this->getServiceContainer()->getGroupPermissionsLookup(); |
| 702 |
|
| 703 |
if ( !$permissionLookup->groupHasPermission( '*', 'read' ) ) { |
| 704 |
$authority = $this->getContext()->getAuthority(); |
| 705 |
$imgTitle = $img->getTitle(); |
| 706 |
|
| 707 |
if ( !$imgTitle || !$authority->authorizeRead( 'read', $imgTitle ) ) { |
| 708 |
$this->thumbErrorText( |
| 709 |
403, |
| 710 |
'Access denied. You do not have permission to access the source file.' |
| 711 |
); |
| 712 |
|
| 713 |
return true; |
| 714 |
} |
| 715 |
$this->header( 'Cache-Control: private' ); |
| 716 |
$this->vary( 'Cookie' ); |
| 717 |
} |
| 718 |
|
| 719 |
return false; |
| 720 |
} |
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
private function maybeNotModified( File $img ): bool { |
| 726 |
if ( $this->getServerInfo( 'HTTP_IF_MODIFIED_SINCE', '' ) !== '' ) { |
| 727 |
|
| 728 |
$imsString = preg_replace( |
| 729 |
'/;.*$/', |
| 730 |
'', |
| 731 |
$this->getServerInfo( 'HTTP_IF_MODIFIED_SINCE' ) ?? '' |
| 732 |
); |
| 733 |
|
| 734 |
|
| 735 |
AtEase::suppressWarnings(); |
| 736 |
$imsUnix = strtotime( $imsString ); |
| 737 |
AtEase::restoreWarnings(); |
| 738 |
if ( wfTimestamp( TS_UNIX, $img->getTimestamp() ) <= $imsUnix ) { |
| 739 |
$this->status( 304 ); |
| 740 |
|
| 741 |
return true; |
| 742 |
} |
| 743 |
} |
| 744 |
|
| 745 |
return false; |
| 746 |
} |
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
private function maybeNormalizeRel404Path( |
| 757 |
File $img, |
| 758 |
string $rel404, |
| 759 |
$thumbName, |
| 760 |
$thumbName2 |
| 761 |
): bool { |
| 762 |
$varyOnXFP = $this->getConfig( MainConfigNames::VaryOnXFP ); |
| 763 |
|
| 764 |
if ( rawurldecode( $rel404 ) === $img->getThumbRel( $thumbName ) ) { |
| 765 |
|
| 766 |
return false; |
| 767 |
} elseif ( rawurldecode( $rel404 ) === $img->getThumbRel( $thumbName2 ) ) { |
| 768 |
|
| 769 |
$target = $this->getUrlUtils()->expand( |
| 770 |
$img->getThumbUrl( $thumbName ), |
| 771 |
PROTO_CURRENT |
| 772 |
) ?? false; |
| 773 |
$this->status( 301 ); |
| 774 |
$this->header( 'Location: ' . $target ); |
| 775 |
$this->header( |
| 776 |
'Expires: ' . gmdate( 'D, d M Y H:i:s', time() + 7 * 86400 ) . ' GMT' |
| 777 |
); |
| 778 |
|
| 779 |
if ( $varyOnXFP ) { |
| 780 |
$this->vary( 'X-Forwarded-Proto' ); |
| 781 |
} |
| 782 |
|
| 783 |
return true; |
| 784 |
} else { |
| 785 |
$this->thumbErrorText( |
| 786 |
404, |
| 787 |
"The given path of the specified thumbnail is incorrect; expected '" . |
| 788 |
$img->getThumbRel( $thumbName ) . "' but got '" . rawurldecode( $rel404 ) . "'." |
| 789 |
); |
| 790 |
|
| 791 |
return true; |
| 792 |
} |
| 793 |
} |
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
private function maybeStreamExistingThumbnail( |
| 799 |
File $img, |
| 800 |
string $thumbName, |
| 801 |
string $thumbPath, |
| 802 |
array $headers |
| 803 |
): bool { |
| 804 |
$stats = $this->getServiceContainer()->getStatsFactory(); |
| 805 |
|
| 806 |
if ( $img->getRepo()->fileExists( $thumbPath ) ) { |
| 807 |
$starttime = microtime( true ); |
| 808 |
$status = $img->getRepo()->streamFileWithStatus( |
| 809 |
$thumbPath, |
| 810 |
$headers |
| 811 |
); |
| 812 |
$streamtime = microtime( true ) - $starttime; |
| 813 |
|
| 814 |
if ( $status->isOK() ) { |
| 815 |
$stats->getTiming( 'media_thumbnail_stream_seconds' ) |
| 816 |
->copyToStatsdAt( 'media.thumbnail.stream' ) |
| 817 |
->observe( $streamtime * 1000 ); |
| 818 |
} else { |
| 819 |
$this->thumbError( |
| 820 |
500, |
| 821 |
'Could not stream the file', |
| 822 |
$status->getWikiText( false, false, 'en' ), |
| 823 |
[ |
| 824 |
'file' => $thumbName, |
| 825 |
'path' => $thumbPath, |
| 826 |
'error' => $status->getWikiText( false, false, 'en' ), |
| 827 |
] |
| 828 |
); |
| 829 |
} |
| 830 |
|
| 831 |
return true; |
| 832 |
} |
| 833 |
|
| 834 |
return false; |
| 835 |
} |
| 836 |
|
| 837 |
private function maybeEnforceRateLimits( File $img, array $params ) { |
| 838 |
$authority = $this->getContext()->getAuthority(); |
| 839 |
$status = PermissionStatus::newEmpty(); |
| 840 |
|
| 841 |
if ( !wfThumbIsStandard( $img, $params ) |
| 842 |
&& !$authority->authorizeAction( 'renderfile-nonstandard', $status ) |
| 843 |
) { |
| 844 |
$statusFormatter = $this->getServiceContainer()->getFormatterFactory() |
| 845 |
->getStatusFormatter( $this->getContext() ); |
| 846 |
|
| 847 |
$this->thumbError( 429, $statusFormatter->getHTML( $status ) ); |
| 848 |
return true; |
| 849 |
} elseif ( !$authority->authorizeAction( 'renderfile', $status ) ) { |
| 850 |
$statusFormatter = $this->getServiceContainer()->getFormatterFactory() |
| 851 |
->getStatusFormatter( $this->getContext() ); |
| 852 |
|
| 853 |
$this->thumbError( 429, $statusFormatter->getHTML( $status ) ); |
| 854 |
return true; |
| 855 |
} |
| 856 |
|
| 857 |
return false; |
| 858 |
} |
| 859 |
|
| 860 |
} |