PageRenderTime 66ms CodeModel.GetById 38ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/zend/Zend/Validate/File/Hash.php

https://bitbucket.org/synergylearning/campusconnect
PHP | 194 lines | 92 code | 21 blank | 81 comment | 13 complexity | c56df906d0192ee4740ef7b4d72dff82 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, LGPL-2.1, Apache-2.0, BSD-3-Clause, AGPL-3.0
  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. * @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-2010 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%' 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. $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. * @return Zend_Validate_File_Hash Provides a fluent interface
  102. */
  103. public function addHash($options)
  104. {
  105. if (is_string($options)) {
  106. $options = array($options);
  107. } else if (!is_array($options)) {
  108. require_once 'Zend/Validate/Exception.php';
  109. throw new Zend_Validate_Exception("False parameter given");
  110. }
  111. $known = hash_algos();
  112. if (!isset($options['algorithm'])) {
  113. $algorithm = 'crc32';
  114. } else {
  115. $algorithm = $options['algorithm'];
  116. unset($options['algorithm']);
  117. }
  118. if (!in_array($algorithm, $known)) {
  119. require_once 'Zend/Validate/Exception.php';
  120. throw new Zend_Validate_Exception("Unknown algorithm '{$algorithm}'");
  121. }
  122. foreach ($options as $value) {
  123. $this->_hash[$value] = $algorithm;
  124. }
  125. return $this;
  126. }
  127. /**
  128. * Defined by Zend_Validate_Interface
  129. *
  130. * Returns true if and only if the given file confirms the set hash
  131. *
  132. * @param string $value Filename to check for hash
  133. * @param array $file File data from Zend_File_Transfer
  134. * @return boolean
  135. */
  136. public function isValid($value, $file = null)
  137. {
  138. // Is file readable ?
  139. require_once 'Zend/Loader.php';
  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. }