-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathCpanel.php
More file actions
451 lines (353 loc) · 14.2 KB
/
Copy pathCpanel.php
File metadata and controls
451 lines (353 loc) · 14.2 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
<?php
namespace ZanySoft\Cpanel;
use Config;
use Exception;
class Cpanel extends xmlapi
{
protected $username = '';
private const MYSQL_MODULE = 'MysqlFE';
private const SUBDOMAIN_MODULE = 'SubDomain';
private const EMAIL_MODULE = 'Email';
/**
* All parameters to this function are optional and can be set via the accessor functions or constants
*
* @param string|null $host The host to perform queries on
* @param string|null $username The username to authenticate as
* @param string|null $password The password to authenticate with
* @throws Exception
*/
public function __construct(?string $host = null, ?string $username = null, ?string $password = null)
{
$config = Config::get('cpanel');
$protocol = $config['protocol'] ?? 'https';
$host = $host ?: ($config['host'] ?? $config['ip']);
$username = $username ?: $config['username'];
$password = $password ?: $config['password'];
if (!$host) {
throw new Exception('Host not defined.');
}
parent::__construct($host, $username, $password);
$auth_type = $config['auth_type'] ?? 'pass';
$this->set_auth_type($auth_type);
if ($auth_type == 'hash') {
$hash = $config['hash'] ?? '';
$this->setHashAuth($username, $hash);
} else {
$this->setAuth($username, $password);
}
$this->setProtocol($protocol);
$this->setHost($host);
$this->setPort($config['port']);
$this->set_debug($config['debug'] ?? 0);
$this->set_output($config['output'] ?? 'array');
}
public static function make(?string $host = null, ?string $username = null, ?string $password = null): self
{
return new self($host, $username, $password);
}
public function setProtocol(string $protocol): self
{
$this->set_protocol($protocol);
return $this;
}
public function setHost(string $host): self
{
$this->set_host($host);
return $this;
}
public function setPort(int $port): self
{
$this->set_port($port);
return $this;
}
public function setAuth(string $username, string $password): self
{
$this->username = $username;
$this->password_auth($username, $password);
return $this;
}
public function setHashAuth(string $username, string $hash): self
{
$this->username = $username;
$this->hash_auth($username, $hash);
return $this;
}
/**
* Call an API1 function
*
* This function allows you to call API1 from within the XML-API, This allowes a user to peform actions
* such as adding ftp accounts, etc
*
* @param string $user The username of the account to perform API1 actions on
* @param string $module The module of the API1 call to use
* @param string $function The function of the API1 call
* @param array $args The arguments for the API1 function, this should be a non-associative array
* @return mixed
*/
public function api1(string $user, string $module, string $function, array $args = []): array
{
if (!$user || !$module || !$function) {
return ['reason' => 'api1 requires username, module and function', 'result' => 0];
}
$result = $this->api1_query($user, $module, $function, $args);
return $this->returnResult($result);
}
/**
* Call an API2 Function
*
* This function allows you to call an API2 function, this is the modern API for cPanel and should be used in preference over
* API1 when possible
*
* @param string $user The username of the account to perform API2 actions on
* @param string $module The module of the API2 call to use
* @param string $function The function of the API2 call
* @param array $args An associative array containing the arguments for the API2 call
* @return mixed
*/
public function api2(string $user, string $module, string $function, array $args = []): array
{
if (!$user || !$module || !$function) {
return ['reason' => 'api2 requires username, module and function', 'result' => 0];
}
try {
$result = $this->api2_query($user, $module, $function, $args);
return $this->returnResult($result);
} catch (\Exception $e) {
return [
'reason' => $e->getMessage(),
'result' => null
];
}
}
public function createSubdomain(string $subdomain, string $username = '', string $subdomain_dir = '', string $main_domain = ''): array
{
$subdomain_dir = $subdomain_dir ?: config('cpanel.subdomain_dir');
$username = $username ?: $this->username;
$domain = $this->parseDomain($main_domain ?: config('cpanel.domain'));
if (!$domain) {
return ['reason' => 'Please provide a valid main domain', 'result' => 0];
}
return $this->api2($username, self::SUBDOMAIN_MODULE, 'addsubdomain', [
'domain' => $subdomain,
'rootdomain' => $domain,
'dir' => $subdomain_dir,
'disallowdot' => 1
]);
}
public function removeSubdomain(string $subdomain, string $main_domain = ''): array
{
$domain = $this->parseDomain($main_domain ?: config('cpanel.domain'));
if (!$domain) {
return ['reason' => 'Please provide a valid main domain', 'result' => 0];
}
return $this->api2($this->username, self::SUBDOMAIN_MODULE, 'delsubdomain', [
'domain' => $subdomain.'.'.$domain
]);
}
public function createdb(string $databaseName): array
{
if (!$databaseName) {
return ['reason' => 'Database name is required', 'result' => 0];
}
$databaseName = $this->fixName($databaseName);
if ($invalid = $this->isNameInvalid($databaseName, 64)) {
return ['reason' => $invalid, 'result' => 0];
}
return $this->api2($this->username, self::MYSQL_MODULE, 'createdb', ['db' => $databaseName]);
}
public function deletedb(string $databaseName): array
{
if (!$databaseName) {
return ['reason' => 'Database name is required', 'result' => 0];
}
$databaseName = $this->fixName($databaseName);
return $this->api2($this->username, self::MYSQL_MODULE, 'deletedb', ['db' => $databaseName]);
}
public function checkdbuser(string $databaseUser): array
{
if (!$databaseUser) {
return ['reason' => 'Database username is required', 'result' => 0];
}
$databaseUser = $this->fixName($databaseUser);
return $this->api2($this->username, self::MYSQL_MODULE, 'dbuserexists', ['dbuser' => $databaseUser]);
}
public function createdbuser(string $username, string $password): array
{
if (!$username || !$password) {
return ['reason' => 'Database username and password are required', 'result' => 0];
}
if ($invalid = $this->isNameInvalid($username, 32, 'username')) {
return ['reason' => $invalid, 'result' => 0];
}
$isInvalid = $this->checkPassword($password);
if ($isInvalid) {
return [
'reason' => $isInvalid[0],
'errors' => $isInvalid,
'result' => '0'
];
}
$username = $this->fixName($username);
$user = $this->checkdbuser($username);
if ($user['result'] == 1) {
return ['reason' => 'Database user '.$username.' already exists.', 'result' => 0];
}
return $this->api2($this->username, self::MYSQL_MODULE, 'createdbuser', [
'dbuser' => $username,
'password' => $password
]
);
}
public function setdbuser(string $databaseName, string $databaseUser, string|array $privileges = ''): array
{
if (!$databaseName || !$databaseUser) {
return ['reason' => 'Database name and username are required', 'result' => 0];
}
$databaseName = $this->fixName($databaseName);
$databaseUser = $this->fixName($databaseUser);
if (is_array($privileges)) {
$privileges = implode(',', $privileges);
}
return $this->api2($this->username, self::MYSQL_MODULE, 'setdbuserprivileges', [
'privileges' => $privileges,
'dbuser' => $databaseUser,
'db' => $databaseName
]);
}
public function accountsList(string $search_type = '', string $search = ''): array
{
return $this->returnResult($this->listaccts($search_type, $search));
}
public function accountDetails(string $username = ''): mixed
{
$username = $username ?: $this->username;
return $this->returnResult($this->accountsummary($username));
}
/**
* @deprecated
*/
public function accountDetials(string $username = ''): mixed
{
return $this->accountDetails($username);
}
public function createEmailAccount(string $email, string $password, int $quota = 500, string $main_domain = ''): array
{
return $this->api2($this->username, self::EMAIL_MODULE, 'addpop', [
'domain' => $main_domain,
'email' => $email,
'password' => $password,
'quota' => $quota
]
);
}
protected function returnResult($result): mixed
{
if (is_bool($result)) {
return ['reason' => '', 'result' => $result];
}
if ($this->get_output() === 'xml') {
$response = simplexml_load_string($result, null, LIBXML_NOERROR | LIBXML_NOWARNING);
if ($response) {
$result = json_decode(json_encode($response), true);
}
} elseif ($this->get_output() === 'json') {
$result = json_decode($result, true);
} else {
$result = json_decode(json_encode($result), true);
}
if (!isset($result['data'])) {
return $result;
}
$data = $result['data'];
if (is_array($data)) {
$reason = (string) (is_array($data['reason']) ? implode(', ', $data['reason']) : $data['reason']);
$resultValue = (string) (is_array($data['result']) ? array_shift($data['result']) : $data['result']);
$reason = $this->cleanReason($reason);
return ['reason' => $reason, 'result' => (int) $resultValue];
}
$reason = $this->getOperationMessage($result['func'] ?? null, $data);
return ['reason' => $reason, 'result' => (int) $data];
}
protected function parseDomain(string $domain): string
{
$parse = parse_url($domain);
if (isset($parse['host'])) {
$domain = $parse['host'];
} elseif (mb_strpos($domain, '/', 2) !== false) {
$domain = strstr($domain, '/', true);
}
$domain = str_replace('www.', '', $domain);
return (mb_strpos($domain, '.') !== false) ? $domain : '';
}
protected function isNameInvalid(string $name, int $maxLength, string $for = 'name'): ?string
{
$length = strlen($name);
if ($this->hasUsernamePrefixed($name)) {
$length = $length - (strlen($this->username) + 1);
}
if ($length < 4) {
return $maxLength
? "Database {$for} must be between 4 and {$maxLength} characters"
: "Database {$for} must be at least 4 characters";
}
if ($maxLength && $length > $maxLength) {
return "Database {$for} must not exceed {$maxLength} characters";
}
return null;
}
protected function checkPassword(string $pwd): ?array
{
$errors = [];
if (strlen($pwd) < 8) {
$errors[] = 'Password must be at least 8 characters';
}
if (!preg_match('#[0-9]+#', $pwd)) {
$errors[] = 'Password must contain at least one number';
}
if (!preg_match('#[a-zA-Z]+#', $pwd)) {
$errors[] = 'Password must contain at least one letter';
}
return !empty($errors) ? $errors : null;
}
protected function slug(string $title, string $separator = '-'): string
{
// Convert all dashes/underscores into separator
$flip = $separator === '-' ? '_' : '-';
$title = preg_replace('!['.preg_quote($flip,'/').']+!u', $separator, $title);
// Remove all characters that are not the separator, letters, numbers, or whitespace.
$title = preg_replace('![^'.preg_quote($separator,'/').'\pL\pN\s]+!u', '', mb_strtolower($title));
// Replace all separator characters and whitespace by a single separator
$title = preg_replace('!['.preg_quote($separator,'/').'\s]+!u', $separator, $title);
return trim($title, $separator);
}
protected function hasUsernamePrefixed(string $name): bool
{
return strpos(strtolower($name), strtolower($this->username)) === 0;
}
protected function fixName(string $name): string
{
return $this->hasUsernamePrefixed($name) ? $name : $this->username.'_'.$name;
}
private function cleanReason(string $reason): string
{
if (mb_strpos($reason, ')') !== false) {
$reason = ltrim(strstr($reason, ')'), ') ');
}
if (mb_strpos($reason, ' at ') !== false) {
$reason = trim(strstr($reason, ' at ', true));
}
return $reason;
}
private function getOperationMessage(?string $function, $status): string
{
$messages = [
'createdb' => 'Database'.($status ? ' ' : ' not ').'created successfully',
'createdbuser' => 'Database user'.($status ? ' ' : ' not ').'created successfully',
'dbuserexists' => 'Database user '.($status ? 'exists' : 'does not exist'),
'addsubdomain' => 'Subdomain '.($status ? 'created successfully' : 'not created'),
'delsubdomain' => 'Subdomain '.($status ? 'removed successfully' : 'not removed'),
'setdbuserprivileges' => 'Database user privileges '.($status ? 'set successfully' : 'not set'),
];
return $messages[$function] ?? '';
}
}