Skip to content
This repository has been archived by the owner on Dec 13, 2022. It is now read-only.

Commit

Permalink
* fixes #3852 : Using an array of token to support multiple form at …
Browse files Browse the repository at this point in the history
…the same time

Conflicts:
	www/lib/HTML/QuickForm.php
  • Loading branch information
Lionel Assepo committed Oct 26, 2015
1 parent b21c52d commit 38f3895
Showing 1 changed file with 34 additions and 13 deletions.
47 changes: 34 additions & 13 deletions www/lib/HTML/QuickForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -2026,39 +2026,43 @@ function errorMessage($value)
} // end func errorMessage

/**
*
* Create the CSRF Token to be set in every form using QuickForm
*/
function createSecurityToken()
{

$token = md5(uniqid());
$_SESSION['x-centreon-token'] = $token;
$_SESSION['x-centreon-token-generated-at'] = time();
if (false === isset($_SESSION['x-centreon-token'])) {
$_SESSION['x-centreon-token'] = array();
$_SESSION['x-centreon-token-generated-at'] = array();
}
$_SESSION['x-centreon-token'][] = $token;
$_SESSION['x-centreon-token-generated-at'][(string)$token] = time();

$myTokenElement = $this->addElement('hidden', 'centreon_token');
$myTokenElement->setValue($token);
}

/**
* Check if the CSRF Token is still valid
*
* @param type $submittedValues
* @return boolean
*/
function checkSecurityToken($submittedValues)
{
$success = false;

if ($this->_tokenValidated) {
$success = true;
} else {
if (isset($submittedValues['centreon_token']) && isset($_SESSION['x-centreon-token']) && isset($_SESSION['x-centreon-token-generated-at'])) {
$elapsedTime = time() - $_SESSION['x-centreon-token-generated-at'];
if (isset($submittedValues['centreon_token']) && in_array($submittedValues['centreon_token'], $_SESSION['x-centreon-token'])) {
$elapsedTime = time() - $_SESSION['x-centreon-token-generated-at'][(string)$submittedValues['centreon_token']];
if ($elapsedTime < (15 * 60)) {
if ($submittedValues['centreon_token'] == $_SESSION['x-centreon-token']) {
unset($_SESSION['x-centreon-token']);
unset($_SESSION['x-centreon-token-generated-at']);
$success = true;
$this->_tokenValidated = true;
}
$key = array_search((string)$submittedValues['centreon_token'], $_SESSION['x-centreon-token']);
unset($_SESSION['x-centreon-token'][$key]);
unset($_SESSION['x-centreon-token-generated-at'][(string)$submittedValues['centreon_token']]);
$success = true;
$this->_tokenValidated = true;
}
}
}
Expand All @@ -2067,12 +2071,29 @@ function checkSecurityToken($submittedValues)
$error = true;
} else {
$error = array('centreon_token' => 'The Token is invalid');
echo "<div class='msg' align='center'>"._("The CRSF token is invalid")."</div>";
echo "<div class='msg' align='center'>"._("The CSRF token is invalid")."</div>";
}

$this->purgeToken();

return $error;
}

/**
* Empty all elapsed Toekn stored
*/
function purgeToken()
{
foreach ($_SESSION['x-centreon-token-generated-at'] as $key => $value) {
$elapsedTime = time() - $value;
if ($elapsedTime > (15 * 60)) {
$tokenKey = array_search((string)$key, $_SESSION['x-centreon-token']);
unset($_SESSION['x-centreon-token'][$tokenKey]);
unset($_SESSION['x-centreon-token-generated-at'][(string)$key]);
}
}
}

// }}}
} // end class HTML_QuickForm

Expand Down

0 comments on commit 38f3895

Please sign in to comment.