forked from FOSSBilling/ISPConfig-3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIspconfig3.php
624 lines (538 loc) · 20.2 KB
/
Ispconfig3.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
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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
<?php
/**
* Copyright 2022-2024 FOSSBilling
* Copyright 2011-2021 BoxBilling, Inc.
* SPDX-License-Identifier: Apache-2.0.
*
* @copyright FOSSBilling (https://www.fossbilling.org)
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache-2.0
*/
class Server_Manager_Ispconfig3 extends Server_Manager
{
private $_session = null;
/**
* Returns server manager parameters.
*
* @return array returns an array with the label of the server manager
*/
public static function getForm(): array
{
return [
'label' => 'ISPConfig3',
];
}
public function __destruct()
{
$this->request('logout', []);
unset($this->session);
}
private function request($action, $data)
{
$useSsl = $this->_config['secure'];
$host = $this->_config['host'];
$username = $this->_config['username'];
$password = $this->_config['password'];
$port = !empty($this->_config['port']) ? ':' . $this->_config['port'] . '/' : '';
$host = ($useSsl) ? 'https://' . $host : 'http://' . $host;
$restUrl = $host . $port . 'remote/json.php';
if (is_null($this->_session)) {
$this->_loadConfigAndLogin();
}
$client = $this->getHttpClient()->withOptions([
'verify_peer' => false,
'verify_host' => false,
'timeout' => 60,
]);
$payload = array_merge(['session_id' => $this->_session], $data);
$response = $client->request('POST', $restUrl . '?' . $action, [
'json' => $payload
]);
return json_decode($response->getContent(), true);
}
private function _loadConfigAndLogin()
{
$useSsl = $this->_config['secure'];
$host = $this->_config['host'];
$username = $this->_config['username'];
$password = $this->_config['password'];
$port = !empty($this->_config['port']) ? ':' . $this->_config['port'] . '/' : '';
$host = ($useSsl) ? 'https://' . $host : 'http://' . $host;
$restUrl = $host . $port . 'remote/json.php';
$client = $this->getHttpClient()->withOptions([
'verify_peer' => false,
'verify_host' => false,
'timeout' => 60,
]);
$response = $client->request('POST', $restUrl . '?login', [
'json' => ['username' => $username, 'password' => $password]
]);
$body = json_decode($response->getContent(), true);
$this->_session = $body['response'];
if (!$this->_session) {
throw new Server_Exception('Failed to login and gain a Session Token.');
}
}
/**
* Returns the URL for reseller account management.
*
* @param Server_Account|null $account the account for which the URL is generated
*
* @return string returns the URL as a string
*/
public function getResellerLoginUrl(Server_Account $account = null): string
{
return $this->getLoginUrl();
}
/**
* Returns the URL for account management.
*
* @param Server_Account|null $account the account for which the URL is generated
*
* @return string returns the URL as a string
*/
public function getLoginUrl(Server_Account $account = null): string
{
$useSsl = $this->_config['secure'];
$host = $this->_config['host'];
$port = !empty($this->_config['port']) ? ':' . $this->_config['port'] . '/' : '';
$host = ($useSsl) ? 'https://' . $host : 'http://' . $host;
return $host . $port;
}
/**
* Tests the connection to the server.
*
* @return bool returns true if the connection is successful
*/
public function testConnection(): bool
{
$this->_loadConfigAndLogin();
return true;
}
/**
* Synchronizes the account with the server.
*
* @param Server_Account $account the account to be synchronized
*
* @return Server_Account returns the synchronized account
*/
public function synchronizeAccount(Server_Account $account): Server_Account
{
throw new Server_Exception(
':type: does not support :action:',
[':type:' => 'ISPConfig 3', ':action:' => __trans('account synchronization')]
);
}
/**
* Creates a new account on the server.
*
* @param Server_Account $account the account to be created
*
* @return bool returns true if the account is successfully created
*/
public function createAccount(Server_Account $account)
{
$response = $this->getClientData($account);
if ($account->getReseller()) {
$isReseller = 1;
} else {
$isReseller = 0;
}
if (!$response['response']) {
$request = $this->createClient($account, $isReseller);
$id = $request['response'];
} else {
$id = $response['response'];
}
$client = $account->getClient();
$client->setId($id);
$this->createSite($account);
$this->createDNSZone($account);
}
private function getClientData(Server_Account $account)
{
$response = $this->request('client_get_by_username', [
'username' => $account->getUsername()
]);
return $response;
}
/**
* Private Functions
*/
private function createClient(Server_Account $account, int $isReseller)
{
$client = $account->getClient();
$package = $account->getPackage();
/**
* These are defaults from the docs, if there are anything that needs to be configured
* with template values, please let me know.
*/
$payload = [
'params' => [
'server_id' => 1,
'company_name' => $client->getCompany(),
'contact_name' => $client->getFullName(),
'vat_id' => $package->getCustomValue('vat_id'),
'street' => $client->getStreet(),
'zip' => $client->getZip(),
'city' => $client->getCity(),
'state' => $client->getState(),
'country' => $client->getCountry(),
'telephone' => $client->getTelephone(),
'mobile' => $client->getTelephone(),
'fax' => $client->getTelephone(),
'email' => $client->getEmail(),
'internet' => $client->getWww(),
'icq' => '',
'notes' => $account->getNote(),
'default_mailserver' => $package->getCustomValue('mailserver') ?? 1,
'limit_maildomain' => $package->getCustomValue('max_mail_domains') ?? -1,
'limit_mailbox' => $package->getMaxPop(),
'limit_mailalias' => $package->getCustomValue('max_mail_aliases') ?? -1,
'limit_mailaliasdomain' => $package->getCustomValue('max_mail_alias_domains') ?? -1,
'limit_mailforward' => $package->getCustomValue('max_mail_forwarders') ?? -1,
'limit_mailcatchall' => $package->getCustomValue('max_mail_catchall') ?? -1,
'limit_mailrouting' => 0,
'limit_mail_wblist' => 0,
'limit_mailfilter' => $package->getCustomValue('max_mail_filters') ?? -1,
'limit_fetchmail' => $package->getCustomValue('max_fetchmail') ?? -1,
'limit_mailquota' => $package->getCustomValue('mail_quota') ?? -1,
'limit_spamfilter_wblist' => 0,
'limit_spamfilter_user' => 0,
'limit_spamfilter_policy' => $package->getCustomValue('limit_spam_policy') ?? -1,
'default_webserver' => 1,
'limit_web_ip' => '',
'limit_web_domain' => $package->getMaxDomains(),
'limit_web_quota' => $package->getQuota(),
'web_php_options' => $package->getCustomValue('web_php_options') ?? 'no,fast-cgi,cgi,mod,suphp,php-fpm',
'limit_web_subdomain' => $package->getMaxSubdomains(),
'limit_web_aliasdomain' => $package->getMaxParkedDomains(),
'limit_ftp_user' => $package->getMaxFtp(),
'limit_shell_user' => $package->getCustomValue('limit_shell_user') ?? 1,
'ssh_chroot' => $package->getCustomValue('ssh_chroot') ?? 'no,jailkit,ssh-chroot',
'limit_webdav_user' => 0,
'default_dnsserver' => 1,
'limit_dns_zone' => -1,
'limit_dns_slave_zone' => -1,
'limit_dns_record' => -1,
'default_dbserver' => 1,
'limit_database' => $package->getMaxSql(),
'limit_cron' => $package->getCustomValue('max_cron_jobs') ?? -1,
'limit_cron_type' => 'url',
'limit_cron_frequency' => $package->getCustomValue('limit_cron_frequency') ?? 5,
'limit_traffic_quota' => $package->getMaxBandwidth(),
'limit_client' => 0, // If this value is > 0, then the client is a reseller
'parent_client_id' => 0,
'username' => $account->getUsername(),
'password' => $account->getPassword(),
'language' => $package->getCustomValue('language') ?? 'en',
'usertheme' => 'default',
'template_master' => 0,
'template_additional' => '',
'created_at' => 0
]
];
$response = $this->request('client_add', $payload);
return $response;
}
private function createSite(Server_Account $account)
{
// Does the site already exist?
if ($this->isSiteAlreadyCreated($account)) {
return true;
}
$client = $account->getClient();
$package = $account->getPackage();
$payload = [
'params' => [
'client_id' => $client->getId(),
'domain' => $account->getDomain(),
'type' => 'vhost',
'vhost_type' => 'name',
'client_group_id' => $client->getId() + 1,
'server_id' => 1,
'hd_quota' => $package->getQuota(),
'traffic_quota' => $package->getBandwidth(),
'traffic_quota_lock' => 'y',
'allow_override' => 'ALL',
'errordocs' => 1,
'is_subdomainwww' => 1,
'subdomain' => 'none',
'php' => 'y',
'cgi' => 'y',
'php' => 'php-fpm',
'ip_address' => '*',
'active' => 'y',
'ssl' => 'y',
'pm' => 'ondemand',
'pm_process_idle_timeout' => $package->getCustomValue('pm_process_idle_timeout') ?? 30,
'pm_max_requests' => $package->getCustomValue('pm_max_requests') ?? 30,
'http_port' => '80',
'https_port' => '443'
]
];
$response = $this->request('sites_web_domain_add', $payload);
return $response;
}
private function isSiteAlreadyCreated(Server_Account $account)
{
$sites = $this->getClientSites($account);
if (is_array($sites['response'])) {
foreach ($clientSites['response'] as $key => $domain) {
if ($account->getDomain() == $domain['domain']) {
return true;
}
}
}
return false;
}
private function getClientSites(Server_Account $account)
{
$client = $this->getClientData($account);
$response = $this->request('client_get_sites_by_user', [
'sys_userid' => $client['response']['userid'],
'sys_groupid' => $client['response']['groups']
]);
return $response;
}
private function createDNSZone(Server_Account $account)
{
$client = $account->getClient();
// Setup DNSZone
$zone = $this->request(
'dns_zone_add',
[
'client_id' => $client->getId(),
'params' => [
'server_id' => 1,
'origin' => $account->getDomain(),
'ns' => $account->getNs1(),
'zone' => $client->getId(),
'name' => $account->getDomain(),
'type' => 'A',
'data' => $account->getIp(),
'mbox' => 'mail.' . $account->getDomain() . '.',
'refresh' => '7200',
'retry' => '540',
'expire' => '604800',
'minimum' => '86400',
'ttl' => '3600',
'active' => 'y'
]
]
);
$zoneId = $zone['response']; // Grab the zone ID to add the other records
//Adding the DNS record A
$testing = $this->request('dns_a_add', [
'client_id' => $client->getid(),
'params' => [
'server_id' => 1,
'zone' => $zoneId,
'name' => $account->getDomain() . '.',
'type' => 'A',
'data' => $account->getIp(),
'ttl' => '3600',
'active' => 'y',
'stamp' => date('Y-m-d H:i:s')
]
]);
//Adding the DNS record A
$this->request('dns_a_add', [
'client_id' => $client->getid(),
'params' => [
'server_id' => 1,
'zone' => $zoneId,
'name' => 'www',
'type' => 'A',
'data' => $account->getIp(),
'ttl' => '3600',
'active' => 'y',
]
]);
//Adding the DNS record A
$this->request('dns_a_add', [
'client_id' => $client->getid(),
'params' => [
'server_id' => 1,
'zone' => $zoneId,
'name' => 'mail',
'type' => 'A',
'data' => $account->getIp(),
'ttl' => '3600',
'active' => 'y',
'stamp' => date('Y-m-d H:i:s')
]
]);
//Adding the DNS record NS1
$this->request('dns_ns_add', [
'client_id' => $client->getid(),
'params' => [
'server_id' => 1,
'zone' => $zoneId,
'name' => $account->getDomain() . '.',
'type' => 'ns',
'data' => $account->getNs1() . '.',
'aux' => '0',
'ttl' => '86400',
'active' => 'Y',
'stamp' => date('Y-m-d H:i:s'),
'serial' => '1',
]
]);
//Adding the DNS record NS1
$this->request('dns_ns_add', [
'client_id' => $client->getid(),
'params' => [
'server_id' => 1,
'zone' => $zoneId,
'name' => $account->getDomain() . '.',
'type' => 'ns',
'data' => $account->getNs2() . '.',
'aux' => '0',
'ttl' => '86400',
'active' => 'Y',
'stamp' => date('Y-m-d H:i:s'),
'serial' => '1',
]
]);
$this->request('mail_domain_add', [
'client_id' => $client->getId(),
'server_id' => 1,
'domain' => $account->getDomain(),
'active' => 'y'
]);
}
/**
* Suspends an account on the server.
*
* @param Server_Account $account the account to be suspended
*
* @return bool returns true if the account is successfully suspended
*/
public function suspendAccount(Server_Account $account)
{
$response = $this->request('sites_web_domain_set_status', [
'primary_id' => $this->getPrimaryId($account),
'status' => 'inactive'
]);
return $response['response'];
}
private function getPrimaryId(Server_Account $account)
{
$sites = $this->getClientSites($account);
if (is_array($sites['response'])) {
foreach ($sites['response'] as $key => $domain) {
if ($account->getDomain() == $domain['domain']) {
return $domain['domain_id'];
}
}
}
return false;
}
/**
* Unsuspends an account on the server.
*
* @param Server_Account $account the account to be unsuspended
*
* @return bool returns true if the account is successfully unsuspended
*/
public function unsuspendAccount(Server_Account $account): bool
{
$response = $this->request('sites_web_domain_set_status', [
'primary_id' => $this->getPrimaryId($account),
'status' => 'active'
]);
return $response['response'];
}
/**
* Cancels an account on the server.
*
* @param Server_Account $account the account to be cancelled
*
* @return bool returns true if the account is successfully cancelled
*/
public function cancelAccount(Server_Account $account): bool
{
$client = $this->getClientData($account);
$response = $this->request('client_delete_everything', [
'client_id' => $client['response']['client_id']
]);
return $response['response'];
}
/**
* Changes the package of an account on the server.
*
* @param Server_Account $account the account for which the package is to be changed
* @param Server_Package $package the new package
*
* @return bool returns true if the package is successfully changed
*/
public function changeAccountPackage(Server_Account $account, Server_Package $package): bool
{
throw new Server_Exception(
':type: does not support :action:',
[':type:' => 'ISPConfig 3', ':action:' => __trans('changing the account IP')]
);
}
/**
* Changes the username of an account on the server.
*
* @param Server_Account $account the account for which the username is to be changed
* @param string $newUsername the new username
*
* @return bool returns true if the username is successfully changed
*/
public function changeAccountUsername(Server_Account $account, string $newUsername): bool
{
throw new Server_Exception(
':type: does not support :action:',
[':type:' => 'ISPConfig 3', ':action:' => __trans('username changes')]
);
}
/**
* Changes the domain of an account on the server.
*
* @param Server_Account $account the account for which the domain is to be changed
* @param string $newDomain the new domain
*
* @return bool returns true if the domain is successfully changed
*/
public function changeAccountDomain(Server_Account $account, string $newDomain): bool
{
throw new Server_Exception(
':type: does not support :action:',
[':type:' => 'ISPConfig 3', ':action:' => __trans('changing the account domain')]
);
}
/**
* Changes the password of an account on the server.
*
* @param Server_Account $account the account for which the password is to be changed
* @param string $newPassword the new password
*
* @return bool returns true if the password is successfully changed
*/
public function changeAccountPassword(Server_Account $account, string $newPassword): bool
{
$client = $this->getClientData($account);
$response = $this->request('client_change_password', [
'client_id' => $client['response']['client_id'],
'password' => $newPassword
]);
return $response['response'];
}
/**
* Changes the IP of an account on the server.
*
* @param Server_Account $account the account for which the IP is to be changed
* @param string $newIp the new IP
*
* @return bool returns true if the IP is successfully changed
*/
public function changeAccountIp(Server_Account $account, string $newIp): bool
{
throw new Server_Exception(
':type: does not support :action:',
[':type:' => 'ISPConfig 3', ':action:' => __trans('changing the account IP')]
);
}
}