~chroot64bit/zivios/gentoo-experimental

« back to all changes in this revision

Viewing changes to application/library/Zend/Validate/File/Upload.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 maximum size of a file up to a max of 2GB
 
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_Upload extends Zend_Validate_Abstract
 
36
{
 
37
    /**@#+
 
38
     * @const string Error constants
 
39
     */
 
40
    const INI_SIZE       = 'fileUploadErrorIniSize';
 
41
    const FORM_SIZE      = 'fileUploadErrorFormSize';
 
42
    const PARTIAL        = 'fileUploadErrorPartial';
 
43
    const NO_FILE        = 'fileUploadErrorNoFile';
 
44
    const NO_TMP_DIR     = 'fileUploadErrorNoTmpDir';
 
45
    const CANT_WRITE     = 'fileUploadErrorCantWrite';
 
46
    const EXTENSION      = 'fileUploadErrorExtension';
 
47
    const ATTACK         = 'fileUploadErrorAttack';
 
48
    const FILE_NOT_FOUND = 'fileUploadErrorFileNotFound';
 
49
    const UNKNOWN        = 'fileUploadErrorUnknown';
 
50
    /**@#-*/
 
51
 
 
52
    /**
 
53
     * @var array Error message templates
 
54
     */
 
55
    protected $_messageTemplates = array(
 
56
        self::INI_SIZE       => "The file '%value%' exceeds the defined ini size",
 
57
        self::FORM_SIZE      => "The file '%value%' exceeds the defined form size",
 
58
        self::PARTIAL        => "The file '%value%' was only partially uploaded",
 
59
        self::NO_FILE        => "The file '%value%' was not uploaded",
 
60
        self::NO_TMP_DIR     => "No temporary directory was found for the file '%value%'",
 
61
        self::CANT_WRITE     => "The file '%value%' can't be written",
 
62
        self::EXTENSION      => "The extension returned an error while uploading the file '%value%'",
 
63
        self::ATTACK         => "The file '%value%' was illegal uploaded, possible attack",
 
64
        self::FILE_NOT_FOUND => "The file '%value%' was not found",
 
65
        self::UNKNOWN        => "Unknown error while uploading the file '%value%'"
 
66
    );
 
67
 
 
68
    /**
 
69
     * Internal array of files
 
70
     * @var array
 
71
     */
 
72
    protected $_files = array();
 
73
 
 
74
    /**
 
75
     * Sets validator options
 
76
     *
 
77
     * The array $files must be given in syntax of Zend_File_Transfer to be checked
 
78
     * If no files are given the $_FILES array will be used automatically.
 
79
     * NOTE: This validator will only work with HTTP POST uploads!
 
80
     *
 
81
     * @param  array $files Array of files in syntax of Zend_File_Transfer
 
82
     * @return void
 
83
     */
 
84
    public function __construct($files = array())
 
85
    {
 
86
        $this->setFiles($files);
 
87
    }
 
88
 
 
89
    /**
 
90
     * Returns the array of set files
 
91
     *
 
92
     * @param  string $files (Optional) The file to return in detail
 
93
     * @return array
 
94
     * @throws Zend_Validate_Exception If file is not found
 
95
     */
 
96
    public function getFiles($file = null)
 
97
    {
 
98
        if ($file !== null) {
 
99
            $return = array();
 
100
            foreach ($this->_files as $name => $content) {
 
101
                if ($name === $file) {
 
102
                    $return[$file] = $this->_files[$name];
 
103
                }
 
104
 
 
105
                if ($content['name'] === $file) {
 
106
                    $return[$name] = $this->_files[$name];
 
107
                }
 
108
            }
 
109
 
 
110
            if (count($return) === 0) {
 
111
                require_once 'Zend/Validate/Exception.php';
 
112
                throw new Zend_Validate_Exception("The file '$file' was not found");
 
113
            }
 
114
 
 
115
            return $return;
 
116
        }
 
117
 
 
118
        return $this->_files;
 
119
    }
 
120
 
 
121
    /**
 
122
     * Sets the minimum filesize
 
123
     *
 
124
     * @param  array $files The files to check in syntax of Zend_File_Transfer
 
125
     * @return Zend_Validate_File_Upload Provides a fluent interface
 
126
     */
 
127
    public function setFiles($files = array())
 
128
    {
 
129
        if (count($files) === 0) {
 
130
            $this->_files = $_FILES;
 
131
        } else {
 
132
            $this->_files = $files;
 
133
        }
 
134
        return $this;
 
135
    }
 
136
 
 
137
    /**
 
138
     * Defined by Zend_Validate_Interface
 
139
     *
 
140
     * Returns true if and only if the file was uploaded without errors
 
141
     *
 
142
     * @param  string $value Single file to check for upload errors, when giving null the $_FILES array
 
143
     *                       from initialization will be used
 
144
     * @return boolean
 
145
     */
 
146
    public function isValid($value, $file = null)
 
147
    {
 
148
        if (array_key_exists($value, $this->_files)) {
 
149
            $files[$value] = $this->_files[$value];
 
150
        } else {
 
151
            foreach ($this->_files as $file => $content) {
 
152
                if ($content['name'] === $value) {
 
153
                    $files[$file] = $this->_files[$file];
 
154
                }
 
155
 
 
156
                if ($content['tmp_name'] === $value) {
 
157
                    $files[$file] = $this->_files[$file];
 
158
                }
 
159
            }
 
160
        }
 
161
 
 
162
        if (empty($files)) {
 
163
            return $this->_throw($file, self::FILE_NOT_FOUND);
 
164
        }
 
165
 
 
166
        foreach ($files as $file => $content) {
 
167
            $this->_value = $file;
 
168
            switch($content['error']) {
 
169
                case 0:
 
170
                    if (!is_uploaded_file($content['tmp_name'])) {
 
171
                        $this->_throw($file, self::ATTACK);
 
172
                    }
 
173
                    break;
 
174
 
 
175
                case 1:
 
176
                    $this->_throw($file, self::INI_SIZE);
 
177
                    break;
 
178
 
 
179
                case 2:
 
180
                    $this->_throw($file, self::FORM_SIZE);
 
181
                    break;
 
182
 
 
183
                case 3:
 
184
                    $this->_throw($file, self::PARTIAL);
 
185
                    break;
 
186
 
 
187
                case 4:
 
188
                    $this->_throw($file, self::NO_FILE);
 
189
                    break;
 
190
 
 
191
                case 6:
 
192
                    $this->_throw($file, self::NO_TMP_DIR);
 
193
                    break;
 
194
 
 
195
                case 7:
 
196
                    $this->_throw($file, self::CANT_WRITE);
 
197
                    break;
 
198
 
 
199
                case 8:
 
200
                    $this->_throw($file, self::EXTENSION);
 
201
                    break;
 
202
 
 
203
                default:
 
204
                    $this->_throw($file, self::UNKNOWN);
 
205
                    break;
 
206
            }
 
207
        }
 
208
 
 
209
        if (count($this->_messages) > 0) {
 
210
            return false;
 
211
        } else {
 
212
            return true;
 
213
        }
 
214
    }
 
215
 
 
216
    /**
 
217
     * Throws an error of the given type
 
218
     *
 
219
     * @param  string $file
 
220
     * @param  string $errorType
 
221
     * @return false
 
222
     */
 
223
    protected function _throw($file, $errorType)
 
224
    {
 
225
        if ($file !== null) {
 
226
            if (is_array($file) and !empty($file['name'])) {
 
227
                $this->_value = $file['name'];
 
228
            }
 
229
        }
 
230
 
 
231
        $this->_error($errorType);
 
232
        return false;
 
233
    }
 
234
}