PageRenderTime 44ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/Zend/Validate/File/Size.php

https://github.com/lanmediaservice/lms-tplib
PHP | 404 lines | 202 code | 42 blank | 160 comment | 36 complexity | 5f47aeb5e653de632254603105bf93b4 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-2009 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 16971 2009-07-22 18:05:45Z mikaelkael $
  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-2009 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 $_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. trigger_error('Multiple constructor options are deprecated in favor of a single options array', E_USER_NOTICE);
  106. $argv = func_get_args();
  107. array_shift($argv);
  108. $options['max'] = array_shift($argv);
  109. if (!empty($argv)) {
  110. $options['bytestring'] = array_shift($argv);
  111. }
  112. }
  113. if (isset($options['bytestring'])) {
  114. $this->setUseByteString($options['bytestring']);
  115. }
  116. if (isset($options['min'])) {
  117. $this->setMin($options['min']);
  118. }
  119. if (isset($options['max'])) {
  120. $this->setMax($options['max']);
  121. }
  122. }
  123. /**
  124. * Returns the minimum filesize
  125. *
  126. * @param boolean $byteString Use bytestring ?
  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. * @return integer|string
  148. */
  149. public function getMin($raw = false)
  150. {
  151. $min = $this->_min;
  152. if (!$raw && $this->useByteString()) {
  153. $min = $this->_toByteString($min);
  154. }
  155. return $min;
  156. }
  157. /**
  158. * Sets the minimum filesize
  159. *
  160. * @param integer $min The minimum filesize
  161. * @throws Zend_Validate_Exception When min is greater than max
  162. * @return Zend_Validate_File_Size Provides a fluent interface
  163. */
  164. public function setMin($min)
  165. {
  166. if (!is_string($min) and !is_numeric($min)) {
  167. //*** require_once 'Zend/Validate/Exception.php';
  168. throw new Zend_Validate_Exception ('Invalid options to validator provided');
  169. }
  170. $min = (integer) $this->_fromByteString($min);
  171. $max = $this->getMax(true);
  172. if (($max !== null) && ($min > $max)) {
  173. //*** require_once 'Zend/Validate/Exception.php';
  174. throw new Zend_Validate_Exception("The minimum must be less than or equal to the maximum filesize, but $min >"
  175. . " $max");
  176. }
  177. $this->_min = $min;
  178. return $this;
  179. }
  180. /**
  181. * Returns the maximum filesize
  182. *
  183. * @param bool $raw Whether or not to force return of the raw value (defaults off)
  184. * @return integer|string
  185. */
  186. public function getMax($raw = false)
  187. {
  188. $max = $this->_max;
  189. if (!$raw && $this->useByteString()) {
  190. $max = $this->_toByteString($max);
  191. }
  192. return $max;
  193. }
  194. /**
  195. * Sets the maximum filesize
  196. *
  197. * @param integer $max The maximum filesize
  198. * @throws Zend_Validate_Exception When max is smaller than min
  199. * @return Zend_Validate_StringLength Provides a fluent interface
  200. */
  201. public function setMax($max)
  202. {
  203. if (!is_string($max) && !is_numeric($max)) {
  204. //*** require_once 'Zend/Validate/Exception.php';
  205. throw new Zend_Validate_Exception ('Invalid options to validator provided');
  206. }
  207. $max = (integer) $this->_fromByteString($max);
  208. $min = $this->getMin(true);
  209. if (($min !== null) && ($max < $min)) {
  210. //*** require_once 'Zend/Validate/Exception.php';
  211. throw new Zend_Validate_Exception("The maximum must be greater than or equal to the minimum filesize, but "
  212. . "$max < $min");
  213. }
  214. $this->_max = $max;
  215. return $this;
  216. }
  217. /**
  218. * Retrieve current detected file size
  219. *
  220. * @return int
  221. */
  222. protected function _getSize()
  223. {
  224. return $this->_size;
  225. }
  226. /**
  227. * Set current size
  228. *
  229. * @param int $size
  230. * @return Zend_Validate_File_Size
  231. */
  232. protected function _setSize($size)
  233. {
  234. $this->_size = $size;
  235. return $this;
  236. }
  237. /**
  238. * Defined by Zend_Validate_Interface
  239. *
  240. * Returns true if and only if the filesize of $value is at least min and
  241. * not bigger than max (when max is not null).
  242. *
  243. * @param string $value Real file to check for size
  244. * @param array $file File data from Zend_File_Transfer
  245. * @return boolean
  246. */
  247. public function isValid($value, $file = null)
  248. {
  249. // Is file readable ?
  250. //*** require_once 'Zend/Loader.php';
  251. if (!Zend_Loader::isReadable($value)) {
  252. return $this->_throw($file, self::NOT_FOUND);
  253. }
  254. // limited to 4GB files
  255. $size = sprintf("%u", @filesize($value));
  256. // Check to see if it's smaller than min size
  257. $min = $this->getMin(true);
  258. $max = $this->getMax(true);
  259. if (($min !== null) && ($size < $min)) {
  260. if ($this->useByteString()) {
  261. $this->_min = $this->_toByteString($min);
  262. $this->_size = $this->_toByteString($size);
  263. $this->_throw($file, self::TOO_SMALL);
  264. $this->_min = $min;
  265. $this->_size = $size;
  266. } else {
  267. $this->_throw($file, self::TOO_SMALL);
  268. }
  269. }
  270. // Check to see if it's larger than max size
  271. if (($max !== null) && ($max < $size)) {
  272. if ($this->useByteString()) {
  273. $this->_max = $this->_toByteString($max);
  274. $this->_size = $this->_toByteString($size);
  275. $this->_throw($file, self::TOO_BIG);
  276. $this->_max = $max;
  277. $this->_size = $size;
  278. } else {
  279. $this->_throw($file, self::TOO_BIG);
  280. }
  281. }
  282. if (count($this->_messages) > 0) {
  283. return false;
  284. }
  285. return true;
  286. }
  287. /**
  288. * Returns the formatted size
  289. *
  290. * @param integer $size
  291. * @return string
  292. */
  293. protected function _toByteString($size)
  294. {
  295. $sizes = array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
  296. for ($i=0; $size >= 1024 && $i < 9; $i++) {
  297. $size /= 1024;
  298. }
  299. return round($size, 2) . $sizes[$i];
  300. }
  301. /**
  302. * Returns the unformatted size
  303. *
  304. * @param string $size
  305. * @return integer
  306. */
  307. protected function _fromByteString($size)
  308. {
  309. if (is_numeric($size)) {
  310. return (integer) $size;
  311. }
  312. $type = trim(substr($size, -2, 1));
  313. $value = substr($size, 0, -1);
  314. if (!is_numeric($value)) {
  315. $value = substr($value, 0, -1);
  316. }
  317. switch (strtoupper($type)) {
  318. case 'Y':
  319. $value *= (1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024);
  320. break;
  321. case 'Z':
  322. $value *= (1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024);
  323. break;
  324. case 'E':
  325. $value *= (1024 * 1024 * 1024 * 1024 * 1024 * 1024);
  326. break;
  327. case 'P':
  328. $value *= (1024 * 1024 * 1024 * 1024 * 1024);
  329. break;
  330. case 'T':
  331. $value *= (1024 * 1024 * 1024 * 1024);
  332. break;
  333. case 'G':
  334. $value *= (1024 * 1024 * 1024);
  335. break;
  336. case 'M':
  337. $value *= (1024 * 1024);
  338. break;
  339. case 'K':
  340. $value *= 1024;
  341. break;
  342. default:
  343. break;
  344. }
  345. return $value;
  346. }
  347. /**
  348. * Throws an error of the given type
  349. *
  350. * @param string $file
  351. * @param string $errorType
  352. * @return false
  353. */
  354. protected function _throw($file, $errorType)
  355. {
  356. if ($file !== null) {
  357. $this->_value = $file['name'];
  358. }
  359. $this->_error($errorType);
  360. return false;
  361. }
  362. }