forked from hestiacp/hestiacp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHestiaApp.php
More file actions
194 lines (156 loc) · 5.88 KB
/
HestiaApp.php
File metadata and controls
194 lines (156 loc) · 5.88 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
190
191
192
193
194
<?php
declare(strict_types=1);
namespace Hestia\System;
class HestiaApp {
protected const TMPDIR_DOWNLOADS="/tmp/hestia-webapp";
public function __construct() {
@mkdir(self::TMPDIR_DOWNLOADS);
}
public function run(string $cmd, $args, &$cmd_result=null) : bool
{
$cli_script = HESTIA_CMD . '/' . basename($cmd);
$cli_arguments = '';
if (!empty($args) && is_array($args)) {
foreach ($args as $arg) {
$cli_arguments .= escapeshellarg((string)$arg) . ' ';
}
} else {
$cli_arguments = escapeshellarg($args);
}
exec ($cli_script . ' ' . $cli_arguments . ' 2>&1', $output, $exit_code);
$result['code'] = $exit_code;
$result['args'] = $cli_arguments;
$result['raw'] = $output;
$result['text'] = implode( PHP_EOL, $output);
$result['json'] = json_decode($result['text'], true);
$cmd_result = (object)$result;
return ($exit_code === 0);
}
public function runUser(string $cmd, $args, &$cmd_result=null) : bool
{
if (!empty($args) && is_array($args)) {
array_unshift($args, $this->user());
} else {
$args = [$this->user(), $args];
}
return $this->run($cmd, $args, $cmd_result);
}
public function installComposer()
{
exec("curl https://composer.github.io/installer.sig", $output);
$signature = implode(PHP_EOL, $output);
if (empty($signature)) {
throw new \Exception("Error reading composer signature");
}
$composer_setup = self::TMPDIR_DOWNLOADS . DIRECTORY_SEPARATOR . 'composer-setup-' . $signature . '.php';
exec("wget https://getcomposer.org/installer --quiet -O " . escapeshellarg($composer_setup), $output, $return_code);
if ($return_code !== 0 ) {
throw new \Exception("Error downloading composer");
}
if ($signature !== hash_file('sha384', $composer_setup)) {
unlink($composer_setup);
throw new \Exception("Invalid composer signature");
}
$install_folder = $this->getUserHomeDir() . DIRECTORY_SEPARATOR . '.composer';
$this->runUser('v-run-cli-cmd', ["/usr/bin/php", $composer_setup, "--1", "--quiet", "--install-dir=".$install_folder, "--filename=composer" ], $status);
unlink($composer_setup);
if ($status->code !== 0 ) {
throw new \Exception("Error installing composer");
}
}
public function runComposer($args, &$cmd_result=null) : bool
{
$composer = $this->getUserHomeDir() . DIRECTORY_SEPARATOR . '.composer' . DIRECTORY_SEPARATOR . 'composer';
if(!is_file($composer)) {
$this->installComposer();
}
if (!empty($args) && is_array($args)) {
array_unshift($args, 'composer');
} else {
$args = ['composer', $args];
}
return $this->runUser('v-run-cli-cmd', $args, $cmd_result);
}
// Logged in user
public function realuser() : string
{
return $_SESSION['user'];
}
// Effective user
public function user() : string
{
$user = $this->realuser();
if ($_SESSION['userContext'] === 'admin' && !empty($_SESSION['look'])) {
$user = $_SESSION['look'];
}
if(strpos($user, DIRECTORY_SEPARATOR) !== false) {
throw new \Exception("illegal characters in username");
}
return $user;
}
public function getUserHomeDir()
{
$info = posix_getpwnam($this->user());
return $info['dir'];
}
public function userOwnsDomain(string $domain) : bool
{
return $this->runUser('v-list-web-domain', [$domain, 'json']);
}
public function databaseAdd(string $dbname, string $dbuser, string $dbpass)
{
$v_password = tempnam("/tmp","hst");
$fp = fopen($v_password, "w");
fwrite($fp, $dbpass."\n");
fclose($fp);
$status = $this->runUser('v-add-database', [$dbname, $dbuser, $v_password]);
unlink($v_password);
return $status;
}
public function getWebDomainIp(string $domain)
{
$this->runUser('v-list-web-domain', [$domain, 'json'], $result);
$ip = $result->json[$domain]['IP'];
return filter_var($ip, FILTER_VALIDATE_IP);
}
public function getWebDomainPath(string $domain)
{
return Util::join_paths( $this->getUserHomeDir() , "web", $domain);
}
public function downloadUrl(string $src, $path=null, &$result=null)
{
if (strpos($src,'http://') !== 0 &&
strpos($src,'https://')!== 0 ) {
return false;
}
exec("/usr/bin/wget --tries 3 --timeout=30 --no-dns-cache -nv " . escapeshellarg($src). " -P " . escapeshellarg(self::TMPDIR_DOWNLOADS) . ' 2>&1', $output, $return_var);
if ($return_var !== 0) {
return false;
}
if(!preg_match('/URL:\s*(.+?)\s*\[(.+?)\]\s*->\s*"(.+?)"/', implode(PHP_EOL, $output), $matches)) {
return false;
}
if(empty($matches) || count($matches) != 4) {
return false;
}
$status['url'] = $matches[1];
$status['file'] = $matches[3];
$result = (object)$status;
return true;
}
public function archiveExtract(string $src, string $path, $skip_components=null)
{
if (empty($path)) {
throw new \Exception("Error extracting archive: missing target folder");
}
if (realpath($src)) {
$archive_file = $src;
} else {
if( !$this->downloadUrl($src, null, $download_result) ) {
throw new \Exception("Error downloading archive");
}
$archive_file = $download_result->file;
}
return $this->runUser('v-extract-fs-archive', [$archive_file, $path, null, $skip_components]);
}
}