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-2010 Zend Technologies USA Inc. (http://www.zend.com) |
---|
18 | * @license http://framework.zend.com/license/new-bsd New BSD License |
---|
19 | * @version $Id: Upload.php 23570 2010-12-20 08:38:30Z mjh_ca $ |
---|
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-2010 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 => "File '%value%' exceeds the defined ini size", |
---|
57 | self::FORM_SIZE => "File '%value%' exceeds the defined form size", |
---|
58 | self::PARTIAL => "File '%value%' was only partially uploaded", |
---|
59 | self::NO_FILE => "File '%value%' was not uploaded", |
---|
60 | self::NO_TMP_DIR => "No temporary directory was found for file '%value%'", |
---|
61 | self::CANT_WRITE => "File '%value%' can't be written", |
---|
62 | self::EXTENSION => "A PHP extension returned an error while uploading the file '%value%'", |
---|
63 | self::ATTACK => "File '%value%' was illegally uploaded. This could be a possible attack", |
---|
64 | self::FILE_NOT_FOUND => "File '%value%' was not found", |
---|
65 | self::UNKNOWN => "Unknown error while uploading 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|Zend_Config $files Array of files in syntax of Zend_File_Transfer |
---|
82 | * @return void |
---|
83 | */ |
---|
84 | public function __construct($files = array()) |
---|
85 | { |
---|
86 | if ($files instanceof Zend_Config) { |
---|
87 | $files = $files->toArray(); |
---|
88 | } |
---|
89 | |
---|
90 | $this->setFiles($files); |
---|
91 | } |
---|
92 | |
---|
93 | /** |
---|
94 | * Returns the array of set files |
---|
95 | * |
---|
96 | * @param string $files (Optional) The file to return in detail |
---|
97 | * @return array |
---|
98 | * @throws Zend_Validate_Exception If file is not found |
---|
99 | */ |
---|
100 | public function getFiles($file = null) |
---|
101 | { |
---|
102 | if ($file !== null) { |
---|
103 | $return = array(); |
---|
104 | foreach ($this->_files as $name => $content) { |
---|
105 | if ($name === $file) { |
---|
106 | $return[$file] = $this->_files[$name]; |
---|
107 | } |
---|
108 | |
---|
109 | if ($content['name'] === $file) { |
---|
110 | $return[$name] = $this->_files[$name]; |
---|
111 | } |
---|
112 | } |
---|
113 | |
---|
114 | if (count($return) === 0) { |
---|
115 | require_once 'Zend/Validate/Exception.php'; |
---|
116 | throw new Zend_Validate_Exception("The file '$file' was not found"); |
---|
117 | } |
---|
118 | |
---|
119 | return $return; |
---|
120 | } |
---|
121 | |
---|
122 | return $this->_files; |
---|
123 | } |
---|
124 | |
---|
125 | /** |
---|
126 | * Sets the files to be checked |
---|
127 | * |
---|
128 | * @param array $files The files to check in syntax of Zend_File_Transfer |
---|
129 | * @return Zend_Validate_File_Upload Provides a fluent interface |
---|
130 | */ |
---|
131 | public function setFiles($files = array()) |
---|
132 | { |
---|
133 | if (count($files) === 0) { |
---|
134 | $this->_files = $_FILES; |
---|
135 | } else { |
---|
136 | $this->_files = $files; |
---|
137 | } |
---|
138 | |
---|
139 | // see ZF-10738 |
---|
140 | if (is_null($this->_files)) { |
---|
141 | $this->_files = array(); |
---|
142 | } |
---|
143 | |
---|
144 | foreach($this->_files as $file => $content) { |
---|
145 | if (!isset($content['error'])) { |
---|
146 | unset($this->_files[$file]); |
---|
147 | } |
---|
148 | } |
---|
149 | |
---|
150 | return $this; |
---|
151 | } |
---|
152 | |
---|
153 | /** |
---|
154 | * Defined by Zend_Validate_Interface |
---|
155 | * |
---|
156 | * Returns true if and only if the file was uploaded without errors |
---|
157 | * |
---|
158 | * @param string $value Single file to check for upload errors, when giving null the $_FILES array |
---|
159 | * from initialization will be used |
---|
160 | * @return boolean |
---|
161 | */ |
---|
162 | public function isValid($value, $file = null) |
---|
163 | { |
---|
164 | $this->_messages = null; |
---|
165 | if (array_key_exists($value, $this->_files)) { |
---|
166 | $files[$value] = $this->_files[$value]; |
---|
167 | } else { |
---|
168 | foreach ($this->_files as $file => $content) { |
---|
169 | if (isset($content['name']) && ($content['name'] === $value)) { |
---|
170 | $files[$file] = $this->_files[$file]; |
---|
171 | } |
---|
172 | |
---|
173 | if (isset($content['tmp_name']) && ($content['tmp_name'] === $value)) { |
---|
174 | $files[$file] = $this->_files[$file]; |
---|
175 | } |
---|
176 | } |
---|
177 | } |
---|
178 | |
---|
179 | if (empty($files)) { |
---|
180 | return $this->_throw($file, self::FILE_NOT_FOUND); |
---|
181 | } |
---|
182 | |
---|
183 | foreach ($files as $file => $content) { |
---|
184 | $this->_value = $file; |
---|
185 | switch($content['error']) { |
---|
186 | case 0: |
---|
187 | if (!is_uploaded_file($content['tmp_name'])) { |
---|
188 | $this->_throw($file, self::ATTACK); |
---|
189 | } |
---|
190 | break; |
---|
191 | |
---|
192 | case 1: |
---|
193 | $this->_throw($file, self::INI_SIZE); |
---|
194 | break; |
---|
195 | |
---|
196 | case 2: |
---|
197 | $this->_throw($file, self::FORM_SIZE); |
---|
198 | break; |
---|
199 | |
---|
200 | case 3: |
---|
201 | $this->_throw($file, self::PARTIAL); |
---|
202 | break; |
---|
203 | |
---|
204 | case 4: |
---|
205 | $this->_throw($file, self::NO_FILE); |
---|
206 | break; |
---|
207 | |
---|
208 | case 6: |
---|
209 | $this->_throw($file, self::NO_TMP_DIR); |
---|
210 | break; |
---|
211 | |
---|
212 | case 7: |
---|
213 | $this->_throw($file, self::CANT_WRITE); |
---|
214 | break; |
---|
215 | |
---|
216 | case 8: |
---|
217 | $this->_throw($file, self::EXTENSION); |
---|
218 | break; |
---|
219 | |
---|
220 | default: |
---|
221 | $this->_throw($file, self::UNKNOWN); |
---|
222 | break; |
---|
223 | } |
---|
224 | } |
---|
225 | |
---|
226 | if (count($this->_messages) > 0) { |
---|
227 | return false; |
---|
228 | } else { |
---|
229 | return true; |
---|
230 | } |
---|
231 | } |
---|
232 | |
---|
233 | /** |
---|
234 | * Throws an error of the given type |
---|
235 | * |
---|
236 | * @param string $file |
---|
237 | * @param string $errorType |
---|
238 | * @return false |
---|
239 | */ |
---|
240 | protected function _throw($file, $errorType) |
---|
241 | { |
---|
242 | if ($file !== null) { |
---|
243 | if (is_array($file) and !empty($file['name'])) { |
---|
244 | $this->_value = $file['name']; |
---|
245 | } |
---|
246 | } |
---|
247 | |
---|
248 | $this->_error($errorType); |
---|
249 | return false; |
---|
250 | } |
---|
251 | } |
---|