mirror of
https://github.com/nuxsmin/sysPass.git
synced 2026-03-04 15:44:07 +01:00
* [MOD] Remote Syslog tweaking
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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());
|
||||
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -34,6 +34,7 @@ interface ConnectionInterface
|
||||
{
|
||||
const TYPE_TCP = 1;
|
||||
const TYPE_UDP = 2;
|
||||
const SOCKET_TIMEOUT = 10;
|
||||
|
||||
/**
|
||||
* Obtener un socket
|
||||
|
||||
@@ -59,6 +59,7 @@
|
||||
</td>
|
||||
<td class="cell-description">
|
||||
<?php
|
||||
$description = str_replace(';;', PHP_EOL, $description);
|
||||
$descriptions = explode(';;', $description);
|
||||
|
||||
foreach ($descriptions as $text) {
|
||||
|
||||
@@ -59,7 +59,8 @@
|
||||
</td>
|
||||
<td class="cell-description">
|
||||
<?php
|
||||
$descriptions = explode(';;', $description);
|
||||
$description = str_replace(';;', PHP_EOL, $description);
|
||||
$descriptions = explode(PHP_EOL, $description);
|
||||
|
||||
foreach ($descriptions as $text) {
|
||||
if (preg_match('/^SQL.*/', $text)) {
|
||||
|
||||
Reference in New Issue
Block a user