X-Git-Url: http://git.cyclocoop.org/%22%20.%20generer_url_ecrire%28%22articles_versions%22%2C%22id_article=%24id_article%22%29%20.%20%22?a=blobdiff_plain;f=thumb.php;h=bdb24f927c5d5cecf010adb33660f1a5dc96f54e;hb=7db9491d8969a60c9f9e4d93f8bac1bd4184f0d5;hp=50d3754da90d993824df7fa0cc9e2200a3cd3030;hpb=4acf38255f60c7f91a30e9f2aefabfcbbe41a6ce;p=lhc%2Fweb%2Fwiklou.git diff --git a/thumb.php b/thumb.php index 50d3754da9..bdb24f927c 100644 --- a/thumb.php +++ b/thumb.php @@ -25,7 +25,7 @@ define( 'MW_NO_OUTPUT_COMPRESSION', 1 ); if ( isset( $_SERVER['MW_COMPILED'] ) ) { require( 'core/includes/WebStart.php' ); } else { - require( dirname( __FILE__ ) . '/includes/WebStart.php' ); + require( __DIR__ . '/includes/WebStart.php' ); } // Don't use fancy mime detection, just check the file extension for jpg/gif/png @@ -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; @@ -93,6 +95,7 @@ function wfThumbHandle404() { * @return void */ function wfStreamThumb( array $params ) { + global $wgVaryOnXFP; wfProfileIn( __METHOD__ ); $headers = array(); // HTTP headers to send @@ -126,13 +129,13 @@ function wfStreamThumb( array $params ) { // Format is ! $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 +147,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; } @@ -156,6 +159,7 @@ function wfStreamThumb( array $params ) { } // Check permissions if there are read restrictions + $varyHeader = array(); if ( !in_array( 'read', User::getGroupPermissions( array( '*' ) ), true ) ) { if ( !$img->getTitle() || !$img->getTitle()->userCan( 'read' ) ) { wfThumbError( 403, 'Access denied. You do not have permission to access ' . @@ -164,12 +168,12 @@ function wfStreamThumb( array $params ) { return; } $headers[] = 'Cache-Control: private'; - $headers[] = 'Vary: Cookie'; + $varyHeader[] = 'Cookie'; } // Check the source file storage path if ( !$img ) { - wfThumbError( 404, wfMsg( 'badtitletext' ) ); + wfThumbError( 404, wfMessage( 'badtitletext' )->text() ); wfProfileOut( __METHOD__ ); return; } @@ -202,33 +206,58 @@ function wfStreamThumb( array $params ) { } } + $thumbName = $img->thumbName( $params ); + if ( !strlen( $thumbName ) ) { // invalid params? + wfThumbError( 400, 'The specified thumbnail parameters are not valid.' ); + wfProfileOut( __METHOD__ ); + return; + } + + $disposition = $img->getThumbDisposition( $thumbName ); + $headers[] = "Content-Disposition: $disposition"; + // Stream the file if it exists already... try { - $thumbName = $img->thumbName( $params ); - if ( strlen( $thumbName ) ) { // valid params? - // For 404 handled thumbnails, we only use the 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). - if ( isset( $params['rel404'] ) // thumbnail was handled via 404 - && urldecode( $params['rel404'] ) !== $img->getThumbRel( $thumbName ) ) - { - wfThumbError( 404, 'The source file for the specified thumbnail does not exist.' ); + $thumbName2 = $img->thumbName( $params, File::THUMB_FULL_NAME ); // b/c; "long" style + // For 404 handled thumbnails, we only use the 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). + if ( isset( $params['rel404'] ) ) { // thumbnail was handled via 404 + if ( urldecode( $params['rel404'] ) === $img->getThumbRel( $thumbName ) ) { + // Request for the canonical thumbnail name + } elseif ( urldecode( $params['rel404'] ) === $img->getThumbRel( $thumbName2 ) ) { + // Request for the "long" thumbnail name; redirect to canonical name + $response = RequestContext::getMain()->getRequest()->response(); + $response->header( "HTTP/1.1 301 " . HttpStatus::getMessage( 301 ) ); + $response->header( 'Location: ' . wfExpandUrl( $img->getThumbUrl( $thumbName ), PROTO_CURRENT ) ); + $response->header( 'Expires: ' . + gmdate( 'D, d M Y H:i:s', time() + 7*86400 ) . ' GMT' ); + if ( $wgVaryOnXFP ) { + $varyHeader[] = 'X-Forwarded-Proto'; + } + $response->header( 'Vary: ' . implode( ', ', $varyHeader ) ); wfProfileOut( __METHOD__ ); return; - } - $thumbPath = $img->getThumbPath( $thumbName ); - if ( $img->getRepo()->fileExists( $thumbPath ) ) { - $img->getRepo()->streamFile( $thumbPath, $headers ); + } else { + wfThumbError( 404, 'The source file for the specified thumbnail does not exist.' ); wfProfileOut( __METHOD__ ); return; } } + $thumbPath = $img->getThumbPath( $thumbName ); + if ( $img->getRepo()->fileExists( $thumbPath ) ) { + $headers[] = 'Vary: ' . implode( ', ', $varyHeader ); + $img->getRepo()->streamFile( $thumbPath, $headers ); + wfProfileOut( __METHOD__ ); + return; + } } catch ( MWException $e ) { wfThumbError( 500, $e->getHTML() ); wfProfileOut( __METHOD__ ); return; } + $headers[] = 'Vary: ' . implode( ', ', $varyHeader ); // Thumbnail isn't already there, so create the new thumbnail... try { @@ -240,15 +269,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 +295,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 +318,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 );