*prechod na novsiu verziu ZF
[sport-group.git] / library / Zend / Validate / File / Hash.php
blobc8767772ee76330592a8d9902e78419d453f240f
1 <?php
2 /**
3 * Zend Framework
5 * LICENSE
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.
15 * @category Zend
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 $
22 /**
23 * @see Zend_Validate_Abstract
25 require_once 'Zend/Validate/Abstract.php';
27 /**
28 * Validator for the hash of given files
30 * @category Zend
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
37 /**
38 * @const string Error constants
40 const DOES_NOT_MATCH = 'fileHashDoesNotMatch';
41 const NOT_DETECTED = 'fileHashHashNotDetected';
42 const NOT_FOUND = 'fileHashNotFound';
44 /**
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"
53 /**
54 * Hash of the file
56 * @var string
58 protected $_hash;
60 /**
61 * Sets validator options
63 * @param string|array $options
64 * @return void
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);
85 /**
86 * Returns the set hash values as array, the hash as key and the algorithm the value
88 * @return array
90 public function getHash()
92 return $this->_hash;
95 /**
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)
103 $this->_hash = null;
104 $this->addHash($options);
106 return $this;
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';
127 } else {
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;
141 return $this;
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
151 * @return boolean
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) {
171 return true;
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
184 * @return false
186 protected function _throw($file, $errorType)
188 if ($file !== null) {
189 $this->_value = $file['name'];
192 $this->_error($errorType);
193 return false;