-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdirectadmin.inc.php
475 lines (458 loc) · 16.1 KB
/
directadmin.inc.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
<?php
/**
* DirectAdmin Related Functionality
* @author Joe Huss <detain@interserver.net>
* @copyright 2025
* @package MyAdmin
* @category Licenses
*/
function get_directadmin_license_types()
{
return [
'ES 5.0' => 'CentOS 5 32-bit',
'ES 5.0 64' => 'CentOS 5 64-bit',
'ES 6.0' => 'CentOS 6 32-bit',
'ES 6.0 64' => 'CentOS 6 64-bit',
'ES 7.0 64' => 'CentOS 7 64-bit',
'ES 8.0 64' => 'CentOS 8 64-bit',
'FreeBSD 8.0 64' => 'FreeBSD 8.x 64-bit',
'FreeBSD 9.1 32' => 'FreeBSD 9.x 32-bit',
'FreeBSD 9.0 64' => 'FreeBSD 9.x 64-bit',
'Debian 5' => 'Debian 5.0 32-bit',
'Debian 5 64' => 'Debian 5.0 64-bit',
'Debian 6' => 'Debian 6.0 32-bit',
'Debian 6 64' => 'Debian 6.0 64-bit',
'Debian 7' => 'Debian 7.0 32-bit',
'Debian 7 64' => 'Debian 7.0 64-bit',
'Debian 8 64' => 'Debian 8.0 64-bit'
];
}
/**
* @param string $module
* @param $packageId
* @param bool $order
* @param bool|array $extra
* @return bool|string
*/
function directadmin_get_best_type($module, $packageId, $order = false, $extra = false)
{
$types = get_directadmin_license_types();
$db = get_module_db($module);
$found = false;
$parts = [];
$settings = \get_module_settings($module);
$db->query("select * from services where services_id={$packageId}");
if ($db->next_record(MYSQL_ASSOC)) {
if ($module == 'licenses') {
return $db->Record['services_field1'];
}
$service = $db->Record;
if ($db->Record['services_field1'] != 'slice') {
$parts = explode(' ', $db->Record['services_name']);
$parts[3] = trim(str_replace(['-', 'bit'], ['', ''], $parts[3]));
}
if ($extra === false) {
$extra = ['os' => '', 'version' => ''];
}
}
if (!isset($order[$settings['PREFIX'].'_os']) || $order[$settings['PREFIX'].'_os'] == '') {
if (in_array($service['services_type'], [get_service_define('KVM_LINUX'), get_service_define('CLOUD_KVM_LINUX')])) {
$order[$settings['PREFIX'].'_os'] = 'centos5';
} elseif (in_array($service['services_type'], [get_service_define('OPENVZ'), get_service_define('SSD_OPENVZ')])) {
$db->query("select * from {$settings['PREFIX']}_masters where {$settings['PREFIX']}_id={$order[$settings['PREFIX'].'_server']}");
$db->next_record(MYSQL_ASSOC);
if ($db->Record[$settings['PREFIX'].'_bits'] == 32) {
$order[$settings['PREFIX'].'_os'] = 'centos-6-x86.tar.gz';
} else {
$order[$settings['PREFIX'].'_os'] = 'centos-6-x86_64.tar.gz';
}
}
}
if (isset($order[$settings['PREFIX'].'_os'])) {
$db->query("select * from vps_templates where template_file='".$db->real_escape($order[$settings['PREFIX'].'_os'])."' limit 1", __LINE__, __FILE__);
if ($db->num_rows() > 0) {
$db->next_record(MYSQL_ASSOC);
$found = true;
$parts = [$db->Record['template_os'], $db->Record['template_version'], $db->Record['template_bits']];
}
}
if (in_array(strtolower($parts[2]), ['i386', 'i586', 'x86'])) {
$parts[2] = 32;
} elseif (in_array(strtolower($parts[2]), ['amd64', 'x86-64'])) {
$parts[2] = 64;
}
if (in_array(strtolower($db->Record['template_os']), ['debian'])) {
$parts[0] = 'Debian';
} elseif (isset($db->Record['template_os']) && in_array(strtolower($db->Record['template_os']), ['ubuntu'])) {
$parts[0] = 'Debian';
$parts[1] = '8';
} elseif (isset($db->Record['template_os']) && in_array(strtolower($db->Record['template_os']), ['freebsd', 'openbsd'])) {
$parts[0] = 'FreeBSD';
} elseif (isset($db->Record['template_os']) && in_array(strtolower($db->Record['template_os']), ['centos', 'fedora', 'rhel', 'redhat'])) {
$parts[0] = 'ES';
} elseif (isset($db->Record['template_os'])) {
$parts[0] = $db->Record['template_os'];
}
if (strtolower($parts[0]) == 'FreeBSD') {
if ($parts[3] == 32) {
$parts[2] = '9.1';
} elseif (mb_substr($parts[2], 0, 1) == 8) {
$parts[2] = '8.0';
} elseif (mb_substr($parts[2], 0, 1) == 9) {
$parts[2] = '9.0';
}
} elseif (!isset($parts[3]) || $parts[3] == 32) {
$parts[3] = '';
}
if ($parts[0] == 'ES') {
$parts[1] = mb_substr($parts[1], 0, 1).'.0';
} else {
$parts[1] = mb_substr($parts[1], 0, 1);
}
$daType = trim("{$parts[0]} {$parts[1]} {$parts[2]}");
if (isset($types[$daType])) {
myadmin_log('licenses', 'info', "Matched DA Type for $types[$daType] to {$daType}", __LINE__, __FILE__);
return $daType;
} else {
myadmin_log('licenses', 'info', "Couldn't find matching da type from os {$daType} fakkubg back go ES 8.0 64", __LINE__, __FILE__);
return "ES 8.0 64";
}
return false;
}
/**
* @param string $page
* @param string $post
* @param bool|string[] $options
* @return string
*/
function directadmin_req($page, $post = '', $options = false)
{
require_once __DIR__.'/../../../workerman/statistics/Applications/Statistics/Clients/StatisticClient.php';
if ($options === false) {
$options = [];
}
$defaultOptions = [
CURLOPT_USERPWD => DIRECTADMIN_USERNAME.':'.DIRECTADMIN_PASSWORD,
CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_SSL_VERIFYPEER => false
];
foreach ($defaultOptions as $key => $value) {
if (!isset($options[$key])) {
$options[$key] = $value;
}
}
if (!is_url($page)) {
if (mb_strpos($page, '.php') === false) {
$page .= '.php';
}
if (mb_strpos($page, '/') === false) {
$page = "clients/api/{$page}";
} elseif (mb_strpos($page, 'api/') === false) {
$page = "api/{$page}";
}
if (mb_strpos($page, 'clients/') === false) {
$page == "clients/{$page}";
}
if (!is_url($page)) {
$page = "https://www.directadmin.com/{$page}";
}
}
$call = basename(parse_url($page)['path'], '.php');
\StatisticClient::tick('DirectAdmin', $call);
$response = getcurlpage($page, $post, $options);
if ($response === false) {
\StatisticClient::report('DirectAdmin', $call, false, 1, 'Curl Error', STATISTICS_SERVER);
} else {
\StatisticClient::report('DirectAdmin', $call, true, 0, '', STATISTICS_SERVER);
}
return trim($response);
}
/**
* @return array
*/
function get_directadmin_licenses()
{
$response = directadmin_req('list');
$licenses = [];
if (trim($response) == '') {
return $licenses;
}
$lines = explode("\n", trim($response));
$linesValues = array_values($lines);
foreach ($linesValues as $line) {
parse_str($line, $license);
if (isset($license['lid'])) {
$licenses[$license['lid']] = $license;
}
}
return $licenses;
}
/**
* @param $lid
* @return string
*/
function get_directadmin_license($lid)
{
$response = directadmin_req('license', ['lid' => $lid]);
_debug_array($response);
return $response;
}
/**
* @param $ipAddress
* @return bool|mixed
*/
function get_directadmin_license_by_ip($ipAddress)
{
$licenses = get_directadmin_licenses();
$licensesValues = array_values($licenses);
foreach ($licensesValues as $license) {
if (isset($license['ip']) && $license['ip'] == $ipAddress) {
return $license;
}
}
return false;
}
/**
* @param $ipAddress
* @return bool
*/
function directadmin_ip_to_lid($ipAddress)
{
$license = get_directadmin_license_by_ip($ipAddress);
if ($license === false) {
return false;
} else {
return $license['lid'];
}
}
/**
* activate_directadmin()
*
* @param $ipAddress
* @param boolean|string $ostype
* @param $pass
* @param $email
* @param string $name
* @param string $domain
* @param false|int $custid optional customer id or null for none
* @return string|false license id
*/
function activate_directadmin($ipAddress, $ostype, $pass, $email, $name, $domain = '', $custid = null)
{
myadmin_log('licenses', 'info', "Called activate_directadmin({$ipAddress}, {$ostype}, {$pass}, {$email}, {$name}, {$domain})", __LINE__, __FILE__);
$settings = \get_module_settings('licenses');
$license = get_directadmin_license_by_ip($ipAddress);
if ($license === false) {
$options = [
CURLOPT_REFERER => 'https://www.directadmin.com/clients/createlicense.php'
];
if (strpos($ostype, ',') !== false) {
[$pid, $os] = explode(',', $ostype);
$ostype = $os;
} else {
$pid = 2712;
}
$post = [
'uid' => DIRECTADMIN_USERNAME,
'id' => DIRECTADMIN_USERNAME,
'password' => DIRECTADMIN_PASSWORD,
'api' => 1,
'name' => $name,
'pid' => $pid,
'os' => $ostype,
'domain' => $domain != '' ? $domain : $ipAddress,
'payment' => 'balance',
'ip' => $ipAddress,
'pass1' => $pass,
'pass2' => $pass,
'username' => 'admin',
'email' => $email,
'admin_pass1' => $pass,
'admin_pass2' => $pass,
'ns1' => 'dns4.interserver.net',
'ns2' => 'dns5.interserver.net',
'ns_on_server' => 'yes',
'ns1ip' => '66.45.228.78',
'ns2ip' => '66.45.228.3'
];
$url = 'https://www.directadmin.com/clients/api/createlicense.php';
$response = directadmin_req($url, $post, $options);
request_log('licenses', $GLOBALS['tf']->session->account_id, __FUNCTION__, 'directadmin', 'createlicense', $post, $response);
myadmin_log('licenses', 'info', $response, __LINE__, __FILE__);
$matches = preg_split('/error=0&text=License Created&lid=/', $response);
if (empty($matches) || !isset($matches[1]) || $matches[1] == '') {
return false;
}
$lid = urldecode($matches[1]);
$response = directadmin_makepayment($lid);
request_log('licenses', $GLOBALS['tf']->session->account_id, __FUNCTION__, 'directadmin', 'makepayment', $lid, $response);
myadmin_log('licenses', 'info', $response, __LINE__, __FILE__);
$GLOBALS['tf']->history->add($settings['TABLE'], 'add_directadmin', 'ip', $ipAddress, $custid);
return $lid;
}
return $license['lid'];
}
/**
* deactivate_directadmin()
* @param mixed $ipAddress
* @return string|null
*/
function deactivate_directadmin($ipAddress)
{
$module = 'licenses';
$response = get_directadmin_licenses();
foreach ($response as $idx => $data) {
if ($data['ip'] == $ipAddress) {
$license = $data;
}
}
if (!isset($license)) {
$license = get_directadmin_license_by_ip($ipAddress);
}
if (is_array($license) && $license['active'] == 'Y') {
$url = 'https://www.directadmin.com/cgi-bin/deletelicense';
$post = [
'uid' => DIRECTADMIN_USERNAME,
'password' => DIRECTADMIN_PASSWORD,
'api' => 1,
'lid' => $license['lid']
];
$options = [
//CURLOPT_REFERER => 'https://www.directadmin.com/clients/license.php',
CURLOPT_REFERER => 'https://www.directadmin.com/clients/license.php?lid='.$license['lid']
];
$response = directadmin_req($url, $post, $options);
myadmin_log('licenses', 'info', $response, __LINE__, __FILE__);
request_log($module, $GLOBALS['tf']->session->account_id, __FUNCTION__, 'directadmin', 'deactivateLicense', $post, $response);
$deActdLicense = get_directadmin_license_by_ip($ipAddress);
$bodyRows = [];
if ($deActdLicense !== false && $deActdLicense['active'] == 'Y') {
$bodyRows[] = 'DirectAdmin license IP: '.$ipAddress.' unable to cancel.';
$bodyRows[] = 'Deactivation Response: .'.json_encode($response);
$subject = 'License Deactivation Issue IP: '.$ipAddress;
$smartyE = new TFSmarty();
$smartyE->assign('h1', 'License Deactivation');
$smartyE->assign('body_rows', $bodyRows);
$msg = $smartyE->fetch('email/client/client_email.tpl');
(new \MyAdmin\Mail())->adminMail($subject, $msg, false, 'client/client_email.tpl');
}
return true;
}
}
/**
* @param $ipAddress
* @return null|string
*/
function directadmin_deactivate($ipAddress)
{
return deactivate_directadmin($ipAddress);
}
/**
* @param string $lid
* @return string
*/
function directadmin_makepayment($lid)
{
$url = 'https://www.directadmin.com/cgi-bin/makepayment';
$referrer = 'https://www.directadmin.com/clients/makepayment.php';
$post = [
'uid' => DIRECTADMIN_USERNAME,
'id' => DIRECTADMIN_USERNAME,
'password' => DIRECTADMIN_PASSWORD,
'api' => 1,
'action' => 'pay',
'lid' => $lid
];
$options = [
CURLOPT_REFERER => $referrer
];
$response = directadmin_req($url, $post, $options);
myadmin_log('licenses', 'info', $response, __LINE__, __FILE__, 'licenses');
return $response;
}
function directadmin_get_os_list($active = '')
{
$url = 'https://www.directadmin.com/clients/api/os_list.php';
$post = [
'uid' => DIRECTADMIN_USERNAME,
'id' => DIRECTADMIN_USERNAME,
'password' => DIRECTADMIN_PASSWORD,
'api' => 1,
];
$response = directadmin_req($url, $post);
myadmin_log('licenses', 'info', $response, __LINE__, __FILE__, 'licenses');
return $response;
}
function directadmin_get_products()
{
$url = 'https://www.directadmin.com/clients/api/products.php';
$post = [
'uid' => DIRECTADMIN_USERNAME,
'id' => DIRECTADMIN_USERNAME,
'password' => DIRECTADMIN_PASSWORD,
'api' => 1,
];
$response = directadmin_req($url, $post);
myadmin_log('licenses', 'info', $response, __LINE__, __FILE__, 'licenses');
return $response;
}
function activate_free_license($ipAddress, $type, $email, $hostname = '')
{
myadmin_log('licenses', 'info', "Called activate_free_license($ipAddress, $type, $email, $hostname)", __LINE__, __FILE__);
function_requirements('class.FreeDirectAdmin');
$daObj = new FreeDirectAdmin('licenses', $type, false, false);
$license_key = $daObj->create($ipAddress, $email, 'vps - '.$ipAddress, $hostname);
return $license_key;
}
/**
* Delete a license
*
* @param int $lid DA portal License ID
*
* @return array/boolean $response
*/
function delete_free_license($lid, $type)
{
myadmin_log('licenses', 'info', "Called delete_free_license($lid)", __LINE__, __FILE__);
function_requirements('class.FreeDirectAdmin');
$daObj = new FreeDirectAdmin('licenses', $type, false, false);
$license_key = $daObj->delete($lid);
return $license_key;
}
/**
* Modify license OS
*
* @param int $lid DA portal License ID
* @param string $os Operating System
*
* @return array/boolean $response
*/
function directadmin_modify_os($lid, $os)
{
if ($lid) {
$licenses = get_directadmin_licenses();
$os_string = directadmin_get_os_list();
$os_arr = explode(PHP_EOL, trim($os_string));
foreach ($os_arr as $row) {
$os_exp = explode('=', $row);
$os_list[$os_exp[0]] = $os_exp[1];
}
if (isset($licenses[$lid])) { //validate license id
if (in_array($os, array_keys($os_list))) {
$url = 'https://www.directadmin.com/clients/api/special.php?lid='.$lid;
$post = [
'saveos' => 'OS Updated',
'os' => $os
];
$response = directadmin_req($url, $post);
return $response;
} else {
myadmin_log('licenses', 'info', 'Invalid OS is passed.', __LINE__, __FILE__, 'licenses');
}
} else {
myadmin_log('licenses', 'info', 'Invalid DA License Id passed.', __LINE__, __FILE__, 'licenses');
}
}
return false;
}