Passed
Branch master (0832fa)
by Shaharia
02:37
created

Accounts::domainDetails()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 39

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 39
ccs 0
cts 28
cp 0
rs 8.9848
c 0
b 0
f 0
cc 5
nc 5
nop 1
crap 30
1
<?php
2
3
namespace PreviewTechs\cPanelWHM\WHM;
4
5
use DateTime;
6
use Http\Client\Exception;
7
use PreviewTechs\cPanelWHM\Entity\Account;
8
use PreviewTechs\cPanelWHM\Entity\Domain;
9
use PreviewTechs\cPanelWHM\Entity\DomainUser;
10
use PreviewTechs\cPanelWHM\Entity\SuspendedAccount;
11
use PreviewTechs\cPanelWHM\Exceptions\ClientExceptions;
12
use PreviewTechs\cPanelWHM\WHMClient;
13
14
/**
15
 * Class Accounts
16
 *
17
 * @package PreviewTechs\cPanelWHM\WHM
18
 */
19
class Accounts
20
{
21
    /**
22
     *
23
     * @var WHMClient
24
     */
25
    protected $client;
26
27
    /**
28
     * Accounts constructor.
29
     *
30
     * @param WHMClient $client
31
     */
32
    public function __construct(WHMClient $client)
33
    {
34
        $this->client = $client;
35
    }
36
37
    /**
38
     * Search accounts from your WHM server.
39
     *
40
     * WHM API function: Accounts -> listaccts
41
     *
42
     * $accounts = new Accounts($c);
43
     * $keyword = "search_keyword";
44
     * $searchType = "username";   //valid search types are "domain", "owner", "user", "ip", "package"
45
     * $options = [
46
     *       'searchmethod' => "exact",   //"exact" or "regex",
47
     *       "page" => 1,
48
     *       "limit" => 10,   //per page,
49
     *       "want" => "username"    //A comma-separated list of fields you want to fetch
50
     *   ];
51
     *
52
     * try {
53
     *       $accounts->searchAccounts($keyword, $searchType, $options);
54
     *   } catch (\Http\Client\Exception $e) {
55
     *       echo $e->getMessage();
56
     *   } catch (\PreviewTechs\cPanelWHM\Exceptions\ClientExceptions $e) {
57
     *       echo $e->getMessage();
58
     *   }
59
     *
60
     * @link https://documentation.cpanel.net/display/DD/WHM+API+1+Functions+-+listaccts
61
     *
62
     * @param null $keyword
63
     * @param null $searchType
64
     * @param array $options
65
     *
66
     * @return array
67
     * @throws ClientExceptions
68
     * @throws Exception
69
     */
70
    public function searchAccounts($keyword = null, $searchType = null, array $options = [])
71
    {
72
        $limit = 10;
73
        $page  = 1;
74
75
        $params = [
76
            'api.version'      => 1,
77
            'api.chunk.enable' => 1,
78
            'api.chunk.size'   => $limit,
79
            'api.chunk.start'  => $page * $limit
80
        ];
81
82
        if ( ! empty($options['limit'])) {
83
            $params['api.chunk.size'] = intval($options['limit']);
84
        }
85
86
        if ( ! empty($options['page'])) {
87
            $params['api.chunk.start'] = intval($options['page']) * $params['api.chunk.size'];
88
        }
89
90
        if ( ! empty($searchType) && ! in_array($searchType, ["domain", "owner", "user", "ip", "package"])) {
91
            throw new \InvalidArgumentException("`searchType` must be one of these - domain, owner, user, ip, package");
92
        }
93
94
        if ( ! empty($options['searchmethod']) && ! in_array($options['searchmethod'], ["exact", "regex"])) {
95
            throw new \InvalidArgumentException("options[searchmethod] must be either `regex` or `exact`");
96
        }
97
98
        if ( ! empty($options['want'])) {
99
            $params['want'] = $options['want'];
100
        }
101
102
        if ( ! empty($searchType)) {
103
            $params['searchtype'] = $searchType;
104
        }
105
106
        if ( ! empty($keyword)) {
107
            $params['search'] = $keyword;
108
            empty($searchType) ? $params['searchtype'] = "user" : null;
109
        }
110
111
        $results = $this->client->sendRequest("/json-api/listaccts", "GET", $params);
112
        if (empty($results['data']['acct'])) {
113
            return [];
114
        }
115
116
        $accounts = [];
117
        foreach ($results['data']['acct'] as $account) {
118
            $accounts[] = Account::buildFromArray($account);
119
        }
120
121
        return [
122
            'accounts' => $accounts,
123
            'count'    => $params['api.chunk.size'],
124
            'page'     => $page
125
        ];
126
    }
127
128
    /**
129
     * Get an account details
130
     *
131
     * WHM API function: Accounts -> accountsummary
132
     *
133
     * @link https://documentation.cpanel.net/display/DD/WHM+API+1+Functions+-+accountsummary
134
     *
135
     * @param null $user
136
     * @param null $domain
137
     *
138
     * @return null|Account
139
     * @throws ClientExceptions
140
     * @throws Exception
141
     */
142
    public function getDetails($user = null, $domain = null)
143
    {
144
        if (empty($user) && empty($domain)) {
145
            throw ClientExceptions::invalidArgument("You must provide either a username or a domain or both");
146
        }
147
148
        if ( ! empty($user) && ! empty($domain)) {
149
            throw ClientExceptions::invalidArgument(
150
                "You must provide only one argument either user OR domain (not both)"
151
            );
152
        }
153
154
        $params = [];
155
156
        if ( ! empty($user)) {
157
            $params['user'] = $user;
158
        }
159
160
        if ( ! empty($domain)) {
161
            $params['domain'] = $domain;
162
        }
163
164
        $result = $this->client->sendRequest("/json-api/accountsummary", "GET", $params);
165
        if (empty($result)) {
166
            return null;
167
        }
168
169
        if ($result['status'] === 0) {
170
            throw ClientExceptions::recordNotFound(
171
                ! empty($result['statusmsg']) ? $result['statusmsg'] : "Record not found"
172
            );
173
        }
174
175
        if ( ! empty($result['acct']) && is_array($result['acct'])) {
176
            return Account::buildFromArray($result['acct'][0]);
177
        }
178
179
        return null;
180
    }
181
182
    /**
183
     * This function lists available WHM API 1 functions.
184
     *
185
     * This function only lists the functions that are available to the current user.
186
     * For example, if the authenticated user is a reseller without root -level privileges,
187
     * the function will not list WHM API 1 functions that require root privileges.
188
     *
189
     * WHM API function: Accounts -> applist
190
     *
191
     * @link https://documentation.cpanel.net/display/DD/WHM+API+1+Functions+-+applist
192
     *
193
     * @return array
194
     * @throws ClientExceptions
195
     * @throws Exception
196
     */
197
    public function availableFunctions()
198
    {
199
        $result = $this->client->sendRequest("/json-api/applist", 'GET', []);
200
201
        if ( ! empty($result['app']) && sizeof($result['app']) > 0) {
202
            return $result['app'];
203
        }
204
205
        return [];
206
    }
207
208
    /**
209
     * Create a new account. This function creates a cPanel account.
210
     * The function also sets up the new account's domain information.
211
     *
212
     * WHM API function: Accounts -> createacct
213
     *
214
     * @link    https://documentation.cpanel.net/display/DD/WHM+API+1+Functions+-+createacct
215
     *
216
     * @param Account $account
217
     * @param array $options
218
     *
219
     * @return array
220
     * @throws ClientExceptions
221
     * @throws Exception
222
     */
223
    public function create(Account $account, array $options = [])
224
    {
225
        if (empty($account->getUser())) {
226
            throw ClientExceptions::invalidArgument("You must provide an username to create new account");
227
        }
228
229
        if (empty($account->getDomain())) {
230
            throw ClientExceptions::invalidArgument("You must provide a domain to create new account");
231
        }
232
233
        $params             = [];
234
        $params['username'] = $account->getUser();
235
        $params['domain']   = $account->getDomain();
236
        ! empty($account->getPlanName()) ? $params['plan'] = $account->getPlanName() : null;
237
        ! empty($options['pkgname']) ? $params['pkgname'] = $options['pkgname'] : null;
238
        if ( ! empty($options['savepkg'])) {
239
            if ( ! in_array(intval($options['savepkg']), [0, 1])) {
240
                throw new ClientExceptions("`savepkg` must be either 0 or 1");
241
            }
242
243
            $params['savepkg'] = $options['savepkg'];
244
        }
245
246
        ! empty($options['featurelist']) ? $params['featurelist'] = $options['featurelist'] : null;
247
        if ( ! empty($account->getDiskLimit())) {
248
            if ($account->getDiskLimit() === -1) {
249
                $params['quota'] = 0;
250
            } else {
251
                $params['quota'] = intval($account->getDiskLimit());
252
            }
253
        }
254
255
        ! empty($account->getPassword()) ? $params['password'] = $account->getPassword() : null;
256
        ! empty($account->getIpAddress()) ? $params['ip'] = $account->getIpAddress() : null;
257
        ! empty($account->isCgiEnable()) ? $params['cgi'] = (int)$account->isCgiEnable() : null;
258
        ! empty($account->isSpamAssassinEnable()) ?
259
            $params['spamassassin'] = (int)$account->isSpamAssassinEnable()
260
            : null;
261
        ! empty($account->isFrontPageEnable()) ? $params['frontpage'] = (int)$account->isFrontPageEnable() : null;
262
        ! empty($account->getShell()) ? $params['hasshell'] = 1 : null;
263
        ! empty($account->getEmail()) ? $params['contactemail'] = 1 : null;
264
        ! empty($account->getEmail()) ? $params['contactemail'] = 1 : null;
265
        ! empty($account->getTheme()) ? $params['cpmod'] = 1 : null;
266
267 View Code Duplication
        if ($account->getMaxFTP() === -1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
268
            $params['maxftp'] = "unlimited";
269
        } elseif ($account->getMaxFTP() > 0) {
270
            $params['maxftp'] = intval($account->getMaxFTP());
271
        }
272
273 View Code Duplication
        if ($account->getMaxSQL() === -1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
274
            $params['maxsql'] = "unlimited";
275
        } elseif ($account->getMaxSQL() > 0) {
276
            $params['maxsql'] = intval($account->getMaxSQL());
277
        }
278
279 View Code Duplication
        if ($account->getMaxPOP() === -1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
280
            $params['maxpop'] = "unlimited";
281
        } elseif ($account->getMaxPOP() > 0) {
282
            $params['maxpop'] = intval($account->getMaxPOP());
283
        }
284
285 View Code Duplication
        if ($account->getMaxMailingList() === -1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
286
            $params['maxlst'] = "unlimited";
287
        } elseif ($account->getMaxMailingList() > 0) {
288
            $params['maxlst'] = intval($account->getMaxMailingList());
289
        }
290
291 View Code Duplication
        if ($account->getMaxSubDomain() === -1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
292
            $params['maxsub'] = "unlimited";
293
        } elseif ($account->getMaxSubDomain() > 0) {
294
            $params['maxsub'] = intval($account->getMaxSubDomain());
295
        }
296
297 View Code Duplication
        if ($account->getMaxParkedDomains() === -1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
298
            $params['maxpark'] = "unlimited";
299
        } elseif ($account->getMaxParkedDomains() > 0) {
300
            $params['maxpark'] = intval($account->getMaxParkedDomains());
301
        }
302
303 View Code Duplication
        if ($account->getMaxAddonDomains() === -1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
304
            $params['maxaddon'] = "unlimited";
305
        } elseif ($account->getMaxAddonDomains() > 0) {
306
            $params['maxaddon'] = intval($account->getMaxAddonDomains());
307
        }
308
309 View Code Duplication
        if ($account->getBandwidthLimit() === -1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
310
            $params['bwlimit'] = "unlimited";
311
        } elseif ($account->getBandwidthLimit() > 0) {
312
            $params['bwlimit'] = intval($account->getBandwidthLimit());
313
        }
314
315
        ! empty($options['customip']) ? $params['customip'] = $options['customip'] : null;
316
317
        ! empty($account->getLanguagePreference()) ? $params['language'] = $account->getLanguagePreference() : null;
318
319
        ! empty($options['useregns']) ? $params['useregns'] = $options['useregns'] : null;
320
        ! empty($options['reseller']) ? $params['reseller'] = (int)$options['reseller'] : null;
321
        ! empty($options['forcedns']) ? $params['forcedns'] = (int)$options['forcedns'] : null;
322
323
        ! empty($account->getMailboxFormat()) ? $params['mailbox_format'] = $account->getMailboxFormat() : null;
324
325
        if ( ! empty($options['mxcheck'])) {
326
            if ( ! in_array($options['mxcheck'], ['local', 'secondary', 'remote', 'auto'])) {
327
                throw new ClientExceptions("options[mxcheck] parameters must be one of local, secondary, remote, auto");
328
            }
329
330
            $params['mxcheck'] = $options['mxcheck'];
331
        }
332
333 View Code Duplication
        if ($account->getMaxEmailPerHour() === -1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
334
            $params['max_email_per_hour'] = "unlimited";
335
        } elseif ($account->getMaxEmailPerHour() > 0) {
336
            $params['max_email_per_hour'] = intval($account->getMaxEmailPerHour());
337
        }
338
339
        if ($account->getMaxEmailAccountQuota() === -1) {
340
            $params['max_emailacct_quota'] = "unlimited";
341
        } elseif ($account->getMaxEmailAccountQuota() > 0) {
342
            $params['max_email_per_hour'] = intval($account->getMaxEmailAccountQuota());
343
        }
344
345 View Code Duplication
        if ($account->getMaxDeferFailMailPercentage() === -1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
346
            $params['max_defer_fail_percentage'] = "unlimited";
347
        } elseif ($account->getMaxDeferFailMailPercentage() > 0) {
348
            $params['max_defer_fail_percentage'] = intval($account->getMaxDeferFailMailPercentage());
349
        }
350
351
        ! empty($account->getUid()) ? $params['uid'] = $account->getUid() : null;
352
        ! empty($account->getPartition()) ? $params['homedir'] = $account->getPartition() : null;
353
        ! empty($options['dkim']) ? $params['dkim'] = intval($options['dkim']) : null;
354
        ! empty($options['spf']) ? $params['spf'] = intval($options['spf']) : null;
355
        ! empty($account->getOwner()) ? $params['owner'] = $account->getOwner() : null;
356
357
        $result = $this->client->sendRequest("/json-api/createacct", "GET", $params);
358
359
        if ( ! empty($result) && ! empty($result['result'][0]) && $result['result'][0]['status'] === 0) {
360
            throw new ClientExceptions($result['result'][0]['statusmsg']);
361
        }
362
363
        if ( ! empty($result) && ! empty($result['result'][0]) && $result['result'][0]['status'] === 1) {
364
            return ['result' => $result['result'][0]['options'], 'raw_output' => $result['result'][0]['rawout']];
365
        }
366
367
        return [];
368
    }
369
370
    /**
371
     * This function retrieves domain data.
372
     *
373
     * WHM API function: Accounts -> domainuserdata
374
     *
375
     * @link https://documentation.cpanel.net/display/DD/WHM+API+1+Functions+-+domainuserdata
376
     *
377
     * @param $domain
378
     *
379
     * @return null|DomainUser
380
     * @throws ClientExceptions
381
     * @throws Exception
382
     */
383
    public function domainDetails($domain)
384
    {
385
        $params = ['domain' => $domain];
386
387
        $result = $this->client->sendRequest("/json-api/domainuserdata", "GET", $params);
388
        if (empty($result)) {
389
            return null;
390
        }
391
392
        if ($result['result'][0]['status'] === 0) {
393
            throw new ClientExceptions($result['result'][0]['statusmsg']);
394
        }
395
396
        $userData   = $result['userdata'];
397
        $domainUser = new DomainUser();
398
        $domainUser->setHasCGI((bool)$userData['hascgi']);
399
        $domainUser->setServerName($userData['servername']);
400
        $domainUser->setOwner($userData['owner']);
401
        $domainUser->setScriptAlias($userData['scriptalias']);
402
        $domainUser->setHomeDirectory($userData['homedir']);
403
        $domainUser->setCustomLog($userData['customlog']);
404
        $domainUser->setUser($userData['user']);
405
        $domainUser->setGroup($userData['group']);
406
        $domainUser->setIpAddress($userData['ip']);
407
        $domainUser->setPort($userData['port']);
408
        $domainUser->setPhpOpenBaseDirectoryProtect((bool)$userData['phpopenbasedirprotect']);
409
410
        if ($userData['usecanonicalname'] === "Off") {
411
            $domainUser->setUseCanonicalName(false);
412
        } elseif ($userData['usecanonicalname'] === "On") {
413
            $domainUser->setUseCanonicalName(true);
414
        }
415
416
        $domainUser->setServerAdmin($userData['serveradmin']);
417
        $domainUser->setServerAlias($userData['serveralias']);
418
        $domainUser->setDocumentRoot($userData['documentroot']);
419
420
        return $domainUser;
421
    }
422
423
    /**
424
     * This function modifies a user's disk quota.
425
     * WHM API function: Accounts -> editquota
426
     * @link https://documentation.cpanel.net/display/DD/WHM+API+1+Functions+-+editquota
427
     *
428
     * @param $username
429
     * @param $newQuota
430
     *
431
     * @return bool
432
     * @throws ClientExceptions
433
     * @throws Exception
434
     */
435
    public function changeDiskSpaceQuota($username, $newQuota)
436
    {
437
        $params = ['user' => $username, 'quota' => $newQuota];
438
439
        $this->client->sendRequest("/json-api/editquota", "GET", $params);
440
441
        return true;
442
    }
443
444
    /**
445
     * This function forces a user to change the account password after the next login attempt.
446
     *
447
     * WHM API function: Accounts -> forcepasswordchange
448
     *
449
     * @link https://documentation.cpanel.net/display/DD/WHM+API+1+Functions+-+forcepasswordchange
450
     *
451
     * @param array $usernames
452
     * @param int $stopOnFail
453
     *
454
     * @return null
455
     * @throws ClientExceptions
456
     * @throws Exception
457
     */
458
    public function forcePasswordChange(array $usernames, $stopOnFail = 1)
459
    {
460
        $params = [
461
            "stop_on_failure" => $stopOnFail
462
        ];
463
464
        $usersJson = [];
465
        foreach ($usernames as $username) {
466
            $usersJson[$username] = 1;
467
        }
468
469
        $params['users_json'] = json_encode($usersJson);
470
471
        $result = $this->client->sendRequest("/json-api/forcepasswordchange", "GET", $params);
472
        if ($result['metadata']['result'] === 0) {
473
            throw new ClientExceptions($result['metadata']['reason']);
474
        }
475
476
        if ( ! empty($result['data'])) {
477
            return $result['updated'];
478
        }
479
480
        return null;
481
    }
482
483
    /**
484
     * This function returns information about each domain on the server.
485
     *
486
     * WHM API function: Accounts -> get_domain_info
487
     * @link https://documentation.cpanel.net/display/DD/WHM+API+1+Functions+-+get_domain_info
488
     *
489
     * @return Domain[]|null
490
     * @throws ClientExceptions
491
     * @throws Exception
492
     */
493
    public function getDomains()
494
    {
495
        $params = [];
496
        $result = $this->client->sendRequest("/json-api/get_domain_info", "GET", $params);
497
498
        if (empty($result)) {
499
            return null;
500
        }
501
502
        if ( ! empty($result['metadata']) && $result['metadata']['result'] === 1) {
503
            $domains = $result['data']['domains'];
504
505
            $domainList = [];
506
            foreach ($domains as $domain) {
507
                $do = new Domain();
508
                $do->setPort(intval($domain['port']));
509
                $do->setUser($domain['user']);
510
                $do->setDomain($domain['domain']);
511
                $do->setIpv4SSL($domain['ipv4_ssl']);
512
                $do->setIpv6($domain['ipv6']);
513
                $do->setSslPort(intval($domain['port_ssl']));
514
                $do->setPhpVersion($domain['php_version']);
515
                $do->setUserOwner($domain['user_owner']);
516
                $do->setDomainType($domain['domain_type']);
517
                $do->setIpv6IsDedicated((bool)$domain['ipv6_is_dedicated']);
518
                $do->setIpv4($domain['ipv4']);
519
                $do->setModSecurityEnabled((bool)$domain['modsecurity_enabled']);
520
                $do->setDocRoot($domain['docroot']);
521
                $domainList[] = $do;
522
            }
523
524
            return $domainList;
525
        }
526
527
        return null;
528
    }
529
530
    /**
531
     * Digest Authentication is enabled or disabled for any specific user.
532
     * This function checks whether Digest Authentication is enabled for
533
     * a cPanel user.
534
     * Windows® Vista, Windows® 7, and Windows® 8 require Digest Authentication
535
     * support in order to access Web Disk over an unencrypted connection.
536
     *
537
     * WHM API function: Accounts -> has_digest_auth
538
     * @link https://documentation.cpanel.net/display/DD/WHM+API+1+Functions+-+has_digest_auth
539
     *
540
     * @param $user
541
     *
542
     * @return bool|null
543
     * @throws ClientExceptions
544
     * @throws Exception
545
     */
546 View Code Duplication
    public function hasDigestAuth($user)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
547
    {
548
        $params = ['user' => $user];
549
        $result = $this->client->sendRequest("/json-api/has_digest_auth", "GET", $params);
550
        if ( ! empty($result['data'])) {
551
            return $result['data']['digestauth'] === 1 ? true : false;
552
        }
553
554
        return null;
555
    }
556
557
    /**
558
     * This function checks whether a cPanel user's home directory contains a valid .my.cnf file.
559
     * WHM API function: Accounts -> has_mycnf_for_cpuser
560
     * @link https://documentation.cpanel.net/display/DD/WHM+API+1+Functions+-+has_mycnf_for_cpuser
561
     *
562
     * @param $user
563
     *
564
     * @return bool|null
565
     * @throws ClientExceptions
566
     * @throws Exception
567
     */
568 View Code Duplication
    public function hasMyCnfInHomeDirectory($user)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
569
    {
570
        $params = ['user' => $user];
571
        $result = $this->client->sendRequest("/json-api/has_mycnf_for_cpuser", "GET", $params);
572
        if ( ! empty($result['data'])) {
573
            return $result['data']['has_mycnf_for_cpuser'] === 1 ? true : false;
574
        }
575
576
        return null;
577
    }
578
579
    /**
580
     * Modify a cPanel account's bandwidth quota.
581
     * WHM API function: Accounts -> limitbw
582
     * @link https://documentation.cpanel.net/display/DD/WHM+API+1+Functions+-+limitbw
583
     *
584
     * @param $user
585
     * @param $bwlimit
586
     *
587
     * @return null
588
     * @throws ClientExceptions
589
     * @throws Exception
590
     */
591
    public function limitBandwidth($user, $bwlimit)
592
    {
593
        $params = ['user' => $user, 'bwlimit' => intval($bwlimit)];
594
        $result = $this->client->sendRequest("/json-api/limitbw", "GET", $params);
595
        if ( ! empty($result['metadata']) && $result['metadata']['result'] === 1) {
596
            return $result['data']['bwlimits'][0];
597
        }
598
599
        return null;
600
    }
601
602
603
    /**
604
     * Get lists of the cPanel user accounts and the root user on the server.
605
     * WHM API function: Accounts -> list_users
606
     * @link https://documentation.cpanel.net/display/DD/WHM+API+1+Functions+-+list_users
607
     *
608
     * @return array
609
     * @throws ClientExceptions
610
     * @throws Exception
611
     */
612 View Code Duplication
    public function getUsers()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
613
    {
614
        $result = $this->client->sendRequest("/json-api/list_users", "GET", []);
615
        if ( ! empty($result['metadata']) && $result['metadata']['result'] === 1) {
616
            return $result['data']['users'];
617
        }
618
619
        return [];
620
    }
621
622
    /**
623
     * Get a list of locked accounts.
624
     * This function lists locked accounts on the server. Only WHM users with root-level privileges can unsuspend locked accounts.
625
     *
626
     * WHM API function: Accounts -> listlockedaccounts
627
     * @link https://documentation.cpanel.net/display/DD/WHM+API+1+Functions+-+listlockedaccounts
628
     *
629
     * @return array
630
     * @throws ClientExceptions
631
     * @throws Exception
632
     */
633 View Code Duplication
    public function getLockedAccounts()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
634
    {
635
        $result = $this->client->sendRequest("/json-api/listlockedaccounts", "GET", []);
636
        if ( ! empty($result['metadata']) && $result['metadata']['result'] === 1) {
637
            return $result['data']['account'];
638
        }
639
640
        return [];
641
    }
642
643
    /**
644
     * Get a list of suspended accounts
645
     *
646
     * WHM API function: listsuspended
647
     * @link https://documentation.cpanel.net/display/DD/WHM+API+1+Functions+-+listsuspended
648
     *
649
     * @return SuspendedAccount[]
650
     * @throws ClientExceptions
651
     * @throws Exception
652
     */
653
    public function getSuspendedAccounts()
654
    {
655
        $result = $this->client->sendRequest("/json-api/listsuspended", "GET", []);
656
        if ( ! empty($result['metadata']) && $result['metadata']['result'] === 1) {
657
            $suspendList = $result['data']['account'];
658
659
            $lists = [];
660
            foreach ($suspendList as $item) {
661
                $sa = new SuspendedAccount();
662
                $sa->setUser($item['user']);
663
                $sa->setOwner($item['owner']);
664
                $sa->setIsLocked((bool)$item['is_locked']);
665
                $sa->setReason($item['reason']);
666
                $sa->setTime(DateTime::createFromFormat("D M j H:i:s Y", $item['time']));
0 ignored issues
show
Security Bug introduced by
It seems like \DateTime::createFromFor...:i:s Y', $item['time']) targeting DateTime::createFromFormat() can also be of type false; however, PreviewTechs\cPanelWHM\E...endedAccount::setTime() does only seem to accept object<DateTime>, did you maybe forget to handle an error condition?
Loading history...
667
                $lists[] = $sa;
668
            }
669
670
            return $lists;
671
        }
672
673
        return [];
674
    }
675
}
676