PageRenderTime 47ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/magento/zendframework1/library/Zend/Validate/File/FilesSize.php

https://gitlab.com/yousafsyed/easternglamor
PHP | 164 lines | 82 code | 15 blank | 67 comment | 16 complexity | ab8dd66b1a133b1851d51927eabea61e 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-2015 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-2015 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|Zend_Config $options Options for this validator
  62. * @throws Zend_Validate_Exception
  63. */
  64. public function __construct($options)
  65. {
  66. $this->_files = array();
  67. $this->_setSize(0);
  68. if ($options instanceof Zend_Config) {
  69. $options = $options->toArray();
  70. } elseif (is_scalar($options)) {
  71. $options = array('max' => $options);
  72. } elseif (!is_array($options)) {
  73. #require_once 'Zend/Validate/Exception.php';
  74. throw new Zend_Validate_Exception('Invalid options to validator provided');
  75. }
  76. if (1 < func_num_args()) {
  77. $argv = func_get_args();
  78. array_shift($argv);
  79. $options['max'] = array_shift($argv);
  80. if (!empty($argv)) {
  81. $options['bytestring'] = array_shift($argv);
  82. }
  83. }
  84. parent::__construct($options);
  85. }
  86. /**
  87. * Defined by Zend_Validate_Interface
  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
  94. * @return boolean
  95. */
  96. public function isValid($value, $file = null)
  97. {
  98. #require_once 'Zend/Loader.php';
  99. if (is_string($value)) {
  100. $value = array($value);
  101. }
  102. $min = $this->getMin(true);
  103. $max = $this->getMax(true);
  104. $size = $this->_getSize();
  105. foreach ($value as $files) {
  106. // Is file readable ?
  107. if (!Zend_Loader::isReadable($files)) {
  108. $this->_throw($file, self::NOT_READABLE);
  109. continue;
  110. }
  111. if (!isset($this->_files[$files])) {
  112. $this->_files[$files] = $files;
  113. } else {
  114. // file already counted... do not count twice
  115. continue;
  116. }
  117. // limited to 2GB files
  118. $size += @filesize($files);
  119. $this->_size = $size;
  120. if (($max !== null) && ($max < $size)) {
  121. if ($this->useByteString()) {
  122. $this->_max = $this->_toByteString($max);
  123. $this->_size = $this->_toByteString($size);
  124. $this->_throw($file, self::TOO_BIG);
  125. $this->_max = $max;
  126. $this->_size = $size;
  127. } else {
  128. $this->_throw($file, self::TOO_BIG);
  129. }
  130. }
  131. }
  132. // Check that aggregate files are >= minimum size
  133. if (($min !== null) && ($size < $min)) {
  134. if ($this->useByteString()) {
  135. $this->_min = $this->_toByteString($min);
  136. $this->_size = $this->_toByteString($size);
  137. $this->_throw($file, self::TOO_SMALL);
  138. $this->_min = $min;
  139. $this->_size = $size;
  140. } else {
  141. $this->_throw($file, self::TOO_SMALL);
  142. }
  143. }
  144. if (count($this->_messages) > 0) {
  145. return false;
  146. }
  147. return true;
  148. }
  149. }