forked from clientexec/directadmin-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPluginDirectadmin.php
More file actions
528 lines (473 loc) · 20.1 KB
/
PluginDirectadmin.php
File metadata and controls
528 lines (473 loc) · 20.1 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
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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
<?php
require_once 'library/CE/NE_MailGateway.php';
require_once 'modules/admin/models/ServerPlugin.php';
/**
* @package Plugins
*/
class PluginDirectAdmin extends ServerPlugin
{
public $features = array(
'packageName' => true,
'testConnection' => true,
'showNameservers' => true,
'directlink' => true,
'upgrades' => true
);
public function getVariables()
{
$variables = [
lang('Name') => [
'type' => 'hidden',
'description' => 'Used By CE to show plugin - must match how you call the action function names',
'value' => 'DirectAdmin'
],
lang('Description') => [
'type' => 'hidden',
'description' => lang('Description viewable by admin in server settings'),
'value' => lang('DirectAdmin control panel integration')
],
lang('Username') => [
'type' => 'text',
'description' => lang('Username used to connect to server'),
'value' => ''
],
lang('Password') => [
'type' => 'password',
'description' => lang('Password used to connect to server'),
'value' => '',
'encryptable' => true
],
lang('Failure E-mail') => [
'type' => 'text',
'description' => lang('An email will be sent to this email address in case of a failure'),
'value' => ''
],
lang('Use SSL') => [
'type' => 'yesno',
'description' => '',
'value' => '1'
],
lang('Port') => [
'type' => 'text',
'description' => lang('Port used to connect to server'),
'value' => '2222'
],
lang('reseller') => [
'type' => 'hidden',
'description' => lang('Whether this server plugin can set reseller accounts'),
'value' => '1',
],
lang('Actions') => [
'type' => 'hidden',
'description' => lang('Current actions that are active for this plugin per server'),
'value' => 'Create,Delete,Update,Suspend,UnSuspend'
]
];
return $variables;
}
public function validateCredentials($args)
{
// direct admin only allows for all lowercase usernames.
$args['package']['username'] = trim(strtolower($args['package']['username']));
return $args['package']['username'];
}
public function create($args)
{
$errormsg = "";
$packageName = $args['package']['name_on_server'];
if (isset($args['package']['is_reseller']) && $args['package']['is_reseller'] == 1) {
$cmd = '/CMD_API_ACCOUNT_RESELLER';
$ip = 'shared';
} else {
$cmd = '/CMD_API_ACCOUNT_USER';
$ip = $args['package']['ip'];
}
$sock = new DA($args);
$sock->setMethod('POST');
$tArray = [
'action' => 'create',
'add' => 'Submit',
'username' => strtolower($args['package']['username']),
'email' => $args['customer']['email'],
'passwd' => $args['package']['password'],
'passwd2' => $args['package']['password'],
'domain' => strtolower($args['package']['domain_name']),
'package' => $packageName,
'ip' => $ip,
'notify' => 'no'
];
try {
$result = $sock->query($cmd, $tArray);
} catch (CE_Exception $e) {
$errorMsg = $e->getMessage();
$mailGateway = new NE_MailGateway();
$mailGateway->mailMessageEmail(
"DirectAdmin plugin: A failure occurred while connecting to the DA server. Message Returned: {$errorMsg}.",
$args['server']['variables']['plugin_directadmin_Failure_E-mail'],
"DirectAdmin Plugin",
$args['server']['variables']['plugin_directadmin_Failure_E-mail'],
"",
"[CE] DirectAdmin plugin: Connection to DA server failed"
);
throw new CE_Exception($errorMsg);
}
return;
}
public function delete($args)
{
$sock = new DA($args);
$sock->setMethod('POST');
$tArray = [
'confirmed' => 'Confirm',
'delete' => 'yes',
'select0' => strtolower($args['package']['username'])
];
try {
$result = $sock->query('/CMD_API_SELECT_USERS', $tArray);
} catch (CE_Exception $e) {
$errorMsg = $e->getMessage();
$mailGateway = new NE_MailGateway();
$mailGateway->mailMessageEmail(
"DirectAdmin plugin: A failure occurred while deleting " . $args['package']['username'] . ". Message Returned: {$errorMsg}.",
$args['server']['variables']['plugin_directadmin_Failure_E-mail'],
"DirectAdmin Plugin",
$args['server']['variables']['plugin_directadmin_Failure_E-mail'],
"",
"[CE] DirectAdmin plugin: Failure on deleting user."
);
throw new CE_Exception($errorMsg);
}
}
public function update($args)
{
$errormsg = "";
$sock = new DA($args);
$sock->setMethod('POST');
foreach ($args['changes'] as $key => $value) {
$mailGateway = new NE_MailGateway();
switch ($key) {
case 'password':
$tArray = [
'username' => strtolower($args['package']['username']),
'passwd' => $value,
'passwd2' => $value
];
$result = $sock->query('/CMD_API_USER_PASSWD', $tArray);
CE_Lib::log(4, 'DirectAdmin Password Update Result: ' . $result);
if ($result['error'] == '1') {
$mailGateway->mailMessageEmail(
"DirectAdmin plugin: A failure occurred while changing the password of " . $args['package']['username'] . ". Message Returned: {$result['msg']}.",
$args['server']['variables']['plugin_directadmin_Failure_E-mail'],
"DirectAdmin Plugin",
$args['server']['variables']['plugin_directadmin_Failure_E-mail'],
"",
"[CE] DirectAdmin plugin: Failure on changing password"
);
// Create and log the error. Then throw an error.
$errormsg = "A failure occurred while changing the password. An E-mail with details has been sent to " . $args['server']['variables']['plugin_directadmin_Failure_E-mail'] . ".";
CE_Lib::log(4, 'DirectAdmin Password Update Error: ' . $errormsg);
throw new CE_Exception($errormsg);
}
break;
case 'ip':
$tArray = [
'action' => 'ip',
'user' => strtolower($args['package']['username']),
'ip' => $value
];
$result = $sock->query('/CMD_MODIFY_USER', $tArray);
CE_Lib::log(4, 'DirectAdmin IP Update Result: ' . $result);
if ($result['error'] == '1') {
$mailGateway->mailMessageEmail(
"DirectAdmin plugin: A failure occurred while changing the IP of user " . $args['package']['username'] . ". Message Returned: {$result['msg']}.",
$args['server']['variables']['plugin_directadmin_Failure_E-mail'],
"DirectAdmin Plugin",
$args['server']['variables']['plugin_directadmin_Failure_E-mail'],
"",
"[CE] DirectAdmin plugin: Failure on changing IP"
);
// Create and log the error. Then throw an error.
$errormsg = "A failure occurred while changing the IP. An E-mail with details has been sent to " . $args['server']['variables']['plugin_directadmin_Failure_E-mail'] . ".";
CE_Lib::log(4, 'DirectAdmin IP Update Error: ' . $errormsg);
throw new CE_Exception($errormsg);
}
break;
case 'package':
$tArray = [
'action' => 'package',
'user' => strtolower($args['package']['username']),
'package' => $args['package']['name_on_server']
];
$result = $sock->query('/CMD_MODIFY_USER', $tArray);
CE_Lib::log(4, 'DirectAdmin Package Update Result: ' . $result);
if ($result['error'] == '1') {
$mailGateway->mailMessageEmail(
"DirectAdmin plugin: A failure occurred while changing the package of user " . $args['package']['username'] . ". Message Returned: {$result['msg']}.",
$args['server']['variables']['plugin_directadmin_Failure_E-mail'],
"DirectAdmin Plugin",
$args['server']['variables']['plugin_directadmin_Failure_E-mail'],
"",
"[CE] DirectAdmin plugin: Failure on changing package"
);
// Create and log the error. Then throw an error.
$errormsg = "A failure occurred while changing the package. An E-mail with details has been sent to " . $args['server']['variables']['plugin_directadmin_Failure_E-mail'] . ".";
CE_Lib::log(4, 'DirectAdmin Package Update Error: ' . $errormsg);
throw new CE_Exception($errormsg);
}
break;
}
}
return;
}
public function suspend($args)
{
$sock = new DA($args);
$sock->setMethod('POST');
$tArray = [
'select0' => strtolower($args['package']['username']),
'dosuspend' => 1,
];
try {
$result = $sock->query('/CMD_API_SELECT_USERS', $tArray);
} catch (CE_Exception $e) {
$errorMsg = $e->getMessage();
$mailGateway = new NE_MailGateway();
$mailGateway->mailMessageEmail(
"DirectAdmin plugin: A failure occurred while suspending " . $args['package']['username'] . ". Message Returned: {$errorMsg}.",
$args['server']['variables']['plugin_directadmin_Failure_E-mail'],
"DirectAdmin Plugin",
$args['server']['variables']['plugin_directadmin_Failure_E-mail'],
"",
"[CE] DirectAdmin plugin: Failure on suspending user."
);
throw new CE_Exception($errorMsg);
}
}
public function unsuspend($args)
{
$sock = new DA($args);
$sock->setMethod('POST');
$tArray = [
'select0' => strtolower($args['package']['username']),
'dounsuspend' => 1
];
try {
$result = $sock->query('/CMD_API_SELECT_USERS', $tArray);
} catch (CE_Exception $e) {
$errorMsg = $e->getMessage();
$mailGateway = new NE_MailGateway();
$mailGateway->mailMessageEmail(
"DirectAdmin plugin: A failure occurred while unsuspending " . $args['package']['username'] . ". Message Returned: {$errorMsg}.",
$args['server']['variables']['plugin_directadmin_Failure_E-mail'],
"DirectAdmin Plugin",
$args['server']['variables']['plugin_directadmin_Failure_E-mail'],
"",
"[CE] DirectAdmin plugin: Failure on suspending user."
);
throw new CE_Exception($errorMsg);
}
}
public function doCreate($args)
{
$userPackage = new UserPackage($args['userPackageId']);
$this->create($this->buildParams($userPackage));
return $userPackage->getCustomField("Domain Name") . ' has been created.';
}
public function doUpdate($args)
{
$userPackage = new UserPackage($args['userPackageId']);
$this->update($this->buildParams($userPackage, $args));
return $userPackage->getCustomField("Domain Name") . ' has been update.';
}
public function doSuspend($args)
{
$userPackage = new UserPackage($args['userPackageId']);
$this->suspend($this->buildParams($userPackage));
return $userPackage->getCustomField("Domain Name") . ' has been suspended.';
}
public function doUnSuspend($args)
{
$userPackage = new UserPackage($args['userPackageId']);
$this->unsuspend($this->buildParams($userPackage));
return $userPackage->getCustomField("Domain Name") . ' has been unsuspended.';
}
public function doDelete($args)
{
$userPackage = new UserPackage($args['userPackageId']);
$this->delete($this->buildParams($userPackage));
return $userPackage->getCustomField("Domain Name") . ' has been deleted.';
}
public function doCheckUserName($args)
{
$userPackage = new UserPackage($args['userPackageId']);
return $this->checkUserName($this->buildParams($userPackage));
}
public function checkUserName($args)
{
$sock = new DA($args);
$sock->setMethod('GET');
$str = 'user=' . strtolower($args['package']['username']);
$result = $sock->query('/CMD_API_SHOW_USER_CONFIG?' . $str);
if (isset($result['error']) && ($result['error'] == 'Unable to show user' || $result['error'] == 'Cannot show user')) {
return false;
} else {
return true;
}
}
public function getAvailableActions($userPackage)
{
$args = $this->buildParams($userPackage);
$actions = [];
$sock = new DA($args);
$sock->setMethod('GET');
$str = 'user=' . strtolower($args['package']['username']);
$result = $sock->query('/CMD_API_SHOW_USER_CONFIG?' . $str);
if (isset($result['error']) && ($result['error'] == 'Unable to show user' || $result['error'] == 'Cannot show user')) {
$actions[] = 'Create';
} else {
if ($result['suspended'] == 'yes') {
$actions[] = 'UnSuspend';
} else {
$actions[] = 'Suspend';
}
$actions[] = 'Delete';
}
return $actions;
}
public function testConnection($args)
{
CE_Lib::log(4, 'Testing connection to DirectAdmin Server');
$sock = new DA($args);
$sock->setMethod('GET');
$result = $sock->query('/CMD_API_SHOW_USERS');
// The exception is thrown in the query method if not logged in (meaning bad user/pass)
}
public function getDirectLink($userPackage, $getRealLink = true, $fromAdmin = false, $isReseller = false)
{
$args = $this->buildParams($userPackage);
$linkText = $this->user->lang('Login to DirectAdmin');
if ($fromAdmin) {
$cmd = 'panellogin';
return [
'cmd' => $cmd,
'label' => $linkText
];
} elseif ($getRealLink) {
$sock = new DA($args);
$sock->setMethod('POST');
$sock->loginAs(strtolower($args['package']['username']));
$result = $sock->query(
'/CMD_API_LOGIN_KEYS',
[
'max_uses' => 1,
'clear_key' => 'no',
'action' => 'create',
'type' => 'one_time_url',
'passwd' => $args['server']['variables']['plugin_directadmin_Password'],
]
);
if (isset($result['result']) && $result['result'] != '') {
CE_Lib::log(4, 'Link: ' . $result['result']);
return [
'fa' => 'fa fa-user fa-fw',
'link' => $result['result'],
'text' => $linkText,
'form' => ''
];
} else {
throw new CE_Exception('No URL, check that login keys are enabled');
}
} else {
$link = 'index.php?fuse=clients&controller=products&action=openpackagedirectlink&packageId=' . $userPackage->getId() . '&sessionHash=' . CE_Lib::getSessionHash();
return [
'fa' => 'fa fa-user fa-fw',
'link' => $link,
'text' => $linkText,
'form' => ''
];
}
}
public function dopanellogin($args)
{
$userPackage = new UserPackage($args['userPackageId']);
$response = $this->getDirectLink($userPackage);
return $response['link'];
}
}
class DA
{
public $method = 'GET';
public $host;
public $port;
public $user;
public $pass;
public $useSSL;
public $settings;
public function __construct($args)
{
if (substr($args['server']['variables']['ServerHostName'], 0, 6) == 'ssl://') {
$this->host = substr($args['server']['variables']['ServerHostName'], 6);
} else {
$this->host = $args['server']['variables']['ServerHostName'];
}
$this->port = $args['server']['variables']['plugin_directadmin_Port'];
$this->useSSL = $args['server']['variables']['plugin_directadmin_Use_SSL'];
$this->user = $args['server']['variables']['plugin_directadmin_Username'];
$this->pass = $args['server']['variables']['plugin_directadmin_Password'];
}
public function loginAs($login)
{
$this->user = $this->user . '|' . $login;
}
public function setMethod($method = 'GET')
{
$this->method = strtoupper($method);
}
public function query($request, $content = '')
{
$queryDelimiter = strpos($request, '?') === false ? '?' : '&';
$url = ($this->useSSL ? 'https://' : 'http://') . "{$this->host}:{$this->port}{$request}{$queryDelimiter}json=yes";
$ch = curl_init($url);
$caPathOrFile = \Composer\CaBundle\CaBundle::getSystemCaRootBundlePath();
if (is_dir($caPathOrFile)) {
curl_setopt($ch, CURLOPT_CAPATH, $caPathOrFile);
} else {
curl_setopt($ch, CURLOPT_CAINFO, $caPathOrFile);
}
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $this->method);
if ($this->method == 'POST') {
curl_setopt($ch, CURLOPT_POSTFIELDS, $content);
}
curl_setopt($ch, CURLOPT_USERPWD, $this->user . ":" . $this->pass);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$this->result = curl_exec($ch);
if (curl_errno($ch)) {
$error = curl_error($ch);
curl_close($ch);
throw new CE_Exception('DirectAdmin Error: ' . $error);
}
curl_close($ch);
$decodedResult = json_decode($this->result, true);
if ($decodedResult && isset($decodedResult['error'])) {
$errorMessage = $decodedResult['error'];
$resultMessage = $decodedResult['result'];
switch ($errorMessage) {
case 'Your IP is blacklisted':
throw new CE_Exception('DirectAdmin Error: IP blacklisted.');
break;
case 'Not logged in':
throw new CE_Exception('DirectAdmin Error: Not logged in.');
break;
case 'Cannot show user':
case 'Unable to show user':
return $decodedResult;
break;
default:
throw new CE_Exception('DirectAdmin Error: ' . $errorMessage . ' ' . $resultMessage);
break;
}
}
return $decodedResult;
}
}