From: Aaron Date: Thu, 26 Jul 2012 19:00:22 +0000 (-0700) Subject: Various thumb.php fixes. X-Git-Tag: 1.31.0-rc.0~22943^2 X-Git-Url: http://git.cyclocoop.org/%7B%24www_url%7Dadmin/compta/exercices/modifier.php?a=commitdiff_plain;h=25fe23a403bdd59a58b86b00d3727f638642f9cc;p=lhc%2Fweb%2Fwiklou.git Various thumb.php fixes. * Made wfThumbHandle404() handle full REQUEST_URI urls. * Made wfExtractThumbParams() handle protocal relative zone urls. * Added FileRepo::getZoneHandlerUrl() and site zone configuration to simplify wmf rewrite rules. * Renamed some variables to be less misleading. Change-Id: Ic3e23c2a623c1241ee22a9811aee073fb07aa68c --- diff --git a/includes/DefaultSettings.php b/includes/DefaultSettings.php index d7feab8906..abf25dc376 100644 --- a/includes/DefaultSettings.php +++ b/includes/DefaultSettings.php @@ -358,9 +358,11 @@ $wgImgAuthPublicTest = true; * * For most core repos: * - zones Associative array of zone names that each map to an array with: - * container : backend container name the zone is in - * directory : root path within container for the zone - * url : base URL to the root of the zone + * container : backend container name the zone is in + * directory : root path within container for the zone + * url : base URL to the root of the zone + * handlerUrl : base script handled URL to the root of the zone + * (see FileRepo::getZoneHandlerUrl() function) * Zones default to using "-" as the container name * and default to using the container root as the zone's root directory. * Nesting of zone locations within other zones should be avoided. diff --git a/includes/filerepo/FileRepo.php b/includes/filerepo/FileRepo.php index 5a550966f3..32ef5a9885 100644 --- a/includes/filerepo/FileRepo.php +++ b/includes/filerepo/FileRepo.php @@ -216,6 +216,28 @@ class FileRepo { } } + /** + * Get the thumb zone URL configured to be handled by scripts like thumb_handler.php. + * This is probably only useful for internal requests, such as from a fast frontend server + * to a slower backend server. + * + * Large sites may use a different host name for uploads than for wikis. In any case, the + * wiki configuration is needed in order to use thumb.php. To avoid extracting the wiki ID + * from the URL path, one can configure thumb_handler.php to recognize a special path on the + * same host name as the wiki that is used for viewing thumbnails. + * + * @param $zone String: one of: public, deleted, temp, thumb + * @return String or false + */ + public function getZoneHandlerUrl( $zone ) { + if ( isset( $this->zones[$zone]['handlerUrl'] ) + && in_array( $zone, array( 'public', 'temp', 'thumb' ) ) ) + { + return $this->zones[$zone]['handlerUrl']; + } + return false; + } + /** * Get the backend storage path corresponding to a virtual URL. * Use this function wisely. diff --git a/thumb.php b/thumb.php index 50d3754da9..9cfdae4d34 100644 --- a/thumb.php +++ b/thumb.php @@ -65,19 +65,22 @@ 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 ) !== '/' ) { + $bits = wfParseUrl( $uriPath ); + if ( $bits && isset( $bits['path'] ) ) { + $uriPath = $bits['path']; + } else { + 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; @@ -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 );