|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* WebHemi. |
|
4
|
|
|
* |
|
5
|
|
|
* PHP version 7.1 |
|
6
|
|
|
* |
|
7
|
|
|
* @copyright 2012 - 2017 Gixx-web (http://www.gixx-web.com) |
|
8
|
|
|
* @license https://opensource.org/licenses/MIT The MIT License (MIT) |
|
9
|
|
|
* |
|
10
|
|
|
* @link http://www.gixx-web.com |
|
11
|
|
|
*/ |
|
12
|
|
|
declare(strict_types = 1); |
|
13
|
|
|
|
|
14
|
|
|
namespace WebHemi\Environment\ServiceAdapter\Base; |
|
15
|
|
|
|
|
16
|
|
|
use Exception; |
|
17
|
|
|
use InvalidArgumentException; |
|
18
|
|
|
use LayerShifter\TLDExtract\Extract; |
|
19
|
|
|
use LayerShifter\TLDExtract\Result; |
|
20
|
|
|
use WebHemi\Configuration\ServiceInterface as ConfigurationInterface; |
|
21
|
|
|
use WebHemi\Environment\AbstractAdapter; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Class ServiceAdapter. |
|
25
|
|
|
* @SuppressWarnings(PHPMD.TooManyFields) |
|
26
|
|
|
*/ |
|
27
|
|
|
class ServiceAdapter extends AbstractAdapter |
|
28
|
|
|
{ |
|
29
|
|
|
/** @var Extract */ |
|
30
|
|
|
private $domainAdapter; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* ServiceAdapter constructor. |
|
34
|
|
|
* |
|
35
|
|
|
* @param ConfigurationInterface $configuration |
|
36
|
|
|
* @param array $getData |
|
37
|
|
|
* @param array $postData |
|
38
|
|
|
* @param array $serverData |
|
39
|
|
|
* @param array $cookieData |
|
40
|
|
|
* @param array $filesData |
|
41
|
|
|
* @param array $optionsData |
|
42
|
|
|
* @throws Exception |
|
43
|
|
|
*/ |
|
44
|
5 |
|
public function __construct( |
|
45
|
|
|
ConfigurationInterface $configuration, |
|
46
|
|
|
array $getData, |
|
47
|
|
|
array $postData, |
|
48
|
|
|
array $serverData, |
|
49
|
|
|
array $cookieData, |
|
50
|
|
|
array $filesData, |
|
51
|
|
|
array $optionsData |
|
52
|
|
|
) { |
|
53
|
5 |
|
$this->configuration = $configuration->getConfig('applications'); |
|
54
|
5 |
|
$this->domainAdapter = new Extract(); |
|
55
|
5 |
|
$this->applicationRoot = realpath(__DIR__.'/../../../../../'); |
|
56
|
|
|
// In case when the backend sources are out of the document root. |
|
57
|
5 |
|
$this->documentRoot = realpath($this->applicationRoot.'/'); |
|
58
|
5 |
|
$this->options = $optionsData; |
|
59
|
|
|
|
|
60
|
5 |
|
if (isset($serverData['HTTP_REFERER'])) { |
|
61
|
5 |
|
$serverData['HTTP_REFERER'] = urldecode($serverData['HTTP_REFERER']); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
5 |
|
$this->environmentData = [ |
|
65
|
5 |
|
'GET' => $getData, |
|
66
|
5 |
|
'POST' => $postData, |
|
67
|
5 |
|
'SERVER' => $serverData, |
|
68
|
5 |
|
'COOKIE' => $cookieData, |
|
69
|
5 |
|
'FILES' => $filesData, |
|
70
|
|
|
]; |
|
71
|
|
|
|
|
72
|
5 |
|
$this->isHttps = isset($this->environmentData['SERVER']['HTTPS']) && $this->environmentData['SERVER']['HTTPS']; |
|
73
|
5 |
|
$this->url = 'http'.($this->isHttps ? 's' : '').'://' |
|
74
|
5 |
|
.$this->environmentData['SERVER']['HTTP_HOST'] |
|
75
|
5 |
|
.$this->environmentData['SERVER']['REQUEST_URI']; // contains also the query string |
|
76
|
|
|
|
|
77
|
5 |
|
$this->selectedModule = self::DEFAULT_MODULE; |
|
78
|
5 |
|
$this->selectedApplication = self::DEFAULT_APPLICATION; |
|
79
|
5 |
|
$this->selectedTheme = self::DEFAULT_THEME; |
|
80
|
5 |
|
$this->selectedThemeResourcePath = self::DEFAULT_THEME_RESOURCE_PATH; |
|
81
|
5 |
|
$this->selectedApplicationUri = self::DEFAULT_APPLICATION_URI; |
|
82
|
|
|
|
|
83
|
5 |
|
$this->setDomain() |
|
84
|
5 |
|
->setApplication(); |
|
85
|
5 |
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Gets the request URI |
|
89
|
|
|
* |
|
90
|
|
|
* @return string |
|
91
|
|
|
*/ |
|
92
|
1 |
|
public function getRequestUri() : string |
|
93
|
|
|
{ |
|
94
|
1 |
|
return rtrim($this->environmentData['SERVER']['REQUEST_URI'], '/'); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Gets the request method. |
|
99
|
|
|
* |
|
100
|
|
|
* @return string |
|
101
|
|
|
*/ |
|
102
|
1 |
|
public function getRequestMethod(): string |
|
103
|
|
|
{ |
|
104
|
1 |
|
return $this->environmentData['SERVER']['REQUEST_METHOD'] ?? 'GET'; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Gets environment data. |
|
109
|
|
|
* |
|
110
|
|
|
* @param string $key |
|
111
|
|
|
* @return array |
|
112
|
|
|
*/ |
|
113
|
10 |
|
public function getEnvironmentData(string $key) : array |
|
114
|
|
|
{ |
|
115
|
10 |
|
if (!isset($this->environmentData[$key])) { |
|
116
|
1 |
|
throw new InvalidArgumentException(sprintf('The "%s" is not a valid environment key.', $key)); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
10 |
|
return $this->environmentData[$key]; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Gets the client IP address. |
|
124
|
|
|
* |
|
125
|
|
|
* @return string |
|
126
|
|
|
*/ |
|
127
|
1 |
|
public function getClientIp() : string |
|
128
|
|
|
{ |
|
129
|
1 |
|
$ipAddress = ''; |
|
130
|
|
|
|
|
131
|
1 |
|
if (!empty($this->environmentData['SERVER']['HTTP_X_FORWARDED_FOR'])) { |
|
132
|
1 |
|
$ipAddress = $this->environmentData['SERVER']['HTTP_X_FORWARDED_FOR']; |
|
133
|
1 |
|
} elseif (!empty($this->environmentData['SERVER']['REMOTE_ADDR'])) { |
|
134
|
1 |
|
$ipAddress = $this->environmentData['SERVER']['REMOTE_ADDR']; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
1 |
|
return (string) $ipAddress; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Parses server data and tries to set domain information. |
|
142
|
|
|
* |
|
143
|
|
|
* @throws Exception |
|
144
|
|
|
* @return ServiceAdapter |
|
145
|
|
|
*/ |
|
146
|
5 |
|
private function setDomain() : ServiceAdapter |
|
147
|
|
|
{ |
|
148
|
5 |
|
if ('dev' == getenv('APPLICATION_ENV')) { |
|
149
|
|
|
$this->domainAdapter->setExtractionMode(Extract::MODE_ALLOW_NOT_EXISTING_SUFFIXES); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** @var Result $domainParts */ |
|
153
|
5 |
|
$domainParts = $this->domainAdapter->parse($this->url); |
|
154
|
|
|
|
|
155
|
5 |
|
if (empty($domainParts->getSuffix())) { |
|
156
|
|
|
throw new Exception('This application does not support IP access'); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
// Redirecting to www when no subdomain is present |
|
160
|
|
|
// @codeCoverageIgnoreStart |
|
161
|
|
|
if (!defined('PHPUNIT_WEBHEMI_TESTSUITE') && empty($domainParts->getSubdomain())) { |
|
162
|
|
|
$schema = 'http'.($this->isSecuredApplication() ? 's' : '').'://'; |
|
163
|
|
|
$uri = $this->environmentData['SERVER']['REQUEST_URI']; |
|
164
|
|
|
header('Location: '.$schema.'www.'.$domainParts->getFullHost().$uri); |
|
165
|
|
|
exit; |
|
166
|
|
|
} |
|
167
|
|
|
// @codeCoverageIgnoreEnd |
|
168
|
|
|
|
|
169
|
5 |
|
$this->subDomain = $domainParts->getSubdomain(); |
|
170
|
5 |
|
$this->mainDomain = $domainParts->getHostname().'.'.$domainParts->getSuffix(); |
|
171
|
5 |
|
$this->applicationDomain = $domainParts->getFullHost(); |
|
172
|
|
|
|
|
173
|
5 |
|
return $this; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* Sets application related data. |
|
178
|
|
|
* |
|
179
|
|
|
* @throws Exception |
|
180
|
|
|
* @return ServiceAdapter |
|
181
|
|
|
*/ |
|
182
|
5 |
|
private function setApplication() : ServiceAdapter |
|
183
|
|
|
{ |
|
184
|
|
|
// for safety purposes |
|
185
|
5 |
|
if (!isset($this->applicationDomain)) { |
|
186
|
|
|
$this->setDomain(); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
5 |
|
$urlParts = parse_url($this->url); |
|
190
|
5 |
|
list($subDirectory) = explode('/', ltrim($urlParts['path'], '/'), 2); |
|
191
|
|
|
|
|
192
|
5 |
|
$applications = $this->configuration->toArray(); |
|
193
|
5 |
|
$aplicationNames = array_keys($applications); |
|
194
|
5 |
|
$selectedApplication = $this->getSelectedApplicationName($aplicationNames, $subDirectory); |
|
195
|
|
|
|
|
196
|
5 |
|
$applicationData = $applications[$selectedApplication]; |
|
197
|
|
|
|
|
198
|
5 |
|
$this->selectedModule = $applicationData['module'] ?? self::DEFAULT_MODULE; |
|
199
|
5 |
|
$this->selectedApplication = $selectedApplication; |
|
200
|
5 |
|
$this->selectedTheme = $applicationData['theme'] ?? self::DEFAULT_THEME; |
|
201
|
5 |
|
$this->selectedApplicationUri = $applicationData['type'] == self::APPLICATION_TYPE_DIRECTORY |
|
202
|
1 |
|
? '/'.$subDirectory |
|
203
|
4 |
|
: '/'; |
|
204
|
|
|
|
|
205
|
|
|
// Final check for config and resources. |
|
206
|
5 |
|
if ($this->selectedTheme !== self::DEFAULT_THEME) { |
|
207
|
1 |
|
$this->selectedThemeResourcePath = '/resources/vendor_themes/'.$this->selectedTheme; |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
5 |
|
return $this; |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
/** |
|
214
|
|
|
* Gets the selected application's name. |
|
215
|
|
|
* |
|
216
|
|
|
* @param array $aplicationNames |
|
217
|
|
|
* @param string $subDirectory |
|
218
|
|
|
* @return string' |
|
|
|
|
|
|
219
|
|
|
*/ |
|
220
|
5 |
|
private function getSelectedApplicationName(array $aplicationNames, string $subDirectory) : string |
|
221
|
|
|
{ |
|
222
|
5 |
|
$selectedApplication = self::DEFAULT_APPLICATION; |
|
223
|
|
|
|
|
224
|
|
|
/** @var string $applicationName */ |
|
225
|
5 |
|
foreach ($aplicationNames as $applicationName) { |
|
226
|
5 |
|
if ($this->checkDirectoryIsValid($applicationName, $subDirectory) |
|
227
|
4 |
|
|| $this->checkDomainIsValid($applicationName) |
|
228
|
|
|
) { |
|
229
|
5 |
|
$selectedApplication = (string) $applicationName; |
|
230
|
5 |
|
break; |
|
231
|
|
|
} |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
5 |
|
return $selectedApplication; |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
/** |
|
238
|
|
|
* Checks from type, path it the current URI segment is valid. |
|
239
|
|
|
* |
|
240
|
|
|
* @param string $applicationName |
|
241
|
|
|
* @param string $subDirectory |
|
242
|
|
|
* @return bool |
|
243
|
|
|
*/ |
|
244
|
5 |
|
private function checkDirectoryIsValid(string $applicationName, string $subDirectory) : bool |
|
245
|
|
|
{ |
|
246
|
5 |
|
$applications = $this->configuration->toArray(); |
|
247
|
5 |
|
$applicationData = $applications[$applicationName]; |
|
248
|
|
|
|
|
249
|
5 |
|
return $applicationName != 'website' |
|
250
|
4 |
|
&& $this->applicationDomain == $applicationData['domain'] |
|
251
|
4 |
|
&& !empty($subDirectory) |
|
252
|
3 |
|
&& $applicationData['type'] == self::APPLICATION_TYPE_DIRECTORY |
|
253
|
5 |
|
&& $applicationData['path'] == '/'.$subDirectory; |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
/** |
|
257
|
|
|
* Checks from type and path if the domain is valid. If so, it sets the $subDirectory to the default. |
|
258
|
|
|
* |
|
259
|
|
|
* @param string $applicationName |
|
260
|
|
|
* @return bool |
|
261
|
|
|
*/ |
|
262
|
4 |
|
private function checkDomainIsValid(string $applicationName) : bool |
|
263
|
|
|
{ |
|
264
|
4 |
|
$applications = $this->configuration->toArray(); |
|
265
|
4 |
|
$applicationData = $applications[$applicationName]; |
|
266
|
|
|
|
|
267
|
4 |
|
return $this->applicationDomain == $applicationData['domain'] |
|
268
|
4 |
|
&& $applicationData['type'] == self::APPLICATION_TYPE_DOMAIN; |
|
269
|
|
|
} |
|
270
|
|
|
} |
|
271
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.