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

/common/third_party/Zend/Validator/File/Md5.php

https://bitbucket.org/WordpressDev/codeigniter-cross-modular-extensions-xhmvc
PHP | 113 lines | 51 code | 12 blank | 50 comment | 5 complexity | e0169f4cb3f1bd59c0e586a23a48c21a MD5 | raw file
  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-2012 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. * @package Zend_Validator
  9. */
  10. namespace Zend\Validator\File;
  11. /**
  12. * Validator for the md5 hash of given files
  13. *
  14. * @category Zend
  15. * @package Zend_Validator
  16. */
  17. class Md5 extends Hash
  18. {
  19. /**
  20. * @const string Error constants
  21. */
  22. const DOES_NOT_MATCH = 'fileMd5DoesNotMatch';
  23. const NOT_DETECTED = 'fileMd5NotDetected';
  24. const NOT_FOUND = 'fileMd5NotFound';
  25. /**
  26. * @var array Error message templates
  27. */
  28. protected $messageTemplates = array(
  29. self::DOES_NOT_MATCH => "File '%value%' does not match the given md5 hashes",
  30. self::NOT_DETECTED => "A md5 hash could not be evaluated for the given file",
  31. self::NOT_FOUND => "File '%value%' is not readable or does not exist",
  32. );
  33. /**
  34. * Options for this validator
  35. *
  36. * @var string
  37. */
  38. protected $options = array(
  39. 'algorithm' => 'md5',
  40. 'hash' => null,
  41. );
  42. /**
  43. * Returns all set md5 hashes
  44. *
  45. * @return array
  46. */
  47. public function getMd5()
  48. {
  49. return $this->getHash();
  50. }
  51. /**
  52. * Sets the md5 hash for one or multiple files
  53. *
  54. * @param string|array $options
  55. * @return Hash Provides a fluent interface
  56. */
  57. public function setMd5($options)
  58. {
  59. $this->setHash($options);
  60. return $this;
  61. }
  62. /**
  63. * Adds the md5 hash for one or multiple files
  64. *
  65. * @param string|array $options
  66. * @return Hash Provides a fluent interface
  67. */
  68. public function addMd5($options)
  69. {
  70. $this->addHash($options);
  71. return $this;
  72. }
  73. /**
  74. * Returns true if and only if the given file confirms the set hash
  75. *
  76. * @param string $value Filename to check for hash
  77. * @param array $file File data from \Zend\File\Transfer\Transfer
  78. * @return boolean
  79. */
  80. public function isValid($value, $file = null)
  81. {
  82. if ($file === null) {
  83. $file = array('name' => basename($value));
  84. }
  85. // Is file readable ?
  86. if (false === stream_resolve_include_path($value)) {
  87. return $this->throwError($file, self::NOT_FOUND);
  88. }
  89. $hashes = array_unique(array_keys($this->getHash()));
  90. $filehash = hash_file('md5', $value);
  91. if ($filehash === false) {
  92. return $this->throwError($file, self::NOT_DETECTED);
  93. }
  94. foreach ($hashes as $hash) {
  95. if ($filehash === $hash) {
  96. return true;
  97. }
  98. }
  99. return $this->throwError($file, self::DOES_NOT_MATCH);
  100. }
  101. }