Passed
Push — master ( 4a52d9...223a91 )
by Morris
11:47 queued 10s
created

LogDetails::logDetailsAsJSON()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 7
nc 4
nop 3
dl 0
loc 14
rs 10
c 1
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2019 Julius Härtl <[email protected]>
4
 *
5
 * @author Julius Härtl <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace OC\Log;
25
26
use OC\SystemConfig;
27
28
abstract class LogDetails {
29
30
	/** @var SystemConfig */
31
	private $config;
32
33
	public function __construct(SystemConfig $config) {
34
		$this->config = $config;
35
	}
36
37
	public function logDetails(string $app, $message, int $level): array {
38
		// default to ISO8601
39
		$format = $this->config->getValue('logdateformat', \DateTime::ATOM);
40
		$logTimeZone = $this->config->getValue('logtimezone', 'UTC');
41
		try {
42
			$timezone = new \DateTimeZone($logTimeZone);
43
		} catch (\Exception $e) {
44
			$timezone = new \DateTimeZone('UTC');
45
		}
46
		$time = \DateTime::createFromFormat("U.u", number_format(microtime(true), 4, ".", ""));
47
		if ($time === false) {
48
			$time = new \DateTime(null, $timezone);
49
		} else {
50
			// apply timezone if $time is created from UNIX timestamp
51
			$time->setTimezone($timezone);
52
		}
53
		$request = \OC::$server->getRequest();
54
		$reqId = $request->getId();
55
		$remoteAddr = $request->getRemoteAddress();
56
		// remove username/passwords from URLs before writing the to the log file
57
		$time = $time->format($format);
58
		$url = ($request->getRequestUri() !== '') ? $request->getRequestUri() : '--';
59
		$method = is_string($request->getMethod()) ? $request->getMethod() : '--';
0 ignored issues
show
introduced by
The condition is_string($request->getMethod()) is always true.
Loading history...
60
		if($this->config->getValue('installed', false)) {
61
			$user = \OC_User::getUser() ? \OC_User::getUser() : '--';
62
		} else {
63
			$user = '--';
64
		}
65
		$userAgent = $request->getHeader('User-Agent');
66
		if ($userAgent === '') {
67
			$userAgent = '--';
68
		}
69
		$version = $this->config->getValue('version', '');
70
		$entry = compact(
71
			'reqId',
72
			'level',
73
			'time',
74
			'remoteAddr',
75
			'user',
76
			'app',
77
			'method',
78
			'url',
79
			'message',
80
			'userAgent',
81
			'version'
82
		);
83
		return $entry;
84
	}
85
86
	public function logDetailsAsJSON(string $app, $message, int $level): string {
87
		$entry = $this->logDetails($app, $message, $level);
88
		// PHP's json_encode only accept proper UTF-8 strings, loop over all
89
		// elements to ensure that they are properly UTF-8 compliant or convert
90
		// them manually.
91
		foreach($entry as $key => $value) {
92
			if(is_string($value)) {
93
				$testEncode = json_encode($value);
94
				if($testEncode === false) {
95
					$entry[$key] = utf8_encode($value);
96
				}
97
			}
98
		}
99
		return json_encode($entry, JSON_PARTIAL_OUTPUT_ON_ERROR);
100
	}
101
}
102