PageRenderTime 58ms CodeModel.GetById 32ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/vmihailenco/zf-blog
PHP | 183 lines | 73 code | 20 blank | 90 comment | 7 complexity | 5d1493baff3659c3a54065fed17f3c92 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-2010 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 20440 2010-01-20 14:40:17Z matthew $
  20. */
  21. /**
  22. * @see Zend_Validate_File_Hash
  23. */
  24. /**
  25. * Validator for the md5 hash of given files
  26. *
  27. * @category Zend
  28. * @package Zend_Validate
  29. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  30. * @license http://framework.zend.com/license/new-bsd New BSD License
  31. */
  32. class Zend_Validate_File_Md5 extends Zend_Validate_File_Hash
  33. {
  34. /**
  35. * @const string Error constants
  36. */
  37. const DOES_NOT_MATCH = 'fileMd5DoesNotMatch';
  38. const NOT_DETECTED = 'fileMd5NotDetected';
  39. const NOT_FOUND = 'fileMd5NotFound';
  40. /**
  41. * @var array Error message templates
  42. */
  43. protected $_messageTemplates = array(
  44. self::DOES_NOT_MATCH => "File '%value%' does not match the given md5 hashes",
  45. self::NOT_DETECTED => "A md5 hash could not be evaluated for the given file",
  46. self::NOT_FOUND => "File '%value%' could not be found",
  47. );
  48. /**
  49. * Hash of the file
  50. *
  51. * @var string
  52. */
  53. protected $_hash;
  54. /**
  55. * Sets validator options
  56. *
  57. * $hash is the hash we accept for the file $file
  58. *
  59. * @param string|array $options
  60. * @return void
  61. */
  62. public function __construct($options)
  63. {
  64. if ($options instanceof Zend_Config) {
  65. $options = $options->toArray();
  66. } elseif (is_scalar($options)) {
  67. $options = array('hash1' => $options);
  68. } elseif (!is_array($options)) {
  69. throw new Zend_Validate_Exception('Invalid options to validator provided');
  70. }
  71. $this->setMd5($options);
  72. }
  73. /**
  74. * Returns all set md5 hashes
  75. *
  76. * @return array
  77. */
  78. public function getMd5()
  79. {
  80. return $this->getHash();
  81. }
  82. /**
  83. * Sets the md5 hash for one or multiple files
  84. *
  85. * @param string|array $options
  86. * @param string $algorithm (Deprecated) Algorithm to use, fixed to md5
  87. * @return Zend_Validate_File_Hash Provides a fluent interface
  88. */
  89. public function setHash($options)
  90. {
  91. if (!is_array($options)) {
  92. $options = (array) $options;
  93. }
  94. $options['algorithm'] = 'md5';
  95. parent::setHash($options);
  96. return $this;
  97. }
  98. /**
  99. * Sets the md5 hash for one or multiple files
  100. *
  101. * @param string|array $options
  102. * @return Zend_Validate_File_Hash Provides a fluent interface
  103. */
  104. public function setMd5($options)
  105. {
  106. $this->setHash($options);
  107. return $this;
  108. }
  109. /**
  110. * Adds the md5 hash for one or multiple files
  111. *
  112. * @param string|array $options
  113. * @param string $algorithm (Deprecated) Algorithm to use, fixed to md5
  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'] = 'md5';
  122. parent::addHash($options);
  123. return $this;
  124. }
  125. /**
  126. * Adds the md5 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 addMd5($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. if (!Zend_Loader::isReadable($value)) {
  149. return $this->_throw($file, self::NOT_FOUND);
  150. }
  151. $hashes = array_unique(array_keys($this->_hash));
  152. $filehash = hash_file('md5', $value);
  153. if ($filehash === false) {
  154. return $this->_throw($file, self::NOT_DETECTED);
  155. }
  156. foreach($hashes as $hash) {
  157. if ($filehash === $hash) {
  158. return true;
  159. }
  160. }
  161. return $this->_throw($file, self::DOES_NOT_MATCH);
  162. }
  163. }