1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
require_once(dirname(__FILE__).'/AbstractConfirmationToken.php'); |
4
|
|
|
require_once(dirname(dirname(dirname(__FILE__))).'/control/Director.php'); |
5
|
|
|
require_once(dirname(dirname(dirname(__FILE__))).'/view/TemplateGlobalProvider.php'); |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* This is used to protect dangerous URLs that need to be detected early in the request lifecycle |
9
|
|
|
* by generating a one-time-use token & redirecting with that token included in the redirected URL |
10
|
|
|
* |
11
|
|
|
* @internal This class is designed specifically for use pre-startup and may change without warning |
12
|
|
|
*/ |
13
|
|
|
class URLConfirmationToken extends AbstractConfirmationToken { |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
protected $urlToCheck; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $currentURL; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected $tokenParameterName; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param string $urlToCheck URL to check |
32
|
|
|
*/ |
33
|
|
|
public function __construct($urlToCheck) |
|
|
|
|
34
|
|
|
{ |
35
|
|
|
$this->urlToCheck = $urlToCheck; |
36
|
|
|
global $url; |
|
|
|
|
37
|
|
|
// Strip leading/trailing slashes |
38
|
|
|
$this->currentURL = preg_replace(array('/\/+/','/^\//', '/\/$/'), array('/','',''), $url); |
39
|
|
|
$this->tokenParameterName = preg_replace('/[^a-z0-9]/i', '', $urlToCheck) . 'token'; |
40
|
|
|
|
41
|
|
|
// If the token provided is valid, mark it as such |
42
|
|
|
$token = isset($_GET[$this->tokenParameterName]) ? $_GET[$this->tokenParameterName] : null; |
43
|
|
|
if ($this->checkToken($token)) { |
44
|
|
|
$this->token = $token; |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return bool |
50
|
|
|
*/ |
51
|
|
|
protected function urlMatches() { |
52
|
|
|
return ($this->currentURL === $this->urlToCheck); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @return string |
57
|
|
|
*/ |
58
|
|
|
public function getURLToCheck() { |
59
|
|
|
return $this->urlToCheck; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function reloadRequired() { |
63
|
|
|
return $this->urlMatches() && !$this->tokenProvided(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function suppress() { |
|
|
|
|
67
|
|
|
$_SERVER['REQUEST_URI'] = '/'; |
68
|
|
|
$_GET['url'] = $_REQUEST['url'] = '/'; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function params($includeToken = true) { |
72
|
|
|
$params = array(); |
73
|
|
|
if ($includeToken) { |
74
|
|
|
$params[$this->tokenParameterName] = $this->genToken(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $params; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function currentURL() { |
81
|
|
|
return Director::baseURL() . $this->currentURL; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function getRedirectUrlBase() { |
85
|
|
|
return (!$this->urlMatches()) ? Director::baseURL() : $this->currentURL(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function getRedirectUrlParams() { |
|
|
|
|
89
|
|
|
$params = (!$this->urlMatches()) |
90
|
|
|
? $this->params() |
91
|
|
|
: array_merge($_GET, $this->params()); |
92
|
|
|
|
93
|
|
|
if (isset($params['url'])) { |
94
|
|
|
unset($params['url']); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $params; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
protected function redirectURL() { |
101
|
|
|
$query = http_build_query($this->getRedirectUrlParams()); |
102
|
|
|
return $this->getRedirectUrlBase() . '?' . $query; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.