Follow-up r70037: Move isIniSizeOverflow magic to WebRequestUpload
authorBryan Tong Minh <btongminh@users.mediawiki.org>
Tue, 27 Jul 2010 20:54:34 +0000 (20:54 +0000)
committerBryan Tong Minh <btongminh@users.mediawiki.org>
Tue, 27 Jul 2010 20:54:34 +0000 (20:54 +0000)
includes/WebRequest.php
includes/upload/UploadFromFile.php

index 7ef1060..68a5cac 100644 (file)
@@ -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;
+       }
 }
 
 /**
index 7ab2e6e..b653a47 100644 (file)
@@ -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' ) )
                                        ),
                                );
                        }