PageRenderTime 45ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Validator/File/FilesSize.php

https://github.com/mrbanzai/zf2
PHP | 164 lines | 84 code | 16 blank | 64 comment | 16 complexity | ce4b5bcb1f4b3307078042d92776d30e 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-2012 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. */
  20. /**
  21. * @namespace
  22. */
  23. namespace Zend\Validator\File;
  24. use Zend\Loader;
  25. /**
  26. * Validator for the size of all files which will be validated in sum
  27. *
  28. * @uses \Zend\Loader
  29. * @uses \Zend\Validator\File\Size
  30. * @uses \Zend\Validator\Exception
  31. * @category Zend
  32. * @package Zend_Validate
  33. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  34. * @license http://framework.zend.com/license/new-bsd New BSD License
  35. */
  36. class FilesSize extends Size
  37. {
  38. /**
  39. * @const string Error constants
  40. */
  41. const TOO_BIG = 'fileFilesSizeTooBig';
  42. const TOO_SMALL = 'fileFilesSizeTooSmall';
  43. const NOT_READABLE = 'fileFilesSizeNotReadable';
  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. * Internal file array
  54. *
  55. * @var array
  56. */
  57. protected $_files;
  58. /**
  59. * Sets validator options
  60. *
  61. * Min limits the used diskspace for all files, when used with max=null it is the maximum filesize
  62. * It also accepts an array with the keys 'min' and 'max'
  63. *
  64. * @param integer|array|\Zend\Config\Config $options Options for this validator
  65. * @return void
  66. */
  67. public function __construct($options = null)
  68. {
  69. $this->_files = array();
  70. $this->_setSize(0);
  71. if ($options instanceof \Zend\Config\Config) {
  72. $options = $options->toArray();
  73. } elseif (is_scalar($options)) {
  74. $options = array('max' => $options);
  75. } elseif (!is_array($options)) {
  76. throw new \Zend\Validator\Exception\InvalidArgumentException('Invalid options to validator provided');
  77. }
  78. if (1 < func_num_args()) {
  79. $argv = func_get_args();
  80. array_shift($argv);
  81. $options['max'] = array_shift($argv);
  82. if (!empty($argv)) {
  83. $options['useByteString'] = array_shift($argv);
  84. }
  85. }
  86. parent::__construct($options);
  87. }
  88. /**
  89. * Returns true if and only if the disk usage of all files is at least min and
  90. * not bigger than max (when max is not null).
  91. *
  92. * @param string|array $value Real file to check for size
  93. * @param array $file File data from \Zend\File\Transfer\Transfer
  94. * @return boolean
  95. */
  96. public function isValid($value, $file = null)
  97. {
  98. if (is_string($value)) {
  99. $value = array($value);
  100. }
  101. $min = $this->getMin(true);
  102. $max = $this->getMax(true);
  103. $size = $this->_getSize();
  104. foreach ($value as $files) {
  105. // Is file readable ?
  106. if (!Loader::isReadable($files)) {
  107. $this->_throw($file, self::NOT_READABLE);
  108. continue;
  109. }
  110. if (!isset($this->_files[$files])) {
  111. $this->_files[$files] = $files;
  112. } else {
  113. // file already counted... do not count twice
  114. continue;
  115. }
  116. // limited to 2GB files
  117. $size += @filesize($files);
  118. $this->_size = $size;
  119. if (($max !== null) && ($max < $size)) {
  120. if ($this->getByteString()) {
  121. $this->options['max'] = $this->_toByteString($max);
  122. $this->_size = $this->_toByteString($size);
  123. $this->_throw($file, self::TOO_BIG);
  124. $this->options['max'] = $max;
  125. $this->_size = $size;
  126. } else {
  127. $this->_throw($file, self::TOO_BIG);
  128. }
  129. }
  130. }
  131. // Check that aggregate files are >= minimum size
  132. if (($min !== null) && ($size < $min)) {
  133. if ($this->getByteString()) {
  134. $this->options['min'] = $this->_toByteString($min);
  135. $this->_size = $this->_toByteString($size);
  136. $this->_throw($file, self::TOO_SMALL);
  137. $this->options['min'] = $min;
  138. $this->_size = $size;
  139. } else {
  140. $this->_throw($file, self::TOO_SMALL);
  141. }
  142. }
  143. if (count($this->getMessages()) > 0) {
  144. return false;
  145. }
  146. return true;
  147. }
  148. }