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
|
|
|
abstract class Log { |
12
|
|
|
/** |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
protected $user = ''; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
protected $authUser = ''; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $devid = ''; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $pid = ''; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
protected $specialLogUsers = []; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Only used as a cache value for IsUserInSpecialLogUsers. |
39
|
|
|
* |
40
|
|
|
* @var array |
41
|
|
|
*/ |
42
|
|
|
private $isUserInSpecialLogUsers = []; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Only used as a cache value for IsAuthUserInSpecialLogUsers function. |
46
|
|
|
* |
47
|
|
|
* @var bool |
48
|
|
|
*/ |
49
|
|
|
private $isAuthUserInSpecialLogUsers = false; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var array |
53
|
|
|
*/ |
54
|
|
|
private $unauthMessageCache = []; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Constructor. |
58
|
|
|
*/ |
59
|
|
|
public function __construct() {} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Returns the current user. |
63
|
|
|
* |
64
|
|
|
* @return string |
65
|
|
|
*/ |
66
|
|
|
public function GetUser() { |
67
|
|
|
return $this->user; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Sets the current user. |
72
|
|
|
* |
73
|
|
|
* @param string $value |
74
|
|
|
*/ |
75
|
|
|
public function SetUser($value) { |
76
|
|
|
$this->user = $value; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Returns the current authenticated user. |
81
|
|
|
* |
82
|
|
|
* @return string |
83
|
|
|
*/ |
84
|
|
|
public function GetAuthUser() { |
85
|
|
|
return $this->authUser; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Sets the current authenticated user. |
90
|
|
|
* |
91
|
|
|
* @param string $value |
92
|
|
|
*/ |
93
|
|
|
public function SetAuthUser($value) { |
94
|
|
|
$this->isAuthUserInSpecialLogUsers = false; |
95
|
|
|
$this->authUser = $value; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Check that the current authUser ($this->GetAuthUser) is in the special log user array. |
100
|
|
|
* This call is equivalent to `$this->IsUserInSpecialLogUsers($this->GetAuthUser())` at the exception that this |
101
|
|
|
* call uses cache so there won't be more than one check to the specialLogUser for the AuthUser. |
102
|
|
|
* |
103
|
|
|
* @return bool |
104
|
|
|
*/ |
105
|
|
|
public function IsAuthUserInSpecialLogUsers() { |
106
|
|
|
if ($this->isAuthUserInSpecialLogUsers) { |
107
|
|
|
return true; |
108
|
|
|
} |
109
|
|
|
if ($this->IsUserInSpecialLogUsers($this->GetAuthUser())) { |
110
|
|
|
$this->isAuthUserInSpecialLogUsers = true; |
111
|
|
|
|
112
|
|
|
return true; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return false; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Returns the current device id. |
120
|
|
|
* |
121
|
|
|
* @return string |
122
|
|
|
*/ |
123
|
|
|
public function GetDevid() { |
124
|
|
|
return $this->devid; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Sets the current device id. |
129
|
|
|
* |
130
|
|
|
* @param string $value |
131
|
|
|
*/ |
132
|
|
|
public function SetDevid($value) { |
133
|
|
|
$this->devid = $value; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Returns the current PID (as string). |
138
|
|
|
* |
139
|
|
|
* @return string |
140
|
|
|
*/ |
141
|
|
|
public function GetPid() { |
142
|
|
|
return $this->pid; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Sets the current PID. |
147
|
|
|
* |
148
|
|
|
* @param string $value |
149
|
|
|
*/ |
150
|
|
|
public function SetPid($value) { |
151
|
|
|
$this->pid = $value; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Indicates if special log users are known. |
156
|
|
|
* |
157
|
|
|
* @return bool True if we do have to log some specific user. False otherwise. |
158
|
|
|
*/ |
159
|
|
|
public function HasSpecialLogUsers() { |
160
|
|
|
return !empty($this->specialLogUsers) || $this->isAuthUserInSpecialLogUsers; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Indicates if the user is in the special log users. |
165
|
|
|
* |
166
|
|
|
* @param string $user |
167
|
|
|
* |
168
|
|
|
* @return bool |
169
|
|
|
*/ |
170
|
|
|
public function IsUserInSpecialLogUsers($user) { |
171
|
|
|
if (isset($this->isUserInSpecialLogUsers[$user])) { |
172
|
|
|
return true; |
173
|
|
|
} |
174
|
|
|
if ($this->HasSpecialLogUsers() && in_array($user, $this->GetSpecialLogUsers())) { |
175
|
|
|
$this->isUserInSpecialLogUsers[$user] = true; |
176
|
|
|
|
177
|
|
|
return true; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
return false; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Returns the current special log users array. |
185
|
|
|
* |
186
|
|
|
* @return array |
187
|
|
|
*/ |
188
|
|
|
public function GetSpecialLogUsers() { |
189
|
|
|
return $this->specialLogUsers; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Sets the current special log users array. |
194
|
|
|
*/ |
195
|
|
|
public function SetSpecialLogUsers(array $value) { |
196
|
|
|
$this->isUserInSpecialLogUsers = []; // reset cache |
197
|
|
|
$this->specialLogUsers = $value; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* If called, the current user should get an extra log-file. |
202
|
|
|
* |
203
|
|
|
* If called until the user is authenticated (e.g. at the end of IBackend->Logon()) all |
204
|
|
|
* messages logged until then will also be logged in the user file. |
205
|
|
|
*/ |
206
|
|
|
public function SpecialLogUser() { |
207
|
|
|
$this->isAuthUserInSpecialLogUsers = true; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Logs a message with a given log level. |
212
|
|
|
* |
213
|
|
|
* @param int $loglevel |
214
|
|
|
* @param string $message |
215
|
|
|
*/ |
216
|
|
|
public function Log($loglevel, $message) { |
217
|
|
|
if ($loglevel <= LOGLEVEL) { |
218
|
|
|
$this->Write($loglevel, $message); |
219
|
|
|
} |
220
|
|
|
if ($loglevel <= LOGUSERLEVEL) { |
221
|
|
|
// cache log messages for unauthenticated users |
222
|
|
|
if (!RequestProcessor::isUserAuthenticated()) { |
223
|
|
|
$this->unauthMessageCache[] = [$loglevel, $message]; |
224
|
|
|
} |
225
|
|
|
// user is authenticated now |
226
|
|
|
elseif ($this->IsAuthUserInSpecialLogUsers()) { |
227
|
|
|
// something was logged before the user was authenticated and cached write it to the log |
228
|
|
|
if (!empty($this->unauthMessageCache)) { |
229
|
|
|
foreach ($this->unauthMessageCache as $authcache) { |
230
|
|
|
$this->WriteForUser($authcache[0], $authcache[1]); |
231
|
|
|
} |
232
|
|
|
$this->unauthMessageCache = []; |
233
|
|
|
} |
234
|
|
|
$this->WriteForUser($loglevel, $message); |
235
|
|
|
} |
236
|
|
|
else { |
237
|
|
|
$this->unauthMessageCache[] = [$loglevel, $message]; |
238
|
|
|
} |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
$this->afterLog($loglevel, $message); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* This function is used as an event for log implementer. |
246
|
|
|
* It happens when the SLog static class is finished with the initialization of this instance. |
247
|
|
|
*/ |
248
|
|
|
public function AfterInitialize() {} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* This function is used as an event for log implementer. |
252
|
|
|
* It happens when the a call to the Log function is finished. |
253
|
|
|
* |
254
|
|
|
* @param mixed $loglevel |
255
|
|
|
* @param mixed $message |
256
|
|
|
*/ |
257
|
|
|
protected function afterLog($loglevel, $message) {} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* Returns the string representation of the given $loglevel. |
261
|
|
|
* String can be padded. |
262
|
|
|
* |
263
|
|
|
* @param int $loglevel one of the LOGLEVELs |
264
|
|
|
* @param bool $pad |
265
|
|
|
* |
266
|
|
|
* @return string |
267
|
|
|
*/ |
268
|
|
|
protected function GetLogLevelString($loglevel, $pad = false) { |
269
|
|
|
if ($pad) { |
270
|
|
|
$s = " "; |
271
|
|
|
} |
272
|
|
|
else { |
273
|
|
|
$s = ""; |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
return match ($loglevel) { |
277
|
|
|
LOGLEVEL_OFF => "", |
278
|
|
|
LOGLEVEL_FATAL => "[FATAL]", |
279
|
|
|
LOGLEVEL_ERROR => "[ERROR]", |
280
|
|
|
LOGLEVEL_WARN => "[" . $s . "WARN]", |
281
|
|
|
LOGLEVEL_INFO => "[" . $s . "INFO]", |
282
|
|
|
LOGLEVEL_DEBUG => "[DEBUG]", |
283
|
|
|
LOGLEVEL_WBXML => "[WBXML]", |
284
|
|
|
LOGLEVEL_DEVICEID => "[DEVICEID]", |
285
|
|
|
LOGLEVEL_WBXMLSTACK => "[WBXMLSTACK]", |
286
|
|
|
default => "", |
287
|
|
|
}; |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* Writes a log message to the general log. |
292
|
|
|
* |
293
|
|
|
* @param int $loglevel |
294
|
|
|
* @param string $message |
295
|
|
|
*/ |
296
|
|
|
abstract protected function Write($loglevel, $message); |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* Writes a log message to the user specific log. |
300
|
|
|
* |
301
|
|
|
* @param int $loglevel |
302
|
|
|
* @param string $message |
303
|
|
|
*/ |
304
|
|
|
abstract public function WriteForUser($loglevel, $message); |
305
|
|
|
} |
306
|
|
|
|