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

/monica/monica/vendor/zendframework/zendframework/library/Zend/Validator/File/Md5.php

https://bitbucket.org/alexandretaz/maniac_divers
PHP | 123 lines | 65 code | 13 blank | 45 comment | 8 complexity | bf8e7756f9a0fda3907c5aea4e72bf81 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. */
  9. namespace Zend\Validator\File;
  10. use Zend\Validator\Exception;
  11. /**
  12. * Validator for the md5 hash of given files
  13. */
  14. class Md5 extends Hash
  15. {
  16. /**
  17. * @const string Error constants
  18. */
  19. const DOES_NOT_MATCH = 'fileMd5DoesNotMatch';
  20. const NOT_DETECTED = 'fileMd5NotDetected';
  21. const NOT_FOUND = 'fileMd5NotFound';
  22. /**
  23. * @var array Error message templates
  24. */
  25. protected $messageTemplates = array(
  26. self::DOES_NOT_MATCH => "File does not match the given md5 hashes",
  27. self::NOT_DETECTED => "A md5 hash could not be evaluated for the given file",
  28. self::NOT_FOUND => "File is not readable or does not exist",
  29. );
  30. /**
  31. * Options for this validator
  32. *
  33. * @var string
  34. */
  35. protected $options = array(
  36. 'algorithm' => 'md5',
  37. 'hash' => null,
  38. );
  39. /**
  40. * Returns all set md5 hashes
  41. *
  42. * @return array
  43. */
  44. public function getMd5()
  45. {
  46. return $this->getHash();
  47. }
  48. /**
  49. * Sets the md5 hash for one or multiple files
  50. *
  51. * @param string|array $options
  52. * @return Hash Provides a fluent interface
  53. */
  54. public function setMd5($options)
  55. {
  56. $this->setHash($options);
  57. return $this;
  58. }
  59. /**
  60. * Adds the md5 hash for one or multiple files
  61. *
  62. * @param string|array $options
  63. * @return Hash Provides a fluent interface
  64. */
  65. public function addMd5($options)
  66. {
  67. $this->addHash($options);
  68. return $this;
  69. }
  70. /**
  71. * Returns true if and only if the given file confirms the set hash
  72. *
  73. * @param string|array $value Filename to check for hash
  74. * @return bool
  75. */
  76. public function isValid($value)
  77. {
  78. if (is_array($value)) {
  79. if (!isset($value['tmp_name']) || !isset($value['name'])) {
  80. throw new Exception\InvalidArgumentException(
  81. 'Value array must be in $_FILES format'
  82. );
  83. }
  84. $file = $value['tmp_name'];
  85. $filename = $value['name'];
  86. } else {
  87. $file = $value;
  88. $filename = basename($file);
  89. }
  90. $this->setValue($filename);
  91. // Is file readable ?
  92. if (false === stream_resolve_include_path($file)) {
  93. $this->error(self::NOT_FOUND);
  94. return false;
  95. }
  96. $hashes = array_unique(array_keys($this->getHash()));
  97. $filehash = hash_file('md5', $file);
  98. if ($filehash === false) {
  99. $this->error(self::NOT_DETECTED);
  100. return false;
  101. }
  102. foreach ($hashes as $hash) {
  103. if ($filehash === $hash) {
  104. return true;
  105. }
  106. }
  107. $this->error(self::DOES_NOT_MATCH);
  108. return false;
  109. }
  110. }