/phpQuery/phpQuery/Zend/Validate/File/Size.php

https://github.com/rduenasf/ucursos-scrapper · PHP · 308 lines · 161 code · 27 blank · 120 comment · 24 complexity · aa50b3cb733fcceff95ff192e894b202 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_Size extends Zend_Validate_Abstract
  34. {
  35. /**#@+
  36. * @const string Error constants
  37. */
  38. const TOO_BIG = 'fileSizeTooBig';
  39. const TOO_SMALL = 'fileSizeTooSmall';
  40. const NOT_FOUND = 'fileSizeNotFound';
  41. /**#@-*/
  42. /**
  43. * @var array Error message templates
  44. */
  45. protected $_messageTemplates = array(
  46. self::TOO_BIG => "The file '%value%' is bigger than allowed",
  47. self::TOO_SMALL => "The file '%value%' is smaller than allowed",
  48. self::NOT_FOUND => "The file '%value%' could not be found"
  49. );
  50. /**
  51. * @var array Error message template variables
  52. */
  53. protected $_messageVariables = array(
  54. 'min' => '_min',
  55. 'max' => '_max'
  56. );
  57. /**
  58. * Minimum filesize
  59. * @var integer
  60. */
  61. protected $_min;
  62. /**
  63. * Maximum filesize
  64. *
  65. * If null, there is no maximum filesize
  66. *
  67. * @var integer|null
  68. */
  69. protected $_max;
  70. /**
  71. * Sets validator options
  72. *
  73. * Min limits the filesize, when used with max=null it is the maximum filesize
  74. * It also accepts an array with the keys 'min' and 'max'
  75. *
  76. * @param integer|array $min Minimum filesize
  77. * @param integer $max Maximum filesize
  78. * @return void
  79. */
  80. public function __construct($min, $max = null)
  81. {
  82. if (is_array($min)) {
  83. $count = count($min);
  84. if (array_key_exists('min', $min)) {
  85. if (array_key_exists('max', $min)) {
  86. $max = $min['max'];
  87. }
  88. $min = $min['min'];
  89. } elseif ($count === 2) {
  90. $minValue = array_shift($min);
  91. $max = array_shift($min);
  92. $min = $minValue;
  93. } elseif($count === 1) {
  94. $min = array_shift($min);
  95. $max = null;
  96. } else {
  97. $min = 0;
  98. $max = null;
  99. }
  100. }
  101. if (empty($max)) {
  102. $max = $min;
  103. $min = 0;
  104. }
  105. $this->setMin($min);
  106. $this->setMax($max);
  107. }
  108. /**
  109. * Returns the minimum filesize
  110. *
  111. * @param boolean $unit Return the value with unit, when false the plan bytes will be returned
  112. * @return integer
  113. */
  114. public function getMin($unit = true)
  115. {
  116. $unit = (bool) $unit;
  117. $min = $this->_min;
  118. if ($unit) {
  119. $min = $this->_toByteString($min);
  120. }
  121. return $min;
  122. }
  123. /**
  124. * Sets the minimum filesize
  125. *
  126. * @param integer $min The minimum filesize
  127. * @return Zend_Validate_File_Size Provides a fluent interface
  128. * @throws Zend_Validate_Exception When min is greater than max
  129. */
  130. public function setMin($min)
  131. {
  132. $min = (integer) $this->_fromByteString($min);
  133. if (($this->_max !== null) && ($min > $this->_max)) {
  134. require_once 'Zend/Validate/Exception.php';
  135. throw new Zend_Validate_Exception("The minimum must be less than or equal to the maximum filesize, but $min >"
  136. . " {$this->_max}");
  137. }
  138. $this->_min = max(0, $min);
  139. return $this;
  140. }
  141. /**
  142. * Returns the maximum filesize
  143. *
  144. * @param boolean $unit Return the value with unit, when false the plan bytes will be returned
  145. * @return integer|null
  146. */
  147. public function getMax($unit = true)
  148. {
  149. $unit = (bool) $unit;
  150. $max = $this->_max;
  151. if ($unit) {
  152. $max = $this->_toByteString($max);
  153. }
  154. return $max;
  155. }
  156. /**
  157. * Sets the maximum filesize
  158. *
  159. * @param integer|null $max The maximum filesize
  160. * @return Zend_Validate_StringLength Provides a fluent interface
  161. * @throws Zend_Validate_Exception When max is smaller than min
  162. */
  163. public function setMax($max)
  164. {
  165. $max = (integer) $this->_fromByteString($max);
  166. if (($this->_min !== null) && ($max < $this->_min)) {
  167. require_once 'Zend/Validate/Exception.php';
  168. throw new Zend_Validate_Exception("The maximum must be greater than or equal to the minimum filesize, but "
  169. . "$max < {$this->_min}");
  170. } else {
  171. $this->_max = $max;
  172. }
  173. return $this;
  174. }
  175. /**
  176. * Defined by Zend_Validate_Interface
  177. *
  178. * Returns true if and only if the filesize of $value is at least min and
  179. * not bigger than max (when max is not null).
  180. *
  181. * @param string $value Real file to check for size
  182. * @param array $file File data from Zend_File_Transfer
  183. * @return boolean
  184. */
  185. public function isValid($value, $file = null)
  186. {
  187. // Is file readable ?
  188. if (!@is_readable($value)) {
  189. $this->_throw($file, self::NOT_FOUND);
  190. return false;
  191. }
  192. // limited to 4GB files
  193. $size = sprintf("%u",@filesize($value));
  194. $this->_setValue($size);
  195. // Check to see if it's smaller than min size
  196. if (($this->_min !== null) && ($size < $this->_min)) {
  197. $this->_throw($file, self::TOO_SMALL);
  198. }
  199. // Check to see if it's larger than max size
  200. if (($this->_max !== null) && ($this->_max < $size)) {
  201. $this->_throw($file, self::TOO_BIG);
  202. }
  203. if (count($this->_messages) > 0) {
  204. return false;
  205. } else {
  206. return true;
  207. }
  208. }
  209. /**
  210. * Returns the formatted size
  211. *
  212. * @param integer $size
  213. * @return string
  214. */
  215. protected function _toByteString($size)
  216. {
  217. $sizes = array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
  218. for ($i=0; $size > 1024 && $i < 9; $i++) {
  219. $size /= 1024;
  220. }
  221. return round($size, 2).$sizes[$i];
  222. }
  223. /**
  224. * Returns the unformatted size
  225. *
  226. * @param string $size
  227. * @return integer
  228. */
  229. protected function _fromByteString($size)
  230. {
  231. if (is_numeric($size)) {
  232. return (integer) $size;
  233. }
  234. $type = trim(substr($size, -2));
  235. $value = substr($size, 0, -2);
  236. switch (strtoupper($type)) {
  237. case 'YB':
  238. $value *= (1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024);
  239. break;
  240. case 'ZB':
  241. $value *= (1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024);
  242. break;
  243. case 'EB':
  244. $value *= (1024 * 1024 * 1024 * 1024 * 1024 * 1024);
  245. break;
  246. case 'PB':
  247. $value *= (1024 * 1024 * 1024 * 1024 * 1024);
  248. break;
  249. case 'TB':
  250. $value *= (1024 * 1024 * 1024 * 1024);
  251. break;
  252. case 'GB':
  253. $value *= (1024 * 1024 * 1024);
  254. break;
  255. case 'MB':
  256. $value *= (1024 * 1024);
  257. break;
  258. case 'KB':
  259. $value *= 1024;
  260. break;
  261. default:
  262. break;
  263. }
  264. return $value;
  265. }
  266. /**
  267. * Throws an error of the given type
  268. *
  269. * @param string $file
  270. * @param string $errorType
  271. * @return false
  272. */
  273. protected function _throw($file, $errorType)
  274. {
  275. if ($file !== null) {
  276. $this->_value = $file['name'];
  277. }
  278. $this->_error($errorType);
  279. return false;
  280. }
  281. }