PageRenderTime 50ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/libs/Zend/Validate/File/Sha1.php

https://bitbucket.org/herloct/gagapi
PHP | 181 lines | 76 code | 17 blank | 88 comment | 7 complexity | 08797d36853c29ae62507b674d8403aa 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-2011 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id: Sha1.php 23775 2011-03-01 17:25:24Z ralph $
  20. */
  21. /**
  22. * @see Zend_Validate_File_Hash
  23. */
  24. require_once 'Zend/Validate/File/Hash.php';
  25. /**
  26. * Validator for the sha1 hash of given files
  27. *
  28. * @category Zend
  29. * @package Zend_Validate
  30. * @copyright Copyright (c) 2005-2011 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_Sha1 extends Zend_Validate_File_Hash
  34. {
  35. /**
  36. * @const string Error constants
  37. */
  38. const DOES_NOT_MATCH = 'fileSha1DoesNotMatch';
  39. const NOT_DETECTED = 'fileSha1NotDetected';
  40. const NOT_FOUND = 'fileSha1NotFound';
  41. /**
  42. * @var array Error message templates
  43. */
  44. protected $_messageTemplates = array(
  45. self::DOES_NOT_MATCH => "File '%value%' does not match the given sha1 hashes",
  46. self::NOT_DETECTED => "A sha1 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. * $hash is the hash we accept for the file $file
  59. *
  60. * @param string|array $options
  61. * @return void
  62. */
  63. public function __construct($options)
  64. {
  65. if ($options instanceof Zend_Config) {
  66. $options = $options->toArray();
  67. } elseif (is_scalar($options)) {
  68. $options = array('hash1' => $options);
  69. } elseif (!is_array($options)) {
  70. require_once 'Zend/Validate/Exception.php';
  71. throw new Zend_Validate_Exception('Invalid options to validator provided');
  72. }
  73. $this->setHash($options);
  74. }
  75. /**
  76. * Returns all set sha1 hashes
  77. *
  78. * @return array
  79. */
  80. public function getSha1()
  81. {
  82. return $this->getHash();
  83. }
  84. /**
  85. * Sets the sha1 hash for one or multiple files
  86. *
  87. * @param string|array $options
  88. * @return Zend_Validate_File_Hash Provides a fluent interface
  89. */
  90. public function setHash($options)
  91. {
  92. if (!is_array($options)) {
  93. $options = (array) $options;
  94. }
  95. $options['algorithm'] = 'sha1';
  96. parent::setHash($options);
  97. return $this;
  98. }
  99. /**
  100. * Sets the sha1 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 setSha1($options)
  106. {
  107. $this->setHash($options);
  108. return $this;
  109. }
  110. /**
  111. * Adds the sha1 hash for one or multiple files
  112. *
  113. * @param string|array $options
  114. * @return Zend_Validate_File_Hash Provides a fluent interface
  115. */
  116. public function addHash($options)
  117. {
  118. if (!is_array($options)) {
  119. $options = (array) $options;
  120. }
  121. $options['algorithm'] = 'sha1';
  122. parent::addHash($options);
  123. return $this;
  124. }
  125. /**
  126. * Adds the sha1 hash for one or multiple files
  127. *
  128. * @param string|array $options
  129. * @return Zend_Validate_File_Hash Provides a fluent interface
  130. */
  131. public function addSha1($options)
  132. {
  133. $this->addHash($options);
  134. return $this;
  135. }
  136. /**
  137. * Defined by Zend_Validate_Interface
  138. *
  139. * Returns true if and only if the given file confirms the set hash
  140. *
  141. * @param string $value Filename to check for hash
  142. * @param array $file File data from Zend_File_Transfer
  143. * @return boolean
  144. */
  145. public function isValid($value, $file = null)
  146. {
  147. // Is file readable ?
  148. require_once 'Zend/Loader.php';
  149. if (!Zend_Loader::isReadable($value)) {
  150. return $this->_throw($file, self::NOT_FOUND);
  151. }
  152. $hashes = array_unique(array_keys($this->_hash));
  153. $filehash = hash_file('sha1', $value);
  154. if ($filehash === false) {
  155. return $this->_throw($file, self::NOT_DETECTED);
  156. }
  157. foreach ($hashes as $hash) {
  158. if ($filehash === $hash) {
  159. return true;
  160. }
  161. }
  162. return $this->_throw($file, self::DOES_NOT_MATCH);
  163. }
  164. }