From 2ff01027665d416eff1def58f709590cde47ff25 Mon Sep 17 00:00:00 2001 From: nuxsmin Date: Fri, 16 Oct 2015 12:36:28 +0200 Subject: [PATCH] * [MOD] Remote Syslog tweaking --- inc/SP/Log/ActionLog.class.php | 6 ++-- inc/SP/Log/Log.class.php | 6 ++-- inc/SP/Log/Syslog.class.php | 42 ++++++++++++++--------- inc/SP/Util/Connection.class.php | 21 ++++++++++-- inc/SP/Util/ConnectionInterface.class.php | 1 + inc/themes/classic/eventlog.inc | 1 + inc/themes/material-blue/eventlog.inc | 3 +- 7 files changed, 55 insertions(+), 25 deletions(-) diff --git a/inc/SP/Log/ActionLog.class.php b/inc/SP/Log/ActionLog.class.php index 9d59e2d8..26a2ab9f 100644 --- a/inc/SP/Log/ActionLog.class.php +++ b/inc/SP/Log/ActionLog.class.php @@ -35,7 +35,7 @@ abstract class ActionLog extends LogLevel /** * Constante de nueva línea para descripciones */ - const NEWLINE_TXT = ';;'; + const NEWLINE_TXT = PHP_EOL; /** * Constante de nueva línea para descriciones en formato HTML */ @@ -136,7 +136,7 @@ abstract class ActionLog extends LogLevel } if (count($this->_details) > 1) { - $newline = ($this->_newLineHtml === false) ? self::NEWLINE_TXT : self::NEWLINE_HTML; + $newline = ($this->_newLineHtml === false) ? PHP_EOL : self::NEWLINE_HTML; return implode($newline, $this->_details); } @@ -176,7 +176,7 @@ abstract class ActionLog extends LogLevel } if (count($this->_description) > 1) { - $newline = ($this->_newLineHtml === false) ? self::NEWLINE_TXT : self::NEWLINE_HTML; + $newline = ($this->_newLineHtml === false) ? PHP_EOL : self::NEWLINE_HTML; return implode($newline, $this->_description); } diff --git a/inc/SP/Log/Log.class.php b/inc/SP/Log/Log.class.php index add1bd64..5756a11e 100644 --- a/inc/SP/Log/Log.class.php +++ b/inc/SP/Log/Log.class.php @@ -138,7 +138,7 @@ class Log extends ActionLog $this->sendToSyslog(); } - $description = trim($this->getDescription() . self::NEWLINE_TXT . $this->getDetails(), ';'); + $description = trim($this->getDescription() . PHP_EOL . $this->getDetails()); $query = 'INSERT INTO log SET ' . 'log_date = UNIX_TIMESTAMP(),' . @@ -168,9 +168,11 @@ class Log extends ActionLog */ private function sendToSyslog() { + $description = trim($this->getDescription() . PHP_EOL . $this->getDetails()); + $msg = 'CEF:0|sysPass|logger|' . implode('.', Util::getVersion(true)) . '|'; $msg .= $this->getAction() . '|'; - $msg .= $this->getDescription() . '|'; + $msg .= $description . '|'; $msg .= '0|'; $msg .= sprintf('ip_addr="%s" user_name="%s"', $_SERVER['REMOTE_ADDR'], Session::getUserLogin()); diff --git a/inc/SP/Log/Syslog.class.php b/inc/SP/Log/Syslog.class.php index 981af416..58cbe979 100644 --- a/inc/SP/Log/Syslog.class.php +++ b/inc/SP/Log/Syslog.class.php @@ -64,22 +64,7 @@ class Syslog extends AbstractLogger syslog($this->getSyslogLevel($level), $message); closelog(); } else { - $server = Config::getValue('syslog_server', '127.0.0.1'); - $port = Config::getValue('syslog_port', 514); - - if (!empty($server)) { - $syslogMsg = date('M d H:i:s ') . "sysPass web: $message"; - - try { - $Connecion = new Connection($server, $port); - $Connecion->getSocket(Connection::TYPE_UDP); - $Connecion->send($syslogMsg); - $Connecion->closeSocket(); - } catch (SPException $e) { - error_log($e->getMessage()); - error_log($e->getHint()); - } - } + $this->logRemote($message); } } @@ -110,4 +95,29 @@ class Syslog extends AbstractLogger return LOG_DEBUG; } } + + /** + * Enviar un mensaje a syslog remoto + * + * @param $message + */ + private function logRemote($message) + { + $server = Config::getValue('syslog_server'); + $port = Config::getValue('syslog_port', 514); + + if (!empty($server)) { + $syslogMsg = date('M d H:i:s ') . "sysPass web: $message"; + + try { + $Connecion = new Connection($server, $port); + $Connecion->getSocket(Connection::TYPE_UDP); + $Connecion->send($syslogMsg); + $Connecion->closeSocket(); + } catch (SPException $e) { + error_log($e->getMessage()); + error_log($e->getHint()); + } + } + } } \ No newline at end of file diff --git a/inc/SP/Util/Connection.class.php b/inc/SP/Util/Connection.class.php index 785ae0eb..d07b5c86 100644 --- a/inc/SP/Util/Connection.class.php +++ b/inc/SP/Util/Connection.class.php @@ -48,6 +48,18 @@ class Connection implements ConnectionInterface * @var int */ protected $_port = 0; + /** + * Código de error del socket + * + * @var int + */ + protected $_errorno = 0; + /** + * Mensaje de error del socket + * + * @var string + */ + protected $_errorstr = ''; /** * @param $host string El host a conectar @@ -83,6 +95,8 @@ class Connection implements ConnectionInterface if ($this->_socket === false) { throw new SPException(SPException::SP_WARNING, $this->getSocketError()); } + + stream_set_timeout($this->_socket, self::SOCKET_TIMEOUT); } /** @@ -124,7 +138,8 @@ class Connection implements ConnectionInterface */ public function getSocketError() { - return socket_strerror(socket_last_error($this->_socket)); + return sprintf('%s (%d)', $this->_errorstr, $this->_errorno); +// return socket_strerror(socket_last_error($this->_socket)); } /** @@ -134,7 +149,7 @@ class Connection implements ConnectionInterface */ private function getUDPSocket() { - return stream_socket_client('udp://' . $this->_host . ':' . $this->_port, $errno, $errstr, 30); + return stream_socket_client('udp://' . $this->_host . ':' . $this->_port, $this->_errorno, $this->_errorstr, self::SOCKET_TIMEOUT); // return @socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); } @@ -145,7 +160,7 @@ class Connection implements ConnectionInterface */ private function getTCPSocket() { - return stream_socket_client('tcp://' . $this->_host . ':' . $this->_port, $errno, $errstr, 30); + return stream_socket_client('tcp://' . $this->_host . ':' . $this->_port, $this->_errorno, $this->_errorstr, self::SOCKET_TIMEOUT); // return @socket_create(AF_INET, SOCK_STREAM, SOL_TCP); } } \ No newline at end of file diff --git a/inc/SP/Util/ConnectionInterface.class.php b/inc/SP/Util/ConnectionInterface.class.php index dd18ff09..5d57a58f 100644 --- a/inc/SP/Util/ConnectionInterface.class.php +++ b/inc/SP/Util/ConnectionInterface.class.php @@ -34,6 +34,7 @@ interface ConnectionInterface { const TYPE_TCP = 1; const TYPE_UDP = 2; + const SOCKET_TIMEOUT = 10; /** * Obtener un socket diff --git a/inc/themes/classic/eventlog.inc b/inc/themes/classic/eventlog.inc index 6598bd40..ad4128ff 100644 --- a/inc/themes/classic/eventlog.inc +++ b/inc/themes/classic/eventlog.inc @@ -59,6 +59,7 @@