2013-07-18 07:09:21 +02:00
|
|
|
<?php
|
|
|
|
|
2018-09-26 18:33:32 +02:00
|
|
|
require_once(dirname(__FILE__).'/AbstractConfirmationToken.php');
|
|
|
|
require_once(dirname(dirname(dirname(__FILE__))).'/view/TemplateGlobalProvider.php');
|
|
|
|
require_once(dirname(dirname(dirname(__FILE__))).'/control/Director.php');
|
|
|
|
|
2013-07-18 07:09:21 +02:00
|
|
|
/**
|
2018-09-26 18:33:32 +02:00
|
|
|
* This is used to protect dangerous GET parameters that need to be detected early in the request
|
|
|
|
* lifecycle by generating a one-time-use token & redirecting with that token included in the
|
|
|
|
* redirected URL
|
2013-07-18 07:09:21 +02:00
|
|
|
*
|
2018-09-26 18:33:32 +02:00
|
|
|
* @internal This class is designed specifically for use pre-startup and may change without warning
|
2013-07-18 07:09:21 +02:00
|
|
|
*/
|
2018-09-26 18:33:32 +02:00
|
|
|
class ParameterConfirmationToken extends AbstractConfirmationToken {
|
2015-05-22 02:58:20 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The name of the parameter
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2013-07-18 07:09:21 +02:00
|
|
|
protected $parameterName = null;
|
2015-05-22 02:58:20 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The parameter given
|
|
|
|
*
|
|
|
|
* @var string|null The string value, or null if not provided
|
|
|
|
*/
|
2013-07-18 07:09:21 +02:00
|
|
|
protected $parameter = null;
|
2015-05-22 02:58:20 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new ParameterConfirmationToken
|
|
|
|
*
|
|
|
|
* @param string $parameterName Name of the querystring parameter to check
|
|
|
|
*/
|
2013-07-18 07:09:21 +02:00
|
|
|
public function __construct($parameterName) {
|
|
|
|
// Store the parameter name
|
|
|
|
$this->parameterName = $parameterName;
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2013-07-18 07:09:21 +02:00
|
|
|
// Store the parameter value
|
|
|
|
$this->parameter = isset($_GET[$parameterName]) ? $_GET[$parameterName] : null;
|
|
|
|
|
2015-05-22 02:58:20 +02:00
|
|
|
// If the token provided is valid, mark it as such
|
|
|
|
$token = isset($_GET[$parameterName.'token']) ? $_GET[$parameterName.'token'] : null;
|
|
|
|
if ($this->checkToken($token)) {
|
|
|
|
$this->token = $token;
|
|
|
|
}
|
2013-07-18 07:09:21 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-07-01 09:07:13 +02:00
|
|
|
/**
|
|
|
|
* Get the name of this token
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2014-07-01 09:07:13 +02:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getName() {
|
|
|
|
return $this->parameterName;
|
|
|
|
}
|
2013-07-18 07:09:21 +02:00
|
|
|
|
|
|
|
public function parameterProvided() {
|
|
|
|
return $this->parameter !== null;
|
|
|
|
}
|
|
|
|
|
2014-07-01 09:07:13 +02:00
|
|
|
public function reloadRequired() {
|
|
|
|
return $this->parameterProvided() && !$this->tokenProvided();
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-07-01 09:07:13 +02:00
|
|
|
public function suppress() {
|
|
|
|
unset($_GET[$this->parameterName]);
|
|
|
|
}
|
2013-07-18 07:09:21 +02:00
|
|
|
|
2018-09-26 18:33:32 +02:00
|
|
|
public function params($includeToken = true) {
|
|
|
|
$params = array(
|
2013-07-18 07:09:21 +02:00
|
|
|
$this->parameterName => $this->parameter,
|
|
|
|
);
|
2018-09-26 18:33:32 +02:00
|
|
|
if ($includeToken) {
|
|
|
|
$params[$this->parameterName . 'token'] = $this->genToken();
|
2013-07-18 07:09:21 +02:00
|
|
|
}
|
2018-09-26 18:33:32 +02:00
|
|
|
return $params;
|
|
|
|
}
|
2013-07-18 07:09:21 +02:00
|
|
|
|
2018-09-26 18:33:32 +02:00
|
|
|
public function getRedirectUrlBase() {
|
|
|
|
return (!$this->parameterProvided()) ? Director::baseURL() : $this->currentAbsoluteURL();
|
2013-08-19 01:35:34 +02:00
|
|
|
}
|
|
|
|
|
2018-09-26 18:33:32 +02:00
|
|
|
public function getRedirectUrlParams() {
|
|
|
|
$params = (!$this->parameterProvided())
|
|
|
|
? $this->params()
|
|
|
|
: array_merge($_GET, $this->params());
|
|
|
|
|
|
|
|
if (isset($params['url'])) {
|
|
|
|
unset($params['url']);
|
2013-07-31 23:44:36 +02:00
|
|
|
}
|
2018-09-26 18:33:32 +02:00
|
|
|
|
|
|
|
return $params;
|
2013-07-18 07:09:21 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2018-09-26 18:33:32 +02:00
|
|
|
protected function redirectURL() {
|
|
|
|
$query = http_build_query($this->getRedirectUrlParams());
|
|
|
|
return $this->getRedirectUrlBase() . '?' . $query;
|
2014-07-01 09:07:13 +02:00
|
|
|
}
|
2013-07-18 07:09:21 +02:00
|
|
|
}
|