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.
16 * @package Zend_Validate
17 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
18 * @license http://framework.zend.com/license/new-bsd New BSD License
19 * @version $Id: Hash.php 16971 2009-07-22 18:05:45Z mikaelkael $
23 * @see Zend_Validate_Abstract
25 require_once 'Zend/Validate/Abstract.php';
28 * Validator for the hash of given files
31 * @package Zend_Validate
32 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
33 * @license http://framework.zend.com/license/new-bsd New BSD License
35 class Zend_Validate_File_Hash
extends Zend_Validate_Abstract
38 * @const string Error constants
40 const DOES_NOT_MATCH
= 'fileHashDoesNotMatch';
41 const NOT_DETECTED
= 'fileHashHashNotDetected';
42 const NOT_FOUND
= 'fileHashNotFound';
45 * @var array Error message templates
47 protected $_messageTemplates = array(
48 self
::DOES_NOT_MATCH
=> "The file '%value%' does not match the given hashes",
49 self
::NOT_DETECTED
=> "There was no hash detected for the given file",
50 self
::NOT_FOUND
=> "The file '%value%' could not be found"
61 * Sets validator options
63 * @param string|array $options
66 public function __construct($options)
68 if ($options instanceof Zend_Config
) {
69 $options = $options->toArray();
70 } elseif (is_scalar($options)) {
71 $options = array('hash1' => $options);
72 } elseif (!is_array($options)) {
73 require_once 'Zend/Validate/Exception.php';
74 throw new Zend_Validate_Exception('Invalid options to validator provided');
77 if (1 < func_num_args()) {
78 trigger_error('Multiple constructor options are deprecated in favor of a single options array', E_USER_NOTICE
);
79 $options['algorithm'] = func_get_arg(1);
82 $this->setHash($options);
86 * Returns the set hash values as array, the hash as key and the algorithm the value
90 public function getHash()
96 * Sets the hash for one or multiple files
98 * @param string|array $options
99 * @return Zend_Validate_File_Hash Provides a fluent interface
101 public function setHash($options)
104 $this->addHash($options);
110 * Adds the hash for one or multiple files
112 * @param string|array $options
113 * @return Zend_Validate_File_Hash Provides a fluent interface
115 public function addHash($options)
117 if (is_string($options)) {
118 $options = array($options);
119 } else if (!is_array($options)) {
120 require_once 'Zend/Validate/Exception.php';
121 throw new Zend_Validate_Exception("False parameter given");
124 $known = hash_algos();
125 if (!isset($options['algorithm'])) {
126 $algorithm = 'crc32';
128 $algorithm = $options['algorithm'];
129 unset($options['algorithm']);
132 if (!in_array($algorithm, $known)) {
133 require_once 'Zend/Validate/Exception.php';
134 throw new Zend_Validate_Exception("Unknown algorithm '{$algorithm}'");
137 foreach ($options as $value) {
138 $this->_hash
[$value] = $algorithm;
145 * Defined by Zend_Validate_Interface
147 * Returns true if and only if the given file confirms the set hash
149 * @param string $value Filename to check for hash
150 * @param array $file File data from Zend_File_Transfer
153 public function isValid($value, $file = null)
155 // Is file readable ?
156 require_once 'Zend/Loader.php';
157 if (!Zend_Loader
::isReadable($value)) {
158 return $this->_throw($file, self
::NOT_FOUND
);
161 $algos = array_unique(array_values($this->_hash
));
162 $hashes = array_unique(array_keys($this->_hash
));
163 foreach ($algos as $algorithm) {
164 $filehash = hash_file($algorithm, $value);
165 if ($filehash === false) {
166 return $this->_throw($file, self
::NOT_DETECTED
);
169 foreach($hashes as $hash) {
170 if ($filehash === $hash) {
176 return $this->_throw($file, self
::DOES_NOT_MATCH
);
180 * Throws an error of the given type
182 * @param string $file
183 * @param string $errorType
186 protected function _throw($file, $errorType)
188 if ($file !== null) {
189 $this->_value
= $file['name'];
192 $this->_error($errorType);