13 |
* |
* |
14 |
*/ |
*/ |
15 |
|
|
16 |
/** |
/** |
17 |
* This is the FileUpload FormElement which builds a |
* This is the FileUpload FormElement which builds a |
18 |
* input field of type="file". |
* input field of type="file". This automagically |
19 |
|
* handles the case of a confirmation page. |
20 |
* |
* |
21 |
* @author Dave Brondsema <dave@brondsema.net> |
* @author Dave Brondsema <dave@brondsema.net> |
22 |
* @package phpHtmlLib |
* @package phpHtmlLib |
26 |
*/ |
*/ |
27 |
class FEFile extends FEText { |
class FEFile extends FEText { |
28 |
|
|
29 |
|
|
30 |
|
/** |
31 |
|
* The $_FILES information about |
32 |
|
* the submitted file. |
33 |
|
*/ |
34 |
|
var $_files_info = array('name' => '', |
35 |
|
'type' => '', |
36 |
|
'size' => '', |
37 |
|
'tmp_name' => ''); |
38 |
|
|
39 |
|
/** |
40 |
|
* The place where we temporarily save |
41 |
|
* the file during confirmation |
42 |
|
*/ |
43 |
|
var $_temp_dir = "/tmp"; |
44 |
|
|
45 |
|
/** |
46 |
|
* Array listing mime types to check for |
47 |
|
* during validation |
48 |
|
*/ |
49 |
|
var $_valid_types = array(); |
50 |
|
|
51 |
|
/** |
52 |
|
* max size in bytes determined by user |
53 |
|
* |
54 |
|
*/ |
55 |
|
var $_max_size; |
56 |
|
|
57 |
/** |
/** |
58 |
* This function builds and returns the |
* This function builds and returns the |
59 |
* form element object |
* form element object |
61 |
* @return object |
* @return object |
62 |
*/ |
*/ |
63 |
function get_element() { |
function get_element() { |
|
|
|
64 |
$attributes = $this->_build_element_attributes(); |
$attributes = $this->_build_element_attributes(); |
65 |
$attributes["type"] = "file"; |
$attributes["type"] = "file"; |
66 |
|
|
79 |
* It validates file size and partial uploads.. |
* It validates file size and partial uploads.. |
80 |
* @param FormValidation object. |
* @param FormValidation object. |
81 |
*/ |
*/ |
82 |
function validate(&$_FormValidation) { |
function validate(&$_FormValidation) { |
83 |
switch ($_FILES[$this->get_element_name()]['error']) |
switch ($_FILES[$this->get_element_name()]['error']) { |
84 |
{ |
case 0: |
85 |
case 0: |
if ($_FILES[$this->get_element_name()]['size'] == 0) { |
86 |
if ($_FILES[$this->get_element_name()]['size'] == 0) { |
$this->set_error_message("The uploaded file was empty."); |
87 |
$this->set_error_message("The uploaded file was empty."); |
return FALSE; |
88 |
return FALSE; |
} |
89 |
} |
break; |
90 |
break; |
case 1: |
91 |
case 1: |
$this->set_error_message("The uploaded file exceeds the maximum file size, " . |
92 |
$this->set_error_message("The uploaded file exceeds the maximum file size, " . ini_get("upload_max_filesize")); |
ini_get("upload_max_filesize")); |
93 |
return FALSE; |
return FALSE; |
94 |
break; |
break; |
95 |
case 2: |
case 2: |
96 |
$this->set_error_message("The uploaded file exceeds the maximum file size, " . $_REQUEST['MAX_FILE_SIZE'] . " bytes, specified for this form."); |
$this->set_error_message("The uploaded file exceeds the maximum file size, " . |
97 |
return FALSE; |
$_REQUEST['MAX_FILE_SIZE'] . " bytes, specified for this form."); |
98 |
break; |
return FALSE; |
99 |
case 3: |
break; |
100 |
$this->set_error_message("The uploaded file was only partially uploaded."); |
case 3: |
101 |
return FALSE; |
$this->set_error_message("The uploaded file was only partially uploaded."); |
102 |
break; |
return FALSE; |
103 |
case 4: |
break; |
104 |
$this->set_error_message("No file was uploaded."); |
case 4: |
105 |
return FALSE; |
//make sure that we are required to have a result |
106 |
break; |
if ($this->is_required()) { |
107 |
case 5: |
$this->set_error_message("No file was uploaded."); |
108 |
$this->set_error_message("The uploaded file was empty."); |
return FALSE; |
109 |
return FALSE; |
} else { |
110 |
break; |
//we aren't required, |
111 |
} |
//and we don't have a value. |
112 |
|
//this is a special case of |
113 |
|
//parent::do_validation |
114 |
|
return TRUE; |
115 |
|
} |
116 |
|
break; |
117 |
|
|
118 |
|
case 5: |
119 |
|
$this->set_error_message("The uploaded file was empty."); |
120 |
|
return FALSE; |
121 |
|
break; |
122 |
|
} |
123 |
|
|
124 |
|
if (!empty($this->_valid_types)) { |
125 |
|
if (!in_array($_FILES[$this->get_element_name()]['type'], $this->_valid_types)) { |
126 |
|
$this->set_error_message('Not a valid file type.'); |
127 |
|
return FALSE; |
128 |
|
} |
129 |
|
} |
130 |
|
|
131 |
|
if (!empty($this->_max_size)) { |
132 |
|
if ($_FILES[$this->get_element_name()]['size'] > $this->_max_size) { |
133 |
|
$this->set_error_message('File can be no larger than ' . $this->_max_size . ' bytes'); |
134 |
|
return FALSE; |
135 |
|
} |
136 |
|
} |
137 |
|
|
138 |
return TRUE; |
return TRUE; |
139 |
} |
} |
140 |
|
|
141 |
/** |
/** |
142 |
* This function will return the |
* This function will return the |
143 |
* elements value |
* elements value |
144 |
* |
* |
145 |
* @return mixed |
* @return mixed |
146 |
*/ |
*/ |
147 |
function get_value() { |
function get_value() { |
148 |
return @$_FILES[$this->get_element_name()]['name']; |
//do we need to repopulate |
149 |
} |
//the file_info? |
150 |
|
if (isset($_REQUEST[FORM_CONFIRM]) && empty($this->_files_info['name'])) { |
151 |
|
$this->_populate_file_info(); |
152 |
|
|
153 |
|
//let the FormContent do what they want. |
154 |
|
return $this->_files_info; |
155 |
|
} else { |
156 |
|
if (isset($_FILES[$this->get_element_name()]['name'])) { |
157 |
|
return $_FILES[$this->get_element_name()]['name']; |
158 |
|
} else { |
159 |
|
return ''; |
160 |
|
} |
161 |
|
} |
162 |
|
} |
163 |
|
|
164 |
|
/** |
165 |
|
* |
166 |
|
* @param mixed - the value to look up |
167 |
|
* @return string - the text associated |
168 |
|
*/ |
169 |
|
function get_value_text() { |
170 |
|
if (empty($this->_files_info['name'])) { |
171 |
|
return ''; |
172 |
|
} else { |
173 |
|
return $this->_files_info['name']." (". |
174 |
|
$this->_files_info['type'].") ". |
175 |
|
$this->_files_info['size']." bytes"; |
176 |
|
} |
177 |
|
} |
178 |
|
|
179 |
|
/** |
180 |
|
* This is so we can save the file information |
181 |
|
* in a hidden form field during confirmation |
182 |
|
* page. |
183 |
|
* |
184 |
|
* @return string |
185 |
|
*/ |
186 |
|
function get_pre_confirm_value() { |
187 |
|
return urlencode(serialize($this->_files_info)); |
188 |
|
} |
189 |
|
|
190 |
|
|
191 |
/** |
|
192 |
|
/** |
193 |
* This function will return this file's portion of the $_FILES array |
* This function will return this file's portion of the $_FILES array |
194 |
* |
* |
195 |
* @return array |
* @return array |
196 |
*/ |
*/ |
197 |
function get_file_info() { |
function get_file_info() { |
198 |
return $_FILES[$this->get_element_name()]; |
return $_FILES[$this->get_element_name()]; |
199 |
} |
} |
200 |
|
|
201 |
|
/** |
202 |
|
* This is so the user can set the temp directory |
203 |
|
* where the file will be saved during confirmation |
204 |
|
* (if any) |
205 |
|
* |
206 |
|
* @param string the new temp dir. |
207 |
|
*/ |
208 |
|
function set_temp_dir($dir) { |
209 |
|
$this->_temp_dir = $dir; |
210 |
|
} |
211 |
|
|
212 |
|
/** |
213 |
|
* This is so the user can get the temp directory |
214 |
|
* where the file was saved during confirmation |
215 |
|
* (if any) |
216 |
|
* |
217 |
|
* @return string the new temp dir. |
218 |
|
*/ |
219 |
|
function get_temp_dir() { |
220 |
|
return $this->_temp_dir; |
221 |
|
} |
222 |
|
|
223 |
|
|
224 |
|
/** |
225 |
|
* The function that allows us to save the |
226 |
|
* temp file someplace we can use it after |
227 |
|
* a confirmation has been accepted. |
228 |
|
* |
229 |
|
* NOTE: if the user doesn't confirm, then |
230 |
|
* this could leave files in /tmp |
231 |
|
* |
232 |
|
*/ |
233 |
|
function _pre_confirm() { |
234 |
|
$name = $this->get_element_name(); |
235 |
|
if (!empty($_FILES[$name]['name'])) { |
236 |
|
//ok we need to move the file so that the web |
237 |
|
//server doesn't nuke it once the request is done. |
238 |
|
$this->_files_info['name'] = $_FILES[$name]["name"]; |
239 |
|
$this->_files_info['type'] = $_FILES[$name]['type']; |
240 |
|
$this->_files_info['size'] = $_FILES[$name]['size']; |
241 |
|
|
242 |
|
$tempname = tempnam($this->_temp_dir, get_class($this)); |
243 |
|
$this->_files_info['tmp_name'] = str_replace($this->_temp_dir, '', $tempname); |
244 |
|
rename($_FILES[$name]['tmp_name'], $tempname); |
245 |
|
} |
246 |
|
|
247 |
|
return TRUE; |
248 |
|
} |
249 |
|
|
250 |
|
/** |
251 |
|
* This method re-populates the $this->_files_info |
252 |
|
* because the form had a confirmation page, |
253 |
|
* and the user accepted the confirmation. |
254 |
|
* |
255 |
|
* This is so we can re populate the local private var |
256 |
|
* so we can get access at the file data on disk. |
257 |
|
* |
258 |
|
*/ |
259 |
|
function _populate_file_info() { |
260 |
|
$this->_files_info = unserialize(urldecode($this->_value)); |
261 |
|
$this->_value = $this->_files_info['name']; |
262 |
|
$this->_files_info['tmp_name'] = $this->_temp_dir.$this->_files_info['tmp_name']; |
263 |
|
} |
264 |
|
|
265 |
|
|
266 |
|
/** |
267 |
|
* This method returns the hidden version of this |
268 |
|
* element for a confirmation page. |
269 |
|
* |
270 |
|
* NOTE: This is called by the FormProcessor only. |
271 |
|
* It shouldn't be called manually. |
272 |
|
* |
273 |
|
* @return INPUTtag of type hidden |
274 |
|
*/ |
275 |
|
function get_confirm_element() { |
276 |
|
return form_hidden($this->get_element_name(), |
277 |
|
$this->get_pre_confirm_value() ); |
278 |
|
} |
279 |
|
|
280 |
|
/** |
281 |
|
* This is so the user can create a list of mime types |
282 |
|
* to allow |
283 |
|
* |
284 |
|
* @param string a mime type to check for |
285 |
|
*/ |
286 |
|
function add_valid_type($mime_type) { |
287 |
|
$this->_valid_types[] = $mime_type; |
288 |
|
} |
289 |
|
|
290 |
|
/** |
291 |
|
* This allows the user to set the max size of the file |
292 |
|
* |
293 |
|
* @param integer max size in bytes |
294 |
|
*/ |
295 |
|
function set_max_size($max_size) { |
296 |
|
$this->_max_size = $max_size; |
297 |
|
} |
298 |
|
|
299 |
|
/** |
300 |
|
* This allows the user to get the max size of the file |
301 |
|
* (if any has been set) |
302 |
|
* |
303 |
|
* @return integer value of $_max_size if any |
304 |
|
*/ |
305 |
|
function get_max_size($max_size) { |
306 |
|
return $this->_max_size; |
307 |
|
} |
308 |
} |
} |
309 |
|
?> |