adding some strings
[moodle-linuxchix.git] / lib / pear / HTML / QuickForm / file.php
blob7d9000c218c72b7ae7b22b633378f65f995bad82
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 // | 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 // +----------------------------------------------------------------------+
20 // $Id$
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');
32 /**
33 * HTML class for a file type element
35 * @author Adam Daniel <adaniel1@eesus.jnj.com>
36 * @author Bertrand Mansion <bmansion@mamasam.com>
37 * @version 1.0
38 * @since PHP4.04pl1
39 * @access public
41 class HTML_QuickForm_file extends HTML_QuickForm_input
43 // {{{ properties
45 /**
46 * Uploaded file data, from $_FILES
47 * @var array
49 var $_value = null;
51 // }}}
52 // {{{ constructor
54 /**
55 * Class constructor
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
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');
68 } //end constructor
70 // }}}
71 // {{{ setSize()
73 /**
74 * Sets size of file element
76 * @param int Size of file element
77 * @since 1.0
78 * @access public
80 function setSize($size)
82 $this->updateAttributes(array('size' => $size));
83 } //end func setSize
85 // }}}
86 // {{{ getSize()
88 /**
89 * Returns size of file element
91 * @since 1.0
92 * @access public
93 * @return int
95 function getSize()
97 return $this->getAttribute('size');
98 } //end func getSize
100 // }}}
101 // {{{ freeze()
104 * Freeze the element so that only its value is returned
106 * @access public
107 * @return bool
109 function freeze()
111 return false;
112 } //end func freeze
114 // }}}
115 // {{{ setValue()
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
127 * @since 3.0
128 * @access public
130 function setValue($value)
132 return null;
133 } //end func setValue
135 // }}}
136 // {{{ getValue()
139 * Returns information about the uploaded file
141 * @since 3.0
142 * @access public
143 * @return array
145 function getValue()
147 return $this->_value;
148 } // end func getValue
150 // }}}
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
159 * @since 1.0
160 * @access public
161 * @return bool
163 function onQuickFormEvent($event, $arg, &$caller)
165 switch ($event) {
166 case 'updateValue':
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();
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;
183 return true;
184 } // end func onQuickFormEvent
186 // }}}
187 // {{{ moveUploadedFile()
190 * Moves an uploaded file into the destination
192 * @param string Destination directory path
193 * @param string New file name
194 * @access public
195 * @return bool Whether the file was moved successfully
197 function moveUploadedFile($dest, $fileName = '')
199 if ($dest != '' && substr($dest, -1) != '/') {
200 $dest .= '/';
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;
208 } // end func moveUploadedFile
210 // }}}
211 // {{{ isUploadedFile()
214 * Checks if the element contains an uploaded file
216 * @access public
217 * @return bool true if file has been uploaded, false otherwise
219 function isUploadedFile()
221 return $this->_ruleIsUploadedFile($this->_value);
222 } // end func isUploadedFile
224 // }}}
225 // {{{ _ruleIsUploadedFile()
228 * Checks if the given element contains an uploaded file
230 * @param array Uploaded file info (from $_FILES)
231 * @access private
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']);
239 } else {
240 return false;
242 } // end func _ruleIsUploadedFile
244 // }}}
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
252 * @access private
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'])) {
259 return false;
261 if (!HTML_QuickForm_file::_ruleIsUploadedFile($elementValue)) {
262 return true;
264 return ($maxSize >= @filesize($elementValue['tmp_name']));
265 } // end func _ruleCheckMaxFileSize
267 // }}}
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)
275 * @access private
276 * @return bool true if mimetype is correct, false otherwise
278 function _ruleCheckMimeType($elementValue, $mimeType)
280 if (!HTML_QuickForm_file::_ruleIsUploadedFile($elementValue)) {
281 return true;
283 if (is_array($mimeType)) {
284 return in_array($elementValue['type'], $mimeType);
286 return $elementValue['type'] == $mimeType;
287 } // end func _ruleCheckMimeType
289 // }}}
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
297 * @access private
298 * @return bool true if name matches regex, false otherwise
300 function _ruleCheckFileName($elementValue, $regex)
302 if (!HTML_QuickForm_file::_ruleIsUploadedFile($elementValue)) {
303 return true;
305 return preg_match($regex, $elementValue['name']);
306 } // end func _ruleCheckFileName
308 // }}}
309 // {{{ _findValue()
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]
317 * @access private
318 * @return mixed
320 function _findValue()
322 if (empty($_FILES)) {
323 return null;
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";
339 return eval($code . " return \$value;\n}\n");
340 } else {
341 return null;
345 // }}}
346 } // end class HTML_QuickForm_file