Added the zend framework 2 library, the path is specified in line no.26 in zend_modul...
[openemr.git] / interface / modules / zend_modules / library / Zend / Validator / File / Sha1.php
blob9f29615640b1804cc36fc8d050e7b0c2a3ab5303
1 <?php
2 /**
3 * Zend Framework (http://framework.zend.com/)
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 */
10 namespace Zend\Validator\File;
12 use Zend\Validator\Exception;
14 /**
15 * Validator for the sha1 hash of given files
17 class Sha1 extends Hash
19 /**
20 * @const string Error constants
22 const DOES_NOT_MATCH = 'fileSha1DoesNotMatch';
23 const NOT_DETECTED = 'fileSha1NotDetected';
24 const NOT_FOUND = 'fileSha1NotFound';
26 /**
27 * @var array Error message templates
29 protected $messageTemplates = array(
30 self::DOES_NOT_MATCH => "File does not match the given sha1 hashes",
31 self::NOT_DETECTED => "A sha1 hash could not be evaluated for the given file",
32 self::NOT_FOUND => "File is not readable or does not exist",
35 /**
36 * Options for this validator
38 * @var string
40 protected $options = array(
41 'algorithm' => 'sha1',
42 'hash' => null,
45 /**
46 * Returns all set sha1 hashes
48 * @return array
50 public function getSha1()
52 return $this->getHash();
55 /**
56 * Sets the sha1 hash for one or multiple files
58 * @param string|array $options
59 * @return Hash Provides a fluent interface
61 public function setSha1($options)
63 $this->setHash($options);
64 return $this;
67 /**
68 * Adds the sha1 hash for one or multiple files
70 * @param string|array $options
71 * @return Hash Provides a fluent interface
73 public function addSha1($options)
75 $this->addHash($options);
76 return $this;
79 /**
80 * Returns true if and only if the given file confirms the set hash
82 * @param string $value|array Filename to check for hash
83 * @param array $file File data from \Zend\File\Transfer\Transfer (optional)
84 * @return bool
86 public function isValid($value, $file = null)
88 if (is_string($value) && is_array($file)) {
89 // Legacy Zend\Transfer API support
90 $filename = $file['name'];
91 $file = $file['tmp_name'];
92 } elseif (is_array($value)) {
93 if (!isset($value['tmp_name']) || !isset($value['name'])) {
94 throw new Exception\InvalidArgumentException(
95 'Value array must be in $_FILES format'
98 $file = $value['tmp_name'];
99 $filename = $value['name'];
100 } else {
101 $file = $value;
102 $filename = basename($file);
104 $this->setValue($filename);
106 // Is file readable ?
107 if (false === stream_resolve_include_path($file)) {
108 $this->error(self::NOT_FOUND);
109 return false;
112 $hashes = array_unique(array_keys($this->getHash()));
113 $filehash = hash_file('sha1', $file);
114 if ($filehash === false) {
115 $this->error(self::NOT_DETECTED);
116 return false;
119 foreach ($hashes as $hash) {
120 if ($filehash === $hash) {
121 return true;
125 $this->error(self::DOES_NOT_MATCH);
126 return false;