Completed
Push — master ( d97cd9...b7b2f6 )
by Thomas
11:48
created

Owncloud::write()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 4
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author Andreas Fischer <[email protected]>
4
 * @author Bart Visscher <[email protected]>
5
 * @author Georg Ehrke <[email protected]>
6
 * @author Lukas Reschke <[email protected]>
7
 * @author Michael Gapczynski <[email protected]>
8
 * @author Morris Jobke <[email protected]>
9
 * @author Phiber2000 <[email protected]>
10
 * @author Roeland Jago Douma <[email protected]>
11
 * @author Thomas Müller <[email protected]>
12
 * @author Vincent Petry <[email protected]>
13
 *
14
 * @copyright Copyright (c) 2018, ownCloud GmbH
15
 * @license AGPL-3.0
16
 *
17
 * This code is free software: you can redistribute it and/or modify
18
 * it under the terms of the GNU Affero General Public License, version 3,
19
 * as published by the Free Software Foundation.
20
 *
21
 * This program is distributed in the hope that it will be useful,
22
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24
 * GNU Affero General Public License for more details.
25
 *
26
 * You should have received a copy of the GNU Affero General Public License, version 3,
27
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
28
 *
29
 */
30
31
namespace OC\Log;
32
33
/**
34
 * logging utilities
35
 *
36
 * Log is saved at data/owncloud.log (on default)
37
 */
38
39
class Owncloud {
40
	protected static $logFile;
41
42
	/**
43
	 * Init class data
44
	 */
45
	public static function init() {
46
		$systemConfig = \OC::$server->getSystemConfig();
47
		$defaultLogFile = $systemConfig->getValue("datadirectory", \OC::$SERVERROOT.'/data').'/owncloud.log';
48
		self::$logFile = $systemConfig->getValue("logfile", $defaultLogFile);
49
50
		/**
51
		 * Fall back to default log file if specified logfile does not exist
52
		 * and can not be created.
53
		 */
54
		if (!\file_exists(self::$logFile)) {
55
			if (!\is_writable(\dirname(self::$logFile))) {
56
				self::$logFile = $defaultLogFile;
57
			} else {
58
				if (!\touch(self::$logFile)) {
59
					self::$logFile = $defaultLogFile;
60
				}
61
			}
62
		}
63
	}
64
65
	/**
66
	 * write a message in the log
67
	 * @param string $app
68
	 * @param string $message
69
	 * @param int $level
70
	 * @param string conditionalLogFile
71
	 */
72
	public static function write($app, $message, $level, $conditionalLogFile = null) {
73
		return self::writeExtra($app, $message, $level, $conditionalLogFile, []);
74
	}
75
76
	public static function writeExtra($app, $message, $level, $conditionalLogFile, $extraFields = []) {
77
		$config = \OC::$server->getSystemConfig();
78
79
		// default to ISO8601
80
		$format = $config->getValue('logdateformat', 'c');
81
		$logTimeZone = $config->getValue("logtimezone", 'UTC');
82
		try {
83
			$timezone = new \DateTimeZone($logTimeZone);
84
		} catch (\Exception $e) {
85
			$timezone = new \DateTimeZone('UTC');
86
		}
87
		$time = \DateTime::createFromFormat("U.u", \number_format(\microtime(true), 4, ".", ""));
88
		if ($time === false) {
89
			$time = new \DateTime(null, $timezone);
90
		} else {
91
			// apply timezone if $time is created from UNIX timestamp
92
			$time->setTimezone($timezone);
93
		}
94
		$request = \OC::$server->getRequest();
95
		$reqId = $request->getId();
96
		$remoteAddr = $request->getRemoteAddress();
97
		// remove username/passwords from URLs before writing the to the log file
98
		$time = $time->format($format);
99
		$url = ($request->getRequestUri() !== '') ? $request->getRequestUri() : '--';
100
		$method = \is_string($request->getMethod()) ? $request->getMethod() : '--';
101 View Code Duplication
		if (\OC::$server->getConfig()->getSystemValue('installed', false)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
102
			$user = (\OC_User::getUser()) ? \OC_User::getUser() : '--';
103
		} else {
104
			$user = '--';
105
		}
106
		$entry = \compact(
107
			'reqId',
108
			'level',
109
			'time',
110
			'remoteAddr',
111
			'user',
112
			'app',
113
			'method',
114
			'url',
115
			'message'
116
		);
117
118
		if (!empty($extraFields)) {
119
			// augment with additional fields
120
			$entry = \array_merge($entry, $extraFields);
121
		}
122
123
		$entry = \json_encode($entry);
124
		if ($conditionalLogFile !== null) {
125
			if ($conditionalLogFile[0] !== '/') {
126
				$conditionalLogFile = \OC::$server->getConfig()->getSystemValue('datadirectory') . "/" . $conditionalLogFile;
127
			}
128
			$handle = @\fopen($conditionalLogFile, 'a');
129
			@\chmod($conditionalLogFile, 0640);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

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...
130
		} else {
131
			$handle = @\fopen(self::$logFile, 'a');
132
			@\chmod(self::$logFile, 0640);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

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...
133
		}
134
		if ($handle) {
135
			\fwrite($handle, $entry."\n");
136
			\fclose($handle);
137
		} else {
138
			// Fall back to error_log
139
			\error_log($entry);
140
		}
141
		if (\php_sapi_name() === 'cli-server') {
142
			\error_log($message, 4);
143
		}
144
	}
145
146
	/**
147
	 * @return string
148
	 */
149
	public static function getLogFilePath() {
150
		return self::$logFile;
151
	}
152
}
153