PageRenderTime 25ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/Ebozavrik/test-application
PHP | 419 lines | 209 code | 47 blank | 163 comment | 36 complexity | ee9607be8933c8e13700598d6870af67 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-2012 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id: Size.php 24593 2012-01-05 20:35:02Z matthew $
  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-2012 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 => "File '%value%' is not readable or does not exist",
  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 $_useByteString = true;
  83. /**
  84. * Sets validator options
  85. *
  86. * If $options is a integer, it will be used as maximum filesize
  87. * As Array is accepts the following keys:
  88. * 'min': Minimum filesize
  89. * 'max': Maximum filesize
  90. * 'bytestring': Use bytestring or real size for messages
  91. *
  92. * @param integer|array $options Options for the adapter
  93. */
  94. public function __construct ($options)
  95. {
  96. if ($options instanceof Zend_Config) {
  97. $options = $options->toArray();
  98. } elseif (is_string($options) || is_numeric($options)) {
  99. $options = array( 'max' => $options );
  100. } elseif (!is_array($options)) {
  101. require_once 'Zend/Validate/Exception.php';
  102. throw new Zend_Validate_Exception ( 'Invalid options to validator provided' );
  103. }
  104. if (1 < func_num_args()) {
  105. $argv = func_get_args();
  106. array_shift($argv);
  107. $options['max'] = array_shift($argv);
  108. if (!empty( $argv )) {
  109. $options['bytestring'] = array_shift($argv);
  110. }
  111. }
  112. if (isset( $options['bytestring'] )) {
  113. $this->setUseByteString($options['bytestring']);
  114. }
  115. if (isset( $options['min'] )) {
  116. $this->setMin($options['min']);
  117. }
  118. if (isset( $options['max'] )) {
  119. $this->setMax($options['max']);
  120. }
  121. }
  122. /**
  123. * Returns the minimum filesize
  124. *
  125. * @param boolean $byteString Use bytestring ?
  126. *
  127. * @return integer
  128. */
  129. public function setUseByteString ($byteString = true)
  130. {
  131. $this->_useByteString = (bool)$byteString;
  132. return $this;
  133. }
  134. /**
  135. * Will bytestring be used?
  136. *
  137. * @return boolean
  138. */
  139. public function useByteString ()
  140. {
  141. return $this->_useByteString;
  142. }
  143. /**
  144. * Returns the minimum filesize
  145. *
  146. * @param bool $raw Whether or not to force return of the raw value (defaults off)
  147. *
  148. * @return integer|string
  149. */
  150. public function getMin ($raw = false)
  151. {
  152. $min = $this->_min;
  153. if (!$raw && $this->useByteString()) {
  154. $min = $this->_toByteString($min);
  155. }
  156. return $min;
  157. }
  158. /**
  159. * Sets the minimum filesize
  160. *
  161. * @param integer $min The minimum filesize
  162. *
  163. * @throws Zend_Validate_Exception When min is greater than max
  164. * @return Zend_Validate_File_Size Provides a fluent interface
  165. */
  166. public function setMin ($min)
  167. {
  168. if (!is_string($min) and !is_numeric($min)) {
  169. require_once 'Zend/Validate/Exception.php';
  170. throw new Zend_Validate_Exception ( 'Invalid options to validator provided' );
  171. }
  172. $min = (integer)$this->_fromByteString($min);
  173. $max = $this->getMax(true);
  174. if (( $max !== null ) && ( $min > $max )) {
  175. require_once 'Zend/Validate/Exception.php';
  176. throw new Zend_Validate_Exception( "The minimum must be less than or equal to the maximum filesize, but $min >"
  177. . " $max" );
  178. }
  179. $this->_min = $min;
  180. return $this;
  181. }
  182. /**
  183. * Returns the maximum filesize
  184. *
  185. * @param bool $raw Whether or not to force return of the raw value (defaults off)
  186. *
  187. * @return integer|string
  188. */
  189. public function getMax ($raw = false)
  190. {
  191. $max = $this->_max;
  192. if (!$raw && $this->useByteString()) {
  193. $max = $this->_toByteString($max);
  194. }
  195. return $max;
  196. }
  197. /**
  198. * Sets the maximum filesize
  199. *
  200. * @param integer $max The maximum filesize
  201. *
  202. * @throws Zend_Validate_Exception When max is smaller than min
  203. * @return Zend_Validate_StringLength Provides a fluent interface
  204. */
  205. public function setMax ($max)
  206. {
  207. if (!is_string($max) && !is_numeric($max)) {
  208. require_once 'Zend/Validate/Exception.php';
  209. throw new Zend_Validate_Exception ( 'Invalid options to validator provided' );
  210. }
  211. $max = (integer)$this->_fromByteString($max);
  212. $min = $this->getMin(true);
  213. if (( $min !== null ) && ( $max < $min )) {
  214. require_once 'Zend/Validate/Exception.php';
  215. throw new Zend_Validate_Exception( "The maximum must be greater than or equal to the minimum filesize, but "
  216. . "$max < $min" );
  217. }
  218. $this->_max = $max;
  219. return $this;
  220. }
  221. /**
  222. * Retrieve current detected file size
  223. *
  224. * @return int
  225. */
  226. protected function _getSize ()
  227. {
  228. return $this->_size;
  229. }
  230. /**
  231. * Set current size
  232. *
  233. * @param int $size
  234. *
  235. * @return Zend_Validate_File_Size
  236. */
  237. protected function _setSize ($size)
  238. {
  239. $this->_size = $size;
  240. return $this;
  241. }
  242. /**
  243. * Defined by Zend_Validate_Interface
  244. *
  245. * Returns true if and only if the filesize of $value is at least min and
  246. * not bigger than max (when max is not null).
  247. *
  248. * @param string $value Real file to check for size
  249. * @param array $file File data from Zend_File_Transfer
  250. *
  251. * @return boolean
  252. */
  253. public function isValid ($value, $file = null)
  254. {
  255. // Is file readable ?
  256. require_once 'Zend/Loader.php';
  257. if (!Zend_Loader::isReadable($value)) {
  258. return $this->_throw($file, self::NOT_FOUND);
  259. }
  260. // limited to 4GB files
  261. $size = sprintf("%u", @filesize($value));
  262. $this->_size = $size;
  263. // Check to see if it's smaller than min size
  264. $min = $this->getMin(true);
  265. $max = $this->getMax(true);
  266. if (( $min !== null ) && ( $size < $min )) {
  267. if ($this->useByteString()) {
  268. $this->_min = $this->_toByteString($min);
  269. $this->_size = $this->_toByteString($size);
  270. $this->_throw($file, self::TOO_SMALL);
  271. $this->_min = $min;
  272. $this->_size = $size;
  273. } else {
  274. $this->_throw($file, self::TOO_SMALL);
  275. }
  276. }
  277. // Check to see if it's larger than max size
  278. if (( $max !== null ) && ( $max < $size )) {
  279. if ($this->useByteString()) {
  280. $this->_max = $this->_toByteString($max);
  281. $this->_size = $this->_toByteString($size);
  282. $this->_throw($file, self::TOO_BIG);
  283. $this->_max = $max;
  284. $this->_size = $size;
  285. } else {
  286. $this->_throw($file, self::TOO_BIG);
  287. }
  288. }
  289. if (count($this->_messages) > 0) {
  290. return false;
  291. }
  292. return true;
  293. }
  294. /**
  295. * Returns the formatted size
  296. *
  297. * @param integer $size
  298. *
  299. * @return string
  300. */
  301. protected function _toByteString ($size)
  302. {
  303. $sizes = array( 'B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB' );
  304. for ($i = 0; $size >= 1024 && $i < 9; $i++) {
  305. $size /= 1024;
  306. }
  307. return round($size, 2) . $sizes[$i];
  308. }
  309. /**
  310. * Returns the unformatted size
  311. *
  312. * @param string $size
  313. *
  314. * @return integer
  315. */
  316. protected function _fromByteString ($size)
  317. {
  318. if (is_numeric($size)) {
  319. return (integer)$size;
  320. }
  321. $type = trim(substr($size, -2, 1));
  322. $value = substr($size, 0, -1);
  323. if (!is_numeric($value)) {
  324. $value = substr($value, 0, -1);
  325. }
  326. switch (strtoupper($type)) {
  327. case 'Y':
  328. $value *= ( 1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024 );
  329. break;
  330. case 'Z':
  331. $value *= ( 1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024 );
  332. break;
  333. case 'E':
  334. $value *= ( 1024 * 1024 * 1024 * 1024 * 1024 * 1024 );
  335. break;
  336. case 'P':
  337. $value *= ( 1024 * 1024 * 1024 * 1024 * 1024 );
  338. break;
  339. case 'T':
  340. $value *= ( 1024 * 1024 * 1024 * 1024 );
  341. break;
  342. case 'G':
  343. $value *= ( 1024 * 1024 * 1024 );
  344. break;
  345. case 'M':
  346. $value *= ( 1024 * 1024 );
  347. break;
  348. case 'K':
  349. $value *= 1024;
  350. break;
  351. default:
  352. break;
  353. }
  354. return $value;
  355. }
  356. /**
  357. * Throws an error of the given type
  358. *
  359. * @param string $file
  360. * @param string $errorType
  361. *
  362. * @return false
  363. */
  364. protected function _throw ($file, $errorType)
  365. {
  366. if ($file !== null) {
  367. $this->_value = $file['name'];
  368. }
  369. $this->_error($errorType);
  370. return false;
  371. }
  372. }