From: Bryan Tong Minh Date: Tue, 27 Jul 2010 20:54:34 +0000 (+0000) Subject: Follow-up r70037: Move isIniSizeOverflow magic to WebRequestUpload X-Git-Tag: 1.31.0-rc.0~35874 X-Git-Url: http://git.cyclocoop.org/%22%2C%20generer_url_ecrire%28?a=commitdiff_plain;h=b62d5dbad7a5640402443f7530c2f5ac6cb6125d;p=lhc%2Fweb%2Fwiklou.git Follow-up r70037: Move isIniSizeOverflow magic to WebRequestUpload --- diff --git a/includes/WebRequest.php b/includes/WebRequest.php index 7ef1060c0e..68a5cacf6f 100644 --- a/includes/WebRequest.php +++ b/includes/WebRequest.php @@ -590,7 +590,7 @@ class WebRequest { * @return string or NULL if no such file. */ public function getFileTempname( $key ) { - $file = new WebRequestUpload( $key ); + $file = new WebRequestUpload( $this, $key ); return $file->getTempName(); } @@ -602,7 +602,7 @@ class WebRequest { * @return integer */ public function getFileSize( $key ) { - $file = new WebRequestUpload( $key ); + $file = new WebRequestUpload( $this, $key ); return $file->getSize(); } @@ -613,7 +613,7 @@ class WebRequest { * @return integer */ public function getUploadError( $key ) { - $file = new WebRequestUpload( $key ); + $file = new WebRequestUpload( $this, $key ); return $file->getError(); } @@ -629,7 +629,7 @@ class WebRequest { * @return string or NULL if no such file. */ public function getFileName( $key ) { - $file = new WebRequestUpload( $key ); + $file = new WebRequestUpload( $this, $key ); return $file->getName(); } @@ -640,7 +640,7 @@ class WebRequest { * @return WebRequestUpload */ public function getUpload( $key ) { - return new WebRequestUpload( $key ); + return new WebRequestUpload( $this, $key ); } /** @@ -675,6 +675,9 @@ class WebRequest { } } else { $name = 'HTTP_' . str_replace( '-', '_', $name ); + if ( $name === 'HTTP_CONTENT_LENGTH' && !isset( $_SERVER[$name] ) ) { + $name = 'CONTENT_LENGTH'; + } if ( isset( $_SERVER[$name] ) ) { return $_SERVER[$name]; } else { @@ -768,15 +771,18 @@ class WebRequest { * Object to access the $_FILES array */ class WebRequestUpload { + protected $request; protected $doesExist; protected $fileInfo; /** * Constructor. Should only be called by WebRequest * + * @param $request WebRequest The associated request * @param $key string Key in $_FILES array (name of form field) */ - public function __construct( $key ) { + public function __construct( $request, $key ) { + $this->request = $request; $this->doesExist = isset( $_FILES[$key] ); if ( $this->doesExist ) { $this->fileInfo = $_FILES[$key]; @@ -852,6 +858,27 @@ class WebRequestUpload { return $this->fileInfo['error']; } + + /** + * Returns whether this upload failed because of overflow of a maximum set + * in php.ini + * + * @return bool + */ + public function isIniSizeOverflow() { + if ( $this->getError() == UPLOAD_ERR_INI_SIZE ) { + # PHP indicated that upload_max_filesize is exceeded + return true; + } + + $contentLength = $this->request->getHeader( 'CONTENT_LENGTH' ); + if ( $contentLength > wfShorthandToInteger( ini_get( 'post_max_size' ) ) ) { + # post_max_size is exceeded + return true; + } + + return false; + } } /** diff --git a/includes/upload/UploadFromFile.php b/includes/upload/UploadFromFile.php index 7ab2e6efcc..b653a47eb7 100644 --- a/includes/upload/UploadFromFile.php +++ b/includes/upload/UploadFromFile.php @@ -38,21 +38,14 @@ class UploadFromFile extends UploadBase { # Check for a post_max_size or upload_max_size overflow, so that a # proper error can be shown to the user if ( is_null( $this->mTempPath ) || $this->isEmptyFile() ) { - # Using the Content-length header is not an absolutely fail-safe - # method, but the only available option. Otherwise broken uploads - # will be handled by the parent method and return empty-file - $contentLength = intval( $_SERVER['CONTENT_LENGTH'] ); - $maxPostSize = wfShorthandToInteger( ini_get( 'post_max_size' ) ); - if ( $this->mWebUpload->getError() == UPLOAD_ERR_INI_SIZE - || $contentLength > $maxPostSize ) { - + if ( $this->mWebUpload->isIniSizeOverflow() ) { global $wgMaxUploadSize; return array( 'status' => self::FILE_TOO_LARGE, 'max' => min( $wgMaxUploadSize, wfShorthandToInteger( ini_get( 'upload_max_filesize' ) ), - $maxPostSize + wfShorthandToInteger( ini_get( 'post_max_size' ) ) ), ); }