-
Notifications
You must be signed in to change notification settings - Fork 0
/
HestiaCP.php
170 lines (146 loc) · 4.69 KB
/
HestiaCP.php
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
<?php
namespace App\Extensions\Servers\HestiaCP;
use App\Classes\Extensions\Server;
use App\Helpers\ExtensionHelper;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Str;
class HestiaCP extends Server
{
public function getMetadata()
{
return [
'display_name' => 'HestiaCP',
'version' => '0.0.1',
'author' => 'HestiaCP Team',
'website' => 'https://hestiacp.com',
];
}
private function request($data = [])
{
$host = rtrim(ExtensionHelper::getConfig('HestiaCP', 'host'), '/');
$port = rtrim(ExtensionHelper::getConfig('HestiaCP', 'port'), '/');
$accesskey = ExtensionHelper::getConfig('HestiaCP', 'accesskey');
$secretkey = ExtensionHelper::getConfig('HestiaCP', 'secretkey');
$data['hash'] = $accesskey.':'.$secretkey;
$response = Http::post($host . ':'. $port . '/api/', $data);
if ($response->failed()) {
dd($response->body(), $response->status());
throw new \Exception('Error while requesting API');
}
return $response;
}
public function getConfig()
{
return [
[
'name' => 'host',
'type' => 'text',
'friendlyName' => 'Hostname',
'validation' => 'url:http,https',
'required' => true,
],
[
'name' => 'port',
'type' => 'text',
'friendlyName' => 'Port',
'validation' => 'numeric',
'required' => true,
],
[
'name' => 'accesskey',
'type' => 'text',
'friendlyName' => 'Access Key',
'required' => true,
],
[
'name' => 'secretkey',
'type' => 'text',
'friendlyName' => 'Secret key',
'required' => true,
],
];
}
public function getProductConfig($options)
{
// Get all the packages
$response = $this->request(['cmd' => 'v-list-user-packages', 'arg1' => 'json']);
$packages = $response->json();
$packageOptions = [];
foreach ($packages as $package => $options) {
$packageOptions[] = [
'value' => $package,
'name' => $package,
];
}
return [
[
'name' => 'package',
'type' => 'dropdown',
'friendlyName' => 'Package',
'options' => $packageOptions,
'required' => true,
],
];
}
public function getUserConfig()
{
return [
[
'name' => 'domain',
'type' => 'text',
'validation' => 'domain',
'friendlyName' => 'Domain',
'required' => true,
]
];
}
public function createServer($user, $params, $order, $orderProduct, $configurableOptions)
{
$username = strtolower(Str::random());
$password = Str::random();
// If first one is a number, add a letter
if (is_numeric($username[0])) {
$username = 'a' . substr($username, 1);
}
$this->request([
'cmd' => 'v-add-user',
'arg1' => $username,
'arg2' => $password,
'arg3' => $user->email,
'arg4' => $params['package'],
'arg5' => $user -> name,
]);
$this -> request([
'cmd' => 'v-add-domain',
'arg1' => $username,
'arg2' => $params['config']['domain'],
]);
ExtensionHelper::setOrderProductConfig('username', strtolower($username), $orderProduct->id);
ExtensionHelper::setOrderProductConfig('password', $password, $orderProduct->id);
return true;
}
public function suspendServer($user, $params, $order, $orderProduct, $configurableOptions)
{
$this->request([
'cmd' => 'v-suspend-user',
'arg1' => $params['config']['username'],
]);
return true;
}
public function unsuspendServer($user, $params, $order, $orderProduct, $configurableOptions)
{
$this->request([
'cmd' => 'v-unsuspend-user',
'arg1' => $params['config']['username'],
]);
return true;
}
public function terminateServer($user, $params, $order, $orderProduct, $configurableOptions)
{
$this->request([
'cmd' => 'v-delete-user',
'arg1' => $params['config']['username'],
]);
return true;
}
}