1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace cse\helpers; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class Request |
9
|
|
|
* |
10
|
|
|
* @package cse\helpers |
11
|
|
|
*/ |
12
|
|
|
class Request |
13
|
|
|
{ |
14
|
|
|
const METHOD_POST = 'POST'; |
15
|
|
|
const METHOD_GET = 'GET'; |
16
|
|
|
const METHOD_PUT = 'PUT'; |
17
|
|
|
const METHOD_DELETE = 'DELETE'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Get POST data by key name |
21
|
|
|
* |
22
|
|
|
* @param string $key |
23
|
|
|
* @param $default |
24
|
|
|
* |
25
|
|
|
* @return null|mixed |
26
|
|
|
*/ |
27
|
|
|
public static function post(string $key, $default = null) |
28
|
|
|
{ |
29
|
|
|
return $_POST[$key] ?? $default; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Get GET data by key name |
34
|
|
|
* |
35
|
|
|
* @param string $key |
36
|
|
|
* @param $default |
37
|
|
|
* |
38
|
|
|
* @return null|mixed |
39
|
|
|
*/ |
40
|
|
|
public static function get(string $key, $default = null) |
41
|
|
|
{ |
42
|
|
|
return $_GET[$key] ?? $default; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Get Request date by key name |
47
|
|
|
* |
48
|
|
|
* @param string $key |
49
|
|
|
* @param $default |
50
|
|
|
* |
51
|
|
|
* @return mixed|null |
52
|
|
|
*/ |
53
|
|
|
public static function request(string $key, $default = null) |
54
|
|
|
{ |
55
|
|
|
return $_REQUEST[$key] ?? $default; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Check Ajax request |
60
|
|
|
* |
61
|
|
|
* @return bool |
62
|
|
|
*/ |
63
|
|
|
public static function isAjax(): bool |
64
|
|
|
{ |
65
|
|
|
return strtolower($_SERVER['HTTP_X_REQUESTED_WITH'] ?? '') === 'xmlhttprequest'; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Check POST request |
70
|
|
|
* |
71
|
|
|
* @return bool |
72
|
|
|
*/ |
73
|
|
|
public static function isPost(): bool |
74
|
|
|
{ |
75
|
|
|
return strtoupper($_SERVER['REQUEST_METHOD'] ?? '') === self::METHOD_POST; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Check GET request |
80
|
|
|
* |
81
|
|
|
* @return bool |
82
|
|
|
*/ |
83
|
|
|
public static function isGet(): bool |
84
|
|
|
{ |
85
|
|
|
return strtoupper($_SERVER['REQUEST_METHOD'] ?? '') === self::METHOD_GET; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Get request uri |
90
|
|
|
* |
91
|
|
|
* @param $default |
92
|
|
|
* |
93
|
|
|
* @return null|string |
94
|
|
|
*/ |
95
|
|
|
public static function getRequestUri($default = null): ?string |
96
|
|
|
{ |
97
|
|
|
return self::isAjax() |
98
|
|
|
? $_SERVER['HTTP_REFERER'] ?? $default |
99
|
|
|
: $_SERVER['REQUEST_URI'] ?? $default; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Check redirect to https |
104
|
|
|
* |
105
|
|
|
* @param string $url |
106
|
|
|
* |
107
|
|
|
* @return bool |
108
|
|
|
*/ |
109
|
|
|
public static function isRedirectedToHttps(string $url): bool |
110
|
|
|
{ |
111
|
|
|
$ch = curl_init($url); |
112
|
|
|
curl_setopt($ch, CURLOPT_HEADER, false); |
|
|
|
|
113
|
|
|
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); |
114
|
|
|
curl_setopt($ch, CURLOPT_NOBODY, true); |
115
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false); |
116
|
|
|
curl_exec($ch); |
|
|
|
|
117
|
|
|
$end_url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL); |
|
|
|
|
118
|
|
|
|
119
|
|
|
return preg_match('/^https:\/\/.*/i', $end_url) === 1; |
120
|
|
|
} |
121
|
|
|
} |