Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Hash.php
Go to the documentation of this file.
1 <?php
25 #require_once 'Zend/Validate/Abstract.php';
26 
36 {
40  const DOES_NOT_MATCH = 'fileHashDoesNotMatch';
41  const NOT_DETECTED = 'fileHashHashNotDetected';
42  const NOT_FOUND = 'fileHashNotFound';
43 
47  protected $_messageTemplates = array(
48  self::DOES_NOT_MATCH => "File '%value%' does not match the given hashes",
49  self::NOT_DETECTED => "A hash could not be evaluated for the given file",
50  self::NOT_FOUND => "File '%value%' is not readable or does not exist"
51  );
52 
58  protected $_hash;
59 
66  public function __construct($options)
67  {
68  if ($options instanceof Zend_Config) {
69  $options = $options->toArray();
70  } elseif (is_scalar($options)) {
71  $options = array('hash1' => $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 
77  if (1 < func_num_args()) {
78  $options['algorithm'] = func_get_arg(1);
79  }
80 
81  $this->setHash($options);
82  }
83 
89  public function getHash()
90  {
91  return $this->_hash;
92  }
93 
100  public function setHash($options)
101  {
102  $this->_hash = null;
103  $this->addHash($options);
104 
105  return $this;
106  }
107 
115  public function addHash($options)
116  {
117  if (is_string($options)) {
118  $options = array($options);
119  } else if (!is_array($options)) {
120  #require_once 'Zend/Validate/Exception.php';
121  throw new Zend_Validate_Exception("False parameter given");
122  }
123 
124  $known = hash_algos();
125  if (!isset($options['algorithm'])) {
126  $algorithm = 'crc32';
127  } else {
128  $algorithm = $options['algorithm'];
129  unset($options['algorithm']);
130  }
131 
132  if (!in_array($algorithm, $known)) {
133  #require_once 'Zend/Validate/Exception.php';
134  throw new Zend_Validate_Exception("Unknown algorithm '{$algorithm}'");
135  }
136 
137  foreach ($options as $value) {
138  $this->_hash[$value] = $algorithm;
139  }
140 
141  return $this;
142  }
143 
153  public function isValid($value, $file = null)
154  {
155  // Is file readable ?
156  #require_once 'Zend/Loader.php';
158  return $this->_throw($file, self::NOT_FOUND);
159  }
160 
161  $algos = array_unique(array_values($this->_hash));
162  $hashes = array_unique(array_keys($this->_hash));
163  foreach ($algos as $algorithm) {
164  $filehash = hash_file($algorithm, $value);
165  if ($filehash === false) {
166  return $this->_throw($file, self::NOT_DETECTED);
167  }
168 
169  foreach($hashes as $hash) {
170  if ($filehash === $hash) {
171  return true;
172  }
173  }
174  }
175 
176  return $this->_throw($file, self::DOES_NOT_MATCH);
177  }
178 
186  protected function _throw($file, $errorType)
187  {
188  if ($file !== null) {
189  $this->_value = $file['name'];
190  }
191 
192  $this->_error($errorType);
193  return false;
194  }
195 }
setHash($options)
Definition: Hash.php:100
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
_error($messageKey, $value=null)
Definition: Abstract.php:284
static isReadable($filename)
Definition: Loader.php:162
$value
Definition: gender.phtml:16
isValid($value, $file=null)
Definition: Hash.php:153
__construct($options)
Definition: Hash.php:66
addHash($options)
Definition: Hash.php:115
_throw($file, $errorType)
Definition: Hash.php:186