forked from hestiacp/hestiacp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppWizard.php
More file actions
189 lines (157 loc) · 5.49 KB
/
AppWizard.php
File metadata and controls
189 lines (157 loc) · 5.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php
declare(strict_types=1);
namespace Hestia\WebApp;
use Hestia\System\HestiaApp;
use Hestia\WebApp\InstallationTarget\InstallationTarget;
use Hestia\WebApp\InstallationTarget\TargetDatabase;
use Hestia\WebApp\InstallationTarget\TargetDomain;
use RuntimeException;
use function array_filter;
use function bin2hex;
use function in_array;
use function max;
use function str_replace;
use function str_starts_with;
use function strtolower;
class AppWizard
{
public function __construct(
private readonly InstallerInterface $installer,
private readonly string $domain,
private readonly HestiaApp $appcontext,
) {
if (!$appcontext->userOwnsDomain($domain)) {
throw new RuntimeException('User does not have access to domain [$domain]');
}
}
public function isDomainRootClean(): bool
{
$installationTarget = $this->getInstallationTarget($this->domain);
$files = $this->appcontext->listFiles($installationTarget->getDocRoot());
$filteredFiles = array_filter(
$files,
fn(string $file) => !in_array($file, ['index.html', 'robots.txt']),
);
return count($filteredFiles) <= 0;
}
public function formNamespace(): string
{
return 'webapp_';
}
/**
* @return mixed[]
*/
public function getOptions(): array
{
$form = $this->installer->getConfig('form');
$info = $this->installer->getInfo();
$form = array_merge($form, [
'php_version' => [
'type' => 'select',
'value' => (string) max($info->supportedPHPVersions),
'options' => $info->supportedPHPVersions,
],
]);
if ($this->installer->getConfig('database') === true) {
$databaseName = $this->generateDatabaseName();
$databaseOptions = [
'database_create' => [
'type' => 'boolean',
'value' => true,
],
'database_host' => [
'type' => 'select',
'options' => $this->appcontext->getDatabaseHosts('mysql'),
],
'database_name' => [
'type' => 'text',
'value' => $databaseName,
],
'database_user' => [
'type' => 'text',
'value' => $databaseName,
],
'database_password' => [
'type' => 'password',
'placeholder' => 'auto',
],
];
$form = array_merge($form, $databaseOptions);
}
return $form;
}
public function applicationName(): string
{
return $this->installer->getInfo()->name;
}
/**
* @param mixed[] $options
* @return mixed[]
*/
public function filterOptions(array $options): array
{
$filteredOptions = [];
foreach ($options as $key => $value) {
if (!str_starts_with($key, $this->formNamespace())) {
continue;
}
$filteredOptions[str_replace($this->formNamespace(), '', $key)] = $value;
}
return $filteredOptions;
}
public function execute(array $options): void
{
$target = $this->getInstallationTarget($this->domain);
$options = $this->filterOptions($options);
if ($this->installer->getConfig('database') === true) {
if (empty($options['database_name'])) {
$options['database_name'] = $this->generateDatabaseName();
}
if (empty($options['database_user'])) {
$options['database_user'] = $this->generateDatabaseName();
}
if (empty($options['database_password'])) {
$options['database_password'] = bin2hex(random_bytes(10));
}
$target->addTargetDatabase(
new TargetDatabase(
$options['database_host'],
$this->appcontext->user() . '_' . $options['database_name'],
$this->appcontext->user() . '_' . $options['database_user'],
$options['database_password'],
!empty($options['database_create']),
),
);
}
$this->installer->install($target, $options);
}
private function getInstallationTarget(string $domainName): InstallationTarget
{
$webDomain = $this->appcontext->getWebDomain($domainName);
if (empty($webDomain->domainPath) || !is_dir($webDomain->domainPath)) {
throw new RuntimeException(
sprintf(
'Web domain path "%s" not found for domain "%s"',
$webDomain->domainPath,
$webDomain->domainName,
),
);
}
return new InstallationTarget(
new TargetDomain(
$webDomain->domainName,
$webDomain->domainPath,
$webDomain->ipAddress,
$webDomain->isSslEnabled,
),
TargetDatabase::noDatabase(),
);
}
private function generateDatabaseName(): string
{
// Make the database and user easy to recognise but hard to guess
$safeAppName = str_replace(' ', '_', strtolower($this->applicationName()));
$randomString = bin2hex(random_bytes(5));
return $safeAppName . '_' . $randomString;
}
}