Merge "Upgrade wikimedia/remex-html to 2.0.1" into REL1_31
[lhc/web/wiklou.git] / thumb.php
index c38b89c..3b71413 100644 (file)
--- a/thumb.php
+++ b/thumb.php
@@ -22,6 +22,7 @@
  */
 
 use MediaWiki\Logger\LoggerFactory;
+use MediaWiki\MediaWikiServices;
 
 define( 'MW_NO_OUTPUT_COMPRESSION', 1 );
 require __DIR__ . '/includes/WebStart.php';
@@ -234,9 +235,9 @@ function wfStreamThumb( array $params ) {
                // Fix IE brokenness
                $imsString = preg_replace( '/;.*$/', '', $_SERVER["HTTP_IF_MODIFIED_SINCE"] );
                // Calculate time
-               MediaWiki\suppressWarnings();
+               Wikimedia\suppressWarnings();
                $imsUnix = strtotime( $imsString );
-               MediaWiki\restoreWarnings();
+               Wikimedia\restoreWarnings();
                if ( wfTimestamp( TS_UNIX, $img->getTimestamp() ) <= $imsUnix ) {
                        HttpStatus::header( 304 );
                        return;
@@ -272,7 +273,7 @@ function wfStreamThumb( array $params ) {
        // For 404 handled thumbnails, we only use the base name of the URI
        // for the thumb params and the parent directory for the source file name.
        // Check that the zone relative path matches up so squid caches won't pick
-       // up thumbs that would not be purged on source file deletion (bug 34231).
+       // up thumbs that would not be purged on source file deletion (T36231).
        if ( $rel404 !== null ) { // thumbnail was handled via 404
                if ( rawurldecode( $rel404 ) === $img->getThumbRel( $thumbName ) ) {
                        // Request for the canonical thumbnail name
@@ -317,7 +318,9 @@ function wfStreamThumb( array $params ) {
                $streamtime = microtime( true ) - $starttime;
 
                if ( $status->isOK() ) {
-                       RequestContext::getMain()->getStats()->timing( 'media.thumbnail.stream', $streamtime );
+                       MediaWikiServices::getInstance()->getStatsdDataFactory()->timing(
+                               'media.thumbnail.stream', $streamtime
+                       );
                } else {
                        wfThumbError( 500, 'Could not stream the file', null, [ 'file' => $thumbName,
                                'path' => $thumbPath, 'error' => $status->getWikiText( false, false, 'en' ) ] );
@@ -334,7 +337,16 @@ function wfStreamThumb( array $params ) {
                return;
        }
 
-       list( $thumb, $errorMsg ) = wfGenerateThumbnail( $img, $params, $thumbName, $thumbPath );
+       $thumbProxyUrl = $img->getRepo()->getThumbProxyUrl();
+
+       if ( strlen( $thumbProxyUrl ) ) {
+               wfProxyThumbnailRequest( $img, $thumbName );
+               // No local fallback when in proxy mode
+               return;
+       } else {
+               // Generate the thumbnail locally
+               list( $thumb, $errorMsg ) = wfGenerateThumbnail( $img, $params, $thumbName, $thumbPath );
+       }
 
        /** @var MediaTransformOutput|MediaTransformError|bool $thumb */
 
@@ -374,6 +386,43 @@ function wfStreamThumb( array $params ) {
        }
 }
 
+/**
+ * Proxies thumbnail request to a service that handles thumbnailing
+ *
+ * @param File $img
+ * @param string $thumbName
+ */
+function wfProxyThumbnailRequest( $img, $thumbName ) {
+       $thumbProxyUrl = $img->getRepo()->getThumbProxyUrl();
+
+       // Instead of generating the thumbnail ourselves, we proxy the request to another service
+       $thumbProxiedUrl = $thumbProxyUrl . $img->getThumbRel( $thumbName );
+
+       $req = MWHttpRequest::factory( $thumbProxiedUrl );
+       $secret = $img->getRepo()->getThumbProxySecret();
+
+       // Pass a secret key shared with the proxied service if any
+       if ( strlen( $secret ) ) {
+               $req->setHeader( 'X-Swift-Secret', $secret );
+       }
+
+       // Send request to proxied service
+       $status = $req->execute();
+
+       // Simply serve the response from the proxied service as-is
+       header( 'HTTP/1.1 ' . $req->getStatus() );
+
+       $headers = $req->getResponseHeaders();
+
+       foreach ( $headers as $key => $values ) {
+               foreach ( $values as $value ) {
+                       header( $key . ': ' . $value, false );
+               }
+       }
+
+       echo $req->getContent();
+}
+
 /**
  * Actually try to generate a new thumbnail
  *
@@ -527,15 +576,6 @@ function wfExtractThumbParams( $file, $params ) {
        $thumbname = $params['thumbName'];
        unset( $params['thumbName'] );
 
-       // Do the hook first for older extensions that rely on it.
-       if ( !Hooks::run( 'ExtractThumbParameters', [ $thumbname, &$params ] ) ) {
-               // Check hooks if parameters can be extracted
-               // Hooks return false if they manage to *resolve* the parameters
-               // This hook should be considered deprecated
-               wfDeprecated( 'ExtractThumbParameters', '1.22' );
-               return $params; // valid thumbnail URL (via extension or config)
-       }
-
        // FIXME: Files in the temp zone don't set a MIME type, which means
        // they don't have a handler. Which means we can't parse the param
        // string. However, not a big issue as what good is a param string
@@ -578,7 +618,7 @@ function wfExtractThumbParams( $file, $params ) {
  * @return void
  */
 function wfThumbErrorText( $status, $msgText ) {
-       wfThumbError( $status, htmlspecialchars( $msgText ) );
+       wfThumbError( $status, htmlspecialchars( $msgText, ENT_NOQUOTES ) );
 }
 
 /**
@@ -608,9 +648,10 @@ function wfThumbError( $status, $msgHtml, $msgText = null, $context = [] ) {
        if ( $wgShowHostnames ) {
                header( 'X-MW-Thumbnail-Renderer: ' . wfHostname() );
                $url = htmlspecialchars(
-                       isset( $_SERVER['REQUEST_URI'] ) ? $_SERVER['REQUEST_URI'] : ''
+                       isset( $_SERVER['REQUEST_URI'] ) ? $_SERVER['REQUEST_URI'] : '',
+                       ENT_NOQUOTES
                );
-               $hostname = htmlspecialchars( wfHostname() );
+               $hostname = htmlspecialchars( wfHostname(), ENT_NOQUOTES );
                $debug = "<!-- $url -->\n<!-- $hostname -->\n";
        } else {
                $debug = '';