1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (C) 2016 SURFnet. |
4
|
|
|
* |
5
|
|
|
* This program is free software: you can redistribute it and/or modify |
6
|
|
|
* it under the terms of the GNU Affero General Public License as |
7
|
|
|
* published by the Free Software Foundation, either version 3 of the |
8
|
|
|
* License, or (at your option) any later version. |
9
|
|
|
* |
10
|
|
|
* This program is distributed in the hope that it will be useful, |
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13
|
|
|
* GNU Affero General Public License for more details. |
14
|
|
|
* |
15
|
|
|
* You should have received a copy of the GNU Affero General Public License |
16
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace fkooman\RemoteStorage\Http; |
20
|
|
|
|
21
|
|
|
use fkooman\RemoteStorage\Http\Exception\HttpException; |
22
|
|
|
|
23
|
|
|
class Request |
24
|
|
|
{ |
25
|
|
|
/** @var array */ |
26
|
|
|
private $serverData; |
27
|
|
|
|
28
|
|
|
/** @var array */ |
29
|
|
|
private $getData; |
30
|
|
|
|
31
|
|
|
/** @var array */ |
32
|
|
|
private $postData; |
33
|
|
|
|
34
|
|
|
/** @var string|null */ |
35
|
|
|
private $rawData; |
36
|
|
|
|
37
|
|
|
public function __construct(array $serverData, array $getData = [], array $postData = [], $rawData = null) |
38
|
|
|
{ |
39
|
|
|
$requiredHeaders = [ |
40
|
|
|
'REQUEST_METHOD', |
41
|
|
|
'SERVER_NAME', |
42
|
|
|
'SERVER_PORT', |
43
|
|
|
'REQUEST_URI', |
44
|
|
|
'SCRIPT_NAME', |
45
|
|
|
]; |
46
|
|
|
|
47
|
|
|
foreach ($requiredHeaders as $key) { |
48
|
|
|
if (!array_key_exists($key, $serverData)) { |
49
|
|
|
// this indicates something wrong with the interaction between |
50
|
|
|
// the web server and PHP, these headers MUST always be available |
51
|
|
|
throw new HttpException(sprintf('missing header "%s"', $key), 500); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
$this->serverData = $serverData; |
55
|
|
|
$this->getData = $getData; |
56
|
|
|
$this->postData = $postData; |
57
|
|
|
$this->rawData = $rawData; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function __toString() |
61
|
|
|
{ |
62
|
|
|
return var_export($this->serverData, true); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function getAuthority() |
66
|
|
|
{ |
67
|
|
|
// scheme |
68
|
|
|
if (!array_key_exists('REQUEST_SCHEME', $this->serverData)) { |
69
|
|
|
$requestScheme = 'http'; |
70
|
|
|
} else { |
71
|
|
|
$requestScheme = $this->serverData['REQUEST_SCHEME']; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
// server_name |
75
|
|
|
$serverName = $this->serverData['SERVER_NAME']; |
76
|
|
|
|
77
|
|
|
// port |
78
|
|
|
$serverPort = (int) $this->serverData['SERVER_PORT']; |
79
|
|
|
|
80
|
|
|
$usePort = false; |
81
|
|
|
if ('https' === $requestScheme && 443 !== $serverPort) { |
82
|
|
|
$usePort = true; |
83
|
|
|
} |
84
|
|
|
if ('http' === $requestScheme && 80 !== $serverPort) { |
85
|
|
|
$usePort = true; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
if ($usePort) { |
89
|
|
|
return sprintf('%s://%s:%d', $requestScheme, $serverName, $serverPort); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return sprintf('%s://%s', $requestScheme, $serverName); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function getUri() |
96
|
|
|
{ |
97
|
|
|
$requestUri = $this->serverData['REQUEST_URI']; |
98
|
|
|
|
99
|
|
|
return sprintf('%s%s', $this->getAuthority(), $requestUri); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function getRoot() |
103
|
|
|
{ |
104
|
|
|
$rootDir = dirname($this->serverData['SCRIPT_NAME']); |
105
|
|
|
if ('/' !== $rootDir) { |
106
|
|
|
return sprintf('%s/', $rootDir); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return $rootDir; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function getRootUri() |
113
|
|
|
{ |
114
|
|
|
return sprintf('%s%s', $this->getAuthority(), $this->getRoot()); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function getRequestMethod() |
118
|
|
|
{ |
119
|
|
|
return $this->serverData['REQUEST_METHOD']; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function getServerName() |
123
|
|
|
{ |
124
|
|
|
return $this->serverData['SERVER_NAME']; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function isBrowser() |
128
|
|
|
{ |
129
|
|
|
if (!array_key_exists('HTTP_ACCEPT', $this->serverData)) { |
130
|
|
|
return false; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return false !== mb_strpos($this->serverData['HTTP_ACCEPT'], 'text/html'); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function getPathInfo() |
137
|
|
|
{ |
138
|
|
|
// remove the query string |
139
|
|
|
$requestUri = $this->serverData['REQUEST_URI']; |
140
|
|
|
if (false !== $pos = mb_strpos($requestUri, '?')) { |
141
|
|
|
$requestUri = mb_substr($requestUri, 0, $pos); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
// remove script_name (if it is part of request_uri |
145
|
|
|
if (0 === mb_strpos($requestUri, $this->serverData['SCRIPT_NAME'])) { |
146
|
|
|
return substr($requestUri, mb_strlen($this->serverData['SCRIPT_NAME'])); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
// remove the root |
150
|
|
|
if ('/' !== $this->getRoot()) { |
151
|
|
|
return mb_substr($requestUri, mb_strlen($this->getRoot()) - 1); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
return $requestUri; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
public function getQueryParameter($key, $isRequired = true, $defaultValue = null) |
158
|
|
|
{ |
159
|
|
|
return Utils::getValueFromArray($this->getData, $key, $isRequired, $defaultValue); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
public function getPostParameter($key, $isRequired = true, $defaultValue = null) |
163
|
|
|
{ |
164
|
|
|
return Utils::getValueFromArray($this->postData, $key, $isRequired, $defaultValue); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
public function getHeader($key, $isRequired = true, $defaultValue = null) |
168
|
|
|
{ |
169
|
|
|
return Utils::getValueFromArray($this->serverData, $key, $isRequired, $defaultValue); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
public function getBody() |
173
|
|
|
{ |
174
|
|
|
return $this->rawData; |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|