Skip to content

Commit a641dd6

Browse files
author
Dominik
committed
Possibility to get traffic quota via remote API
1 parent 4452e73 commit a641dd6

File tree

3 files changed

+78
-2
lines changed

3 files changed

+78
-2
lines changed

interface/lib/classes/quota_lib.inc.php

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,68 @@ public function get_quota_data($clientid = null, $readable = true) {
9595

9696
return $sites;
9797
}
98-
98+
99+
public function get_trafficquota_data($clientid = null, $lastdays = 0) {
100+
global $app;
101+
102+
$traffic_data = array();
103+
104+
// select vhosts (belonging to client)
105+
if($clientid != null){
106+
$sql_where = " AND sys_groupid = (SELECT default_group FROM sys_user WHERE client_id=".$clientid.")";
107+
}
108+
$sites = $app->db->queryAllRecords("SELECT * FROM web_domain WHERE active = 'y' AND (type = 'vhost' OR type = 'vhostsubdomain' OR type = 'vhostalias')".$sql_where);
109+
110+
$hostnames = array();
111+
$traffic_data = array();
112+
113+
foreach ($sites as $site) {
114+
$hostnames[] = $site['domain'];
115+
$traffic_data[$site['domain']]['domain_id'] = $site['domain_id'];
116+
}
117+
118+
// fetch all traffic-data of selected vhosts
119+
if (!empty($hostnames)) {
120+
$tmp_year = date('Y');
121+
$tmp_month = date('m');
122+
// This Month
123+
$tmp_recs = $app->db->queryAllRecords("SELECT hostname, SUM(traffic_bytes) as t FROM web_traffic WHERE YEAR(traffic_date) = ? AND MONTH(traffic_date) = ? AND hostname IN ('".join("','",$hostnames)."') GROUP BY hostname", $tmp_year, $tmp_month);
124+
foreach ($tmp_recs as $tmp_rec) {
125+
$traffic_data[]['this_month'] = $tmp_rec['t'];
126+
}
127+
// This Year
128+
$tmp_recs = $app->db->queryAllRecords("SELECT hostname, SUM(traffic_bytes) as t FROM web_traffic WHERE YEAR(traffic_date) = ? AND hostname IN ('".join("','",$hostnames)."') GROUP BY hostname", $tmp_year);
129+
foreach ($tmp_recs as $tmp_rec) {
130+
$traffic_data[$tmp_rec['hostname']]['this_year'] = $tmp_rec['t'];
131+
}
132+
133+
$tmp_year = date('Y', mktime(0, 0, 0, date("m")-1, date("d"), date("Y")));
134+
$tmp_month = date('m', mktime(0, 0, 0, date("m")-1, date("d"), date("Y")));
135+
// Last Month
136+
$tmp_recs = $app->db->queryAllRecords("SELECT hostname, SUM(traffic_bytes) as t FROM web_traffic WHERE YEAR(traffic_date) = ? AND MONTH(traffic_date) = ? AND hostname IN ('".join("','",$hostnames)."') GROUP BY hostname", $tmp_year, $tmp_month);
137+
foreach ($tmp_recs as $tmp_rec) {
138+
$traffic_data[$tmp_rec['hostname']]['last_month'] = $tmp_rec['t'];
139+
}
140+
141+
$tmp_year = date('Y', mktime(0, 0, 0, date("m"), date("d"), date("Y")-1));
142+
// Last Year
143+
$tmp_recs = $app->db->queryAllRecords("SELECT hostname, SUM(traffic_bytes) as t FROM web_traffic WHERE YEAR(traffic_date) = ? AND hostname IN ('".join("','",$hostnames)."') GROUP BY hostname", $tmp_year);
144+
foreach ($tmp_recs as $tmp_rec) {
145+
$traffic_data[$tmp_rec['hostname']]['last_year'] = $tmp_rec['t'];
146+
}
147+
148+
if (is_int($lastdays) && ($lastdays > 0)) {
149+
// Last xx Days
150+
$tmp_recs = $app->db->queryAllRecords("SELECT hostname, SUM(traffic_bytes) as t FROM web_traffic WHERE (traffic_date >= DATE_SUB(NOW(), INTERVAL ".$app->db->quote($lastdays)." DAY)) AND hostname IN ('".join("','",$hostnames)."') GROUP BY hostname");
151+
foreach ($tmp_recs as $tmp_rec) {
152+
$traffic_data[$tmp_rec['hostname']]['lastdays'] = $tmp_rec['t'];
153+
}
154+
}
155+
}
156+
157+
return $traffic_data;
158+
}
159+
99160
public function get_mailquota_data($clientid = null, $readable = true) {
100161
global $app;
101162

interface/lib/classes/remote.d/sites.inc.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -966,6 +966,21 @@ public function quota_get_by_user($session_id, $client_id)
966966
return $app->quota_lib->get_quota_data($client_id, false);
967967
}
968968

969+
public function trafficquota_get_by_user($session_id, $client_id, $lastdays = 0)
970+
{
971+
global $app;
972+
$app->uses('quota_lib');
973+
974+
if(!$this->checkPerm($session_id, 'trafficquota_get_by_user')) {
975+
$this->server->fault('permission_denied', 'You do not have the permissions to access this function.');
976+
return false;
977+
}
978+
if ($client_id != null)
979+
$client_id = $app->functions->intval($client_id);
980+
981+
return $app->quota_lib->get_trafficquota_data($client_id, $lastdays);
982+
}
983+
969984
public function databasequota_get_by_user($session_id, $client_id)
970985
{
971986
global $app;

interface/web/client/lib/remote.conf.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
$function_list['client_get_all,client_get,client_add,client_update,client_delete,client_get_sites_by_user,client_get_by_username,client_change_password,client_get_id,client_delete_everything,client_get_emailcontact'] = 'Client functions';
44
$function_list['domains_domain_get,domains_domain_add,domains_domain_delete,domains_get_all_by_user'] = 'Domaintool functions';
5-
$function_list['quota_get_by_user,mailquota_get_by_user,databasequota_get_by_user'] = 'Quota functions';
5+
$function_list['quota_get_by_user,trafficquota_get_by_user,mailquota_get_by_user,databasequota_get_by_user'] = 'Quota functions';
66

77

88
?>

0 commit comments

Comments
 (0)