1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Pimf |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright (c) Gjero Krsteski (http://krsteski.de) |
6
|
|
|
* @license http://opensource.org/licenses/MIT MIT |
7
|
|
|
*/ |
8
|
|
|
namespace Pimf; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Server and execution environment information. |
12
|
|
|
* |
13
|
|
|
* @package Pimf |
14
|
|
|
* @author Gjero Krsteski <[email protected]> |
15
|
|
|
* |
16
|
|
|
* @property string X_REQUESTED_WITH It is sent by the Ajax functions of most major Frameworks |
17
|
|
|
* @property string HTTP Is the application running under HTTP protocol? |
18
|
|
|
* @property string HTTPS Is the application running under HTTPS protocol? |
19
|
|
|
* @property string SERVER_PROTOCOL Name and revision of the information protocol via which the page was |
20
|
|
|
* requested; i.e. 'HTTP/1.0'; |
21
|
|
|
* @property string CONTENT_LENGTH The Content-Length |
22
|
|
|
* @property string CONTENT_TYPE The Content-Type |
23
|
|
|
* @property string HOST The name of the server host under which the current script is executing. |
24
|
|
|
* @property string SERVER_NAME The name of the server host under which the current script is executing. |
25
|
|
|
* @property string SERVER_PORT Get the port |
26
|
|
|
* @property string PHP_SELF Filename of the currently executing script. |
27
|
|
|
* @property string SCRIPT_NAME Get Script Name (physical path) |
28
|
|
|
* @property string PATH_INFO Get Path Info (virtual path) |
29
|
|
|
* @property string X_FORWARDED_FOR Do on your machine is behind the proxy than us it instead of REMOTE_ADDR |
30
|
|
|
* @property string CLIENT_IP Get the client ip address |
31
|
|
|
* @property string REMOTE_ADDR The IP address from which the user is viewing the current page. |
32
|
|
|
* @property string HTTP_REFERER Get Referer - it cannot really be trusted. |
33
|
|
|
* @property string USER_AGENT Contents of the User-Agent from the current request, if there is one. |
34
|
|
|
* @property string HTTP_USER_AGENT Contents of the User-Agent: header from the current request, if there is |
35
|
|
|
* one. |
36
|
|
|
* @property string REQUEST_URI The URI which was given in order to access this page; for instance, |
37
|
|
|
* '/index.html'. |
38
|
|
|
* @property string REQUEST_METHOD Which request method was used to access the page; i.e. 'GET', 'HEAD', |
39
|
|
|
* 'POST', 'PUT'. |
40
|
|
|
* @property string REDIRECT_URL The URL to redirect the user agent to |
41
|
|
|
* @property string HTTP_IF_MODIFIED_SINCE Get request header from Apache even on PHP running as a CGI |
42
|
|
|
* @property string HTTP_IF_NONE_MATCH Get request header from Apache even on PHP running as a CGI |
43
|
|
|
*/ |
44
|
|
|
class Environment |
45
|
|
|
{ |
46
|
|
|
/** |
47
|
|
|
* @var Param |
48
|
|
|
*/ |
49
|
|
|
private $data; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param array $server |
53
|
|
|
*/ |
54
|
|
|
public function __construct(array $server) |
55
|
|
|
{ |
56
|
|
|
$this->data = new Param($server); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return Param |
61
|
|
|
*/ |
62
|
|
|
public function data() |
63
|
|
|
{ |
64
|
|
|
return $this->data; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param $key |
69
|
|
|
* |
70
|
|
|
* @return string |
71
|
|
|
*/ |
72
|
|
|
public function __get($key) |
73
|
|
|
{ |
74
|
|
|
return $this->data->get($key, null, false); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Is this an AJAX request? |
79
|
|
|
* |
80
|
|
|
* @return bool |
81
|
|
|
*/ |
82
|
|
|
public function isAjax() |
83
|
|
|
{ |
84
|
|
|
return $this->X_REQUESTED_WITH === 'XMLHttpRequest'; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Is the application running under HTTP protocol? |
89
|
|
|
* |
90
|
|
|
* @return bool |
91
|
|
|
*/ |
92
|
|
|
public function isHttp() |
93
|
|
|
{ |
94
|
|
|
return (bool)$this->HTTP; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Is the application running under HTTPS protocol? |
99
|
|
|
* |
100
|
|
|
* @return bool |
101
|
|
|
*/ |
102
|
|
|
public function isHttps() |
103
|
|
|
{ |
104
|
|
|
return $this->HTTPS === 'on'; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Get Host |
109
|
|
|
* |
110
|
|
|
* @return string |
111
|
|
|
*/ |
112
|
|
|
public function getHost() |
113
|
|
|
{ |
114
|
|
|
if ($this->HOST) { |
115
|
|
|
|
116
|
|
|
if (strpos($this->HOST, ':') !== false) { |
117
|
|
|
$hostParts = explode(':', $this->HOST); |
118
|
|
|
|
119
|
|
|
return $hostParts[0]; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return $this->HOST; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return $this->SERVER_NAME; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Get Host with Port |
130
|
|
|
* |
131
|
|
|
* @return string |
132
|
|
|
*/ |
133
|
|
|
public function getHostWithPort() |
134
|
|
|
{ |
135
|
|
|
return '' . $this->getHost() . ':' . $this->SERVER_PORT; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Physical path + virtual path |
140
|
|
|
* |
141
|
|
|
* @return string |
142
|
|
|
*/ |
143
|
|
|
public function getPath() |
144
|
|
|
{ |
145
|
|
|
return $this->SCRIPT_NAME . $this->PATH_INFO; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Get remote IP |
150
|
|
|
* |
151
|
|
|
* @return string |
152
|
|
|
*/ |
153
|
|
|
public function getIp() |
154
|
|
|
{ |
155
|
|
|
if ($this->X_FORWARDED_FOR) { |
156
|
|
|
return $this->X_FORWARDED_FOR; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
if ($this->CLIENT_IP) { |
160
|
|
|
return $this->CLIENT_IP; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
if ($this->SERVER_NAME) { |
164
|
|
|
return gethostbyname($this->SERVER_NAME); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
return $this->REMOTE_ADDR; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Get User Agent |
172
|
|
|
* |
173
|
|
|
* @return string|null |
174
|
|
|
*/ |
175
|
|
|
public function getUserAgent() |
176
|
|
|
{ |
177
|
|
|
if ($this->USER_AGENT) { |
178
|
|
|
return $this->USER_AGENT; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
if ($this->HTTP_USER_AGENT) { |
182
|
|
|
return $this->HTTP_USER_AGENT; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
return null; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Gives you the current page URL |
190
|
|
|
* |
191
|
|
|
* @return string |
192
|
|
|
*/ |
193
|
|
|
public function getUrl() |
194
|
|
|
{ |
195
|
|
|
$protocol = strpos(strtolower($this->PATH_INFO), 'https') === false ? 'http' : 'https'; |
196
|
|
|
|
197
|
|
|
return $protocol . '://' . $this->getHost(); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Try to get a request header. |
202
|
|
|
* |
203
|
|
|
* @param string $header |
204
|
|
|
* |
205
|
|
|
* @return array |
206
|
|
|
*/ |
207
|
|
|
public function getRequestHeader($header) |
208
|
|
|
{ |
209
|
|
|
$header = str_replace('-', '_', strtoupper($header)); |
210
|
|
|
$value = $this->{'HTTP_' . $header}; |
211
|
|
|
|
212
|
|
|
if (!$value) { |
213
|
|
|
$headers = $this->getRequestHeaders(); |
214
|
|
|
$value = !empty($headers[$header]) ? $headers[$header] : null; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
return $value; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Try to determine all request headers |
222
|
|
|
* |
223
|
|
|
* @return array |
224
|
|
|
*/ |
225
|
|
|
public function getRequestHeaders() |
226
|
|
|
{ |
227
|
|
|
$headers = array(); |
228
|
|
|
|
229
|
|
|
foreach ($this->data->getAll() as $key => $value) { |
230
|
|
|
if ('HTTP_' === substr($key, 0, 5)) { |
231
|
|
|
$headers[substr($key, 5)] = $value; |
232
|
|
|
} |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
return $headers; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
} |
239
|
|
|
|