Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Md5.php
Go to the documentation of this file.
1 <?php
25 #require_once 'Zend/Validate/File/Hash.php';
26 
36 {
40  const DOES_NOT_MATCH = 'fileMd5DoesNotMatch';
41  const NOT_DETECTED = 'fileMd5NotDetected';
42  const NOT_FOUND = 'fileMd5NotFound';
43 
47  protected $_messageTemplates = array(
48  self::DOES_NOT_MATCH => "File '%value%' does not match the given md5 hashes",
49  self::NOT_DETECTED => "A md5 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 
69  public function __construct($options)
70  {
71  if ($options instanceof Zend_Config) {
72  $options = $options->toArray();
73  } elseif (is_scalar($options)) {
74  $options = array('hash1' => $options);
75  } elseif (!is_array($options)) {
76  #require_once 'Zend/Validate/Exception.php';
77  throw new Zend_Validate_Exception('Invalid options to validator provided');
78  }
79 
80  $this->setMd5($options);
81  }
82 
88  public function getMd5()
89  {
90  return $this->getHash();
91  }
92 
99  public function setHash($options)
100  {
101  if (!is_array($options)) {
102  $options = (array) $options;
103  }
104 
105  $options['algorithm'] = 'md5';
106  parent::setHash($options);
107  return $this;
108  }
109 
116  public function setMd5($options)
117  {
118  $this->setHash($options);
119  return $this;
120  }
121 
128  public function addHash($options)
129  {
130  if (!is_array($options)) {
131  $options = (array) $options;
132  }
133 
134  $options['algorithm'] = 'md5';
135  parent::addHash($options);
136  return $this;
137  }
138 
145  public function addMd5($options)
146  {
147  $this->addHash($options);
148  return $this;
149  }
150 
160  public function isValid($value, $file = null)
161  {
162  // Is file readable ?
163  #require_once 'Zend/Loader.php';
165  return $this->_throw($file, self::NOT_FOUND);
166  }
167 
168  $hashes = array_unique(array_keys($this->_hash));
169  $filehash = hash_file('md5', $value);
170  if ($filehash === false) {
171  return $this->_throw($file, self::NOT_DETECTED);
172  }
173 
174  foreach($hashes as $hash) {
175  if ($filehash === $hash) {
176  return true;
177  }
178  }
179 
180  return $this->_throw($file, self::DOES_NOT_MATCH);
181  }
182 }
setHash($options)
Definition: Md5.php:99
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
isValid($value, $file=null)
Definition: Md5.php:160
const DOES_NOT_MATCH
Definition: Md5.php:40
setMd5($options)
Definition: Md5.php:116
static isReadable($filename)
Definition: Loader.php:162
addMd5($options)
Definition: Md5.php:145
$value
Definition: gender.phtml:16
addHash($options)
Definition: Md5.php:128
__construct($options)
Definition: Md5.php:69
_throw($file, $errorType)
Definition: Hash.php:186
const NOT_DETECTED
Definition: Md5.php:41