/Zend/Validate/File/Upload.php

https://github.com/ftaiolivista/Zend-Framework-Namespaced- · PHP · 251 lines · 207 code · 4 blank · 40 comment · 1 complexity · 017aa43ba28a1d153ea5d4f1d90d00aa MD5 · raw file

  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Validate
  17. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id: Upload.php 22398 2010-06-09 19:05:46Z thomas $
  20. */
  21. /**
  22. * @namespace
  23. */
  24. namespace Zend\Validate\File;
  25. /**
  26. * @see Zend_Validate_Abstract
  27. */
  28. require_once 'Zend/Validate/Abstract.php';
  29. /**
  30. * Validator for the maximum size of a file up to a max of 2GB
  31. *
  32. * @category Zend
  33. * @package Zend_Validate
  34. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. */
  37. class Upload extends \Zend\Validate\AbstractValidate
  38. {
  39. /**@#+
  40. * @const string Error constants
  41. */
  42. const INI_SIZE = 'fileUploadErrorIniSize';
  43. const FORM_SIZE = 'fileUploadErrorFormSize';
  44. const PARTIAL = 'fileUploadErrorPartial';
  45. const NO_FILE = 'fileUploadErrorNoFile';
  46. const NO_TMP_DIR = 'fileUploadErrorNoTmpDir';
  47. const CANT_WRITE = 'fileUploadErrorCantWrite';
  48. const EXTENSION = 'fileUploadErrorExtension';
  49. const ATTACK = 'fileUploadErrorAttack';
  50. const FILE_NOT_FOUND = 'fileUploadErrorFileNotFound';
  51. const UNKNOWN = 'fileUploadErrorUnknown';
  52. /**@#-*/
  53. /**
  54. * @var array Error message templates
  55. */
  56. protected $_messageTemplates = array(
  57. self::INI_SIZE => "File '%value%' exceeds the defined ini size",
  58. self::FORM_SIZE => "File '%value%' exceeds the defined form size",
  59. self::PARTIAL => "File '%value%' was only partially uploaded",
  60. self::NO_FILE => "File '%value%' was not uploaded",
  61. self::NO_TMP_DIR => "No temporary directory was found for file '%value%'",
  62. self::CANT_WRITE => "File '%value%' can't be written",
  63. self::EXTENSION => "A PHP extension returned an error while uploading the file '%value%'",
  64. self::ATTACK => "File '%value%' was illegally uploaded. This could be a possible attack",
  65. self::FILE_NOT_FOUND => "File '%value%' was not found",
  66. self::UNKNOWN => "Unknown error while uploading file '%value%'"
  67. );
  68. /**
  69. * Internal array of files
  70. * @var array
  71. */
  72. protected $_files = array();
  73. /**
  74. * Sets validator options
  75. *
  76. * The array $files must be given in syntax of Zend_File_Transfer to be checked
  77. * If no files are given the $_FILES array will be used automatically.
  78. * NOTE: This validator will only work with HTTP POST uploads!
  79. *
  80. * @param array|Zend_Config $files Array of files in syntax of Zend_File_Transfer
  81. * @return void
  82. */
  83. public function __construct($files = array())
  84. {
  85. if ($files instanceof \Zend\Config\Config) {
  86. $files = $files->toArray();
  87. }
  88. $this->setFiles($files);
  89. }
  90. /**
  91. * Returns the array of set files
  92. *
  93. * @param string $files (Optional) The file to return in detail
  94. * @return array
  95. * @throws Zend_Validate_Exception If file is not found
  96. */
  97. public function getFiles($file = null)
  98. {
  99. if ($file !== null) {
  100. $return = array();
  101. foreach ($this->_files as $name => $content) {
  102. if ($name === $file) {
  103. $return[$file] = $this->_files[$name];
  104. }
  105. if ($content['name'] === $file) {
  106. $return[$name] = $this->_files[$name];
  107. }
  108. }
  109. if (count($return) === 0) {
  110. require_once 'Zend/Validate/Exception.php';
  111. throw new \Zend\Validate\Exception("The file '$file' was not found");
  112. }
  113. return $return;
  114. }
  115. return $this->_files;
  116. }
  117. /**
  118. * Sets the files to be checked
  119. *
  120. * @param array $files The files to check in syntax of Zend_File_Transfer
  121. * @return Zend_Validate_File_Upload Provides a fluent interface
  122. */
  123. public function setFiles($files = array())
  124. {
  125. if (count($files) === 0) {
  126. $this->_files = $_FILES;
  127. } else {
  128. $this->_files = $files;
  129. }
  130. foreach($this->_files as $file => $content) {
  131. if (!isset($content['error'])) {
  132. unset($this->_files[$file]);
  133. }
  134. }
  135. return $this;
  136. }
  137. /**
  138. * Defined by Zend_Validate_Interface
  139. *
  140. * Returns true if and only if the file was uploaded without errors
  141. *
  142. * @param string $value Single file to check for upload errors, when giving null the $_FILES array
  143. * from initialization will be used
  144. * @return boolean
  145. */
  146. public function isValid($value, $file = null)
  147. {
  148. $this->_messages = null;
  149. if (array_key_exists($value, $this->_files)) {
  150. $files[$value] = $this->_files[$value];
  151. } else {
  152. foreach ($this->_files as $file => $content) {
  153. if (isset($content['name']) && ($content['name'] === $value)) {
  154. $files[$file] = $this->_files[$file];
  155. }
  156. if (isset($content['tmp_name']) && ($content['tmp_name'] === $value)) {
  157. $files[$file] = $this->_files[$file];
  158. }
  159. }
  160. }
  161. if (empty($files)) {
  162. return $this->_throw($file, self::FILE_NOT_FOUND);
  163. }
  164. foreach ($files as $file => $content) {
  165. $this->_value = $file;
  166. switch($content['error']) {
  167. case 0:
  168. if (!is_uploaded_file($content['tmp_name'])) {
  169. $this->_throw($file, self::ATTACK);
  170. }
  171. break;
  172. case 1:
  173. $this->_throw($file, self::INI_SIZE);
  174. break;
  175. case 2:
  176. $this->_throw($file, self::FORM_SIZE);
  177. break;
  178. case 3:
  179. $this->_throw($file, self::PARTIAL);
  180. break;
  181. case 4:
  182. $this->_throw($file, self::NO_FILE);
  183. break;
  184. case 6:
  185. $this->_throw($file, self::NO_TMP_DIR);
  186. break;
  187. case 7:
  188. $this->_throw($file, self::CANT_WRITE);
  189. break;
  190. case 8:
  191. $this->_throw($file, self::EXTENSION);
  192. break;
  193. default:
  194. $this->_throw($file, self::UNKNOWN);
  195. break;
  196. }
  197. }
  198. if (count($this->_messages) > 0) {
  199. return false;
  200. } else {
  201. return true;
  202. }
  203. }
  204. /**
  205. * Throws an error of the given type
  206. *
  207. * @param string $file
  208. * @param string $errorType
  209. * @return false
  210. */
  211. protected function _throw($file, $errorType)
  212. {
  213. if ($file !== null) {
  214. if (is_array($file) and !empty($file['name'])) {
  215. $this->_value = $file['name'];
  216. }
  217. }
  218. $this->_error($errorType);
  219. return false;
  220. }
  221. }