Marius Burkard
2016-07-01 49441bdd0f3ff75d5092d5b832b97ea722a66363
commit | author | age
e8b329 1 <?php
DM 2
3 class quota_lib {
f1c4cd 4     public function get_quota_data($clientid = null, $readable = true) {
e8b329 5         global $app; 
DM 6         
7         $tmp_rec =  $app->db->queryAllRecords("SELECT data from monitor_data WHERE type = 'harddisk_quota' ORDER BY created DESC");
8         $monitor_data = array();
9         if(is_array($tmp_rec)) {
10             foreach ($tmp_rec as $tmp_mon) {
11                 $monitor_data = array_merge_recursive($monitor_data, unserialize($app->db->unquote($tmp_mon['data'])));
12             }
13         }
14         //print_r($monitor_data);
15         
b45479 16         // select all websites or websites belonging to client
d2713e 17         $sites = $app->db->queryAllRecords("SELECT * FROM web_domain WHERE active = 'y' AND type = 'vhost'".(($clientid != null)?" AND sys_groupid = (SELECT default_group FROM sys_user WHERE client_id=?)":''), $clientid);
e8b329 18         
DM 19         //print_r($sites);
20         if(is_array($sites) && !empty($sites)){
21             for($i=0;$i<sizeof($sites);$i++){
22                 $username = $sites[$i]['system_user'];
23                 $sites[$i]['used'] = $monitor_data['user'][$username]['used'];
24                 $sites[$i]['soft'] = $monitor_data['user'][$username]['soft'];
25                 $sites[$i]['hard'] = $monitor_data['user'][$username]['hard'];
26                 $sites[$i]['files'] = $monitor_data['user'][$username]['files'];
27         
28                 if (!is_numeric($sites[$i]['used'])){
29                     if ($sites[$i]['used'][0] > $sites[$i]['used'][1]){
30                         $sites[$i]['used'] = $sites[$i]['used'][0];
31                     } else {
32                         $sites[$i]['used'] = $sites[$i]['used'][1];
33                     }
34                 }
35                 if (!is_numeric($sites[$i]['soft'])) $sites[$i]['soft']=$sites[$i]['soft'][1];
36                 if (!is_numeric($sites[$i]['hard'])) $sites[$i]['hard']=$sites[$i]['hard'][1];
37                 if (!is_numeric($sites[$i]['files'])) $sites[$i]['files']=$sites[$i]['files'][1];
d2713e 38                 
MC 39                 $sites[$i]['used_raw'] = $sites[$i]['used'];
40                 $sites[$i]['soft_raw'] = $sites[$i]['soft'];
41                 $sites[$i]['hard_raw'] = $sites[$i]['hard'];
42                 $sites[$i]['files_raw'] = $sites[$i]['files'];
43                 $sites[$i]['used_percentage'] = ($sites[$i]['soft'] > 0 && $sites[$i]['used'] > 0 ? round($sites[$i]['used'] * 100 / $sites[$i]['soft']) : 0);
44                 
e8b329 45                 if ($readable) {
DM 46                     // colours
47                     $sites[$i]['display_colour'] = '#000000';
48                     if($sites[$i]['soft'] > 0){
49                         $used_ratio = $sites[$i]['used']/$sites[$i]['soft'];
50                     } else {
51                         $used_ratio = 0;
52                     }
53                     if($used_ratio >= 0.8) $sites[$i]['display_colour'] = '#fd934f';
54                     if($used_ratio >= 1) $sites[$i]['display_colour'] = '#cc0000';
55             
56                     if($sites[$i]['used'] > 1024) {
57                         $sites[$i]['used'] = round($sites[$i]['used'] / 1024, 2).' MB';
58                     } else {
59                         if ($sites[$i]['used'] != '') $sites[$i]['used'] .= ' KB';
60                     }
61             
62                     if($sites[$i]['soft'] > 1024) {
63                         $sites[$i]['soft'] = round($sites[$i]['soft'] / 1024, 2).' MB';
64                     } else {
65                         $sites[$i]['soft'] .= ' KB';
66                     }
67             
68                     if($sites[$i]['hard'] > 1024) {
69                         $sites[$i]['hard'] = round($sites[$i]['hard'] / 1024, 2).' MB';
70                     } else {
71                         $sites[$i]['hard'] .= ' KB';
72                     }
73             
74                     if($sites[$i]['soft'] == " KB") $sites[$i]['soft'] = $app->lng('unlimited');
75                     if($sites[$i]['hard'] == " KB") $sites[$i]['hard'] = $app->lng('unlimited');
76                     
77                     if($sites[$i]['soft'] == '0 B' || $sites[$i]['soft'] == '0 KB' || $sites[$i]['soft'] == '0') $sites[$i]['soft'] = $app->lng('unlimited');
78                     if($sites[$i]['hard'] == '0 B' || $sites[$i]['hard'] == '0 KB' || $sites[$i]['hard'] == '0') $sites[$i]['hard'] = $app->lng('unlimited');
79                     
80                     /*
81                      if(!strstr($sites[$i]['used'],'M') && !strstr($sites[$i]['used'],'K')) $sites[$i]['used'].= ' B';
82                     if(!strstr($sites[$i]['soft'],'M') && !strstr($sites[$i]['soft'],'K')) $sites[$i]['soft'].= ' B';
83                     if(!strstr($sites[$i]['hard'],'M') && !strstr($sites[$i]['hard'],'K')) $sites[$i]['hard'].= ' B';
84                     */
85                 }
86                 else {
87                     if (empty($sites[$i]['soft'])) $sites[$i]['soft'] = -1;
88                     if (empty($sites[$i]['hard'])) $sites[$i]['hard'] = -1;
89                     
90                     if($sites[$i]['soft'] == '0 B' || $sites[$i]['soft'] == '0 KB' || $sites[$i]['soft'] == '0') $sites[$i]['soft'] = -1;
91                     if($sites[$i]['hard'] == '0 B' || $sites[$i]['hard'] == '0 KB' || $sites[$i]['hard'] == '0') $sites[$i]['hard'] = -1;
92                 }
93             }
94         }
95         
96         return $sites;
97     }
a641dd 98     
D 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){
cc7a82 106             $sql_where = " AND sys_groupid = (SELECT default_group FROM sys_user WHERE client_id=?)";
a641dd 107         }
cc7a82 108         $sites = $app->db->queryAllRecords("SELECT * FROM web_domain WHERE active = 'y' AND (type = 'vhost' OR type = 'vhostsubdomain' OR type = 'vhostalias')".$sql_where, $clientid);
a641dd 109     
D 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
cc7a82 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 ? GROUP BY hostname", $tmp_year, $tmp_month, $hostnames);
a641dd 124             foreach ($tmp_recs as $tmp_rec) {
7105a0 125                 $traffic_data[$tmp_rec['hostname']]['this_month'] = $tmp_rec['t'];
a641dd 126             }
D 127             // This Year
cc7a82 128             $tmp_recs = $app->db->queryAllRecords("SELECT hostname, SUM(traffic_bytes) as t FROM web_traffic WHERE YEAR(traffic_date) = ? AND hostname IN ? GROUP BY hostname", $tmp_year, $hostnames);
a641dd 129             foreach ($tmp_recs as $tmp_rec) {
D 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
cc7a82 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 ? GROUP BY hostname", $tmp_year, $tmp_month, $hostnames);
a641dd 137             foreach ($tmp_recs as $tmp_rec) {
D 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
cc7a82 143             $tmp_recs = $app->db->queryAllRecords("SELECT hostname, SUM(traffic_bytes) as t FROM web_traffic WHERE YEAR(traffic_date) = ? AND hostname IN ? GROUP BY hostname", $tmp_year, $hostnames);
a641dd 144             foreach ($tmp_recs as $tmp_rec) {
D 145                 $traffic_data[$tmp_rec['hostname']]['last_year'] = $tmp_rec['t'];
146             }
147                 
148             if (is_int($lastdays)  && ($lastdays > 0)) {
149                 // Last xx Days
cc7a82 150                 $tmp_recs = $app->db->queryAllRecords("SELECT hostname, SUM(traffic_bytes) as t FROM web_traffic WHERE (traffic_date >= DATE_SUB(NOW(), INTERVAL ? DAY)) AND hostname IN ? GROUP BY hostname", $lastdays, $hostnames);
a641dd 151                 foreach ($tmp_recs as $tmp_rec) {
D 152                     $traffic_data[$tmp_rec['hostname']]['lastdays'] = $tmp_rec['t'];
153                 }
154             }
155         }
156     
157         return $traffic_data;
158     }
cfc79e 159
LC 160     public function get_ftptrafficquota_data($clientid = null, $lastdays = 0) {
161         global $app;
162     
163         $traffic_data = array();
164     
165         // select vhosts (belonging to client)
166         if($clientid != null){
167             $sql_where = " AND sys_groupid = (SELECT default_group FROM sys_user WHERE client_id=?)";
168         }
169         $sites = $app->db->queryAllRecords("SELECT * FROM web_domain WHERE active = 'y' AND (type = 'vhost' OR type = 'vhostsubdomain' OR type = 'vhostalias')".$sql_where, $clientid);
170     
171         $hostnames = array();
172         $traffic_data = array();
173     
174         foreach ($sites as $site) {
175             $hostnames[] = $site['domain'];
176             $traffic_data[$site['domain']]['domain_id'] = $site['domain_id'];
177         }
178     
179         // fetch all traffic-data of selected vhosts
180         if (!empty($hostnames)) {
181             $tmp_year = date('Y');
182             $tmp_month = date('m');
183             // This Month
184             $tmp_recs = $app->db->queryAllRecords("SELECT hostname, SUM(in_bytes) AS ftp_in, SUM(out_bytes) AS ftp_out FROM ftp_traffic WHERE YEAR(traffic_date) = ? AND MONTH(traffic_date) = ? AND hostname IN ? GROUP BY hostname", $tmp_year, $tmp_month, $hostnames);
185             foreach ($tmp_recs as $tmp_rec) {
186                 $traffic_data[$tmp_rec['hostname']]['this_month'] = $tmp_rec['t'];
187             }
188             // This Year
189             $tmp_recs = $app->db->queryAllRecords("SELECT hostname, SUM(in_bytes) AS ftp_in, SUM(out_bytes) AS ftp_out FROM ftp_traffic WHERE YEAR(traffic_date) = ? AND hostname IN ? GROUP BY hostname", $tmp_year, $hostnames);
190             foreach ($tmp_recs as $tmp_rec) {
191                 $traffic_data[$tmp_rec['hostname']]['this_year'] = $tmp_rec['t'];
192             }
193                 
194             $tmp_year = date('Y', mktime(0, 0, 0, date("m")-1, date("d"), date("Y")));
195             $tmp_month = date('m', mktime(0, 0, 0, date("m")-1, date("d"), date("Y")));
196             // Last Month
197             $tmp_recs = $app->db->queryAllRecords("SELECT hostname, SUM(in_bytes) AS ftp_in, SUM(out_bytes) AS ftp_out FROM ftp_traffic WHERE YEAR(traffic_date) = ? AND MONTH(traffic_date) = ? AND hostname IN ? GROUP BY hostname", $tmp_year, $tmp_month, $hostnames);
198             foreach ($tmp_recs as $tmp_rec) {
199                 $traffic_data[$tmp_rec['hostname']]['last_month'] = $tmp_rec['t'];
200             }
201                 
202             $tmp_year = date('Y', mktime(0, 0, 0, date("m"), date("d"), date("Y")-1));
203             // Last Year
204             $tmp_recs = $app->db->queryAllRecords("SELECT hostname, SUM(in_bytes) AS ftp_in, SUM(out_bytes) AS ftp_out FROM ftp_traffic WHERE YEAR(traffic_date) = ? AND hostname IN ? GROUP BY hostname", $tmp_year, $hostnames);
205             foreach ($tmp_recs as $tmp_rec) {
206                 $traffic_data[$tmp_rec['hostname']]['last_year'] = $tmp_rec['t'];
207             }
208                 
209             if (is_int($lastdays)  && ($lastdays > 0)) {
210                 // Last xx Days
211                 $tmp_recs = $app->db->queryAllRecords("SELECT hostname, SUM(in_bytes) AS ftp_in, SUM(out_bytes) AS ftp_out FROM ftp_traffic WHERE (traffic_date >= DATE_SUB(NOW(), INTERVAL ? DAY)) AND hostname IN ? GROUP BY hostname", $lastdays, $hostnames);
212                 foreach ($tmp_recs as $tmp_rec) {
213                     $traffic_data[$tmp_rec['hostname']]['lastdays'] = $tmp_rec['t'];
214                 }
215             }
216         }
217     
218         return $traffic_data;
219     }
a641dd 220     
f1c4cd 221     public function get_mailquota_data($clientid = null, $readable = true) {
e8b329 222         global $app;
DM 223         
224         $tmp_rec =  $app->db->queryAllRecords("SELECT data from monitor_data WHERE type = 'email_quota' ORDER BY created DESC");
225         $monitor_data = array();
226         if(is_array($tmp_rec)) {
227             foreach ($tmp_rec as $tmp_mon) {
228                 //$monitor_data = array_merge_recursive($monitor_data,unserialize($app->db->unquote($tmp_mon['data'])));
229                 $tmp_array = unserialize($app->db->unquote($tmp_mon['data']));
230                 if(is_array($tmp_array)) {
231                     foreach($tmp_array as $username => $data) {
232                         if(!$monitor_data[$username]['used']) $monitor_data[$username]['used'] = $data['used'];
233                     }
234                 }
235             }
236         }
237         //print_r($monitor_data);
238         
b45479 239         // select all email accounts or email accounts belonging to client
d2713e 240         $emails = $app->db->queryAllRecords("SELECT * FROM mail_user".(($clientid != null)? " WHERE sys_groupid = (SELECT default_group FROM sys_user WHERE client_id=?)" : ''), $clientid);
e8b329 241         
DM 242         //print_r($emails);
243         if(is_array($emails) && !empty($emails)){
244             for($i=0;$i<sizeof($emails);$i++){
245                 $email = $emails[$i]['email'];
246         
247                 $emails[$i]['used'] = isset($monitor_data[$email]['used']) ? $monitor_data[$email]['used'] : array(1 => 0);
248         
249                 if (!is_numeric($emails[$i]['used'])) $emails[$i]['used']=$emails[$i]['used'][1];
3eefd1 250                 
d2713e 251                 $emails[$i]['quota_raw'] = $emails[$i]['quota'];
MC 252                 $emails[$i]['used_raw'] = $emails[$i]['used'];
253                 $emails[$i]['used_percentage'] = ($emails[$i]['quota'] > 0 && $emails[$i]['used'] > 0 ? round($emails[$i]['used'] * 100 / $emails[$i]['quota']) : 0);
254
255                 
3eefd1 256                 if ($readable) {
DM 257                     // colours
258                     $emails[$i]['display_colour'] = '#000000';
259                     if($emails[$i]['quota'] > 0){
260                         $used_ratio = $emails[$i]['used']/$emails[$i]['quota'];
261                     } else {
262                         $used_ratio = 0;
263                     }
264                     if($used_ratio >= 0.8) $emails[$i]['display_colour'] = '#fd934f';
265                     if($used_ratio >= 1) $emails[$i]['display_colour'] = '#cc0000';
266             
267                     if($emails[$i]['quota'] == 0){
268                         $emails[$i]['quota'] = $app->lng('unlimited');
269                     } else {
270                         $emails[$i]['quota'] = round($emails[$i]['quota'] / 1048576, 4).' MB';
271                     }
272             
273             
274                     if($emails[$i]['used'] < 1544000) {
275                         $emails[$i]['used'] = round($emails[$i]['used'] / 1024, 4).' KB';
276                     } else {
277                         $emails[$i]['used'] = round($emails[$i]['used'] / 1048576, 4).' MB';
278                     }
e8b329 279                 }
DM 280             }
281         }
282         
283         return $emails;
284     }
e9a084 285     
D 286     public function get_databasequota_data($clientid = null, $readable = true) {
287         global $app;
288     
289         $tmp_rec =  $app->db->queryAllRecords("SELECT data from monitor_data WHERE type = 'database_size' ORDER BY created DESC");
290         $monitor_data = array();
291         if(is_array($tmp_rec)) {
292             foreach ($tmp_rec as $tmp_mon) {
293                 $tmp_array = unserialize($app->db->unquote($tmp_mon['data']));
294                 if(is_array($tmp_array)) {
295                     foreach($tmp_array as $key => $data) {
296                         if(!isset($monitor_data[$data['database_name']]['size'])) $monitor_data[$data['database_name']]['size'] = $data['size'];
297                     }
298                 }
299             }
300         }
301         //print_r($monitor_data);
302     
303         // select all databases belonging to client
304         $databases = $app->db->queryAllRecords("SELECT * FROM web_database".(($clientid != null)? " WHERE sys_groupid = (SELECT default_group FROM sys_user WHERE client_id=?)" : ''), $clientid);
305     
306         //print_r($databases);
307         if(is_array($databases) && !empty($databases)){
308             for($i=0;$i<sizeof($databases);$i++){
309                 $databasename = $databases[$i]['database_name'];
310     
311                 $databases[$i]['used'] = isset($monitor_data[$databasename]['size']) ? $monitor_data[$databasename]['size'] : 0;
312     
313                 $databases[$i]['quota_raw'] = $databases[$i]['database_quota'];
07235f 314                 $databases[$i]['used_raw'] = $databases[$i]['used'] / 1024 / 1024; //* quota is stored as MB - calculated bytes
116598 315                 $databases[$i]['used_percentage'] = (($databases[$i]['database_quota'] > 0) && ($databases[$i]['used'] > 0)) ? round($databases[$i]['used_raw'] * 100 / $databases[$i]['database_quota']) : 0;
e9a084 316     
D 317                 if ($readable) {
318                     // colours
319                     $databases[$i]['display_colour'] = '#000000';
320                     if($databases[$i]['database_quota'] > 0){
116598 321                         $used_ratio = $databases[$i]['used'] / $databases[$i]['database_quota'];
e9a084 322                     } else {
D 323                         $used_ratio = 0;
324                     }
325                     if($used_ratio >= 0.8) $databases[$i]['display_colour'] = '#fd934f';
326                     if($used_ratio >= 1) $databases[$i]['display_colour'] = '#cc0000';
327                         
328                     if($databases[$i]['database_quota'] == 0){
329                         $databases[$i]['database_quota'] = $app->lng('unlimited');
330                     } else {
07235f 331                         $databases[$i]['database_quota'] = $databases[$i]['database_quota'] . ' MB';
e9a084 332                     }
D 333                         
334                         
335                     if($databases[$i]['used'] < 1544000) {
336                         $databases[$i]['used'] = round($databases[$i]['used'] / 1024, 4).' KB';
337                     } else {
338                         $databases[$i]['used'] = round($databases[$i]['used'] / 1048576, 4).' MB';
339                     }
340                 }
341             }
342         }
343     
344         return $databases;
345     }
346     
07235f 347 }