2013-07-15 13:58:09 +02:00
|
|
|
<?php
|
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
|
|
|
|
|
|
|
namespace Icinga\Web;
|
|
|
|
|
2014-07-18 09:51:15 +02:00
|
|
|
use LogicException;
|
2014-04-24 10:13:47 +02:00
|
|
|
use Zend_Form;
|
2014-07-18 09:51:15 +02:00
|
|
|
use Zend_View_Interface;
|
2014-04-24 10:13:47 +02:00
|
|
|
use Icinga\Web\Form\Decorator\HelpText;
|
2014-07-10 11:13:45 +02:00
|
|
|
use Icinga\Web\Form\Decorator\ElementWrapper;
|
2014-08-12 14:43:10 +02:00
|
|
|
use Icinga\Web\Form\Element\CsrfCounterMeasure;
|
2013-07-15 13:58:09 +02:00
|
|
|
|
2013-07-15 14:32:18 +02:00
|
|
|
/**
|
2013-08-12 11:23:01 +02:00
|
|
|
* Base class for forms providing CSRF protection, confirmation logic and auto submission
|
2013-07-15 14:32:18 +02:00
|
|
|
*/
|
2013-08-26 16:56:23 +02:00
|
|
|
class Form extends Zend_Form
|
2013-07-15 13:58:09 +02:00
|
|
|
{
|
2014-07-18 09:51:15 +02:00
|
|
|
/**
|
|
|
|
* Whether this form has been created
|
|
|
|
*
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
protected $created = false;
|
|
|
|
|
2013-07-24 10:56:41 +02:00
|
|
|
/**
|
2014-07-10 11:13:45 +02:00
|
|
|
* The view script to use when rendering this form
|
2013-08-21 11:02:53 +02:00
|
|
|
*
|
2013-08-12 11:23:01 +02:00
|
|
|
* @var string
|
2013-07-24 10:56:41 +02:00
|
|
|
*/
|
2014-07-10 11:13:45 +02:00
|
|
|
protected $viewScript;
|
2013-07-24 10:56:41 +02:00
|
|
|
|
2013-08-26 15:06:07 +02:00
|
|
|
/**
|
2014-07-10 11:13:45 +02:00
|
|
|
* Whether this form should NOT add random generated "challenge" tokens that are associated with the user's current
|
|
|
|
* session in order to prevent Cross-Site Request Forgery (CSRF). It is the form's responsibility to verify the
|
|
|
|
* existence and correctness of this token
|
2013-08-26 15:06:07 +02:00
|
|
|
*
|
2014-07-10 11:13:45 +02:00
|
|
|
* @var bool
|
2013-08-26 15:06:07 +02:00
|
|
|
*/
|
2014-07-10 11:13:45 +02:00
|
|
|
protected $tokenDisabled = false;
|
2013-09-11 17:19:18 +02:00
|
|
|
|
2013-07-24 10:56:41 +02:00
|
|
|
/**
|
2014-07-10 11:13:45 +02:00
|
|
|
* Name of the CSRF token element
|
2013-08-12 11:23:01 +02:00
|
|
|
*
|
2014-07-10 11:13:45 +02:00
|
|
|
* @var string
|
2013-07-24 10:56:41 +02:00
|
|
|
*/
|
2014-07-10 11:13:45 +02:00
|
|
|
protected $tokenElementName = 'CSRFToken';
|
2013-09-11 17:19:18 +02:00
|
|
|
|
2013-07-18 10:32:53 +02:00
|
|
|
/**
|
2014-07-10 11:13:45 +02:00
|
|
|
* Set the view script to use when rendering this form
|
2013-08-12 11:23:01 +02:00
|
|
|
*
|
2014-07-10 11:13:45 +02:00
|
|
|
* @param string $viewScript The view script to use
|
2013-08-21 11:02:53 +02:00
|
|
|
*
|
2014-07-10 11:13:45 +02:00
|
|
|
* @return self
|
2013-07-18 10:32:53 +02:00
|
|
|
*/
|
2014-07-10 11:13:45 +02:00
|
|
|
public function setViewScript($viewScript)
|
2013-07-16 15:39:47 +02:00
|
|
|
{
|
2014-07-10 11:13:45 +02:00
|
|
|
$this->viewScript = $viewScript;
|
|
|
|
return $this;
|
2013-07-15 13:58:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-07-10 11:13:45 +02:00
|
|
|
* Return the view script being used when rendering this form
|
|
|
|
*
|
|
|
|
* @return string
|
2013-07-15 13:58:09 +02:00
|
|
|
*/
|
2014-07-10 11:13:45 +02:00
|
|
|
public function getViewScript()
|
2013-08-27 14:37:22 +02:00
|
|
|
{
|
2014-07-10 11:13:45 +02:00
|
|
|
return $this->viewScript;
|
2013-08-27 14:37:22 +02:00
|
|
|
}
|
2013-07-15 13:58:09 +02:00
|
|
|
|
2013-08-27 14:27:31 +02:00
|
|
|
/**
|
2014-07-10 11:13:45 +02:00
|
|
|
* Disable CSRF counter measure and remove its field if already added
|
2013-08-27 14:27:31 +02:00
|
|
|
*
|
2014-07-10 11:13:45 +02:00
|
|
|
* @param bool $disabled Set true in order to disable CSRF protection for this form, otherwise false
|
2013-08-27 14:27:31 +02:00
|
|
|
*
|
2014-07-10 11:13:45 +02:00
|
|
|
* @return self
|
2013-08-27 14:27:31 +02:00
|
|
|
*/
|
2014-07-10 11:13:45 +02:00
|
|
|
public function setTokenDisabled($disabled = true)
|
2013-08-27 14:27:31 +02:00
|
|
|
{
|
2014-07-10 11:13:45 +02:00
|
|
|
$this->tokenDisabled = (bool) $disabled;
|
|
|
|
|
|
|
|
if ($disabled && $this->getElement($this->tokenElementName) !== null) {
|
|
|
|
$this->removeElement($this->tokenElementName);
|
2013-08-27 14:27:31 +02:00
|
|
|
}
|
2014-04-24 10:13:47 +02:00
|
|
|
|
2014-07-10 11:13:45 +02:00
|
|
|
return $this;
|
2013-08-27 14:27:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-07-10 11:13:45 +02:00
|
|
|
* Return whether CSRF counter measures are disabled for this form
|
2013-08-27 14:27:31 +02:00
|
|
|
*
|
2014-07-10 11:13:45 +02:00
|
|
|
* @return bool
|
2013-08-27 14:27:31 +02:00
|
|
|
*/
|
2014-07-10 11:13:45 +02:00
|
|
|
public function getTokenDisabled()
|
2013-08-27 14:27:31 +02:00
|
|
|
{
|
2014-07-10 11:13:45 +02:00
|
|
|
return $this->tokenDisabled;
|
2013-08-27 14:27:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-07-10 11:13:45 +02:00
|
|
|
* Set the name to use for the CSRF element
|
|
|
|
*
|
|
|
|
* @param string $name The name to set
|
2013-08-27 14:27:31 +02:00
|
|
|
*
|
2014-07-10 11:13:45 +02:00
|
|
|
* @return self
|
2013-08-27 14:27:31 +02:00
|
|
|
*/
|
2014-07-10 11:13:45 +02:00
|
|
|
public function setTokenElementName($name)
|
2013-08-27 14:27:31 +02:00
|
|
|
{
|
2014-07-10 11:13:45 +02:00
|
|
|
$this->tokenElementName = $name;
|
|
|
|
return $this;
|
2013-08-27 14:27:31 +02:00
|
|
|
}
|
|
|
|
|
2013-07-18 10:32:53 +02:00
|
|
|
/**
|
2014-07-10 11:13:45 +02:00
|
|
|
* Return the name of the CSRF element
|
2013-08-12 11:23:01 +02:00
|
|
|
*
|
2014-07-10 11:13:45 +02:00
|
|
|
* @return string
|
2013-07-18 10:32:53 +02:00
|
|
|
*/
|
2014-07-10 11:13:45 +02:00
|
|
|
public function getTokenElementName()
|
2013-07-18 10:32:53 +02:00
|
|
|
{
|
2014-07-10 11:13:45 +02:00
|
|
|
return $this->tokenElementName;
|
2013-07-18 10:32:53 +02:00
|
|
|
}
|
|
|
|
|
2014-07-18 09:51:15 +02:00
|
|
|
/**
|
|
|
|
* Create this form
|
|
|
|
*
|
2014-08-22 12:04:14 +02:00
|
|
|
* @param array $formData The data sent by the user
|
2014-07-18 09:51:15 +02:00
|
|
|
*
|
|
|
|
* @return self
|
|
|
|
*/
|
|
|
|
public function create(array $formData = array())
|
|
|
|
{
|
|
|
|
if (false === $this->created) {
|
2014-08-13 14:50:21 +02:00
|
|
|
$this->addElements($this->createElements($formData));
|
|
|
|
$this->addCsrfCounterMeasure()->addSubmitButton();
|
|
|
|
|
2014-08-12 10:41:19 +02:00
|
|
|
if ($this->getAction() === '') {
|
|
|
|
// We MUST set an action as JS gets confused otherwise, if
|
|
|
|
// this form is being displayed in an additional column
|
2014-08-13 14:50:21 +02:00
|
|
|
$this->setAction(Url::fromRequest()->getUrlWithout(array_keys($this->getElements())));
|
2014-08-12 10:41:19 +02:00
|
|
|
}
|
|
|
|
|
2014-07-18 09:51:15 +02:00
|
|
|
$this->created = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2013-08-05 11:05:11 +02:00
|
|
|
/**
|
2014-07-10 11:13:45 +02:00
|
|
|
* Create and return the elements to add to this form
|
|
|
|
*
|
|
|
|
* Intended to be implemented by concrete form classes.
|
|
|
|
*
|
2014-08-22 12:04:14 +02:00
|
|
|
* @param array $formData The data sent by the user
|
2014-07-18 09:51:15 +02:00
|
|
|
*
|
2014-07-10 11:13:45 +02:00
|
|
|
* @return array
|
2013-08-05 11:05:11 +02:00
|
|
|
*/
|
2014-07-18 09:51:15 +02:00
|
|
|
public function createElements(array $formData)
|
2013-08-05 11:05:11 +02:00
|
|
|
{
|
2014-07-10 11:13:45 +02:00
|
|
|
return array();
|
2013-08-05 11:05:11 +02:00
|
|
|
}
|
|
|
|
|
2014-07-18 09:54:36 +02:00
|
|
|
/**
|
|
|
|
* Add a submit button to this form
|
|
|
|
*
|
|
|
|
* Intended to be implemented by concrete form classes.
|
|
|
|
*
|
|
|
|
* @return self
|
|
|
|
*/
|
|
|
|
public function addSubmitButton()
|
|
|
|
{
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2013-08-05 11:05:11 +02:00
|
|
|
/**
|
2014-07-23 10:57:46 +02:00
|
|
|
* Create a new element
|
2014-07-10 11:13:45 +02:00
|
|
|
*
|
|
|
|
* Additionally, all structural form element decorators by Zend are replaced with our own ones.
|
|
|
|
*
|
2014-07-23 10:57:46 +02:00
|
|
|
* @param string $type String element type
|
|
|
|
* @param string $name The name of the element to add
|
|
|
|
* @param array $options The options for the element
|
2014-07-10 11:13:45 +02:00
|
|
|
*
|
2014-07-23 10:57:46 +02:00
|
|
|
* @return Zend_Form_Element
|
2013-08-12 11:23:01 +02:00
|
|
|
*
|
2014-07-23 10:57:46 +02:00
|
|
|
* @see Zend_Form::createElement()
|
2013-08-05 11:05:11 +02:00
|
|
|
*/
|
2014-07-23 10:57:46 +02:00
|
|
|
public function createElement($type, $name, $options = null)
|
2013-08-05 11:05:11 +02:00
|
|
|
{
|
2014-07-23 10:57:46 +02:00
|
|
|
$el = parent::createElement($type, $name, $options);
|
2013-08-05 11:05:11 +02:00
|
|
|
|
2014-07-10 11:13:45 +02:00
|
|
|
if ($el) {
|
|
|
|
if (strpos(strtolower(get_class($el)), 'hidden') !== false) {
|
|
|
|
$el->setDecorators(array('ViewHelper'));
|
|
|
|
} else {
|
|
|
|
$el->removeDecorator('HtmlTag');
|
|
|
|
$el->removeDecorator('Label');
|
|
|
|
$el->removeDecorator('DtDdWrapper');
|
|
|
|
$el->addDecorator(new ElementWrapper());
|
|
|
|
$el->addDecorator(new HelpText());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-23 10:57:46 +02:00
|
|
|
return $el;
|
2013-08-05 11:05:11 +02:00
|
|
|
}
|
|
|
|
|
2014-07-18 09:43:03 +02:00
|
|
|
/**
|
|
|
|
* Add CSRF counter measure field to this form
|
|
|
|
*
|
|
|
|
* @return self
|
|
|
|
*/
|
2014-08-12 14:43:10 +02:00
|
|
|
public function addCsrfCounterMeasure()
|
2014-07-18 09:43:03 +02:00
|
|
|
{
|
|
|
|
if (false === $this->tokenDisabled && $this->getElement($this->tokenElementName) === null) {
|
2014-08-12 14:43:10 +02:00
|
|
|
$element = new CsrfCounterMeasure($this->tokenElementName);
|
|
|
|
$element->setDecorators(array('ViewHelper'));
|
|
|
|
$this->addElement($element);
|
2014-07-18 09:43:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2014-07-18 09:51:15 +02:00
|
|
|
/**
|
|
|
|
* Populate the elements with the given values
|
|
|
|
*
|
|
|
|
* @param array $defaults The values to populate the elements with
|
|
|
|
*/
|
|
|
|
public function setDefaults(array $defaults)
|
|
|
|
{
|
|
|
|
$this->create($defaults);
|
|
|
|
return parent::setDefaults($defaults);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return whether the given data provides a value for each element of this form
|
|
|
|
*
|
2014-07-21 15:25:00 +02:00
|
|
|
* Note that elements that are disabled or of type submit are not taken into consideration.
|
|
|
|
*
|
2014-07-18 09:51:15 +02:00
|
|
|
* @param array $formData The data to check
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*
|
|
|
|
* @throws LogicException In case this form has no elements
|
|
|
|
*/
|
|
|
|
public function isComplete(array $formData)
|
|
|
|
{
|
|
|
|
$elements = $this->create($formData)->getElements();
|
|
|
|
if (empty($elements)) {
|
|
|
|
throw new LogicException('Forms without elements cannot be complete');
|
|
|
|
}
|
|
|
|
|
2014-07-21 08:57:41 +02:00
|
|
|
$missingValues = array_diff_key(
|
|
|
|
array_filter(
|
|
|
|
$elements,
|
|
|
|
function ($el) {
|
2014-07-23 10:58:45 +02:00
|
|
|
return $el->getAttrib('disabled') === null
|
|
|
|
&& 0 === preg_match('@(submit|button)@', strtolower(get_class($el)));
|
2014-07-21 08:57:41 +02:00
|
|
|
}
|
|
|
|
),
|
|
|
|
$formData
|
|
|
|
);
|
2014-07-18 09:51:15 +02:00
|
|
|
return empty($missingValues);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return whether the given values (possibly incomplete) are valid
|
|
|
|
*
|
|
|
|
* Unlike Zend_Form::isValid() this will not set NULL as value for
|
|
|
|
* an element that is not present in the given data.
|
|
|
|
*
|
|
|
|
* @param array $formData The data to validate
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isValidPartial(array $formData)
|
|
|
|
{
|
|
|
|
$this->create($formData);
|
|
|
|
return parent::isValidPartial($formData);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return whether the given values are complete and valid
|
|
|
|
*
|
|
|
|
* @param array $formData The data to validate
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isValid($formData)
|
|
|
|
{
|
|
|
|
if ($this->isComplete($formData)) {
|
|
|
|
return parent::isValid($formData);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->isValidPartial($formData); // The form can't be processed but we want to show validation errors though
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove all elements of this form
|
|
|
|
*
|
|
|
|
* @return self
|
|
|
|
*/
|
|
|
|
public function clearElements()
|
|
|
|
{
|
|
|
|
$this->created = false;
|
|
|
|
return parent::clearElements();
|
|
|
|
}
|
|
|
|
|
2013-08-26 15:06:07 +02:00
|
|
|
/**
|
2014-07-10 11:13:45 +02:00
|
|
|
* Load the default decorators
|
|
|
|
*
|
2014-07-18 09:52:50 +02:00
|
|
|
* Overwrites Zend_Form::loadDefaultDecorators to avoid having
|
|
|
|
* the HtmlTag-Decorator added and to provide viewscript usage
|
2013-08-26 15:06:07 +02:00
|
|
|
*
|
2014-07-10 11:13:45 +02:00
|
|
|
* @return self
|
2013-08-26 15:06:07 +02:00
|
|
|
*/
|
2014-07-10 11:13:45 +02:00
|
|
|
public function loadDefaultDecorators()
|
2013-08-26 15:06:07 +02:00
|
|
|
{
|
2014-07-10 11:13:45 +02:00
|
|
|
if ($this->loadDefaultDecoratorsIsDisabled()) {
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
$decorators = $this->getDecorators();
|
|
|
|
if (empty($decorators)) {
|
|
|
|
if ($this->viewScript) {
|
|
|
|
$this->addDecorator('ViewScript', array('viewScript' => $this->viewScript));
|
|
|
|
} else {
|
|
|
|
$this->addDecorator('FormElements')
|
|
|
|
//->addDecorator('HtmlTag', array('tag' => 'dl', 'class' => 'zend_form'))
|
|
|
|
->addDecorator('Form');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
2013-08-26 15:06:07 +02:00
|
|
|
}
|
|
|
|
|
2014-07-18 09:51:15 +02:00
|
|
|
/**
|
|
|
|
* Render this form
|
|
|
|
*
|
|
|
|
* @param Zend_View_Interface $view The view context to use
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function render(Zend_View_Interface $view = null)
|
|
|
|
{
|
|
|
|
$this->create();
|
|
|
|
return parent::render($view);
|
|
|
|
}
|
2013-07-15 13:58:09 +02:00
|
|
|
}
|