/phpQuery/phpQuery/Zend/Validate/File/Upload.php

https://github.com/rduenasf/ucursos-scrapper · PHP · 216 lines · 176 code · 3 blank · 37 comment · 1 complexity · b41e4a3abed1aefed5d75b3d774d0703 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-2008 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-2008 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 => "The file '%value%' exceeds the defined ini size",
  54. self::FORM_SIZE => "The file '%value%' exceeds the defined form size",
  55. self::PARTIAL => "The file '%value%' was only partially uploaded",
  56. self::NO_FILE => "The file '%value%' was not uploaded",
  57. self::NO_TMP_DIR => "No temporary directory was found for the file '%value%'",
  58. self::CANT_WRITE => "The file '%value%' can't be written",
  59. self::EXTENSION => "The extension returned an error while uploading the file '%value%'",
  60. self::ATTACK => "The file '%value%' was illegal uploaded, possible attack",
  61. self::FILE_NOT_FOUND => "The file '%value%' was not found",
  62. self::UNKNOWN => "Unknown error while uploading the 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 $files Array of files in syntax of Zend_File_Transfer
  77. * @return void
  78. */
  79. public function __construct($files = array())
  80. {
  81. $this->setFiles($files);
  82. }
  83. /**
  84. * Returns the array of set files
  85. *
  86. * @param string $files (Optional) The file to return in detail
  87. * @return array
  88. * @throws Zend_Validate_Exception If file is not found
  89. */
  90. public function getFiles($file = null)
  91. {
  92. if ($file !== null) {
  93. $return = array();
  94. foreach ($this->_files as $name => $content) {
  95. if ($name === $file) {
  96. $return[$file] = $this->_files[$name];
  97. }
  98. if ($content['name'] === $file) {
  99. $return[$name] = $this->_files[$name];
  100. }
  101. }
  102. if (count($return) === 0) {
  103. require_once 'Zend/Validate/Exception.php';
  104. throw new Zend_Validate_Exception("The file '$file' was not found");
  105. }
  106. return $return;
  107. }
  108. return $this->_files;
  109. }
  110. /**
  111. * Sets the minimum filesize
  112. *
  113. * @param array $files The files to check in syntax of Zend_File_Transfer
  114. * @return Zend_Validate_File_Upload Provides a fluent interface
  115. */
  116. public function setFiles($files = array())
  117. {
  118. if (count($files) === 0) {
  119. $this->_files = $_FILES;
  120. } else {
  121. $this->_files = $files;
  122. }
  123. return $this;
  124. }
  125. /**
  126. * Defined by Zend_Validate_Interface
  127. *
  128. * Returns true if and only if the file was uploaded without errors
  129. *
  130. * @param string $value Single file to check for upload errors, when giving null the $_FILES array
  131. * from initialization will be used
  132. * @return boolean
  133. */
  134. public function isValid($value)
  135. {
  136. if (array_key_exists($value, $this->_files)) {
  137. $files[$value] = $this->_files[$value];
  138. } else {
  139. foreach ($this->_files as $file => $content) {
  140. if ($content['name'] === $value) {
  141. $files[$file] = $this->_files[$file];
  142. }
  143. if ($content['tmp_name'] === $value) {
  144. $files[$file] = $this->_files[$file];
  145. }
  146. }
  147. }
  148. if (empty($files)) {
  149. $this->_error(self::FILE_NOT_FOUND);
  150. return false;
  151. }
  152. foreach ($files as $file => $content) {
  153. $this->_value = $file;
  154. switch($content['error']) {
  155. case 0:
  156. if (!is_uploaded_file($content['tmp_name'])) {
  157. $this->_error(self::ATTACK);
  158. }
  159. break;
  160. case 1:
  161. $this->_error(self::INI_SIZE);
  162. break;
  163. case 2:
  164. $this->_error(self::FORM_SIZE);
  165. break;
  166. case 3:
  167. $this->_error(self::PARTIAL);
  168. break;
  169. case 4:
  170. $this->_error(self::NO_FILE);
  171. break;
  172. case 6:
  173. $this->_error(self::NO_TMP_DIR);
  174. break;
  175. case 7:
  176. $this->_error(self::CANT_WRITE);
  177. break;
  178. case 8:
  179. $this->_error(self::EXTENSION);
  180. break;
  181. default:
  182. $this->_error(self::UNKNOWN);
  183. break;
  184. }
  185. }
  186. if (count($this->_messages) > 0) {
  187. return false;
  188. } else {
  189. return true;
  190. }
  191. }
  192. }