WebRequest::setSessionTokenData()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
cc 1
nc 1
nop 1
crap 2
1
<?php
2
/******************************************************************************
3
 * Wikipedia Account Creation Assistance tool                                 *
4
 * ACC Development Team. Please see team.json for a list of contributors.     *
5
 *                                                                            *
6
 * This is free and unencumbered software released into the public domain.    *
7
 * Please see LICENSE.md for the full licencing statement.                    *
8
 ******************************************************************************/
9
10
/** @noinspection PhpPassByRefInspection - disable seemingly broken check in PhpStorm */
11
12
namespace Waca;
13
14
use Waca\DataObjects\Domain;
15
use Waca\DataObjects\User;
16
use Waca\Providers\GlobalState\IGlobalStateProvider;
17
18
/**
19
 * Holds helper functions regarding the current request.
20
 *
21
 * This is the only place where it is allowed to use super-globals, but even then access MUST be pushed through the
22
 * global state provider to allow for unit tests. It's strongly recommended to do sanitising of data here, especially
23
 * if extra logic is required to get a deterministic value, like isHttps().
24
 *
25
 * @package Waca
26
 */
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 3
     */
38
    public static function wasPosted()
39 3
    {
40
        return self::method() === 'POST';
41
    }
42
43
    /**
44
     * Gets the HTTP Method used
45
     * @return string|null
46 3
     */
47
    public static function method()
48 3
    {
49
        $server = &self::$globalStateProvider->getServerSuperGlobal();
50 3
51 2
        if (isset($server['REQUEST_METHOD'])) {
52
            return $server['REQUEST_METHOD'];
53
        }
54 1
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 7
     */
62
    public static function isHttps()
63 7
    {
64
        $server = &self::$globalStateProvider->getServerSuperGlobal();
65 7
66 3
        if (isset($server['HTTP_X_FORWARDED_PROTO'])) {
67
            if ($server['HTTP_X_FORWARDED_PROTO'] === 'https') {
68 2
                // Client <=> Proxy is encrypted
69
                return true;
70
            }
71
            else {
72 1
                // Proxy <=> Server link unknown, Client <=> Proxy is not encrypted.
73
                return false;
74
            }
75
        }
76 4
77 3
        if (isset($server['HTTPS'])) {
78
            if ($server['HTTPS'] === 'off') {
79 1
                // ISAPI on IIS breaks the spec. :(
80
                return false;
81
            }
82 2
83
            if ($server['HTTPS'] !== '') {
84 2
                // Set to a non-empty value
85
                return true;
86
            }
87
        }
88 1
89
        return false;
90
    }
91
92
    /**
93
     * Gets the path info
94
     *
95
     * @return array Array of path info segments
96 13
     */
97
    public static function pathInfo()
98 13
    {
99 13
        $server = &self::$globalStateProvider->getServerSuperGlobal();
100 1
        if (!isset($server['PATH_INFO'])) {
101
            return array();
102
        }
103 12
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 12
        // 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 2
     */
115
    public static function remoteAddress()
116 2
    {
117
        $server = &self::$globalStateProvider->getServerSuperGlobal();
118 2
119 1
        if (isset($server['REMOTE_ADDR'])) {
120
            return $server['REMOTE_ADDR'];
121
        }
122 1
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 2
     */
145
    public static function forwardedAddress()
146 2
    {
147
        $server = &self::$globalStateProvider->getServerSuperGlobal();
148 2
149 1
        if (isset($server['HTTP_X_FORWARDED_FOR'])) {
150
            return $server['HTTP_X_FORWARDED_FOR'];
151
        }
152 1
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 51
     */
163
    public static function setGlobalStateProvider($globalState)
164 51
    {
165
        self::$globalStateProvider = $globalState;
166
    }
167
168
    #region POST variables
169
170
    /**
171
     * @param string $key
172
     *
173
     * @return null|string
174 1
     */
175
    public static function postString($key)
176 1
    {
177 1
        $post = &self::$globalStateProvider->getPostSuperGlobal();
178
        if (!array_key_exists($key, $post)) {
179
            return null;
180
        }
181 1
182 1
        if ($post[$key] === "") {
183
            return null;
184
        }
185 1
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 1
     */
215
    public static function postInt($key)
216 1
    {
217 1
        $post = &self::$globalStateProvider->getPostSuperGlobal();
218 1
        if (!array_key_exists($key, $post)) {
219
            return null;
220
        }
221 1
222
        $filteredValue = filter_var($post[$key], FILTER_VALIDATE_INT, FILTER_NULL_ON_FAILURE);
223 1
224 1
        if ($filteredValue === null) {
225
            return null;
226
        }
227 1
228
        return (int)$filteredValue;
229
    }
230
231
    /**
232
     * @param string $key
233
     *
234
     * @return bool
235 1
     */
236
    public static function postBoolean($key)
237 1
    {
238 1
        $get = &self::$globalStateProvider->getPostSuperGlobal();
239 1
        if (!array_key_exists($key, $get)) {
240
            return false;
241
        }
242
243 1
        // presence of parameter only
244 1
        if ($get[$key] === "") {
245
            return true;
246
        }
247 1
248 1
        if (in_array($get[$key], array(false, 'no', 'off', 0, 'false'), true)) {
249
            return false;
250
        }
251 1
252
        return true;
253
    }
254
255
    #endregion
256
257
    #region GET variables
258
259
    /**
260
     * @param string $key
261
     *
262
     * @return bool
263 1
     */
264
    public static function getBoolean($key)
265 1
    {
266 1
        $get = &self::$globalStateProvider->getGetSuperGlobal();
267 1
        if (!array_key_exists($key, $get)) {
268
            return false;
269
        }
270
271 1
        // presence of parameter only
272 1
        if ($get[$key] === "") {
273
            return true;
274
        }
275 1
276 1
        if (in_array($get[$key], array(false, 'no', 'off', 0, 'false'), true)) {
277
            return false;
278
        }
279 1
280
        return true;
281
    }
282
283
    /**
284
     * @param string $key
285
     *
286
     * @return int|null
287 1
     */
288
    public static function getInt($key)
289 1
    {
290 1
        $get = &self::$globalStateProvider->getGetSuperGlobal();
291 1
        if (!array_key_exists($key, $get)) {
292
            return null;
293
        }
294 1
295
        $filteredValue = filter_var($get[$key], FILTER_VALIDATE_INT, FILTER_NULL_ON_FAILURE);
296 1
297 1
        if ($filteredValue === null) {
298
            return null;
299
        }
300 1
301
        return (int)$filteredValue;
302
    }
303
304
    /**
305
     * @param string $key
306
     *
307
     * @return null|string
308 7
     */
309
    public static function getString($key)
310 7
    {
311 7
        $get = &self::$globalStateProvider->getGetSuperGlobal();
312 1
        if (!array_key_exists($key, $get)) {
313
            return null;
314
        }
315 6
316 1
        if ($get[$key] === "") {
317
            return null;
318
        }
319 6
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 1
     */
330
    public static function setLoggedInUser(User $user)
331 1
    {
332
        $session = &self::$globalStateProvider->getSessionSuperGlobal();
333 1
334 1
        $session['userID'] = $user->getId();
335
        unset($session['partialLogin']);
336
    }
337
338
    public static function setActiveDomain(Domain $domain)
339
    {
340
        $session = &self::$globalStateProvider->getSessionSuperGlobal();
341
342
        $session['domainID'] = $domain->getId();
343
    }
344
345
    /**
346
     * Sets the post-login redirect
347
     *
348
     * @param string|null $uri The URI to redirect to
349 2
     */
350
    public static function setPostLoginRedirect($uri = null)
351 2
    {
352
        $session = &self::$globalStateProvider->getSessionSuperGlobal();
353 2
354 2
        if ($uri === null) {
355
            $uri = self::requestUri();
356
        }
357 2
358
        $session['returnTo'] = $uri;
359
    }
360
361
    /**
362
     * @return string|null
363 2
     */
364
    public static function requestUri()
365 2
    {
366
        $server = &self::$globalStateProvider->getServerSuperGlobal();
367 2
368 1
        if (isset($server['REQUEST_URI'])) {
369
            return $server['REQUEST_URI'];
370
        }
371 1
372
        return null;
373
    }
374
375
    /**
376
     * Clears the post-login redirect
377
     * @return string
378 2
     */
379
    public static function clearPostLoginRedirect()
380 2
    {
381 2
        $session = &self::$globalStateProvider->getSessionSuperGlobal();
382 1
        if (array_key_exists('returnTo', $session)) {
383 1
            $path = $session['returnTo'];
384
            unset($session['returnTo']);
385 1
386
            return $path;
387
        }
388 1
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()
600
    {
601
        $server = &self::$globalStateProvider->getServerSuperGlobal();
602
603
        if (isset($server['HTTP_ORIGIN'])) {
604
            return $server['HTTP_ORIGIN'];
605
        }
606
607
        return null;
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();
625
626
        if (isset($cookie['sitenotice'])) {
627
            return $cookie['sitenotice'] === $expectedHash;
628
        }
629
630
        return false;
631
    }
632
633
    public static function requestListDefaultSort()
634
    {
635
        $cookie = &self::$globalStateProvider->getCookieSuperGlobal();
636
637
        if (isset($cookie['request_table_sort'])) {
638
            return explode('/', $cookie['request_table_sort'], 2);
639
        }
640
        else {
641
            return ['id', 'asc'];
642
        }
643
    }
644
}
645