PageRenderTime 49ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Validator/File/Md5.php

https://github.com/cwt137/zf2
PHP | 181 lines | 74 code | 17 blank | 90 comment | 7 complexity | c22e193a73fb5b701f531d01ddf5260f 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. */
  20. /**
  21. * @namespace
  22. */
  23. namespace Zend\Validator\File;
  24. /**
  25. * Validator for the md5 hash of given files
  26. *
  27. * @uses \Zend\Loader
  28. * @uses \Zend\Validator\Exception
  29. * @uses \Zend\Validator\File\Hash
  30. * @category Zend
  31. * @package Zend_Validate
  32. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. */
  35. class Md5 extends Hash
  36. {
  37. /**
  38. * @const string Error constants
  39. */
  40. const DOES_NOT_MATCH = 'fileMd5DoesNotMatch';
  41. const NOT_DETECTED = 'fileMd5NotDetected';
  42. const NOT_FOUND = 'fileMd5NotFound';
  43. /**
  44. * @var array Error message templates
  45. */
  46. protected $_messageTemplates = array(
  47. self::DOES_NOT_MATCH => "File '%value%' does not match the given md5 hashes",
  48. self::NOT_DETECTED => "A md5 hash could not be evaluated for the given file",
  49. self::NOT_FOUND => "File '%value%' is not readable or does not exist",
  50. );
  51. /**
  52. * Hash of the file
  53. *
  54. * @var string
  55. */
  56. protected $_hash;
  57. /**
  58. * Sets validator options
  59. *
  60. * $hash is the hash we accept for the file $file
  61. *
  62. * @param string|array $options
  63. * @return void
  64. */
  65. public function __construct($options)
  66. {
  67. if ($options instanceof \Zend\Config\Config) {
  68. $options = $options->toArray();
  69. } elseif (is_scalar($options)) {
  70. $options = array('hash1' => $options);
  71. } elseif (!is_array($options)) {
  72. throw new \Zend\Validator\Exception\InvalidArgumentException('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. * @param string $algorithm (Deprecated) Algorithm to use, fixed to md5
  90. * @return \Zend\Validator\File\Hash Provides a fluent interface
  91. */
  92. public function setHash($options)
  93. {
  94. if (!is_array($options)) {
  95. $options = (array) $options;
  96. }
  97. $options['algorithm'] = 'md5';
  98. parent::setHash($options);
  99. return $this;
  100. }
  101. /**
  102. * Sets the md5 hash for one or multiple files
  103. *
  104. * @param string|array $options
  105. * @return \Zend\Validator\File\Hash Provides a fluent interface
  106. */
  107. public function setMd5($options)
  108. {
  109. $this->setHash($options);
  110. return $this;
  111. }
  112. /**
  113. * Adds the md5 hash for one or multiple files
  114. *
  115. * @param string|array $options
  116. * @param string $algorithm (Deprecated) Algorithm to use, fixed to md5
  117. * @return \Zend\Validator\File\Hash Provides a fluent interface
  118. */
  119. public function addHash($options)
  120. {
  121. if (!is_array($options)) {
  122. $options = (array) $options;
  123. }
  124. $options['algorithm'] = 'md5';
  125. parent::addHash($options);
  126. return $this;
  127. }
  128. /**
  129. * Adds the md5 hash for one or multiple files
  130. *
  131. * @param string|array $options
  132. * @return \Zend\Validator\File\Hash Provides a fluent interface
  133. */
  134. public function addMd5($options)
  135. {
  136. $this->addHash($options);
  137. return $this;
  138. }
  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\Transfer
  144. * @return boolean
  145. */
  146. public function isValid($value, $file = null)
  147. {
  148. // Is file readable ?
  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('md5', $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. }