|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @package presentation |
|
4
|
|
|
* @subpackage requests |
|
5
|
|
|
* @author marius orcsik <[email protected]> |
|
6
|
|
|
* @date 28.07.15 |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace vsc\presentation\requests; |
|
10
|
|
|
|
|
11
|
|
|
trait ServerRequestTrait { |
|
12
|
|
|
/** |
|
13
|
|
|
* @var string |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $sHttpMethod; |
|
16
|
|
|
/** |
|
17
|
|
|
* @var string |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $sServerName; |
|
20
|
|
|
/** |
|
21
|
|
|
* @var string |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $sServerProtocol; |
|
24
|
|
|
/** |
|
25
|
|
|
* @var array |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $aAccept = []; |
|
28
|
|
|
/** |
|
29
|
|
|
* @var array |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $aAcceptCharset = []; |
|
32
|
|
|
/** |
|
33
|
|
|
* @var array |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $aAcceptEncoding = []; |
|
36
|
|
|
/** |
|
37
|
|
|
* @var array |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $aAcceptLanguage = []; |
|
40
|
|
|
/** |
|
41
|
|
|
* @var string |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $sReferer = ''; |
|
44
|
|
|
/** |
|
45
|
|
|
* @var string |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $sUserAgent = ''; |
|
48
|
|
|
/** |
|
49
|
|
|
* @var string |
|
50
|
|
|
*/ |
|
51
|
|
|
protected $sIfModifiedSince = ''; |
|
52
|
|
|
/** |
|
53
|
|
|
* @var string |
|
54
|
|
|
*/ |
|
55
|
|
|
protected $sIfNoneMatch = ''; |
|
56
|
|
|
/** |
|
57
|
|
|
* @var string |
|
58
|
|
|
*/ |
|
59
|
|
|
protected $sContentType = ''; |
|
60
|
|
|
/** |
|
61
|
|
|
* @var bool |
|
62
|
|
|
*/ |
|
63
|
|
|
protected $bDoNotTrack = false; |
|
64
|
|
|
/** |
|
65
|
|
|
* @param HttpAuthenticationA $oHttpAuthentication |
|
66
|
|
|
*/ |
|
67
|
|
|
abstract public function setAuthentication(HttpAuthenticationA $oHttpAuthentication); |
|
|
|
|
|
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @return string |
|
71
|
|
|
*/ |
|
72
|
1 |
|
public function getServerName() { |
|
73
|
1 |
|
return $this->sServerName; |
|
74
|
|
|
} |
|
75
|
|
|
/** |
|
76
|
|
|
* @return string |
|
77
|
|
|
*/ |
|
78
|
1 |
|
public function getServerProtocol() { |
|
79
|
1 |
|
return $this->sServerProtocol; |
|
80
|
|
|
} |
|
81
|
|
|
/** |
|
82
|
|
|
* @return string |
|
83
|
|
|
*/ |
|
84
|
1 |
|
public function getHttpMethod() { |
|
85
|
1 |
|
return $this->sHttpMethod; |
|
86
|
|
|
} |
|
87
|
|
|
/** |
|
88
|
|
|
* @return array |
|
89
|
|
|
*/ |
|
90
|
1 |
|
public function getHttpAccept() { |
|
91
|
1 |
|
return $this->aAccept; |
|
92
|
|
|
} |
|
93
|
|
|
/** |
|
94
|
|
|
* @return array |
|
95
|
|
|
*/ |
|
96
|
1 |
|
public function getHttpAcceptCharset() { |
|
97
|
1 |
|
return $this->aAcceptCharset; |
|
98
|
|
|
} |
|
99
|
|
|
/** |
|
100
|
|
|
* @return bool |
|
101
|
|
|
*/ |
|
102
|
2 |
|
public function hasContentType() { |
|
103
|
2 |
|
return !empty($this->sContentType); |
|
104
|
|
|
} |
|
105
|
|
|
/** |
|
106
|
|
|
* @return string |
|
107
|
|
|
*/ |
|
108
|
1 |
|
public function getContentType() { |
|
109
|
1 |
|
return $this->sContentType; |
|
110
|
|
|
} |
|
111
|
|
|
/** |
|
112
|
|
|
* @return array |
|
113
|
|
|
*/ |
|
114
|
1 |
|
public function getHttpAcceptEncoding() { |
|
115
|
1 |
|
return $this->aAcceptEncoding; |
|
116
|
|
|
} |
|
117
|
|
|
/** |
|
118
|
|
|
* @return array |
|
119
|
|
|
*/ |
|
120
|
1 |
|
public function getHttpAcceptLanguage() { |
|
121
|
1 |
|
return $this->aAcceptLanguage; |
|
122
|
|
|
} |
|
123
|
|
|
/** |
|
124
|
|
|
* @return string |
|
125
|
|
|
*/ |
|
126
|
1 |
|
public function getIfModifiedSince() { |
|
127
|
1 |
|
return $this->sIfModifiedSince; |
|
128
|
|
|
} |
|
129
|
|
|
/** |
|
130
|
|
|
* @return string |
|
131
|
|
|
*/ |
|
132
|
1 |
|
public function getIfNoneMatch() { |
|
133
|
1 |
|
return $this->sIfNoneMatch; |
|
134
|
|
|
} |
|
135
|
|
|
/** |
|
136
|
|
|
* @return string |
|
137
|
|
|
*/ |
|
138
|
1 |
|
public function getHttpReferer() { |
|
139
|
1 |
|
return $this->sReferer; |
|
140
|
|
|
} |
|
141
|
|
|
/** |
|
142
|
|
|
* @return string |
|
143
|
|
|
*/ |
|
144
|
1 |
|
public function getHttpUserAgent() { |
|
145
|
1 |
|
return $this->sUserAgent; |
|
146
|
|
|
} |
|
147
|
|
|
/** |
|
148
|
|
|
* @return bool |
|
149
|
|
|
*/ |
|
150
|
|
|
public function getDoNotTrack() { |
|
151
|
|
|
return $this->bDoNotTrack; |
|
152
|
|
|
} |
|
153
|
|
|
/** |
|
154
|
|
|
* @return bool |
|
155
|
|
|
*/ |
|
156
|
1 |
|
public static function isSecure() { |
|
157
|
1 |
|
return (array_key_exists('HTTPS', $_SERVER) && $_SERVER['HTTPS'] == 'on'); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* @param array $aServer |
|
162
|
|
|
*/ |
|
163
|
18 |
|
private function loadServerHeaders($aServer) { |
|
164
|
18 |
|
if (isset ($aServer['SERVER_PROTOCOL'])) { |
|
165
|
1 |
|
$this->sServerProtocol = $aServer['SERVER_PROTOCOL']; |
|
166
|
|
|
} |
|
167
|
18 |
|
if (isset ($aServer['SERVER_NAME'])) { |
|
168
|
1 |
|
$this->sServerName = $aServer['SERVER_NAME']; |
|
169
|
|
|
} |
|
170
|
18 |
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* @param array $aServer |
|
174
|
|
|
*/ |
|
175
|
18 |
|
private function loadAcceptHeaders($aServer) { |
|
176
|
18 |
|
if (isset ($aServer['HTTP_ACCEPT'])) { |
|
177
|
18 |
|
$this->aAccept = explode(',', $aServer['HTTP_ACCEPT']); |
|
178
|
|
|
} |
|
179
|
18 |
View Code Duplication |
if (isset ($aServer['HTTP_ACCEPT_LANGUAGE'])) { |
|
|
|
|
|
|
180
|
1 |
|
$this->aAcceptLanguage = explode(',', $aServer['HTTP_ACCEPT_LANGUAGE']); |
|
181
|
1 |
|
foreach ($this->aAcceptLanguage as $index => $value) { |
|
182
|
1 |
|
$this->aAcceptLanguage[$index] = trim($value); |
|
183
|
|
|
} |
|
184
|
|
|
} |
|
185
|
18 |
View Code Duplication |
if (isset ($aServer['HTTP_ACCEPT_ENCODING'])) { |
|
|
|
|
|
|
186
|
1 |
|
$this->aAcceptEncoding = explode(',', $aServer['HTTP_ACCEPT_ENCODING']); |
|
187
|
1 |
|
foreach ($this->aAcceptEncoding as $index => $value) { |
|
188
|
1 |
|
$this->aAcceptEncoding[$index] = trim($value); |
|
189
|
|
|
} |
|
190
|
|
|
} |
|
191
|
18 |
View Code Duplication |
if (isset ($aServer['HTTP_ACCEPT_CHARSET'])) { |
|
|
|
|
|
|
192
|
1 |
|
$this->aAcceptCharset = explode(',', $aServer['HTTP_ACCEPT_CHARSET']); |
|
193
|
1 |
|
foreach ($this->aAcceptCharset as $index => $value) { |
|
194
|
1 |
|
$this->aAcceptCharset[$index] = trim($value); |
|
195
|
|
|
} |
|
196
|
|
|
} |
|
197
|
18 |
|
} |
|
198
|
|
|
|
|
199
|
18 |
|
private function loadCachingHeaders($aServer) { |
|
200
|
18 |
|
if (isset($aServer['HTTP_IF_MODIFIED_SINCE'])) { |
|
201
|
1 |
|
$this->sIfModifiedSince = $aServer['HTTP_IF_MODIFIED_SINCE']; |
|
202
|
|
|
} |
|
203
|
18 |
|
if (isset($aServer['HTTP_IF_NONE_MATCH'])) { |
|
204
|
1 |
|
$this->sIfNoneMatch = $aServer['HTTP_IF_NONE_MATCH']; |
|
205
|
|
|
} |
|
206
|
18 |
|
} |
|
207
|
|
|
|
|
208
|
19 |
|
private function loadAuthenticationHeaders($aServer) { |
|
209
|
19 |
|
if (isset($aServer['PHP_AUTH_DIGEST'])) { |
|
210
|
|
|
// DIGEST authorization attempt |
|
211
|
1 |
|
$this->setAuthentication(new DigestHttpAuthentication($aServer['PHP_AUTH_DIGEST'], $aServer['REQUEST_METHOD'])); |
|
212
|
|
|
} |
|
213
|
19 |
|
if (isset($aServer['PHP_AUTH_USER']) && isset($aServer['PHP_AUTH_PW'])) { |
|
214
|
1 |
|
$this->setAuthentication(new BasicHttpAuthentication($aServer['PHP_AUTH_USER'], $aServer['PHP_AUTH_PW'])); |
|
215
|
|
|
} |
|
216
|
19 |
|
} |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* @param $aServer |
|
220
|
|
|
*/ |
|
221
|
19 |
|
public function initServer($aServer) { |
|
222
|
19 |
|
if (isset ($aServer['REQUEST_METHOD'])) { |
|
223
|
1 |
|
$this->sHttpMethod = $aServer['REQUEST_METHOD']; |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
19 |
|
$this->loadServerHeaders($aServer); |
|
227
|
19 |
|
$this->loadAcceptHeaders($aServer); |
|
228
|
19 |
|
$this->loadCachingHeaders($aServer); |
|
229
|
19 |
|
$this->loadAuthenticationHeaders($aServer); |
|
230
|
|
|
|
|
231
|
19 |
|
if (isset ($aServer['HTTP_USER_AGENT'])) { |
|
232
|
1 |
|
$this->sUserAgent = $aServer['HTTP_USER_AGENT']; |
|
233
|
|
|
} |
|
234
|
19 |
|
if (isset ($aServer['HTTP_REFERER'])) { |
|
235
|
1 |
|
$this->sReferer = $aServer['HTTP_REFERER']; |
|
236
|
|
|
} |
|
237
|
19 |
|
if (isset($aServer['CONTENT_TYPE'])) { |
|
238
|
1 |
|
if (stripos($aServer['CONTENT_TYPE'], ';') !== false) { |
|
239
|
|
|
$this->sContentType = substr($aServer['CONTENT_TYPE'], 0, stripos($aServer['CONTENT_TYPE'], ';')); |
|
240
|
|
|
} else { |
|
241
|
1 |
|
$this->sContentType = $aServer['CONTENT_TYPE']; |
|
242
|
|
|
} |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
19 |
|
if (isset($aServer['HTTP_DNT'])) { |
|
246
|
1 |
|
$this->bDoNotTrack = (bool)$aServer['HTTP_DNT']; |
|
247
|
|
|
} |
|
248
|
19 |
|
} |
|
249
|
|
|
} |
|
250
|
|
|
|
For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a
@returndoc comment to communicate to implementors of these methods what they are expected to return.