PageRenderTime 51ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Validator/File/Sha1.php

http://github.com/zendframework/zf2
PHP | 128 lines | 68 code | 13 blank | 47 comment | 10 complexity | d1226ca5345e4193867ab5ae7962d395 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-2015 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 sha1 hash of given files
  13. */
  14. class Sha1 extends Hash
  15. {
  16. /**
  17. * @const string Error constants
  18. */
  19. const DOES_NOT_MATCH = 'fileSha1DoesNotMatch';
  20. const NOT_DETECTED = 'fileSha1NotDetected';
  21. const NOT_FOUND = 'fileSha1NotFound';
  22. /**
  23. * @var array Error message templates
  24. */
  25. protected $messageTemplates = array(
  26. self::DOES_NOT_MATCH => "File does not match the given sha1 hashes",
  27. self::NOT_DETECTED => "A sha1 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' => 'sha1',
  37. 'hash' => null,
  38. );
  39. /**
  40. * Returns all set sha1 hashes
  41. *
  42. * @return array
  43. */
  44. public function getSha1()
  45. {
  46. return $this->getHash();
  47. }
  48. /**
  49. * Sets the sha1 hash for one or multiple files
  50. *
  51. * @param string|array $options
  52. * @return Hash Provides a fluent interface
  53. */
  54. public function setSha1($options)
  55. {
  56. $this->setHash($options);
  57. return $this;
  58. }
  59. /**
  60. * Adds the sha1 hash for one or multiple files
  61. *
  62. * @param string|array $options
  63. * @return Hash Provides a fluent interface
  64. */
  65. public function addSha1($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 $value|array Filename to check for hash
  74. * @param array $file File data from \Zend\File\Transfer\Transfer (optional)
  75. * @return bool
  76. */
  77. public function isValid($value, $file = null)
  78. {
  79. if (is_string($value) && is_array($file)) {
  80. // Legacy Zend\Transfer API support
  81. $filename = $file['name'];
  82. $file = $file['tmp_name'];
  83. } elseif (is_array($value)) {
  84. if (!isset($value['tmp_name']) || !isset($value['name'])) {
  85. throw new Exception\InvalidArgumentException(
  86. 'Value array must be in $_FILES format'
  87. );
  88. }
  89. $file = $value['tmp_name'];
  90. $filename = $value['name'];
  91. } else {
  92. $file = $value;
  93. $filename = basename($file);
  94. }
  95. $this->setValue($filename);
  96. // Is file readable ?
  97. if (empty($file) || false === stream_resolve_include_path($file)) {
  98. $this->error(self::NOT_FOUND);
  99. return false;
  100. }
  101. $hashes = array_unique(array_keys($this->getHash()));
  102. $filehash = hash_file('sha1', $file);
  103. if ($filehash === false) {
  104. $this->error(self::NOT_DETECTED);
  105. return false;
  106. }
  107. foreach ($hashes as $hash) {
  108. if ($filehash === $hash) {
  109. return true;
  110. }
  111. }
  112. $this->error(self::DOES_NOT_MATCH);
  113. return false;
  114. }
  115. }