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