1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package presentation |
4
|
|
|
* @subpackage requests |
5
|
|
|
* @author marius orcsik <[email protected]> |
6
|
|
|
* @date 09.07.13 |
7
|
|
|
*/ |
8
|
|
|
namespace vsc\presentation\requests; |
9
|
|
|
|
10
|
|
|
use vsc\infrastructure\BaseObject; |
11
|
|
|
use vsc\infrastructure\urls\Url; |
12
|
|
|
use vsc\infrastructure\urls\UrlParserA; |
13
|
|
|
use vsc\Exception; |
14
|
|
|
|
15
|
|
|
abstract class HttpRequestA extends BaseObject { |
16
|
|
|
use GetRequestTrait; |
17
|
|
|
use PostRequestTrait; |
18
|
|
|
use CookieRequestTrait; |
19
|
|
|
use FilesRequestTrait; |
20
|
|
|
use SessionRequestTrait; |
21
|
|
|
use AuthenticatedRequestTrait; |
22
|
|
|
use ServerRequestTrait; |
23
|
|
|
|
24
|
|
|
protected $sUri = null; |
25
|
|
|
/** |
26
|
|
|
* @var Url |
27
|
|
|
*/ |
28
|
|
|
protected $oUri; |
29
|
|
|
static private $aVarOrder = []; |
30
|
|
|
|
31
|
|
|
protected $sAuthorization = ''; |
32
|
|
|
protected $iContentLength = 0; // ? I don't think I'm interested in the length of the request |
33
|
|
|
|
34
|
18 |
|
public function __construct() { |
35
|
18 |
|
if (isset($_COOKIE)) { |
36
|
18 |
|
$this->initCookie($_COOKIE); |
37
|
|
|
} |
38
|
18 |
|
if (isset($_FILES)) { |
39
|
18 |
|
$this->initFiles($_FILES); |
40
|
|
|
} |
41
|
18 |
|
if (isset($_SERVER)) { |
42
|
18 |
|
$this->initServer($_SERVER); |
43
|
|
|
} |
44
|
18 |
|
$this->initGet($_GET); |
45
|
18 |
|
$this->initPost($_POST); |
46
|
18 |
|
$this->initSession(); |
47
|
18 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @return array |
51
|
|
|
*/ |
52
|
1 |
|
public static function getVarOrder() { |
53
|
1 |
|
if (count(self::$aVarOrder) != 4) { |
54
|
|
|
// get gpc order |
55
|
|
|
$sOrder = ini_get('variables_order'); |
56
|
|
|
for ($i = 0; $i < 4; $i++) { |
57
|
|
|
// reversing the order |
58
|
|
|
self::$aVarOrder[$i] = substr($sOrder, $i, 1); |
59
|
|
|
} |
60
|
|
|
} |
61
|
1 |
|
return self::$aVarOrder; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @return array |
66
|
|
|
*/ |
67
|
1 |
|
public function getVars() { |
68
|
1 |
|
$aRet = array(); |
69
|
1 |
|
foreach (self::getVarOrder() as $sMethod) { |
70
|
1 |
|
switch ($sMethod) { |
71
|
1 |
|
case 'S': |
72
|
1 |
|
if (self::hasSession()) { |
73
|
|
|
$aRet = array_merge($aRet, $this->aSessionVars); |
74
|
|
|
} |
75
|
1 |
|
break; |
76
|
1 |
|
case 'C': |
77
|
1 |
|
$aRet = array_merge($aRet, $this->aCookieVars); |
78
|
1 |
|
break; |
79
|
1 |
|
case 'P': |
80
|
1 |
|
$aRet = array_merge($aRet, $this->aPostVars); |
81
|
1 |
|
break; |
82
|
1 |
|
case 'G': |
83
|
1 |
|
$aRet = array_merge($aRet, $this->aGetVars); |
84
|
1 |
|
break; |
85
|
|
|
} |
86
|
|
|
} |
87
|
1 |
|
return $aRet; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param string $sVarName |
92
|
|
|
* @throws Exception |
93
|
|
|
* @return mixed |
94
|
|
|
*/ |
95
|
1 |
|
public function getVar($sVarName) { |
96
|
1 |
|
foreach (self::getVarOrder() as $sMethod) { |
97
|
1 |
|
switch ($sMethod) { |
98
|
1 |
|
case 'G': |
99
|
1 |
|
$mVal = $this->getGetVar($sVarName); |
100
|
1 |
|
break; |
101
|
1 |
|
case 'P': |
102
|
1 |
|
$mVal = $this->getPostVar($sVarName); |
103
|
1 |
|
break; |
104
|
1 |
|
case 'C': |
105
|
1 |
|
$mVal = $this->getCookieVar($sVarName); |
106
|
1 |
|
break; |
107
|
1 |
|
case 'S': |
108
|
1 |
|
if (self::hasSession()) { |
109
|
|
|
$mVal = $this->getSessionVar($sVarName); |
110
|
|
|
} |
111
|
1 |
|
break; |
112
|
|
|
} |
113
|
1 |
|
if (isset($mVal)) { |
114
|
1 |
|
return $mVal; |
115
|
|
|
} |
116
|
|
|
} |
117
|
1 |
|
return null; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param string $sContentType |
122
|
|
|
* @return bool |
123
|
|
|
*/ |
124
|
|
|
public static function validContentType($sContentType) { |
125
|
|
|
return (preg_match('/^([-a-z]+|\*{1})\/([-a-z\+\.]+|\*{1})(;.*)?$/i', $sContentType) > 0); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param string $sVarName |
130
|
|
|
* @return bool |
131
|
|
|
*/ |
132
|
1 |
|
public function hasVar($sVarName) { |
133
|
|
|
return ( |
134
|
1 |
|
$this->hasGetVar($sVarName) || |
135
|
1 |
|
$this->hasPostVar($sVarName) || |
136
|
1 |
|
$this->hasSessionVar($sVarName) || |
137
|
1 |
|
$this->hasCookieVar($sVarName) |
138
|
|
|
); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @return bool |
143
|
|
|
*/ |
144
|
1 |
|
public function isGet() { |
145
|
1 |
|
return ($this->getHttpMethod() == HttpRequestTypes::GET); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @return bool |
150
|
|
|
*/ |
151
|
1 |
|
public function isHead() { |
152
|
1 |
|
return ($this->getHttpMethod() == HttpRequestTypes::HEAD); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @return bool |
157
|
|
|
*/ |
158
|
1 |
|
public function isPost() { |
159
|
1 |
|
return ($this->getHttpMethod() == HttpRequestTypes::POST); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @return bool |
164
|
|
|
*/ |
165
|
1 |
|
public function isPut() { |
166
|
1 |
|
return ($this->getHttpMethod() == HttpRequestTypes::PUT); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @return bool |
171
|
|
|
*/ |
172
|
1 |
|
public function isDelete() { |
173
|
1 |
|
return ($this->getHttpMethod() == HttpRequestTypes::DELETE); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @param string $sMimeType |
178
|
|
|
* @return bool |
179
|
|
|
*/ |
180
|
6 |
|
public function accepts($sMimeType) { |
181
|
6 |
|
return ContentType::isAccepted($sMimeType, $this->getHttpAccept()); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Returns the REQUEST_URI which is used to get the URL Rewrite variables |
186
|
|
|
* This will also remove the part of the path that is actually an existing path |
187
|
|
|
* lighttpd: |
188
|
|
|
* url.rewrite = ( |
189
|
|
|
* "^/([^?]*)?(.*)$" => "/index.php$2" <- this doesn't look right to me |
190
|
|
|
* ) |
191
|
|
|
* |
192
|
|
|
* @todo move to the UrlRWParser |
193
|
|
|
* @param bool $bUrlDecode |
194
|
|
|
* @return string |
195
|
|
|
*/ |
196
|
18 |
|
public function getUri($bUrlDecode = false) { |
197
|
18 |
|
if (!$this->sUri && isset($_SERVER['REQUEST_URI'])) { |
198
|
|
|
// this header is present for all servers in the same form |
199
|
1 |
|
if (isset($_SERVER['PHP_SELF'])) { |
200
|
1 |
|
$sCurrentScriptDir = dirname($_SERVER['PHP_SELF']) != '/' ? dirname($_SERVER['PHP_SELF']) : ''; |
201
|
|
|
} else { |
202
|
|
|
$sCurrentScriptDir = ''; |
203
|
|
|
} |
204
|
1 |
|
$sReqUri = $_SERVER['REQUEST_URI']; |
205
|
1 |
|
$this->sUri = str_replace($sCurrentScriptDir, '', $sReqUri); |
206
|
|
|
|
207
|
|
|
// removing unnecessary get vars |
208
|
1 |
|
$iQMarkPos = strpos($this->sUri, '?'); |
209
|
1 |
|
if ($iQMarkPos) { |
210
|
|
|
$this->sUri = substr($this->sUri, 0, $iQMarkPos); |
211
|
|
|
} |
212
|
|
|
} |
213
|
18 |
|
if ($bUrlDecode) { |
214
|
17 |
|
$this->sUri = urldecode($this->sUri); |
215
|
|
|
} |
216
|
|
|
|
217
|
18 |
|
return $this->sUri; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* @return Url |
222
|
|
|
*/ |
223
|
18 |
|
public function getUriObject() { |
224
|
18 |
|
if (!Url::isValid($this->oUri)) { |
225
|
18 |
|
$this->oUri = UrlParserA::url($this->getUri()); |
226
|
|
|
} |
227
|
18 |
|
return $this->oUri; |
228
|
|
|
} |
229
|
|
|
|
230
|
24 |
|
static public function getDecodedVar($mVar) { |
|
|
|
|
231
|
24 |
|
if (is_array($mVar)) { |
232
|
1 |
|
foreach ($mVar as $key => $sValue) { |
233
|
1 |
|
$mVar[$key] = self::getDecodedVar($sValue); |
234
|
|
|
} |
235
|
24 |
|
} elseif (is_string($mVar)) { |
236
|
24 |
|
$mVar = urldecode($mVar); |
237
|
|
|
} |
238
|
24 |
|
return $mVar; |
239
|
|
|
} |
240
|
|
|
} |
241
|
|
|
|
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@return
annotation as described here.