FilesSize.php 5.3 KB

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