WebRequest::isHttps()   A
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 28
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

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