Crc32.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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_File_Hash
  23. */
  24. #require_once 'Zend/Validate/File/Hash.php';
  25. /**
  26. * Validator for the crc32 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_Crc32 extends Zend_Validate_File_Hash
  34. {
  35. /**
  36. * @const string Error constants
  37. */
  38. const DOES_NOT_MATCH = 'fileCrc32DoesNotMatch';
  39. const NOT_DETECTED = 'fileCrc32NotDetected';
  40. const NOT_FOUND = 'fileCrc32NotFound';
  41. /**
  42. * @var array Error message templates
  43. */
  44. protected $_messageTemplates = array(
  45. self::DOES_NOT_MATCH => "File '%value%' does not match the given crc32 hashes",
  46. self::NOT_DETECTED => "A crc32 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|Zend_Config $options
  59. * @throws Zend_Validate_Exception
  60. * @return Zend_Validate_File_Crc32
  61. */
  62. public function __construct($options)
  63. {
  64. if ($options instanceof Zend_Config) {
  65. $options = $options->toArray();
  66. } elseif (is_scalar($options)) {
  67. $options = array('hash1' => $options);
  68. } elseif (!is_array($options)) {
  69. #require_once 'Zend/Validate/Exception.php';
  70. throw new Zend_Validate_Exception('Invalid options to validator provided');
  71. }
  72. $this->setCrc32($options);
  73. }
  74. /**
  75. * Returns all set crc32 hashes
  76. *
  77. * @return array
  78. */
  79. public function getCrc32()
  80. {
  81. return $this->getHash();
  82. }
  83. /**
  84. * Sets the crc32 hash for one or multiple files
  85. *
  86. * @param string|array $options
  87. * @return Zend_Validate_File_Hash Provides a fluent interface
  88. */
  89. public function setHash($options)
  90. {
  91. if (!is_array($options)) {
  92. $options = array($options);
  93. }
  94. $options['algorithm'] = 'crc32';
  95. parent::setHash($options);
  96. return $this;
  97. }
  98. /**
  99. * Sets the crc32 hash for one or multiple files
  100. *
  101. * @param string|array $options
  102. * @return Zend_Validate_File_Hash Provides a fluent interface
  103. */
  104. public function setCrc32($options)
  105. {
  106. $this->setHash($options);
  107. return $this;
  108. }
  109. /**
  110. * Adds the crc32 hash for one or multiple files
  111. *
  112. * @param string|array $options
  113. * @return Zend_Validate_File_Hash Provides a fluent interface
  114. */
  115. public function addHash($options)
  116. {
  117. if (!is_array($options)) {
  118. $options = array($options);
  119. }
  120. $options['algorithm'] = 'crc32';
  121. parent::addHash($options);
  122. return $this;
  123. }
  124. /**
  125. * Adds the crc32 hash for one or multiple files
  126. *
  127. * @param string|array $options
  128. * @return Zend_Validate_File_Hash Provides a fluent interface
  129. */
  130. public function addCrc32($options)
  131. {
  132. $this->addHash($options);
  133. return $this;
  134. }
  135. /**
  136. * Defined by Zend_Validate_Interface
  137. *
  138. * Returns true if and only if the given file confirms the set hash
  139. *
  140. * @param string $value Filename to check for hash
  141. * @param array $file File data from Zend_File_Transfer
  142. * @return boolean
  143. */
  144. public function isValid($value, $file = null)
  145. {
  146. // Is file readable ?
  147. #require_once 'Zend/Loader.php';
  148. if (!Zend_Loader::isReadable($value)) {
  149. return $this->_throw($file, self::NOT_FOUND);
  150. }
  151. $hashes = array_unique(array_keys($this->_hash));
  152. $filehash = hash_file('crc32', $value);
  153. if ($filehash === false) {
  154. return $this->_throw($file, self::NOT_DETECTED);
  155. }
  156. foreach($hashes as $hash) {
  157. if ($filehash === $hash) {
  158. return true;
  159. }
  160. }
  161. return $this->_throw($file, self::DOES_NOT_MATCH);
  162. }
  163. }