|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ByJG\RestServer; |
|
4
|
|
|
|
|
5
|
|
|
class HttpRequest |
|
6
|
|
|
{ |
|
7
|
|
|
|
|
8
|
|
|
protected $get; |
|
9
|
|
|
protected $post; |
|
10
|
|
|
protected $server; |
|
11
|
|
|
protected $session; |
|
12
|
|
|
protected $cookie; |
|
13
|
|
|
protected $param; |
|
14
|
|
|
protected $phpRequest; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* |
|
18
|
|
|
* @param array $get |
|
19
|
|
|
* @param array $post |
|
20
|
|
|
* @param array $server |
|
21
|
|
|
* @param array $session |
|
22
|
|
|
* @param array $cookie |
|
23
|
|
|
* @param array $param |
|
24
|
|
|
*/ |
|
25
|
10 |
|
public function __construct($get, $post, $server, $session, $cookie, $param = []) |
|
26
|
|
|
{ |
|
27
|
10 |
|
$this->get = $get; |
|
28
|
10 |
|
$this->post = $post; |
|
29
|
10 |
|
$this->server = $server; |
|
30
|
10 |
|
$this->session = $session; |
|
31
|
10 |
|
$this->cookie = $cookie; |
|
32
|
10 |
|
$this->param = $param; |
|
33
|
|
|
|
|
34
|
10 |
|
$this->phpRequest = array_merge($get, $post, $server, $session, $cookie); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Get a parameter passed by GET (the same as $_GET). If not found return false. |
|
39
|
|
|
* |
|
40
|
|
|
* @param string $value |
|
41
|
|
|
* @return string|boolean |
|
42
|
|
|
*/ |
|
43
|
|
|
public function get($value) |
|
44
|
|
|
{ |
|
45
|
|
|
if (!isset($this->get[$value])) { |
|
46
|
|
|
return false; |
|
47
|
|
|
} else { |
|
48
|
|
|
return $this->get[$value]; |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Get a parameter passed by POST (the same as $_POST). If not found return false. |
|
54
|
|
|
* |
|
55
|
|
|
* @param string $value |
|
56
|
|
|
* @return string|boolean |
|
57
|
|
|
*/ |
|
58
|
|
|
public function post($value) |
|
59
|
|
|
{ |
|
60
|
|
|
if (!isset($this->post[$value])) { |
|
61
|
|
|
return false; |
|
62
|
|
|
} else { |
|
63
|
|
|
return $this->post[$value]; |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Get the parameters sent by server (the same as $_SERVER). If not found return false. |
|
69
|
|
|
* |
|
70
|
|
|
* @param string $value |
|
71
|
|
|
* @return string|boolean |
|
72
|
|
|
*/ |
|
73
|
7 |
|
public function server($value) |
|
74
|
|
|
{ |
|
75
|
7 |
|
if (!isset($this->server[$value])) { |
|
76
|
|
|
return false; |
|
77
|
|
|
} else { |
|
78
|
7 |
|
return $this->server[$value]; |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Get a server session value(the same as $_SESSION). If not found return false. |
|
84
|
|
|
* |
|
85
|
|
|
* @param string $value |
|
86
|
|
|
* @return string|boolean |
|
87
|
|
|
*/ |
|
88
|
|
|
public function session($value) |
|
89
|
|
|
{ |
|
90
|
|
|
if (!isset($this->session[$value])) { |
|
91
|
|
|
return false; |
|
92
|
|
|
} else { |
|
93
|
|
|
return $this->session[$value]; |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Get the cookie sent by the client (the same as $_COOKIE). If not found return false. |
|
99
|
|
|
* |
|
100
|
|
|
* @param string $value |
|
101
|
|
|
* @return string|boolean |
|
102
|
|
|
*/ |
|
103
|
|
|
public function cookie($value) |
|
104
|
|
|
{ |
|
105
|
|
|
if (!isset($this->cookie[$value])) { |
|
106
|
|
|
return false; |
|
107
|
|
|
} else { |
|
108
|
|
|
return $this->cookie[$value]; |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Get a value from any of get, post, server, cookie or session. If not found return false. |
|
114
|
|
|
* |
|
115
|
|
|
* @param string $value |
|
116
|
|
|
* @return string|boolean |
|
117
|
|
|
*/ |
|
118
|
|
|
public function request($value) |
|
119
|
|
|
{ |
|
120
|
|
|
if (!isset($this->phpRequest[$value])) { |
|
121
|
|
|
return false; |
|
122
|
|
|
} else { |
|
123
|
|
|
return $this->phpRequest[$value]; |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Get a value from the params found in the URL |
|
129
|
|
|
* |
|
130
|
|
|
* @param string $value |
|
131
|
|
|
* @return string|boolean |
|
132
|
|
|
*/ |
|
133
|
1 |
|
public function param($value) |
|
134
|
|
|
{ |
|
135
|
1 |
|
if (!isset($this->param[$value])) { |
|
136
|
|
|
return false; |
|
137
|
|
|
} else { |
|
138
|
1 |
|
return $this->param[$value]; |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
private $payload; |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Get the payload passed during the request(the same as php://input). If not found return empty. |
|
146
|
|
|
* |
|
147
|
|
|
* @return string |
|
148
|
|
|
*/ |
|
149
|
|
|
public function payload() |
|
150
|
|
|
{ |
|
151
|
|
|
if (is_null($this->payload)) { |
|
152
|
|
|
$this->payload = file_get_contents("php://input"); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
return $this->payload; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Use this method to get the CLIENT REQUEST IP. |
|
160
|
|
|
* Note that if you behing a Proxy, the variable REMOTE_ADDR will always have the same IP |
|
161
|
|
|
* @return string |
|
162
|
|
|
*/ |
|
163
|
|
|
public function getRequestIp() |
|
164
|
|
|
{ |
|
165
|
|
|
$headers = [ |
|
166
|
|
|
'HTTP_CLIENT_IP', |
|
167
|
|
|
'HTTP_X_FORWARDED_FOR', |
|
168
|
|
|
'HTTP_X_FORWARDED', |
|
169
|
|
|
'HTTP_FORWARDED_FOR', |
|
170
|
|
|
'HTTP_FORWARDED', |
|
171
|
|
|
'REMOTE_ADDR', |
|
172
|
|
|
]; |
|
173
|
|
|
foreach ($headers as $header) { |
|
174
|
|
|
if ($this->server($header) !== false) { |
|
175
|
|
|
return $this->server($header); |
|
|
|
|
|
|
176
|
|
|
} |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
return 'UNKNOWN'; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
public function getServerName() |
|
183
|
|
|
{ |
|
184
|
|
|
$headers = [ |
|
185
|
|
|
'SERVER_NAME', |
|
186
|
|
|
'HTTP_HOST', |
|
187
|
|
|
]; |
|
188
|
|
|
foreach ($headers as $header) { |
|
189
|
|
|
if ($this->server($header) !== false) { |
|
190
|
|
|
return $this->server('SERVER_NAME'); |
|
191
|
|
|
} |
|
192
|
|
|
} |
|
193
|
|
|
return $this->server('SERVER_ADDR'); |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* Use this method to get the SERVER NAME. |
|
198
|
|
|
* @param bool $port |
|
199
|
|
|
* @param bool $protocol |
|
200
|
|
|
* @return string |
|
201
|
|
|
*/ |
|
202
|
|
|
public function getRequestServer($port = false, $protocol = false) |
|
203
|
|
|
{ |
|
204
|
|
|
$servername = $this->getServerName(); |
|
205
|
|
|
|
|
206
|
|
|
if ($port && $this->server('SERVER_PORT' !== false)) { |
|
207
|
|
|
$servername .= ':' . $this->server('SERVER_PORT'); |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
if ($protocol) { |
|
211
|
|
|
$servername = ( |
|
212
|
|
|
($this->server('HTTPS') !== 'off' |
|
213
|
|
|
|| $this->server('SERVER_PORT') == 443) ? "https://" : "http://") . $servername |
|
214
|
|
|
; |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
return $servername; |
|
|
|
|
|
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
private $uploadedFiles; |
|
221
|
|
|
|
|
222
|
|
|
/** |
|
223
|
|
|
* @return UploadedFiles |
|
224
|
|
|
*/ |
|
225
|
|
|
public function uploadedFiles() |
|
226
|
|
|
{ |
|
227
|
|
|
if (!isset($this->uploadedFiles)) { |
|
228
|
|
|
$this->uploadedFiles = new UploadedFiles(); |
|
229
|
|
|
} |
|
230
|
|
|
return $this->uploadedFiles; |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
3 |
|
public function appendVars($array) |
|
234
|
|
|
{ |
|
235
|
3 |
|
$this->param = array_merge($this->param, $array); |
|
236
|
|
|
} |
|
237
|
|
|
} |
|
238
|
|
|
|