Commit 534d873c authored by Sebastian Kummer's avatar Sebastian Kummer

ZP-972 Implement ZLog::SpecialLogUser() to be called before

authentication is completed to force the generation of a user log, fixed
method descriptions, use Log::unauthMessageCache for all unauthenticated
users, fixed ZLog dependency in PrintWBXML, removed
ZLog::RequestHeaders(), removed ZLog::SetSpecialLogUsers(), removed
ZLog::EnableDeviceLog().

Released under the Affero GNU General Public License (AGPL) version 3.
parent ced80a55
...@@ -46,22 +46,11 @@ class ZLog { ...@@ -46,22 +46,11 @@ class ZLog {
if (!defined('LOGLEVEL')) if (!defined('LOGLEVEL'))
define('LOGLEVEL', LOGLEVEL_OFF); define('LOGLEVEL', LOGLEVEL_OFF);
self::RequestHeader(); $logger = self::getLogger();
return true; return true;
} }
/**
* Write request header to log
*/
static protected function RequestHeader() {
self::Write(LOGLEVEL_DEBUG,"-------- Start");
self::Write(LOGLEVEL_DEBUG,
sprintf("cmd='%s' devType='%s' devId='%s' getUser='%s' from='%s' version='%s' method='%s'",
Request::GetCommand(), Request::GetDeviceType(), Request::GetDeviceID(), Request::GetGETUser(),
Request::GetRemoteAddr(), @constant('ZPUSH_VERSION'), Request::GetMethod()));
}
/** /**
* Check if WBXML logging is enabled in current LOG(USER)LEVEL. * Check if WBXML logging is enabled in current LOG(USER)LEVEL.
* *
...@@ -126,42 +115,16 @@ class ZLog { ...@@ -126,42 +115,16 @@ class ZLog {
} }
/** /**
* Set given users, so they get an extra log-file. * If called, the authenticated current user gets an extra log-file.
*
* Depending on when you call that function, instead of defining users in config.php,
* you might not get all log-messages.
* Setting it eg. in Login method of backend misses Start and cmd='***' lines.
*
* @param array $users
*/
static public function SetSpecialLogUsers($users) {
self::getLogger()->SetSpecialLogUsers($users);
}
/**
* Enable device specific logging for a given device to $devId.log.
* *
* If logger does not support setting a specific file, logging will happen * If called until the user is authenticated (e.g. at the end of IBackend->Logon()) all log
* as if enabled for the user of the device. * messages that happened until this point will also be logged.
* *
* @param string $devId
* @access public * @access public
*
* @return void * @return void
*/ */
public function EnableDeviceLog($devId) { static public function SpecialLogUser() {
global $specialLogUsers; // This variable comes from the configuration file (config.php) self::getLogger()->SpecialLogUser();
$logger = self::getLogger();
if (strtolower($devId) === $logger->GetDevid()) {
if (!in_array($logger->GetAuthUser(), $specialLogUsers)) {
$specialLogUsers[] = $logger->GetAuthUser();
}
$logger->SetSpecialLogUsers($specialLogUsers);
$logger->SetLogToUserFile(preg_replace('/[^a-z0-9]/', '_', $logger->GetDevid()).'.log');
self::RequestHeader();
}
} }
/** /**
......
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
* *
* Consult LICENSE file for details * Consult LICENSE file for details
************************************************/ ************************************************/
class FileLog extends Log { class FileLog extends Log {
/** /**
...@@ -43,7 +44,7 @@ class FileLog extends Log { ...@@ -43,7 +44,7 @@ class FileLog extends Log {
*/ */
private function getLogToUserFile() { private function getLogToUserFile() {
if ($this->log_to_user_file === false) { if ($this->log_to_user_file === false) {
$this->SetLogToUserFile(preg_replace('/[^a-z0-9]/', '_', strtolower($this->GetAuthUser())) . '.log'); $this->setLogToUserFile(preg_replace('/[^a-z0-9]/', '_', strtolower($this->GetAuthUser())) . '.log');
} }
return $this->log_to_user_file; return $this->log_to_user_file;
} }
...@@ -53,10 +54,10 @@ class FileLog extends Log { ...@@ -53,10 +54,10 @@ class FileLog extends Log {
* *
* @param string $value * @param string $value
* *
* @access public * @access private
* @return void * @return void
*/ */
public function SetLogToUserFile($value) { private function setLogToUserFile($value) {
$this->log_to_user_file = $value; $this->log_to_user_file = $value;
} }
...@@ -82,6 +83,15 @@ class FileLog extends Log { ...@@ -82,6 +83,15 @@ class FileLog extends Log {
// Implementation of Log // Implementation of Log
// //
/**
* Writes a log message to the general log.
*
* @param int $loglevel
* @param string $message
*
* @access protected
* @return void
*/
protected function Write($loglevel, $message) { protected function Write($loglevel, $message) {
$data = $this->buildLogString($loglevel, $message) . PHP_EOL; $data = $this->buildLogString($loglevel, $message) . PHP_EOL;
@file_put_contents(LOGFILE, $data, FILE_APPEND); @file_put_contents(LOGFILE, $data, FILE_APPEND);
...@@ -91,11 +101,26 @@ class FileLog extends Log { ...@@ -91,11 +101,26 @@ class FileLog extends Log {
} }
} }
/**
* Writes a log message to the user specific log.
* @param int $loglevel
* @param string $message
*
* @access public
* @return void
*/
public function WriteForUser($loglevel, $message) { public function WriteForUser($loglevel, $message) {
$data = $this->buildLogString($loglevel, $message) . PHP_EOL; $data = $this->buildLogString($loglevel, $message) . PHP_EOL;
@file_put_contents(LOGFILEDIR . $this->getLogToUserFile(), $data, FILE_APPEND); @file_put_contents(LOGFILEDIR . $this->getLogToUserFile(), $data, FILE_APPEND);
} }
/**
* This function is used as an event for log implementer.
* It happens when the a call to the Log function is finished.
*
* @access protected
* @return void
*/
protected function afterLog($loglevel, $message) { protected function afterLog($loglevel, $message) {
if (($loglevel & LOGLEVEL_FATAL) || ($loglevel & LOGLEVEL_ERROR)) { if (($loglevel & LOGLEVEL_FATAL) || ($loglevel & LOGLEVEL_ERROR)) {
$data = $this->buildLogString($loglevel, $message) . PHP_EOL; $data = $this->buildLogString($loglevel, $message) . PHP_EOL;
......
...@@ -74,6 +74,8 @@ abstract class Log { ...@@ -74,6 +74,8 @@ abstract class Log {
} }
/** /**
* Returns the current user.
*
* @access public * @access public
* @return string * @return string
*/ */
...@@ -82,15 +84,20 @@ abstract class Log { ...@@ -82,15 +84,20 @@ abstract class Log {
} }
/** /**
* Sets the current user.
*
* @param string $value * @param string $value
* *
* @access public * @access public
* @return void
*/ */
public function SetUser($value) { public function SetUser($value) {
$this->user = $value; $this->user = $value;
} }
/** /**
* Returns the current authenticated user.
*
* @access public * @access public
* @return string * @return string
*/ */
...@@ -99,9 +106,12 @@ abstract class Log { ...@@ -99,9 +106,12 @@ abstract class Log {
} }
/** /**
* Sets the current authenticated user.
*
* @param string $value * @param string $value
* *
* @access public * @access public
* @return void
*/ */
public function SetAuthUser($value) { public function SetAuthUser($value) {
$this->isAuthUserInSpecialLogUsers = false; $this->isAuthUserInSpecialLogUsers = false;
...@@ -128,6 +138,8 @@ abstract class Log { ...@@ -128,6 +138,8 @@ abstract class Log {
} }
/** /**
* Returns the current device id.
*
* @access public * @access public
* @return string * @return string
*/ */
...@@ -136,15 +148,20 @@ abstract class Log { ...@@ -136,15 +148,20 @@ abstract class Log {
} }
/** /**
* Sets the current device id.
*
* @param string $value * @param string $value
* *
* @access public * @access public
* @return void
*/ */
public function SetDevid($value) { public function SetDevid($value) {
$this->devid = $value; $this->devid = $value;
} }
/** /**
* Returns the current PID (as string).
*
* @access public * @access public
* @return string * @return string
*/ */
...@@ -153,15 +170,20 @@ abstract class Log { ...@@ -153,15 +170,20 @@ abstract class Log {
} }
/** /**
* Sets the current PID.
*
* @param string $value * @param string $value
* *
* @access public * @access public
* @return void
*/ */
public function SetPidstr($value) { public function SetPidstr($value) {
$this->pidstr = $value; $this->pidstr = $value;
} }
/** /**
* Indicates if special log users are known.
*
* @access public * @access public
* @return bool True if we do have to log some specific user. False otherwise. * @return bool True if we do have to log some specific user. False otherwise.
*/ */
...@@ -170,6 +192,8 @@ abstract class Log { ...@@ -170,6 +192,8 @@ abstract class Log {
} }
/** /**
* Indicates if the user is in the special log users.
*
* @param string $user * @param string $user
* *
* @access public * @access public
...@@ -187,6 +211,8 @@ abstract class Log { ...@@ -187,6 +211,8 @@ abstract class Log {
} }
/** /**
* Returns the current special log users array.
*
* @access public * @access public
* @return array * @return array
*/ */
...@@ -195,15 +221,31 @@ abstract class Log { ...@@ -195,15 +221,31 @@ abstract class Log {
} }
/** /**
* Sets the current special log users array.
*
* @param array $value * @param array $value
* *
* @access public * @access public
* @return void
*/ */
public function SetSpecialLogUsers(array $value) { public function SetSpecialLogUsers(array $value) {
$this->isUserInSpecialLogUsers = array(); // reset cache $this->isUserInSpecialLogUsers = array(); // reset cache
$this->specialLogUsers = $value; $this->specialLogUsers = $value;
} }
/**
* If called, the current user should get an extra log-file.
*
* If called until the user is authenticated (e.g. at the end of IBackend->Logon()) all
* messages logged until then will also be logged in the user file.
*
* @access public
* @return void
*/
public function SpecialLogUser() {
$this->isAuthUserInSpecialLogUsers = true;
}
/** /**
* Logs a message with a given log level. * Logs a message with a given log level.
* *
...@@ -218,9 +260,14 @@ abstract class Log { ...@@ -218,9 +260,14 @@ abstract class Log {
if ($loglevel <= LOGLEVEL) { if ($loglevel <= LOGLEVEL) {
$this->Write($loglevel, $message); $this->Write($loglevel, $message);
} }
if ($loglevel <= LOGUSERLEVEL && $this->IsAuthUserInSpecialLogUsers()) { if ($loglevel <= LOGUSERLEVEL) {
if (RequestProcessor::isUserAuthenticated()) { // cache log messages for unauthenticated users
// something was logged before the user was authenticated, write this to the log if (!RequestProcessor::isUserAuthenticated()) {
$this->unauthMessageCache[] = array($loglevel, $message);
}
// user is authenticated now
elseif ($this->IsAuthUserInSpecialLogUsers()) {
// something was logged before the user was authenticated and cached write it to the log
if (!empty($this->unauthMessageCache)) { if (!empty($this->unauthMessageCache)) {
foreach ($this->unauthMessageCache as $authcache) { foreach ($this->unauthMessageCache as $authcache) {
$this->WriteForUser($authcache[0], $authcache[1]); $this->WriteForUser($authcache[0], $authcache[1]);
...@@ -247,17 +294,6 @@ abstract class Log { ...@@ -247,17 +294,6 @@ abstract class Log {
public function AfterInitialize() { public function AfterInitialize() {
} }
/**
* Set user log-file relative to log directory.
*
* @param string $value
*
* @access public
* @return void
*/
public function SetLogToUserFile($value) {
}
/** /**
* This function is used as an event for log implementer. * This function is used as an event for log implementer.
* It happens when the a call to the Log function is finished. * It happens when the a call to the Log function is finished.
...@@ -297,20 +333,23 @@ abstract class Log { ...@@ -297,20 +333,23 @@ abstract class Log {
} }
/** /**
* Writes a log message to the general log.
*
* @param int $loglevel * @param int $loglevel
* @param string $message * @param string $message
* *
* @access public * @access protected
* @return null * @return void
*/ */
abstract protected function Write($loglevel, $message); abstract protected function Write($loglevel, $message);
/** /**
* Writes a log message to the user specific log.
* @param int $loglevel * @param int $loglevel
* @param string $message * @param string $message
* *
* @access public * @access public
* @return null * @return void
*/ */
abstract public function WriteForUser($loglevel, $message); abstract public function WriteForUser($loglevel, $message);
} }
\ No newline at end of file
...@@ -84,6 +84,14 @@ class Syslog extends Log { ...@@ -84,6 +84,14 @@ class Syslog extends Log {
} }
} }
/**
* Constructor.
* Sets configured values if no parameters are given. *
*
* @param string $program_name
* @param string $host
* @param string $port
*/
public function __construct($program_name = null, $host = null, $port = null) { public function __construct($program_name = null, $host = null, $port = null) {
parent::__construct(); parent::__construct();
...@@ -104,7 +112,6 @@ class Syslog extends Log { ...@@ -104,7 +112,6 @@ class Syslog extends Log {
* @return string * @return string
*/ */
protected function GenerateProgramName() { protected function GenerateProgramName() {
// @TODO Use another mechanism than debug_backtrace to determine to origin of the log // @TODO Use another mechanism than debug_backtrace to determine to origin of the log
$backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
// Shift the "syslog.php" entry. // Shift the "syslog.php" entry.
...@@ -166,11 +173,19 @@ class Syslog extends Log { ...@@ -166,11 +173,19 @@ class Syslog extends Log {
return $log; return $log;
} }
// //
// Implementation of Log // Implementation of Log
// //
/**
* Writes a log message to the general log.
*
* @param int $loglevel
* @param string $message
*
* @access protected
* @return void
*/
protected function Write($loglevel, $message) { protected function Write($loglevel, $message) {
if ($this->GetHost() && $this->GetPort()) { if ($this->GetHost() && $this->GetPort()) {
$sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); $sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
...@@ -191,6 +206,13 @@ class Syslog extends Log { ...@@ -191,6 +206,13 @@ class Syslog extends Log {
} }
} }
/**
* This function is used as an event for log implementer.
* It happens when the a call to the Log function is finished.
*
* @access protected
* @return void
*/
public function WriteForUser($loglevel, $message) { public function WriteForUser($loglevel, $message) {
$this->Write(LOGLEVEL_DEBUG, $message); // Always pass the logleveldebug so it uses syslog level LOG_DEBUG $this->Write(LOGLEVEL_DEBUG, $message); // Always pass the logleveldebug so it uses syslog level LOG_DEBUG
} }
......
...@@ -69,6 +69,10 @@ class ZLog { ...@@ -69,6 +69,10 @@ class ZLog {
} }
} }
} }
static public function IsWbxmlDebugEnabled() {
return true;
}
} }
// setup // setup
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment