/library/Zend/Validator/File/Hash.php

https://github.com/snippet/zf2 · PHP · 194 lines · 89 code · 21 blank · 84 comment · 13 complexity · 034bab0fc6d863ce832ed0d2349da24f MD5 · raw file

  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-2010 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. * @namespace
  23. */
  24. namespace Zend\Validator\File;
  25. use Zend\Validator;
  26. /**
  27. * Validator for the hash of given files
  28. *
  29. * @uses \Zend\Loader
  30. * @uses \Zend\Validator\AbstractValidator
  31. * @uses \Zend\Validator\Exception
  32. * @category Zend
  33. * @package Zend_Validate
  34. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. */
  37. class Hash extends Validator\AbstractValidator
  38. {
  39. /**
  40. * @const string Error constants
  41. */
  42. const DOES_NOT_MATCH = 'fileHashDoesNotMatch';
  43. const NOT_DETECTED = 'fileHashHashNotDetected';
  44. const NOT_FOUND = 'fileHashNotFound';
  45. /**
  46. * @var array Error message templates
  47. */
  48. protected $_messageTemplates = array(
  49. self::DOES_NOT_MATCH => "File '%value%' does not match the given hashes",
  50. self::NOT_DETECTED => "A hash could not be evaluated for the given file",
  51. self::NOT_FOUND => "File '%value%' could not be found"
  52. );
  53. /**
  54. * Hash of the file
  55. *
  56. * @var string
  57. */
  58. protected $_hash;
  59. /**
  60. * Sets validator options
  61. *
  62. * @param string|array $options
  63. * @return void
  64. */
  65. public function __construct($options)
  66. {
  67. if ($options instanceof \Zend\Config\Config) {
  68. $options = $options->toArray();
  69. } elseif (is_scalar($options)) {
  70. $options = array('hash1' => $options);
  71. } elseif (!is_array($options)) {
  72. throw new Validator\Exception('Invalid options to validator provided');
  73. }
  74. if (1 < func_num_args()) {
  75. $options['algorithm'] = func_get_arg(1);
  76. }
  77. $this->setHash($options);
  78. }
  79. /**
  80. * Returns the set hash values as array, the hash as key and the algorithm the value
  81. *
  82. * @return array
  83. */
  84. public function getHash()
  85. {
  86. return $this->_hash;
  87. }
  88. /**
  89. * Sets the hash for one or multiple files
  90. *
  91. * @param string|array $options
  92. * @return \Zend\Validator\File\Hash Provides a fluent interface
  93. */
  94. public function setHash($options)
  95. {
  96. $this->_hash = null;
  97. $this->addHash($options);
  98. return $this;
  99. }
  100. /**
  101. * Adds the hash for one or multiple files
  102. *
  103. * @param string|array $options
  104. * @return \Zend\Validator\File\Hash Provides a fluent interface
  105. */
  106. public function addHash($options)
  107. {
  108. if (is_string($options)) {
  109. $options = array($options);
  110. } else if (!is_array($options)) {
  111. throw new Validator\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. throw new Validator\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\Transfer
  135. * @return boolean
  136. */
  137. public function isValid($value, $file = null)
  138. {
  139. // Is file readable ?
  140. if (!\Zend\Loader::isReadable($value)) {
  141. return $this->_throw($file, self::NOT_FOUND);
  142. }
  143. $algos = array_unique(array_values($this->_hash));
  144. $hashes = array_unique(array_keys($this->_hash));
  145. foreach ($algos as $algorithm) {
  146. $filehash = hash_file($algorithm, $value);
  147. if ($filehash === false) {
  148. return $this->_throw($file, self::NOT_DETECTED);
  149. }
  150. foreach($hashes as $hash) {
  151. if ($filehash === $hash) {
  152. return true;
  153. }
  154. }
  155. }
  156. return $this->_throw($file, self::DOES_NOT_MATCH);
  157. }
  158. /**
  159. * Throws an error of the given type
  160. *
  161. * @param string $file
  162. * @param string $errorType
  163. * @return false
  164. */
  165. protected function _throw($file, $errorType)
  166. {
  167. if ($file !== null) {
  168. $this->_value = $file['name'];
  169. }
  170. $this->_error($errorType);
  171. return false;
  172. }
  173. }