"Fossies" - the Fresh Open Source Software Archive

Member "ispconfig3_install/interface/lib/classes/quota_lib.inc.php" (8 Jun 2021, 14917 Bytes) of package /linux/privat/ISPConfig-3.2.5.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "quota_lib.inc.php": 3.2.4_vs_3.2.5.

    1 <?php
    2 
    3 class quota_lib {
    4     public function get_quota_data($clientid = null, $readable = true) {
    5         global $app;
    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 
   16         // select all websites or websites belonging to client
   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=?)":'') . " ORDER BY domain", $clientid);
   18 
   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];
   38 
   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 
   45                 if ($readable) {
   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, 1).' 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, 1).' 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, 1).' MB';
   70                     } else {
   71                         $sites[$i]['hard'] .= ' KB';
   72                     }
   73 
   74                     if($sites[$i]['soft'] == " KB") $sites[$i]['soft'] = $app->lng('unlimited_txt');
   75                     if($sites[$i]['hard'] == " KB") $sites[$i]['hard'] = $app->lng('unlimited_txt');
   76 
   77                     if($sites[$i]['soft'] == '0 B' || $sites[$i]['soft'] == '0 KB' || $sites[$i]['soft'] == '0') $sites[$i]['soft'] = $app->lng('unlimited_txt');
   78                     if($sites[$i]['hard'] == '0 B' || $sites[$i]['hard'] == '0 KB' || $sites[$i]['hard'] == '0') $sites[$i]['hard'] = $app->lng('unlimited_txt');
   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     }
   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=?)";
  107         }
  108         $sites = $app->db->queryAllRecords("SELECT * FROM web_domain WHERE active = 'y' AND (type = 'vhost' OR type = 'vhostsubdomain' OR type = 'vhostalias')".$sql_where, $clientid);
  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 ? GROUP BY hostname", $tmp_year, $tmp_month, $hostnames);
  124             foreach ($tmp_recs as $tmp_rec) {
  125                 $traffic_data[$tmp_rec['hostname']]['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 ? GROUP BY hostname", $tmp_year, $hostnames);
  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 ? GROUP BY hostname", $tmp_year, $tmp_month, $hostnames);
  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 ? GROUP BY hostname", $tmp_year, $hostnames);
  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 ? DAY)) AND hostname IN ? GROUP BY hostname", $lastdays, $hostnames);
  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 
  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     }
  220 
  221     public function get_mailquota_data($clientid = null, $readable = true) {
  222         global $app;
  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 
  239         // select all email accounts or email accounts belonging to client
  240         $emails = $app->db->queryAllRecords("SELECT * FROM mail_user".(($clientid != null)? " WHERE sys_groupid = (SELECT default_group FROM sys_user WHERE client_id=?)" : '') . " ORDER BY email", $clientid);
  241 
  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]['name'] = $app->functions->htmlentities($emails[$i]['name']);
  248                 $emails[$i]['used'] = isset($monitor_data[$email]['used']) ? $monitor_data[$email]['used'] : array(1 => 0);
  249 
  250                 if (!is_numeric($emails[$i]['used'])) $emails[$i]['used']=$emails[$i]['used'][1];
  251 
  252                 $emails[$i]['quota_raw'] = $emails[$i]['quota'];
  253                 $emails[$i]['used_raw'] = $emails[$i]['used'];
  254                 $emails[$i]['used_percentage'] = ($emails[$i]['quota'] > 0 && $emails[$i]['used'] > 0 ? round($emails[$i]['used'] * 100 / $emails[$i]['quota']) : 0);
  255 
  256 
  257                 if ($readable) {
  258                     // colours
  259                     $emails[$i]['display_colour'] = '#000000';
  260                     if($emails[$i]['quota'] > 0){
  261                         $used_ratio = $emails[$i]['used']/$emails[$i]['quota'];
  262                     } else {
  263                         $used_ratio = 0;
  264                     }
  265                     if($used_ratio >= 0.8) $emails[$i]['display_colour'] = '#fd934f';
  266                     if($used_ratio >= 1) $emails[$i]['display_colour'] = '#cc0000';
  267 
  268                     if($emails[$i]['quota'] == 0){
  269                         $emails[$i]['quota'] = $app->lng('unlimited_txt');
  270                     } else {
  271                                                $emails[$i]['quota'] = round($emails[$i]['quota'] / 1048576, 1).' MB';
  272                     }
  273 
  274 
  275                     if($emails[$i]['used'] < 1544000) {
  276                                                $emails[$i]['used'] = round($emails[$i]['used'] / 1024, 1).' KB';
  277                     } else {
  278                                                $emails[$i]['used'] = round($emails[$i]['used'] / 1048576, 1).' MB';
  279                     }
  280                 }
  281             }
  282         }
  283 
  284         return $emails;
  285     }
  286 
  287     public function get_databasequota_data($clientid = null, $readable = true) {
  288         global $app;
  289 
  290         $tmp_rec =  $app->db->queryAllRecords("SELECT data from monitor_data WHERE type = 'database_size' ORDER BY created DESC");
  291         $monitor_data = array();
  292         if(is_array($tmp_rec)) {
  293             foreach ($tmp_rec as $tmp_mon) {
  294                 $tmp_array = unserialize($app->db->unquote($tmp_mon['data']));
  295                 if(is_array($tmp_array)) {
  296                     foreach($tmp_array as $key => $data) {
  297                         if(!isset($monitor_data[$data['database_name']]['size'])) $monitor_data[$data['database_name']]['size'] = $data['size'];
  298                     }
  299                 }
  300             }
  301         }
  302         //print_r($monitor_data);
  303 
  304         // select all databases belonging to client
  305         $databases = $app->db->queryAllRecords("SELECT * FROM web_database".(($clientid != null)? " WHERE sys_groupid = (SELECT default_group FROM sys_user WHERE client_id=?)" : '') . " ORDER BY database_name", $clientid);
  306 
  307         //print_r($databases);
  308         if(is_array($databases) && !empty($databases)){
  309             for($i=0;$i<sizeof($databases);$i++){
  310                 $databasename = $databases[$i]['database_name'];
  311 
  312                 $databases[$i]['used'] = isset($monitor_data[$databasename]['size']) ? $monitor_data[$databasename]['size'] : 0;
  313 
  314                 $databases[$i]['quota_raw'] = $databases[$i]['database_quota'];
  315                 $databases[$i]['used_raw'] = $databases[$i]['used'] / 1024 / 1024; //* quota is stored as MB - calculated bytes
  316                 $databases[$i]['used_percentage'] = (($databases[$i]['database_quota'] > 0) && ($databases[$i]['used'] > 0)) ? round($databases[$i]['used_raw'] * 100 / $databases[$i]['database_quota']) : 0;
  317 
  318                 if ($readable) {
  319                     // colours
  320                     $databases[$i]['display_colour'] = '#000000';
  321                     if($databases[$i]['database_quota'] > 0){
  322                         $used_ratio = $databases[$i]['used'] / $databases[$i]['database_quota'];
  323                     } else {
  324                         $used_ratio = 0;
  325                     }
  326                     if($used_ratio >= 0.8) $databases[$i]['display_colour'] = '#fd934f';
  327                     if($used_ratio >= 1) $databases[$i]['display_colour'] = '#cc0000';
  328 
  329                     if($databases[$i]['database_quota'] == 0){
  330                         $databases[$i]['database_quota'] = $app->lng('unlimited_txt');
  331                     } else {
  332                         $databases[$i]['database_quota'] = $databases[$i]['database_quota'] . ' MB';
  333                     }
  334 
  335 
  336                     if($databases[$i]['used'] < 1544000) {
  337                         $databases[$i]['used'] = round($databases[$i]['used'] / 1024, 1).' KB';
  338                     } else {
  339                         $databases[$i]['used'] = round($databases[$i]['used'] / 1048576, 1).' MB';
  340                     }
  341                 }
  342             }
  343         }
  344 
  345         return $databases;
  346     }
  347 
  348 }