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 {
if (!defined('LOGLEVEL'))
define('LOGLEVEL', LOGLEVEL_OFF);
self::RequestHeader();
$logger = self::getLogger();
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.
*
......@@ -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.
* If called until the user is authenticated (e.g. at the end of IBackend->Logon()) all log
* messages that happened until this point will also be logged.
*
* @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
* as if enabled for the user of the device.
*
* @param string $devId
* @access public
*
* @return void
*/
public function EnableDeviceLog($devId) {
global $specialLogUsers; // This variable comes from the configuration file (config.php)
$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();
}
static public function SpecialLogUser() {
self::getLogger()->SpecialLogUser();
}
/**
......
......@@ -22,6 +22,7 @@
*
* Consult LICENSE file for details
************************************************/
class FileLog extends Log {
/**
......@@ -43,7 +44,7 @@ class FileLog extends Log {
*/
private function getLogToUserFile() {
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;
}
......@@ -53,10 +54,10 @@ class FileLog extends Log {
*
* @param string $value
*
* @access public
* @access private
* @return void
*/
public function SetLogToUserFile($value) {
private function setLogToUserFile($value) {
$this->log_to_user_file = $value;
}
......@@ -82,6 +83,15 @@ class FileLog extends 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) {
$data = $this->buildLogString($loglevel, $message) . PHP_EOL;
@file_put_contents(LOGFILE, $data, FILE_APPEND);
......@@ -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) {
$data = $this->buildLogString($loglevel, $message) . PHP_EOL;
@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) {
if (($loglevel & LOGLEVEL_FATAL) || ($loglevel & LOGLEVEL_ERROR)) {
$data = $this->buildLogString($loglevel, $message) . PHP_EOL;
......
......@@ -74,6 +74,8 @@ abstract class Log {
}
/**
* Returns the current user.
*
* @access public
* @return string
*/
......@@ -82,15 +84,20 @@ abstract class Log {
}
/**
* Sets the current user.
*
* @param string $value
*
* @access public
* @return void
*/
public function SetUser($value) {
$this->user = $value;
}
/**
* Returns the current authenticated user.
*
* @access public
* @return string
*/
......@@ -99,9 +106,12 @@ abstract class Log {
}
/**
* Sets the current authenticated user.
*
* @param string $value
*
* @access public
* @return void
*/
public function SetAuthUser($value) {
$this->isAuthUserInSpecialLogUsers = false;
......@@ -128,6 +138,8 @@ abstract class Log {
}
/**
* Returns the current device id.
*
* @access public
* @return string
*/
......@@ -136,15 +148,20 @@ abstract class Log {
}
/**
* Sets the current device id.
*
* @param string $value
*
* @access public
* @return void
*/
public function SetDevid($value) {
$this->devid = $value;
}
/**
* Returns the current PID (as string).
*
* @access public
* @return string
*/
......@@ -153,15 +170,20 @@ abstract class Log {
}
/**
* Sets the current PID.
*
* @param string $value
*
* @access public
* @return void
*/
public function SetPidstr($value) {
$this->pidstr = $value;
}
/**
* Indicates if special log users are known.
*
* @access public
* @return bool True if we do have to log some specific user. False otherwise.
*/
......@@ -170,6 +192,8 @@ abstract class Log {
}
/**
* Indicates if the user is in the special log users.
*
* @param string $user
*
* @access public
......@@ -187,6 +211,8 @@ abstract class Log {
}
/**
* Returns the current special log users array.
*
* @access public
* @return array
*/
......@@ -195,15 +221,31 @@ abstract class Log {
}
/**
* Sets the current special log users array.
*
* @param array $value
*
* @access public
* @return void
*/
public function SetSpecialLogUsers(array $value) {
$this->isUserInSpecialLogUsers = array(); // reset cache
$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.
*
......@@ -218,9 +260,14 @@ abstract class Log {
if ($loglevel <= LOGLEVEL) {
$this->Write($loglevel, $message);
}
if ($loglevel <= LOGUSERLEVEL && $this->IsAuthUserInSpecialLogUsers()) {
if (RequestProcessor::isUserAuthenticated()) {
// something was logged before the user was authenticated, write this to the log
if ($loglevel <= LOGUSERLEVEL) {
// cache log messages for unauthenticated users
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)) {
foreach ($this->unauthMessageCache as $authcache) {
$this->WriteForUser($authcache[0], $authcache[1]);
......@@ -247,17 +294,6 @@ abstract class Log {
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.
* It happens when the a call to the Log function is finished.
......@@ -297,20 +333,23 @@ abstract class Log {
}
/**
* Writes a log message to the general log.
*
* @param int $loglevel
* @param string $message
*
* @access public
* @return null
* @access protected
* @return void
*/
abstract protected function Write($loglevel, $message);
/**
* Writes a log message to the user specific log.
* @param int $loglevel
* @param string $message
*
* @access public
* @return null
* @return void
*/
abstract public function WriteForUser($loglevel, $message);
}
\ No newline at end of file
......@@ -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) {
parent::__construct();
......@@ -104,7 +112,6 @@ class Syslog extends Log {
* @return string
*/
protected function GenerateProgramName() {
// @TODO Use another mechanism than debug_backtrace to determine to origin of the log
$backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
// Shift the "syslog.php" entry.
......@@ -166,11 +173,19 @@ class Syslog extends Log {
return $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) {
if ($this->GetHost() && $this->GetPort()) {
$sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
......@@ -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) {
$this->Write(LOGLEVEL_DEBUG, $message); // Always pass the logleveldebug so it uses syslog level LOG_DEBUG
}
......
......@@ -69,6 +69,10 @@ class ZLog {
}
}
}
static public function IsWbxmlDebugEnabled() {
return true;
}
}
// 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