(bug 23226) Add |class= parameter to image links in order to add class(es) to HTML...
[lhc/web/wiklou.git] / thumb.php
index 50d3754..5fc4446 100644 (file)
--- a/thumb.php
+++ b/thumb.php
@@ -65,19 +65,21 @@ function wfThumbHandle404() {
        # that to the 404 handler, and puts the original request in REDIRECT_URL.
        if ( isset( $_SERVER['REDIRECT_URL'] ) ) {
                # The URL is un-encoded, so put it back how it was
-               $uri = str_replace( "%2F", "/", urlencode( $_SERVER['REDIRECT_URL'] ) );
-               # Just get the URI path (REDIRECT_URL is either a full URL or a path)
-               if ( $uri[0] !== '/' ) {
-                       $bits = wfParseUrl( $uri );
-                       if ( $bits && isset( $bits['path'] ) ) {
-                               $uri = $bits['path'];
-                       }
-               }
+               $uriPath = str_replace( "%2F", "/", urlencode( $_SERVER['REDIRECT_URL'] ) );
        } else {
-               $uri = $_SERVER['REQUEST_URI'];
+               $uriPath = $_SERVER['REQUEST_URI'];
+       }
+       # Just get the URI path (REDIRECT_URL/REQUEST_URI is either a full URL or a path)
+       if ( substr( $uriPath, 0, 1 ) !== '/' ) {
+               $uri = new Uri( $uriPath );
+               $uriPath = $uri->getPath();
+               if ( $uriPath === null ) {
+                       wfThumbError( 404, 'The source file for the specified thumbnail does not exist.' );
+                       return;
+               }
        }
 
-       $params = wfExtractThumbParams( $uri ); // basic wiki URL param extracting
+       $params = wfExtractThumbParams( $uriPath ); // basic wiki URL param extracting
        if ( $params == null ) {
                wfThumbError( 404, 'The source file for the specified thumbnail does not exist.' );
                return;
@@ -126,13 +128,13 @@ function wfStreamThumb( array $params ) {
                // Format is <timestamp>!<name>
                $bits = explode( '!', $fileName, 2 );
                if ( count( $bits ) != 2 ) {
-                       wfThumbError( 404, wfMsg( 'badtitletext' ) );
+                       wfThumbError( 404, wfMessage( 'badtitletext' )->text() );
                        wfProfileOut( __METHOD__ );
                        return;
                }
                $title = Title::makeTitleSafe( NS_FILE, $bits[1] );
                if ( !$title ) {
-                       wfThumbError( 404, wfMsg( 'badtitletext' ) );
+                       wfThumbError( 404, wfMessage( 'badtitletext' )->text() );
                        wfProfileOut( __METHOD__ );
                        return;
                }
@@ -144,7 +146,7 @@ function wfStreamThumb( array $params ) {
                // Get the name without the timestamp so hash paths are correctly computed
                $title = Title::makeTitleSafe( NS_FILE, isset( $bits[1] ) ? $bits[1] : $fileName );
                if ( !$title ) {
-                       wfThumbError( 404, wfMsg( 'badtitletext' ) );
+                       wfThumbError( 404, wfMessage( 'badtitletext' )->text() );
                        wfProfileOut( __METHOD__ );
                        return;
                }
@@ -169,7 +171,7 @@ function wfStreamThumb( array $params ) {
 
        // Check the source file storage path
        if ( !$img ) {
-               wfThumbError( 404, wfMsg( 'badtitletext' ) );
+               wfThumbError( 404, wfMessage( 'badtitletext' )->text() );
                wfProfileOut( __METHOD__ );
                return;
        }
@@ -240,15 +242,16 @@ function wfStreamThumb( array $params ) {
 
        // Check for thumbnail generation errors...
        $errorMsg = false;
+       $msg = wfMessage( 'thumbnail_error' );
        if ( !$thumb ) {
-               $errorMsg = wfMsgHtml( 'thumbnail_error', 'File::transform() returned false' );
+               $errorMsg = $msg->rawParams( 'File::transform() returned false' )->escaped();
        } elseif ( $thumb->isError() ) {
                $errorMsg = $thumb->getHtmlMsg();
        } elseif ( !$thumb->hasFile() ) {
-               $errorMsg = wfMsgHtml( 'thumbnail_error', 'No path supplied in thumbnail object' );
+               $errorMsg = $msg->rawParams( 'No path supplied in thumbnail object' )->escaped();
        } elseif ( $thumb->fileIsSource() ) {
-               $errorMsg = wfMsgHtml( 'thumbnail_error',
-                       'Image was not scaled, is the requested width bigger than the source?' );
+               $errorMsg = $msg->
+                       rawParams( 'Image was not scaled, is the requested width bigger than the source?' )->escaped();
        }
 
        if ( $errorMsg !== false ) {
@@ -265,22 +268,22 @@ function wfStreamThumb( array $params ) {
  * Extract the required params for thumb.php from the thumbnail request URI.
  * At least 'width' and 'f' should be set if the result is an array.
  *
- * @param $uri String Thumbnail request URI path
+ * @param $uriPath String Thumbnail request URI path
  * @return Array|null associative params array or null
  */
-function wfExtractThumbParams( $uri ) {
+function wfExtractThumbParams( $uriPath ) {
        $repo = RepoGroup::singleton()->getLocalRepo();
 
-       $zoneURI = $repo->getZoneUrl( 'thumb' );
-       if ( substr( $zoneURI, 0, 1 ) !== '/' ) {
-               $bits = wfParseUrl( $zoneURI );
-               if ( $bits && isset( $bits['path'] ) ) {
-                       $zoneURI = $bits['path'];
-               } else {
-                       return null;
-               }
+       $zoneUriPath = $repo->getZoneHandlerUrl( 'thumb' )
+               ? $repo->getZoneHandlerUrl( 'thumb' ) // custom URL
+               : $repo->getZoneUrl( 'thumb' ); // default to main URL
+       // URL might be relative ("/images") or protocol-relative ("//lang.site/image")
+       $bits = wfParseUrl( wfExpandUrl( $zoneUriPath, PROTO_INTERNAL ) );
+       if ( $bits && isset( $bits['path'] ) ) {
+               $zoneUriPath = $bits['path'];
+       } else {
+               return null;
        }
-       $zoneUrlRegex = preg_quote( $zoneURI );
 
        $hashDirRegex = $subdirRegex = '';
        for ( $i = 0; $i < $repo->getHashLevels(); $i++ ) {
@@ -288,10 +291,11 @@ function wfExtractThumbParams( $uri ) {
                $hashDirRegex .= "$subdirRegex/";
        }
 
-       $thumbUrlRegex = "!^$zoneUrlRegex/((archive/|temp/)?$hashDirRegex([^/]*)/([^/]*))$!";
+       $thumbPathRegex = "!^" . preg_quote( $zoneUriPath ) .
+               "/((archive/|temp/)?$hashDirRegex([^/]*)/([^/]*))$!";
 
        // Check if this is a valid looking thumbnail request...
-       if ( preg_match( $thumbUrlRegex, $uri, $matches ) ) {
+       if ( preg_match( $thumbPathRegex, $uriPath, $matches ) ) {
                list( /* all */, $rel, $archOrTemp, $filename, $thumbname ) = $matches;
                $filename = urldecode( $filename );
                $thumbname = urldecode( $thumbname );