2 /* vim: set expandtab tabstop=4 shiftwidth=4: */
3 // +----------------------------------------------------------------------+
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 // | license@php.net so we can mail you a copy immediately. |
15 // +----------------------------------------------------------------------+
16 // | Authors: Adam Daniel <adaniel1@eesus.jnj.com> |
17 // | Bertrand Mansion <bmansion@mamasam.com> |
18 // +----------------------------------------------------------------------+
22 require_once("HTML/QuickForm/input.php");
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');
33 * HTML class for a file type element
35 * @author Adam Daniel <adaniel1@eesus.jnj.com>
36 * @author Bertrand Mansion <bmansion@mamasam.com>
41 class HTML_QuickForm_file
extends HTML_QuickForm_input
46 * Uploaded file data, from $_FILES
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
64 function HTML_QuickForm_file($elementName=null, $elementLabel=null, $attributes=null)
66 HTML_QuickForm_input
::HTML_QuickForm_input($elementName, $elementLabel, $attributes);
67 $this->setType('file');
74 * Sets size of file element
76 * @param int Size of file element
80 function setSize($size)
82 $this->updateAttributes(array('size' => $size));
89 * Returns size of file element
97 return $this->getAttribute('size');
104 * Freeze the element so that only its value is returned
118 * Sets value for file element.
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.
126 * @param mixed Value for file element
130 function setValue($value)
133 } //end func setValue
139 * Returns information about the uploaded file
147 return $this->_value
;
148 } // end func getValue
151 // {{{ onQuickFormEvent()
154 * Called by HTML_QuickForm whenever form event is made on this element
156 * @param string Name of event
157 * @param mixed event arguments
158 * @param object calling object
163 function onQuickFormEvent($event, $arg, &$caller)
167 if ($caller->getAttribute('method') == 'get') {
168 return PEAR
::raiseError('Cannot add a file upload field to a GET method form');
170 $this->_value
= $this->_findValue();
171 $caller->updateAttributes(array('enctype' => 'multipart/form-data'));
172 $caller->setMaxFileSize();
175 $this->onQuickFormEvent('createElement', $arg, $caller);
176 return $this->onQuickFormEvent('updateValue', null, $caller);
178 case 'createElement':
179 $className = get_class($this);
180 $this->$className($arg[0], $arg[1], $arg[2]);
184 } // end func onQuickFormEvent
187 // {{{ moveUploadedFile()
190 * Moves an uploaded file into the destination
192 * @param string Destination directory path
193 * @param string New file name
195 * @return bool Whether the file was moved successfully
197 function moveUploadedFile($dest, $fileName = '')
199 if ($dest != '' && substr($dest, -1) != '/') {
202 $fileName = ($fileName != '') ?
$fileName : basename($this->_value
['name']);
203 if (move_uploaded_file($this->_value
['tmp_name'], $dest . $fileName)) {
208 } // end func moveUploadedFile
211 // {{{ isUploadedFile()
214 * Checks if the element contains an uploaded file
217 * @return bool true if file has been uploaded, false otherwise
219 function isUploadedFile()
221 return $this->_ruleIsUploadedFile($this->_value
);
222 } // end func isUploadedFile
225 // {{{ _ruleIsUploadedFile()
228 * Checks if the given element contains an uploaded file
230 * @param array Uploaded file info (from $_FILES)
232 * @return bool true if file has been uploaded, false otherwise
234 function _ruleIsUploadedFile($elementValue)
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']);
242 } // end func _ruleIsUploadedFile
245 // {{{ _ruleCheckMaxFileSize()
248 * Checks that the file does not exceed the max file size
250 * @param array Uploaded file info (from $_FILES)
251 * @param int Max file size
253 * @return bool true if filesize is lower than maxsize, false otherwise
255 function _ruleCheckMaxFileSize($elementValue, $maxSize)
257 if (!empty($elementValue['error']) &&
258 (UPLOAD_ERR_FORM_SIZE
== $elementValue['error'] || UPLOAD_ERR_INI_SIZE
== $elementValue['error'])) {
261 if (!HTML_QuickForm_file
::_ruleIsUploadedFile($elementValue)) {
264 return ($maxSize >= @filesize
($elementValue['tmp_name']));
265 } // end func _ruleCheckMaxFileSize
268 // {{{ _ruleCheckMimeType()
271 * Checks if the given element contains an uploaded file of the right mime type
273 * @param array Uploaded file info (from $_FILES)
274 * @param mixed Mime Type (can be an array of allowed types)
276 * @return bool true if mimetype is correct, false otherwise
278 function _ruleCheckMimeType($elementValue, $mimeType)
280 if (!HTML_QuickForm_file
::_ruleIsUploadedFile($elementValue)) {
283 if (is_array($mimeType)) {
284 return in_array($elementValue['type'], $mimeType);
286 return $elementValue['type'] == $mimeType;
287 } // end func _ruleCheckMimeType
290 // {{{ _ruleCheckFileName()
293 * Checks if the given element contains an uploaded file of the filename regex
295 * @param array Uploaded file info (from $_FILES)
296 * @param string Regular expression
298 * @return bool true if name matches regex, false otherwise
300 function _ruleCheckFileName($elementValue, $regex)
302 if (!HTML_QuickForm_file
::_ruleIsUploadedFile($elementValue)) {
305 return preg_match($regex, $elementValue['name']);
306 } // end func _ruleCheckFileName
312 * Tries to find the element value from the values array
314 * Needs to be redefined here as $_FILES is populated differently from
315 * other arrays when element name is of the form foo[bar]
320 function _findValue()
322 if (empty($_FILES)) {
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" .
335 " \$value = array();\n";
336 foreach ($props as $prop) {
337 $code .= " \$value['{$prop}'] = \$_FILES['{$base}']['{$prop}']{$idx};\n";
339 return eval($code . " return \$value;\n}\n");
346 } // end class HTML_QuickForm_file