/usr/share/php/Horde/Form.php is in php-horde-form 2.0.12-1build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 | <?php
require_once 'Horde/Form/Type.php';
/**
* Horde_Form Master Class.
*
* The Horde_Form:: package provides form rendering, validation, and
* other functionality for the Horde Application Framework.
*
* Copyright 2001-2007 Robert E. Coyle <robertecoyle@hotmail.com>
* Copyright 2001-2016 Horde LLC (http://www.horde.org/)
*
* See the enclosed file COPYING for license information (LGPL). If you
* did not receive this file, see http://www.horde.org/licenses/lgpl21.
*
* @author Robert E. Coyle <robertecoyle@hotmail.com>
* @author Chuck Hagenbuch <chuck@horde.org>
* @package Form
*/
class Horde_Form {
protected $_name = '';
protected $_title = '';
protected $_extra = '';
protected $_vars;
protected $_submit = array();
protected $_reset = false;
protected $_errors = array();
protected $_submitted = null;
public $_sections = array();
protected $_open_section = null;
protected $_currentSection = array();
protected $_variables = array();
protected $_hiddenVariables = array();
protected $_useFormToken = true;
protected $_autofilled = false;
protected $_enctype = null;
public $_help = false;
function __construct($vars, $title = '', $name = null)
{
if (empty($name)) {
$name = Horde_String::lower(get_class($this));
}
$this->_vars = &$vars;
$this->_title = $title;
$this->_name = $name;
}
function singleton($form, &$vars, $title = '', $name = null)
{
static $instances = array();
$signature = serialize(array($form, $vars, $title, $name));
if (!isset($instances[$signature])) {
if (class_exists($form)) {
$instances[$signature] = new $form($vars, $title, $name);
} else {
$instances[$signature] = new Horde_Form($vars, $title, $name);
}
}
return $instances[$signature];
}
function setVars(&$vars)
{
$this->_vars = &$vars;
}
function getVars()
{
return $this->_vars;
}
function getTitle()
{
return $this->_title;
}
function setTitle($title)
{
$this->_title = $title;
}
function getExtra()
{
return $this->_extra;
}
function setExtra($extra)
{
$this->_extra = $extra;
}
function getName()
{
return $this->_name;
}
/**
* Sets or gets whether the form should be verified by tokens.
* Tokens are used to verify that a form is only submitted once.
*
* @param boolean $token If specified, sets whether to use form tokens.
*
* @return boolean Whether form tokens are being used.
*/
function useToken($token = null)
{
if (!is_null($token)) {
$this->_useFormToken = $token;
}
return $this->_useFormToken;
}
/**
* Get the renderer for this form, either a custom renderer or the
* standard one.
*
* To use a custom form renderer, your form class needs to
* override this function:
* <code>
* function getRenderer()
* {
* return new CustomFormRenderer();
* }
* </code>
*
* ... where CustomFormRenderer is the classname of the custom
* renderer class, which should extend Horde_Form_Renderer.
*
* @param array $params A hash of renderer-specific parameters.
*
* @return object Horde_Form_Renderer The form renderer.
*/
function getRenderer($params = array())
{
$renderer = new Horde_Form_Renderer($params);
return $renderer;
}
/**
* @throws Horde_Exception
*/
function getType($type, $params = array())
{
if (strpos($type, ':') !== false) {
list($app, $type) = explode(':', $type);
$type_class = $app . '_Form_Type_' . $type;
} else {
$type_class = 'Horde_Form_Type_' . $type;
}
if (!class_exists($type_class)) {
throw new Horde_Exception(sprintf('Nonexistant class "%s" for field type "%s"', $type_class, $type));
}
$type_ob = new $type_class();
if (!$params) {
$params = array();
}
call_user_func_array(array($type_ob, 'init'), $params);
return $type_ob;
}
function setSection($section = '', $desc = '', $image = '', $expanded = true)
{
$this->_currentSection = $section;
if (!count($this->_sections) && !$this->getOpenSection()) {
$this->setOpenSection($section);
}
$this->_sections[$section]['desc'] = $desc;
$this->_sections[$section]['expanded'] = $expanded;
$this->_sections[$section]['image'] = $image;
}
function getSectionDesc($section)
{
return $this->_sections[$section]['desc'];
}
function getSectionImage($section)
{
return $this->_sections[$section]['image'];
}
function setOpenSection($section)
{
$this->_vars->set('__formOpenSection', $section);
}
function getOpenSection()
{
return $this->_vars->get('__formOpenSection');
}
function getSectionExpandedState($section, $boolean = false)
{
if ($boolean) {
/* Only the boolean value is required. */
return $this->_sections[$section]['expanded'];
}
/* Need to return the values for use in styles. */
if ($this->_sections[$section]['expanded']) {
return 'block';
} else {
return 'none';
}
}
/**
* TODO
*/
function addVariable($humanName, $varName, $type, $required,
$readonly = false, $description = null,
$params = array())
{
return $this->insertVariableBefore(null, $humanName, $varName, $type,
$required, $readonly, $description,
$params);
}
/**
* TODO
*/
function insertVariableBefore($before, $humanName, $varName, $type,
$required, $readonly = false,
$description = null, $params = array())
{
$type = $this->getType($type, $params);
$var = new Horde_Form_Variable($humanName, $varName, $type,
$required, $readonly, $description);
/* Set the form object reference in the var. */
$var->setFormOb($this);
if ($var->getTypeName() == 'enum' &&
!strlen($type->getPrompt()) &&
count($var->getValues()) == 1) {
$vals = array_keys($var->getValues());
$this->_vars->add($var->varName, $vals[0]);
$var->_autofilled = true;
} elseif ($var->getTypeName() == 'file' ||
$var->getTypeName() == 'image') {
$this->_enctype = 'multipart/form-data';
}
if (empty($this->_currentSection) && $this->_currentSection !== 0) {
$this->_currentSection = '__base';
}
if (is_null($before)) {
$this->_variables[$this->_currentSection][] = &$var;
} else {
$num = 0;
while (isset($this->_variables[$this->_currentSection][$num]) &&
$this->_variables[$this->_currentSection][$num]->getVarName() != $before) {
$num++;
}
if (!isset($this->_variables[$this->_currentSection][$num])) {
$this->_variables[$this->_currentSection][] = &$var;
} else {
$this->_variables[$this->_currentSection] = array_merge(
array_slice($this->_variables[$this->_currentSection], 0, $num),
array(&$var),
array_slice($this->_variables[$this->_currentSection], $num));
}
}
return $var;
}
/**
* Removes a variable from the form.
*
* As only variables can be passed by reference, you need to call this
* method this way if want to pass a variable name:
* <code>
* $form->removeVariable($var = 'varname');
* </code>
*
* @param Horde_Form_Variable|string $var Either the variable's name or
* the variable to remove from the
* form.
*
* @return boolean True if the variable was found (and deleted).
*/
function removeVariable(&$var)
{
foreach (array_keys($this->_variables) as $section) {
foreach (array_keys($this->_variables[$section]) as $i) {
if ((is_a($var, 'Horde_Form_Variable') && $this->_variables[$section][$i] === $var) ||
($this->_variables[$section][$i]->getVarName() == $var)) {
// Slice out the variable to be removed.
$this->_variables[$this->_currentSection] = array_merge(
array_slice($this->_variables[$this->_currentSection], 0, $i),
array_slice($this->_variables[$this->_currentSection], $i + 1));
return true;
}
}
}
return false;
}
/**
* TODO
*/
function addHidden($humanName, $varName, $type, $required,
$readonly = false, $description = null,
$params = array())
{
$type = $this->getType($type, $params);
$var = new Horde_Form_Variable($humanName, $varName, $type,
$required, $readonly, $description);
$var->hide();
$this->_hiddenVariables[] = &$var;
return $var;
}
function getVariables($flat = true, $withHidden = false)
{
if ($flat) {
$vars = array();
foreach ($this->_variables as $section) {
foreach ($section as $var) {
$vars[] = $var;
}
}
if ($withHidden) {
foreach ($this->_hiddenVariables as $var) {
$vars[] = $var;
}
}
return $vars;
} else {
return $this->_variables;
}
}
function setButtons($submit, $reset = false)
{
if ($submit === true || is_null($submit) || empty($submit)) {
/* Default to 'Submit'. */
$submit = array(Horde_Form_Translation::t("Submit"));
} elseif (!is_array($submit)) {
/* Default to array if not passed. */
$submit = array($submit);
}
/* Only if $reset is strictly true insert default 'Reset'. */
if ($reset === true) {
$reset = Horde_Form_Translation::t("Reset");
}
$this->_submit = $submit;
$this->_reset = $reset;
}
function appendButtons($submit)
{
if (!is_array($submit)) {
$submit = array($submit);
}
$this->_submit = array_merge($this->_submit, $submit);
}
function preserveVarByPost(&$vars, $varname, $alt_varname = '')
{
$value = $vars->getExists($varname, $wasset);
/* If an alternate name is given under which to preserve use that. */
if ($alt_varname) {
$varname = $alt_varname;
}
if ($wasset) {
$this->_preserveVarByPost($varname, $value);
}
}
/**
* @access private
*/
function _preserveVarByPost($varname, $value)
{
if (is_array($value)) {
foreach ($value as $id => $val) {
$this->_preserveVarByPost($varname . '[' . $id . ']', $val);
}
} else {
$varname = htmlspecialchars($varname);
$value = htmlspecialchars($value);
printf('<input type="hidden" name="%s" value="%s" />' . "\n",
$varname,
$value);
}
}
function open(&$renderer, &$vars, $action, $method = 'get', $enctype = null)
{
if (is_null($enctype) && !is_null($this->_enctype)) {
$enctype = $this->_enctype;
}
$renderer->open($action, $method, $this->_name, $enctype);
$renderer->listFormVars($this);
if (!empty($this->_name)) {
$this->_preserveVarByPost('formname', $this->_name);
}
if ($this->_useFormToken) {
$token = Horde_Token::generateId($this->_name);
$GLOBALS['session']->set('horde', 'form_secrets/' . $token, true);
$this->_preserveVarByPost($this->_name . '_formToken', $token);
}
/* Loop through vars and check for any special cases to preserve. */
$variables = $this->getVariables();
foreach ($variables as $var) {
/* Preserve value if change has to be tracked. */
if ($var->getOption('trackchange')) {
$varname = $var->getVarName();
$this->preserveVarByPost($vars, $varname, '__old_' . $varname);
}
}
foreach ($this->_hiddenVariables as $var) {
$this->preserveVarByPost($vars, $var->getVarName());
}
}
function close($renderer)
{
$renderer->close();
}
/**
* Renders the form for editing.
*
* @param Horde_Form_Renderer $renderer A renderer instance, optional
* since Horde 3.2.
* @param Variables $vars A Variables instance, optional
* since Horde 3.2.
* @param string $action The form action (url).
* @param string $method The form method, usually either
* 'get' or 'post'.
* @param string $enctype The form encoding type. Determined
* automatically if null.
* @param boolean $focus Focus the first form field?
*/
function renderActive($renderer = null, $vars = null, $action = '',
$method = 'get', $enctype = null, $focus = true)
{
if (is_null($renderer)) {
$renderer = $this->getRenderer();
}
if (is_null($vars)) {
$vars = $this->_vars;
}
if (is_null($enctype) && !is_null($this->_enctype)) {
$enctype = $this->_enctype;
}
$renderer->open($action, $method, $this->getName(), $enctype);
$renderer->listFormVars($this);
if (!empty($this->_name)) {
$this->_preserveVarByPost('formname', $this->_name);
}
if ($this->_useFormToken) {
$token = Horde_Token::generateId($this->_name);
$GLOBALS['session']->set('horde', 'form_secrets/' . $token, true);
$this->_preserveVarByPost($this->_name . '_formToken', $token);
}
if (count($this->_sections)) {
$this->_preserveVarByPost('__formOpenSection', $this->getOpenSection());
}
/* Loop through vars and check for any special cases to
* preserve. */
$variables = $this->getVariables();
foreach ($variables as $var) {
/* Preserve value if change has to be tracked. */
if ($var->getOption('trackchange')) {
$varname = $var->getVarName();
$this->preserveVarByPost($vars, $varname, '__old_' . $varname);
}
}
foreach ($this->_hiddenVariables as $var) {
$this->preserveVarByPost($vars, $var->getVarName());
}
$renderer->beginActive($this->getTitle(), $this->getExtra());
$renderer->renderFormActive($this, $vars);
$renderer->submit($this->_submit, $this->_reset);
$renderer->end();
$renderer->close($focus);
}
/**
* Renders the form for displaying.
*
* @param Horde_Form_Renderer $renderer A renderer instance, optional
* since Horde 3.2.
* @param Variables $vars A Variables instance, optional
* since Horde 3.2.
*/
function renderInactive($renderer = null, $vars = null)
{
if (is_null($renderer)) {
$renderer = $this->getRenderer();
}
if (is_null($vars)) {
$vars = $this->_vars;
}
$renderer->_name = $this->_name;
$renderer->beginInactive($this->getTitle(), $this->getExtra());
$renderer->renderFormInactive($this, $vars);
$renderer->end();
}
function preserve($vars)
{
if ($this->_useFormToken) {
$token = Horde_Token::generateId($this->_name);
$GLOBALS['session']->set('horde', 'form_secrets/' . $token, true);
$this->_preserveVarByPost($this->_name . '_formToken', $token);
}
$variables = $this->getVariables();
foreach ($variables as $var) {
$varname = $var->getVarName();
/* Save value of individual components. */
switch ($var->getTypeName()) {
case 'passwordconfirm':
case 'emailconfirm':
$this->preserveVarByPost($vars, $varname . '[original]');
$this->preserveVarByPost($vars, $varname . '[confirm]');
break;
case 'monthyear':
$this->preserveVarByPost($vars, $varname . '[month]');
$this->preserveVarByPost($vars, $varname . '[year]');
break;
case 'monthdayyear':
$this->preserveVarByPost($vars, $varname . '[month]');
$this->preserveVarByPost($vars, $varname . '[day]');
$this->preserveVarByPost($vars, $varname . '[year]');
break;
}
$this->preserveVarByPost($vars, $varname);
}
foreach ($this->_hiddenVariables as $var) {
$this->preserveVarByPost($vars, $var->getVarName());
}
}
function unsetVars(&$vars)
{
foreach ($this->getVariables() as $var) {
$vars->remove($var->getVarName());
}
}
/**
* Validates the form, checking if it really has been submitted by calling
* isSubmitted() and if true does any onSubmit() calls for variable types
* in the form. The _submitted variable is then rechecked.
*
* @param Variables $vars A Variables instance, optional since Horde
* 3.2.
* @param boolean $canAutofill Can the form be valid without being
* submitted?
*
* @return boolean True if the form is valid.
*/
function validate($vars = null, $canAutoFill = false)
{
if (is_null($vars)) {
$vars = $this->_vars;
}
/* Get submitted status. */
if ($this->isSubmitted() || $canAutoFill) {
/* Form was submitted or can autofill; check for any variable
* types' onSubmit(). */
$this->onSubmit($vars);
/* Recheck submitted status. */
if (!$this->isSubmitted() && !$canAutoFill) {
return false;
}
} else {
/* Form has not been submitted; return false. */
return false;
}
$message = '';
$this->_autofilled = true;
if ($this->_useFormToken) {
$tokenSource = $GLOBALS['injector']->getInstance('Horde_Token');
$passedToken = $vars->get($this->_name . '_formToken');
if (!empty($passedToken) && !$tokenSource->verify($passedToken)) {
$this->_errors['_formToken'] = Horde_Form_Translation::t("This form has already been processed.");
}
if (!$GLOBALS['session']->get('horde', 'form_secrets/' . $passedToken)) {
$this->_errors['_formSecret'] = Horde_Form_Translation::t("Required secret is invalid - potentially malicious request.");
}
}
foreach ($this->getVariables() as $var) {
$this->_autofilled = $var->_autofilled && $this->_autofilled;
if (!$var->validate($vars, $message)) {
$this->_errors[$var->getVarName()] = $message;
}
}
if ($this->_autofilled) {
unset($this->_errors['_formToken']);
}
foreach ($this->_hiddenVariables as $var) {
if (!$var->validate($vars, $message)) {
$this->_errors[$var->getVarName()] = $message;
}
}
return $this->isValid();
}
function clearValidation()
{
$this->_errors = array();
}
function getErrors()
{
return $this->_errors;
}
function getError($var)
{
if (is_a($var, 'Horde_Form_Variable')) {
$name = $var->getVarName();
} else {
$name = $var;
}
return isset($this->_errors[$name]) ? $this->_errors[$name] : null;
}
function setError($var, $message)
{
if (is_a($var, 'Horde_Form_Variable')) {
$name = $var->getVarName();
} else {
$name = $var;
}
$this->_errors[$name] = $message;
}
function clearError($var)
{
if (is_a($var, 'Horde_Form_Variable')) {
$name = $var->getVarName();
} else {
$name = $var;
}
unset($this->_errors[$name]);
}
function isValid()
{
return ($this->_autofilled || count($this->_errors) == 0);
}
function execute()
{
Horde::log('Warning: Horde_Form::execute() called, should be overridden', 'DEBUG');
}
/**
* Fetch the field values of the submitted form.
*
* @param Variables $vars A Variables instance, optional since Horde 3.2.
* @param array $info Array to be filled with the submitted field
* values.
*/
function getInfo($vars, &$info)
{
if (is_null($vars)) {
$vars = $this->_vars;
}
$this->_getInfoFromVariables($this->getVariables(), $vars, $info);
$this->_getInfoFromVariables($this->_hiddenVariables, $vars, $info);
}
/**
* Fetch the field values from a given array of variables.
*
* @access private
*
* @param array $variables An array of Horde_Form_Variable objects to
* fetch from.
* @param object $vars The Variables object.
* @param array $info The array to be filled with the submitted
* field values.
*/
function _getInfoFromVariables($variables, &$vars, &$info)
{
foreach ($variables as $var) {
if ($var->isDisabled()) {
// Disabled fields are not submitted by some browsers, so don't
// pretend they were.
continue;
}
if ($var->isArrayVal()) {
$var->getInfo($vars, $values);
if (is_array($values)) {
$varName = str_replace('[]', '', $var->getVarName());
foreach ($values as $i => $val) {
$info[$i][$varName] = $val;
}
}
} else {
if (Horde_Array::getArrayParts($var->getVarName(), $base, $keys)) {
if (!isset($info[$base])) {
$info[$base] = array();
}
$pointer = &$info[$base];
while (count($keys)) {
$key = array_shift($keys);
if (!isset($pointer[$key])) {
$pointer[$key] = array();
}
$pointer = &$pointer[$key];
}
$var->getInfo($vars, $pointer);
} else {
$var->getInfo($vars, $info[$var->getVarName()]);
}
}
}
}
function hasHelp()
{
return $this->_help;
}
/**
* Determines if this form has been submitted or not. If the class
* var _submitted is null then it will check for the presence of
* the formname in the form variables.
*
* Other events can explicitly set the _submitted variable to
* false to indicate a form submit but not for actual posting of
* data (eg. onChange events to update the display of fields).
*
* @return boolean True or false indicating if the form has been
* submitted.
*/
function isSubmitted()
{
if (is_null($this->_submitted)) {
if ($this->_vars->get('formname') == $this->getName()) {
$this->_submitted = true;
} else {
$this->_submitted = false;
}
}
return $this->_submitted;
}
/**
* Checks if there is anything to do on the submission of the form by
* looping through each variable's onSubmit() function.
*
* @param Horde_Variables $vars
*/
function onSubmit(&$vars)
{
/* Loop through all vars and check if there's anything to do on
* submit. */
$variables = $this->getVariables();
foreach ($variables as $var) {
$var->type->onSubmit($var, $vars);
/* If changes to var being tracked don't register the form as
* submitted if old value and new value differ. */
if ($var->getOption('trackchange')) {
$varname = $var->getVarName();
if (!is_null($vars->get('formname')) &&
$vars->get($varname) != $vars->get('__old_' . $varname)) {
$this->_submitted = false;
}
}
}
}
/**
* Explicitly sets the state of the form submit.
*
* An event can override the automatic determination of the submit state
* in the isSubmitted() function.
*
* @param boolean $state Whether to set the state of the form as being
* submitted.
*/
function setSubmitted($state = true)
{
$this->_submitted = $state;
}
}
|