File indexing completed on 2024-05-26 06:03:37

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category  Zend
0016  * @package   Zend_Validate
0017  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0018  * @license   http://framework.zend.com/license/new-bsd     New BSD License
0019  * @version   $Id$
0020  */
0021 
0022 /**
0023  * @see Zend_Validate_File_Size
0024  */
0025 // require_once 'Zend/Validate/File/Size.php';
0026 
0027 /**
0028  * Validator for the size of all files which will be validated in sum
0029  *
0030  * @category  Zend
0031  * @package   Zend_Validate
0032  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0033  * @license   http://framework.zend.com/license/new-bsd     New BSD License
0034  */
0035 class Zend_Validate_File_FilesSize extends Zend_Validate_File_Size
0036 {
0037     /**
0038      * @const string Error constants
0039      */
0040     const TOO_BIG      = 'fileFilesSizeTooBig';
0041     const TOO_SMALL    = 'fileFilesSizeTooSmall';
0042     const NOT_READABLE = 'fileFilesSizeNotReadable';
0043 
0044     /**
0045      * @var array Error message templates
0046      */
0047     protected $_messageTemplates = array(
0048         self::TOO_BIG      => "All files in sum should have a maximum size of '%max%' but '%size%' were detected",
0049         self::TOO_SMALL    => "All files in sum should have a minimum size of '%min%' but '%size%' were detected",
0050         self::NOT_READABLE => "One or more files can not be read",
0051     );
0052 
0053     /**
0054      * Internal file array
0055      *
0056      * @var array
0057      */
0058     protected $_files;
0059 
0060     /**
0061      * Sets validator options
0062      *
0063      * Min limits the used diskspace for all files, when used with max=null it is the maximum filesize
0064      * It also accepts an array with the keys 'min' and 'max'
0065      *
0066      * @param  integer|array|Zend_Config $options Options for this validator
0067      * @throws Zend_Validate_Exception
0068      */
0069     public function __construct($options)
0070     {
0071         $this->_files = array();
0072         $this->_setSize(0);
0073 
0074         if ($options instanceof Zend_Config) {
0075             $options = $options->toArray();
0076         } elseif (is_scalar($options)) {
0077             $options = array('max' => $options);
0078         } elseif (!is_array($options)) {
0079             // require_once 'Zend/Validate/Exception.php';
0080             throw new Zend_Validate_Exception('Invalid options to validator provided');
0081         }
0082 
0083         if (1 < func_num_args()) {
0084             $argv = func_get_args();
0085             array_shift($argv);
0086             $options['max'] = array_shift($argv);
0087             if (!empty($argv)) {
0088                 $options['bytestring'] = array_shift($argv);
0089             }
0090         }
0091 
0092         parent::__construct($options);
0093     }
0094 
0095     /**
0096      * Defined by Zend_Validate_Interface
0097      *
0098      * Returns true if and only if the disk usage of all files is at least min and
0099      * not bigger than max (when max is not null).
0100      *
0101      * @param  string|array $value Real file to check for size
0102      * @param  array        $file  File data from Zend_File_Transfer
0103      * @return boolean
0104      */
0105     public function isValid($value, $file = null)
0106     {
0107         // require_once 'Zend/Loader.php';
0108         if (is_string($value)) {
0109             $value = array($value);
0110         }
0111 
0112         $min  = $this->getMin(true);
0113         $max  = $this->getMax(true);
0114         $size = $this->_getSize();
0115         foreach ($value as $files) {
0116             // Is file readable ?
0117             if (!Zend_Loader::isReadable($files)) {
0118                 $this->_throw($file, self::NOT_READABLE);
0119                 continue;
0120             }
0121 
0122             if (!isset($this->_files[$files])) {
0123                 $this->_files[$files] = $files;
0124             } else {
0125                 // file already counted... do not count twice
0126                 continue;
0127             }
0128 
0129             // limited to 2GB files
0130             $size += @filesize($files);
0131             $this->_size = $size;
0132             if (($max !== null) && ($max < $size)) {
0133                 if ($this->useByteString()) {
0134                     $this->_max  = $this->_toByteString($max);
0135                     $this->_size = $this->_toByteString($size);
0136                     $this->_throw($file, self::TOO_BIG);
0137                     $this->_max  = $max;
0138                     $this->_size = $size;
0139                 } else {
0140                     $this->_throw($file, self::TOO_BIG);
0141                 }
0142             }
0143         }
0144 
0145         // Check that aggregate files are >= minimum size
0146         if (($min !== null) && ($size < $min)) {
0147             if ($this->useByteString()) {
0148                 $this->_min  = $this->_toByteString($min);
0149                 $this->_size = $this->_toByteString($size);
0150                 $this->_throw($file, self::TOO_SMALL);
0151                 $this->_min  = $min;
0152                 $this->_size = $size;
0153             } else {
0154                 $this->_throw($file, self::TOO_SMALL);
0155             }
0156         }
0157 
0158         if (count($this->_messages) > 0) {
0159             return false;
0160         }
0161 
0162         return true;
0163     }
0164 }