[5146] | 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-2010 Zend Technologies USA Inc. (http://www.zend.com) |
---|
| 18 | * @license http://framework.zend.com/license/new-bsd New BSD License |
---|
| 19 | * @version $Id: FilesSize.php 20454 2010-01-20 22:50:59Z thomas $ |
---|
| 20 | */ |
---|
| 21 | |
---|
| 22 | /** |
---|
| 23 | * @see Zend_Validate_File_Size |
---|
| 24 | */ |
---|
| 25 | require_once 'Zend/Validate/File/Size.php'; |
---|
| 26 | |
---|
| 27 | /** |
---|
| 28 | * Validator for the size of all files which will be validated in sum |
---|
| 29 | * |
---|
| 30 | * @category Zend |
---|
| 31 | * @package Zend_Validate |
---|
| 32 | * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
---|
| 33 | * @license http://framework.zend.com/license/new-bsd New BSD License |
---|
| 34 | */ |
---|
| 35 | class Zend_Validate_File_FilesSize extends Zend_Validate_File_Size |
---|
| 36 | { |
---|
| 37 | /** |
---|
| 38 | * @const string Error constants |
---|
| 39 | */ |
---|
| 40 | const TOO_BIG = 'fileFilesSizeTooBig'; |
---|
| 41 | const TOO_SMALL = 'fileFilesSizeTooSmall'; |
---|
| 42 | const NOT_READABLE = 'fileFilesSizeNotReadable'; |
---|
| 43 | |
---|
| 44 | /** |
---|
| 45 | * @var array Error message templates |
---|
| 46 | */ |
---|
| 47 | protected $_messageTemplates = array( |
---|
| 48 | self::TOO_BIG => "All files in sum should have a maximum size of '%max%' but '%size%' were detected", |
---|
| 49 | self::TOO_SMALL => "All files in sum should have a minimum size of '%min%' but '%size%' were detected", |
---|
| 50 | self::NOT_READABLE => "One or more files can not be read", |
---|
| 51 | ); |
---|
| 52 | |
---|
| 53 | /** |
---|
| 54 | * Internal file array |
---|
| 55 | * |
---|
| 56 | * @var array |
---|
| 57 | */ |
---|
| 58 | protected $_files; |
---|
| 59 | |
---|
| 60 | /** |
---|
| 61 | * Sets validator options |
---|
| 62 | * |
---|
| 63 | * Min limits the used diskspace for all files, when used with max=null it is the maximum filesize |
---|
| 64 | * It also accepts an array with the keys 'min' and 'max' |
---|
| 65 | * |
---|
| 66 | * @param integer|array|Zend_Config $options Options for this validator |
---|
| 67 | * @return void |
---|
| 68 | */ |
---|
| 69 | public function __construct($options) |
---|
| 70 | { |
---|
| 71 | $this->_files = array(); |
---|
| 72 | $this->_setSize(0); |
---|
| 73 | |
---|
| 74 | if ($options instanceof Zend_Config) { |
---|
| 75 | $options = $options->toArray(); |
---|
| 76 | } elseif (is_scalar($options)) { |
---|
| 77 | $options = array('max' => $options); |
---|
| 78 | } elseif (!is_array($options)) { |
---|
| 79 | require_once 'Zend/Validate/Exception.php'; |
---|
| 80 | throw new Zend_Validate_Exception('Invalid options to validator provided'); |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | if (1 < func_num_args()) { |
---|
| 84 | $argv = func_get_args(); |
---|
| 85 | array_shift($argv); |
---|
| 86 | $options['max'] = array_shift($argv); |
---|
| 87 | if (!empty($argv)) { |
---|
| 88 | $options['bytestring'] = array_shift($argv); |
---|
| 89 | } |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | parent::__construct($options); |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | /** |
---|
| 96 | * Defined by Zend_Validate_Interface |
---|
| 97 | * |
---|
| 98 | * Returns true if and only if the disk usage of all files is at least min and |
---|
| 99 | * not bigger than max (when max is not null). |
---|
| 100 | * |
---|
| 101 | * @param string|array $value Real file to check for size |
---|
| 102 | * @param array $file File data from Zend_File_Transfer |
---|
| 103 | * @return boolean |
---|
| 104 | */ |
---|
| 105 | public function isValid($value, $file = null) |
---|
| 106 | { |
---|
| 107 | require_once 'Zend/Loader.php'; |
---|
| 108 | if (is_string($value)) { |
---|
| 109 | $value = array($value); |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | $min = $this->getMin(true); |
---|
| 113 | $max = $this->getMax(true); |
---|
| 114 | $size = $this->_getSize(); |
---|
| 115 | foreach ($value as $files) { |
---|
| 116 | // Is file readable ? |
---|
| 117 | if (!Zend_Loader::isReadable($files)) { |
---|
| 118 | $this->_throw($file, self::NOT_READABLE); |
---|
| 119 | continue; |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | if (!isset($this->_files[$files])) { |
---|
| 123 | $this->_files[$files] = $files; |
---|
| 124 | } else { |
---|
| 125 | // file already counted... do not count twice |
---|
| 126 | continue; |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | // limited to 2GB files |
---|
| 130 | $size += @filesize($files); |
---|
| 131 | $this->_size = $size; |
---|
| 132 | if (($max !== null) && ($max < $size)) { |
---|
| 133 | if ($this->useByteString()) { |
---|
| 134 | $this->_max = $this->_toByteString($max); |
---|
| 135 | $this->_size = $this->_toByteString($size); |
---|
| 136 | $this->_throw($file, self::TOO_BIG); |
---|
| 137 | $this->_max = $max; |
---|
| 138 | $this->_size = $size; |
---|
| 139 | } else { |
---|
| 140 | $this->_throw($file, self::TOO_BIG); |
---|
| 141 | } |
---|
| 142 | } |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | // Check that aggregate files are >= minimum size |
---|
| 146 | if (($min !== null) && ($size < $min)) { |
---|
| 147 | if ($this->useByteString()) { |
---|
| 148 | $this->_min = $this->_toByteString($min); |
---|
| 149 | $this->_size = $this->_toByteString($size); |
---|
| 150 | $this->_throw($file, self::TOO_SMALL); |
---|
| 151 | $this->_min = $min; |
---|
| 152 | $this->_size = $size; |
---|
| 153 | } else { |
---|
| 154 | $this->_throw($file, self::TOO_SMALL); |
---|
| 155 | } |
---|
| 156 | } |
---|
| 157 | |
---|
| 158 | if (count($this->_messages) > 0) { |
---|
| 159 | return false; |
---|
| 160 | } |
---|
| 161 | |
---|
| 162 | return true; |
---|
| 163 | } |
---|
| 164 | } |
---|