Crc32.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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-2012 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-2012 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. * @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. $this->setCrc32($options);
  72. }
  73. /**
  74. * Returns all set crc32 hashes
  75. *
  76. * @return array
  77. */
  78. public function getCrc32()
  79. {
  80. return $this->getHash();
  81. }
  82. /**
  83. * Sets the crc32 hash for one or multiple files
  84. *
  85. * @param string|array $options
  86. * @return Zend_Validate_File_Hash Provides a fluent interface
  87. */
  88. public function setHash($options)
  89. {
  90. if (!is_array($options)) {
  91. $options = array($options);
  92. }
  93. $options['algorithm'] = 'crc32';
  94. parent::setHash($options);
  95. return $this;
  96. }
  97. /**
  98. * Sets the crc32 hash for one or multiple files
  99. *
  100. * @param string|array $options
  101. * @return Zend_Validate_File_Hash Provides a fluent interface
  102. */
  103. public function setCrc32($options)
  104. {
  105. $this->setHash($options);
  106. return $this;
  107. }
  108. /**
  109. * Adds the crc32 hash for one or multiple files
  110. *
  111. * @param string|array $options
  112. * @return Zend_Validate_File_Hash Provides a fluent interface
  113. */
  114. public function addHash($options)
  115. {
  116. if (!is_array($options)) {
  117. $options = array($options);
  118. }
  119. $options['algorithm'] = 'crc32';
  120. parent::addHash($options);
  121. return $this;
  122. }
  123. /**
  124. * Adds the crc32 hash for one or multiple files
  125. *
  126. * @param string|array $options
  127. * @return Zend_Validate_File_Hash Provides a fluent interface
  128. */
  129. public function addCrc32($options)
  130. {
  131. $this->addHash($options);
  132. return $this;
  133. }
  134. /**
  135. * Defined by Zend_Validate_Interface
  136. *
  137. * Returns true if and only if the given file confirms the set hash
  138. *
  139. * @param string $value Filename to check for hash
  140. * @param array $file File data from Zend_File_Transfer
  141. * @return boolean
  142. */
  143. public function isValid($value, $file = null)
  144. {
  145. // Is file readable ?
  146. require_once 'Zend/Loader.php';
  147. if (!Zend_Loader::isReadable($value)) {
  148. return $this->_throw($file, self::NOT_FOUND);
  149. }
  150. $hashes = array_unique(array_keys($this->_hash));
  151. $filehash = hash_file('crc32', $value);
  152. if ($filehash === false) {
  153. return $this->_throw($file, self::NOT_DETECTED);
  154. }
  155. foreach($hashes as $hash) {
  156. if ($filehash === $hash) {
  157. return true;
  158. }
  159. }
  160. return $this->_throw($file, self::DOES_NOT_MATCH);
  161. }
  162. }