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 => "The file '%value%' does not match the given hashes",
00049 self::NOT_DETECTED => "There was no hash detected for the given file",
00050 self::NOT_FOUND => "The 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
00079
00080 $options['algorithm'] = func_get_arg(1);
00081 }
00082
00083 $this->setHash($options);
00084 }
00085
00091 public function getHash()
00092 {
00093 return $this->_hash;
00094 }
00095
00102 public function setHash($options)
00103 {
00104 $this->_hash = null;
00105 $this->addHash($options);
00106
00107 return $this;
00108 }
00109
00116 public function addHash($options)
00117 {
00118 if (is_string($options)) {
00119 $options = array($options);
00120 } else if (!is_array($options)) {
00121 require_once 'Zend/Validate/Exception.php';
00122 throw new Zend_Validate_Exception("False parameter given");
00123 }
00124
00125 $known = hash_algos();
00126 if (!isset($options['algorithm'])) {
00127 $algorithm = 'crc32';
00128 } else {
00129 $algorithm = $options['algorithm'];
00130 unset($options['algorithm']);
00131 }
00132
00133 if (!in_array($algorithm, $known)) {
00134 require_once 'Zend/Validate/Exception.php';
00135 throw new Zend_Validate_Exception("Unknown algorithm '{$algorithm}'");
00136 }
00137
00138 foreach ($options as $value) {
00139 $this->_hash[$value] = $algorithm;
00140 }
00141
00142 return $this;
00143 }
00144
00154 public function isValid($value, $file = null)
00155 {
00156
00157 require_once 'Zend/Loader.php';
00158 if (!Zend_Loader::isReadable($value)) {
00159 return $this->_throw($file, self::NOT_FOUND);
00160 }
00161
00162 $algos = array_unique(array_values($this->_hash));
00163 $hashes = array_unique(array_keys($this->_hash));
00164 foreach ($algos as $algorithm) {
00165 $filehash = hash_file($algorithm, $value);
00166 if ($filehash === false) {
00167 return $this->_throw($file, self::NOT_DETECTED);
00168 }
00169
00170 foreach($hashes as $hash) {
00171 if ($filehash === $hash) {
00172 return true;
00173 }
00174 }
00175 }
00176
00177 return $this->_throw($file, self::DOES_NOT_MATCH);
00178 }
00179
00187 protected function _throw($file, $errorType)
00188 {
00189 if ($file !== null) {
00190 $this->_value = $file['name'];
00191 }
00192
00193 $this->_error($errorType);
00194 return false;
00195 }
00196 }