Completed
Push — master ( 399f71...73ad09 )
by Thomas
19:59
created

Owncloud::getEntries()   C

Complexity

Conditions 11
Paths 2

Size

Total Lines 42
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 30
nc 2
nop 2
dl 0
loc 42
rs 5.2653
c 0
b 0
f 0

How to fix   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 Robin Appelman <[email protected]>
11
 * @author Roeland Jago Douma <[email protected]>
12
 * @author Thomas Müller <[email protected]>
13
 * @author Vincent Petry <[email protected]>
14
 *
15
 * @copyright Copyright (c) 2016, ownCloud GmbH.
16
 * @license AGPL-3.0
17
 *
18
 * This code is free software: you can redistribute it and/or modify
19
 * it under the terms of the GNU Affero General Public License, version 3,
20
 * as published by the Free Software Foundation.
21
 *
22
 * This program is distributed in the hope that it will be useful,
23
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25
 * GNU Affero General Public License for more details.
26
 *
27
 * You should have received a copy of the GNU Affero General Public License, version 3,
28
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
29
 *
30
 */
31
32
namespace OC\Log;
33
34
/**
35
 * logging utilities
36
 *
37
 * Log is saved at data/owncloud.log (on default)
38
 */
39
40
class Owncloud {
41
	static protected $logFile;
42
43
	/**
44
	 * Init class data
45
	 */
46
	public static function init() {
47
		$systemConfig = \OC::$server->getSystemConfig();
48
		$defaultLogFile = $systemConfig->getValue("datadirectory", \OC::$SERVERROOT.'/data').'/owncloud.log';
49
		self::$logFile = $systemConfig->getValue("logfile", $defaultLogFile);
50
51
		/**
52
		 * Fall back to default log file if specified logfile does not exist
53
		 * and can not be created.
54
		 */
55
		if (!file_exists(self::$logFile)) {
56
			if(!is_writable(dirname(self::$logFile))) {
57
				self::$logFile = $defaultLogFile;
58
			} else {
59
				if(!touch(self::$logFile)) {
60
					self::$logFile = $defaultLogFile;
61
				}
62
			}
63
		}
64
	}
65
66
	/**
67
	 * write a message in the log
68
	 * @param string $app
69
	 * @param string $message
70
	 * @param int $level
71
	 */
72
	public static function write($app, $message, $level) {
73
		$config = \OC::$server->getSystemConfig();
74
75
		// default to ISO8601
76
		$format = $config->getValue('logdateformat', 'c');
77
		$logTimeZone = $config->getValue( "logtimezone", 'UTC' );
78
		try {
79
			$timezone = new \DateTimeZone($logTimeZone);
80
		} catch (\Exception $e) {
81
			$timezone = new \DateTimeZone('UTC');
82
		}
83
		$time = \DateTime::createFromFormat("U.u", number_format(microtime(true), 4, ".", ""));
84
		if ($time === false) {
85
			$time = new \DateTime(null, $timezone);
86
		} else {
87
			// apply timezone if $time is created from UNIX timestamp
88
			$time->setTimezone($timezone);
89
		}
90
		$request = \OC::$server->getRequest();
91
		$reqId = $request->getId();
92
		$remoteAddr = $request->getRemoteAddress();
93
		// remove username/passwords from URLs before writing the to the log file
94
		$time = $time->format($format);
95
		$url = ($request->getRequestUri() !== '') ? $request->getRequestUri() : '--';
96
		$method = is_string($request->getMethod()) ? $request->getMethod() : '--';
97
		if(\OC::$server->getConfig()->getSystemValue('installed', false)) {
98
			$user = (\OC_User::getUser()) ? \OC_User::getUser() : '--';
99
		} else {
100
			$user = '--';
101
		}
102
		$entry = compact(
103
			'reqId',
104
			'remoteAddr',
105
			'app',
106
			'message',
107
			'level',
108
			'time',
109
			'method',
110
			'url',
111
			'user'
112
		);
113
		$entry = json_encode($entry);
114
		$handle = @fopen(self::$logFile, 'a');
115
		@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...
116
		if ($handle) {
117
			fwrite($handle, $entry."\n");
118
			fclose($handle);
119
		} else {
120
			// Fall back to error_log
121
			error_log($entry);
122
		}
123
		if (php_sapi_name() === 'cli-server') {
124
			error_log($message, 4);
125
		}
126
	}
127
128
	/**
129
	 * @return string
130
	 */
131
	public static function getLogFilePath() {
132
		return self::$logFile;
133
	}
134
}
135