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