|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00025 require_once 'Zend/Validate/File/Hash.php'; 00026 00035 class Zend_Validate_File_Crc32 extends Zend_Validate_File_Hash 00036 { 00040 const DOES_NOT_MATCH = 'fileCrc32DoesNotMatch'; 00041 const NOT_DETECTED = 'fileCrc32NotDetected'; 00042 const NOT_FOUND = 'fileCrc32NotFound'; 00043 00047 protected $_messageTemplates = array( 00048 self::DOES_NOT_MATCH => "File '%value%' does not match the given crc32 hashes", 00049 self::NOT_DETECTED => "A crc32 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 $this->setCrc32($options); 00078 } 00079 00085 public function getCrc32() 00086 { 00087 return $this->getHash(); 00088 } 00089 00096 public function setHash($options) 00097 { 00098 if (!is_array($options)) { 00099 $options = array($options); 00100 } 00101 00102 $options['algorithm'] = 'crc32'; 00103 parent::setHash($options); 00104 return $this; 00105 } 00106 00113 public function setCrc32($options) 00114 { 00115 $this->setHash($options); 00116 return $this; 00117 } 00118 00125 public function addHash($options) 00126 { 00127 if (!is_array($options)) { 00128 $options = array($options); 00129 } 00130 00131 $options['algorithm'] = 'crc32'; 00132 parent::addHash($options); 00133 return $this; 00134 } 00135 00142 public function addCrc32($options) 00143 { 00144 $this->addHash($options); 00145 return $this; 00146 } 00147 00157 public function isValid($value, $file = null) 00158 { 00159 // Is file readable ? 00160 require_once 'Zend/Loader.php'; 00161 if (!Zend_Loader::isReadable($value)) { 00162 return $this->_throw($file, self::NOT_FOUND); 00163 } 00164 00165 $hashes = array_unique(array_keys($this->_hash)); 00166 $filehash = hash_file('crc32', $value); 00167 if ($filehash === false) { 00168 return $this->_throw($file, self::NOT_DETECTED); 00169 } 00170 00171 foreach($hashes as $hash) { 00172 if ($filehash === $hash) { 00173 return true; 00174 } 00175 } 00176 00177 return $this->_throw($file, self::DOES_NOT_MATCH); 00178 } 00179 }