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() { |
||
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) { |
||
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() { |
||
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: