From 9d314aaa3fac12db15b0c3d79a350c2b81209a26 Mon Sep 17 00:00:00 2001 From: Michael Kaufmann Date: Tue, 18 Dec 2018 12:16:48 +0100 Subject: [PATCH] use Monolog via composer instead of our own implementation, @TODO MySQL logging-handler Signed-off-by: Michael Kaufmann --- composer.json | 3 +- lib/Froxlor/FroxlorLogger.php | 247 ++++++++++++++++++ .../MailLogParser.php} | 173 ++++++------ .../logger/abstract.AbstractLogger.php | 97 ------- lib/classes/logger/class.FileLogger.php | 164 ------------ lib/classes/logger/class.FroxlorLogger.php | 190 -------------- lib/classes/logger/class.MysqlLogger.php | 111 -------- lib/classes/logger/class.SysLogger.php | 125 --------- lib/init.php | 2 +- scripts/jobs/cron_traffic.php | 5 +- 10 files changed, 354 insertions(+), 763 deletions(-) create mode 100644 lib/Froxlor/FroxlorLogger.php rename lib/{classes/mail/class.mailLogParser.php => Froxlor/MailLogParser.php} (67%) delete mode 100644 lib/classes/logger/abstract.AbstractLogger.php delete mode 100644 lib/classes/logger/class.FileLogger.php delete mode 100644 lib/classes/logger/class.FroxlorLogger.php delete mode 100644 lib/classes/logger/class.MysqlLogger.php delete mode 100644 lib/classes/logger/class.SysLogger.php diff --git a/composer.json b/composer.json index 0602b2da..1ca89aae 100644 --- a/composer.json +++ b/composer.json @@ -42,7 +42,8 @@ "ext-json" : "*", "ext-openssl": "*", "mso/idna-convert" : "1.*", - "phpmailer/phpmailer": "~6.0" + "phpmailer/phpmailer": "~6.0", + "monolog/monolog": "^1.24" }, "require-dev" : { "phpunit/phpunit" : "6.5.13", diff --git a/lib/Froxlor/FroxlorLogger.php b/lib/Froxlor/FroxlorLogger.php new file mode 100644 index 00000000..c22e33aa --- /dev/null +++ b/lib/Froxlor/FroxlorLogger.php @@ -0,0 +1,247 @@ +initMonolog(); + self::$logtypes = array(); + + if ((Settings::Get('logger.logtypes') == null || Settings::Get('logger.logtypes') == '') && (Settings::Get('logger.enabled') !== null && Settings::Get('logger.enabled'))) { + self::$logtypes[0] = 'syslog'; + self::$logtypes[1] = 'mysql'; + } else { + if (Settings::Get('logger.logtypes') !== null && Settings::Get('logger.logtypes') != '') { + self::$logtypes = explode(',', Settings::Get('logger.logtypes')); + } else { + self::$logtypes = null; + } + } + + foreach (self::$logtypes as $logger) { + + switch ($logger) { + case 'syslog': + self::$_ml->pushHandler(new SyslogHandler('froxlor', Logger::DEBUG)); + break; + case 'file': + self::$_ml->pushHandler(new StreamHandler(Settings::Get('logger.logfile'), Logger::DEBUG)); + break; + case 'mysql': + // @fixme add MySQL-Handler + break; + } + } + } + + /** + * return FroxlorLogger instance + * + * @param array $userinfo unused + * + * @return FroxlorLogger + */ + public static function getInstanceOf($userinfo = null) + { + return new FroxlorLogger(); + } + + /** + * initiate monolog object + * + * @return \Monolog\Logger + */ + private function initMonolog() + { + if (empty(self::$_ml)) { + // get Theme object + self::$_ml = new Logger('froxlor'); + } + return self::$_ml; + } + + /** + * logs a given text to all enabled logger-facilities + * + * @param int $action + * @param int $type + * @param string $text + */ + public function logAction($action = USR_ACTION, $type = LOG_NOTICE, $text = null) + { + // not logging normal stuff if not set to "paranoid" logging + if (Settings::Get('logger.severity') == '1' && $type > LOG_NOTICE) { + return; + } + + if (empty(self::$_ml)) { + $this->initMonolog(); + } + + if (self::$crondebug_flag || ($action == CRON_ACTION && $type <= LOG_WARNING)) { + echo "[" . $this->getLogLevelDesc($type) . "] " . $text . PHP_EOL; + } + + if (Settings::Get('logger.log_cron') == '0' && $action == CRON_ACTION && $type > LOG_WARNING) // warnings, errors and critical mesages WILL be logged + { + return; + } + + switch ($type) { + case LOG_DEBUG: + self::$_ml->addDebug($text, array( + 'source' => $this->getActionTypeDesc($action) + )); + break; + case LOG_INFO: + self::$_ml->addInfo($text, array( + 'source' => $this->getActionTypeDesc($action) + )); + break; + case LOG_NOTICE: + self::$_ml->addNotice($text, array( + 'source' => $this->getActionTypeDesc($action) + )); + break; + case LOG_WARNING: + self::$_ml->addWarning($text, array( + 'source' => $this->getActionTypeDesc($action) + )); + break; + case LOG_ERR: + self::$_ml->addError($text, array( + 'source' => $this->getActionTypeDesc($action) + )); + break; + default: + self::$_ml->addDebug($text, array( + 'source' => $this->getActionTypeDesc($action) + )); + } + } + + /** + * Set whether to log cron-runs + * + * @param bool $_cronlog + * + * @return boolean + */ + public function setCronLog($_cronlog = 0) + { + $_cronlog = (int) $_cronlog; + + if ($_cronlog < 0 || $_cronlog > 2) { + $_cronlog = 0; + } + Settings::Set('logger.log_cron', $_cronlog); + return $_cronlog; + } + + /** + * setter for crondebug-flag + * + * @param bool $_flag + * + * @return void + */ + public function setCronDebugFlag($_flag = false) + { + self::$crondebug_flag = (bool) $_flag; + } + + /** + * setter for crondebug-flag + * + * @param bool $_flag + * + * @return void + */ + public function setCronDebugFlag($_flag = false) + { + self::$crondebug_flag = (bool) $_flag; + } + + private function getLogLevelDesc($type) + { + switch ($type) { + case LOG_INFO: + $_type = 'information'; + break; + case LOG_NOTICE: + $_type = 'notice'; + break; + case LOG_WARNING: + $_type = 'warning'; + break; + case LOG_ERR: + $_type = 'error'; + break; + case LOG_CRIT: + $_type = 'critical'; + break; + case LOG_DEBUG: + $_type = 'debug'; + break; + default: + $_type = 'unknown'; + break; + } + return $_type; + } + + private function getActionTypeDesc($action) + { + switch ($action) { + case USR_ACTION: + $_action = 'user'; + break; + case ADM_ACTION: + $_action = 'admin'; + break; + case RES_ACTION: + $_action = 'reseller'; + break; + case CRON_ACTION: + $_action = 'cron'; + break; + case LOGIN_ACTION: + $_action = 'login'; + break; + } + return $_action; + } +} diff --git a/lib/classes/mail/class.mailLogParser.php b/lib/Froxlor/MailLogParser.php similarity index 67% rename from lib/classes/mail/class.mailLogParser.php rename to lib/Froxlor/MailLogParser.php index aebddd64..5870d021 100644 --- a/lib/classes/mail/class.mailLogParser.php +++ b/lib/Froxlor/MailLogParser.php @@ -1,4 +1,7 @@ - * @author Froxlor team (2010-) - * @license GPLv2 http://files.froxlor.org/misc/COPYING.txt - * @package Cron - * - * @since 0.9.32 - * + * @copyright (c) the authors + * @author Roman Schmerold + * @author Froxlor team (2010-) + * @license GPLv2 http://files.froxlor.org/misc/COPYING.txt + * @package Cron + * + * @since 0.9.32 + * */ - -class MailLogParser { +class MailLogParser +{ private $startTime; + private $domainTraffic = array(); + private $myDomains = array(); + private $mails = array(); /** * constructor - * @param string logFile - * @param int startTime - * @param string logFileExim + * + * @param + * string logFile + * @param + * int startTime + * @param + * string logFileExim */ - public function __construct($startTime = 0) { + public function __construct($startTime = 0) + { $this->startTime = $startTime; // Get all domains from Database $stmt = Database::prepare("SELECT domain FROM `" . TABLE_PANEL_DOMAINS . "`"); Database::pexecute($stmt, array()); - while ($domain_row = $stmt->fetch(PDO::FETCH_ASSOC)) { + while ($domain_row = $stmt->fetch(\PDO::FETCH_ASSOC)) { $this->myDomains[] = $domain_row["domain"]; } @@ -59,30 +70,32 @@ class MailLogParser { } } - /** * parsePostfixLog * parses the traffic from a postfix logfile - * @param logFile + * + * @param + * logFile */ - private function _parsePostfixLog($logFile) { + private function _parsePostfixLog($logFile) + { // Check if file exists - if (!file_exists($logFile)) { + if (! file_exists($logFile)) { return false; } // Open the log file try { $file_handle = fopen($logFile, "r"); - if (!$file_handle) { - throw new Exception("Could not open the file!"); + if (! $file_handle) { + throw new \Exception("Could not open the file!"); } - } catch (Exception $e) { - echo "Error (File: ".$e->getFile().", line ". $e->getLine()."): ".$e->getMessage(); + } catch (\Exception $e) { + echo "Error (File: " . $e->getFile() . ", line " . $e->getLine() . "): " . $e->getMessage(); return false; } - while (!feof($file_handle)) { + while (! feof($file_handle)) { unset($matches); $line = fgets($file_handle); @@ -90,7 +103,10 @@ class MailLogParser { if ($this->startTime < $timestamp) { if (preg_match("/postfix\/qmgr.*(?::|\])\s([A-Z\d]+).*from=?, size=(\d+),/", $line, $matches)) { // Postfix from - $this->mails[$matches[1]] = array("domainFrom" => strtolower($matches[2]), "size" => $matches[3]); + $this->mails[$matches[1]] = array( + "domainFrom" => strtolower($matches[2]), + "size" => $matches[3] + ); } elseif (preg_match("/postfix\/(?:pipe|smtp).*(?::|\])\s([A-Z\d]+).*to=?,/", $line, $matches)) { // Postfix to if (array_key_exists($matches[1], $this->mails)) { @@ -118,30 +134,32 @@ class MailLogParser { return true; } - /** * parseExim4Log * parses the smtp traffic from a exim4 logfile - * @param logFile + * + * @param + * logFile */ - private function _parseExim4Log($logFile) { + private function _parseExim4Log($logFile) + { // Check if file exists - if (!file_exists($logFile)) { + if (! file_exists($logFile)) { return false; } // Open the log file try { $file_handle = fopen($logFile, "r"); - if (!$file_handle) { - throw new Exception("Could not open the file!"); + if (! $file_handle) { + throw new \Exception("Could not open the file!"); } - } catch (Exception $e) { - echo "Error (File: ".$e->getFile().", line ". $e->getLine()."): ".$e->getMessage(); + } catch (\Exception $e) { + echo "Error (File: " . $e->getFile() . ", line " . $e->getLine() . "): " . $e->getMessage(); return false; } - while (!feof($file_handle)) { + while (! feof($file_handle)) { unset($matches); $line = fgets($file_handle); @@ -150,7 +168,7 @@ class MailLogParser { if (preg_match("/<= .*@([a-z0-9.\-]+) .*S=(\d+)/i", $line, $matches)) { // Outgoing traffic $this->_addDomainTraffic($matches[1], $matches[2], $timestamp); - } elseif (preg_match("/=> .*? .*S=(\d+)/i" , $line, $matches)) { + } elseif (preg_match("/=> .*? .*S=(\d+)/i", $line, $matches)) { // Incoming traffic $this->_addDomainTraffic($matches[1], $matches[2], $timestamp); } @@ -160,30 +178,32 @@ class MailLogParser { return true; } - /** * parseDovecotLog * parses the dovecot imap/pop3 traffic from logfile - * @param logFile + * + * @param + * logFile */ - private function _parseDovecotLog($logFile) { + private function _parseDovecotLog($logFile) + { // Check if file exists - if (!file_exists($logFile)) { + if (! file_exists($logFile)) { return false; } // Open the log file try { $file_handle = fopen($logFile, "r"); - if (!$file_handle) { - throw new Exception("Could not open the file!"); + if (! $file_handle) { + throw new \Exception("Could not open the file!"); } - } catch (Exception $e) { - echo "Error (File: ".$e->getFile().", line ". $e->getLine()."): ".$e->getMessage(); + } catch (\Exception $e) { + echo "Error (File: " . $e->getFile() . ", line " . $e->getLine() . "): " . $e->getMessage(); return false; } - while (!feof($file_handle)) { + while (! feof($file_handle)) { unset($matches); $line = fgets($file_handle); @@ -191,10 +211,10 @@ class MailLogParser { if ($this->startTime < $timestamp) { if (preg_match("/dovecot.*(?::|\]) imap\(.*@([a-z0-9\.\-]+)\):.*(?:in=(\d+) out=(\d+)|bytes=(\d+)\/(\d+))/i", $line, $matches)) { // Dovecot IMAP - $this->_addDomainTraffic($matches[1], (int)$matches[2] + (int)$matches[3], $timestamp); + $this->_addDomainTraffic($matches[1], (int) $matches[2] + (int) $matches[3], $timestamp); } elseif (preg_match("/dovecot.*(?::|\]) pop3\(.*@([a-z0-9\.\-]+)\):.*in=(\d+).*out=(\d+)/i", $line, $matches)) { // Dovecot POP3 - $this->_addDomainTraffic($matches[1], (int)$matches[2] + (int)$matches[3], $timestamp); + $this->_addDomainTraffic($matches[1], (int) $matches[2] + (int) $matches[3], $timestamp); } } } @@ -202,30 +222,32 @@ class MailLogParser { return true; } - /** * parseCourierLog * parses the dovecot imap/pop3 traffic from logfile - * @param logFile + * + * @param + * logFile */ - private function _parseCourierLog($logFile) { + private function _parseCourierLog($logFile) + { // Check if file exists - if (!file_exists($logFile)) { + if (! file_exists($logFile)) { return false; } // Open the log file try { $file_handle = fopen($logFile, "r"); - if (!$file_handle) { - throw new Exception("Could not open the file!"); + if (! $file_handle) { + throw new \Exception("Could not open the file!"); } - } catch (Exception $e) { - echo "Error (File: ".$e->getFile().", line ". $e->getLine()."): ".$e->getMessage(); + } catch (\Exception $e) { + echo "Error (File: " . $e->getFile() . ", line " . $e->getLine() . "): " . $e->getMessage(); return false; } - while (!feof($file_handle)) { + while (! feof($file_handle)) { unset($matches); $line = fgets($file_handle); @@ -233,7 +255,7 @@ class MailLogParser { if ($this->startTime < $timestamp) { if (preg_match("/(?:imapd|pop3d)(?:-ssl)?.*(?::|\]).*user=.*@([a-z0-9\.\-]+),.*rcvd=(\d+), sent=(\d+),/i", $line, $matches)) { // Courier IMAP & POP3 - $this->_addDomainTraffic($matches[1], (int)$matches[2] + (int)$matches[3], $timestamp); + $this->_addDomainTraffic($matches[1], (int) $matches[2] + (int) $matches[3], $timestamp); } } } @@ -241,34 +263,39 @@ class MailLogParser { return true; } - /** * _addDomainTraffic * adds the traffic to the domain array if we own the domain - * @param string domain - * @param int traffic + * + * @param + * string domain + * @param + * int traffic */ - private function _addDomainTraffic($domain, $traffic, $timestamp) { + private function _addDomainTraffic($domain, $traffic, $timestamp) + { $date = date("Y-m-d", $timestamp); if (in_array($domain, $this->myDomains)) { if (array_key_exists($domain, $this->domainTraffic) && array_key_exists($date, $this->domainTraffic[$domain])) { - $this->domainTraffic[$domain][$date] += (int)$traffic; + $this->domainTraffic[$domain][$date] += (int) $traffic; } else { - if (!array_key_exists($domain, $this->domainTraffic)) { + if (! array_key_exists($domain, $this->domainTraffic)) { $this->domainTraffic[$domain] = array(); } - $this->domainTraffic[$domain][$date] = (int)$traffic; + $this->domainTraffic[$domain][$date] = (int) $traffic; } } } - /** * getLogTimestamp - * @param string line - * return int + * + * @param + * string line + * return int */ - private function _getLogTimestamp($line) { + private function _getLogTimestamp($line) + { if (preg_match("/((?:[A-Z]{3}\s{1,2}\d{1,2}|\d{4}-\d{2}-\d{2}) \d{2}:\d{2}:\d{2})/i", $line, $matches)) { $timestamp = strtotime($matches[1]); if ($timestamp > ($this->startTime + 60 * 60 * 24)) { @@ -281,20 +308,20 @@ class MailLogParser { } } - /** * getDomainTraffic * returns the traffic of a given domain or 0 if the domain has no traffic - * @param string domain - * return array + * + * @param + * string domain + * return array */ - public function getDomainTraffic($domain) { + public function getDomainTraffic($domain) + { if (array_key_exists($domain, $this->domainTraffic)) { return $this->domainTraffic[$domain]; } else { return 0; } } - - } diff --git a/lib/classes/logger/abstract.AbstractLogger.php b/lib/classes/logger/abstract.AbstractLogger.php deleted file mode 100644 index cfd01f70..00000000 --- a/lib/classes/logger/abstract.AbstractLogger.php +++ /dev/null @@ -1,97 +0,0 @@ - - * @author Froxlor team (2010-) - * @license GPLv2 http://files.froxlor.org/misc/COPYING.txt - * @package Logger - * - * @link http://www.nutime.de/ - * - * Logger - Abstract-Logger-Class - */ - -/* We're using the syslog constants for all the loggers (partly implemented) - -LOG_EMERG system is unusable -LOG_ALERT action must be taken immediately -LOG_CRIT critical conditions -LOG_ERR error conditions -LOG_WARNING warning conditions -LOG_NOTICE normal, but significant, condition -LOG_INFO informational message -LOG_DEBUG debug-level message - -*/ - -abstract class AbstractLogger { - - /** - * Enable/Disable Logging - * @var boolean - */ - private $logenabled = false; - - /** - * Enable/Disable Cronjob-Logging - * @var boolean - */ - private $logcronjob = false; - - /** - * Loggin-Severity - * @var int - */ - private $severity = 1; - - /** - * setup the main logger - */ - protected function setupLogger() { - $this->logenabled = Settings::Get('logger.enabled'); - $this->logcronjob = Settings::Get('logger.log_cron'); - $this->severity = Settings::Get('logger.severity'); - } - - /** - * return whether this logging is enabled - * - * @return bool - */ - protected function isEnabled() { - return $this->logenabled; - } - - /** - * return the log severity - * - * @return int - */ - protected function getSeverity() { - return $this->severity; - } - - /** - * whether to log cron-runs or not - * - * @return bool - */ - protected function logCron() { - return $this->logcronjob; - } - - /** - * logs a given text - */ - abstract public function logAction(); - -} diff --git a/lib/classes/logger/class.FileLogger.php b/lib/classes/logger/class.FileLogger.php deleted file mode 100644 index 5f48c5d7..00000000 --- a/lib/classes/logger/class.FileLogger.php +++ /dev/null @@ -1,164 +0,0 @@ - - * @author Froxlor team (2010-) - * @license GPLv2 http://files.froxlor.org/misc/COPYING.txt - * @package Logger - * - * @link http://www.nutime.de/ - * - * Logger - File-Logger-Class - */ - -class FileLogger extends AbstractLogger { - - /** - * Userinfo - * @var array - */ - private $userinfo = array(); - - /** - * Logfile - * @var string - */ - private $logfile = null; - - /** - * Syslogger Objects Array - * @var FileLogger[] - */ - static private $loggers = array(); - - /** - * Class constructor. - * - * @param array userinfo - */ - protected function __construct($userinfo) { - parent::setupLogger(); - $this->userinfo = $userinfo; - $this->setLogFile(Settings::Get('logger.logfile')); - } - - /** - * Singleton ftw ;-) - */ - static public function getInstanceOf($_usernfo) { - if (!isset(self::$loggers[$_usernfo['loginname']])) { - self::$loggers[$_usernfo['loginname']] = new FileLogger($_usernfo); - } - return self::$loggers[$_usernfo['loginname']]; - } - - /** - * logs a given text to all enabled logger-facilities - * - * @param int $action - * @param int $type - * @param string $text - */ - public function logAction($action = USR_ACTION, $type = LOG_NOTICE, $text = null) { - global $lng; - - if (parent::isEnabled()) { - - if (parent::getSeverity() <= 1 - && $type == LOG_NOTICE - ) { - return; - } - - $_action = 'unknown'; - - switch($action) - { - case USR_ACTION: - $_action = $lng['admin']['customer']; - break; - case RES_ACTION: - $_action = $lng['logger']['reseller']; - break; - case ADM_ACTION: - $_action = $lng['logger']['admin']; - break; - case CRON_ACTION: - $_action = $lng['logger']['cron']; - break; - case LOGIN_ACTION: - $_action = $lng['logger']['login']; - break; - case LOG_ERROR: - $_action = $lng['logger']['intern']; - break; - default: - $_action = $lng['logger']['unknown']; - break; - } - - $_type = getLogLevelDesc($type); - - if(!isset($this->userinfo['loginname']) - || $this->userinfo['loginname'] == '') - { - $name = 'unknown'; - } - else - { - $name = $this->userinfo['loginname']; - } - - $fp = @fopen($this->logfile, 'a'); - - if($fp !== false) - { - $now = time(); - - if($text != null - && $text != '') - { - fwrite($fp, date("d.m.Y H:i:s", $now) . " [" . $_type . "] [" . $_action . "-action " . $name . "] " . $text . "\n"); - } - else - { - fwrite($fp, date("d.m.Y H:i:s", $now) . " [" . $_type . "] [" . $_action . "-action " . $name . "] No text given!!! Check scripts!\n"); - } - - fclose($fp); - } - else - { - if($this->logfile != null - || $this->logfile != '') - { - throw new Exception("Cannot open logfile '" . $this->logfile . "' for writing!"); - } - } - } - } - - public function setLogFile($filename = null) - { - if($filename != null - && $filename != '' - && $filename != "." - && $filename != ".." - && !is_dir($filename)) - { - $this->logfile = $filename; - return true; - } - - return false; - } -} diff --git a/lib/classes/logger/class.FroxlorLogger.php b/lib/classes/logger/class.FroxlorLogger.php deleted file mode 100644 index 78fae694..00000000 --- a/lib/classes/logger/class.FroxlorLogger.php +++ /dev/null @@ -1,190 +0,0 @@ - - * @author Froxlor team (2010-) - * @license GPLv2 http://files.froxlor.org/misc/COPYING.txt - * @package Logger - * - * @link http://www.nutime.de/ - * - * Logger - Froxlor-Base-Logger-Class - */ - -class FroxlorLogger { - - /** - * Userinfo - * @var array - */ - private $userinfo = array(); - - /** - * LogTypes Array - * @var array - */ - static private $logtypes = null; - - /** - * Logger-Object-Array - * @var FroxlorLogger[] - */ - static private $loggers = null; - - /** - * whether to output log-messages to STDOUT (cron) - * @var bool - */ - static private $crondebug_flag = false; - - /** - * Class constructor. - * - * @param array userinfo - */ - protected function __construct($userinfo) { - $this->userinfo = $userinfo; - self::$logtypes = array(); - - if ((Settings::Get('logger.logtypes') == null || Settings::Get('logger.logtypes') == '') - && (Settings::Get('logger.enabled') !== null && Settings::Get('logger.enabled')) - ) { - self::$logtypes[0] = 'syslog'; - self::$logtypes[1] = 'mysql'; - } else { - if (Settings::Get('logger.logtypes') !== null - && Settings::Get('logger.logtypes') != '' - ) { - self::$logtypes = explode(',', Settings::Get('logger.logtypes')); - } else { - self::$logtypes = null; - } - } - } - - /** - * Singleton ftw ;-) - * - */ - static public function getInstanceOf($_usernfo) { - - if (!isset($_usernfo) - || $_usernfo == null - ) { - $_usernfo = array(); - $_usernfo['loginname'] = 'unknown'; - } - - if (!isset(self::$loggers[$_usernfo['loginname']])) { - self::$loggers[$_usernfo['loginname']] = new FroxlorLogger($_usernfo); - } - - return self::$loggers[$_usernfo['loginname']]; - } - - /** - * logs a given text to all enabled logger-facilities - * - * @param int $action - * @param int $type - * @param string $text - */ - public function logAction ($action = USR_ACTION, $type = LOG_NOTICE, $text = null) { - - if (self::$logtypes == null) { - return; - } - - if (self::$crondebug_flag - || ($action == CRON_ACTION && $type <= LOG_WARNING)) { - echo "[".getLogLevelDesc($type)."] ".$text.PHP_EOL; - } - - if (Settings::Get('logger.log_cron') == '0' - && $action == CRON_ACTION - && $type > LOG_WARNING // warnings, errors and critical mesages WILL be logged - ) { - return; - } - - foreach (self::$logtypes as $logger) { - - switch ($logger) - { - case 'syslog': - $_log = SysLogger::getInstanceOf($this->userinfo); - break; - case 'file': - try - { - $_log = FileLogger::getInstanceOf($this->userinfo); - } - catch(Exception $e) - { - if ($action != CRON_ACTION) { - standard_error('logerror', $e->getMessage()); - } else { - echo "Log-Error: " . $e->getMessage(); - } - } - break; - case 'mysql': - $_log = MysqlLogger::getInstanceOf($this->userinfo); - break; - default: - $_log = null; - break; - } - - if ($_log != null) { - try { - $_log->logAction($action, $type, $text); - } catch(Exception $e) { - if ($action != CRON_ACTION) { - standard_error('logerror', $e->getMessage()); - } else { - echo "Log-Error: " . $e->getMessage(); - } - } - } - } - } - - /** - * Set whether to log cron-runs - * - * @param bool $_cronlog - * - * @return boolean - */ - public function setCronLog($_cronlog = 0) { - - $_cronlog = (int)$_cronlog; - - if ($_cronlog < 0 || $_cronlog > 2) { - $_cronlog = 0; - } - Settings::Set('logger.log_cron', $_cronlog); - return $_cronlog; - } - - /** - * setter for crondebug-flag - * - * @param bool $_flag - * - * @return void - */ - public function setCronDebugFlag($_flag = false) { - self::$crondebug_flag = (bool)$_flag; - } -} diff --git a/lib/classes/logger/class.MysqlLogger.php b/lib/classes/logger/class.MysqlLogger.php deleted file mode 100644 index 0136b56f..00000000 --- a/lib/classes/logger/class.MysqlLogger.php +++ /dev/null @@ -1,111 +0,0 @@ - - * @author Froxlor team (2010-) - * @license GPLv2 http://files.froxlor.org/misc/COPYING.txt - * @package Logger - * - * @link http://www.nutime.de/ - * - * Logger - MySQL-Logger-Class - */ - -class MysqlLogger extends AbstractLogger { - - /** - * Userinfo - * @var array - */ - private $userinfo = array(); - - /** - * Syslogger Objects Array - * @var MysqlLogger[] - */ - static private $loggers = array(); - - /** - * Class constructor. - * - * @param array userinfo - */ - protected function __construct($userinfo) { - parent::setupLogger(); - $this->userinfo = $userinfo; - } - - /** - * Singleton ftw ;-) - */ - static public function getInstanceOf($_usernfo) { - if (!isset(self::$loggers[$_usernfo['loginname']])) { - self::$loggers[$_usernfo['loginname']] = new MysqlLogger($_usernfo); - } - return self::$loggers[$_usernfo['loginname']]; - } - - /** - * logs a given text to all enabled logger-facilities - * - * @param int $action - * @param int $type - * @param string $text - */ - public function logAction($action = USR_ACTION, $type = LOG_NOTICE, $text = null) { - - if (parent::isEnabled()) { - - if (parent::getSeverity() <= 1 - && $type == LOG_NOTICE - ) { - return; - } - - if (!isset($this->userinfo['loginname']) - || $this->userinfo['loginname'] == '' - ) { - $name = 'unknown'; - } else { - $name = $this->userinfo['loginname']; - } - - $now = time(); - - $stmt = Database::prepare(" - INSERT INTO `panel_syslog` SET - `type` = :type, - `date` = :now, - `action` = :action, - `user` = :user, - `text` = :text" - ); - - $ins_data = array( - 'type' => $type, - 'now' => $now, - 'action' => $action, - 'user' => $name - ); - - if ($text != null - && $text != '' - ) { - $ins_data['text'] = $text; - Database::pexecute($stmt, $ins_data); - } else { - $ins_data['text'] = 'No text given!!! Check scripts!'; - Database::pexecute($stmt, $ins_data); - } - } - } -} diff --git a/lib/classes/logger/class.SysLogger.php b/lib/classes/logger/class.SysLogger.php deleted file mode 100644 index 42255484..00000000 --- a/lib/classes/logger/class.SysLogger.php +++ /dev/null @@ -1,125 +0,0 @@ - - * @author Froxlor team (2010-) - * @license GPLv2 http://files.froxlor.org/misc/COPYING.txt - * @package Logger - * - * @link http://www.nutime.de/ - * - * Logger - SysLog-Logger-Class - */ - -class SysLogger extends AbstractLogger { - - /** - * Userinfo - * @var array - */ - private $userinfo = array(); - - /** - * Syslogger Objects Array - * @var SysLogger[] - */ - static private $loggers = array(); - - /** - * Class constructor. - * - * @param array userinfo - */ - protected function __construct($userinfo) { - parent::setupLogger(); - $this->userinfo = $userinfo; - } - - /** - * Singleton ftw ;-) - */ - static public function getInstanceOf($_usernfo) { - if (!isset(self::$loggers[$_usernfo['loginname']])) { - self::$loggers[$_usernfo['loginname']] = new SysLogger($_usernfo); - } - return self::$loggers[$_usernfo['loginname']]; - } - - /** - * logs a given text to all enabled logger-facilities - * - * @param int $action - * @param int $type - * @param string $text - */ - public function logAction($action = USR_ACTION, $type = LOG_NOTICE, $text = null) { - - global $lng; - if (parent::isEnabled()) { - - if (parent::getSeverity() <= 1 - && $type == LOG_NOTICE - ) { - return; - } - - $_action = 'unknown'; - - switch($action) - { - case USR_ACTION: - $_action = $lng['admin']['customer']; - break; - case RES_ACTION: - $_action = $lng['logger']['reseller']; - break; - case ADM_ACTION: - $_action = $lng['logger']['admin']; - break; - case CRON_ACTION: - $_action = $lng['logger']['cron']; - break; - case LOGIN_ACTION: - $_action = $lng['logger']['login']; - break; - case LOG_ERROR: - $_action = $lng['logger']['intern']; - break; - default: - $_action = $lng['logger']['unknown']; - break; - } - - if (!isset($this->userinfo['loginname']) - || $this->userinfo['loginname'] == '' - ) { - $name = 'unknown'; - } - else - { - $name = $this->userinfo['loginname']; - } - - openlog("Froxlor", LOG_NDELAY, LOG_USER); - - if ($text != null - && $text != '' - ) { - syslog((int)$type, "[" . ucfirst($_action) . " Action " . $name . "] [".getLogLevelDesc($type)."] " . $text); - } else { - syslog((int)$type, "[" . ucfirst($_action) . " Action " . $name . "] [".getLogLevelDesc($type)."] No text given!!! Check scripts!"); - } - - closelog(); - } - } -} diff --git a/lib/init.php b/lib/init.php index 5d03353b..dd8c905a 100644 --- a/lib/init.php +++ b/lib/init.php @@ -387,7 +387,7 @@ if (isset($userinfo['loginname']) ) { $lng['menue']['main']['username'].= $userinfo['loginname']; //Initialize logging - $log = FroxlorLogger::getInstanceOf($userinfo); + $log = \Froxlor\FroxlorLogger::getInstanceOf($userinfo); } /** diff --git a/scripts/jobs/cron_traffic.php b/scripts/jobs/cron_traffic.php index c420e5e6..d29e2076 100644 --- a/scripts/jobs/cron_traffic.php +++ b/scripts/jobs/cron_traffic.php @@ -17,6 +17,9 @@ * */ +use \Froxlor\Database; +use \Froxlor\Settings; + // Check Traffic-Lock if (function_exists('pcntl_fork') && !defined('CRON_NOFORK_FLAG')) { $TrafficLock = makeCorrectFile(dirname($lockfile)."/froxlor_cron_traffic.lock"); @@ -154,7 +157,7 @@ if (Settings::Get('system.diskquota_enabled')) { * MAIL-Traffic */ if (Settings::Get("system.mailtraffic_enabled")) { - $mailTrafficCalc = new MailLogParser(Settings::Get("system.last_traffic_run")); + $mailTrafficCalc = new \Froxlor\MailLogParser(Settings::Get("system.last_traffic_run")); } $result_stmt = Database::query("SELECT * FROM `" . TABLE_PANEL_CUSTOMERS . "` ORDER BY `customerid` ASC");