forked from hestiacp/hestiacp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstaller.php
More file actions
100 lines (81 loc) · 3.03 KB
/
Copy pathinstaller.php
File metadata and controls
100 lines (81 loc) · 3.03 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
<?php
// declare(strict_types=1);
require_once("Hestia.php");
class AppInstaller {
private $domain;
private $appsetup;
private $appcontext;
private $formNamespace = 'webapp';
private $errors;
private $database_config = [
'database_create' => ['type'=>'boolean', 'value'=>false],
'database_name' => 'text',
'database_user' => 'text',
'database_password' => 'password',
];
public function __construct(string $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]");
}
$appclass = ucfirst($app).'Setup';
if(file_exists('installer/' . $appclass.".php"))
require_once('installer/' . $appclass.".php");
if (class_exists($appclass)) {
$this->appsetup = new $appclass($domain, $this->appcontext);
}
if (!$this->appsetup) {
throw new Exception( "Application [".ucfirst($app)."] does not have a installer" );
}
}
public function getStatus() {
return $this->errors;
}
public function formNs() {
return $this->formNamespace;
}
public function getOptions() {
if(!$this->appsetup) return;
$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) {
if (!$this->appsetup) return;
$options = $this->filterOptions($options);
$random_num = 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);
}
}
}
// TO DO : create a WebDomain model class, hidrate from v-list-web-domain(json)