Hash.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Validate
  17. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id$
  20. */
  21. /**
  22. * @see Zend_Validate_Abstract
  23. */
  24. #require_once 'Zend/Validate/Abstract.php';
  25. /**
  26. * Validator for the hash of given files
  27. *
  28. * @category Zend
  29. * @package Zend_Validate
  30. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. */
  33. class Zend_Validate_File_Hash extends Zend_Validate_Abstract
  34. {
  35. /**
  36. * @const string Error constants
  37. */
  38. const DOES_NOT_MATCH = 'fileHashDoesNotMatch';
  39. const NOT_DETECTED = 'fileHashHashNotDetected';
  40. const NOT_FOUND = 'fileHashNotFound';
  41. /**
  42. * @var array Error message templates
  43. */
  44. protected $_messageTemplates = array(
  45. self::DOES_NOT_MATCH => "File '%value%' does not match the given hashes",
  46. self::NOT_DETECTED => "A hash could not be evaluated for the given file",
  47. self::NOT_FOUND => "File '%value%' is not readable or does not exist"
  48. );
  49. /**
  50. * Hash of the file
  51. *
  52. * @var string
  53. */
  54. protected $_hash;
  55. /**
  56. * Sets validator options
  57. *
  58. * @param string|array $options
  59. * @throws Zend_Validate_Exception
  60. */
  61. public function __construct($options)
  62. {
  63. if ($options instanceof Zend_Config) {
  64. $options = $options->toArray();
  65. } elseif (is_scalar($options)) {
  66. $options = array('hash1' => $options);
  67. } elseif (!is_array($options)) {
  68. #require_once 'Zend/Validate/Exception.php';
  69. throw new Zend_Validate_Exception('Invalid options to validator provided');
  70. }
  71. if (1 < func_num_args()) {
  72. $options['algorithm'] = func_get_arg(1);
  73. }
  74. $this->setHash($options);
  75. }
  76. /**
  77. * Returns the set hash values as array, the hash as key and the algorithm the value
  78. *
  79. * @return array
  80. */
  81. public function getHash()
  82. {
  83. return $this->_hash;
  84. }
  85. /**
  86. * Sets the hash for one or multiple files
  87. *
  88. * @param string|array $options
  89. * @return Zend_Validate_File_Hash Provides a fluent interface
  90. */
  91. public function setHash($options)
  92. {
  93. $this->_hash = null;
  94. $this->addHash($options);
  95. return $this;
  96. }
  97. /**
  98. * Adds the hash for one or multiple files
  99. *
  100. * @param string|array $options
  101. * @throws Zend_Validate_Exception
  102. * @return Zend_Validate_File_Hash Provides a fluent interface
  103. */
  104. public function addHash($options)
  105. {
  106. if (is_string($options)) {
  107. $options = array($options);
  108. } else if (!is_array($options)) {
  109. #require_once 'Zend/Validate/Exception.php';
  110. throw new Zend_Validate_Exception("False parameter given");
  111. }
  112. $known = hash_algos();
  113. if (!isset($options['algorithm'])) {
  114. $algorithm = 'crc32';
  115. } else {
  116. $algorithm = $options['algorithm'];
  117. unset($options['algorithm']);
  118. }
  119. if (!in_array($algorithm, $known)) {
  120. #require_once 'Zend/Validate/Exception.php';
  121. throw new Zend_Validate_Exception("Unknown algorithm '{$algorithm}'");
  122. }
  123. foreach ($options as $value) {
  124. $this->_hash[$value] = $algorithm;
  125. }
  126. return $this;
  127. }
  128. /**
  129. * Defined by Zend_Validate_Interface
  130. *
  131. * Returns true if and only if the given file confirms the set hash
  132. *
  133. * @param string $value Filename to check for hash
  134. * @param array $file File data from Zend_File_Transfer
  135. * @return boolean
  136. */
  137. public function isValid($value, $file = null)
  138. {
  139. // Is file readable ?
  140. #require_once 'Zend/Loader.php';
  141. if (!Zend_Loader::isReadable($value)) {
  142. return $this->_throw($file, self::NOT_FOUND);
  143. }
  144. $algos = array_unique(array_values($this->_hash));
  145. $hashes = array_unique(array_keys($this->_hash));
  146. foreach ($algos as $algorithm) {
  147. $filehash = hash_file($algorithm, $value);
  148. if ($filehash === false) {
  149. return $this->_throw($file, self::NOT_DETECTED);
  150. }
  151. foreach($hashes as $hash) {
  152. if ($filehash === $hash) {
  153. return true;
  154. }
  155. }
  156. }
  157. return $this->_throw($file, self::DOES_NOT_MATCH);
  158. }
  159. /**
  160. * Throws an error of the given type
  161. *
  162. * @param string $file
  163. * @param string $errorType
  164. * @return false
  165. */
  166. protected function _throw($file, $errorType)
  167. {
  168. if ($file !== null) {
  169. $this->_value = $file['name'];
  170. }
  171. $this->_error($errorType);
  172. return false;
  173. }
  174. }