From 1c369e550338bc35955e93b0e2b228b15f6ace38 Mon Sep 17 00:00:00 2001 From: "Michael Kaufmann (d00p)" Date: Thu, 14 Jul 2016 07:58:56 +0200 Subject: [PATCH] add possibility to disable web or traffic cron by setting the max-percentage value to 0, fixes #1639 Signed-off-by: Michael Kaufmann (d00p) --- actions/admin/settings/120.system.php | 4 +- lng/english.lng.php | 6 +- lng/german.lng.php | 6 +- scripts/jobs/cron_usage.inc.diskspace.php | 355 ++++++++------- scripts/jobs/cron_usage_report.php | 532 +++++++++++----------- 5 files changed, 456 insertions(+), 447 deletions(-) diff --git a/actions/admin/settings/120.system.php b/actions/admin/settings/120.system.php index 46faefcf..810c931e 100644 --- a/actions/admin/settings/120.system.php +++ b/actions/admin/settings/120.system.php @@ -145,7 +145,7 @@ return array( 'settinggroup' => 'system', 'varname' => 'report_webmax', 'type' => 'int', - 'int_min' => 1, + 'int_min' => 0, 'int_max' => 150, 'default' => 90, 'save_method' => 'storeSettingField', @@ -155,7 +155,7 @@ return array( 'settinggroup' => 'system', 'varname' => 'report_trafficmax', 'type' => 'int', - 'int_min' => 1, + 'int_min' => 0, 'int_max' => 150, 'default' => 90, 'save_method' => 'storeSettingField', diff --git a/lng/english.lng.php b/lng/english.lng.php index 98b58f1a..c0d1d49b 100644 --- a/lng/english.lng.php +++ b/lng/english.lng.php @@ -1314,8 +1314,10 @@ $lng['phpfpm']['ownvhost']['description'] = 'If enabled, Froxlor will also be ru // ADDED IN FROXLOR 0.9.17 $lng['crondesc']['cron_usage_report'] = 'Web- and traffic-reports'; $lng['serversettings']['report']['report'] = 'Enable sending of reports about web- and traffic-usage'; -$lng['serversettings']['report']['webmax'] = 'Warning-level in percent for webspace'; -$lng['serversettings']['report']['trafficmax'] = 'Warning-level in percent for traffic'; +$lng['serversettings']['report']['webmax']['title'] = 'Warning-level in percent for webspace'; +$lng['serversettings']['report']['webmax']['description'] = 'Valid values are 0 up to 150. Setting this value to 0 disables this report.'; +$lng['serversettings']['report']['trafficmax']['title'] = 'Warning-level in percent for traffic'; +$lng['serversettings']['report']['trafficmax']['description'] = 'Valid values are 0 up to 150. Setting this value to 0 disables this report.'; $lng['mails']['trafficmaxpercent']['mailbody'] = 'Dear {NAME},\n\nyou used {TRAFFICUSED} MB of your available {TRAFFIC} MB of traffic.\nThis is more than {MAX_PERCENT}%.\n\nYours sincerely, your administrator'; $lng['mails']['trafficmaxpercent']['subject'] = 'Reaching your traffic limit'; $lng['admin']['templates']['trafficmaxpercent'] = 'Notification mail for customers when given maximum of percent of traffic is exhausted'; diff --git a/lng/german.lng.php b/lng/german.lng.php index f9397b1f..16b05c54 100644 --- a/lng/german.lng.php +++ b/lng/german.lng.php @@ -1293,8 +1293,10 @@ $lng['phpfpm']['ownvhost']['description'] = 'Wenn verwendet, wird Froxlor selbst // ADDED IN FROXLOR 0.9.17 $lng['crondesc']['cron_usage_report'] = 'Webspace- und Trafficreport'; $lng['serversettings']['report']['report'] = 'Aktiviere das Senden von Reports über Webspace- und Trafficverbrauch'; -$lng['serversettings']['report']['webmax'] = 'Warn-Level in Prozent für Webspace'; -$lng['serversettings']['report']['trafficmax'] = 'Warn-Level in Prozent für Traffic'; +$lng['serversettings']['report']['webmax']['title'] = 'Warn-Level in Prozent für Webspace'; +$lng['serversettings']['report']['webmax']['description'] = 'Gültige Werte sind von 0 bis 150. Der Wert 0 deaktiviert diesen Report.'; +$lng['serversettings']['report']['trafficmax']['title'] = 'Warn-Level in Prozent für Traffic'; +$lng['serversettings']['report']['trafficmax']['description'] = 'Gültige Werte sind von 0 bis 150. Der Wert 0 deaktiviert diesen Report.'; $lng['mails']['trafficmaxpercent']['mailbody'] = 'Sehr geehrte(r) {NAME},\n\nSie haben bereits {TRAFFICUSED} MB von Ihren insgesamt {TRAFFIC} MB Traffic verbraucht.\nDies sind mehr als {MAX_PERCENT}%.\n\nVielen Dank,\nIhr Administrator'; $lng['mails']['trafficmaxpercent']['subject'] = 'Sie erreichen bald Ihr Traffic-Limit'; $lng['admin']['templates']['trafficmaxpercent'] = 'Hinweismail für Kunden, wenn sie die angegebenen Prozent des Traffics verbraucht haben'; diff --git a/scripts/jobs/cron_usage.inc.diskspace.php b/scripts/jobs/cron_usage.inc.diskspace.php index 2c74ef64..045ae9a2 100644 --- a/scripts/jobs/cron_usage.inc.diskspace.php +++ b/scripts/jobs/cron_usage.inc.diskspace.php @@ -15,194 +15,197 @@ * */ -/** - * report about diskusage for customers - */ -$result_stmt = Database::query(" - SELECT `c`.`customerid`, `c`.`adminid`, `c`.`name`, `c`.`firstname`, - `c`.`company`, `c`.`diskspace`, `c`.`diskspace_used`, `c`.`email`, `c`.`def_language`, - `a`.`name` AS `adminname`, `a`.`email` AS `adminmail` - FROM `" . TABLE_PANEL_CUSTOMERS . "` AS `c` - LEFT JOIN `" . TABLE_PANEL_ADMINS . "` AS `a` - ON `a`.`adminid` = `c`.`adminid` - WHERE `c`.`diskspace` > '0' AND `c`.`reportsent` <> '2' -"); +if ((int)Settings::Get('system.report_webmax') > 0) +{ + /** + * report about diskusage for customers + */ + $result_stmt = Database::query(" + SELECT `c`.`customerid`, `c`.`adminid`, `c`.`name`, `c`.`firstname`, + `c`.`company`, `c`.`diskspace`, `c`.`diskspace_used`, `c`.`email`, `c`.`def_language`, + `a`.`name` AS `adminname`, `a`.`email` AS `adminmail` + FROM `" . TABLE_PANEL_CUSTOMERS . "` AS `c` + LEFT JOIN `" . TABLE_PANEL_ADMINS . "` AS `a` + ON `a`.`adminid` = `c`.`adminid` + WHERE `c`.`diskspace` > '0' AND `c`.`reportsent` <> '2' + "); -while ($row = $result_stmt->fetch(PDO::FETCH_ASSOC)) { + while ($row = $result_stmt->fetch(PDO::FETCH_ASSOC)) { - if (isset($row['diskspace']) - && $row['diskspace_used'] != null - && $row['diskspace_used'] > 0 - && (($row['diskspace_used'] * 100) / $row['diskspace']) >= (int)Settings::Get('system.report_webmax') - ) { + if (isset($row['diskspace']) + && $row['diskspace_used'] != null + && $row['diskspace_used'] > 0 + && (($row['diskspace_used'] * 100) / $row['diskspace']) >= (int)Settings::Get('system.report_webmax') + ) { - $rep_userinfo = array( - 'name' => $row['name'], - 'firstname' => $row['firstname'], - 'company' => $row['company'] - ); - $replace_arr = array( - 'SALUTATION' => getCorrectUserSalutation($rep_userinfo), - 'NAME' => $row['name'], // < keep this for compatibility - 'DISKAVAILABLE' => round(($row['diskspace'] / 1024), 2), /* traffic is stored in KB, template uses MB */ - 'DISKUSED' => round($row['diskspace_used'] / 1024, 2), /* traffic is stored in KB, template uses MB */ - 'USAGE_PERCENT' => round(($row['diskspace_used'] * 100) / $row['diskspace'], 2), - 'MAX_PERCENT' => Settings::Get('system.report_webmax') - ); + $rep_userinfo = array( + 'name' => $row['name'], + 'firstname' => $row['firstname'], + 'company' => $row['company'] + ); + $replace_arr = array( + 'SALUTATION' => getCorrectUserSalutation($rep_userinfo), + 'NAME' => $row['name'], // < keep this for compatibility + 'DISKAVAILABLE' => round(($row['diskspace'] / 1024), 2), /* traffic is stored in KB, template uses MB */ + 'DISKUSED' => round($row['diskspace_used'] / 1024, 2), /* traffic is stored in KB, template uses MB */ + 'USAGE_PERCENT' => round(($row['diskspace_used'] * 100) / $row['diskspace'], 2), + 'MAX_PERCENT' => Settings::Get('system.report_webmax') + ); - $lngfile_stmt = Database::prepare(" - SELECT `file` FROM `" . TABLE_PANEL_LANGUAGE . "` - WHERE `language` = :deflang - "); - $lngfile = Database::pexecute_first($lngfile_stmt, array('deflang' => $row['def_language'])); + $lngfile_stmt = Database::prepare(" + SELECT `file` FROM `" . TABLE_PANEL_LANGUAGE . "` + WHERE `language` = :deflang + "); + $lngfile = Database::pexecute_first($lngfile_stmt, array('deflang' => $row['def_language'])); - if ($lngfile !== null) { - $langfile = $lngfile['file']; - } else { - $lngfile = Database::pexecute_first($lngfile_stmt, array('deflang' => Settings::Get('panel.standardlanguage'))); - $langfile = $lngfile['file']; + if ($lngfile !== null) { + $langfile = $lngfile['file']; + } else { + $lngfile = Database::pexecute_first($lngfile_stmt, array('deflang' => Settings::Get('panel.standardlanguage'))); + $langfile = $lngfile['file']; + } + + // include english language file (fallback) + include_once makeCorrectFile(FROXLOR_INSTALL_DIR . '/lng/english.lng.php'); + // include admin/customer language file + include_once makeCorrectFile(FROXLOR_INSTALL_DIR . '/' . $langfile); + + // Get mail templates from database; the ones from 'admin' are fetched for fallback + $result2_stmt = Database::prepare(" + SELECT `value` FROM `" . TABLE_PANEL_TEMPLATES . "` + WHERE `adminid` = :adminid + AND `language` = :lang + AND `templategroup` = 'mails' AND `varname` = :varname + "); + $result2_data = array( + 'adminid' => $row['adminid'], + 'lang' => $row['def_language'], + 'varname' => 'diskmaxpercent_subject' + ); + $result2 = Database::pexecute_first($result2_stmt, $result2_data); + $mail_subject = html_entity_decode(replace_variables((($result2['value'] != '') ? $result2['value'] : $lng['mails']['diskmaxpercent']['subject']), $replace_arr)); + + $result2_data['varname'] = 'diskmaxpercent_mailbody'; + $result2 = Database::pexecute_first($result2_stmt, $result2_data); + $mail_body = html_entity_decode(replace_variables((($result2['value'] != '') ? $result2['value'] : $lng['mails']['diskmaxpercent']['mailbody']), $replace_arr)); + + $_mailerror = false; + try { + $mail->SetFrom($row['adminmail'], $row['adminname']); + $mail->Subject = $mail_subject; + $mail->AltBody = $mail_body; + $mail->MsgHTML(nl2br($mail_body)); + $mail->AddAddress($row['email'], $row['name']); + $mail->Send(); + } catch(phpmailerException $e) { + $mailerr_msg = $e->errorMessage(); + $_mailerror = true; + } catch (Exception $e) { + $mailerr_msg = $e->getMessage(); + $_mailerror = true; + } + + if ($_mailerror) { + $cronlog->logAction(CRON_ACTION, LOG_ERR, "Error sending mail: " . $mailerr_msg); + echo "Error sending mail: " . $mailerr_msg . "\n"; + } + + $mail->ClearAddresses(); + $upd_stmt = Database::prepare(" + UPDATE `" . TABLE_PANEL_CUSTOMERS . "` SET `reportsent` = '2' + WHERE `customerid` = :customerid + "); + Database::pexecute($upd_stmt, array('customerid' => $row['customerid'])); } - - // include english language file (fallback) - include_once makeCorrectFile(FROXLOR_INSTALL_DIR . '/lng/english.lng.php'); - // include admin/customer language file - include_once makeCorrectFile(FROXLOR_INSTALL_DIR . '/' . $langfile); - - // Get mail templates from database; the ones from 'admin' are fetched for fallback - $result2_stmt = Database::prepare(" - SELECT `value` FROM `" . TABLE_PANEL_TEMPLATES . "` - WHERE `adminid` = :adminid - AND `language` = :lang - AND `templategroup` = 'mails' AND `varname` = :varname - "); - $result2_data = array( - 'adminid' => $row['adminid'], - 'lang' => $row['def_language'], - 'varname' => 'diskmaxpercent_subject' - ); - $result2 = Database::pexecute_first($result2_stmt, $result2_data); - $mail_subject = html_entity_decode(replace_variables((($result2['value'] != '') ? $result2['value'] : $lng['mails']['diskmaxpercent']['subject']), $replace_arr)); - - $result2_data['varname'] = 'diskmaxpercent_mailbody'; - $result2 = Database::pexecute_first($result2_stmt, $result2_data); - $mail_body = html_entity_decode(replace_variables((($result2['value'] != '') ? $result2['value'] : $lng['mails']['diskmaxpercent']['mailbody']), $replace_arr)); - - $_mailerror = false; - try { - $mail->SetFrom($row['adminmail'], $row['adminname']); - $mail->Subject = $mail_subject; - $mail->AltBody = $mail_body; - $mail->MsgHTML(nl2br($mail_body)); - $mail->AddAddress($row['email'], $row['name']); - $mail->Send(); - } catch(phpmailerException $e) { - $mailerr_msg = $e->errorMessage(); - $_mailerror = true; - } catch (Exception $e) { - $mailerr_msg = $e->getMessage(); - $_mailerror = true; - } - - if ($_mailerror) { - $cronlog->logAction(CRON_ACTION, LOG_ERR, "Error sending mail: " . $mailerr_msg); - echo "Error sending mail: " . $mailerr_msg . "\n"; - } - - $mail->ClearAddresses(); - $upd_stmt = Database::prepare(" - UPDATE `" . TABLE_PANEL_CUSTOMERS . "` SET `reportsent` = '2' - WHERE `customerid` = :customerid - "); - Database::pexecute($upd_stmt, array('customerid' => $row['customerid'])); } -} -/** - * report about diskusage for admins/reseller - */ -$result_stmt = Database::query(" - SELECT `a`.* FROM `" . TABLE_PANEL_ADMINS . "` `a` WHERE `a`.`reportsent` <> '2' -"); + /** + * report about diskusage for admins/reseller + */ + $result_stmt = Database::query(" + SELECT `a`.* FROM `" . TABLE_PANEL_ADMINS . "` `a` WHERE `a`.`reportsent` <> '2' + "); -while ($row = $result_stmt->fetch(PDO::FETCH_ASSOC)) { + while ($row = $result_stmt->fetch(PDO::FETCH_ASSOC)) { - if (isset($row['diskspace']) - && $row['diskspace_used'] != null - && $row['diskspace_used'] > 0 - && (($row['diskspace_used'] * 100) / $row['diskspace']) >= (int)Settings::Get('system.report_webmax') - ) { + if (isset($row['diskspace']) + && $row['diskspace_used'] != null + && $row['diskspace_used'] > 0 + && (($row['diskspace_used'] * 100) / $row['diskspace']) >= (int)Settings::Get('system.report_webmax') + ) { - $replace_arr = array( - 'NAME' => $row['name'], - 'DISKAVAILABLE' => ($row['diskspace'] / 1024), /* traffic is stored in KB, template uses MB */ - 'DISKUSED' => round($row['diskspace_used'] / 1024, 2), /* traffic is stored in KB, template uses MB */ - 'USAGE_PERCENT' => ($row['diskspace_used'] * 100) / $row['diskspace'], - 'MAX_PERCENT' => Settings::Get('system.report_webmax') - ); + $replace_arr = array( + 'NAME' => $row['name'], + 'DISKAVAILABLE' => ($row['diskspace'] / 1024), /* traffic is stored in KB, template uses MB */ + 'DISKUSED' => round($row['diskspace_used'] / 1024, 2), /* traffic is stored in KB, template uses MB */ + 'USAGE_PERCENT' => ($row['diskspace_used'] * 100) / $row['diskspace'], + 'MAX_PERCENT' => Settings::Get('system.report_webmax') + ); - $lngfile_stmt = Database::prepare(" - SELECT `file` FROM `" . TABLE_PANEL_LANGUAGE . "` - WHERE `language` = :deflang - "); - $lngfile = Database::pexecute_first($lngfile_stmt, array('deflang' => $row['def_language'])); + $lngfile_stmt = Database::prepare(" + SELECT `file` FROM `" . TABLE_PANEL_LANGUAGE . "` + WHERE `language` = :deflang + "); + $lngfile = Database::pexecute_first($lngfile_stmt, array('deflang' => $row['def_language'])); - if ($lngfile !== null) { - $langfile = $lngfile['file']; - } else { - $lngfile = Database::pexecute_first($lngfile_stmt, array('deflang' => Settings::Get('panel.standardlanguage'))); - $langfile = $lngfile['file']; + if ($lngfile !== null) { + $langfile = $lngfile['file']; + } else { + $lngfile = Database::pexecute_first($lngfile_stmt, array('deflang' => Settings::Get('panel.standardlanguage'))); + $langfile = $lngfile['file']; + } + + // include english language file (fallback) + include_once makeCorrectFile(FROXLOR_INSTALL_DIR . '/lng/english.lng.php'); + // include admin/customer language file + include_once makeCorrectFile(FROXLOR_INSTALL_DIR . '/' . $langfile); + + // Get mail templates from database; the ones from 'admin' are fetched for fallback + $result2_stmt = Database::prepare(" + SELECT `value` FROM `" . TABLE_PANEL_TEMPLATES . "` + WHERE `adminid` = :adminid + AND `language` = :lang + AND `templategroup` = 'mails' AND `varname` = :varname + "); + $result2_data = array( + 'adminid' => $row['adminid'], + 'lang' => $row['def_language'], + 'varname' => 'diskmaxpercent_subject' + ); + $result2 = Database::pexecute_first($result2_stmt, $result2_data); + $mail_subject = html_entity_decode(replace_variables((($result2['value'] != '') ? $result2['value'] : $lng['mails']['diskmaxpercent']['subject']), $replace_arr)); + + $result2_data['varname'] = 'diskmaxpercent_mailbody'; + $result2 = Database::pexecute_first($result2_stmt, $result2_data); + $mail_body = html_entity_decode(replace_variables((($result2['value'] != '') ? $result2['value'] : $lng['mails']['diskmaxpercent']['mailbody']), $replace_arr)); + + $_mailerror = false; + try { + $mail->SetFrom($row['email'], $row['name']); + $mail->Subject = $mail_subject; + $mail->AltBody = $mail_body; + $mail->MsgHTML(nl2br($mail_body)); + $mail->AddAddress($row['email'], $row['name']); + $mail->Send(); + } catch(phpmailerException $e) { + $mailerr_msg = $e->errorMessage(); + $_mailerror = true; + } catch (Exception $e) { + $mailerr_msg = $e->getMessage(); + $_mailerror = true; + } + + if ($_mailerror) { + $cronlog->logAction(CRON_ACTION, LOG_ERR, "Error sending mail: " . $mailerr_msg); + echo "Error sending mail: " . $mailerr_msg . "\n"; + } + + $mail->ClearAddresses(); + $upd_stmt = Database::prepare(" + UPDATE `" . TABLE_PANEL_ADMINS . "` SET `reportsent` = '2' + WHERE `adminid` = :adminid + "); + Database::pexecute($upd_stmt, array('adminid' => $row['adminid'])); } - - // include english language file (fallback) - include_once makeCorrectFile(FROXLOR_INSTALL_DIR . '/lng/english.lng.php'); - // include admin/customer language file - include_once makeCorrectFile(FROXLOR_INSTALL_DIR . '/' . $langfile); - - // Get mail templates from database; the ones from 'admin' are fetched for fallback - $result2_stmt = Database::prepare(" - SELECT `value` FROM `" . TABLE_PANEL_TEMPLATES . "` - WHERE `adminid` = :adminid - AND `language` = :lang - AND `templategroup` = 'mails' AND `varname` = :varname - "); - $result2_data = array( - 'adminid' => $row['adminid'], - 'lang' => $row['def_language'], - 'varname' => 'diskmaxpercent_subject' - ); - $result2 = Database::pexecute_first($result2_stmt, $result2_data); - $mail_subject = html_entity_decode(replace_variables((($result2['value'] != '') ? $result2['value'] : $lng['mails']['diskmaxpercent']['subject']), $replace_arr)); - - $result2_data['varname'] = 'diskmaxpercent_mailbody'; - $result2 = Database::pexecute_first($result2_stmt, $result2_data); - $mail_body = html_entity_decode(replace_variables((($result2['value'] != '') ? $result2['value'] : $lng['mails']['diskmaxpercent']['mailbody']), $replace_arr)); - - $_mailerror = false; - try { - $mail->SetFrom($row['email'], $row['name']); - $mail->Subject = $mail_subject; - $mail->AltBody = $mail_body; - $mail->MsgHTML(nl2br($mail_body)); - $mail->AddAddress($row['email'], $row['name']); - $mail->Send(); - } catch(phpmailerException $e) { - $mailerr_msg = $e->errorMessage(); - $_mailerror = true; - } catch (Exception $e) { - $mailerr_msg = $e->getMessage(); - $_mailerror = true; - } - - if ($_mailerror) { - $cronlog->logAction(CRON_ACTION, LOG_ERR, "Error sending mail: " . $mailerr_msg); - echo "Error sending mail: " . $mailerr_msg . "\n"; - } - - $mail->ClearAddresses(); - $upd_stmt = Database::prepare(" - UPDATE `" . TABLE_PANEL_ADMINS . "` SET `reportsent` = '2' - WHERE `adminid` = :adminid - "); - Database::pexecute($upd_stmt, array('adminid' => $row['adminid'])); } -} +} // webmax > 0 diff --git a/scripts/jobs/cron_usage_report.php b/scripts/jobs/cron_usage_report.php index 60d9cc21..8de32c71 100644 --- a/scripts/jobs/cron_usage_report.php +++ b/scripts/jobs/cron_usage_report.php @@ -34,289 +34,291 @@ if (PHPMailer::ValidateAddress(Settings::Get('panel.adminmail')) !== false) { } } -// Warn the customers at xx% traffic-usage +if ((int)Settings::Get('system.report_trafficmax') > 0) +{ + // Warn the customers at xx% traffic-usage + $result_stmt = Database::prepare(" + SELECT `c`.`customerid`, `c`.`adminid`, `c`.`name`, `c`.`firstname`, + `c`.`company`, `c`.`traffic`, `c`.`email`, `c`.`def_language`, + `a`.`name` AS `adminname`, `a`.`email` AS `adminmail`, + (SELECT SUM(`t`.`http` + `t`.`ftp_up` + `t`.`ftp_down` + `t`.`mail`) + FROM `" . TABLE_PANEL_TRAFFIC . "` `t` + WHERE `t`.`customerid` = `c`.`customerid` AND `t`.`year` = :year AND `t`.`month` = :month + ) as `traffic_used` + FROM `" . TABLE_PANEL_CUSTOMERS . "` AS `c` + LEFT JOIN `" . TABLE_PANEL_ADMINS . "` AS `a` + ON `a`.`adminid` = `c`.`adminid` WHERE `c`.`reportsent` <> '1' + "); -$result_stmt = Database::prepare(" - SELECT `c`.`customerid`, `c`.`adminid`, `c`.`name`, `c`.`firstname`, - `c`.`company`, `c`.`traffic`, `c`.`email`, `c`.`def_language`, - `a`.`name` AS `adminname`, `a`.`email` AS `adminmail`, - (SELECT SUM(`t`.`http` + `t`.`ftp_up` + `t`.`ftp_down` + `t`.`mail`) - FROM `" . TABLE_PANEL_TRAFFIC . "` `t` - WHERE `t`.`customerid` = `c`.`customerid` AND `t`.`year` = :year AND `t`.`month` = :month - ) as `traffic_used` - FROM `" . TABLE_PANEL_CUSTOMERS . "` AS `c` - LEFT JOIN `" . TABLE_PANEL_ADMINS . "` AS `a` - ON `a`.`adminid` = `c`.`adminid` WHERE `c`.`reportsent` <> '1' -"); + $result_data = array( + 'year' => date("Y", $yesterday), + 'month' => date("m", $yesterday) + ); + Database::pexecute($result_stmt, $result_data); -$result_data = array( - 'year' => date("Y", $yesterday), - 'month' => date("m", $yesterday) -); -Database::pexecute($result_stmt, $result_data); + while ($row = $result_stmt->fetch(PDO::FETCH_ASSOC)) { -while ($row = $result_stmt->fetch(PDO::FETCH_ASSOC)) { + if (isset($row['traffic']) + && $row['traffic'] > 0 + && $row['traffic_used'] != null + && (($row['traffic_used'] * 100) / $row['traffic']) >= (int)Settings::Get('system.report_trafficmax') + ) { + $rep_userinfo = array( + 'name' => $row['name'], + 'firstname' => $row['firstname'], + 'company' => $row['company'] + ); + $replace_arr = array( + 'SALUTATION' => getCorrectUserSalutation($rep_userinfo), + 'NAME' => $row['name'], // < keep this for compatibility + 'TRAFFIC' => round(($row['traffic'] / 1024), 2), /* traffic is stored in KB, template uses MB */ + 'TRAFFICUSED' => round(($row['traffic_used'] / 1024), 2), /* traffic is stored in KB, template uses MB */ + 'USAGE_PERCENT' => round(($row['traffic_used'] * 100) / $row['traffic'], 2), + 'MAX_PERCENT' => Settings::Get('system.report_trafficmax') + ); - if (isset($row['traffic']) - && $row['traffic'] > 0 - && $row['traffic_used'] != null - && (($row['traffic_used'] * 100) / $row['traffic']) >= (int)Settings::Get('system.report_trafficmax') - ) { - $rep_userinfo = array( - 'name' => $row['name'], - 'firstname' => $row['firstname'], - 'company' => $row['company'] - ); - $replace_arr = array( - 'SALUTATION' => getCorrectUserSalutation($rep_userinfo), - 'NAME' => $row['name'], // < keep this for compatibility - 'TRAFFIC' => round(($row['traffic'] / 1024), 2), /* traffic is stored in KB, template uses MB */ - 'TRAFFICUSED' => round(($row['traffic_used'] / 1024), 2), /* traffic is stored in KB, template uses MB */ - 'USAGE_PERCENT' => round(($row['traffic_used'] * 100) / $row['traffic'], 2), - 'MAX_PERCENT' => Settings::Get('system.report_trafficmax') - ); + $lngfile_stmt = Database::prepare(" + SELECT `file` FROM `" . TABLE_PANEL_LANGUAGE . "` + WHERE `language` = :deflang + "); + $lngfile = Database::pexecute_first($lngfile_stmt, array('deflang' => $row['def_language'])); - $lngfile_stmt = Database::prepare(" - SELECT `file` FROM `" . TABLE_PANEL_LANGUAGE . "` - WHERE `language` = :deflang - "); - $lngfile = Database::pexecute_first($lngfile_stmt, array('deflang' => $row['def_language'])); + if ($lngfile !== null) { + $langfile = $lngfile['file']; + } else { + $lngfile = Database::pexecute_first($lngfile_stmt, array('deflang' => Settings::Get('panel.standardlanguage'))); + $langfile = $lngfile['file']; + } - if ($lngfile !== null) { - $langfile = $lngfile['file']; - } else { - $lngfile = Database::pexecute_first($lngfile_stmt, array('deflang' => Settings::Get('panel.standardlanguage'))); - $langfile = $lngfile['file']; + // include english language file (fallback) + include_once makeCorrectFile(FROXLOR_INSTALL_DIR . '/lng/english.lng.php'); + // include admin/customer language file + include_once makeCorrectFile(FROXLOR_INSTALL_DIR . '/' . $langfile); + + // Get mail templates from database; the ones from 'admin' are fetched for fallback + $result2_stmt = Database::prepare(" + SELECT `value` FROM `" . TABLE_PANEL_TEMPLATES . "` + WHERE `adminid` = :adminid + AND `language` = :lang + AND `templategroup` = 'mails' AND `varname` = :varname + "); + $result2_data = array( + 'adminid' => $row['adminid'], + 'lang' => $row['def_language'], + 'varname' => 'trafficmaxpercent_subject' + ); + $result2 = Database::pexecute_first($result2_stmt, $result2_data); + $mail_subject = html_entity_decode(replace_variables((($result2['value'] != '') ? $result2['value'] : $lng['mails']['trafficmaxpercent']['subject']), $replace_arr)); + + $result2_data['varname'] = 'trafficmaxpercent_mailbody'; + $result2 = Database::pexecute_first($result2_stmt, $result2_data); + $mail_body = html_entity_decode(replace_variables((($result2['value'] != '') ? $result2['value'] : $lng['mails']['trafficmaxpercent']['mailbody']), $replace_arr)); + + $_mailerror = false; + try { + $mail->SetFrom($row['adminmail'], $row['adminname']); + $mail->Subject = $mail_subject; + $mail->AltBody = $mail_body; + $mail->MsgHTML(nl2br($mail_body)); + $mail->AddAddress($row['email'], $row['firstname'] . ' ' . $row['name']); + $mail->Send(); + } catch(phpmailerException $e) { + $mailerr_msg = $e->errorMessage(); + $_mailerror = true; + } catch (Exception $e) { + $mailerr_msg = $e->getMessage(); + $_mailerror = true; + } + + if ($_mailerror) { + $cronlog->logAction(CRON_ACTION, LOG_ERR, 'Error sending mail: ' . $mailerr_msg); + echo 'Error sending mail: ' . $mailerr_msg . "\n"; + } + + $mail->ClearAddresses(); + $upd_stmt = Database::prepare(" + UPDATE `" . TABLE_PANEL_CUSTOMERS . "` SET `reportsent` = '1' + WHERE `customerid` = :customerid + "); + Database::pexecute($upd_stmt, array('customerid' => $row['customerid'])); } - - // include english language file (fallback) - include_once makeCorrectFile(FROXLOR_INSTALL_DIR . '/lng/english.lng.php'); - // include admin/customer language file - include_once makeCorrectFile(FROXLOR_INSTALL_DIR . '/' . $langfile); - - // Get mail templates from database; the ones from 'admin' are fetched for fallback - $result2_stmt = Database::prepare(" - SELECT `value` FROM `" . TABLE_PANEL_TEMPLATES . "` - WHERE `adminid` = :adminid - AND `language` = :lang - AND `templategroup` = 'mails' AND `varname` = :varname - "); - $result2_data = array( - 'adminid' => $row['adminid'], - 'lang' => $row['def_language'], - 'varname' => 'trafficmaxpercent_subject' - ); - $result2 = Database::pexecute_first($result2_stmt, $result2_data); - $mail_subject = html_entity_decode(replace_variables((($result2['value'] != '') ? $result2['value'] : $lng['mails']['trafficmaxpercent']['subject']), $replace_arr)); - - $result2_data['varname'] = 'trafficmaxpercent_mailbody'; - $result2 = Database::pexecute_first($result2_stmt, $result2_data); - $mail_body = html_entity_decode(replace_variables((($result2['value'] != '') ? $result2['value'] : $lng['mails']['trafficmaxpercent']['mailbody']), $replace_arr)); - - $_mailerror = false; - try { - $mail->SetFrom($row['adminmail'], $row['adminname']); - $mail->Subject = $mail_subject; - $mail->AltBody = $mail_body; - $mail->MsgHTML(nl2br($mail_body)); - $mail->AddAddress($row['email'], $row['firstname'] . ' ' . $row['name']); - $mail->Send(); - } catch(phpmailerException $e) { - $mailerr_msg = $e->errorMessage(); - $_mailerror = true; - } catch (Exception $e) { - $mailerr_msg = $e->getMessage(); - $_mailerror = true; - } - - if ($_mailerror) { - $cronlog->logAction(CRON_ACTION, LOG_ERR, 'Error sending mail: ' . $mailerr_msg); - echo 'Error sending mail: ' . $mailerr_msg . "\n"; - } - - $mail->ClearAddresses(); - $upd_stmt = Database::prepare(" - UPDATE `" . TABLE_PANEL_CUSTOMERS . "` SET `reportsent` = '1' - WHERE `customerid` = :customerid - "); - Database::pexecute($upd_stmt, array('customerid' => $row['customerid'])); - } -} - -// Warn the admins at xx% traffic-usage -$result_stmt = Database::prepare(" - SELECT `a`.*, - (SELECT SUM(`t`.`http` + `t`.`ftp_up` + `t`.`ftp_down` + `t`.`mail`) - FROM `" . TABLE_PANEL_TRAFFIC_ADMINS . "` `t` - WHERE `t`.`adminid` = `a`.`adminid` AND `t`.`year` = :year AND `t`.`month` = :month - ) as `traffic_used_total` - FROM `" . TABLE_PANEL_ADMINS . "` `a` WHERE `a`.`reportsent` = '0' -"); - -$result_data = array( - 'year' => date("Y", $yesterday), - 'month' => date("m", $yesterday) -); -Database::pexecute($result_stmt, $result_data); - -while ($row = $result_stmt->fetch(PDO::FETCH_ASSOC)) { - - if (isset($row['traffic']) - && $row['traffic'] > 0 - && (($row['traffic_used_total'] * 100) / $row['traffic']) >= (int)Settings::Get('system.report_trafficmax') - ) { - - $replace_arr = array( - 'NAME' => $row['name'], - 'TRAFFIC' => round(($row['traffic'] / 1024), 2), /* traffic is stored in KB, template uses MB */ - 'TRAFFICUSED' => round(($row['traffic_used_total'] / 1024), 2), /* traffic is stored in KB, template uses MB */ - 'USAGE_PERCENT' => round(($row['traffic_used_total'] * 100) / $row['traffic'], 2), - 'MAX_PERCENT' => Settings::Get('system.report_trafficmax') - ); - - $lngfile_stmt = Database::prepare(" - SELECT `file` FROM `" . TABLE_PANEL_LANGUAGE . "` - WHERE `language` = :deflang - "); - $lngfile = Database::pexecute_first($lngfile_stmt, array('deflang' => $row['def_language'])); - - if ($lngfile !== null) { - $langfile = $lngfile['file']; - } else { - $lngfile = Database::pexecute_first($lngfile_stmt, array('deflang' => Settings::Get('panel.standardlanguage'))); - $langfile = $lngfile['file']; - } - - // include english language file (fallback) - include_once makeCorrectFile(FROXLOR_INSTALL_DIR . '/lng/english.lng.php'); - // include admin/customer language file - include_once makeCorrectFile(FROXLOR_INSTALL_DIR . '/' . $langfile); - - // Get mail templates from database; the ones from 'admin' are fetched for fallback - $result2_stmt = Database::prepare(" - SELECT `value` FROM `" . TABLE_PANEL_TEMPLATES . "` - WHERE `adminid` = :adminid - AND `language` = :lang - AND `templategroup` = 'mails' AND `varname` = :varname - "); - $resul2_data = array( - 'adminid' => $row['adminid'], - 'lang' => $row['def_language'], - 'varname' => 'trafficmaxpercent_subject' - ); - $result2 = Database::pexecute_first($result2_stmt, $result2_data); - $mail_subject = html_entity_decode(replace_variables((($result2['value'] != '') ? $result2['value'] : $lng['mails']['trafficmaxpercent']['subject']), $replace_arr)); - - $resul2_data['varname'] = 'trafficmaxpercent_mailbody'; - $result2 = Database::pexecute_first($result2_stmt, $result2_data); - $mail_body = html_entity_decode(replace_variables((($result2['value'] != '') ? $result2['value'] : $lng['mails']['trafficmaxpercent']['mailbody']), $replace_arr)); - - $_mailerror = false; - try { - $mail->SetFrom($row['email'], $row['name']); - $mail->Subject = $mail_subject; - $mail->AltBody = $mail_body; - $mail->MsgHTML(nl2br($mail_body)); - $mail->AddAddress($row['email'], $row['name']); - $mail->Send(); - } catch(phpmailerException $e) { - $mailerr_msg = $e->errorMessage(); - $_mailerror = true; - } catch (Exception $e) { - $mailerr_msg = $e->getMessage(); - $_mailerror = true; - } - - if ($_mailerror) { - $cronlog->logAction(CRON_ACTION, LOG_ERR, "Error sending mail: " . $mailerr_msg); - echo "Error sending mail: " . $mailerr_msg . "\n"; - } - - $mail->ClearAddresses(); - $upd_stmt = Database::prepare(" - UPDATE `" . TABLE_PANEL_ADMINS . "` SET `reportsent` = '1' - WHERE `adminid` = :adminid - "); - Database::pexecute($upd_stmt, array('adminid' => $row['adminid'])); } - // Another month, let's build our report - if (date('d') == '01') { + // Warn the admins at xx% traffic-usage + $result_stmt = Database::prepare(" + SELECT `a`.*, + (SELECT SUM(`t`.`http` + `t`.`ftp_up` + `t`.`ftp_down` + `t`.`mail`) + FROM `" . TABLE_PANEL_TRAFFIC_ADMINS . "` `t` + WHERE `t`.`adminid` = `a`.`adminid` AND `t`.`year` = :year AND `t`.`month` = :month + ) as `traffic_used_total` + FROM `" . TABLE_PANEL_ADMINS . "` `a` WHERE `a`.`reportsent` = '0' + "); - $mail_subject = 'Trafficreport ' . date("m/y", $yesterday) . ' for ' . $row['name']; - $mail_body = 'Trafficreport ' . date("m/y", $yesterday) . ' for ' . $row['name'] . "\n"; - $mail_body.= '---------------------------------------------------------------' . "\n"; - $mail_body.= 'Loginname Traffic used (Percent) | Traffic available' . "\n"; - $customers_stmt = Database::prepare(" - SELECT `c`.*, - (SELECT SUM(`t`.`http` + `t`.`ftp_up` + `t`.`ftp_down` + `t`.`mail`) - FROM `" . TABLE_PANEL_TRAFFIC . "` `t` - WHERE `t`.`customerid` = `c`.`customerid` AND `t`.`year` = :year AND `t`.`month` = :month - ) as `traffic_used_total` - FROM `" . TABLE_PANEL_CUSTOMERS . "` `c` WHERE `c`.`adminid` = :adminid - "); - $customers_data = array( - 'year' => date("Y", $yesterday), - 'month' => date("m", $yesterday), - 'adminid' => $row['adminid'] - ); - Database::pexecute($customers_stmt, $customers_data); + $result_data = array( + 'year' => date("Y", $yesterday), + 'month' => date("m", $yesterday) + ); + Database::pexecute($result_stmt, $result_data); - while ($customer = $customers_stmt->fetch(PDO::FETCH_ASSOC)) { - $t = $customer['traffic_used_total']/1048576; - if ($customer['traffic'] > 0) { - $p = (($customer['traffic_used_total'] * 100) / $customer['traffic'] ); - $tg = $customer['traffic']/1048576; + while ($row = $result_stmt->fetch(PDO::FETCH_ASSOC)) { + + if (isset($row['traffic']) + && $row['traffic'] > 0 + && (($row['traffic_used_total'] * 100) / $row['traffic']) >= (int)Settings::Get('system.report_trafficmax') + ) { + + $replace_arr = array( + 'NAME' => $row['name'], + 'TRAFFIC' => round(($row['traffic'] / 1024), 2), /* traffic is stored in KB, template uses MB */ + 'TRAFFICUSED' => round(($row['traffic_used_total'] / 1024), 2), /* traffic is stored in KB, template uses MB */ + 'USAGE_PERCENT' => round(($row['traffic_used_total'] * 100) / $row['traffic'], 2), + 'MAX_PERCENT' => Settings::Get('system.report_trafficmax') + ); + + $lngfile_stmt = Database::prepare(" + SELECT `file` FROM `" . TABLE_PANEL_LANGUAGE . "` + WHERE `language` = :deflang + "); + $lngfile = Database::pexecute_first($lngfile_stmt, array('deflang' => $row['def_language'])); + + if ($lngfile !== null) { + $langfile = $lngfile['file']; + } else { + $lngfile = Database::pexecute_first($lngfile_stmt, array('deflang' => Settings::Get('panel.standardlanguage'))); + $langfile = $lngfile['file']; + } + + // include english language file (fallback) + include_once makeCorrectFile(FROXLOR_INSTALL_DIR . '/lng/english.lng.php'); + // include admin/customer language file + include_once makeCorrectFile(FROXLOR_INSTALL_DIR . '/' . $langfile); + + // Get mail templates from database; the ones from 'admin' are fetched for fallback + $result2_stmt = Database::prepare(" + SELECT `value` FROM `" . TABLE_PANEL_TEMPLATES . "` + WHERE `adminid` = :adminid + AND `language` = :lang + AND `templategroup` = 'mails' AND `varname` = :varname + "); + $resul2_data = array( + 'adminid' => $row['adminid'], + 'lang' => $row['def_language'], + 'varname' => 'trafficmaxpercent_subject' + ); + $result2 = Database::pexecute_first($result2_stmt, $result2_data); + $mail_subject = html_entity_decode(replace_variables((($result2['value'] != '') ? $result2['value'] : $lng['mails']['trafficmaxpercent']['subject']), $replace_arr)); + + $resul2_data['varname'] = 'trafficmaxpercent_mailbody'; + $result2 = Database::pexecute_first($result2_stmt, $result2_data); + $mail_body = html_entity_decode(replace_variables((($result2['value'] != '') ? $result2['value'] : $lng['mails']['trafficmaxpercent']['mailbody']), $replace_arr)); + + $_mailerror = false; + try { + $mail->SetFrom($row['email'], $row['name']); + $mail->Subject = $mail_subject; + $mail->AltBody = $mail_body; + $mail->MsgHTML(nl2br($mail_body)); + $mail->AddAddress($row['email'], $row['name']); + $mail->Send(); + } catch(phpmailerException $e) { + $mailerr_msg = $e->errorMessage(); + $_mailerror = true; + } catch (Exception $e) { + $mailerr_msg = $e->getMessage(); + $_mailerror = true; + } + + if ($_mailerror) { + $cronlog->logAction(CRON_ACTION, LOG_ERR, "Error sending mail: " . $mailerr_msg); + echo "Error sending mail: " . $mailerr_msg . "\n"; + } + + $mail->ClearAddresses(); + $upd_stmt = Database::prepare(" + UPDATE `" . TABLE_PANEL_ADMINS . "` SET `reportsent` = '1' + WHERE `adminid` = :adminid + "); + Database::pexecute($upd_stmt, array('adminid' => $row['adminid'])); + } + + // Another month, let's build our report + if (date('d') == '01') { + + $mail_subject = 'Trafficreport ' . date("m/y", $yesterday) . ' for ' . $row['name']; + $mail_body = 'Trafficreport ' . date("m/y", $yesterday) . ' for ' . $row['name'] . "\n"; + $mail_body.= '---------------------------------------------------------------' . "\n"; + $mail_body.= 'Loginname Traffic used (Percent) | Traffic available' . "\n"; + $customers_stmt = Database::prepare(" + SELECT `c`.*, + (SELECT SUM(`t`.`http` + `t`.`ftp_up` + `t`.`ftp_down` + `t`.`mail`) + FROM `" . TABLE_PANEL_TRAFFIC . "` `t` + WHERE `t`.`customerid` = `c`.`customerid` AND `t`.`year` = :year AND `t`.`month` = :month + ) as `traffic_used_total` + FROM `" . TABLE_PANEL_CUSTOMERS . "` `c` WHERE `c`.`adminid` = :adminid + "); + $customers_data = array( + 'year' => date("Y", $yesterday), + 'month' => date("m", $yesterday), + 'adminid' => $row['adminid'] + ); + Database::pexecute($customers_stmt, $customers_data); + + while ($customer = $customers_stmt->fetch(PDO::FETCH_ASSOC)) { + $t = $customer['traffic_used_total']/1048576; + if ($customer['traffic'] > 0) { + $p = (($customer['traffic_used_total'] * 100) / $customer['traffic'] ); + $tg = $customer['traffic']/1048576; + $str = sprintf('%00.1f GB ( %00.1f %% )', $t, $p); + $mail_body.= sprintf('%-15s', $customer['loginname']) . ' ' . sprintf('%-25s', $str) . ' ' . sprintf('%00.1f GB', $tg) . "\n"; + } else if ($customer['traffic'] == 0) { + $str = sprintf('%00.1f GB ( - )', $t); + $mail_body.= sprintf('%-15s', $customer['loginname']) . ' ' . sprintf('%-25s', $str) . ' ' . '0' . "\n"; + } else { + $str = sprintf('%00.1f GB ( - )', $t); + $mail_body.= sprintf('%-15s', $customer['loginname']) . ' ' . sprintf('%-25s', $str) . ' ' . 'unlimited' . "\n"; + } + } + + $mail_body.= '---------------------------------------------------------------' . "\n"; + + $t = $row['traffic_used_total']/1048576; + if ($row['traffic'] > 0) { + $p = (($row['traffic_used_total'] * 100) / $row['traffic']); + $tg = $row['traffic']/1048576; $str = sprintf('%00.1f GB ( %00.1f %% )', $t, $p); - $mail_body.= sprintf('%-15s', $customer['loginname']) . ' ' . sprintf('%-25s', $str) . ' ' . sprintf('%00.1f GB', $tg) . "\n"; - } else if ($customer['traffic'] == 0) { + $mail_body.= sprintf('%-15s', $row['loginname']) . ' ' . sprintf('%-25s', $str) . ' ' . sprintf('%00.1f GB', $tg) . "\n"; + } else if ($row['traffic'] == 0) { $str = sprintf('%00.1f GB ( - )', $t); - $mail_body.= sprintf('%-15s', $customer['loginname']) . ' ' . sprintf('%-25s', $str) . ' ' . '0' . "\n"; + $mail_body.= sprintf('%-15s', $row['loginname']) . ' ' . sprintf('%-25s', $str) . ' ' . '0' . "\n"; } else { $str = sprintf('%00.1f GB ( - )', $t); - $mail_body.= sprintf('%-15s', $customer['loginname']) . ' ' . sprintf('%-25s', $str) . ' ' . 'unlimited' . "\n"; + $mail_body.= sprintf('%-15s', $row['loginname']) . ' ' . sprintf('%-25s', $str) . ' ' . 'unlimited' . "\n"; } + + $_mailerror = false; + try { + $mail->SetFrom($row['email'], $row['name']); + $mail->Subject = $mail_subject; + $mail->Body = $mail_body; + $mail->AddAddress($row['email'], $row['name']); + $mail->Send(); + } catch(phpmailerException $e) { + $mailerr_msg = $e->errorMessage(); + $_mailerror = true; + } catch (Exception $e) { + $mailerr_msg = $e->getMessage(); + $_mailerror = true; + } + + if ($_mailerror) { + $cronlog->logAction(CRON_ACTION, LOG_ERR, 'Error sending mail: ' . $mailerr_msg); + echo 'Error sending mail: ' . $mailerr_msg . "\n"; + } + + $mail->ClearAddresses(); } - - $mail_body.= '---------------------------------------------------------------' . "\n"; - - $t = $row['traffic_used_total']/1048576; - if ($row['traffic'] > 0) { - $p = (($row['traffic_used_total'] * 100) / $row['traffic']); - $tg = $row['traffic']/1048576; - $str = sprintf('%00.1f GB ( %00.1f %% )', $t, $p); - $mail_body.= sprintf('%-15s', $row['loginname']) . ' ' . sprintf('%-25s', $str) . ' ' . sprintf('%00.1f GB', $tg) . "\n"; - } else if ($row['traffic'] == 0) { - $str = sprintf('%00.1f GB ( - )', $t); - $mail_body.= sprintf('%-15s', $row['loginname']) . ' ' . sprintf('%-25s', $str) . ' ' . '0' . "\n"; - } else { - $str = sprintf('%00.1f GB ( - )', $t); - $mail_body.= sprintf('%-15s', $row['loginname']) . ' ' . sprintf('%-25s', $str) . ' ' . 'unlimited' . "\n"; - } - - $_mailerror = false; - try { - $mail->SetFrom($row['email'], $row['name']); - $mail->Subject = $mail_subject; - $mail->Body = $mail_body; - $mail->AddAddress($row['email'], $row['name']); - $mail->Send(); - } catch(phpmailerException $e) { - $mailerr_msg = $e->errorMessage(); - $_mailerror = true; - } catch (Exception $e) { - $mailerr_msg = $e->getMessage(); - $_mailerror = true; - } - - if ($_mailerror) { - $cronlog->logAction(CRON_ACTION, LOG_ERR, 'Error sending mail: ' . $mailerr_msg); - echo 'Error sending mail: ' . $mailerr_msg . "\n"; - } - - $mail->ClearAddresses(); } -} +} // trafficmax > 0 // include diskspace-usage report, #466 include dirname(__FILE__).'/cron_usage.inc.diskspace.php';