Total Complexity | 88 |
Total Lines | 615 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like WebRequest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use WebRequest, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
27 | class WebRequest |
||
28 | { |
||
29 | /** |
||
30 | * @var IGlobalStateProvider Provides access to the global state. |
||
31 | */ |
||
32 | private static $globalStateProvider; |
||
33 | |||
34 | /** |
||
35 | * Returns a boolean value if the request was submitted with the HTTP POST method. |
||
36 | * @return bool |
||
37 | */ |
||
38 | public static function wasPosted() |
||
39 | { |
||
40 | return self::method() === 'POST'; |
||
41 | } |
||
42 | |||
43 | /** |
||
44 | * Gets the HTTP Method used |
||
45 | * @return string|null |
||
46 | */ |
||
47 | public static function method() |
||
48 | { |
||
49 | $server = &self::$globalStateProvider->getServerSuperGlobal(); |
||
50 | |||
51 | if (isset($server['REQUEST_METHOD'])) { |
||
52 | return $server['REQUEST_METHOD']; |
||
53 | } |
||
54 | |||
55 | return null; |
||
56 | } |
||
57 | |||
58 | /** |
||
59 | * Gets a boolean value stating whether the request was served over HTTPS or not. |
||
60 | * @return bool |
||
61 | */ |
||
62 | public static function isHttps() |
||
63 | { |
||
64 | $server = &self::$globalStateProvider->getServerSuperGlobal(); |
||
65 | |||
66 | if (isset($server['HTTP_X_FORWARDED_PROTO'])) { |
||
67 | if ($server['HTTP_X_FORWARDED_PROTO'] === 'https') { |
||
68 | // Client <=> Proxy is encrypted |
||
69 | return true; |
||
70 | } |
||
71 | else { |
||
72 | // Proxy <=> Server link unknown, Client <=> Proxy is not encrypted. |
||
73 | return false; |
||
74 | } |
||
75 | } |
||
76 | |||
77 | if (isset($server['HTTPS'])) { |
||
78 | if ($server['HTTPS'] === 'off') { |
||
79 | // ISAPI on IIS breaks the spec. :( |
||
80 | return false; |
||
81 | } |
||
82 | |||
83 | if ($server['HTTPS'] !== '') { |
||
84 | // Set to a non-empty value |
||
85 | return true; |
||
86 | } |
||
87 | } |
||
88 | |||
89 | return false; |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * Gets the path info |
||
94 | * |
||
95 | * @return array Array of path info segments |
||
96 | */ |
||
97 | public static function pathInfo() |
||
98 | { |
||
99 | $server = &self::$globalStateProvider->getServerSuperGlobal(); |
||
100 | if (!isset($server['PATH_INFO'])) { |
||
101 | return array(); |
||
102 | } |
||
103 | |||
104 | $exploded = explode('/', $server['PATH_INFO']); |
||
105 | |||
106 | // filter out empty values, and reindex from zero. Notably, the first element is always zero, since it starts |
||
107 | // with a / |
||
108 | return array_values(array_filter($exploded)); |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * Gets the remote address of the web request |
||
113 | * @return null|string |
||
114 | */ |
||
115 | public static function remoteAddress() |
||
116 | { |
||
117 | $server = &self::$globalStateProvider->getServerSuperGlobal(); |
||
118 | |||
119 | if (isset($server['REMOTE_ADDR'])) { |
||
120 | return $server['REMOTE_ADDR']; |
||
121 | } |
||
122 | |||
123 | return null; |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * Gets the remote address of the web request |
||
128 | * @return null|string |
||
129 | */ |
||
130 | public static function httpHost() |
||
131 | { |
||
132 | $server = &self::$globalStateProvider->getServerSuperGlobal(); |
||
133 | |||
134 | if (isset($server['HTTP_HOST'])) { |
||
135 | return $server['HTTP_HOST']; |
||
136 | } |
||
137 | |||
138 | return null; |
||
139 | } |
||
140 | |||
141 | /** |
||
142 | * Gets the XFF header contents for the web request |
||
143 | * @return null|string |
||
144 | */ |
||
145 | public static function forwardedAddress() |
||
146 | { |
||
147 | $server = &self::$globalStateProvider->getServerSuperGlobal(); |
||
148 | |||
149 | if (isset($server['HTTP_X_FORWARDED_FOR'])) { |
||
150 | return $server['HTTP_X_FORWARDED_FOR']; |
||
151 | } |
||
152 | |||
153 | return null; |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | * Sets the global state provider. |
||
158 | * |
||
159 | * Almost guaranteed this is not the method you want in production code. |
||
160 | * |
||
161 | * @param IGlobalStateProvider $globalState |
||
162 | */ |
||
163 | public static function setGlobalStateProvider($globalState) |
||
164 | { |
||
165 | self::$globalStateProvider = $globalState; |
||
166 | } |
||
167 | |||
168 | #region POST variables |
||
169 | |||
170 | /** |
||
171 | * @param string $key |
||
172 | * |
||
173 | * @return null|string |
||
174 | */ |
||
175 | public static function postString($key) |
||
176 | { |
||
177 | $post = &self::$globalStateProvider->getPostSuperGlobal(); |
||
178 | if (!array_key_exists($key, $post)) { |
||
179 | return null; |
||
180 | } |
||
181 | |||
182 | if ($post[$key] === "") { |
||
183 | return null; |
||
184 | } |
||
185 | |||
186 | return (string)$post[$key]; |
||
187 | } |
||
188 | |||
189 | /** |
||
190 | * @param string $key |
||
191 | * |
||
192 | * @return null|string |
||
193 | */ |
||
194 | public static function postEmail($key) |
||
195 | { |
||
196 | $post = &self::$globalStateProvider->getPostSuperGlobal(); |
||
197 | if (!array_key_exists($key, $post)) { |
||
198 | return null; |
||
199 | } |
||
200 | |||
201 | $filteredValue = filter_var($post[$key], FILTER_SANITIZE_EMAIL); |
||
202 | |||
203 | if ($filteredValue === false) { |
||
204 | return null; |
||
205 | } |
||
206 | |||
207 | return (string)$filteredValue; |
||
208 | } |
||
209 | |||
210 | /** |
||
211 | * @param string $key |
||
212 | * |
||
213 | * @return int|null |
||
214 | */ |
||
215 | public static function postInt($key) |
||
216 | { |
||
217 | $post = &self::$globalStateProvider->getPostSuperGlobal(); |
||
218 | if (!array_key_exists($key, $post)) { |
||
219 | return null; |
||
220 | } |
||
221 | |||
222 | $filteredValue = filter_var($post[$key], FILTER_VALIDATE_INT, FILTER_NULL_ON_FAILURE); |
||
223 | |||
224 | if ($filteredValue === null) { |
||
225 | return null; |
||
226 | } |
||
227 | |||
228 | return (int)$filteredValue; |
||
229 | } |
||
230 | |||
231 | /** |
||
232 | * @param string $key |
||
233 | * |
||
234 | * @return bool |
||
235 | */ |
||
236 | public static function postBoolean($key) |
||
237 | { |
||
238 | $get = &self::$globalStateProvider->getPostSuperGlobal(); |
||
239 | if (!array_key_exists($key, $get)) { |
||
240 | return false; |
||
241 | } |
||
242 | |||
243 | // presence of parameter only |
||
244 | if ($get[$key] === "") { |
||
245 | return true; |
||
246 | } |
||
247 | |||
248 | if (in_array($get[$key], array(false, 'no', 'off', 0, 'false'), true)) { |
||
249 | return false; |
||
250 | } |
||
251 | |||
252 | return true; |
||
253 | } |
||
254 | |||
255 | #endregion |
||
256 | |||
257 | #region GET variables |
||
258 | |||
259 | /** |
||
260 | * @param string $key |
||
261 | * |
||
262 | * @return bool |
||
263 | */ |
||
264 | public static function getBoolean($key) |
||
265 | { |
||
266 | $get = &self::$globalStateProvider->getGetSuperGlobal(); |
||
267 | if (!array_key_exists($key, $get)) { |
||
268 | return false; |
||
269 | } |
||
270 | |||
271 | // presence of parameter only |
||
272 | if ($get[$key] === "") { |
||
273 | return true; |
||
274 | } |
||
275 | |||
276 | if (in_array($get[$key], array(false, 'no', 'off', 0, 'false'), true)) { |
||
277 | return false; |
||
278 | } |
||
279 | |||
280 | return true; |
||
281 | } |
||
282 | |||
283 | /** |
||
284 | * @param string $key |
||
285 | * |
||
286 | * @return int|null |
||
287 | */ |
||
288 | public static function getInt($key) |
||
289 | { |
||
290 | $get = &self::$globalStateProvider->getGetSuperGlobal(); |
||
291 | if (!array_key_exists($key, $get)) { |
||
292 | return null; |
||
293 | } |
||
294 | |||
295 | $filteredValue = filter_var($get[$key], FILTER_VALIDATE_INT, FILTER_NULL_ON_FAILURE); |
||
296 | |||
297 | if ($filteredValue === null) { |
||
298 | return null; |
||
299 | } |
||
300 | |||
301 | return (int)$filteredValue; |
||
302 | } |
||
303 | |||
304 | /** |
||
305 | * @param string $key |
||
306 | * |
||
307 | * @return null|string |
||
308 | */ |
||
309 | public static function getString($key) |
||
310 | { |
||
311 | $get = &self::$globalStateProvider->getGetSuperGlobal(); |
||
312 | if (!array_key_exists($key, $get)) { |
||
313 | return null; |
||
314 | } |
||
315 | |||
316 | if ($get[$key] === "") { |
||
317 | return null; |
||
318 | } |
||
319 | |||
320 | return (string)$get[$key]; |
||
321 | } |
||
322 | |||
323 | #endregion |
||
324 | |||
325 | /** |
||
326 | * Sets the logged-in user to the specified user. |
||
327 | * |
||
328 | * @param User $user |
||
329 | */ |
||
330 | public static function setLoggedInUser(User $user) |
||
331 | { |
||
332 | $session = &self::$globalStateProvider->getSessionSuperGlobal(); |
||
333 | |||
334 | $session['userID'] = $user->getId(); |
||
335 | unset($session['partialLogin']); |
||
336 | } |
||
337 | |||
338 | public static function setActiveDomain(Domain $domain) |
||
343 | } |
||
344 | |||
345 | /** |
||
346 | * Sets the post-login redirect |
||
347 | * |
||
348 | * @param string|null $uri The URI to redirect to |
||
349 | */ |
||
350 | public static function setPostLoginRedirect($uri = null) |
||
351 | { |
||
352 | $session = &self::$globalStateProvider->getSessionSuperGlobal(); |
||
353 | |||
354 | if ($uri === null) { |
||
355 | $uri = self::requestUri(); |
||
356 | } |
||
357 | |||
358 | $session['returnTo'] = $uri; |
||
359 | } |
||
360 | |||
361 | /** |
||
362 | * @return string|null |
||
363 | */ |
||
364 | public static function requestUri() |
||
365 | { |
||
366 | $server = &self::$globalStateProvider->getServerSuperGlobal(); |
||
367 | |||
368 | if (isset($server['REQUEST_URI'])) { |
||
369 | return $server['REQUEST_URI']; |
||
370 | } |
||
371 | |||
372 | return null; |
||
373 | } |
||
374 | |||
375 | /** |
||
376 | * Clears the post-login redirect |
||
377 | * @return string |
||
378 | */ |
||
379 | public static function clearPostLoginRedirect() |
||
380 | { |
||
381 | $session = &self::$globalStateProvider->getSessionSuperGlobal(); |
||
382 | if (array_key_exists('returnTo', $session)) { |
||
383 | $path = $session['returnTo']; |
||
384 | unset($session['returnTo']); |
||
385 | |||
386 | return $path; |
||
387 | } |
||
388 | |||
389 | return null; |
||
390 | } |
||
391 | |||
392 | /** |
||
393 | * @return string|null |
||
394 | */ |
||
395 | public static function serverName() |
||
396 | { |
||
397 | $server = &self::$globalStateProvider->getServerSuperGlobal(); |
||
398 | |||
399 | if (isset($server['SERVER_NAME'])) { |
||
400 | return $server['SERVER_NAME']; |
||
401 | } |
||
402 | |||
403 | return null; |
||
404 | } |
||
405 | |||
406 | /** |
||
407 | * You probably only want to deal with this through SessionAlert. |
||
408 | * @return void |
||
409 | */ |
||
410 | public static function clearSessionAlertData() |
||
411 | { |
||
412 | $session = &self::$globalStateProvider->getSessionSuperGlobal(); |
||
413 | if (array_key_exists('alerts', $session)) { |
||
414 | unset($session['alerts']); |
||
415 | } |
||
416 | } |
||
417 | |||
418 | /** |
||
419 | * You probably only want to deal with this through SessionAlert. |
||
420 | * |
||
421 | * @return string[] |
||
422 | */ |
||
423 | public static function getSessionAlertData() |
||
424 | { |
||
425 | $session = &self::$globalStateProvider->getSessionSuperGlobal(); |
||
426 | if (array_key_exists('alerts', $session)) { |
||
427 | return $session['alerts']; |
||
428 | } |
||
429 | |||
430 | return array(); |
||
431 | } |
||
432 | |||
433 | /** |
||
434 | * You probably only want to deal with this through SessionAlert. |
||
435 | * |
||
436 | * @param string[] $data |
||
437 | */ |
||
438 | public static function setSessionAlertData($data) |
||
439 | { |
||
440 | $session = &self::$globalStateProvider->getSessionSuperGlobal(); |
||
441 | $session['alerts'] = $data; |
||
442 | } |
||
443 | |||
444 | /** |
||
445 | * You probably only want to deal with this through TokenManager. |
||
446 | * |
||
447 | * @return string[] |
||
448 | */ |
||
449 | public static function getSessionTokenData() |
||
450 | { |
||
451 | $session = &self::$globalStateProvider->getSessionSuperGlobal(); |
||
452 | if (array_key_exists('tokens', $session)) { |
||
453 | return $session['tokens']; |
||
454 | } |
||
455 | |||
456 | return array(); |
||
457 | } |
||
458 | |||
459 | /** |
||
460 | * You probably only want to deal with this through TokenManager. |
||
461 | * |
||
462 | * @param string[] $data |
||
463 | */ |
||
464 | public static function setSessionTokenData($data) |
||
465 | { |
||
466 | $session = &self::$globalStateProvider->getSessionSuperGlobal(); |
||
467 | $session['tokens'] = $data; |
||
468 | } |
||
469 | |||
470 | /** |
||
471 | * @param string $key |
||
472 | * |
||
473 | * @return mixed |
||
474 | */ |
||
475 | public static function getSessionContext($key) |
||
476 | { |
||
477 | $session = &self::$globalStateProvider->getSessionSuperGlobal(); |
||
478 | |||
479 | if (!isset($session['context'])) { |
||
480 | $session['context'] = array(); |
||
481 | } |
||
482 | |||
483 | if (!isset($session['context'][$key])) { |
||
484 | return null; |
||
485 | } |
||
486 | |||
487 | return $session['context'][$key]; |
||
488 | } |
||
489 | |||
490 | /** |
||
491 | * @param string $key |
||
492 | * @param mixed $data |
||
493 | */ |
||
494 | public static function setSessionContext($key, $data) |
||
495 | { |
||
496 | $session = &self::$globalStateProvider->getSessionSuperGlobal(); |
||
497 | |||
498 | if (!isset($session['context'])) { |
||
499 | $session['context'] = array(); |
||
500 | } |
||
501 | |||
502 | $session['context'][$key] = $data; |
||
503 | } |
||
504 | |||
505 | /** |
||
506 | * @return int|null |
||
507 | */ |
||
508 | public static function getSessionUserId() |
||
509 | { |
||
510 | $session = &self::$globalStateProvider->getSessionSuperGlobal(); |
||
511 | |||
512 | return isset($session['userID']) ? (int)$session['userID'] : null; |
||
513 | } |
||
514 | |||
515 | /** |
||
516 | * @return int|null |
||
517 | */ |
||
518 | public static function getSessionDomain() |
||
519 | { |
||
520 | $session = &self::$globalStateProvider->getSessionSuperGlobal(); |
||
521 | |||
522 | return isset($session['domainID']) ? (int)$session['domainID'] : null; |
||
523 | } |
||
524 | |||
525 | /** |
||
526 | * @param User $user |
||
527 | */ |
||
528 | public static function setOAuthPartialLogin(User $user) |
||
529 | { |
||
530 | $session = &self::$globalStateProvider->getSessionSuperGlobal(); |
||
531 | $session['oauthPartialLogin'] = $user->getId(); |
||
532 | } |
||
533 | |||
534 | /** |
||
535 | * @return int|null |
||
536 | */ |
||
537 | public static function getOAuthPartialLogin() |
||
538 | { |
||
539 | $session = &self::$globalStateProvider->getSessionSuperGlobal(); |
||
540 | |||
541 | return isset($session['oauthPartialLogin']) ? (int)$session['oauthPartialLogin'] : null; |
||
542 | } |
||
543 | |||
544 | public static function setAuthPartialLogin($userId, $stage) |
||
545 | { |
||
546 | $session = &self::$globalStateProvider->getSessionSuperGlobal(); |
||
547 | $session['authPartialLoginId'] = $userId; |
||
548 | $session['authPartialLoginStage'] = $stage; |
||
549 | } |
||
550 | |||
551 | public static function getAuthPartialLogin() |
||
552 | { |
||
553 | $session = &self::$globalStateProvider->getSessionSuperGlobal(); |
||
554 | |||
555 | $userId = isset($session['authPartialLoginId']) ? (int)$session['authPartialLoginId'] : null; |
||
556 | $stage = isset($session['authPartialLoginStage']) ? (int)$session['authPartialLoginStage'] : null; |
||
557 | |||
558 | return array($userId, $stage); |
||
559 | } |
||
560 | |||
561 | public static function clearAuthPartialLogin() |
||
562 | { |
||
563 | $session = &self::$globalStateProvider->getSessionSuperGlobal(); |
||
564 | unset($session['authPartialLoginId']); |
||
565 | unset($session['authPartialLoginStage']); |
||
566 | } |
||
567 | |||
568 | /** |
||
569 | * @return null|string |
||
570 | */ |
||
571 | public static function userAgent() |
||
572 | { |
||
573 | $server = &self::$globalStateProvider->getServerSuperGlobal(); |
||
574 | |||
575 | if (isset($server['HTTP_USER_AGENT'])) { |
||
576 | return $server['HTTP_USER_AGENT']; |
||
577 | } |
||
578 | |||
579 | return null; |
||
580 | } |
||
581 | |||
582 | /** |
||
583 | * @return null|string |
||
584 | */ |
||
585 | public static function scriptName() |
||
586 | { |
||
587 | $server = &self::$globalStateProvider->getServerSuperGlobal(); |
||
588 | |||
589 | if (isset($server['SCRIPT_NAME'])) { |
||
590 | return $server['SCRIPT_NAME']; |
||
591 | } |
||
592 | |||
593 | return null; |
||
594 | } |
||
595 | |||
596 | /** |
||
597 | * @return null|string |
||
598 | */ |
||
599 | public static function origin() |
||
608 | } |
||
609 | |||
610 | public static function httpHeader(string $headerName): ?string { |
||
611 | $server = &self::$globalStateProvider->getServerSuperGlobal(); |
||
612 | |||
613 | $headerKey = strtoupper("HTTP_" . str_replace('-', '_', $headerName)); |
||
614 | |||
615 | if (isset($server[$headerKey])) { |
||
616 | return $server[$headerKey]; |
||
617 | } |
||
618 | |||
619 | return null; |
||
620 | } |
||
621 | |||
622 | public static function testSiteNoticeCookieValue($expectedHash) |
||
623 | { |
||
624 | $cookie = &self::$globalStateProvider->getCookieSuperGlobal(); |
||
631 | } |
||
632 | |||
633 | public static function requestListDefaultSort() |
||
642 | } |
||
643 | } |
||
644 | } |
||
645 |