Hash.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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-2009 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-2009 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 => "The file '%value%' does not match the given hashes",
  46. self::NOT_DETECTED => "There was no hash detected for the given file",
  47. self::NOT_FOUND => "The file '%value%' could not be found"
  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. * @return void
  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. // @todo: Preperation for 2.0... needs to be cleared with the dev-team
  73. // trigger_error('Multiple constructor options are deprecated in favor of a single options array', E_USER_NOTICE);
  74. $options['algorithm'] = func_get_arg(1);
  75. }
  76. $this->setHash($options);
  77. }
  78. /**
  79. * Returns the set hash values as array, the hash as key and the algorithm the value
  80. *
  81. * @return array
  82. */
  83. public function getHash()
  84. {
  85. return $this->_hash;
  86. }
  87. /**
  88. * Sets the hash for one or multiple files
  89. *
  90. * @param string|array $options
  91. * @return Zend_Validate_File_Hash Provides a fluent interface
  92. */
  93. public function setHash($options)
  94. {
  95. $this->_hash = null;
  96. $this->addHash($options);
  97. return $this;
  98. }
  99. /**
  100. * Adds the hash for one or multiple files
  101. *
  102. * @param string|array $options
  103. * @return Zend_Validate_File_Hash Provides a fluent interface
  104. */
  105. public function addHash($options)
  106. {
  107. if (is_string($options)) {
  108. $options = array($options);
  109. } else if (!is_array($options)) {
  110. require_once 'Zend/Validate/Exception.php';
  111. throw new Zend_Validate_Exception("False parameter given");
  112. }
  113. $known = hash_algos();
  114. if (!isset($options['algorithm'])) {
  115. $algorithm = 'crc32';
  116. } else {
  117. $algorithm = $options['algorithm'];
  118. unset($options['algorithm']);
  119. }
  120. if (!in_array($algorithm, $known)) {
  121. require_once 'Zend/Validate/Exception.php';
  122. throw new Zend_Validate_Exception("Unknown algorithm '{$algorithm}'");
  123. }
  124. foreach ($options as $value) {
  125. $this->_hash[$value] = $algorithm;
  126. }
  127. return $this;
  128. }
  129. /**
  130. * Defined by Zend_Validate_Interface
  131. *
  132. * Returns true if and only if the given file confirms the set hash
  133. *
  134. * @param string $value Filename to check for hash
  135. * @param array $file File data from Zend_File_Transfer
  136. * @return boolean
  137. */
  138. public function isValid($value, $file = null)
  139. {
  140. // Is file readable ?
  141. require_once 'Zend/Loader.php';
  142. if (!Zend_Loader::isReadable($value)) {
  143. return $this->_throw($file, self::NOT_FOUND);
  144. }
  145. $algos = array_unique(array_values($this->_hash));
  146. $hashes = array_unique(array_keys($this->_hash));
  147. foreach ($algos as $algorithm) {
  148. $filehash = hash_file($algorithm, $value);
  149. if ($filehash === false) {
  150. return $this->_throw($file, self::NOT_DETECTED);
  151. }
  152. foreach($hashes as $hash) {
  153. if ($filehash === $hash) {
  154. return true;
  155. }
  156. }
  157. }
  158. return $this->_throw($file, self::DOES_NOT_MATCH);
  159. }
  160. /**
  161. * Throws an error of the given type
  162. *
  163. * @param string $file
  164. * @param string $errorType
  165. * @return false
  166. */
  167. protected function _throw($file, $errorType)
  168. {
  169. if ($file !== null) {
  170. $this->_value = $file['name'];
  171. }
  172. $this->_error($errorType);
  173. return false;
  174. }
  175. }