Index: trunk/phase3/includes/upload/UploadFromFile.php |
— | — | @@ -38,21 +38,14 @@ |
39 | 39 | # Check for a post_max_size or upload_max_size overflow, so that a |
40 | 40 | # proper error can be shown to the user |
41 | 41 | if ( is_null( $this->mTempPath ) || $this->isEmptyFile() ) { |
42 | | - # Using the Content-length header is not an absolutely fail-safe |
43 | | - # method, but the only available option. Otherwise broken uploads |
44 | | - # will be handled by the parent method and return empty-file |
45 | | - $contentLength = intval( $_SERVER['CONTENT_LENGTH'] ); |
46 | | - $maxPostSize = wfShorthandToInteger( ini_get( 'post_max_size' ) ); |
47 | | - if ( $this->mWebUpload->getError() == UPLOAD_ERR_INI_SIZE |
48 | | - || $contentLength > $maxPostSize ) { |
49 | | - |
| 42 | + if ( $this->mWebUpload->isIniSizeOverflow() ) { |
50 | 43 | global $wgMaxUploadSize; |
51 | 44 | return array( |
52 | 45 | 'status' => self::FILE_TOO_LARGE, |
53 | 46 | 'max' => min( |
54 | 47 | $wgMaxUploadSize, |
55 | 48 | wfShorthandToInteger( ini_get( 'upload_max_filesize' ) ), |
56 | | - $maxPostSize |
| 49 | + wfShorthandToInteger( ini_get( 'post_max_size' ) ) |
57 | 50 | ), |
58 | 51 | ); |
59 | 52 | } |
Index: trunk/phase3/includes/WebRequest.php |
— | — | @@ -590,7 +590,7 @@ |
591 | 591 | * @return string or NULL if no such file. |
592 | 592 | */ |
593 | 593 | public function getFileTempname( $key ) { |
594 | | - $file = new WebRequestUpload( $key ); |
| 594 | + $file = new WebRequestUpload( $this, $key ); |
595 | 595 | return $file->getTempName(); |
596 | 596 | } |
597 | 597 | |
— | — | @@ -602,7 +602,7 @@ |
603 | 603 | * @return integer |
604 | 604 | */ |
605 | 605 | public function getFileSize( $key ) { |
606 | | - $file = new WebRequestUpload( $key ); |
| 606 | + $file = new WebRequestUpload( $this, $key ); |
607 | 607 | return $file->getSize(); |
608 | 608 | } |
609 | 609 | |
— | — | @@ -613,7 +613,7 @@ |
614 | 614 | * @return integer |
615 | 615 | */ |
616 | 616 | public function getUploadError( $key ) { |
617 | | - $file = new WebRequestUpload( $key ); |
| 617 | + $file = new WebRequestUpload( $this, $key ); |
618 | 618 | return $file->getError(); |
619 | 619 | } |
620 | 620 | |
— | — | @@ -629,7 +629,7 @@ |
630 | 630 | * @return string or NULL if no such file. |
631 | 631 | */ |
632 | 632 | public function getFileName( $key ) { |
633 | | - $file = new WebRequestUpload( $key ); |
| 633 | + $file = new WebRequestUpload( $this, $key ); |
634 | 634 | return $file->getName(); |
635 | 635 | } |
636 | 636 | |
— | — | @@ -640,7 +640,7 @@ |
641 | 641 | * @return WebRequestUpload |
642 | 642 | */ |
643 | 643 | public function getUpload( $key ) { |
644 | | - return new WebRequestUpload( $key ); |
| 644 | + return new WebRequestUpload( $this, $key ); |
645 | 645 | } |
646 | 646 | |
647 | 647 | /** |
— | — | @@ -675,6 +675,9 @@ |
676 | 676 | } |
677 | 677 | } else { |
678 | 678 | $name = 'HTTP_' . str_replace( '-', '_', $name ); |
| 679 | + if ( $name === 'HTTP_CONTENT_LENGTH' && !isset( $_SERVER[$name] ) ) { |
| 680 | + $name = 'CONTENT_LENGTH'; |
| 681 | + } |
679 | 682 | if ( isset( $_SERVER[$name] ) ) { |
680 | 683 | return $_SERVER[$name]; |
681 | 684 | } else { |
— | — | @@ -768,15 +771,18 @@ |
769 | 772 | * Object to access the $_FILES array |
770 | 773 | */ |
771 | 774 | class WebRequestUpload { |
| 775 | + protected $request; |
772 | 776 | protected $doesExist; |
773 | 777 | protected $fileInfo; |
774 | 778 | |
775 | 779 | /** |
776 | 780 | * Constructor. Should only be called by WebRequest |
777 | 781 | * |
| 782 | + * @param $request WebRequest The associated request |
778 | 783 | * @param $key string Key in $_FILES array (name of form field) |
779 | 784 | */ |
780 | | - public function __construct( $key ) { |
| 785 | + public function __construct( $request, $key ) { |
| 786 | + $this->request = $request; |
781 | 787 | $this->doesExist = isset( $_FILES[$key] ); |
782 | 788 | if ( $this->doesExist ) { |
783 | 789 | $this->fileInfo = $_FILES[$key]; |
— | — | @@ -852,6 +858,27 @@ |
853 | 859 | |
854 | 860 | return $this->fileInfo['error']; |
855 | 861 | } |
| 862 | + |
| 863 | + /** |
| 864 | + * Returns whether this upload failed because of overflow of a maximum set |
| 865 | + * in php.ini |
| 866 | + * |
| 867 | + * @return bool |
| 868 | + */ |
| 869 | + public function isIniSizeOverflow() { |
| 870 | + if ( $this->getError() == UPLOAD_ERR_INI_SIZE ) { |
| 871 | + # PHP indicated that upload_max_filesize is exceeded |
| 872 | + return true; |
| 873 | + } |
| 874 | + |
| 875 | + $contentLength = $this->request->getHeader( 'CONTENT_LENGTH' ); |
| 876 | + if ( $contentLength > wfShorthandToInteger( ini_get( 'post_max_size' ) ) ) { |
| 877 | + # post_max_size is exceeded |
| 878 | + return true; |
| 879 | + } |
| 880 | + |
| 881 | + return false; |
| 882 | + } |
856 | 883 | } |
857 | 884 | |
858 | 885 | /** |