~chroot64bit/zivios/gentoo-experimental

« back to all changes in this revision

Viewing changes to application/library/Zend/Validate/File/Hash.php

  • Committer: Mustafa A. Hashmi
  • Date: 2008-12-04 13:32:21 UTC
  • Revision ID: mhashmi@zivios.org-20081204133221-0nd1trunwevijj38
Inclusion of new installation framework with ties to zend layout and dojo layout

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * Zend Framework
 
4
 *
 
5
 * LICENSE
 
6
 *
 
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.
 
14
 *
 
15
 * @category  Zend
 
16
 * @package   Zend_Validate
 
17
 * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
 
18
 * @license   http://framework.zend.com/license/new-bsd     New BSD License
 
19
 * @version   $Id: $
 
20
 */
 
21
 
 
22
/**
 
23
 * @see Zend_Validate_Abstract
 
24
 */
 
25
require_once 'Zend/Validate/Abstract.php';
 
26
 
 
27
/**
 
28
 * Validator for the hash of given files
 
29
 *
 
30
 * @category  Zend
 
31
 * @package   Zend_Validate
 
32
 * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
 
33
 * @license   http://framework.zend.com/license/new-bsd     New BSD License
 
34
 */
 
35
class Zend_Validate_File_Hash extends Zend_Validate_Abstract
 
36
{
 
37
    /**
 
38
     * @const string Error constants
 
39
     */
 
40
    const DOES_NOT_MATCH = 'fileHashDoesNotMatch';
 
41
    const NOT_DETECTED   = 'fileHashHashNotDetected';
 
42
    const NOT_FOUND      = 'fileHashNotFound';
 
43
 
 
44
    /**
 
45
     * @var array Error message templates
 
46
     */
 
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"
 
51
    );
 
52
 
 
53
    /**
 
54
     * Hash of the file
 
55
     *
 
56
     * @var string
 
57
     */
 
58
    protected $_hash;
 
59
 
 
60
    /**
 
61
     * Sets validator options
 
62
     *
 
63
     * @param  string|array $options
 
64
     * @return void
 
65
     */
 
66
    public function __construct($options)
 
67
    {
 
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');
 
75
        }
 
76
 
 
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);
 
80
        }
 
81
 
 
82
        $this->setHash($options);
 
83
    }
 
84
 
 
85
    /**
 
86
     * Returns the set hash values as array, the hash as key and the algorithm the value
 
87
     *
 
88
     * @return array
 
89
     */
 
90
    public function getHash()
 
91
    {
 
92
        return $this->_hash;
 
93
    }
 
94
 
 
95
    /**
 
96
     * Sets the hash for one or multiple files
 
97
     *
 
98
     * @param  string|array $options
 
99
     * @return Zend_Validate_File_Hash Provides a fluent interface
 
100
     */
 
101
    public function setHash($options)
 
102
    {
 
103
        $this->_hash  = null;
 
104
        $this->addHash($options);
 
105
 
 
106
        return $this;
 
107
    }
 
108
 
 
109
    /**
 
110
     * Adds the hash for one or multiple files
 
111
     *
 
112
     * @param  string|array $options
 
113
     * @return Zend_Validate_File_Hash Provides a fluent interface
 
114
     */
 
115
    public function addHash($options)
 
116
    {
 
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");
 
122
        }
 
123
 
 
124
        $known = hash_algos();
 
125
        if (!isset($options['algorithm'])) {
 
126
            $algorithm = 'crc32';
 
127
        } else {
 
128
            $algorithm = $options['algorithm'];
 
129
            unset($options['algorithm']);
 
130
        }
 
131
 
 
132
        if (!in_array($algorithm, $known)) {
 
133
            require_once 'Zend/Validate/Exception.php';
 
134
            throw new Zend_Validate_Exception("Unknown algorithm '{$algorithm}'");
 
135
        }
 
136
 
 
137
        foreach ($options as $value) {
 
138
            $this->_hash[$value] = $algorithm;
 
139
        }
 
140
 
 
141
        return $this;
 
142
    }
 
143
 
 
144
    /**
 
145
     * Defined by Zend_Validate_Interface
 
146
     *
 
147
     * Returns true if and only if the given file confirms the set hash
 
148
     *
 
149
     * @param  string $value Filename to check for hash
 
150
     * @param  array  $file  File data from Zend_File_Transfer
 
151
     * @return boolean
 
152
     */
 
153
    public function isValid($value, $file = null)
 
154
    {
 
155
        // Is file readable ?
 
156
        require_once 'Zend/Loader.php';
 
157
        if (!Zend_Loader::isReadable($value)) {
 
158
            return $this->_throw($file, self::NOT_FOUND);
 
159
        }
 
160
 
 
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);
 
167
            }
 
168
 
 
169
            foreach($hashes as $hash) {
 
170
                if ($filehash === $hash) {
 
171
                    return true;
 
172
                }
 
173
            }
 
174
        }
 
175
 
 
176
        return $this->_throw($file, self::DOES_NOT_MATCH);
 
177
    }
 
178
 
 
179
    /**
 
180
     * Throws an error of the given type
 
181
     *
 
182
     * @param  string $file
 
183
     * @param  string $errorType
 
184
     * @return false
 
185
     */
 
186
    protected function _throw($file, $errorType)
 
187
    {
 
188
        if ($file !== null) {
 
189
            $this->_value = $file['name'];
 
190
        }
 
191
 
 
192
        $this->_error($errorType);
 
193
        return false;
 
194
    }
 
195
}