Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Sha1.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 = 'fileSha1DoesNotMatch';
41  const NOT_DETECTED = 'fileSha1NotDetected';
42  const NOT_FOUND = 'fileSha1NotFound';
43 
47  protected $_messageTemplates = array(
48  self::DOES_NOT_MATCH => "File '%value%' does not match the given sha1 hashes",
49  self::NOT_DETECTED => "A sha1 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->setHash($options);
81  }
82 
88  public function getSha1()
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'] = 'sha1';
106  parent::setHash($options);
107  return $this;
108  }
109 
116  public function setSha1($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'] = 'sha1';
135  parent::addHash($options);
136  return $this;
137  }
138 
145  public function addSha1($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('sha1', $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 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
addSha1($options)
Definition: Sha1.php:145
setSha1($options)
Definition: Sha1.php:116
isValid($value, $file=null)
Definition: Sha1.php:160
static isReadable($filename)
Definition: Loader.php:162
$value
Definition: gender.phtml:16
addHash($options)
Definition: Sha1.php:128
setHash($options)
Definition: Sha1.php:99
__construct($options)
Definition: Sha1.php:69
_throw($file, $errorType)
Definition: Hash.php:186