-
Notifications
You must be signed in to change notification settings - Fork 1
/
export.php
79 lines (63 loc) · 1.83 KB
/
export.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
<?php
use Eugenevdm\WhmApi\Cpanel;
require_once "vendor/autoload.php";
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->load();
// Check if the required command line parameter is provided
if ($argc != 2) {
echo "Usage:\n";
echo " php export.php username\n";
echo " php export.php all\n";
exit(1);
}
$username = $argv[1];
$api = new Cpanel([
'host' => $_ENV['WHM_HOST'],
'username' => $_ENV['WHM_USERNAME'],
'auth_type' => $_ENV['WHM_AUTH_TYPE'],
'password' => $_ENV['WHM_PASSWORD']
]);
if ($argv[1] == "all") {
getAccounts($api);
} else {
getEmailUsage($api, $username);
}
/**
* Retrieve all email accounts and their usage for a given cPanel user and write to a CSV file
*
* https://api.docs.cpanel.net/openapi/cpanel/operation/list_pops_with_disk/
*/
function getEmailUsage($api, $username) {
$data = $api->execute_action(
'3',
'Email',
'list_pops_with_disk',
$username
);
$stream = fopen($username . ".csv", 'a');
foreach($data['result']['data'] as $emailAccount) {
$line = $emailAccount['login']
. ","
. $emailAccount['diskused']
. ","
. $emailAccount['diskquota'] . "\n";
echo $line;
fwrite($stream, $line);
}
fclose($stream);
}
// Retrieve all accounts with mailbox names and write it to a CSV file called `all.csv`
function getAccounts($api) {
$accounts = $api->listAccounts();
$stream = fopen("all.csv", 'a');
foreach ($accounts['acct'] as $account) {
$user = $account['user'];
$emails = $api->listEmailAccounts($user);
foreach ($emails['cpanelresult']['data'] as $data) {
$line = $user . "," . $data['email'] . "\n";
echo $line;
fwrite($stream, $line);
}
}
fclose($stream);
}