cvs: pear /HTML_QuickForm QuickForm.php /HTML_QuickForm/QuickForm file.php
| From: | Alexey Borzov | Date: | Sat, 14 Jun 2003 16:26:01 +0000 |
| Subject: | cvs: pear /HTML_QuickForm QuickForm.php /HTML_QuickForm/QuickForm file.php | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-10961@lists.php.net to get a copy of this message | ||
avb Sat Jun 14 12:26:01 2003 EDT
Modified files:
/pear/HTML_QuickForm QuickForm.php
/pear/HTML_QuickForm/QuickForm file.php
Log:
* registerRule() should work now :]
* File-related rules are moved to HTML_QuickForm_file (automatically registered on include)
* isUploadedFile() and moveUploadedFile() added to HTML_QuickForm_file
* HTML_QuickForm's isUploadedFile(), moveUploadedFile(), getUploadeFile() are now deprecated
Index: pear/HTML_QuickForm/QuickForm.php diff -u pear/HTML_QuickForm/QuickForm.php:1.101 pear/HTML_QuickForm/QuickForm.php:1.102 --- pear/HTML_QuickForm/QuickForm.php:1.101 Mon Jun 9 11:05:26 2003 +++ pear/HTML_QuickForm/QuickForm.php Sat Jun 14 12:26:01 2003 @@ -17,7 +17,7 @@ // | Bertrand Mansion <bmansion@mamasam.com> | // +----------------------------------------------------------------------+ // -// $Id: QuickForm.php,v 1.101 2003/06/09 15:05:26 avb Exp $ +// $Id: QuickForm.php,v 1.102 2003/06/14 16:26:01 avb Exp $ require_once('PEAR.php'); require_once('HTML/Common.php'); @@ -58,11 +58,7 @@ 'alphanumeric' =>array('regex', '/^[a-zA-Z0-9]+$/'), 'numeric' =>array('regex', '/(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/'), 'nopunctuation' =>array('regex', '/^[^().\/\*\^\?#!@$%+=,\"\'><~\[\]{}]+$/'), - 'nonzero' =>array('regex', '/^[1-9][0-9]+/'), - 'uploadedfile' =>array('function', '_ruleIsUploadedFile'), - 'maxfilesize' =>array('function', '_ruleCheckMaxFileSize'), - 'mimetype' =>array('function', '_ruleCheckMimeType'), - 'filename' =>array('function', '_ruleCheckFileName') + 'nonzero' =>array('regex', '/^[1-9][0-9]+/') ); // {{{ error codes @@ -295,7 +291,7 @@ */ function registerRule($ruleName, $type, $data1, $data2=null) { - $GLOBALS['_HTML_QuickForm_registered_rules'] = array($type, $data1, $data2); + $GLOBALS['_HTML_QuickForm_registered_rules'][$ruleName] = array($type, $data1, $data2); } // end func registerRule // }}} @@ -394,12 +390,13 @@ // {{{ moveUploadedFile() /** - * Moves an uploaded file into the destination + * Moves an uploaded file into the destination (DEPRECATED) * @param string $element Element name * @param string $dest Destination directory path * @param string $fileName (optional) New file name * @since 1.0 * @access public + * @deprecated Use HTML_QuickForm_file::moveUploadedFile() method */ function moveUploadedFile($element, $dest, $fileName='') { @@ -457,26 +454,33 @@ // {{{ isUploadedFile() /** - * Checks if the given element contains an uploaded file + * Checks if the given element contains an uploaded file (DEPRECATED) * * @param string $element Element name * @since 2.10 * @access public * @return bool true if file has been uploaded, false otherwise + * @deprecated Use HTML_QuickForm_file::isUploadedFile() method */ function isUploadedFile($element) { - return $this->_ruleIsUploadedFile($element); + if (!$this->elementExists($element) || 'file' != $this->getElementType($element)) { + return false; + } else { + $elementObject =& $this->getElement($element); + return $elementObject->isUploadedFile(); + } } // end func isUploadedFile // }}} // {{{ getUploadedFile() /** - * Returns temporary filename of uploaded file + * Returns temporary filename of uploaded file (DEPRECATED) * @param string $element * @since 2.10 * @access public + * @deprecated Use either of HTML_QuickForm_file::getValue(), HTML_QuickForm::getElementValue(), HTML_QuickForm::getSubmitValue() methods to access this information */ function getUploadedFile($element) { @@ -1367,15 +1371,6 @@ } foreach ($this->_rules as $target => $rules) { - $submitValue = null; - $elementType = $this->getElementType($target); - if ($elementType == 'file'&& - !$this->isUploadedFile($target) && - !$this->isElementRequired($target)) { - // File is not required so do not validate further - continue; - } - $submitValue = $this->_findElementValue($target); foreach ($rules as $rule) { @@ -1515,98 +1510,6 @@ } } // end func _validateGroup - // }}} - // {{{ _ruleIsUploadedFile() - - /** - * Checks if the given element contains an uploaded file - * - * @param string $element Element name - * @since 1.1 - * @access private - * @return bool true if file has been uploaded, false otherwise - * @throws - */ - function _ruleIsUploadedFile($element) - { - $file =& $this->_submitFiles[$element]; - if ((isset($file['error']) && $file['error'] == 0) || - (!empty($file['tmp_name']) && $file['tmp_name'] != 'none')) { - return is_uploaded_file($file['tmp_name']); - } else { - return false; - } - } // end func _ruleIsUploadedFile - - // }}} - // {{{ _ruleCheckMaxFileSize() - - /** - * Checks that the file does not exceed the max file size - * - * @param string $element Element name - * @param mixed $value Element value - * @param int $maxSize Max file size - * @since 1.1 - * @access private - * @return bool true if filesize is lower than maxsize, false otherwise - * @throws - */ - function _ruleCheckMaxFileSize($element, $value, $maxSize) - { - if (empty($this->_submitFiles[$element]['tmp_name'])) { - return true; - } - return ($maxSize >= @filesize($this->_submitFiles[$element]['tmp_name'])); - } // end func _ruleCheckMaxFileSize - - // }}} - // {{{ _ruleCheckMimeType() - - /** - * Checks if the given element contains an uploaded file of the right mime type - * - * @param string $element Element name - * @param mixed $value Element value - * @param mixed $mimeType Mime Type (can be an array of allowed types) - * @since 1.1 - * @access private - * @return bool true if mimetype is correct, false otherwise - * @throws - */ - function _ruleCheckMimeType($element, $value, $mimeType) - { - if (empty($this->_submitFiles[$element]['tmp_name'])) { - return true; - } - if (is_array($mimeType)) { - return in_array($this->_submitFiles[$element]['type'], $mimeType); - } - return $this->_submitFiles[$element]['type'] == $mimeType; - } // end func _ruleCheckMimeType - - // }}} - // {{{ _ruleCheckFileName() - - /** - * Checks if the given element contains an uploaded file of the filename regex - * - * @param string $element Element name - * @param mixed $value Element value - * @param string $regex Regular expression - * @since 1.1 - * @access private - * @return bool true if name matches regex, false otherwise - * @throws - */ - function _ruleCheckFileName($element, $value, $regex) - { - if (empty($this->_submitFiles[$element]['tmp_name'])) { - return true; - } - return preg_match($regex, $this->_submitFiles[$element]['name']); - } // end func _ruleCheckFileName - // }}} // {{{ freeze() Index: pear/HTML_QuickForm/QuickForm/file.php diff -u pear/HTML_QuickForm/QuickForm/file.php:1.11 pear/HTML_QuickForm/QuickForm/file.php:1.12 --- pear/HTML_QuickForm/QuickForm/file.php:1.11 Wed May 21 08:17:29 2003 +++ pear/HTML_QuickForm/QuickForm/file.php Sat Jun 14 12:26:01 2003 @@ -17,9 +17,18 @@ // | Bertrand Mansion <bmansion@mamasam.com> | // +----------------------------------------------------------------------+ // -// $Id: file.php,v 1.11 2003/05/21 12:17:29 mansion Exp $ +// $Id: file.php,v 1.12 2003/06/14 16:26:01 avb Exp $ + require_once("HTML/QuickForm/input.php"); +// register file-related rules +if (class_exists('HTML_QuickForm')) { + HTML_QuickForm::registerRule('uploadedfile', 'function', '_ruleIsUploadedFile', 'HTML_QuickForm_file'); + HTML_QuickForm::registerRule('maxfilesize', 'function', '_ruleCheckMaxFileSize', 'HTML_QuickForm_file'); + HTML_QuickForm::registerRule('mimetype', 'function', '_ruleCheckMimeType', 'HTML_QuickForm_file'); + HTML_QuickForm::registerRule('filename', 'function', '_ruleCheckFileName', 'HTML_QuickForm_file'); +} + /** * HTML class for a file type element * @@ -45,14 +54,12 @@ /** * Class constructor * - * @param string $elementName Input field name attribute - * @param string $elementLabel Input field label - * @param mixed $attributes (optional)Either a typical HTML attribute string - * or an associative array + * @param string Input field name attribute + * @param string Input field label + * @param mixed (optional)Either a typical HTML attribute string + * or an associative array * @since 1.0 * @access public - * @return void - * @throws */ function HTML_QuickForm_file($elementName=null, $elementLabel=null, $attributes=null) { @@ -66,15 +73,13 @@ /** * Sets size of file element * - * @param int $size Size of password field + * @param int Size of file element * @since 1.0 * @access public - * @return void - * @throws */ function setSize($size) { - $this->updateAttributes(array("size"=>$size)); + $this->updateAttributes(array('size' => $size)); } //end func setSize // }}} @@ -85,12 +90,11 @@ * * @since 1.0 * @access public - * @return void - * @throws + * @return int */ function getSize() { - return $this->getAttribute("size"); + return $this->getAttribute('size'); } //end func getSize // }}} @@ -100,8 +104,7 @@ * Freeze the element so that only its value is returned * * @access public - * @return void - * @throws + * @return bool */ function freeze() { @@ -120,11 +123,9 @@ * (because of security implications) we implement file's value as a * read-only property with a special meaning. * - * @param mixed $value Value for file element + * @param mixed Value for file element * @since 3.0 * @access public - * @return void - * @throws */ function setValue($value) { @@ -140,7 +141,6 @@ * @since 3.0 * @access public * @return array - * @throws */ function getValue() { @@ -153,20 +153,19 @@ /** * Called by HTML_QuickForm whenever form event is made on this element * - * @param string $event Name of event - * @param mixed $arg event arguments - * @param object $caller calling object + * @param string Name of event + * @param mixed event arguments + * @param object calling object * @since 1.0 * @access public - * @return void - * @throws + * @return bool */ function onQuickFormEvent($event, $arg, &$caller) { switch ($event) { case 'updateValue': if ($caller->getAttribute('method') == 'get') { - $caller->_submitValues = $GLOBALS['_POST']; + $caller->_submitValues = $_POST; } $this->_value = $this->_findValue($caller->_submitFiles); $caller->updateAttributes(array('method' => 'post', 'enctype' => 'multipart/form-data')); @@ -184,6 +183,123 @@ return true; } // end func onQuickFormEvent + // }}} + // {{{ moveUploadedFile() + + /** + * Moves an uploaded file into the destination + * + * @param string Destination directory path + * @param string New file name + * @access public + */ + function moveUploadedFile($dest, $fileName = '') + { + if ($dest != '' && substr($dest, -1) != '/') { + $dest .= '/'; + } + $fileName = ($fileName != '') ? $fileName : $this->_value['name']; + if (move_uploaded_file($this->_value['tmp_name'], $dest . $fileName)) { + return true; + } else { + return false; + } + } // end func moveUploadedFile + + // }}} + // {{{ isUploadedFile() + + /** + * Checks if the element contains an uploaded file + * + * @access public + * @return bool true if file has been uploaded, false otherwise + */ + function isUploadedFile() + { + return $this->_ruleIsUploadedFile($this->_value); + } // end func isUploadedFile + + // }}} + // {{{ _ruleIsUploadedFile() + + /** + * Checks if the given element contains an uploaded file + * + * @param array Uploaded file info (from $_FILES) + * @access private + * @return bool true if file has been uploaded, false otherwise + */ + function _ruleIsUploadedFile($elementValue) + { + if ((isset($elementValue['error']) && $elementValue['error'] == 0) || + (!empty($elementValue['tmp_name']) && $elementValue['tmp_name'] != 'none')) { + return is_uploaded_file($elementValue['tmp_name']); + } else { + return false; + } + } // end func _ruleIsUploadedFile + + // }}} + // {{{ _ruleCheckMaxFileSize() + + /** + * Checks that the file does not exceed the max file size + * + * @param array Uploaded file info (from $_FILES) + * @param int Max file size + * @access private + * @return bool true if filesize is lower than maxsize, false otherwise + */ + function _ruleCheckMaxFileSize($elementValue, $maxSize) + { + if (!$this->_ruleIsUploadedFile($elementValue)) { + return true; + } + return ($maxSize >= @filesize($elementValue['tmp_name'])); + } // end func _ruleCheckMaxFileSize + + // }}} + // {{{ _ruleCheckMimeType() + + /** + * Checks if the given element contains an uploaded file of the right mime type + * + * @param array Uploaded file info (from $_FILES) + * @param mixed Mime Type (can be an array of allowed types) + * @access private + * @return bool true if mimetype is correct, false otherwise + */ + function _ruleCheckMimeType($elementValue, $mimeType) + { + if (!$this->_ruleIsUploadedFile($elementValue)) { + return true; + } + if (is_array($mimeType)) { + return in_array($elementValue['type'], $mimeType); + } + return $elementValue['type'] == $mimeType; + } // end func _ruleCheckMimeType + + // }}} + // {{{ _ruleCheckFileName() + + /** + * Checks if the given element contains an uploaded file of the filename regex + * + * @param array Uploaded file info (from $_FILES) + * @param string Regular expression + * @access private + * @return bool true if name matches regex, false otherwise + */ + function _ruleCheckFileName($elementValue, $regex) + { + if (!$this->_ruleIsUploadedFile($elementValue)) { + return true; + } + return preg_match($regex, $elementValue['name']); + } // end func _ruleCheckFileName + // }}} } // end class HTML_QuickForm_file
Index: pear/HTML_QuickForm/QuickForm.php diff -u pear/HTML_QuickForm/QuickForm.php:1.101 pear/HTML_QuickForm/QuickForm.php:1.102 --- pear/HTML_QuickForm/QuickForm.php:1.101 Mon Jun 9 11:05:26 2003 +++ pear/HTML_QuickForm/QuickForm.php Sat Jun 14 12:26:01 2003 @@ -17,7 +17,7 @@ // | Bertrand Mansion <bmansion@mamasam.com> | // +----------------------------------------------------------------------+ // -// $Id: QuickForm.php,v 1.101 2003/06/09 15:05:26 avb Exp $ +// $Id: QuickForm.php,v 1.102 2003/06/14 16:26:01 avb Exp $ require_once('PEAR.php'); require_once('HTML/Common.php'); @@ -58,11 +58,7 @@ 'alphanumeric' =>array('regex', '/^[a-zA-Z0-9]+$/'), 'numeric' =>array('regex', '/(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/'), 'nopunctuation' =>array('regex', '/^[^().\/\*\^\?#!@$%+=,\"\'><~\[\]{}]+$/'), - 'nonzero' =>array('regex', '/^[1-9][0-9]+/'), - 'uploadedfile' =>array('function', '_ruleIsUploadedFile'), - 'maxfilesize' =>array('function', '_ruleCheckMaxFileSize'), - 'mimetype' =>array('function', '_ruleCheckMimeType'), - 'filename' =>array('function', '_ruleCheckFileName') + 'nonzero' =>array('regex', '/^[1-9][0-9]+/') ); // {{{ error codes @@ -295,7 +291,7 @@ */ function registerRule($ruleName, $type, $data1, $data2=null) { - $GLOBALS['_HTML_QuickForm_registered_rules'] = array($type, $data1, $data2); + $GLOBALS['_HTML_QuickForm_registered_rules'][$ruleName] = array($type, $data1, $data2); } // end func registerRule // }}} @@ -394,12 +390,13 @@ // {{{ moveUploadedFile() /** - * Moves an uploaded file into the destination + * Moves an uploaded file into the destination (DEPRECATED) * @param string $element Element name * @param string $dest Destination directory path * @param string $fileName (optional) New file name * @since 1.0 * @access public + * @deprecated Use HTML_QuickForm_file::moveUploadedFile() method */ function moveUploadedFile($element, $dest, $fileName='') { @@ -457,26 +454,33 @@ // {{{ isUploadedFile() /** - * Checks if the given element contains an uploaded file + * Checks if the given element contains an uploaded file (DEPRECATED) * * @param string $element Element name * @since 2.10 * @access public * @return bool true if file has been uploaded, false otherwise + * @deprecated Use HTML_QuickForm_file::isUploadedFile() method */ function isUploadedFile($element) { - return $this->_ruleIsUploadedFile($element); + if (!$this->elementExists($element) || 'file' != $this->getElementType($element)) { + return false; + } else { + $elementObject =& $this->getElement($element); + return $elementObject->isUploadedFile(); + } } // end func isUploadedFile // }}} // {{{ getUploadedFile() /** - * Returns temporary filename of uploaded file + * Returns temporary filename of uploaded file (DEPRECATED) * @param string $element * @since 2.10 * @access public + * @deprecated Use either of HTML_QuickForm_file::getValue(), HTML_QuickForm::getElementValue(), HTML_QuickForm::getSubmitValue() methods to access this information */ function getUploadedFile($element) { @@ -1367,15 +1371,6 @@ } foreach ($this->_rules as $target => $rules) { - $submitValue = null; - $elementType = $this->getElementType($target); - if ($elementType == 'file'&& - !$this->isUploadedFile($target) && - !$this->isElementRequired($target)) { - // File is not required so do not validate further - continue; - } - $submitValue = $this->_findElementValue($target); foreach ($rules as $rule) { @@ -1515,98 +1510,6 @@ } } // end func _validateGroup - // }}} - // {{{ _ruleIsUploadedFile() - - /** - * Checks if the given element contains an uploaded file - * - * @param string $element Element name - * @since 1.1 - * @access private - * @return bool true if file has been uploaded, false otherwise - * @throws - */ - function _ruleIsUploadedFile($element) - { - $file =& $this->_submitFiles[$element]; - if ((isset($file['error']) && $file['error'] == 0) || - (!empty($file['tmp_name']) && $file['tmp_name'] != 'none')) { - return is_uploaded_file($file['tmp_name']); - } else { - return false; - } - } // end func _ruleIsUploadedFile - - // }}} - // {{{ _ruleCheckMaxFileSize() - - /** - * Checks that the file does not exceed the max file size - * - * @param string $element Element name - * @param mixed $value Element value - * @param int $maxSize Max file size - * @since 1.1 - * @access private - * @return bool true if filesize is lower than maxsize, false otherwise - * @throws - */ - function _ruleCheckMaxFileSize($element, $value, $maxSize) - { - if (empty($this->_submitFiles[$element]['tmp_name'])) { - return true; - } - return ($maxSize >= @filesize($this->_submitFiles[$element]['tmp_name'])); - } // end func _ruleCheckMaxFileSize - - // }}} - // {{{ _ruleCheckMimeType() - - /** - * Checks if the given element contains an uploaded file of the right mime type - * - * @param string $element Element name - * @param mixed $value Element value - * @param mixed $mimeType Mime Type (can be an array of allowed types) - * @since 1.1 - * @access private - * @return bool true if mimetype is correct, false otherwise - * @throws - */ - function _ruleCheckMimeType($element, $value, $mimeType) - { - if (empty($this->_submitFiles[$element]['tmp_name'])) { - return true; - } - if (is_array($mimeType)) { - return in_array($this->_submitFiles[$element]['type'], $mimeType); - } - return $this->_submitFiles[$element]['type'] == $mimeType; - } // end func _ruleCheckMimeType - - // }}} - // {{{ _ruleCheckFileName() - - /** - * Checks if the given element contains an uploaded file of the filename regex - * - * @param string $element Element name - * @param mixed $value Element value - * @param string $regex Regular expression - * @since 1.1 - * @access private - * @return bool true if name matches regex, false otherwise - * @throws - */ - function _ruleCheckFileName($element, $value, $regex) - { - if (empty($this->_submitFiles[$element]['tmp_name'])) { - return true; - } - return preg_match($regex, $this->_submitFiles[$element]['name']); - } // end func _ruleCheckFileName - // }}} // {{{ freeze() Index: pear/HTML_QuickForm/QuickForm/file.php diff -u pear/HTML_QuickForm/QuickForm/file.php:1.11 pear/HTML_QuickForm/QuickForm/file.php:1.12 --- pear/HTML_QuickForm/QuickForm/file.php:1.11 Wed May 21 08:17:29 2003 +++ pear/HTML_QuickForm/QuickForm/file.php Sat Jun 14 12:26:01 2003 @@ -17,9 +17,18 @@ // | Bertrand Mansion <bmansion@mamasam.com> | // +----------------------------------------------------------------------+ // -// $Id: file.php,v 1.11 2003/05/21 12:17:29 mansion Exp $ +// $Id: file.php,v 1.12 2003/06/14 16:26:01 avb Exp $ + require_once("HTML/QuickForm/input.php"); +// register file-related rules +if (class_exists('HTML_QuickForm')) { + HTML_QuickForm::registerRule('uploadedfile', 'function', '_ruleIsUploadedFile', 'HTML_QuickForm_file'); + HTML_QuickForm::registerRule('maxfilesize', 'function', '_ruleCheckMaxFileSize', 'HTML_QuickForm_file'); + HTML_QuickForm::registerRule('mimetype', 'function', '_ruleCheckMimeType', 'HTML_QuickForm_file'); + HTML_QuickForm::registerRule('filename', 'function', '_ruleCheckFileName', 'HTML_QuickForm_file'); +} + /** * HTML class for a file type element * @@ -45,14 +54,12 @@ /** * Class constructor * - * @param string $elementName Input field name attribute - * @param string $elementLabel Input field label - * @param mixed $attributes (optional)Either a typical HTML attribute string - * or an associative array + * @param string Input field name attribute + * @param string Input field label + * @param mixed (optional)Either a typical HTML attribute string + * or an associative array * @since 1.0 * @access public - * @return void - * @throws */ function HTML_QuickForm_file($elementName=null, $elementLabel=null, $attributes=null) { @@ -66,15 +73,13 @@ /** * Sets size of file element * - * @param int $size Size of password field + * @param int Size of file element * @since 1.0 * @access public - * @return void - * @throws */ function setSize($size) { - $this->updateAttributes(array("size"=>$size)); + $this->updateAttributes(array('size' => $size)); } //end func setSize // }}} @@ -85,12 +90,11 @@ * * @since 1.0 * @access public - * @return void - * @throws + * @return int */ function getSize() { - return $this->getAttribute("size"); + return $this->getAttribute('size'); } //end func getSize // }}} @@ -100,8 +104,7 @@ * Freeze the element so that only its value is returned * * @access public - * @return void - * @throws + * @return bool */ function freeze() { @@ -120,11 +123,9 @@ * (because of security implications) we implement file's value as a * read-only property with a special meaning. * - * @param mixed $value Value for file element + * @param mixed Value for file element * @since 3.0 * @access public - * @return void - * @throws */ function setValue($value) { @@ -140,7 +141,6 @@ * @since 3.0 * @access public * @return array - * @throws */ function getValue() { @@ -153,20 +153,19 @@ /** * Called by HTML_QuickForm whenever form event is made on this element * - * @param string $event Name of event - * @param mixed $arg event arguments - * @param object $caller calling object + * @param string Name of event + * @param mixed event arguments + * @param object calling object * @since 1.0 * @access public - * @return void - * @throws + * @return bool */ function onQuickFormEvent($event, $arg, &$caller) { switch ($event) { case 'updateValue': if ($caller->getAttribute('method') == 'get') { - $caller->_submitValues = $GLOBALS['_POST']; + $caller->_submitValues = $_POST; } $this->_value = $this->_findValue($caller->_submitFiles); $caller->updateAttributes(array('method' => 'post', 'enctype' => 'multipart/form-data')); @@ -184,6 +183,123 @@ return true; } // end func onQuickFormEvent + // }}} + // {{{ moveUploadedFile() + + /** + * Moves an uploaded file into the destination + * + * @param string Destination directory path + * @param string New file name + * @access public + */ + function moveUploadedFile($dest, $fileName = '') + { + if ($dest != '' && substr($dest, -1) != '/') { + $dest .= '/'; + } + $fileName = ($fileName != '') ? $fileName : $this->_value['name']; + if (move_uploaded_file($this->_value['tmp_name'], $dest . $fileName)) { + return true; + } else { + return false; + } + } // end func moveUploadedFile + + // }}} + // {{{ isUploadedFile() + + /** + * Checks if the element contains an uploaded file + * + * @access public + * @return bool true if file has been uploaded, false otherwise + */ + function isUploadedFile() + { + return $this->_ruleIsUploadedFile($this->_value); + } // end func isUploadedFile + + // }}} + // {{{ _ruleIsUploadedFile() + + /** + * Checks if the given element contains an uploaded file + * + * @param array Uploaded file info (from $_FILES) + * @access private + * @return bool true if file has been uploaded, false otherwise + */ + function _ruleIsUploadedFile($elementValue) + { + if ((isset($elementValue['error']) && $elementValue['error'] == 0) || + (!empty($elementValue['tmp_name']) && $elementValue['tmp_name'] != 'none')) { + return is_uploaded_file($elementValue['tmp_name']); + } else { + return false; + } + } // end func _ruleIsUploadedFile + + // }}} + // {{{ _ruleCheckMaxFileSize() + + /** + * Checks that the file does not exceed the max file size + * + * @param array Uploaded file info (from $_FILES) + * @param int Max file size + * @access private + * @return bool true if filesize is lower than maxsize, false otherwise + */ + function _ruleCheckMaxFileSize($elementValue, $maxSize) + { + if (!$this->_ruleIsUploadedFile($elementValue)) { + return true; + } + return ($maxSize >= @filesize($elementValue['tmp_name'])); + } // end func _ruleCheckMaxFileSize + + // }}} + // {{{ _ruleCheckMimeType() + + /** + * Checks if the given element contains an uploaded file of the right mime type + * + * @param array Uploaded file info (from $_FILES) + * @param mixed Mime Type (can be an array of allowed types) + * @access private + * @return bool true if mimetype is correct, false otherwise + */ + function _ruleCheckMimeType($elementValue, $mimeType) + { + if (!$this->_ruleIsUploadedFile($elementValue)) { + return true; + } + if (is_array($mimeType)) { + return in_array($elementValue['type'], $mimeType); + } + return $elementValue['type'] == $mimeType; + } // end func _ruleCheckMimeType + + // }}} + // {{{ _ruleCheckFileName() + + /** + * Checks if the given element contains an uploaded file of the filename regex + * + * @param array Uploaded file info (from $_FILES) + * @param string Regular expression + * @access private + * @return bool true if name matches regex, false otherwise + */ + function _ruleCheckFileName($elementValue, $regex) + { + if (!$this->_ruleIsUploadedFile($elementValue)) { + return true; + } + return preg_match($regex, $elementValue['name']); + } // end func _ruleCheckFileName + // }}} } // end class HTML_QuickForm_file