WebRequestUpload.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /**
  3. * Object to access the $_FILES array
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. */
  22. use MediaWiki\MediaWikiServices;
  23. /**
  24. * Object to access the $_FILES array
  25. *
  26. * @ingroup HTTP
  27. */
  28. class WebRequestUpload {
  29. protected $request;
  30. protected $doesExist;
  31. protected $fileInfo;
  32. /**
  33. * Constructor. Should only be called by WebRequest
  34. *
  35. * @param WebRequest $request The associated request
  36. * @param string $key Key in $_FILES array (name of form field)
  37. */
  38. public function __construct( $request, $key ) {
  39. $this->request = $request;
  40. $this->doesExist = isset( $_FILES[$key] );
  41. if ( $this->doesExist ) {
  42. $this->fileInfo = $_FILES[$key];
  43. }
  44. }
  45. /**
  46. * Return whether a file with this name was uploaded.
  47. *
  48. * @return bool
  49. */
  50. public function exists() {
  51. return $this->doesExist;
  52. }
  53. /**
  54. * Return the original filename of the uploaded file
  55. *
  56. * @return string|null Filename or null if non-existent
  57. */
  58. public function getName() {
  59. if ( !$this->exists() ) {
  60. return null;
  61. }
  62. $name = $this->fileInfo['name'];
  63. # Safari sends filenames in HTML-encoded Unicode form D...
  64. # Horrid and evil! Let's try to make some kind of sense of it.
  65. $name = Sanitizer::decodeCharReferences( $name );
  66. $name = MediaWikiServices::getInstance()->getContentLanguage()->normalize( $name );
  67. wfDebug( __METHOD__ . ": {$this->fileInfo['name']} normalized to '$name'\n" );
  68. return $name;
  69. }
  70. /**
  71. * Return the file size of the uploaded file
  72. *
  73. * @return int File size or zero if non-existent
  74. */
  75. public function getSize() {
  76. if ( !$this->exists() ) {
  77. return 0;
  78. }
  79. return $this->fileInfo['size'];
  80. }
  81. /**
  82. * Return the path to the temporary file
  83. *
  84. * @return string|null Path or null if non-existent
  85. */
  86. public function getTempName() {
  87. if ( !$this->exists() ) {
  88. return null;
  89. }
  90. return $this->fileInfo['tmp_name'];
  91. }
  92. /**
  93. * Return the upload error. See link for explanation
  94. * https://www.php.net/manual/en/features.file-upload.errors.php
  95. *
  96. * @return int One of the UPLOAD_ constants, 0 if non-existent
  97. */
  98. public function getError() {
  99. if ( !$this->exists() ) {
  100. return 0; # UPLOAD_ERR_OK
  101. }
  102. return $this->fileInfo['error'];
  103. }
  104. /**
  105. * Returns whether this upload failed because of overflow of a maximum set
  106. * in php.ini
  107. *
  108. * @return bool
  109. */
  110. public function isIniSizeOverflow() {
  111. if ( $this->getError() == UPLOAD_ERR_INI_SIZE ) {
  112. # PHP indicated that upload_max_filesize is exceeded
  113. return true;
  114. }
  115. $contentLength = $this->request->getHeader( 'Content-Length' );
  116. $maxPostSize = wfShorthandToInteger(
  117. ini_get( 'post_max_size' ) ?: ini_get( 'hhvm.server.max_post_size' ),
  118. 0
  119. );
  120. if ( $maxPostSize && $contentLength > $maxPostSize ) {
  121. # post_max_size is exceeded
  122. return true;
  123. }
  124. return false;
  125. }
  126. }