1 | <?php |
||
31 | class Security |
||
32 | { |
||
33 | private $errors = array(); |
||
34 | |||
35 | /** |
||
36 | * Check if there is a valid token in $_REQUEST[$name . '_REQUEST'] |
||
37 | * |
||
38 | * @param bool $clearIfValid whether to clear the token after validation |
||
39 | * @param string|false $token token to validate |
||
40 | * @param string $name name of session variable |
||
41 | * |
||
42 | * @return bool |
||
43 | */ |
||
44 | 1 | public function check($clearIfValid = true, $token = false, $name = 'XOOPS_TOKEN') |
|
48 | |||
49 | /** |
||
50 | * Create a token in the user's session |
||
51 | * |
||
52 | * @param int $timeout time in seconds the token should be valid |
||
53 | * @param string $name name of session variable |
||
54 | * |
||
55 | * @return string token value |
||
56 | */ |
||
57 | 10 | public function createToken($timeout = 300, $name = 'XOOPS_TOKEN') |
|
|
|||
58 | { |
||
59 | 10 | $this->garbageCollection($name); |
|
60 | 10 | $timeout = ($timeout <= 0) ? 300 : $timeout; |
|
61 | 10 | $token_id = Random::generateOneTimeToken(); |
|
62 | // save token data on the server |
||
63 | 10 | if (!isset($_SESSION[$name . '_SESSION'])) { |
|
64 | 7 | $_SESSION[$name . '_SESSION'] = array(); |
|
65 | } |
||
66 | $token_data = array( |
||
67 | 10 | 'id' => $token_id, 'expire' => time() + (int)($timeout) |
|
68 | ); |
||
69 | 10 | array_push($_SESSION[$name . '_SESSION'], $token_data); |
|
70 | 10 | return $token_id; |
|
71 | } |
||
72 | |||
73 | /** |
||
74 | * Check if a token is valid. If no token is specified, $_REQUEST[$name . '_REQUEST'] is checked |
||
75 | * |
||
76 | * @param string|false $token token to validate |
||
77 | * @param bool $clearIfValid whether to clear the token value if valid |
||
78 | * @param string $name session name to validate |
||
79 | * |
||
80 | * @return bool |
||
81 | */ |
||
82 | 2 | public function validateToken($token = false, $clearIfValid = true, $name = 'XOOPS_TOKEN') |
|
121 | |||
122 | /** |
||
123 | * Clear all token values from user's session |
||
124 | * |
||
125 | * @param string $name session name |
||
126 | * |
||
127 | * @return void |
||
128 | */ |
||
129 | 1 | public function clearTokens($name = 'XOOPS_TOKEN') |
|
133 | |||
134 | /** |
||
135 | * Check whether a token value is expired or not |
||
136 | * |
||
137 | * @param string $token token |
||
138 | * |
||
139 | * @return bool |
||
140 | */ |
||
141 | 8 | public function filterToken($token) |
|
145 | |||
146 | /** |
||
147 | * Perform garbage collection, clearing expired tokens |
||
148 | * |
||
149 | * @param string $name session name |
||
150 | * |
||
151 | * @return void |
||
152 | */ |
||
153 | 10 | public function garbageCollection($name = 'XOOPS_TOKEN') |
|
160 | |||
161 | /** |
||
162 | * Check the user agent's HTTP REFERER against XOOPS_URL |
||
163 | * |
||
164 | * @param int $docheck 0 to not check the referer (used with XML-RPC), 1 to actively check it |
||
165 | * |
||
166 | * @return bool |
||
167 | */ |
||
168 | 1 | public function checkReferer($docheck = 1) |
|
182 | |||
183 | /** |
||
184 | * Check if visitor's IP address is banned |
||
185 | * Should be changed to return bool and let the action be up to the calling script |
||
186 | * |
||
187 | * @return void |
||
188 | */ |
||
189 | 1 | public function checkBadips() |
|
190 | { |
||
191 | 1 | $xoops = \Xoops::getInstance(); |
|
192 | 1 | if ($xoops->getConfig('enable_badips') == 1 |
|
193 | 1 | && isset($_SERVER['REMOTE_ADDR']) |
|
194 | 1 | && $_SERVER['REMOTE_ADDR'] != '' |
|
195 | ) { |
||
196 | 1 | foreach ($xoops->getConfig('bad_ips') as $bi) { |
|
197 | 1 | if (!empty($bi) && preg_match('/' . $bi . '/', $_SERVER['REMOTE_ADDR'])) { |
|
198 | exit(); |
||
199 | } |
||
200 | } |
||
201 | } |
||
202 | 1 | } |
|
203 | |||
204 | /** |
||
205 | * Get the HTML code for a Xoops\Form\Token object - provides a hidden token field |
||
206 | * used in forms that do not use Xoops\Form elements |
||
207 | * |
||
208 | * @param string $name session token name |
||
209 | * |
||
210 | * @return string |
||
211 | */ |
||
212 | 2 | public function getTokenHTML($name = 'XOOPS_TOKEN') |
|
217 | |||
218 | /** |
||
219 | * Add an error |
||
220 | * |
||
221 | * @param string $error message |
||
222 | * |
||
223 | * @return void |
||
224 | */ |
||
225 | 3 | public function setErrors($error) |
|
229 | |||
230 | /** |
||
231 | * Get generated errors |
||
232 | * |
||
233 | * @param bool $ashtml Format using HTML? |
||
234 | * |
||
235 | * @return array|string Array of array messages OR HTML string |
||
236 | */ |
||
237 | 3 | public function getErrors($ashtml = false) |
|
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: