Upload.php 7.5 KB

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