X-Git-Url: http://git.heureux-cyclage.org/?a=blobdiff_plain;f=thumb.php;h=88aecbdbf9ed7643043174f36f4ecb3d12202d32;hb=c537bdca6dde2f47cf9a1fc2f84b8db3a394efcc;hp=913adc18ecb1140a6a9c79627af2e9e54c70a441;hpb=498693b9eaddaf59dfb2cba5020df4dfb5fffa56;p=lhc%2Fweb%2Fwiklou.git diff --git a/thumb.php b/thumb.php index 913adc18ec..88aecbdbf9 100644 --- a/thumb.php +++ b/thumb.php @@ -107,6 +107,15 @@ function wfStreamThumb( array $params ) { $fileName = isset( $params['f'] ) ? $params['f'] : ''; + // Backwards compatibility parameters + if ( isset( $params['w'] ) ) { + $params['width'] = $params['w']; + unset( $params['w'] ); + } + if ( isset( $params['p'] ) ) { + $params['page'] = $params['p']; + } + // Is this a thumb of an archived file? $isOld = ( isset( $params['archived'] ) && $params['archived'] ); unset( $params['archived'] ); // handlers don't care @@ -171,7 +180,6 @@ function wfStreamThumb( array $params ) { return; } - // Check the source file storage path if ( !$img->exists() ) { $redirectedLocation = false; @@ -235,18 +243,9 @@ function wfStreamThumb( array $params ) { } } - // Backwards compatibility parameters - if ( isset( $params['w'] ) ) { - $params['width'] = $params['w']; - unset( $params['w'] ); - } - if ( isset( $params['p'] ) ) { - $params['page'] = $params['p']; - } unset( $params['r'] ); // ignore 'r' because we unconditionally pass File::RENDER unset( $params['f'] ); // We're done with 'f' parameter. - // Get the normalized thumbnail name from the parameters... try { $thumbName = $img->thumbName( $params ); @@ -308,6 +307,9 @@ function wfStreamThumb( array $params ) { if ( $user->pingLimiter( 'renderfile' ) ) { wfThumbError( 500, wfMessage( 'actionthrottledtext' ) ); return; + } elseif ( wfThumbIsAttemptThrottled( $img, $thumbName, 5 ) ) { + wfThumbError( 500, wfMessage( 'thumbnail_image-failure-limit', 5 ) ); + return; } // Thumbnail isn't already there, so create the new thumbnail... @@ -333,6 +335,7 @@ function wfStreamThumb( array $params ) { } if ( $errorMsg !== false ) { + wfThumbIncrAttemptFailures( $img, $thumbName ); wfThumbError( 500, $errorMsg ); } else { // Stream the file if there were no errors @@ -340,6 +343,45 @@ function wfStreamThumb( array $params ) { } } +/** + * @param File $img + * @param string $thumbName + * @param int $limit + * @return int|bool + */ +function wfThumbIsAttemptThrottled( File $img, $thumbName, $limit ) { + global $wgMemc; + + return ( $wgMemc->get( wfThumbAttemptKey( $img, $thumbName ) ) >= $limit ); +} + +/** + * @param File $img + * @param string $thumbName + */ +function wfThumbIncrAttemptFailures( File $img, $thumbName ) { + global $wgMemc; + + $key = wfThumbAttemptKey( $img, $thumbName ); + if ( !$wgMemc->incr( $key, 1 ) ) { + if ( !$wgMemc->add( $key, 1, 3600 ) ) { + $wgMemc->incr( $key, 1 ); + } + } +} + +/** + * @param File $img + * @param string $thumbName + * @return string + */ +function wfThumbAttemptKey( File $img, $thumbName ) { + global $wgAttemptFailureEpoch; + + return wfMemcKey( 'attempt-failures', $wgAttemptFailureEpoch, + $img->getRepo()->getName(), md5( $img->getName() ), md5( $thumbName ) ); +} + /** * Convert pathinfo type parameter, into normal request parameters *