|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only |
|
5
|
|
|
* SPDX-FileCopyrightText: Copyright 2007-2016 Zarafa Deutschland GmbH |
|
6
|
|
|
* SPDX-FileCopyrightText: Copyright 2020-2022 grommunio GmbH |
|
7
|
|
|
* |
|
8
|
|
|
* Logging functionalities |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
class FileLog extends Log { |
|
12
|
|
|
/** |
|
13
|
|
|
* @var bool|string |
|
14
|
|
|
*/ |
|
15
|
|
|
private $log_to_user_file = false; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Constructor. |
|
19
|
|
|
*/ |
|
20
|
|
|
public function __construct() {} |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Get the log user file. |
|
24
|
|
|
* |
|
25
|
|
|
* @return string |
|
26
|
|
|
*/ |
|
27
|
|
|
private function getLogToUserFile() { |
|
28
|
|
|
if ($this->log_to_user_file === false) { |
|
29
|
|
|
if (in_array(strtolower($this->GetDevid()), ['', 'validate'])) { |
|
30
|
|
|
$this->setLogToUserFile(preg_replace('/[^a-z0-9]/', '_', strtolower($this->GetAuthUser())) . '.log'); |
|
31
|
|
|
} |
|
32
|
|
|
else { |
|
33
|
|
|
$this->setLogToUserFile( |
|
34
|
|
|
preg_replace('/[^a-z0-9]/', '_', strtolower($this->GetAuthUser())) . '-' . |
|
35
|
|
|
(($this->GetAuthUser() != $this->GetUser()) ? preg_replace('/[^a-z0-9]/', '_', strtolower($this->GetUser())) . '-' : '') . |
|
36
|
|
|
preg_replace('/[^a-z0-9]/', '_', strtolower($this->GetDevid())) . |
|
37
|
|
|
'.log' |
|
38
|
|
|
); |
|
39
|
|
|
} |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
return $this->log_to_user_file; |
|
|
|
|
|
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Set user log-file relative to log directory. |
|
47
|
|
|
* |
|
48
|
|
|
* @param string $value |
|
49
|
|
|
*/ |
|
50
|
|
|
private function setLogToUserFile($value) { |
|
51
|
|
|
$this->log_to_user_file = $value; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Returns the string to be logged. |
|
56
|
|
|
* |
|
57
|
|
|
* @param int $loglevel |
|
58
|
|
|
* @param string $message |
|
59
|
|
|
* @param bool $includeUserDevice puts username and device in the string, default: true |
|
60
|
|
|
* |
|
61
|
|
|
* @return string |
|
62
|
|
|
*/ |
|
63
|
|
|
public function BuildLogString($loglevel, $message, $includeUserDevice = true) { |
|
64
|
|
|
$log = Utils::GetFormattedTime() . ' [' . str_pad($this->GetPid(), 5, " ", STR_PAD_LEFT) . '] ' . $this->GetLogLevelString($loglevel, LOGLEVEL >= LOGLEVEL_INFO); |
|
65
|
|
|
|
|
66
|
|
|
if ($includeUserDevice) { |
|
67
|
|
|
// when the users differ, we need to log both |
|
68
|
|
|
if (strcasecmp($this->GetAuthUser(), $this->GetUser()) == 0) { |
|
69
|
|
|
$log .= ' [' . $this->GetUser() . ']'; |
|
70
|
|
|
} |
|
71
|
|
|
else { |
|
72
|
|
|
$log .= ' [' . $this->GetAuthUser() . Request::IMPERSONATE_DELIM . $this->GetUser() . ']'; |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
if ($includeUserDevice && (LOGLEVEL >= LOGLEVEL_DEVICEID || (LOGUSERLEVEL >= LOGLEVEL_DEVICEID && $this->IsAuthUserInSpecialLogUsers()))) { |
|
76
|
|
|
$log .= ' [' . $this->GetDevid() . ']'; |
|
77
|
|
|
} |
|
78
|
|
|
$log .= ' ' . $message; |
|
79
|
|
|
|
|
80
|
|
|
return $log; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
// |
|
84
|
|
|
// Implementation of Log |
|
85
|
|
|
// |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Writes a log message to the general log. |
|
89
|
|
|
* |
|
90
|
|
|
* @param int $loglevel |
|
91
|
|
|
* @param string $message |
|
92
|
|
|
*/ |
|
93
|
|
|
protected function Write($loglevel, $message) { |
|
94
|
|
|
$data = $this->BuildLogString($loglevel, $message) . PHP_EOL; |
|
95
|
|
|
@file_put_contents(LOGFILE, $data, FILE_APPEND); |
|
|
|
|
|
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Writes a log message to the user specific log. |
|
100
|
|
|
* |
|
101
|
|
|
* @param int $loglevel |
|
102
|
|
|
* @param string $message |
|
103
|
|
|
*/ |
|
104
|
|
|
public function WriteForUser($loglevel, $message) { |
|
105
|
|
|
$data = $this->BuildLogString($loglevel, $message, false) . PHP_EOL; |
|
106
|
|
|
@file_put_contents(LOGFILEDIR . $this->getLogToUserFile(), $data, FILE_APPEND); |
|
|
|
|
|
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* This function is used as an event for log implementer. |
|
111
|
|
|
* It happens when the a call to the Log function is finished. |
|
112
|
|
|
* |
|
113
|
|
|
* @param mixed $loglevel |
|
114
|
|
|
* @param mixed $message |
|
115
|
|
|
*/ |
|
116
|
|
|
protected function afterLog($loglevel, $message) { |
|
117
|
|
|
if ($loglevel & (LOGLEVEL_FATAL | LOGLEVEL_ERROR | LOGLEVEL_WARN)) { |
|
118
|
|
|
$data = $this->BuildLogString($loglevel, $message) . PHP_EOL; |
|
119
|
|
|
@file_put_contents(LOGERRORFILE, $data, FILE_APPEND); |
|
|
|
|
|
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|