From 8ca6cc32b2c789a3149861159ad258f2cb9491e3 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 28 Apr 2024 14:39:39 +0200 Subject: Adding upstream version 2.11.4. Signed-off-by: Daniel Baumann --- library/vendor/Zend/Validate/File/Upload.php | 249 +++++++++++++++++++++++++++ 1 file changed, 249 insertions(+) create mode 100644 library/vendor/Zend/Validate/File/Upload.php (limited to 'library/vendor/Zend/Validate/File/Upload.php') diff --git a/library/vendor/Zend/Validate/File/Upload.php b/library/vendor/Zend/Validate/File/Upload.php new file mode 100644 index 0000000..b86c1c7 --- /dev/null +++ b/library/vendor/Zend/Validate/File/Upload.php @@ -0,0 +1,249 @@ + "File '%value%' exceeds the defined ini size", + self::FORM_SIZE => "File '%value%' exceeds the defined form size", + self::PARTIAL => "File '%value%' was only partially uploaded", + self::NO_FILE => "File '%value%' was not uploaded", + self::NO_TMP_DIR => "No temporary directory was found for file '%value%'", + self::CANT_WRITE => "File '%value%' can't be written", + self::EXTENSION => "A PHP extension returned an error while uploading the file '%value%'", + self::ATTACK => "File '%value%' was illegally uploaded. This could be a possible attack", + self::FILE_NOT_FOUND => "File '%value%' was not found", + self::UNKNOWN => "Unknown error while uploading file '%value%'" + ); + + /** + * Internal array of files + * @var array + */ + protected $_files = array(); + + /** + * Sets validator options + * + * The array $files must be given in syntax of Zend_File_Transfer to be checked + * If no files are given the $_FILES array will be used automatically. + * NOTE: This validator will only work with HTTP POST uploads! + * + * @param array|Zend_Config $files Array of files in syntax of Zend_File_Transfer + */ + public function __construct($files = array()) + { + if ($files instanceof Zend_Config) { + $files = $files->toArray(); + } + + $this->setFiles($files); + } + + /** + * Returns the array of set files + * + * @param string $file (Optional) The file to return in detail + * @return array + * @throws Zend_Validate_Exception If file is not found + */ + public function getFiles($file = null) + { + if ($file !== null) { + $return = array(); + foreach ($this->_files as $name => $content) { + if ($name === $file) { + $return[$file] = $this->_files[$name]; + } + + if ($content['name'] === $file) { + $return[$name] = $this->_files[$name]; + } + } + + if (count($return) === 0) { + throw new Zend_Validate_Exception("The file '$file' was not found"); + } + + return $return; + } + + return $this->_files; + } + + /** + * Sets the files to be checked + * + * @param array $files The files to check in syntax of Zend_File_Transfer + * @return Zend_Validate_File_Upload Provides a fluent interface + */ + public function setFiles($files = array()) + { + if (count($files) === 0) { + $this->_files = $_FILES; + } else { + $this->_files = $files; + } + + // see ZF-10738 + if (is_null($this->_files)) { + $this->_files = array(); + } + + foreach($this->_files as $file => $content) { + if (!isset($content['error'])) { + unset($this->_files[$file]); + } + } + + return $this; + } + + /** + * Defined by Zend_Validate_Interface + * + * Returns true if and only if the file was uploaded without errors + * + * @param string $value Single file to check for upload errors, when giving null the $_FILES array + * from initialization will be used + * @param string|null $file + * @return boolean + */ + public function isValid($value, $file = null) + { + $this->_messages = []; + if (array_key_exists($value, $this->_files)) { + $files[$value] = $this->_files[$value]; + } else { + foreach ($this->_files as $file => $content) { + if (isset($content['name']) && ($content['name'] === $value)) { + $files[$file] = $this->_files[$file]; + } + + if (isset($content['tmp_name']) && ($content['tmp_name'] === $value)) { + $files[$file] = $this->_files[$file]; + } + } + } + + if (empty($files)) { + return $this->_throw($file, self::FILE_NOT_FOUND); + } + + foreach ($files as $file => $content) { + $this->_value = $file; + switch($content['error']) { + case 0: + if (!is_uploaded_file($content['tmp_name'])) { + $this->_throw($content, self::ATTACK); + } + break; + + case 1: + $this->_throw($content, self::INI_SIZE); + break; + + case 2: + $this->_throw($content, self::FORM_SIZE); + break; + + case 3: + $this->_throw($content, self::PARTIAL); + break; + + case 4: + $this->_throw($content, self::NO_FILE); + break; + + case 6: + $this->_throw($content, self::NO_TMP_DIR); + break; + + case 7: + $this->_throw($content, self::CANT_WRITE); + break; + + case 8: + $this->_throw($content, self::EXTENSION); + break; + + default: + $this->_throw($content, self::UNKNOWN); + break; + } + } + + if (count($this->_messages) > 0) { + return false; + } else { + return true; + } + } + + /** + * Throws an error of the given type + * + * @param string $file + * @param string $errorType + * @return false + */ + protected function _throw($file, $errorType) + { + if ($file !== null) { + if (is_array($file) and !empty($file['name'])) { + $this->_value = $file['name']; + } + } + + $this->_error($errorType); + return false; + } +} -- cgit v1.2.3