1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace System\Http; |
4
|
|
|
|
5
|
|
|
use System\Application; |
6
|
|
|
|
7
|
|
|
class Request |
8
|
|
|
{ |
9
|
|
|
private $app; |
10
|
|
|
|
11
|
|
|
private $url; |
12
|
|
|
|
13
|
|
|
private $baseUrl; |
14
|
|
|
|
15
|
|
|
private $files = []; |
16
|
|
|
|
17
|
|
|
private $link; |
18
|
|
|
|
19
|
|
|
public function __construct(Application $app) |
20
|
|
|
{ |
21
|
|
|
$this->app = $app; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function prepareUrl() |
25
|
|
|
{ |
26
|
|
|
$script = dirname($this->server('SCRIPT_NAME')); |
27
|
|
|
|
28
|
|
|
$requestUri = $this->server('REQUEST_URI'); |
29
|
|
|
|
30
|
|
|
if (strpos($requestUri, '?')) { |
31
|
|
|
|
32
|
|
|
list($requestUri, $queryString) = explode('?', $requestUri); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
$this->url = $this->cleanUrl($script, $requestUri); |
36
|
|
|
|
37
|
|
|
$REQUEST_PROTOCOL = $this->isSecure() ? 'https' : 'http'; |
38
|
|
|
|
39
|
|
|
$this->link = $REQUEST_PROTOCOL . '://' . $this->server('HTTP_HOST'); |
40
|
|
|
|
41
|
|
|
$this->baseUrl = $this->link . $requestUri; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
private function cleanUrl($script, $requestUri) |
45
|
|
|
{ |
46
|
|
|
if (!in_array($script, ['/', '\\'])) { |
47
|
|
|
|
48
|
|
|
$url = preg_replace('#^' . $script . '#', '', $requestUri); |
49
|
|
|
|
50
|
|
|
} else { |
51
|
|
|
|
52
|
|
|
$url = $requestUri; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
if ($url !== '/') { |
56
|
|
|
|
57
|
|
|
$url = rtrim($url, '/'); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $url; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
private function isSecure() |
64
|
|
|
{ |
65
|
|
|
if ($this->checkHttp() || $this->checkHttpXforwardedProto() || $this->checkHttpXforwardedSsl()) { |
66
|
|
|
|
67
|
|
|
return true; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return false; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
private function checkHttp() |
74
|
|
|
{ |
75
|
|
|
if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') { |
76
|
|
|
|
77
|
|
|
return true; |
78
|
|
|
|
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return false; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
private function checkHttpXforwardedProto() |
85
|
|
|
{ |
86
|
|
|
if (!empty($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') { |
87
|
|
|
|
88
|
|
|
return true; |
89
|
|
|
|
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return false; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
private function checkHttpXforwardedSsl() |
96
|
|
|
{ |
97
|
|
|
if (!empty($_SERVER['HTTP_X_FORWARDED_SSL']) && $_SERVER['HTTP_X_FORWARDED_SSL'] == 'on') { |
98
|
|
|
|
99
|
|
|
return true; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return false; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function get($key) |
106
|
|
|
{ |
107
|
|
|
$value = array_get($_GET, $key); |
108
|
|
|
|
109
|
|
|
if (is_array($value)) { |
110
|
|
|
|
111
|
|
|
$value = array_filter($value); |
112
|
|
|
|
113
|
|
|
} else { |
114
|
|
|
|
115
|
|
|
$value = trim($value); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return $value; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public function post($key) |
122
|
|
|
{ |
123
|
|
|
$value = array_get($_POST, $key); |
124
|
|
|
|
125
|
|
|
if (is_array($value)) { |
126
|
|
|
|
127
|
|
|
$value = array_filter($value); |
128
|
|
|
|
129
|
|
|
} else { |
130
|
|
|
|
131
|
|
|
$value = trim($value); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return $value; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function setPost($key, $value) |
138
|
|
|
{ |
139
|
|
|
$_POST[$key] = $value; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function posts() |
143
|
|
|
{ |
144
|
|
|
return $_POST; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
public function files() |
148
|
|
|
{ |
149
|
|
|
return $_FILES; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
public function file($input) |
153
|
|
|
{ |
154
|
|
|
if (isset($this->files[$input])) { |
155
|
|
|
|
156
|
|
|
return $this->files[$input]; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
$upoadedFile = new UploadeFile($this->app, $input); |
160
|
|
|
|
161
|
|
|
$this->files[$input] = $upoadedFile; |
162
|
|
|
|
163
|
|
|
return $this->files[$input]; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
public function server($key) |
167
|
|
|
{ |
168
|
|
|
return array_get($_SERVER, $key); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
public function method() |
172
|
|
|
{ |
173
|
|
|
return $this->server('REQUEST_METHOD'); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
public function referer() |
177
|
|
|
{ |
178
|
|
|
return $this->server('HTTP_REFERER'); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
public function baseUrl() |
182
|
|
|
{ |
183
|
|
|
return $this->baseUrl; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
public function url() |
187
|
|
|
{ |
188
|
|
|
return $this->url; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
public function link() |
192
|
|
|
{ |
193
|
|
|
return $this->link; |
194
|
|
|
} |
195
|
|
|
} |