1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* admin |
5
|
|
|
* |
6
|
|
|
* @category Tollwerk |
7
|
|
|
* @package Tollwerk\Admin |
8
|
|
|
* @subpackage Tollwerk\Admin\Infrastructure\Persistence |
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\Infrastructure\Persistence; |
38
|
|
|
|
39
|
|
|
use Psr\Log\LogLevel; |
40
|
|
|
use Tollwerk\Admin\Domain\Account\AccountInterface; |
41
|
|
|
use Tollwerk\Admin\Domain\Vhost\Vhost; |
42
|
|
|
use Tollwerk\Admin\Domain\Vhost\VhostInterface; |
43
|
|
|
use Tollwerk\Admin\Infrastructure\App; |
44
|
|
|
use Tollwerk\Admin\Infrastructure\Factory\CertbotServiceFactory; |
45
|
|
|
use Tollwerk\Admin\Infrastructure\Service\TemplateService; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Apache persister |
49
|
|
|
* |
50
|
|
|
* @package Tollwerk\Admin |
51
|
|
|
* @subpackage Tollwerk\Admin\Infrastructure |
52
|
|
|
*/ |
53
|
|
|
class Apache |
54
|
|
|
{ |
55
|
|
|
/** |
56
|
|
|
* Account |
57
|
|
|
* |
58
|
|
|
* @var AccountInterface |
59
|
|
|
*/ |
60
|
|
|
protected $account; |
61
|
|
|
/** |
62
|
|
|
* Configuration |
63
|
|
|
* |
64
|
|
|
* @var array |
65
|
|
|
*/ |
66
|
|
|
protected $config; |
67
|
|
|
/** |
68
|
|
|
* Account helper |
69
|
|
|
* |
70
|
|
|
* @var AccountHelper |
71
|
|
|
*/ |
72
|
|
|
protected $helper; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Constructor |
76
|
|
|
* |
77
|
|
|
* @param AccountInterface $account Account |
78
|
|
|
* @param array $config Configuration |
79
|
|
|
*/ |
80
|
|
|
public function __construct(AccountInterface $account, array $config) |
81
|
|
|
{ |
82
|
|
|
$this->account = $account; |
83
|
|
|
$this->config = $config; |
84
|
|
|
$this->helper = new AccountHelper($this->account); |
85
|
|
|
|
86
|
|
|
// Add some directory names |
87
|
|
|
$this->config['dataroot'] = $this->helper->directory('data'); |
88
|
|
|
$this->config['vhostroot'] = $this->helper->directory('config'.DIRECTORY_SEPARATOR.'vhosts-available'); |
89
|
|
|
$this->config['logdir'] = $this->helper->directory('log'); |
90
|
|
|
$this->config['account'] = $this->account->getName(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Persist a virtual host |
95
|
|
|
* |
96
|
|
|
* @param VhostInterface $vhost Virtual host |
97
|
|
|
* @return array Files |
98
|
|
|
*/ |
99
|
|
|
public function __invoke(VhostInterface $vhost) |
100
|
|
|
{ |
101
|
|
|
$files = []; |
102
|
|
|
|
103
|
|
|
$variables = $this->config; |
104
|
|
|
$variables['primary_domain'] = strval($vhost->getPrimaryDomain()); |
105
|
|
|
$variables['secondary_domains'] = implode(', ', array_map('strval', $vhost->getSecondaryDomains())); |
106
|
|
|
$variables['secondary_domains_without_wildcards'] = |
107
|
|
|
implode(', ', array_map('strval', $vhost->getSecondaryDomains(true))); |
108
|
|
|
$variables['docroot'] = |
109
|
|
|
rtrim($this->config['dataroot'].DIRECTORY_SEPARATOR.$vhost->getDocroot(), DIRECTORY_SEPARATOR); |
110
|
|
|
$variables['configroot'] = $this->config['vhostroot'].DIRECTORY_SEPARATOR.$variables['primary_domain']; |
111
|
|
|
$variables['php_version'] = $vhost->getPhp(); |
112
|
|
|
|
113
|
|
|
// If the virtual host should redirect |
114
|
|
|
if ($vhost->getRedirectUrl() !== null) { |
115
|
|
|
// TODO: Redirect |
116
|
|
|
|
117
|
|
|
return $files; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
// If the virtual host should support PHP |
121
|
|
|
if ($vhost->getPhp() !== null) { |
122
|
|
|
$this->createPhpConfig($vhost, $files, $variables); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
// Add the virtual host include |
126
|
|
|
$this->addEntry($files, 'apache_vhost.include', |
127
|
|
|
TemplateService::render('apache_vhost.include', $variables)); |
128
|
|
|
|
129
|
|
|
// If the HTTPS protocol is supported |
130
|
|
|
if ($httpsport = $vhost->getPort(Vhost::PROTOCOL_HTTPS)) { |
131
|
|
|
$certbotService = CertbotServiceFactory::create(); |
132
|
|
|
$primaryDomainIsCertified = $certbotService->isCertified($variables['primary_domain']); |
133
|
|
|
|
134
|
|
|
// Add the SSL configuration include |
135
|
|
|
$this->addEntry( |
136
|
|
|
$files, |
137
|
|
|
'apache_ssl.include', |
138
|
|
|
TemplateService::render('apache_ssl.include', $variables, !$primaryDomainIsCertified) |
139
|
|
|
); |
140
|
|
|
|
141
|
|
|
// Add the HTTPS vhost declaration |
142
|
|
|
$variables['port'] = $httpsport; |
143
|
|
|
$variables['ssl'] = true; |
144
|
|
|
$this->addEntry($files, 'apache_vhost.conf', |
145
|
|
|
TemplateService::render('apache_vhost.conf', $variables)); |
146
|
|
|
|
147
|
|
|
// Add the Certbot configuration |
148
|
|
|
$this->addEntry($files, 'certbot.ini', |
149
|
|
|
TemplateService::render('certbot.ini', $variables)); |
150
|
|
|
|
151
|
|
|
// Output a hint if the primary domain isn't certified |
152
|
|
|
if (!$primaryDomainIsCertified) { |
153
|
|
|
App::addMessage( |
154
|
|
|
sprintf( |
155
|
|
|
'The virtual host primary domain "%s" should be properly certified!', |
156
|
|
|
$variables['primary_domain'] |
157
|
|
|
), |
158
|
|
|
LogLevel::NOTICE |
159
|
|
|
); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
// If the HTTP protocol is supported |
164
|
|
|
if ($httpport = $vhost->getPort(Vhost::PROTOCOL_HTTP)) { |
|
|
|
|
165
|
|
|
|
166
|
|
|
// Add the HTTP vhost declaration |
167
|
|
|
$variables['port'] = $httpport; |
168
|
|
|
$variables['ssl'] = false; |
169
|
|
|
$this->addEntry($files, 'apache_vhost.conf', |
170
|
|
|
TemplateService::render('apache_vhost.conf', $variables)); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
$absoluteFiles = []; |
174
|
|
|
foreach ($files as $relativeFile => $fileContent) { |
175
|
|
|
$absoluteFiles[$this->helper->vhostDirectory($vhost).DIRECTORY_SEPARATOR.$relativeFile] = |
176
|
|
|
implode(PHP_EOL, (array)$fileContent); |
177
|
|
|
} |
178
|
|
|
return $absoluteFiles; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Create a PHP configuration include |
183
|
|
|
* |
184
|
|
|
* @param VhostInterface $vhost Virtual host |
185
|
|
|
* @param array $files Configuration files |
186
|
|
|
* @param array $variables Templating variables |
187
|
|
|
*/ |
188
|
|
|
protected function createPhpConfig(VhostInterface $vhost, array &$files, array $variables) |
189
|
|
|
{ |
190
|
|
|
// Add the FPM configuration |
191
|
|
|
$this->addEntry($files, 'fpm-'.$vhost->getPhp().'.conf', TemplateService::render('fpm.conf', $variables)); |
192
|
|
|
|
193
|
|
|
// Add the FPM include |
194
|
|
|
$this->addEntry($files, 'apache_fmp.include', |
195
|
|
|
TemplateService::render('apache_fpm.include', $variables)); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Add a file entry |
200
|
|
|
* |
201
|
|
|
* @param array $files Files |
202
|
|
|
* @param string $name File name |
203
|
|
|
* @param string $entry entry |
204
|
|
|
*/ |
205
|
|
|
protected function addEntry(&$files, $name, $entry) |
206
|
|
|
{ |
207
|
|
|
if (empty($files[$name])) { |
208
|
|
|
$files[$name] = []; |
209
|
|
|
} |
210
|
|
|
$files[$name][] = $entry; |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
|