HEAD My Page =======
Yii2 ISPConfig API Wrapper
The preferred way to install this extension is through composer.
Either run
php composer.phar require --prefer-dist "mzdani/yii2-ispconfig-api" "dev-master"
or add
"mzdani/yii2-ispconfig-api": "dev-master"
to the require section of your composer.json
file.
Below is some example how to use the wrapper.
<?php
use mdscomp\ISPConfig\SoapClient;
$cp = new SoapClient('http://your.cpdomain.tld:8080/remote/index.php', 'remoteusername', 'remoteuserpass');
// Get Quota Status from Client
$client_records = $cp->clientGetQuota('yourclientusername');
print_r($client_records);
$cp->logout();
?>
Don't forget to logout()
after using, or your ISPConfig database remote session will full of "open session". And sometimes I get the wrong result because not logout()
after get data.
Because ISPConfig doesn't have any support to show quota usage with remote api without call one by one related function, and I'm too lazy to always call that, so I add new function into default ISPConfig remote api file. You can find it from /usr/local/ispconfig/interface/lib/classes/remoting.inc.php
, open it and insert new function below into at the end of class.
<?php
/*******************
* CUSTOM PATCH START
*******************/
public function client_get_quota($session_id, $client_username){
global $app;
/*
* Get Client Data
*/
$client_records = $this->client_get_by_username($session_id, $client_username);
$client_data = $this->client_get($session_id, $client_records['client_id']);
/*
* Website Harddisk Quota
*/
$tmp_site = $app->db->queryAllRecords("SELECT data from monitor_data WHERE type = 'harddisk_quota' ORDER BY created DESC");
$monitor_data_site = array();
if(is_array($tmp_site)) {
foreach ($tmp_site as $tmp_mon_site) {
$monitor_data_site = array_merge_recursive($monitor_data_site, unserialize($app->db->unquote($tmp_mon_site['data'])));
}
}
$sites = $app->db->queryAllRecords("SELECT * FROM web_domain WHERE active = 'y' AND type = 'vhost' AND sys_groupid = ".$client_records['default_group']);
$has_quota = false;
$total_quota['limit_web_quota'] = ($client_data['limit_web_quota'] == '-1') ? $app->lng('unlimited') : $client_data['limit_web_quota']*1048576;
$total_quota['limit_traffic_quota'] = ($client_data['limit_traffic_quota'] == '-1') ? $app->lng('unlimited') : $client_data['limit_traffic_quota']*1048576;
$total_quota['limit_mailquota'] = ($client_data['limit_mailquota'] == '-1') ? $app->lng('unlimited') : $client_data['limit_mailquota']*1048576;
$web_quota = [];
if(is_array($sites) && !empty($sites)){
for($i=0;$i<sizeof($sites);$i++){
$username = $sites[$i]['system_user'];
$web_quota[$i]['domain'] = $sites[$i]['domain'];
$web_quota[$i]['hd_quota'] = ($sites[$i]['hd_quota'] == '-1') ? $app->lng('unlimited') : $sites[$i]['hd_quota']*1048576;
$web_quota[$i]['traffic_quota'] = ($sites[$i]['traffic_quota'] == '-1') ? $app->lng('unlimited') : $sites[$i]['traffic_quota']*1048576;
$web_quota[$i]['used'] = $monitor_data_site['user'][$username]['used']*1024;
$web_quota[$i]['soft'] = $monitor_data_site['user'][$username]['soft']*1024;
$web_quota[$i]['hard'] = $monitor_data_site['user'][$username]['hard']*1024;
$web_quota[$i]['files'] = $monitor_data_site['user'][$username]['files'];
if (!is_numeric($web_quota[$i]['used'])){
if ($web_quota[$i]['used'][0] > $web_quota[$i]['used'][1]){
$web_quota[$i]['used'] = $web_quota[$i]['used'][0];
} else {
$web_quota[$i]['used'] = $web_quota[$i]['used'][1];
}
}
if (!is_numeric($web_quota[$i]['soft'])) $web_quota[$i]['soft']=$web_quota[$i]['soft'][1];
if (!is_numeric($web_quota[$i]['hard'])) $web_quota[$i]['hard']=$web_quota[$i]['hard'][1];
if (!is_numeric($web_quota[$i]['files'])) $web_quota[$i]['files']=$web_quota[$i]['files'][1];
if($web_quota[$i]['soft'] == 0) $web_quota[$i]['soft'] = $app->lng('unlimited');
if($web_quota[$i]['hard'] == 0) $web_quota[$i]['hard'] = $app->lng('unlimited');
if($web_quota[$i]['soft'] == 0 || $web_quota[$i]['soft'] == '0') $web_quota[$i]['soft'] = $app->lng('unlimited');
if($web_quota[$i]['hard'] == 0 || $web_quota[$i]['hard'] == '0') $web_quota[$i]['hard'] = $app->lng('unlimited');
/*
* Website Traffic Statistic
*/
//** Traffic of the current month
$tmp_year = date('Y');
$tmp_month = date('m');
$tmp_rec = $app->db->queryOneRecord("SELECT SUM(traffic_bytes) as t FROM web_traffic WHERE hostname = '".$app->db->quote($sites[$i]['domain'])."' AND YEAR(traffic_date) = '$tmp_year' AND MONTH(traffic_date) = '$tmp_month'");
$web_quota[$i]['traffic']['this_month'] = number_format($tmp_rec['t'], 0, '.', '');
//** Traffic of the last month
$tmp_year = date('Y', mktime(0, 0, 0, date("m")-1, date("d"), date("Y")));
$tmp_month = date('m', mktime(0, 0, 0, date("m")-1, date("d"), date("Y")));
$tmp_rec = $app->db->queryOneRecord("SELECT sum(traffic_bytes) as t FROM web_traffic WHERE hostname = '".$app->db->quote($sites[$i]['domain'])."' AND YEAR(traffic_date) = '$tmp_year' AND MONTH(traffic_date) = '$tmp_month'");
$web_quota[$i]['traffic']['last_month'] = number_format($tmp_rec['t']*1024, 0, '.', '');
}
$has_quota = true;
}
/*
* Email Quota
*/
$tmp_mails = $app->db->queryAllRecords("SELECT data from monitor_data WHERE type = 'email_quota' ORDER BY created DESC");
$monitor_data_mails = array();
if(is_array($tmp_mails)) {
foreach ($tmp_mails as $tmp_mon_mails) {
$tmp_array = unserialize($app->db->unquote($tmp_mon_mails['data']));
if(is_array($tmp_array)) {
foreach($tmp_array as $username => $data) {
if(!$monitor_data_mails[$username]['used']) $monitor_data_mails[$username]['used'] = $data['used'];
}
}
}
}
$emails = $app->db->queryAllRecords("SELECT * FROM mail_user WHERE 1 AND sys_groupid = ".$client_records['default_group']);
$has_mailquota = false;
$mails_quota = [];
if(is_array($emails) && !empty($emails)){
for($i=0;$i<sizeof($emails);$i++){
$email = $emails[$i]['email'];
$mails_quota[$i]['email'] = $emails[$i]['email'];
$mails_quota[$i]['used'] = isset($monitor_data_mails[$email]['used']) ? $monitor_data_mails[$email]['used'] : array(1 => 0);
if (!is_numeric($mails_quota[$i]['used'])) $mails_quota[$i]['used']=$mails_quota[$i]['used'][1];
if($mails_quota[$i]['quota'] == 0){
$mails_quota[$i]['quota'] = $app->lng('unlimited');
}
}
$has_mailquota = true;
}
/*
* Returning Data
*/
$return = [
'limit' => $total_quota,
'used' => [
'site' => ($has_quota) ? $web_quota : false,
'mail' => ($has_mailquota) ? $mails_quota : false,
],
];
return $return;
}
/*****************
* CUSTOM PATCH END
*****************/
?>
After adding those function. Here is some example using the new function.
<?php
/**
* Get Status ISPConfig Panel
*/
use mdscomp\ISPConfig\SoapClient;
$cp = new SoapClient('http://your.cpdomain.tld:8080/remote/index.php', 'remoteusername', 'remoteuserpasswd');
$data = $cp->clientGetQuota('clientusername');
$max_quota_hdd = $data['limit']['limit_web_quota'];
$max_quota_traffic = $data['limit']['limit_traffic_quota'];
$max_quota_mail = $data['limit']['limit_mailquota'];
$used_quota_hdd = 0;
$used_quota_traffic = 0;
foreach ($data['used']['site'] as $key => $value) {
$used_quota_hdd += $value['used'];
$used_quota_traffic += $value['traffic']['this_month'];
}
Yii::$app->formatter->sizeFormatBase = 1000;
$tpl[] = [
'item' => 'Disk Space Usage',
'used' => Yii::$app->formatter->asShortSize($used_quota_hdd, 2),
'max' => ($max_quota_hdd !== 'unlimited') ? Yii::$app->formatter->asShortSize($max_quota_hdd, 2) : $max_quota_hdd,
'percent' => ($max_quota_hdd !== 'unlimited') ? round((($used_quota_hdd/$max_quota_hdd) * 100), 2) : 0,
];
Yii::$app->formatter->sizeFormatBase = 1024;
$tpl[] = [
'item' => 'Traffic Usage',
'used' => Yii::$app->formatter->asShortSize($used_quota_traffic, 2),
'max' => ($max_quota_traffic !== 'unlimited') ? Yii::$app->formatter->asShortSize($max_quota_traffic, 2) : $max_quota_traffic,
'percent' => ($max_quota_traffic !== 'unlimited') ? round((($used_quota_traffic/$max_quota_traffic) * 100), 2) : 0,
];
Yii::$app->formatter->sizeFormatBase = 1000;
$used_quota_mail = 0;
if (isset($data['used']['mail']) && is_array($data['used']['mail'])) {
foreach ($data['used']['mail'] as $key => $value) {
$used_quota_mail += $value['used'];
}
$tpl[] = [
'item' => 'Email Space Usage',
'used' => Yii::$app->formatter->asShortSize($used_quota_mail, 2),
'max' => ($max_quota_hdd !== 'unlimited') ? Yii::$app->formatter->asShortSize($max_quota_mail, 2) : $max_quota_mail,
'percent' => ($max_quota_hdd !== 'unlimited') ? round((($used_quota_mail/$max_quota_mail) * 100), 2) : 0,
];
}
$cp->logout();
?>
and after some theming, here is the result.
Sorry for my bad english and code. Have fun!