Issues (1513)

lib/log/filelog.php (4 issues)

1
<?php
2
/*
3
 * SPDX-License-Identifier: AGPL-3.0-only
4
 * SPDX-FileCopyrightText: Copyright 2007-2016 Zarafa Deutschland GmbH
5
 * SPDX-FileCopyrightText: Copyright 2020-2022 grommunio GmbH
6
 *
7
 * Logging functionalities
8
 */
9
10
class FileLog extends Log {
11
	/**
12
	 * @var bool|string
13
	 */
14
	private $log_to_user_file = false;
15
16
	/**
17
	 * Constructor.
18
	 */
19
	public function __construct() {
20
	}
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;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->log_to_user_file also could return the type true which is incompatible with the documented return type string.
Loading history...
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);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for file_put_contents(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

95
		/** @scrutinizer ignore-unhandled */ @file_put_contents(LOGFILE, $data, FILE_APPEND);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
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);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for file_put_contents(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

106
		/** @scrutinizer ignore-unhandled */ @file_put_contents(LOGFILEDIR . $this->getLogToUserFile(), $data, FILE_APPEND);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
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);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for file_put_contents(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

119
			/** @scrutinizer ignore-unhandled */ @file_put_contents(LOGERRORFILE, $data, FILE_APPEND);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
120
		}
121
	}
122
}
123