|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00025 require_once 'Zend/Validate/Abstract.php'; 00026 00035 class Zend_Validate_File_Size extends Zend_Validate_Abstract 00036 { 00040 const TOO_BIG = 'fileSizeTooBig'; 00041 const TOO_SMALL = 'fileSizeTooSmall'; 00042 const NOT_FOUND = 'fileSizeNotFound'; 00048 protected $_messageTemplates = array( 00049 self::TOO_BIG => "Maximum allowed size for file '%value%' is '%max%' but '%size%' detected", 00050 self::TOO_SMALL => "Minimum expected size for file '%value%' is '%min%' but '%size%' detected", 00051 self::NOT_FOUND => "File '%value%' could not be found", 00052 ); 00053 00057 protected $_messageVariables = array( 00058 'min' => '_min', 00059 'max' => '_max', 00060 'size' => '_size', 00061 ); 00062 00067 protected $_min; 00068 00076 protected $_max; 00077 00083 protected $_size; 00084 00090 protected $_useByteString = true; 00091 00103 public function __construct($options) 00104 { 00105 if ($options instanceof Zend_Config) { 00106 $options = $options->toArray(); 00107 } elseif (is_string($options) || is_numeric($options)) { 00108 $options = array('max' => $options); 00109 } elseif (!is_array($options)) { 00110 require_once 'Zend/Validate/Exception.php'; 00111 throw new Zend_Validate_Exception ('Invalid options to validator provided'); 00112 } 00113 00114 if (1 < func_num_args()) { 00115 $argv = func_get_args(); 00116 array_shift($argv); 00117 $options['max'] = array_shift($argv); 00118 if (!empty($argv)) { 00119 $options['bytestring'] = array_shift($argv); 00120 } 00121 } 00122 00123 if (isset($options['bytestring'])) { 00124 $this->setUseByteString($options['bytestring']); 00125 } 00126 00127 if (isset($options['min'])) { 00128 $this->setMin($options['min']); 00129 } 00130 00131 if (isset($options['max'])) { 00132 $this->setMax($options['max']); 00133 } 00134 } 00135 00142 public function setUseByteString($byteString = true) 00143 { 00144 $this->_useByteString = (bool) $byteString; 00145 return $this; 00146 } 00147 00153 public function useByteString() 00154 { 00155 return $this->_useByteString; 00156 } 00157 00164 public function getMin($raw = false) 00165 { 00166 $min = $this->_min; 00167 if (!$raw && $this->useByteString()) { 00168 $min = $this->_toByteString($min); 00169 } 00170 00171 return $min; 00172 } 00173 00181 public function setMin($min) 00182 { 00183 if (!is_string($min) and !is_numeric($min)) { 00184 require_once 'Zend/Validate/Exception.php'; 00185 throw new Zend_Validate_Exception ('Invalid options to validator provided'); 00186 } 00187 00188 $min = (integer) $this->_fromByteString($min); 00189 $max = $this->getMax(true); 00190 if (($max !== null) && ($min > $max)) { 00191 require_once 'Zend/Validate/Exception.php'; 00192 throw new Zend_Validate_Exception("The minimum must be less than or equal to the maximum filesize, but $min >" 00193 . " $max"); 00194 } 00195 00196 $this->_min = $min; 00197 return $this; 00198 } 00199 00206 public function getMax($raw = false) 00207 { 00208 $max = $this->_max; 00209 if (!$raw && $this->useByteString()) { 00210 $max = $this->_toByteString($max); 00211 } 00212 00213 return $max; 00214 } 00215 00223 public function setMax($max) 00224 { 00225 if (!is_string($max) && !is_numeric($max)) { 00226 require_once 'Zend/Validate/Exception.php'; 00227 throw new Zend_Validate_Exception ('Invalid options to validator provided'); 00228 } 00229 00230 $max = (integer) $this->_fromByteString($max); 00231 $min = $this->getMin(true); 00232 if (($min !== null) && ($max < $min)) { 00233 require_once 'Zend/Validate/Exception.php'; 00234 throw new Zend_Validate_Exception("The maximum must be greater than or equal to the minimum filesize, but " 00235 . "$max < $min"); 00236 } 00237 00238 $this->_max = $max; 00239 return $this; 00240 } 00241 00247 protected function _getSize() 00248 { 00249 return $this->_size; 00250 } 00251 00258 protected function _setSize($size) 00259 { 00260 $this->_size = $size; 00261 return $this; 00262 } 00263 00274 public function isValid($value, $file = null) 00275 { 00276 // Is file readable ? 00277 require_once 'Zend/Loader.php'; 00278 if (!Zend_Loader::isReadable($value)) { 00279 return $this->_throw($file, self::NOT_FOUND); 00280 } 00281 00282 // limited to 4GB files 00283 $size = sprintf("%u", @filesize($value)); 00284 $this->_size = $size; 00285 00286 // Check to see if it's smaller than min size 00287 $min = $this->getMin(true); 00288 $max = $this->getMax(true); 00289 if (($min !== null) && ($size < $min)) { 00290 if ($this->useByteString()) { 00291 $this->_min = $this->_toByteString($min); 00292 $this->_size = $this->_toByteString($size); 00293 $this->_throw($file, self::TOO_SMALL); 00294 $this->_min = $min; 00295 $this->_size = $size; 00296 } else { 00297 $this->_throw($file, self::TOO_SMALL); 00298 } 00299 } 00300 00301 // Check to see if it's larger than max size 00302 if (($max !== null) && ($max < $size)) { 00303 if ($this->useByteString()) { 00304 $this->_max = $this->_toByteString($max); 00305 $this->_size = $this->_toByteString($size); 00306 $this->_throw($file, self::TOO_BIG); 00307 $this->_max = $max; 00308 $this->_size = $size; 00309 } else { 00310 $this->_throw($file, self::TOO_BIG); 00311 } 00312 } 00313 00314 if (count($this->_messages) > 0) { 00315 return false; 00316 } 00317 00318 return true; 00319 } 00320 00327 protected function _toByteString($size) 00328 { 00329 $sizes = array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'); 00330 for ($i=0; $size >= 1024 && $i < 9; $i++) { 00331 $size /= 1024; 00332 } 00333 00334 return round($size, 2) . $sizes[$i]; 00335 } 00336 00343 protected function _fromByteString($size) 00344 { 00345 if (is_numeric($size)) { 00346 return (integer) $size; 00347 } 00348 00349 $type = trim(substr($size, -2, 1)); 00350 00351 $value = substr($size, 0, -1); 00352 if (!is_numeric($value)) { 00353 $value = substr($value, 0, -1); 00354 } 00355 00356 switch (strtoupper($type)) { 00357 case 'Y': 00358 $value *= (1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024); 00359 break; 00360 case 'Z': 00361 $value *= (1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024); 00362 break; 00363 case 'E': 00364 $value *= (1024 * 1024 * 1024 * 1024 * 1024 * 1024); 00365 break; 00366 case 'P': 00367 $value *= (1024 * 1024 * 1024 * 1024 * 1024); 00368 break; 00369 case 'T': 00370 $value *= (1024 * 1024 * 1024 * 1024); 00371 break; 00372 case 'G': 00373 $value *= (1024 * 1024 * 1024); 00374 break; 00375 case 'M': 00376 $value *= (1024 * 1024); 00377 break; 00378 case 'K': 00379 $value *= 1024; 00380 break; 00381 default: 00382 break; 00383 } 00384 00385 return $value; 00386 } 00387 00395 protected function _throw($file, $errorType) 00396 { 00397 if ($file !== null) { 00398 $this->_value = $file['name']; 00399 } 00400 00401 $this->_error($errorType); 00402 return false; 00403 } 00404 }