|
1
|
|
|
<?php |
|
2
|
|
|
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */ |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* HTML class for a file upload field |
|
6
|
|
|
* |
|
7
|
|
|
* PHP versions 4 and 5 |
|
8
|
|
|
* |
|
9
|
|
|
* LICENSE: This source file is subject to version 3.01 of the PHP license |
|
10
|
|
|
* that is available through the world-wide-web at the following URI: |
|
11
|
|
|
* http://www.php.net/license/3_01.txt If you did not receive a copy of |
|
12
|
|
|
* the PHP License and are unable to obtain it through the web, please |
|
13
|
|
|
* send a note to [email protected] so we can mail you a copy immediately. |
|
14
|
|
|
* |
|
15
|
|
|
* @category HTML |
|
16
|
|
|
* @package HTML_QuickForm |
|
17
|
|
|
* @author Adam Daniel <[email protected]> |
|
18
|
|
|
* @author Bertrand Mansion <[email protected]> |
|
19
|
|
|
* @author Alexey Borzov <[email protected]> |
|
20
|
|
|
* @copyright 2001-2009 The PHP Group |
|
21
|
|
|
* @license http://www.php.net/license/3_01.txt PHP License 3.01 |
|
22
|
|
|
* @version CVS: $Id: file.php,v 1.25 2009/04/04 21:34:02 avb Exp $ |
|
23
|
|
|
* @link http://pear.php.net/package/HTML_QuickForm |
|
24
|
|
|
*/ |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* HTML class for a file upload field |
|
28
|
|
|
* |
|
29
|
|
|
* @category HTML |
|
30
|
|
|
* @package HTML_QuickForm |
|
31
|
|
|
* @author Adam Daniel <[email protected]> |
|
32
|
|
|
* @author Bertrand Mansion <[email protected]> |
|
33
|
|
|
* @author Alexey Borzov <[email protected]> |
|
34
|
|
|
* @version Release: 3.2.11 |
|
35
|
|
|
* @since 1.0 |
|
36
|
|
|
*/ |
|
37
|
|
|
class HTML_QuickForm_file extends HTML_QuickForm_input |
|
38
|
|
|
{ |
|
39
|
|
|
// {{{ properties |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Uploaded file data, from $_FILES |
|
43
|
|
|
* @var array |
|
44
|
|
|
*/ |
|
45
|
|
|
var $_value = null; |
|
46
|
|
|
|
|
47
|
|
|
// }}} |
|
48
|
|
|
// {{{ constructor |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Class constructor |
|
52
|
|
|
* |
|
53
|
|
|
* @param string Input field name attribute |
|
54
|
|
|
* @param string Input field label |
|
55
|
|
|
* @param mixed (optional)Either a typical HTML attribute string |
|
56
|
|
|
* or an associative array |
|
57
|
|
|
* @since 1.0 |
|
58
|
|
|
* @access public |
|
59
|
|
|
*/ |
|
60
|
|
|
public function __construct($elementName=null, $elementLabel=null, $attributes=null) |
|
61
|
|
|
{ |
|
62
|
|
|
parent::__construct($elementName, $elementLabel, $attributes); |
|
63
|
|
|
$this->setType('file'); |
|
64
|
|
|
} //end constructor |
|
65
|
|
|
|
|
66
|
|
|
// }}} |
|
67
|
|
|
// {{{ setSize() |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Sets size of file element |
|
71
|
|
|
* |
|
72
|
|
|
* @param int Size of file element |
|
73
|
|
|
* @since 1.0 |
|
74
|
|
|
* @access public |
|
75
|
|
|
*/ |
|
76
|
|
|
function setSize($size) |
|
77
|
|
|
{ |
|
78
|
|
|
$this->updateAttributes(array('size' => $size)); |
|
79
|
|
|
} //end func setSize |
|
80
|
|
|
|
|
81
|
|
|
// }}} |
|
82
|
|
|
// {{{ getSize() |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Returns size of file element |
|
86
|
|
|
* |
|
87
|
|
|
* @since 1.0 |
|
88
|
|
|
* @access public |
|
89
|
|
|
* @return int |
|
90
|
|
|
*/ |
|
91
|
|
|
function getSize() |
|
92
|
|
|
{ |
|
93
|
|
|
return $this->getAttribute('size'); |
|
94
|
|
|
} //end func getSize |
|
95
|
|
|
|
|
96
|
|
|
// }}} |
|
97
|
|
|
// {{{ freeze() |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Freeze the element so that only its value is returned |
|
101
|
|
|
* |
|
102
|
|
|
* @access public |
|
103
|
|
|
* @return bool |
|
104
|
|
|
*/ |
|
105
|
|
|
function freeze() |
|
106
|
|
|
{ |
|
107
|
|
|
return false; |
|
108
|
|
|
} //end func freeze |
|
109
|
|
|
|
|
110
|
|
|
// }}} |
|
111
|
|
|
// {{{ setValue() |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Sets value for file element. |
|
115
|
|
|
* |
|
116
|
|
|
* Actually this does nothing. The function is defined here to override |
|
117
|
|
|
* HTML_Quickform_input's behaviour of setting the 'value' attribute. As |
|
118
|
|
|
* no sane user-agent uses <input type="file">'s value for anything |
|
119
|
|
|
* (because of security implications) we implement file's value as a |
|
120
|
|
|
* read-only property with a special meaning. |
|
121
|
|
|
* |
|
122
|
|
|
* @param mixed Value for file element |
|
123
|
|
|
* @since 3.0 |
|
124
|
|
|
* @access public |
|
125
|
|
|
*/ |
|
126
|
|
|
function setValue($value) |
|
127
|
|
|
{ |
|
128
|
|
|
return null; |
|
129
|
|
|
} //end func setValue |
|
130
|
|
|
|
|
131
|
|
|
// }}} |
|
132
|
|
|
// {{{ getValue() |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Returns information about the uploaded file |
|
136
|
|
|
* |
|
137
|
|
|
* @since 3.0 |
|
138
|
|
|
* @access public |
|
139
|
|
|
* @return array |
|
140
|
|
|
*/ |
|
141
|
|
|
function getValue() |
|
142
|
|
|
{ |
|
143
|
|
|
return $this->_value; |
|
144
|
|
|
} // end func getValue |
|
145
|
|
|
|
|
146
|
|
|
// }}} |
|
147
|
|
|
// {{{ onQuickFormEvent() |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Called by HTML_QuickForm whenever form event is made on this element |
|
151
|
|
|
* |
|
152
|
|
|
* @param string Name of event |
|
153
|
|
|
* @param mixed event arguments |
|
154
|
|
|
* @param object calling object |
|
155
|
|
|
* @since 1.0 |
|
156
|
|
|
* @access public |
|
157
|
|
|
* @return bool |
|
158
|
|
|
*/ |
|
159
|
|
|
function onQuickFormEvent($event, $arg, &$caller) |
|
160
|
|
|
{ |
|
161
|
|
|
switch ($event) { |
|
162
|
|
|
case 'updateValue': |
|
163
|
|
|
if ($caller->getAttribute('method') == 'get') { |
|
164
|
|
|
return PEAR::raiseError('Cannot add a file upload field to a GET method form'); |
|
165
|
|
|
} |
|
166
|
|
|
$this->_value = $this->_findValue(); |
|
|
|
|
|
|
167
|
|
|
$caller->updateAttributes(array('enctype' => 'multipart/form-data')); |
|
168
|
|
|
$caller->setMaxFileSize(); |
|
169
|
|
|
break; |
|
170
|
|
|
case 'addElement': |
|
171
|
|
|
$this->onQuickFormEvent('createElement', $arg, $caller); |
|
172
|
|
|
return $this->onQuickFormEvent('updateValue', null, $caller); |
|
|
|
|
|
|
173
|
|
|
break; |
|
174
|
|
|
case 'createElement': |
|
175
|
|
|
//$className = get_class($this); |
|
176
|
|
|
//$this->$className($arg[0], $arg[1], $arg[2]); |
|
177
|
|
|
break; |
|
178
|
|
|
} |
|
179
|
|
|
return true; |
|
180
|
|
|
} // end func onQuickFormEvent |
|
181
|
|
|
|
|
182
|
|
|
// }}} |
|
183
|
|
|
// {{{ moveUploadedFile() |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* Moves an uploaded file into the destination |
|
187
|
|
|
* |
|
188
|
|
|
* @param string Destination directory path |
|
189
|
|
|
* @param string New file name |
|
190
|
|
|
* @access public |
|
191
|
|
|
* @return bool Whether the file was moved successfully |
|
192
|
|
|
*/ |
|
193
|
|
|
public function moveUploadedFile($dest, $fileName = '') |
|
194
|
|
|
{ |
|
195
|
|
|
if ($dest != '' && substr($dest, -1) != '/') { |
|
196
|
|
|
$dest .= '/'; |
|
197
|
|
|
} |
|
198
|
|
|
$fileName = ($fileName != '') ? $fileName : basename($this->_value['name']); |
|
199
|
|
|
return move_uploaded_file($this->_value['tmp_name'], $dest . $fileName); |
|
200
|
|
|
} // end func moveUploadedFile |
|
201
|
|
|
|
|
202
|
|
|
// }}} |
|
203
|
|
|
// {{{ isUploadedFile() |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* Checks if the element contains an uploaded file |
|
207
|
|
|
* |
|
208
|
|
|
* @access public |
|
209
|
|
|
* @return bool true if file has been uploaded, false otherwise |
|
210
|
|
|
*/ |
|
211
|
|
|
public function isUploadedFile() |
|
212
|
|
|
{ |
|
213
|
|
|
return self::_ruleIsUploadedFile($this->_value); |
|
214
|
|
|
} // end func isUploadedFile |
|
215
|
|
|
|
|
216
|
|
|
|
|
217
|
|
|
// }}} |
|
218
|
|
|
// {{{ _ruleIsUploadedFile() |
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* Checks if the given element contains an uploaded file |
|
222
|
|
|
* |
|
223
|
|
|
* @param array Uploaded file info (from $_FILES) |
|
224
|
|
|
* @access private |
|
225
|
|
|
* @return bool true if file has been uploaded, false otherwise |
|
226
|
|
|
*/ |
|
227
|
|
|
public static function _ruleIsUploadedFile($elementValue) |
|
228
|
|
|
{ |
|
229
|
|
|
if ((isset($elementValue['error']) && $elementValue['error'] == 0) || |
|
230
|
|
|
(!empty($elementValue['tmp_name']) && $elementValue['tmp_name'] != 'none')) { |
|
231
|
|
|
return is_uploaded_file($elementValue['tmp_name']); |
|
232
|
|
|
} else { |
|
233
|
|
|
return false; |
|
234
|
|
|
} |
|
235
|
|
|
} // end func _ruleIsUploadedFile |
|
236
|
|
|
|
|
237
|
|
|
// }}} |
|
238
|
|
|
// {{{ _ruleCheckMaxFileSize() |
|
239
|
|
|
|
|
240
|
|
|
/** |
|
241
|
|
|
* Tries to find the element value from the values array |
|
242
|
|
|
* |
|
243
|
|
|
* Needs to be redefined here as $_FILES is populated differently from |
|
244
|
|
|
* other arrays when element name is of the form foo[bar] |
|
245
|
|
|
* |
|
246
|
|
|
* @access private |
|
247
|
|
|
* @return mixed |
|
248
|
|
|
*/ |
|
249
|
|
|
function _findValue(&$values = null) |
|
250
|
|
|
{ |
|
251
|
|
|
if (empty($_FILES)) { |
|
252
|
|
|
return null; |
|
253
|
|
|
} |
|
254
|
|
|
$elementName = $this->getName(); |
|
255
|
|
|
if (isset($_FILES[$elementName])) { |
|
256
|
|
|
return $_FILES[$elementName]; |
|
257
|
|
|
} elseif (false !== ($pos = strpos($elementName, '['))) { |
|
258
|
|
|
$base = str_replace( |
|
259
|
|
|
array('\\', '\''), array('\\\\', '\\\''), |
|
260
|
|
|
substr($elementName, 0, $pos) |
|
261
|
|
|
); |
|
262
|
|
|
$idx = "['" . str_replace( |
|
263
|
|
|
array('\\', '\'', ']', '['), array('\\\\', '\\\'', '', "']['"), |
|
264
|
|
|
substr($elementName, $pos + 1, -1) |
|
265
|
|
|
) . "']"; |
|
266
|
|
|
$props = array('name', 'type', 'size', 'tmp_name', 'error'); |
|
267
|
|
|
$code = "if (!isset(\$_FILES['{$base}']['name']{$idx})) {\n" . |
|
268
|
|
|
" return null;\n" . |
|
269
|
|
|
"} else {\n" . |
|
270
|
|
|
" \$value = array();\n"; |
|
271
|
|
|
foreach ($props as $prop) { |
|
272
|
|
|
$code .= " \$value['{$prop}'] = \$_FILES['{$base}']['{$prop}']{$idx};\n"; |
|
273
|
|
|
} |
|
274
|
|
|
return eval($code . " return \$value;\n}\n"); |
|
|
|
|
|
|
275
|
|
|
} else { |
|
276
|
|
|
return null; |
|
277
|
|
|
} |
|
278
|
|
|
} |
|
279
|
|
|
} |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..