1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* admin |
5
|
|
|
* |
6
|
|
|
* @category Tollwerk |
7
|
|
|
* @package Tollwerk\Admin |
8
|
|
|
* @subpackage Tollwerk\Admin\Domain\Vhost |
9
|
|
|
* @author Joschi Kuphal <[email protected]> / @jkphl |
10
|
|
|
* @copyright Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl |
11
|
|
|
* @license http://opensource.org/licenses/MIT The MIT License (MIT) |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
/*********************************************************************************** |
15
|
|
|
* The MIT License (MIT) |
16
|
|
|
* |
17
|
|
|
* Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl |
18
|
|
|
* |
19
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of |
20
|
|
|
* this software and associated documentation files (the "Software"), to deal in |
21
|
|
|
* the Software without restriction, including without limitation the rights to |
22
|
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
23
|
|
|
* the Software, and to permit persons to whom the Software is furnished to do so, |
24
|
|
|
* subject to the following conditions: |
25
|
|
|
* |
26
|
|
|
* The above copyright notice and this permission notice shall be included in all |
27
|
|
|
* copies or substantial portions of the Software. |
28
|
|
|
* |
29
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
30
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
31
|
|
|
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
32
|
|
|
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
33
|
|
|
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
34
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
35
|
|
|
***********************************************************************************/ |
36
|
|
|
|
37
|
|
|
namespace Tollwerk\Admin\Domain\Vhost; |
38
|
|
|
|
39
|
|
|
use Tollwerk\Admin\Domain\Domain\DomainInterface; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Virtual host |
43
|
|
|
* |
44
|
|
|
* @package Tollwerk\Admin |
45
|
|
|
* @subpackage Tollwerk\Admin\Domain |
46
|
|
|
*/ |
47
|
|
|
class Vhost implements VhostInterface |
48
|
|
|
{ |
49
|
|
|
/** |
50
|
|
|
* Active account |
51
|
|
|
* |
52
|
|
|
* @var boolean |
53
|
|
|
*/ |
54
|
|
|
protected $active = false; |
55
|
|
|
/** |
56
|
|
|
* Primary domain |
57
|
|
|
* |
58
|
|
|
* @var DomainInterface |
59
|
|
|
*/ |
60
|
|
|
protected $primaryDomain; |
61
|
|
|
/** |
62
|
|
|
* Secondary domains |
63
|
|
|
* |
64
|
|
|
* @var DomainInterface[] |
65
|
|
|
*/ |
66
|
|
|
protected $secondaryDomains = []; |
67
|
|
|
/** |
68
|
|
|
* Document root |
69
|
|
|
* |
70
|
|
|
* @var string |
71
|
|
|
*/ |
72
|
|
|
protected $docroot; |
73
|
|
|
/** |
74
|
|
|
* Virtual host type |
75
|
|
|
* |
76
|
|
|
* @var string |
77
|
|
|
*/ |
78
|
|
|
protected $type = self::TYPE_APACHE; |
79
|
|
|
/** |
80
|
|
|
* Ports |
81
|
|
|
* |
82
|
|
|
* @var array |
83
|
|
|
*/ |
84
|
|
|
protected $ports = []; |
85
|
|
|
/** |
86
|
|
|
* Active PHP version |
87
|
|
|
* |
88
|
|
|
* @var null|string |
89
|
|
|
*/ |
90
|
|
|
protected $php = null; |
91
|
|
|
/** |
92
|
|
|
* Absolute URL to redirect to |
93
|
|
|
* |
94
|
|
|
* @var null|string |
95
|
|
|
*/ |
96
|
|
|
protected $redirectUrl = null; |
97
|
|
|
/** |
98
|
|
|
* Redirect status code |
99
|
|
|
* |
100
|
|
|
* @var int |
101
|
|
|
*/ |
102
|
|
|
protected $redirectStatus = self::REDIRECT_DEFAULT_STATUS; |
103
|
|
|
/** |
104
|
|
|
* Default port for HTTP virtual hosts |
105
|
|
|
* |
106
|
|
|
* @var int |
107
|
|
|
*/ |
108
|
|
|
const PORT_HTTP_DEFAULT = 80; |
109
|
|
|
/** |
110
|
|
|
* Default port for HTTPS virtual hosts |
111
|
|
|
* |
112
|
|
|
* @var int |
113
|
|
|
*/ |
114
|
|
|
const PORT_HTTPS_DEFAULT = 443; |
115
|
|
|
/** |
116
|
|
|
* Default redirect status |
117
|
|
|
* |
118
|
|
|
* @var int |
119
|
|
|
*/ |
120
|
|
|
const REDIRECT_DEFAULT_STATUS = 301; |
121
|
|
|
/** |
122
|
|
|
* HTTP protocol |
123
|
|
|
* |
124
|
|
|
* @var int |
125
|
|
|
*/ |
126
|
|
|
const PROTOCOL_HTTP = 1; |
127
|
|
|
/** |
128
|
|
|
* HTTPS protocol |
129
|
|
|
* |
130
|
|
|
* @var int |
131
|
|
|
*/ |
132
|
|
|
const PROTOCOL_HTTPS = 2; |
133
|
|
|
/** |
134
|
|
|
* Apache virtual host |
135
|
|
|
* |
136
|
|
|
* @var string |
137
|
|
|
*/ |
138
|
|
|
const TYPE_APACHE = 'apache'; |
139
|
|
|
/** |
140
|
|
|
* Supported protocols |
141
|
|
|
* |
142
|
|
|
* @var array |
143
|
|
|
*/ |
144
|
|
|
public static $supportedProtocols = [ |
145
|
|
|
self::PROTOCOL_HTTP => 'http', |
146
|
|
|
self::PROTOCOL_HTTPS => 'https', |
147
|
|
|
]; |
148
|
|
|
/** |
149
|
|
|
* Default protocol ports |
150
|
|
|
* |
151
|
|
|
* @var array |
152
|
|
|
*/ |
153
|
|
|
public static $defaultProtocolPorts = [ |
154
|
|
|
self::PROTOCOL_HTTP => 80, |
155
|
|
|
self::PROTOCOL_HTTPS => 443, |
156
|
|
|
]; |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Virtual host constructor |
160
|
|
|
* |
161
|
|
|
* @param DomainInterface $primaryDomain Primary domain |
162
|
|
|
* @param string $docroot Document root |
163
|
|
|
* @param string $type Virtual host type |
164
|
|
|
* @internal param int $port Port |
165
|
|
|
*/ |
166
|
10 |
|
public function __construct(DomainInterface $primaryDomain, $docroot, $type = self::TYPE_APACHE) |
167
|
|
|
{ |
168
|
10 |
|
$this->primaryDomain = $primaryDomain; |
169
|
10 |
|
$this->docroot = $docroot; |
170
|
10 |
|
$this->type = $type; |
171
|
10 |
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Return whether the account is active |
175
|
|
|
* |
176
|
|
|
* @return boolean Active |
177
|
|
|
*/ |
178
|
|
|
public function isActive() |
179
|
|
|
{ |
180
|
|
|
return $this->active; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Set whether the account is active |
185
|
|
|
* |
186
|
|
|
* @param boolean $active Active |
187
|
|
|
* @return VhostInterface Self reference |
188
|
|
|
*/ |
189
|
|
|
public function setActive($active) |
190
|
|
|
{ |
191
|
|
|
$this->active = $active; |
192
|
|
|
return $this; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Return the primary domain |
197
|
|
|
* |
198
|
|
|
* @return DomainInterface Primary domain |
199
|
|
|
*/ |
200
|
2 |
|
public function getPrimaryDomain() |
201
|
|
|
{ |
202
|
2 |
|
return $this->primaryDomain; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Return the document root |
207
|
|
|
* |
208
|
|
|
* @return string Document root |
209
|
|
|
*/ |
210
|
1 |
|
public function getDocroot() |
211
|
|
|
{ |
212
|
1 |
|
return $this->docroot; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Return the virtual host type |
217
|
|
|
* |
218
|
|
|
* @return string Virtual host type |
219
|
|
|
*/ |
220
|
|
|
public function getType() |
221
|
|
|
{ |
222
|
|
|
return $this->type; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Return the port |
227
|
|
|
* |
228
|
|
|
* @param int $protocol Protocol |
229
|
|
|
* @return int|null Port |
230
|
|
|
* @throws \RuntimeException If the protocol is unsupported |
231
|
|
|
*/ |
232
|
3 |
|
public function getPort($protocol) |
233
|
|
|
{ |
234
|
3 |
|
$protocol = intval($protocol); |
235
|
|
|
|
236
|
|
|
// If the protocol is unsupported |
237
|
3 |
|
if (empty(self::$supportedProtocols[$protocol])) { |
238
|
1 |
|
throw new \RuntimeException(sprintf('Invalid protocol "%s"', $protocol), 1475484081); |
239
|
|
|
} |
240
|
|
|
|
241
|
2 |
|
return empty($this->ports[$protocol]) ? null : $this->ports[$protocol]; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Return all supported protocols and corresponding ports |
246
|
|
|
* |
247
|
|
|
* @return array Protocols and ports |
248
|
|
|
*/ |
249
|
1 |
|
public function getPorts() |
250
|
|
|
{ |
251
|
1 |
|
return $this->ports; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* Return the secondary domains |
256
|
|
|
* |
257
|
|
|
* @param bool $excludeWildcards Exclude wildcard domains |
258
|
|
|
* @return DomainInterface[] Secondary domains |
259
|
|
|
*/ |
260
|
1 |
|
public function getSecondaryDomains($excludeWildcards = false) |
261
|
|
|
{ |
262
|
1 |
|
$secondaryDomains = array_values($this->secondaryDomains); |
263
|
1 |
|
return $excludeWildcards ? |
264
|
|
|
array_filter($secondaryDomains, function (DomainInterface $domain) { |
265
|
|
|
return !$domain->isWildcard(); |
266
|
|
|
}) : |
267
|
1 |
|
$secondaryDomains; |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* Set the secondary domains |
272
|
|
|
* |
273
|
|
|
* @param DomainInterface[] $secondaryDomains |
274
|
|
|
* @return Vhost Self reference |
275
|
|
|
* @throws \RuntimeException If the domain is invalid |
276
|
|
|
*/ |
277
|
1 |
|
public function setSecondaryDomains(array $secondaryDomains) |
278
|
|
|
{ |
279
|
1 |
|
$this->secondaryDomains = []; |
280
|
|
|
/** @var DomainInterface $secondaryDomain */ |
281
|
1 |
|
foreach ($secondaryDomains as $secondaryDomain) { |
282
|
|
|
// If the domain is invalid |
283
|
1 |
|
if (!is_object($secondaryDomain) |
284
|
1 |
|
|| !(new \ReflectionClass($secondaryDomain))->implementsInterface(DomainInterface::class) |
285
|
|
|
) { |
286
|
1 |
|
throw new \RuntimeException(sprintf('Invalid secondary domain "%s"', $secondaryDomain), 1475484852); |
287
|
|
|
} |
288
|
1 |
|
$this->secondaryDomains[strval($secondaryDomain)] = $secondaryDomain; |
289
|
|
|
} |
290
|
|
|
|
291
|
1 |
|
return $this; |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
/** |
295
|
|
|
* Add a secondary domain |
296
|
|
|
* |
297
|
|
|
* @param DomainInterface $secondaryDomain Secondary domain |
298
|
|
|
* @return Vhost Self reference |
299
|
|
|
*/ |
300
|
1 |
|
public function addSecondaryDomain(DomainInterface $secondaryDomain) |
301
|
|
|
{ |
302
|
1 |
|
if (!array_key_exists(strval($secondaryDomain), $this->secondaryDomains)) { |
303
|
1 |
|
$this->secondaryDomains[strval($secondaryDomain)] = $secondaryDomain; |
304
|
|
|
} |
305
|
1 |
|
return $this; |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* Remove a secondary domain |
310
|
|
|
* |
311
|
|
|
* @param DomainInterface $secondaryDomain Secondary domain |
312
|
|
|
* @return Vhost Self reference |
313
|
|
|
*/ |
314
|
1 |
|
public function removeSecondaryDomain(DomainInterface $secondaryDomain) |
315
|
|
|
{ |
316
|
1 |
|
unset($this->secondaryDomains[strval($secondaryDomain)]); |
317
|
1 |
|
return $this; |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
/** |
321
|
|
|
* Return the active PHP version |
322
|
|
|
* |
323
|
|
|
* @return null|string Active PHP version |
324
|
|
|
*/ |
325
|
1 |
|
public function getPhp() |
326
|
|
|
{ |
327
|
1 |
|
return $this->php; |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
/** |
331
|
|
|
* Set the active PHP version |
332
|
|
|
* |
333
|
|
|
* @param null|string $php Active PHP version |
334
|
|
|
* @return Vhost Self reference |
335
|
|
|
* @throws \RuntimeException If the PHP version is invalid |
336
|
|
|
*/ |
337
|
1 |
|
public function setPhp($php) |
338
|
|
|
{ |
339
|
|
|
// If the PHP version is invalid |
340
|
1 |
|
if (($php !== null) && !preg_match('%^\d\.\d$%', $php)) { |
341
|
1 |
|
throw new \RuntimeException(sprintf('Invalid PHP version "%s"', $php), 1475485163); |
342
|
|
|
} |
343
|
|
|
|
344
|
1 |
|
$this->php = $php; |
345
|
1 |
|
return $this; |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
/** |
349
|
|
|
* Enable a supported protocol |
350
|
|
|
* |
351
|
|
|
* @param int $protocol Protocol |
352
|
|
|
* @param int $port Port |
353
|
|
|
* @return Vhost Self reference |
354
|
|
|
* @throws \RuntimeException If the protocol is unsupported |
355
|
|
|
* @throws \RuntimeException If the protocol port is invalid |
356
|
|
|
*/ |
357
|
3 |
|
public function enableProtocol($protocol, $port = null) |
358
|
|
|
{ |
359
|
3 |
|
$protocol = intval($protocol); |
360
|
|
|
|
361
|
|
|
// If the protocol is unsupported |
362
|
3 |
|
if (empty(self::$supportedProtocols[$protocol])) { |
363
|
1 |
|
throw new \RuntimeException(sprintf('Invalid protocol "%s"', $protocol), 1475484081); |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
// If the protocol port is invalid |
367
|
3 |
|
$port = ($port === null) ? self::$defaultProtocolPorts[$protocol] : intval($port); |
368
|
3 |
|
if ($port <= 0) { |
369
|
1 |
|
throw new \RuntimeException(sprintf('Invalid protocol port "%s"', $port), 1475502412); |
370
|
|
|
} |
371
|
|
|
|
372
|
2 |
|
$this->ports[$protocol] = $port; |
373
|
2 |
|
return $this; |
374
|
|
|
} |
375
|
|
|
|
376
|
|
|
/** |
377
|
|
|
* Disable a supported protocol |
378
|
|
|
* |
379
|
|
|
* @param int $protocol Protocol |
380
|
|
|
* @return Vhost Self reference |
381
|
|
|
* @throws \RuntimeException If the protocol is unsupported |
382
|
|
|
*/ |
383
|
2 |
|
public function disableProtocol($protocol) |
384
|
|
|
{ |
385
|
2 |
|
$protocol = intval($protocol); |
386
|
|
|
|
387
|
|
|
// If the protocol is unsupported |
388
|
2 |
|
if (empty(self::$supportedProtocols[$protocol])) { |
389
|
1 |
|
throw new \RuntimeException(sprintf('Invalid protocol "%s"', $protocol), 1475484081); |
390
|
|
|
} |
391
|
|
|
|
392
|
1 |
|
unset($this->ports[$protocol]); |
393
|
1 |
|
return $this; |
394
|
|
|
} |
395
|
|
|
|
396
|
|
|
/** |
397
|
|
|
* Return the redirect URL |
398
|
|
|
* |
399
|
|
|
* @return null|string Redirect URL |
400
|
|
|
*/ |
401
|
1 |
|
public function getRedirectUrl() |
402
|
|
|
{ |
403
|
1 |
|
return $this->redirectUrl; |
404
|
|
|
} |
405
|
|
|
|
406
|
|
|
/** |
407
|
|
|
* Set the redirect URL |
408
|
|
|
* |
409
|
|
|
* @param null|string $redirectUrl Redirect URL |
410
|
|
|
* @return Vhost Self reference |
411
|
|
|
* @throws \RuntimeException If the redirect URL is invalid |
412
|
|
|
*/ |
413
|
1 |
|
public function setRedirectUrl($redirectUrl) |
414
|
|
|
{ |
415
|
1 |
|
$redirectUrl = trim($redirectUrl) ?: null; |
416
|
|
|
|
417
|
|
|
// If the redirect URL is invalid |
418
|
1 |
|
if (($redirectUrl !== null) && |
419
|
1 |
|
(!filter_var($redirectUrl, FILTER_VALIDATE_URL) |
420
|
1 |
|
|| !in_array(strtolower(parse_url($redirectUrl, PHP_URL_SCHEME)), self::$supportedProtocols)) |
421
|
|
|
) { |
422
|
1 |
|
throw new \RuntimeException(sprintf('Invalid redirect URL "%s"', $redirectUrl), 1475486589); |
423
|
|
|
} |
424
|
|
|
|
425
|
1 |
|
$this->redirectUrl = $redirectUrl; |
426
|
1 |
|
return $this; |
427
|
|
|
} |
428
|
|
|
|
429
|
|
|
/** |
430
|
|
|
* Return the redirect HTTP status code |
431
|
|
|
* |
432
|
|
|
* @return int Redirect HTTP status code |
433
|
|
|
*/ |
434
|
1 |
|
public function getRedirectStatus() |
435
|
|
|
{ |
436
|
1 |
|
return $this->redirectStatus; |
437
|
|
|
} |
438
|
|
|
|
439
|
|
|
/** |
440
|
|
|
* Set the redirect HTTP status code |
441
|
|
|
* |
442
|
|
|
* @param int $redirectStatus Redirect HTTP status code |
443
|
|
|
* @return Vhost Self reference |
444
|
|
|
* @throw \RuntimeException If the redirect HTTP status code is invalid |
445
|
|
|
*/ |
446
|
1 |
|
public function setRedirectStatus($redirectStatus) |
447
|
|
|
{ |
448
|
|
|
// If the redirect HTTP status code is invalid |
449
|
1 |
|
if (!is_int($redirectStatus) || (($redirectStatus < 300) || ($redirectStatus > 308))) { |
450
|
1 |
|
throw new \RuntimeException(sprintf('Invalid redirect HTTP status code "%s"', $redirectStatus), 1475486679); |
451
|
|
|
} |
452
|
1 |
|
$this->redirectStatus = $redirectStatus; |
453
|
1 |
|
return $this; |
454
|
|
|
} |
455
|
|
|
} |
456
|
|
|
|