2013-07-18 07:09:21 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class ParameterConfirmationToken
|
|
|
|
*
|
|
|
|
* When you need to use a dangerous GET parameter that needs to be set before core/Core.php is
|
|
|
|
* established, this class takes care of allowing some other code of confirming the parameter,
|
|
|
|
* by generating a one-time-use token & redirecting with that token included in the redirected URL
|
|
|
|
*
|
|
|
|
* WARNING: This class is experimental and designed specifically for use pre-startup in main.php
|
|
|
|
* It will likely be heavily refactored before the release of 3.2
|
|
|
|
*/
|
|
|
|
class ParameterConfirmationToken {
|
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
|
|
|
|
|
|
|
/**
|
|
|
|
* The validated and checked token for this parameter
|
|
|
|
*
|
|
|
|
* @var string|null A string value, or null if either not provided or invalid
|
|
|
|
*/
|
2013-07-18 07:09:21 +02:00
|
|
|
protected $token = null;
|
|
|
|
|
|
|
|
protected function pathForToken($token) {
|
2013-07-22 03:52:00 +02:00
|
|
|
return TEMP_FOLDER.'/token_'.preg_replace('/[^a-z0-9]+/', '', $token);
|
2013-07-18 07:09:21 +02:00
|
|
|
}
|
|
|
|
|
2015-05-22 02:58:20 +02:00
|
|
|
/**
|
|
|
|
* Generate a new random token and store it
|
|
|
|
*
|
|
|
|
* @return string Token name
|
|
|
|
*/
|
2013-07-18 07:09:21 +02:00
|
|
|
protected function genToken() {
|
|
|
|
// Generate a new random token (as random as possible)
|
2013-07-19 04:33:56 +02:00
|
|
|
require_once(dirname(dirname(dirname(__FILE__))).'/security/RandomGenerator.php');
|
2013-07-18 07:09:21 +02:00
|
|
|
$rg = new RandomGenerator();
|
|
|
|
$token = $rg->randomToken('md5');
|
|
|
|
|
|
|
|
// Store a file in the session save path (safer than /tmp, as open_basedir might limit that)
|
|
|
|
file_put_contents($this->pathForToken($token), $token);
|
|
|
|
|
|
|
|
return $token;
|
|
|
|
}
|
|
|
|
|
2015-05-22 02:58:20 +02:00
|
|
|
/**
|
|
|
|
* Validate a token
|
|
|
|
*
|
|
|
|
* @param string $token
|
|
|
|
* @return boolean True if the token is valid
|
|
|
|
*/
|
2013-07-18 07:09:21 +02:00
|
|
|
protected function checkToken($token) {
|
2015-05-22 02:58:20 +02:00
|
|
|
if(!$token) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-07-18 07:09:21 +02:00
|
|
|
$file = $this->pathForToken($token);
|
|
|
|
$content = null;
|
|
|
|
|
|
|
|
if (file_exists($file)) {
|
|
|
|
$content = file_get_contents($file);
|
|
|
|
unlink($file);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $content == $token;
|
|
|
|
}
|
|
|
|
|
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;
|
2015-05-22 02:58:20 +02: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
|
|
|
|
2014-07-01 09:07:13 +02:00
|
|
|
/**
|
|
|
|
* Is the parameter requested?
|
2015-05-22 02:58:20 +02:00
|
|
|
* ?parameter and ?parameter=1 are both considered requested
|
2014-07-01 09:07:13 +02:00
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2013-07-18 07:09:21 +02:00
|
|
|
public function parameterProvided() {
|
|
|
|
return $this->parameter !== null;
|
|
|
|
}
|
|
|
|
|
2014-07-01 09:07:13 +02:00
|
|
|
/**
|
|
|
|
* Is the necessary token provided for this parameter?
|
2015-05-22 02:58:20 +02:00
|
|
|
* A value must be provided for the token
|
2014-07-01 09:07:13 +02:00
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2013-07-18 07:09:21 +02:00
|
|
|
public function tokenProvided() {
|
2015-05-22 02:58:20 +02:00
|
|
|
return !empty($this->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
|
|
|
/**
|
|
|
|
* Is this parameter requested without a valid token?
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2014-07-01 09:07:13 +02:00
|
|
|
* @return bool True if the parameter is given without a valid token
|
|
|
|
*/
|
|
|
|
public function reloadRequired() {
|
|
|
|
return $this->parameterProvided() && !$this->tokenProvided();
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-07-01 09:07:13 +02:00
|
|
|
/**
|
|
|
|
* Suppress the current parameter by unsetting it from $_GET
|
|
|
|
*/
|
|
|
|
public function suppress() {
|
|
|
|
unset($_GET[$this->parameterName]);
|
|
|
|
}
|
2013-07-18 07:09:21 +02:00
|
|
|
|
2015-05-22 02:58:20 +02:00
|
|
|
/**
|
|
|
|
* Determine the querystring parameters to include
|
|
|
|
*
|
|
|
|
* @return array List of querystring parameters with name and token parameters
|
|
|
|
*/
|
2013-07-18 07:09:21 +02:00
|
|
|
public function params() {
|
|
|
|
return array(
|
|
|
|
$this->parameterName => $this->parameter,
|
|
|
|
$this->parameterName.'token' => $this->genToken()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2013-08-19 01:35:34 +02:00
|
|
|
/** What to use instead of BASE_URL. Must not contain protocol or host. @var string */
|
|
|
|
static public $alternateBaseURL = null;
|
|
|
|
|
|
|
|
protected function currentAbsoluteURL() {
|
2013-07-18 07:09:21 +02:00
|
|
|
global $url;
|
|
|
|
|
2014-05-22 08:34:15 +02:00
|
|
|
// Are we http or https? Replicates Director::is_https() without its dependencies/
|
2013-07-18 07:09:21 +02:00
|
|
|
$proto = 'http';
|
2014-05-22 08:34:15 +02:00
|
|
|
if(
|
2015-05-25 04:31:20 +02:00
|
|
|
TRUSTED_PROXY
|
|
|
|
&& isset($_SERVER['HTTP_X_FORWARDED_PROTO'])
|
2014-05-22 08:34:15 +02:00
|
|
|
&& strtolower($_SERVER['HTTP_X_FORWARDED_PROTO']) == 'https'
|
2014-08-15 08:53:05 +02:00
|
|
|
) {
|
2014-05-22 08:34:15 +02:00
|
|
|
// Convention for (non-standard) proxy signaling a HTTPS forward,
|
|
|
|
// see https://en.wikipedia.org/wiki/List_of_HTTP_header_fields
|
|
|
|
$proto = 'https';
|
|
|
|
} else if(
|
2015-05-25 04:31:20 +02:00
|
|
|
TRUSTED_PROXY
|
|
|
|
&& isset($_SERVER['HTTP_X_FORWARDED_PROTOCOL'])
|
2014-05-22 08:34:15 +02:00
|
|
|
&& strtolower($_SERVER['HTTP_X_FORWARDED_PROTOCOL']) == 'https'
|
2014-08-15 08:53:05 +02:00
|
|
|
) {
|
2014-05-22 08:34:15 +02:00
|
|
|
// Less conventional proxy header
|
|
|
|
$proto = 'https';
|
|
|
|
} else if(
|
|
|
|
isset($_SERVER['HTTP_FRONT_END_HTTPS'])
|
|
|
|
&& strtolower($_SERVER['HTTP_FRONT_END_HTTPS']) == 'on'
|
2014-08-15 08:53:05 +02:00
|
|
|
) {
|
2014-05-22 08:34:15 +02:00
|
|
|
// Microsoft proxy convention: https://support.microsoft.com/?kbID=307347
|
|
|
|
$proto = 'https';
|
|
|
|
} else if((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off')) {
|
|
|
|
$proto = 'https';
|
|
|
|
} else if(isset($_SERVER['SSL'])) {
|
|
|
|
$proto = 'https';
|
2013-07-18 07:09:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off')) $proto = 'https';
|
|
|
|
if(isset($_SERVER['SSL'])) $proto = 'https';
|
|
|
|
|
2013-08-19 01:35:34 +02:00
|
|
|
$parts = array_filter(array(
|
|
|
|
// What's our host
|
|
|
|
$_SERVER['HTTP_HOST'],
|
|
|
|
// SilverStripe base
|
|
|
|
self::$alternateBaseURL !== null ? self::$alternateBaseURL : BASE_URL,
|
|
|
|
// And URL
|
|
|
|
$url
|
|
|
|
));
|
|
|
|
|
|
|
|
// Join together with protocol into our current absolute URL, avoiding duplicated "/" characters
|
|
|
|
return "$proto://" . preg_replace('#/{2,}#', '/', implode('/', $parts));
|
|
|
|
}
|
|
|
|
|
2015-05-22 02:58:20 +02:00
|
|
|
/**
|
|
|
|
* Forces a reload of the request with the token included
|
|
|
|
* This method will terminate the script with `die`
|
|
|
|
*/
|
2013-08-19 01:35:34 +02:00
|
|
|
public function reloadWithToken() {
|
|
|
|
$location = $this->currentAbsoluteURL();
|
2013-07-18 07:09:21 +02:00
|
|
|
|
|
|
|
// What's our GET params (ensuring they include the original parameter + a new token)
|
|
|
|
$params = array_merge($_GET, $this->params());
|
|
|
|
unset($params['url']);
|
|
|
|
|
2013-08-19 01:35:34 +02:00
|
|
|
if ($params) $location .= '?'.http_build_query($params);
|
2013-07-18 07:09:21 +02:00
|
|
|
|
|
|
|
// And redirect
|
2013-07-31 23:44:36 +02:00
|
|
|
if (headers_sent()) {
|
|
|
|
echo "
|
|
|
|
<script>location.href='$location';</script>
|
|
|
|
<noscript><meta http-equiv='refresh' content='0; url=$location'></noscript>
|
|
|
|
You are being redirected. If you are not redirected soon, <a href='$location'>click here to continue the flush</a>
|
|
|
|
";
|
|
|
|
}
|
|
|
|
else header('location: '.$location, true, 302);
|
2013-07-18 07:09:21 +02:00
|
|
|
die;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-07-01 09:07:13 +02:00
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* Given a list of token names, suppress all tokens that have not been validated, and
|
2014-07-01 09:07:13 +02:00
|
|
|
* return the non-validated token with the highest priority
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2015-05-22 02:58:20 +02:00
|
|
|
* @param array $keys List of token keys in ascending priority (low to high)
|
2014-07-01 09:07:13 +02:00
|
|
|
* @return ParameterConfirmationToken The token container for the unvalidated $key given with the highest priority
|
|
|
|
*/
|
|
|
|
public static function prepare_tokens($keys) {
|
|
|
|
$target = null;
|
|
|
|
foreach($keys as $key) {
|
|
|
|
$token = new ParameterConfirmationToken($key);
|
|
|
|
// Validate this token
|
|
|
|
if($token->reloadRequired()) {
|
|
|
|
$token->suppress();
|
|
|
|
$target = $token;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $target;
|
|
|
|
}
|
2013-07-18 07:09:21 +02:00
|
|
|
}
|