[ Index ] |
PHP Cross Reference of moodle-2.8 |
[Summary view] [Print] [Text view]
1 <?php 2 /* vim: set expandtab tabstop=4 shiftwidth=4: */ 3 // +----------------------------------------------------------------------+ 4 // | PHP version 4.0 | 5 // +----------------------------------------------------------------------+ 6 // | Copyright (c) 1997, 1998, 1999, 2000, 2001 The PHP Group | 7 // +----------------------------------------------------------------------+ 8 // | This source file is subject to version 2.0 of the PHP license, | 9 // | that is bundled with this package in the file LICENSE, and is | 10 // | available at through the world-wide-web at | 11 // | http://www.php.net/license/2_02.txt. | 12 // | If you did not receive a copy of the PHP license and are unable to | 13 // | obtain it through the world-wide-web, please send a note to | 14 // | [email protected] so we can mail you a copy immediately. | 15 // +----------------------------------------------------------------------+ 16 // | Authors: Adam Daniel <[email protected]> | 17 // | Bertrand Mansion <[email protected]> | 18 // +----------------------------------------------------------------------+ 19 // 20 // $Id$ 21 22 require_once("HTML/QuickForm/input.php"); 23 24 // register file-related rules 25 if (class_exists('HTML_QuickForm')) { 26 HTML_QuickForm::registerRule('uploadedfile', 'callback', '_ruleIsUploadedFile', 'HTML_QuickForm_file'); 27 HTML_QuickForm::registerRule('maxfilesize', 'callback', '_ruleCheckMaxFileSize', 'HTML_QuickForm_file'); 28 HTML_QuickForm::registerRule('mimetype', 'callback', '_ruleCheckMimeType', 'HTML_QuickForm_file'); 29 HTML_QuickForm::registerRule('filename', 'callback', '_ruleCheckFileName', 'HTML_QuickForm_file'); 30 } 31 32 /** 33 * HTML class for a file type element 34 * 35 * @author Adam Daniel <[email protected]> 36 * @author Bertrand Mansion <[email protected]> 37 * @version 1.0 38 * @since PHP4.04pl1 39 * @access public 40 */ 41 class HTML_QuickForm_file extends HTML_QuickForm_input 42 { 43 // {{{ properties 44 45 /** 46 * Uploaded file data, from $_FILES 47 * @var array 48 */ 49 var $_value = null; 50 51 // }}} 52 // {{{ constructor 53 54 /** 55 * Class constructor 56 * 57 * @param string Input field name attribute 58 * @param string Input field label 59 * @param mixed (optional)Either a typical HTML attribute string 60 * or an associative array 61 * @since 1.0 62 * @access public 63 */ 64 function HTML_QuickForm_file($elementName=null, $elementLabel=null, $attributes=null) 65 { 66 HTML_QuickForm_input::HTML_QuickForm_input($elementName, $elementLabel, $attributes); 67 $this->setType('file'); 68 } //end constructor 69 70 // }}} 71 // {{{ setSize() 72 73 /** 74 * Sets size of file element 75 * 76 * @param int Size of file element 77 * @since 1.0 78 * @access public 79 */ 80 function setSize($size) 81 { 82 $this->updateAttributes(array('size' => $size)); 83 } //end func setSize 84 85 // }}} 86 // {{{ getSize() 87 88 /** 89 * Returns size of file element 90 * 91 * @since 1.0 92 * @access public 93 * @return int 94 */ 95 function getSize() 96 { 97 return $this->getAttribute('size'); 98 } //end func getSize 99 100 // }}} 101 // {{{ freeze() 102 103 /** 104 * Freeze the element so that only its value is returned 105 * 106 * @access public 107 * @return bool 108 */ 109 function freeze() 110 { 111 return false; 112 } //end func freeze 113 114 // }}} 115 // {{{ setValue() 116 117 /** 118 * Sets value for file element. 119 * 120 * Actually this does nothing. The function is defined here to override 121 * HTML_Quickform_input's behaviour of setting the 'value' attribute. As 122 * no sane user-agent uses <input type="file">'s value for anything 123 * (because of security implications) we implement file's value as a 124 * read-only property with a special meaning. 125 * 126 * @param mixed Value for file element 127 * @since 3.0 128 * @access public 129 */ 130 function setValue($value) 131 { 132 return null; 133 } //end func setValue 134 135 // }}} 136 // {{{ getValue() 137 138 /** 139 * Returns information about the uploaded file 140 * 141 * @since 3.0 142 * @access public 143 * @return array 144 */ 145 function getValue() 146 { 147 return $this->_value; 148 } // end func getValue 149 150 // }}} 151 // {{{ onQuickFormEvent() 152 153 /** 154 * Called by HTML_QuickForm whenever form event is made on this element 155 * 156 * @param string Name of event 157 * @param mixed event arguments 158 * @param object calling object 159 * @since 1.0 160 * @access public 161 * @return bool 162 */ 163 function onQuickFormEvent($event, $arg, &$caller) 164 { 165 switch ($event) { 166 case 'updateValue': 167 if ($caller->getAttribute('method') == 'get') { 168 return self::raiseError('Cannot add a file upload field to a GET method form'); 169 } 170 $this->_value = $this->_findValue(); 171 $caller->updateAttributes(array('enctype' => 'multipart/form-data')); 172 $caller->setMaxFileSize(); 173 break; 174 case 'addElement': 175 $this->onQuickFormEvent('createElement', $arg, $caller); 176 return $this->onQuickFormEvent('updateValue', null, $caller); 177 break; 178 case 'createElement': 179 $className = get_class($this); 180 $this->$className($arg[0], $arg[1], $arg[2]); 181 break; 182 } 183 return true; 184 } // end func onQuickFormEvent 185 186 // }}} 187 // {{{ moveUploadedFile() 188 189 /** 190 * Moves an uploaded file into the destination 191 * 192 * @param string Destination directory path 193 * @param string New file name 194 * @access public 195 * @return bool Whether the file was moved successfully 196 */ 197 function moveUploadedFile($dest, $fileName = '') 198 { 199 if ($dest != '' && substr($dest, -1) != '/') { 200 $dest .= '/'; 201 } 202 $fileName = ($fileName != '') ? $fileName : basename($this->_value['name']); 203 if (move_uploaded_file($this->_value['tmp_name'], $dest . $fileName)) { 204 return true; 205 } else { 206 return false; 207 } 208 } // end func moveUploadedFile 209 210 // }}} 211 // {{{ isUploadedFile() 212 213 /** 214 * Checks if the element contains an uploaded file 215 * 216 * @access public 217 * @return bool true if file has been uploaded, false otherwise 218 */ 219 function isUploadedFile() 220 { 221 return $this->_ruleIsUploadedFile($this->_value); 222 } // end func isUploadedFile 223 224 // }}} 225 // {{{ _ruleIsUploadedFile() 226 227 /** 228 * Checks if the given element contains an uploaded file 229 * 230 * @param array Uploaded file info (from $_FILES) 231 * @access private 232 * @return bool true if file has been uploaded, false otherwise 233 */ 234 function _ruleIsUploadedFile($elementValue) 235 { 236 if ((isset($elementValue['error']) && $elementValue['error'] == 0) || 237 (!empty($elementValue['tmp_name']) && $elementValue['tmp_name'] != 'none')) { 238 return is_uploaded_file($elementValue['tmp_name']); 239 } else { 240 return false; 241 } 242 } // end func _ruleIsUploadedFile 243 244 // }}} 245 // {{{ _ruleCheckMaxFileSize() 246 247 /** 248 * Checks that the file does not exceed the max file size 249 * 250 * @param array Uploaded file info (from $_FILES) 251 * @param int Max file size 252 * @access private 253 * @return bool true if filesize is lower than maxsize, false otherwise 254 */ 255 function _ruleCheckMaxFileSize($elementValue, $maxSize) 256 { 257 if (!empty($elementValue['error']) && 258 (UPLOAD_ERR_FORM_SIZE == $elementValue['error'] || UPLOAD_ERR_INI_SIZE == $elementValue['error'])) { 259 return false; 260 } 261 if (!HTML_QuickForm_file::_ruleIsUploadedFile($elementValue)) { 262 return true; 263 } 264 return ($maxSize >= @filesize($elementValue['tmp_name'])); 265 } // end func _ruleCheckMaxFileSize 266 267 // }}} 268 // {{{ _ruleCheckMimeType() 269 270 /** 271 * Checks if the given element contains an uploaded file of the right mime type 272 * 273 * @param array Uploaded file info (from $_FILES) 274 * @param mixed Mime Type (can be an array of allowed types) 275 * @access private 276 * @return bool true if mimetype is correct, false otherwise 277 */ 278 function _ruleCheckMimeType($elementValue, $mimeType) 279 { 280 if (!HTML_QuickForm_file::_ruleIsUploadedFile($elementValue)) { 281 return true; 282 } 283 if (is_array($mimeType)) { 284 return in_array($elementValue['type'], $mimeType); 285 } 286 return $elementValue['type'] == $mimeType; 287 } // end func _ruleCheckMimeType 288 289 // }}} 290 // {{{ _ruleCheckFileName() 291 292 /** 293 * Checks if the given element contains an uploaded file of the filename regex 294 * 295 * @param array Uploaded file info (from $_FILES) 296 * @param string Regular expression 297 * @access private 298 * @return bool true if name matches regex, false otherwise 299 */ 300 function _ruleCheckFileName($elementValue, $regex) 301 { 302 if (!HTML_QuickForm_file::_ruleIsUploadedFile($elementValue)) { 303 return true; 304 } 305 return preg_match($regex, $elementValue['name']); 306 } // end func _ruleCheckFileName 307 308 // }}} 309 // {{{ _findValue() 310 311 /** 312 * Tries to find the element value from the values array 313 * 314 * Needs to be redefined here as $_FILES is populated differently from 315 * other arrays when element name is of the form foo[bar] 316 * 317 * @access private 318 * @return mixed 319 */ 320 function _findValue() 321 { 322 if (empty($_FILES)) { 323 return null; 324 } 325 $elementName = $this->getName(); 326 if (isset($_FILES[$elementName])) { 327 return $_FILES[$elementName]; 328 } elseif (false !== ($pos = strpos($elementName, '['))) { 329 $base = substr($elementName, 0, $pos); 330 $idx = "['" . str_replace(array(']', '['), array('', "']['"), substr($elementName, $pos + 1, -1)) . "']"; 331 $props = array('name', 'type', 'size', 'tmp_name', 'error'); 332 $code = "if (!isset(\$_FILES['{$base}']['name']{$idx})) {\n" . 333 " return null;\n" . 334 "} else {\n" . 335 " \$value = array();\n"; 336 foreach ($props as $prop) { 337 $code .= " \$value['{$prop}'] = \$_FILES['{$base}']['{$prop}']{$idx};\n"; 338 } 339 return eval($code . " return \$value;\n}\n"); 340 } else { 341 return null; 342 } 343 } 344 345 // }}} 346 } // end class HTML_QuickForm_file 347 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Fri Nov 28 20:29:05 2014 | Cross-referenced by PHPXref 0.7.1 |