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

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

https://github.com/selfchief/zf2
PHP | 187 lines | 78 code | 19 blank | 90 comment | 8 complexity | 596e19fe52f6a025077bc0843c610df3 MD5 | raw file
Possible License(s): BSD-3-Clause
  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. use Zend\Loader;
  25. /**
  26. * Validator for the md5 hash of given files
  27. *
  28. * @uses \Zend\Loader
  29. * @uses \Zend\Validator\Exception
  30. * @uses \Zend\Validator\File\Hash
  31. * @category Zend
  32. * @package Zend_Validate
  33. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  34. * @license http://framework.zend.com/license/new-bsd New BSD License
  35. */
  36. class Md5 extends Hash
  37. {
  38. /**
  39. * @const string Error constants
  40. */
  41. const DOES_NOT_MATCH = 'fileMd5DoesNotMatch';
  42. const NOT_DETECTED = 'fileMd5NotDetected';
  43. const NOT_FOUND = 'fileMd5NotFound';
  44. /**
  45. * @var array Error message templates
  46. */
  47. protected $_messageTemplates = array(
  48. self::DOES_NOT_MATCH => "File '%value%' does not match the given md5 hashes",
  49. self::NOT_DETECTED => "A md5 hash could not be evaluated for the given file",
  50. self::NOT_FOUND => "File '%value%' is not readable or does not exist",
  51. );
  52. /**
  53. * Hash of the file
  54. *
  55. * @var string
  56. */
  57. protected $_hash;
  58. /**
  59. * Sets validator options
  60. *
  61. * $hash is the hash we accept for the file $file
  62. *
  63. * @param string|array $options
  64. * @return void
  65. */
  66. public function __construct($options)
  67. {
  68. if ($options instanceof \Zend\Config\Config) {
  69. $options = $options->toArray();
  70. } elseif (is_scalar($options)) {
  71. $options = array('hash1' => $options);
  72. } elseif (!is_array($options)) {
  73. throw new \Zend\Validator\Exception\InvalidArgumentException('Invalid options to validator provided');
  74. }
  75. $this->setMd5($options);
  76. }
  77. /**
  78. * Returns all set md5 hashes
  79. *
  80. * @return array
  81. */
  82. public function getMd5()
  83. {
  84. return $this->getHash();
  85. }
  86. /**
  87. * Sets the md5 hash for one or multiple files
  88. *
  89. * @param string|array $options
  90. * @param string $algorithm (Deprecated) Algorithm to use, fixed to md5
  91. * @return \Zend\Validator\File\Hash Provides a fluent interface
  92. */
  93. public function setHash($options)
  94. {
  95. if (!is_array($options)) {
  96. $options = (array) $options;
  97. }
  98. $options['algorithm'] = 'md5';
  99. parent::setHash($options);
  100. return $this;
  101. }
  102. /**
  103. * Sets the md5 hash for one or multiple files
  104. *
  105. * @param string|array $options
  106. * @return \Zend\Validator\File\Hash Provides a fluent interface
  107. */
  108. public function setMd5($options)
  109. {
  110. $this->setHash($options);
  111. return $this;
  112. }
  113. /**
  114. * Adds the md5 hash for one or multiple files
  115. *
  116. * @param string|array $options
  117. * @param string $algorithm (Deprecated) Algorithm to use, fixed to md5
  118. * @return \Zend\Validator\File\Hash Provides a fluent interface
  119. */
  120. public function addHash($options)
  121. {
  122. if (!is_array($options)) {
  123. $options = (array) $options;
  124. }
  125. $options['algorithm'] = 'md5';
  126. parent::addHash($options);
  127. return $this;
  128. }
  129. /**
  130. * Adds the md5 hash for one or multiple files
  131. *
  132. * @param string|array $options
  133. * @return \Zend\Validator\File\Hash Provides a fluent interface
  134. */
  135. public function addMd5($options)
  136. {
  137. $this->addHash($options);
  138. return $this;
  139. }
  140. /**
  141. * Returns true if and only if the given file confirms the set hash
  142. *
  143. * @param string $value Filename to check for hash
  144. * @param array $file File data from \Zend\File\Transfer\Transfer
  145. * @return boolean
  146. */
  147. public function isValid($value, $file = null)
  148. {
  149. if ($file === null) {
  150. $file = array('name' => basename($value));
  151. }
  152. // Is file readable ?
  153. if (!Loader::isReadable($value)) {
  154. return $this->_throw($file, self::NOT_FOUND);
  155. }
  156. $hashes = array_unique(array_keys($this->_hash));
  157. $filehash = hash_file('md5', $value);
  158. if ($filehash === false) {
  159. return $this->_throw($file, self::NOT_DETECTED);
  160. }
  161. foreach($hashes as $hash) {
  162. if ($filehash === $hash) {
  163. return true;
  164. }
  165. }
  166. return $this->_throw($file, self::DOES_NOT_MATCH);
  167. }
  168. }