|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00025 require_once 'Zend/Validate/Abstract.php'; 00026 00035 class Zend_Validate_File_Hash extends Zend_Validate_Abstract 00036 { 00040 const DOES_NOT_MATCH = 'fileHashDoesNotMatch'; 00041 const NOT_DETECTED = 'fileHashHashNotDetected'; 00042 const NOT_FOUND = 'fileHashNotFound'; 00043 00047 protected $_messageTemplates = array( 00048 self::DOES_NOT_MATCH => "File '%value%' does not match the given hashes", 00049 self::NOT_DETECTED => "A hash could not be evaluated for the given file", 00050 self::NOT_FOUND => "File '%value%' could not be found" 00051 ); 00052 00058 protected $_hash; 00059 00066 public function __construct($options) 00067 { 00068 if ($options instanceof Zend_Config) { 00069 $options = $options->toArray(); 00070 } elseif (is_scalar($options)) { 00071 $options = array('hash1' => $options); 00072 } elseif (!is_array($options)) { 00073 require_once 'Zend/Validate/Exception.php'; 00074 throw new Zend_Validate_Exception('Invalid options to validator provided'); 00075 } 00076 00077 if (1 < func_num_args()) { 00078 $options['algorithm'] = func_get_arg(1); 00079 } 00080 00081 $this->setHash($options); 00082 } 00083 00089 public function getHash() 00090 { 00091 return $this->_hash; 00092 } 00093 00100 public function setHash($options) 00101 { 00102 $this->_hash = null; 00103 $this->addHash($options); 00104 00105 return $this; 00106 } 00107 00114 public function addHash($options) 00115 { 00116 if (is_string($options)) { 00117 $options = array($options); 00118 } else if (!is_array($options)) { 00119 require_once 'Zend/Validate/Exception.php'; 00120 throw new Zend_Validate_Exception("False parameter given"); 00121 } 00122 00123 $known = hash_algos(); 00124 if (!isset($options['algorithm'])) { 00125 $algorithm = 'crc32'; 00126 } else { 00127 $algorithm = $options['algorithm']; 00128 unset($options['algorithm']); 00129 } 00130 00131 if (!in_array($algorithm, $known)) { 00132 require_once 'Zend/Validate/Exception.php'; 00133 throw new Zend_Validate_Exception("Unknown algorithm '{$algorithm}'"); 00134 } 00135 00136 foreach ($options as $value) { 00137 $this->_hash[$value] = $algorithm; 00138 } 00139 00140 return $this; 00141 } 00142 00152 public function isValid($value, $file = null) 00153 { 00154 // Is file readable ? 00155 require_once 'Zend/Loader.php'; 00156 if (!Zend_Loader::isReadable($value)) { 00157 return $this->_throw($file, self::NOT_FOUND); 00158 } 00159 00160 $algos = array_unique(array_values($this->_hash)); 00161 $hashes = array_unique(array_keys($this->_hash)); 00162 foreach ($algos as $algorithm) { 00163 $filehash = hash_file($algorithm, $value); 00164 if ($filehash === false) { 00165 return $this->_throw($file, self::NOT_DETECTED); 00166 } 00167 00168 foreach($hashes as $hash) { 00169 if ($filehash === $hash) { 00170 return true; 00171 } 00172 } 00173 } 00174 00175 return $this->_throw($file, self::DOES_NOT_MATCH); 00176 } 00177 00185 protected function _throw($file, $errorType) 00186 { 00187 if ($file !== null) { 00188 $this->_value = $file['name']; 00189 } 00190 00191 $this->_error($errorType); 00192 return false; 00193 } 00194 }