1 | <?php |
||
16 | class ParameterConfirmationToken { |
||
17 | |||
18 | /** |
||
19 | * The name of the parameter |
||
20 | * |
||
21 | * @var string |
||
22 | */ |
||
23 | protected $parameterName = null; |
||
24 | |||
25 | /** |
||
26 | * The parameter given |
||
27 | * |
||
28 | * @var string|null The string value, or null if not provided |
||
29 | */ |
||
30 | protected $parameter = null; |
||
31 | |||
32 | /** |
||
33 | * The validated and checked token for this parameter |
||
34 | * |
||
35 | * @var string|null A string value, or null if either not provided or invalid |
||
36 | */ |
||
37 | protected $token = null; |
||
38 | |||
39 | protected function pathForToken($token) { |
||
42 | |||
43 | /** |
||
44 | * Generate a new random token and store it |
||
45 | * |
||
46 | * @return string Token name |
||
47 | */ |
||
48 | protected function genToken() { |
||
49 | // Generate a new random token (as random as possible) |
||
50 | require_once(dirname(dirname(dirname(__FILE__))).'/security/RandomGenerator.php'); |
||
51 | $rg = new RandomGenerator(); |
||
52 | $token = $rg->randomToken('md5'); |
||
53 | |||
54 | // Store a file in the session save path (safer than /tmp, as open_basedir might limit that) |
||
55 | file_put_contents($this->pathForToken($token), $token); |
||
56 | |||
57 | return $token; |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * Validate a token |
||
62 | * |
||
63 | * @param string $token |
||
64 | * @return boolean True if the token is valid |
||
65 | */ |
||
66 | protected function checkToken($token) { |
||
67 | if(!$token) { |
||
68 | return false; |
||
69 | } |
||
70 | |||
71 | $file = $this->pathForToken($token); |
||
72 | $content = null; |
||
73 | |||
74 | if (file_exists($file)) { |
||
75 | $content = file_get_contents($file); |
||
76 | unlink($file); |
||
77 | } |
||
78 | |||
79 | return $content == $token; |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * Create a new ParameterConfirmationToken |
||
84 | * |
||
85 | * @param string $parameterName Name of the querystring parameter to check |
||
86 | */ |
||
87 | public function __construct($parameterName) { |
||
100 | |||
101 | /** |
||
102 | * Get the name of this token |
||
103 | * |
||
104 | * @return string |
||
105 | */ |
||
106 | public function getName() { |
||
109 | |||
110 | /** |
||
111 | * Is the parameter requested? |
||
112 | * ?parameter and ?parameter=1 are both considered requested |
||
113 | * |
||
114 | * @return bool |
||
115 | */ |
||
116 | public function parameterProvided() { |
||
119 | |||
120 | /** |
||
121 | * Is the necessary token provided for this parameter? |
||
122 | * A value must be provided for the token |
||
123 | * |
||
124 | * @return bool |
||
125 | */ |
||
126 | public function tokenProvided() { |
||
129 | |||
130 | /** |
||
131 | * Is this parameter requested without a valid token? |
||
132 | * |
||
133 | * @return bool True if the parameter is given without a valid token |
||
134 | */ |
||
135 | public function reloadRequired() { |
||
138 | |||
139 | /** |
||
140 | * Suppress the current parameter by unsetting it from $_GET |
||
141 | */ |
||
142 | public function suppress() { |
||
145 | |||
146 | /** |
||
147 | * Determine the querystring parameters to include |
||
148 | * |
||
149 | * @return array List of querystring parameters with name and token parameters |
||
150 | */ |
||
151 | public function params() { |
||
157 | |||
158 | /** What to use instead of BASE_URL. Must not contain protocol or host. @var string */ |
||
159 | static public $alternateBaseURL = null; |
||
160 | |||
161 | protected function currentAbsoluteURL() { |
||
162 | global $url; |
||
163 | |||
164 | // Are we http or https? Replicates Director::is_https() without its dependencies/ |
||
165 | $proto = 'http'; |
||
166 | // See https://en.wikipedia.org/wiki/List_of_HTTP_header_fields |
||
167 | // See https://support.microsoft.com/en-us/kb/307347 |
||
168 | $headerOverride = false; |
||
169 | if(TRUSTED_PROXY) { |
||
170 | $headers = (defined('SS_TRUSTED_PROXY_PROTOCOL_HEADER')) ? array(SS_TRUSTED_PROXY_PROTOCOL_HEADER) : null; |
||
171 | if(!$headers) { |
||
172 | // Backwards compatible defaults |
||
173 | $headers = array('HTTP_X_FORWARDED_PROTO', 'HTTP_X_FORWARDED_PROTOCOL', 'HTTP_FRONT_END_HTTPS'); |
||
174 | } |
||
175 | foreach($headers as $header) { |
||
176 | $headerCompareVal = ($header === 'HTTP_FRONT_END_HTTPS' ? 'on' : 'https'); |
||
177 | if(!empty($_SERVER[$header]) && strtolower($_SERVER[$header]) == $headerCompareVal) { |
||
178 | $headerOverride = true; |
||
179 | break; |
||
180 | } |
||
181 | } |
||
182 | } |
||
183 | |||
184 | if($headerOverride) { |
||
185 | $proto = 'https'; |
||
186 | } else if((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off')) { |
||
187 | $proto = 'https'; |
||
188 | } else if(isset($_SERVER['SSL'])) { |
||
189 | $proto = 'https'; |
||
190 | } |
||
191 | |||
192 | $parts = array_filter(array( |
||
193 | // What's our host |
||
194 | $_SERVER['HTTP_HOST'], |
||
195 | // SilverStripe base |
||
196 | self::$alternateBaseURL !== null ? self::$alternateBaseURL : BASE_URL, |
||
197 | // And URL including base script (eg: if it's index.php/page/url/) |
||
198 | (defined('BASE_SCRIPT_URL') ? '/' . BASE_SCRIPT_URL : '') . $url, |
||
199 | )); |
||
200 | |||
201 | // Join together with protocol into our current absolute URL, avoiding duplicated "/" characters |
||
202 | return "$proto://" . preg_replace('#/{2,}#', '/', implode('/', $parts)); |
||
203 | } |
||
204 | |||
205 | /** |
||
206 | * Forces a reload of the request with the token included |
||
207 | * This method will terminate the script with `die` |
||
208 | */ |
||
209 | public function reloadWithToken() { |
||
229 | |||
230 | /** |
||
231 | * Given a list of token names, suppress all tokens that have not been validated, and |
||
232 | * return the non-validated token with the highest priority |
||
233 | * |
||
234 | * @param array $keys List of token keys in ascending priority (low to high) |
||
235 | * @return ParameterConfirmationToken The token container for the unvalidated $key given with the highest priority |
||
236 | */ |
||
237 | public static function prepare_tokens($keys) { |
||
249 | } |
||
250 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: