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

/library/Zend/Validate/File/Md5.php

https://bitbucket.org/hamidrezas/melobit
PHP | 183 lines | 76 code | 17 blank | 90 comment | 7 complexity | 138ae7500fe194f7b233fd52a4c29bae MD5 | raw file
Possible License(s): AGPL-1.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-2012 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id: Md5.php 24594 2012-01-05 21:27:01Z matthew $
  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-2012 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. * @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->setMd5($options);
  74. }
  75. /**
  76. * Returns all set md5 hashes
  77. *
  78. * @return array
  79. */
  80. public function getMd5()
  81. {
  82. return $this->getHash();
  83. }
  84. /**
  85. * Sets the md5 hash for one or multiple files
  86. *
  87. * @param string|array $options
  88. * @param string $algorithm (Deprecated) Algorithm to use, fixed to md5
  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. * @param string $algorithm (Deprecated) Algorithm to use, fixed to md5
  116. * @return Zend_Validate_File_Hash Provides a fluent interface
  117. */
  118. public function addHash($options)
  119. {
  120. if (!is_array($options)) {
  121. $options = (array) $options;
  122. }
  123. $options['algorithm'] = 'md5';
  124. parent::addHash($options);
  125. return $this;
  126. }
  127. /**
  128. * Adds the md5 hash for one or multiple files
  129. *
  130. * @param string|array $options
  131. * @return Zend_Validate_File_Hash Provides a fluent interface
  132. */
  133. public function addMd5($options)
  134. {
  135. $this->addHash($options);
  136. return $this;
  137. }
  138. /**
  139. * Defined by Zend_Validate_Interface
  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
  145. * @return boolean
  146. */
  147. public function isValid($value, $file = null)
  148. {
  149. // Is file readable ?
  150. require_once 'Zend/Loader.php';
  151. if (!Zend_Loader::isReadable($value)) {
  152. return $this->_throw($file, self::NOT_FOUND);
  153. }
  154. $hashes = array_unique(array_keys($this->_hash));
  155. $filehash = hash_file('md5', $value);
  156. if ($filehash === false) {
  157. return $this->_throw($file, self::NOT_DETECTED);
  158. }
  159. foreach($hashes as $hash) {
  160. if ($filehash === $hash) {
  161. return true;
  162. }
  163. }
  164. return $this->_throw($file, self::DOES_NOT_MATCH);
  165. }
  166. }