Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Crc32.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 = 'fileCrc32DoesNotMatch';
41  const NOT_DETECTED = 'fileCrc32NotDetected';
42  const NOT_FOUND = 'fileCrc32NotFound';
43 
47  protected $_messageTemplates = array(
48  self::DOES_NOT_MATCH => "File '%value%' does not match the given crc32 hashes",
49  self::NOT_DETECTED => "A crc32 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 
67  public function __construct($options)
68  {
69  if ($options instanceof Zend_Config) {
70  $options = $options->toArray();
71  } elseif (is_scalar($options)) {
72  $options = array('hash1' => $options);
73  } elseif (!is_array($options)) {
74  #require_once 'Zend/Validate/Exception.php';
75  throw new Zend_Validate_Exception('Invalid options to validator provided');
76  }
77 
78  $this->setCrc32($options);
79  }
80 
86  public function getCrc32()
87  {
88  return $this->getHash();
89  }
90 
97  public function setHash($options)
98  {
99  if (!is_array($options)) {
100  $options = array($options);
101  }
102 
103  $options['algorithm'] = 'crc32';
104  parent::setHash($options);
105  return $this;
106  }
107 
114  public function setCrc32($options)
115  {
116  $this->setHash($options);
117  return $this;
118  }
119 
126  public function addHash($options)
127  {
128  if (!is_array($options)) {
129  $options = array($options);
130  }
131 
132  $options['algorithm'] = 'crc32';
133  parent::addHash($options);
134  return $this;
135  }
136 
143  public function addCrc32($options)
144  {
145  $this->addHash($options);
146  return $this;
147  }
148 
158  public function isValid($value, $file = null)
159  {
160  // Is file readable ?
161  #require_once 'Zend/Loader.php';
163  return $this->_throw($file, self::NOT_FOUND);
164  }
165 
166  $hashes = array_unique(array_keys($this->_hash));
167  $filehash = hash_file('crc32', $value);
168  if ($filehash === false) {
169  return $this->_throw($file, self::NOT_DETECTED);
170  }
171 
172  foreach($hashes as $hash) {
173  if ($filehash === $hash) {
174  return true;
175  }
176  }
177 
178  return $this->_throw($file, self::DOES_NOT_MATCH);
179  }
180 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
__construct($options)
Definition: Crc32.php:67
static isReadable($filename)
Definition: Loader.php:162
isValid($value, $file=null)
Definition: Crc32.php:158
$value
Definition: gender.phtml:16
_throw($file, $errorType)
Definition: Hash.php:186