PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/src/lib/Zend/Validate/File/Md5.php

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