Owncloud::writeExtra()   F
last analyzed

Complexity

Conditions 12
Paths 1152

Size

Total Lines 69

Duplication

Lines 5
Ratio 7.25 %

Importance

Changes 0
Metric Value
cc 12
nc 1152
nop 5
dl 5
loc 69
rs 2.9163
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 (!self::createLogFile(self::$logFile)) {
55
			self::$logFile = $defaultLogFile;
56
		}
57
	}
58
59
	/**
60
	 * write a message in the log
61
	 * @param string $app
62
	 * @param string $message
63
	 * @param int $level
64
	 * @param string conditionalLogFile
65
	 */
66
	public static function write($app, $message, $level, $conditionalLogFile = null) {
67
		return self::writeExtra($app, $message, $level, $conditionalLogFile, []);
68
	}
69
70
	public static function writeExtra($app, $message, $level, $conditionalLogFile, $extraFields = []) {
71
		$config = \OC::$server->getSystemConfig();
72
73
		// default to ISO8601
74
		$format = $config->getValue('logdateformat', 'c');
75
		$logTimeZone = $config->getValue("logtimezone", 'UTC');
76
		try {
77
			$timezone = new \DateTimeZone($logTimeZone);
78
		} catch (\Exception $e) {
79
			$timezone = new \DateTimeZone('UTC');
80
		}
81
		$time = \DateTime::createFromFormat("U.u", \number_format(\microtime(true), 4, ".", ""));
82
		if ($time === false) {
83
			$time = new \DateTime(null, $timezone);
84
		} else {
85
			// apply timezone if $time is created from UNIX timestamp
86
			$time->setTimezone($timezone);
87
		}
88
		$request = \OC::$server->getRequest();
89
		$reqId = $request->getId();
90
		$remoteAddr = $request->getRemoteAddress();
91
		// remove username/passwords from URLs before writing the to the log file
92
		$time = $time->format($format);
93
		$url = ($request->getRequestUri() !== '') ? $request->getRequestUri() : '--';
94
		$method = \is_string($request->getMethod()) ? $request->getMethod() : '--';
95 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...
96
			$user = (\OC_User::getUser()) ? \OC_User::getUser() : '--';
97
		} else {
98
			$user = '--';
99
		}
100
		$entry = \compact(
101
			'reqId',
102
			'level',
103
			'time',
104
			'remoteAddr',
105
			'user',
106
			'app',
107
			'method',
108
			'url',
109
			'message'
110
		);
111
112
		if (!empty($extraFields)) {
113
			// augment with additional fields
114
			$entry = \array_merge($entry, $extraFields);
115
		}
116
117
		$entry = \json_encode($entry);
118
		if ($conditionalLogFile !== null) {
119
			if ($conditionalLogFile[0] !== '/') {
120
				$conditionalLogFile = \OC::$server->getConfig()->getSystemValue('datadirectory') . "/" . $conditionalLogFile;
121
			}
122
			self::createLogFile($conditionalLogFile);
123
			$handle = @\fopen($conditionalLogFile, 'a');
124
		} else {
125
			self::createLogFile(self::$logFile);
126
			$handle = @\fopen(self::$logFile, 'a');
127
		}
128
		if ($handle) {
129
			\fwrite($handle, $entry."\n");
130
			\fclose($handle);
131
		} else {
132
			// Fall back to error_log
133
			\error_log($entry);
134
		}
135
		if (\php_sapi_name() === 'cli-server') {
136
			\error_log($message, 4);
137
		}
138
	}
139
140
	/**
141
	 * create a log file and chmod it to the correct permissions
142
	 * @param string $logFile
143
	 * @return boolean
144
	 */
145
	public static function createLogFile($logFile) {
146
		if (\file_exists($logFile)) {
147
			return true;
148
		}
149
		if (\is_writable(\dirname($logFile)) && \touch($logFile)) {
150
			@\chmod($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...
151
			return true;
152
		}
153
		return false;
154
	}
155
	
156
	/**
157
	 * @return string
158
	 */
159
	public static function getLogFilePath() {
160
		return self::$logFile;
161
	}
162
}
163