diff --git a/.gitignore b/.gitignore index 869f498..e895efe 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,8 @@ composer.lock composer.phar vendor + +# eclipse +.buildpath +.project +.settings diff --git a/.travis.yml b/.travis.yml index 5ba3d83..c3f0051 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,17 @@ language: php install: - - pear install package.xml -php: - - 5.4 + - pear install package2.xml +matrix: + include: + - php: 5.2 + dist: precise + - php: 5.3 + dist: precise + - php: 5.4 + dist: trusty + - php: 5.5 + dist: trusty + - php: 5.6 + dist: trusty sudo: false -script: phpunit tests/ \ No newline at end of file +script: phpunit tests/ diff --git a/QuickForm.php b/QuickForm.php index ac1eecf..fee4266 100644 --- a/QuickForm.php +++ b/QuickForm.php @@ -3,7 +3,7 @@ /** * Create, validate and process HTML forms - * + * * PHP versions 4 and 5 * * LICENSE: This source file is subject to version 3.01 of the PHP license @@ -31,14 +31,18 @@ * Base class for all HTML classes */ require_once 'HTML/Common.php'; +/** + * Static utility methods + */ +require_once 'HTML/QuickForm/utils.php'; /** * Element types known to HTML_QuickForm * @see HTML_QuickForm::registerElementType(), HTML_QuickForm::getRegisteredTypes(), * HTML_QuickForm::isTypeRegistered() * @global array $GLOBALS['HTML_QUICKFORM_ELEMENT_TYPES'] - */ -$GLOBALS['HTML_QUICKFORM_ELEMENT_TYPES'] = + */ +$GLOBALS['HTML_QUICKFORM_ELEMENT_TYPES'] = array( 'group' =>array('HTML/QuickForm/group.php','HTML_QuickForm_group'), 'hidden' =>array('HTML/QuickForm/hidden.php','HTML_QuickForm_hidden'), @@ -66,7 +70,7 @@ ); /** - * Validation rules known to HTML_QuickForm + * Validation rules known to HTML_QuickForm * @see HTML_QuickForm::registerRule(), HTML_QuickForm::getRegisteredRules(), * HTML_QuickForm::isRuleRegistered() * @global array $GLOBALS['_HTML_QuickForm_registered_rules'] @@ -92,11 +96,11 @@ /**#@+ * Error codes for HTML_QuickForm * - * Codes are mapped to textual messages by errorMessage() method, if you add a + * Codes are mapped to textual messages by errorMessage() method, if you add a * new code be sure to add a new message for it to errorMessage() * * @see HTML_QuickForm::errorMessage() - */ + */ define('QUICKFORM_OK', 1); define('QUICKFORM_ERROR', -1); define('QUICKFORM_INVALID_RULE', -2); @@ -154,7 +158,7 @@ class HTML_QuickForm extends HTML_Common * @since 1.0 * @var array * @access private - */ + */ var $_required = array(); /** @@ -162,7 +166,7 @@ class HTML_QuickForm extends HTML_Common * @since 1.0 * @var string * @access public - */ + */ var $_jsPrefix = 'Invalid information entered.'; /** @@ -170,7 +174,7 @@ class HTML_QuickForm extends HTML_Common * @since 1.0 * @var string * @access public - */ + */ var $_jsPostfix = 'Please correct these fields.'; /** @@ -326,7 +330,7 @@ function HTML_QuickForm($formName='', $method='post', $action='', $target='', $a default: $this->_maxFileSize = $matches['1']; } - } + } } // end constructor // }}} @@ -476,8 +480,8 @@ function setDefaults($defaultValues = null, $filter = null) * Initializes constant form values. * These values won't get overridden by POST or GET vars * - * @param array $constantValues values used to fill the form - * @param mixed $filter (optional) filter(s) to apply to all default values + * @param array $constantValues values used to fill the form + * @param mixed $filter (optional) filter(s) to apply to all default values * * @since 2.0 * @access public @@ -553,8 +557,8 @@ function getMaxFileSize() /** * Creates a new form element of the given type. - * - * This method accepts variable number of parameters, their + * + * This method accepts variable number of parameters, their * meaning and count depending on $elementType * * @param string $elementType type of element to add (text, textarea, file...) @@ -612,9 +616,9 @@ function &_loadElement($event, $type, $args) /** * Adds an element into the form - * - * If $element is a string representing element type, then this - * method accepts variable number of parameters, their meaning + * + * If $element is a string representing element type, then this + * method accepts variable number of parameters, their meaning * and count depending on $element * * @param mixed $element element object or type of element to add (text, textarea, file...) @@ -659,7 +663,7 @@ function &addElement($element) return $elementObject; } // end func addElement - + // }}} // {{{ insertElementBefore() @@ -757,7 +761,7 @@ function &addGroup($elements, $name=null, $groupLabel='', $separator=null, $appe $group =& $this->addElement('group', $name, $groupLabel, $elements, $separator, $appendName); return $group; } // end func addGroup - + // }}} // {{{ &getElement() @@ -785,8 +789,8 @@ function &getElement($element) /** * Returns the element's raw value - * - * This returns the value as submitted by the form (not filtered) + * + * This returns the value as submitted by the form (not filtered) * or set via setDefaults() or setConstants() * * @param string $element Element name @@ -826,7 +830,7 @@ function &getElementValue($element) * @since 2.0 * @access public * @return mixed submitted element value or null if not set - */ + */ function getSubmitValue($elementName) { $value = null; @@ -843,15 +847,19 @@ function getSubmitValue($elementName) } elseif (false !== ($pos = strpos($elementName, '['))) { $base = str_replace( - array('\\', '\''), array('\\\\', '\\\''), + array('\\', '\''), array('\\\\', '\\\''), substr($elementName, 0, $pos) ); - $idx = "['" . str_replace( - array('\\', '\'', ']', '['), array('\\\\', '\\\'', '', "']['"), - substr($elementName, $pos + 1, -1) - ) . "']"; + + $keys = str_replace( + array('\\', '\'', ']', '['), array('\\\\', '\\\'', '', "']['"), + substr($elementName, $pos + 1, -1) + ); + $idx = "['" . $keys . "']"; + $keyArray = explode("']['", $keys); + if (isset($this->_submitValues[$base])) { - $value = eval("return (isset(\$this->_submitValues['{$base}']{$idx})) ? \$this->_submitValues['{$base}']{$idx} : null;"); + $value = HTML_QuickForm_utils::recursiveValue($this->_submitValues[$base], $keyArray, NULL); } if ((is_array($value) || null === $value) && isset($this->_submitFiles[$base])) { @@ -869,7 +877,7 @@ function getSubmitValue($elementName) } } } - + // This is only supposed to work for groups with appendName = false if (null === $value && 'group' == $this->getElementType($elementName)) { $group =& $this->getElement($elementName); @@ -927,7 +935,7 @@ function getElementError($element) return $this->_errors[$element]; } } // end func getElementError - + // }}} // {{{ setElementError() @@ -936,7 +944,7 @@ function getElementError($element) * * @param string $element Name of form element to set error for * @param string $message Error message, if empty then removes the current error message - * @since 1.0 + * @since 1.0 * @access public * @return void */ @@ -948,7 +956,7 @@ function setElementError($element, $message = null) unset($this->_errors[$element]); } } // end func setElementError - + // }}} // {{{ getElementType() @@ -1006,11 +1014,11 @@ function updateElementAttr($elements, $attrs) * Removes an element * * The method "unlinks" an element from the form, returning the reference - * to the element object. If several elements named $elementName exist, + * to the element object. If several elements named $elementName exist, * it removes the first one, leaving the others intact. - * + * * @param string $elementName The element name - * @param boolean $removeRules True if rules for this element are to be removed too + * @param boolean $removeRules True if rules for this element are to be removed too * @access public * @since 2.0 * @return HTML_QuickForm_element a reference to the removed element @@ -1048,7 +1056,7 @@ function &removeElement($elementName, $removeRules = true) * Adds a validation rule for the given field * * If the element is in fact a group, it will be considered as a whole. - * To validate grouped elements as separated entities, + * To validate grouped elements as separated entities, * use addGroupRule instead of addRule. * * @param string $element Form element name @@ -1152,7 +1160,7 @@ function addGroupRule($group, $arg1, $type='', $format=null, $howmany=0, $valida $this->_rules[$elementName][] = array( 'type' => $type, - 'format' => $format, + 'format' => $format, 'message' => $rule[0], 'validation' => $validation, 'reset' => $reset, @@ -1189,7 +1197,7 @@ function addGroupRule($group, $arg1, $type='', $format=null, $howmany=0, $valida } $this->_rules[$group][] = array('type' => $type, - 'format' => $format, + 'format' => $format, 'message' => $arg1, 'validation' => $validation, 'howmany' => $howmany, @@ -1207,13 +1215,13 @@ function addGroupRule($group, $arg1, $type='', $format=null, $howmany=0, $valida // {{{ addFormRule() /** - * Adds a global validation rule - * + * Adds a global validation rule + * * This should be used when for a rule involving several fields or if * you want to use some completely custom validation for your form. - * The rule function/method should return true in case of successful + * The rule function/method should return true in case of successful * validation and array('element name' => 'error') when there were errors. - * + * * @access public * @param mixed Callback, either function name or array(&$object, 'method') * @throws HTML_QuickForm_Error @@ -1225,7 +1233,7 @@ function addFormRule($rule) } $this->_formRules[] = $rule; } - + // }}} // {{{ applyFilter() @@ -1256,7 +1264,7 @@ function applyFilter($element, $filter) $this->_submitValues[$elName] = $this->_recursiveFilter($filter, $value); } else { $idx = "['" . str_replace( - array('\\', '\'', ']', '['), array('\\\\', '\\\'', '', "']['"), + array('\\', '\'', ']', '['), array('\\\\', '\\\'', '', "']['"), $elName ) . "']"; eval("\$this->_submitValues{$idx} = \$this->_recursiveFilter(\$filter, \$value);"); @@ -1463,7 +1471,7 @@ function setJsWarnings($pref, $post) $this->_jsPrefix = $pref; $this->_jsPostfix = $post; } // end func setJsWarnings - + // }}} // {{{ setRequiredNote() @@ -1507,7 +1515,7 @@ function getRequiredNote() */ function validate() { - if (count($this->_rules) == 0 && count($this->_formRules) == 0 && + if (count($this->_rules) == 0 && count($this->_formRules) == 0 && $this->isSubmitted()) { return (0 == count($this->_errors)); } elseif (!$this->isSubmitted()) { @@ -1535,7 +1543,7 @@ function validate() // See also bug #12014, we should only consider a file that has // status UPLOAD_ERR_NO_FILE as not uploaded, in all other cases // validation should be performed, so that e.g. 'maxfilesize' rule - // will display an error if status is UPLOAD_ERR_INI_SIZE + // will display an error if status is UPLOAD_ERR_INI_SIZE // or UPLOAD_ERR_FORM_SIZE } elseif (is_array($submitValue)) { if (false === ($pos = strpos($target, '['))) { @@ -1543,10 +1551,10 @@ function validate() } else { $base = str_replace( array('\\', '\''), array('\\\\', '\\\''), - substr($target, 0, $pos) - ); + substr($target, 0, $pos) + ); $idx = "['" . str_replace( - array('\\', '\'', ']', '['), array('\\\\', '\\\'', '', "']['"), + array('\\', '\'', ']', '['), array('\\\\', '\\\'', '', "']['"), substr($target, $pos + 1, -1) ) . "']"; eval("\$isUpload = isset(\$this->_submitFiles['{$base}']['name']{$idx});"); @@ -1628,7 +1636,7 @@ function freeze($elementList=null) } return true; } // end func freeze - + // }}} // {{{ isFrozen() @@ -1808,7 +1816,7 @@ function getValidationScript() if (count($test) > 0) { return "\n