/escolaonline/library/Zend/Validate/File/Size.php

https://github.com/uester/escolaonline · PHP · 372 lines · 197 code · 33 blank · 142 comment · 31 complexity · 359cf228d2ef944474b1c68e60cd9897 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 => "Maximum allowed size for file '%value%' is '%max%' but '%size%' detected",
  47. self::TOO_SMALL => "Minimum expected size for file '%value%' is '%min%' but '%size%' detected",
  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. 'size' => '_size',
  57. );
  58. /**
  59. * Minimum filesize
  60. * @var integer
  61. */
  62. protected $_min;
  63. /**
  64. * Maximum filesize
  65. *
  66. * If null, there is no maximum filesize
  67. *
  68. * @var integer|null
  69. */
  70. protected $_max;
  71. /**
  72. * Detected size
  73. *
  74. * @var integer
  75. */
  76. protected $_size;
  77. /**
  78. * Use bytestring ?
  79. *
  80. * @var boolean
  81. */
  82. protected $_bytestr;
  83. /**
  84. * Sets validator options
  85. *
  86. * Min limits the filesize, when used with max=null it is the maximum filesize
  87. * It also accepts an array with the keys 'min' and 'max'
  88. *
  89. * @param integer|array $min Minimum filesize
  90. * @param integer $max Maximum filesize
  91. * @param boolean $bytestring Use bytestring or real size
  92. * @return void
  93. */
  94. public function __construct($min, $max = null, $bytestring = true)
  95. {
  96. if (is_array($min)) {
  97. $count = count($min);
  98. if (array_key_exists('min', $min)) {
  99. if (array_key_exists('bytestring', $min)) {
  100. $bytestring = $min['bytestring'];
  101. }
  102. if (array_key_exists('max', $min)) {
  103. $max = $min['max'];
  104. }
  105. $min = $min['min'];
  106. } elseif ($count === 3) {
  107. $minValue = array_shift($min);
  108. $max = array_shift($min);
  109. $bytestring = array_shift($min);
  110. $min = $minValue;
  111. } elseif ($count === 2) {
  112. $minValue = array_shift($min);
  113. $max = array_shift($min);
  114. $min = $minValue;
  115. } elseif($count === 1) {
  116. $min = array_shift($min);
  117. $max = null;
  118. } else {
  119. $min = 0;
  120. $max = null;
  121. $bytestring = false;
  122. }
  123. }
  124. if (empty($max)) {
  125. $max = $min;
  126. $min = 0;
  127. }
  128. $this->setMin($min);
  129. $this->setMax($max);
  130. $this->useByteString($bytestring);
  131. }
  132. /**
  133. * Returns the minimum filesize
  134. *
  135. * @param boolean $bytestring Use bytestring ?
  136. * @return integer
  137. */
  138. public function useByteString($bytestring = true)
  139. {
  140. $this->_bytestr = (boolean) $bytestring;
  141. return $this;
  142. }
  143. /**
  144. * Will bytestring be used ?
  145. *
  146. * @return boolean
  147. */
  148. public function isByteString()
  149. {
  150. return (boolean) $this->_bytestr;
  151. }
  152. /**
  153. * Returns the minimum filesize
  154. *
  155. * @param boolean $unit Return the value with unit, when false the plan bytes will be returned
  156. * @return integer
  157. */
  158. public function getMin($unit = true)
  159. {
  160. $unit = (bool) $unit;
  161. $min = $this->_min;
  162. if ($unit) {
  163. $min = $this->_toByteString($min);
  164. }
  165. return $min;
  166. }
  167. /**
  168. * Sets the minimum filesize
  169. *
  170. * @param integer $min The minimum filesize
  171. * @return Zend_Validate_File_Size Provides a fluent interface
  172. * @throws Zend_Validate_Exception When min is greater than max
  173. */
  174. public function setMin($min)
  175. {
  176. $min = (integer) $this->_fromByteString($min);
  177. if (($this->_max !== null) && ($min > $this->_max)) {
  178. require_once 'Zend/Validate/Exception.php';
  179. throw new Zend_Validate_Exception("The minimum must be less than or equal to the maximum filesize, but $min >"
  180. . " {$this->_max}");
  181. }
  182. $this->_min = max(0, $min);
  183. return $this;
  184. }
  185. /**
  186. * Returns the maximum filesize
  187. *
  188. * @param boolean $unit Return the value with unit, when false the plan bytes will be returned
  189. * @return integer|null
  190. */
  191. public function getMax($unit = true)
  192. {
  193. $unit = (bool) $unit;
  194. $max = $this->_max;
  195. if ($unit) {
  196. $max = $this->_toByteString($max);
  197. }
  198. return $max;
  199. }
  200. /**
  201. * Sets the maximum filesize
  202. *
  203. * @param integer|null $max The maximum filesize
  204. * @return Zend_Validate_StringLength Provides a fluent interface
  205. * @throws Zend_Validate_Exception When max is smaller than min
  206. */
  207. public function setMax($max)
  208. {
  209. $max = (integer) $this->_fromByteString($max);
  210. if (($this->_min !== null) && ($max < $this->_min)) {
  211. require_once 'Zend/Validate/Exception.php';
  212. throw new Zend_Validate_Exception("The maximum must be greater than or equal to the minimum filesize, but "
  213. . "$max < {$this->_min}");
  214. } else {
  215. $this->_max = $max;
  216. }
  217. return $this;
  218. }
  219. /**
  220. * Defined by Zend_Validate_Interface
  221. *
  222. * Returns true if and only if the filesize of $value is at least min and
  223. * not bigger than max (when max is not null).
  224. *
  225. * @param string $value Real file to check for size
  226. * @param array $file File data from Zend_File_Transfer
  227. * @return boolean
  228. */
  229. public function isValid($value, $file = null)
  230. {
  231. // Is file readable ?
  232. if (!@is_readable($value)) {
  233. $this->_throw($file, self::NOT_FOUND);
  234. return false;
  235. }
  236. // limited to 4GB files
  237. $this->_size = sprintf("%u",@filesize($value));
  238. $this->_sizestr = $this->_toByteString($this->_size);
  239. // Check to see if it's smaller than min size
  240. if (($this->_min !== null) && ($this->_size < $this->_min)) {
  241. if ($this->_bytestr) {
  242. $min = $this->_min;
  243. $this->_min = $this->_toByteString($this->_min);
  244. $this->_throw($file, self::TOO_SMALL);
  245. $this->_min = $min;
  246. } else {
  247. $this->_throw($file, self::TOO_SMALL);
  248. }
  249. }
  250. // Check to see if it's larger than max size
  251. if (($this->_max !== null) && ($this->_max < $this->_size)) {
  252. if ($this->_bytestr) {
  253. $max = $this->_max;
  254. $this->_max = $this->_toByteString($this->_max);
  255. $this->_throw($file, self::TOO_BIG);
  256. $this->_max = $max;
  257. } else {
  258. $this->_throw($file, self::TOO_BIG);
  259. }
  260. }
  261. if (count($this->_messages) > 0) {
  262. return false;
  263. } else {
  264. return true;
  265. }
  266. }
  267. /**
  268. * Returns the formatted size
  269. *
  270. * @param integer $size
  271. * @return string
  272. */
  273. protected function _toByteString($size)
  274. {
  275. $sizes = array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
  276. for ($i=0; $size >= 1024 && $i < 9; $i++) {
  277. $size /= 1024;
  278. }
  279. return round($size, 2).$sizes[$i];
  280. }
  281. /**
  282. * Returns the unformatted size
  283. *
  284. * @param string $size
  285. * @return integer
  286. */
  287. protected function _fromByteString($size)
  288. {
  289. if (is_numeric($size)) {
  290. return (integer) $size;
  291. }
  292. $type = trim(substr($size, -2));
  293. $value = substr($size, 0, -2);
  294. switch (strtoupper($type)) {
  295. case 'YB':
  296. $value *= (1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024);
  297. break;
  298. case 'ZB':
  299. $value *= (1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024);
  300. break;
  301. case 'EB':
  302. $value *= (1024 * 1024 * 1024 * 1024 * 1024 * 1024);
  303. break;
  304. case 'PB':
  305. $value *= (1024 * 1024 * 1024 * 1024 * 1024);
  306. break;
  307. case 'TB':
  308. $value *= (1024 * 1024 * 1024 * 1024);
  309. break;
  310. case 'GB':
  311. $value *= (1024 * 1024 * 1024);
  312. break;
  313. case 'MB':
  314. $value *= (1024 * 1024);
  315. break;
  316. case 'KB':
  317. $value *= 1024;
  318. break;
  319. default:
  320. break;
  321. }
  322. return $value;
  323. }
  324. /**
  325. * Throws an error of the given type
  326. *
  327. * @param string $file
  328. * @param string $errorType
  329. * @return false
  330. */
  331. protected function _throw($file, $errorType)
  332. {
  333. if ($file !== null) {
  334. $this->_value = $file['name'];
  335. }
  336. $this->_error($errorType);
  337. return false;
  338. }
  339. }