1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* admin |
5
|
|
|
* |
6
|
|
|
* @category Tollwerk |
7
|
|
|
* @package Tollwerk\Admin |
8
|
|
|
* @subpackage Tollwerk\Admin\Application\Service |
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\Application\Service; |
38
|
|
|
|
39
|
|
|
use Tollwerk\Admin\Domain\Account\AccountInterface; |
40
|
|
|
use Tollwerk\Admin\Domain\Domain\DomainInterface; |
41
|
|
|
use Tollwerk\Admin\Domain\Vhost\Vhost; |
42
|
|
|
use Tollwerk\Admin\Domain\Vhost\VhostInterface; |
43
|
|
|
use Tollwerk\Admin\Infrastructure\Service\DirectoryService; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Virtual host service |
47
|
|
|
* |
48
|
|
|
* @package Tollwerk\Admin |
49
|
|
|
* @subpackage Tollwerk\Admin\Application |
50
|
|
|
*/ |
51
|
|
|
class VirtualHostService extends AbstractService |
52
|
|
|
{ |
53
|
|
|
/** |
54
|
|
|
* Create a virtual host |
55
|
|
|
* |
56
|
|
|
* @param AccountInterface $account Account |
57
|
|
|
* @param DomainInterface $domain Domain |
58
|
|
|
* @param string $docroot Document root |
59
|
|
|
* @param string $type Virtual host Type |
60
|
|
|
* @return VhostInterface Virtual host |
61
|
|
|
*/ |
62
|
|
|
public function create( |
63
|
|
|
AccountInterface $account, |
64
|
|
|
DomainInterface $domain, |
65
|
|
|
$docroot = '', |
66
|
|
|
$type = Vhost::TYPE_APACHE |
67
|
|
|
) { |
68
|
|
|
return $this->storageAdapterStrategy->createVhost( |
69
|
|
|
$account, |
70
|
|
|
$domain, |
71
|
|
|
$this->validateDocroot($account, $docroot), |
72
|
|
|
$type |
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Delete a virtual host |
78
|
|
|
* |
79
|
|
|
* @param AccountInterface $account Account |
80
|
|
|
* @param string $docroot Document root |
81
|
|
|
* @return VhostInterface Virtual host |
82
|
|
|
*/ |
83
|
|
|
public function delete(AccountInterface $account, $docroot = '') |
84
|
|
|
{ |
85
|
|
|
return $this->storageAdapterStrategy->deleteVhost($account, $this->validateDocroot($account, $docroot)); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Enable a virtual host |
90
|
|
|
* |
91
|
|
|
* @param AccountInterface $account Account |
92
|
|
|
* @param string $docroot Document root |
93
|
|
|
* @return VhostInterface Virtual host |
94
|
|
|
*/ |
95
|
|
|
public function enable(AccountInterface $account, $docroot = '') |
96
|
|
|
{ |
97
|
|
|
return $this->storageAdapterStrategy->enableVhost($account, $this->validateDocroot($account, $docroot)); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Disable a virtual host |
102
|
|
|
* |
103
|
|
|
* @param AccountInterface $account Account |
104
|
|
|
* @param string $docroot Document root |
105
|
|
|
* @return VhostInterface Virtual host |
106
|
|
|
*/ |
107
|
|
|
public function disable(AccountInterface $account, $docroot = '') |
108
|
|
|
{ |
109
|
|
|
return $this->storageAdapterStrategy->disableVhost($account, $this->validateDocroot($account, $docroot)); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Redirect a virtual host |
114
|
|
|
* |
115
|
|
|
* @param string $account Account name |
116
|
|
|
* @param string $docroot Document root |
117
|
|
|
* @param string $url Redirect URL |
118
|
|
|
* @param int $status Redirect HTTP status |
119
|
|
|
* @return VhostInterface Virtual host |
120
|
|
|
* @throws \RuntimeException If the redirect URL is invalid |
121
|
|
|
* @throws \RuntimeException If the redirect HTTP status code is invalid |
122
|
|
|
*/ |
123
|
|
|
public function redirect( |
124
|
|
|
AccountInterface $account, |
125
|
|
|
$docroot = '', |
126
|
|
|
$url = '', |
127
|
|
|
$status = Vhost::REDIRECT_DEFAULT_STATUS |
128
|
|
|
) { |
129
|
|
|
$url = trim($url) ?: null; |
130
|
|
|
|
131
|
|
|
// If the redirect URL is invalid |
132
|
|
|
if (($url !== null) && |
133
|
|
|
(!filter_var($url, FILTER_VALIDATE_URL) |
134
|
|
|
|| !in_array(strtolower(parse_url($url, PHP_URL_SCHEME)), Vhost::$supportedProtocols)) |
135
|
|
|
) { |
136
|
|
|
throw new \RuntimeException(sprintf('Invalid redirect URL "%s"', $url), 1475486589); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
// If the redirect HTTP status code is invalid |
140
|
|
|
if (!is_int($status) || (($status < 300) || ($status > 308))) { |
141
|
|
|
throw new \RuntimeException(sprintf('Invalid redirect HTTP status code "%s"', $status), 1475486679); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return $this->storageAdapterStrategy->redirectVhost( |
145
|
|
|
$account, |
146
|
|
|
$this->validateDocroot($account, $docroot), |
147
|
|
|
$url, |
148
|
|
|
$status |
149
|
|
|
); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Configure the PHP version of a virtual host |
154
|
|
|
* |
155
|
|
|
* @param string $account Account name |
156
|
|
|
* @param string $docroot Document root |
157
|
|
|
* @param string|null $php PHP version |
158
|
|
|
* @return VhostInterface Virtual host |
159
|
|
|
* @throws \RuntimeException If the redirect URL is invalid |
160
|
|
|
* @throws \RuntimeException If the redirect HTTP status code is invalid |
161
|
|
|
*/ |
162
|
|
|
public function php(AccountInterface $account, $docroot = '', $php = null) |
163
|
|
|
{ |
164
|
|
|
$php = trim($php) ?: null; |
165
|
|
|
|
166
|
|
|
// If the PHP version is invalid |
167
|
|
|
if (($php !== null) && (!preg_match("%^[5789]\.\d$%", $php) || (floatval($php) < 5.6))) { |
168
|
|
|
throw new \RuntimeException(sprintf('Invalid PHP version "%s"', $php), 1475937755); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
return $this->storageAdapterStrategy->phpVhost($account, $this->validateDocroot($account, $docroot), $php); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Configure a protocol based port for a virtual host |
176
|
|
|
* |
177
|
|
|
* @param AccountInterface $account Account |
178
|
|
|
* @param string $docroot Document root |
179
|
|
|
* @param int $protocol Protocol |
180
|
|
|
* @param int|null $port Port |
181
|
|
|
* @return VhostInterface Virtual host |
182
|
|
|
* @throws \RuntimeException If the protocol is invalid |
183
|
|
|
* @throws \RuntimeException If the protocol port is invalid |
184
|
|
|
*/ |
185
|
|
|
public function port( |
186
|
|
|
AccountInterface $account, |
187
|
|
|
$docroot = '', |
188
|
|
|
$protocol = \Tollwerk\Admin\Domain\Vhost\Vhost::PROTOCOL_HTTP, |
189
|
|
|
$port = null |
190
|
|
|
) { |
191
|
|
|
// If the protocol is unsupported |
192
|
|
|
if (empty(Vhost::$supportedProtocols[$protocol])) { |
193
|
|
|
throw new \RuntimeException(sprintf('Invalid protocol "%s"', $protocol), 1475484081); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
// If the protocol port is invalid |
197
|
|
|
$port = ($port === null) ? Vhost::$defaultProtocolPorts[$protocol] : (intval($port) ?: null); |
198
|
|
|
if (($port !== null) && ($port <= 0)) { |
199
|
|
|
throw new \RuntimeException(sprintf('Invalid protocol port "%s"', $port), 1475502412); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
return $this->storageAdapterStrategy->portVhost( |
203
|
|
|
$account, |
204
|
|
|
$this->validateDocroot($account, $docroot), |
205
|
|
|
$protocol, |
206
|
|
|
$port |
207
|
|
|
); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Add a secondary domain to a virtual host |
212
|
|
|
* |
213
|
|
|
* @param string $account Account name |
214
|
|
|
* @param DomainInterface $domain Domain |
215
|
|
|
* @param string $docroot Document root |
216
|
|
|
* @return VhostInterface Virtual host |
217
|
|
|
*/ |
218
|
|
|
public function addDomain(AccountInterface $account, DomainInterface $domain, $docroot = '') |
219
|
|
|
{ |
220
|
|
|
return $this->storageAdapterStrategy->addVhostDomain( |
221
|
|
|
$account, |
222
|
|
|
$this->validateDocroot($account, $docroot), |
223
|
|
|
$domain |
224
|
|
|
); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Remove a secondary domain from a virtual host |
229
|
|
|
* |
230
|
|
|
* @param string $account Account name |
231
|
|
|
* @param DomainInterface $domain Domain |
232
|
|
|
* @param string $docroot Document root |
233
|
|
|
* @return VhostInterface Virtual host |
234
|
|
|
*/ |
235
|
|
|
public function removeDomain(AccountInterface $account, DomainInterface $domain, $docroot = '') |
236
|
|
|
{ |
237
|
|
|
return $this->storageAdapterStrategy->removeVhostDomain( |
238
|
|
|
$account, |
239
|
|
|
$this->validateDocroot($account, $docroot), |
240
|
|
|
$domain |
241
|
|
|
); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Validate a given document root path |
246
|
|
|
* |
247
|
|
|
* @param AccountInterface $account Account |
248
|
|
|
* @param string $docroot Document root |
249
|
|
|
* @return string Validated document root |
250
|
|
|
*/ |
251
|
|
|
protected function validateDocroot(AccountInterface $account, $docroot) |
252
|
|
|
{ |
253
|
|
|
$accountDirectorySvc = new DirectoryService($account); |
254
|
|
|
return $accountDirectorySvc->getDataDirectory($docroot, false); |
255
|
|
|
} |
256
|
|
|
} |
257
|
|
|
|