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
115 lines (93 loc) · 3.19 KB
/
AppWizard.php
File metadata and controls
115 lines (93 loc) · 3.19 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
<?php
declare(strict_types=1);
namespace Hestia\WebApp;
use Hestia\System\HestiaApp;
class AppWizard {
private $domain;
private $appsetup;
private $appcontext;
private $formNamespace = 'webapp';
private $errors;
private $database_config = [
'database_create' => ['type'=>'boolean', 'value'=>false],
'database_name' => ['type'=>'text', 'placeholder' => 'auto'],
'database_user' => ['type'=>'text', 'placeholder' => 'auto'],
'database_password' => ['type'=>'password', 'placeholder' => 'auto'],
];
public function __construct(InstallerInterface $app, string $domain, HestiaApp $context)
{
$this->domain = $domain;
$this->appcontext = $context;
if (!$this->appcontext->userOwnsDomain($domain)) {
throw new \Exception("User does not have access to domain [$domain]");
}
$this->appsetup = $app;
}
public function getStatus()
{
return $this->errors;
}
public function isDomainRootClean()
{
$this->appcontext->runUser('v-run-cli-cmd', [ "ls", $this->appsetup->getDocRoot() ], $status);
if($status->code !== 0) {
throw new \Exception("Cannot list domain files");
}
$files = $status->raw;
if( count($files) > 2) {
return false;
}
foreach($files as $file) {
if ( !in_array($file,['index.html','robots.txt']) ) {
return false;
}
}
return true;
}
public function formNs()
{
return $this->formNamespace;
}
public function getOptions()
{
$options = $this->appsetup->getOptions();
if ($this->appsetup->withDatabase()) {
$options = array_merge($options, $this->database_config);
}
return $options;
}
public function filterOptions(array $options)
{
$filteredoptions = [];
array_walk($options, function($value, $key) use(&$filteredoptions) {
if (strpos($key, $this->formNs().'_')===0) {
$option = str_replace($this->formNs().'_','',$key);
$filteredoptions[$option] = $value;
}
});
return $filteredoptions;
}
public function execute(array $options)
{
$options = $this->filterOptions($options);
$random_num = (string)random_int(10000, 99999);
if ($this->appsetup->withDatabase() && !empty($options['database_create'])) {
if(empty($options['database_name'])) {
$options['database_name'] = $random_num;
}
if(empty($options['database_user'])) {
$options['database_user'] = $random_num;
}
if(empty($options['database_password'])) {
$options['database_password'] = bin2hex(random_bytes(10));
}
if(!$this->appcontext->databaseAdd($options['database_name'], $options['database_user'], $options['database_password'])) {
$this->errors[] = "Error adding database";
return false;
}
}
if(empty($this->errors)) {
return $this->appsetup->install($options);
}
}
}