[ZF-6295] Generic:
[zend.git] / library / Zend / Validate / File / Size.php
blobc85403c517a7f6da326377a168c782d013bb6288
1 <?php
2 /**
3 * Zend Framework
5 * LICENSE
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.
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: $
22 /**
23 * @see Zend_Validate_Abstract
25 require_once 'Zend/Validate/Abstract.php';
27 /**
28 * Validator for the maximum size of a file up to a max of 2GB
30 * @category Zend
31 * @package Zend_Validate
32 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
33 * @license http://framework.zend.com/license/new-bsd New BSD License
35 class Zend_Validate_File_Size extends Zend_Validate_Abstract
37 /**#@+
38 * @const string Error constants
40 const TOO_BIG = 'fileSizeTooBig';
41 const TOO_SMALL = 'fileSizeTooSmall';
42 const NOT_FOUND = 'fileSizeNotFound';
43 /**#@-*/
45 /**
46 * @var array Error message templates
48 protected $_messageTemplates = array(
49 self::TOO_BIG => "Maximum allowed size for file '%value%' is '%max%' but '%size%' detected",
50 self::TOO_SMALL => "Minimum expected size for file '%value%' is '%min%' but '%size%' detected",
51 self::NOT_FOUND => "The file '%value%' could not be found"
54 /**
55 * @var array Error message template variables
57 protected $_messageVariables = array(
58 'min' => '_min',
59 'max' => '_max',
60 'size' => '_size',
63 /**
64 * Minimum filesize
65 * @var integer
67 protected $_min;
69 /**
70 * Maximum filesize
72 * If null, there is no maximum filesize
74 * @var integer|null
76 protected $_max;
78 /**
79 * Detected size
81 * @var integer
83 protected $_size;
85 /**
86 * Use bytestring ?
88 * @var boolean
90 protected $_useByteString = true;
92 /**
93 * Sets validator options
95 * If $options is a integer, it will be used as maximum filesize
96 * As Array is accepts the following keys:
97 * 'min': Minimum filesize
98 * 'max': Maximum filesize
99 * 'bytestring': Use bytestring or real size for messages
101 * @param integer|array $options Options for the adapter
103 public function __construct($options)
105 if ($options instanceof Zend_Config) {
106 $options = $options->toArray();
107 } elseif (is_string($options) || is_numeric($options)) {
108 $options = array('max' => $options);
109 } elseif (!is_array($options)) {
110 require_once 'Zend/Validate/Exception.php';
111 throw new Zend_Validate_Exception ('Invalid options to validator provided');
114 if (1 < func_num_args()) {
115 trigger_error('Multiple constructor options are deprecated in favor of a single options array', E_USER_NOTICE);
116 $argv = func_get_args();
117 array_shift($argv);
118 $options['max'] = array_shift($argv);
119 if (!empty($argv)) {
120 $options['bytestring'] = array_shift($argv);
124 if (isset($options['bytestring'])) {
125 $this->setUseByteString($options['bytestring']);
128 if (isset($options['min'])) {
129 $this->setMin($options['min']);
132 if (isset($options['max'])) {
133 $this->setMax($options['max']);
138 * Returns the minimum filesize
140 * @param boolean $byteString Use bytestring ?
141 * @return integer
143 public function setUseByteString($byteString = true)
145 $this->_useByteString = (bool) $byteString;
146 return $this;
150 * Will bytestring be used?
152 * @return boolean
154 public function useByteString()
156 return $this->_useByteString;
160 * Returns the minimum filesize
162 * @param bool $raw Whether or not to force return of the raw value (defaults off)
163 * @return integer|string
165 public function getMin($raw = false)
167 $min = $this->_min;
168 if (!$raw && $this->useByteString()) {
169 $min = $this->_toByteString($min);
172 return $min;
176 * Sets the minimum filesize
178 * @param integer $min The minimum filesize
179 * @throws Zend_Validate_Exception When min is greater than max
180 * @return Zend_Validate_File_Size Provides a fluent interface
182 public function setMin($min)
184 if (!is_string($min) and !is_numeric($min)) {
185 require_once 'Zend/Validate/Exception.php';
186 throw new Zend_Validate_Exception ('Invalid options to validator provided');
189 $min = (integer) $this->_fromByteString($min);
190 $max = $this->getMax(true);
191 if (($max !== null) && ($min > $max)) {
192 require_once 'Zend/Validate/Exception.php';
193 throw new Zend_Validate_Exception("The minimum must be less than or equal to the maximum filesize, but $min >"
194 . " $max");
197 $this->_min = $min;
198 return $this;
202 * Returns the maximum filesize
204 * @param bool $raw Whether or not to force return of the raw value (defaults off)
205 * @return integer|string
207 public function getMax($raw = false)
209 $max = $this->_max;
210 if (!$raw && $this->useByteString()) {
211 $max = $this->_toByteString($max);
214 return $max;
218 * Sets the maximum filesize
220 * @param integer $max The maximum filesize
221 * @throws Zend_Validate_Exception When max is smaller than min
222 * @return Zend_Validate_StringLength Provides a fluent interface
224 public function setMax($max)
226 if (!is_string($max) && !is_numeric($max)) {
227 require_once 'Zend/Validate/Exception.php';
228 throw new Zend_Validate_Exception ('Invalid options to validator provided');
231 $max = (integer) $this->_fromByteString($max);
232 $min = $this->getMin(true);
233 if (($min !== null) && ($max < $min)) {
234 require_once 'Zend/Validate/Exception.php';
235 throw new Zend_Validate_Exception("The maximum must be greater than or equal to the minimum filesize, but "
236 . "$max < $min");
239 $this->_max = $max;
240 return $this;
244 * Retrieve current detected file size
246 * @return int
248 protected function _getSize()
250 return $this->_size;
254 * Set current size
256 * @param int $size
257 * @return Zend_Validate_File_Size
259 protected function _setSize($size)
261 $this->_size = $size;
262 return $this;
266 * Defined by Zend_Validate_Interface
268 * Returns true if and only if the filesize of $value is at least min and
269 * not bigger than max (when max is not null).
271 * @param string $value Real file to check for size
272 * @param array $file File data from Zend_File_Transfer
273 * @return boolean
275 public function isValid($value, $file = null)
277 // Is file readable ?
278 require_once 'Zend/Loader.php';
279 if (!Zend_Loader::isReadable($value)) {
280 return $this->_throw($file, self::NOT_FOUND);
283 // limited to 4GB files
284 $size = sprintf("%u", @filesize($value));
286 // Check to see if it's smaller than min size
287 $min = $this->getMin(true);
288 $max = $this->getMax(true);
289 if (($min !== null) && ($size < $min)) {
290 if ($this->useByteString()) {
291 $this->_min = $this->_toByteString($min);
292 $this->_size = $this->_toByteString($size);
293 $this->_throw($file, self::TOO_SMALL);
294 $this->_min = $min;
295 $this->_size = $size;
296 } else {
297 $this->_throw($file, self::TOO_SMALL);
301 // Check to see if it's larger than max size
302 if (($max !== null) && ($max < $size)) {
303 if ($this->useByteString()) {
304 $this->_max = $this->_toByteString($max);
305 $this->_size = $this->_toByteString($size);
306 $this->_throw($file, self::TOO_BIG);
307 $this->_max = $max;
308 $this->_size = $size;
309 } else {
310 $this->_throw($file, self::TOO_BIG);
314 if (count($this->_messages) > 0) {
315 return false;
318 return true;
322 * Returns the formatted size
324 * @param integer $size
325 * @return string
327 protected function _toByteString($size)
329 $sizes = array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
330 for ($i=0; $size >= 1024 && $i < 9; $i++) {
331 $size /= 1024;
334 return round($size, 2) . $sizes[$i];
338 * Returns the unformatted size
340 * @param string $size
341 * @return integer
343 protected function _fromByteString($size)
345 if (is_numeric($size)) {
346 return (integer) $size;
349 $type = trim(substr($size, -2, 1));
351 $value = substr($size, 0, -1);
352 if (!is_numeric($value)) {
353 $value = substr($value, 0, -1);
356 switch (strtoupper($type)) {
357 case 'Y':
358 $value *= (1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024);
359 break;
360 case 'Z':
361 $value *= (1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024);
362 break;
363 case 'E':
364 $value *= (1024 * 1024 * 1024 * 1024 * 1024 * 1024);
365 break;
366 case 'P':
367 $value *= (1024 * 1024 * 1024 * 1024 * 1024);
368 break;
369 case 'T':
370 $value *= (1024 * 1024 * 1024 * 1024);
371 break;
372 case 'G':
373 $value *= (1024 * 1024 * 1024);
374 break;
375 case 'M':
376 $value *= (1024 * 1024);
377 break;
378 case 'K':
379 $value *= 1024;
380 break;
381 default:
382 break;
385 return $value;
389 * Throws an error of the given type
391 * @param string $file
392 * @param string $errorType
393 * @return false
395 protected function _throw($file, $errorType)
397 if ($file !== null) {
398 $this->_value = $file['name'];
401 $this->_error($errorType);
402 return false;