Completed
Push — stable12 ( 080ed7...2e954f )
by Morris
86:37 queued 52:25
created
lib/private/Log.php 1 patch
Indentation   +324 added lines, -324 removed lines patch added patch discarded remove patch
@@ -48,328 +48,328 @@
 block discarded – undo
48 48
 
49 49
 class Log implements ILogger {
50 50
 
51
-	/** @var string */
52
-	private $logger;
53
-
54
-	/** @var SystemConfig */
55
-	private $config;
56
-
57
-	/** @var boolean|null cache the result of the log condition check for the request */
58
-	private $logConditionSatisfied = null;
59
-
60
-	/** @var Normalizer */
61
-	private $normalizer;
62
-
63
-	protected $methodsWithSensitiveParameters = [
64
-		// Session/User
65
-		'completeLogin',
66
-		'login',
67
-		'checkPassword',
68
-		'checkPasswordNoLogging',
69
-		'loginWithPassword',
70
-		'updatePrivateKeyPassword',
71
-		'validateUserPass',
72
-		'loginWithToken',
73
-		'\{closure\}',
74
-		'createSessionToken',
75
-
76
-		// TokenProvider
77
-		'getToken',
78
-		'isTokenPassword',
79
-		'getPassword',
80
-		'decryptPassword',
81
-		'logClientIn',
82
-		'generateToken',
83
-		'validateToken',
84
-
85
-		// TwoFactorAuth
86
-		'solveChallenge',
87
-		'verifyChallenge',
88
-
89
-		// ICrypto
90
-		'calculateHMAC',
91
-		'encrypt',
92
-		'decrypt',
93
-
94
-		// LoginController
95
-		'tryLogin',
96
-		'confirmPassword',
97
-
98
-		// LDAP
99
-		'bind',
100
-		'areCredentialsValid',
101
-		'invokeLDAPMethod',
102
-
103
-		// Encryption
104
-		'storeKeyPair',
105
-		'setupUser',
106
-	];
107
-
108
-	/**
109
-	 * @param string $logger The logger that should be used
110
-	 * @param SystemConfig $config the system config object
111
-	 * @param null $normalizer
112
-	 */
113
-	public function __construct($logger = null, SystemConfig $config = null, $normalizer = null) {
114
-		// FIXME: Add this for backwards compatibility, should be fixed at some point probably
115
-		if($config === null) {
116
-			$config = \OC::$server->getSystemConfig();
117
-		}
118
-
119
-		$this->config = $config;
120
-
121
-		// FIXME: Add this for backwards compatibility, should be fixed at some point probably
122
-		if($logger === null) {
123
-			$logType = $this->config->getValue('log_type', 'file');
124
-			$this->logger = static::getLogClass($logType);
125
-			call_user_func(array($this->logger, 'init'));
126
-		} else {
127
-			$this->logger = $logger;
128
-		}
129
-		if ($normalizer === null) {
130
-			$this->normalizer = new Normalizer();
131
-		} else {
132
-			$this->normalizer = $normalizer;
133
-		}
134
-
135
-	}
136
-
137
-	/**
138
-	 * System is unusable.
139
-	 *
140
-	 * @param string $message
141
-	 * @param array $context
142
-	 * @return void
143
-	 */
144
-	public function emergency($message, array $context = array()) {
145
-		$this->log(Util::FATAL, $message, $context);
146
-	}
147
-
148
-	/**
149
-	 * Action must be taken immediately.
150
-	 *
151
-	 * Example: Entire website down, database unavailable, etc. This should
152
-	 * trigger the SMS alerts and wake you up.
153
-	 *
154
-	 * @param string $message
155
-	 * @param array $context
156
-	 * @return void
157
-	 */
158
-	public function alert($message, array $context = array()) {
159
-		$this->log(Util::ERROR, $message, $context);
160
-	}
161
-
162
-	/**
163
-	 * Critical conditions.
164
-	 *
165
-	 * Example: Application component unavailable, unexpected exception.
166
-	 *
167
-	 * @param string $message
168
-	 * @param array $context
169
-	 * @return void
170
-	 */
171
-	public function critical($message, array $context = array()) {
172
-		$this->log(Util::ERROR, $message, $context);
173
-	}
174
-
175
-	/**
176
-	 * Runtime errors that do not require immediate action but should typically
177
-	 * be logged and monitored.
178
-	 *
179
-	 * @param string $message
180
-	 * @param array $context
181
-	 * @return void
182
-	 */
183
-	public function error($message, array $context = array()) {
184
-		$this->log(Util::ERROR, $message, $context);
185
-	}
186
-
187
-	/**
188
-	 * Exceptional occurrences that are not errors.
189
-	 *
190
-	 * Example: Use of deprecated APIs, poor use of an API, undesirable things
191
-	 * that are not necessarily wrong.
192
-	 *
193
-	 * @param string $message
194
-	 * @param array $context
195
-	 * @return void
196
-	 */
197
-	public function warning($message, array $context = array()) {
198
-		$this->log(Util::WARN, $message, $context);
199
-	}
200
-
201
-	/**
202
-	 * Normal but significant events.
203
-	 *
204
-	 * @param string $message
205
-	 * @param array $context
206
-	 * @return void
207
-	 */
208
-	public function notice($message, array $context = array()) {
209
-		$this->log(Util::INFO, $message, $context);
210
-	}
211
-
212
-	/**
213
-	 * Interesting events.
214
-	 *
215
-	 * Example: User logs in, SQL logs.
216
-	 *
217
-	 * @param string $message
218
-	 * @param array $context
219
-	 * @return void
220
-	 */
221
-	public function info($message, array $context = array()) {
222
-		$this->log(Util::INFO, $message, $context);
223
-	}
224
-
225
-	/**
226
-	 * Detailed debug information.
227
-	 *
228
-	 * @param string $message
229
-	 * @param array $context
230
-	 * @return void
231
-	 */
232
-	public function debug($message, array $context = array()) {
233
-		$this->log(Util::DEBUG, $message, $context);
234
-	}
235
-
236
-
237
-	/**
238
-	 * Logs with an arbitrary level.
239
-	 *
240
-	 * @param mixed $level
241
-	 * @param string $message
242
-	 * @param array $context
243
-	 * @return void
244
-	 */
245
-	public function log($level, $message, array $context = array()) {
246
-		$minLevel = min($this->config->getValue('loglevel', Util::WARN), Util::FATAL);
247
-		$logCondition = $this->config->getValue('log.condition', []);
248
-
249
-		array_walk($context, [$this->normalizer, 'format']);
250
-
251
-		if (isset($context['app'])) {
252
-			$app = $context['app'];
253
-
254
-			/**
255
-			 * check log condition based on the context of each log message
256
-			 * once this is met -> change the required log level to debug
257
-			 */
258
-			if(!empty($logCondition)
259
-				&& isset($logCondition['apps'])
260
-				&& in_array($app, $logCondition['apps'], true)) {
261
-				$minLevel = Util::DEBUG;
262
-			}
263
-
264
-		} else {
265
-			$app = 'no app in context';
266
-		}
267
-		// interpolate $message as defined in PSR-3
268
-		$replace = array();
269
-		foreach ($context as $key => $val) {
270
-			$replace['{' . $key . '}'] = $val;
271
-		}
272
-
273
-		// interpolate replacement values into the message and return
274
-		$message = strtr($message, $replace);
275
-
276
-		/**
277
-		 * check for a special log condition - this enables an increased log on
278
-		 * a per request/user base
279
-		 */
280
-		if($this->logConditionSatisfied === null) {
281
-			// default to false to just process this once per request
282
-			$this->logConditionSatisfied = false;
283
-			if(!empty($logCondition)) {
284
-
285
-				// check for secret token in the request
286
-				if(isset($logCondition['shared_secret'])) {
287
-					$request = \OC::$server->getRequest();
288
-
289
-					if ($request->getMethod() === 'PUT' &&
290
-						strpos($request->getHeader('Content-Type'), 'application/x-www-form-urlencoded') === false &&
291
-						strpos($request->getHeader('Content-Type'), 'application/json') === false) {
292
-						$logSecretRequest = '';
293
-					} else {
294
-						$logSecretRequest = $request->getParam('log_secret', '');
295
-					}
296
-
297
-					// if token is found in the request change set the log condition to satisfied
298
-					if ($request && hash_equals($logCondition['shared_secret'], $logSecretRequest)) {
299
-						$this->logConditionSatisfied = true;
300
-					}
301
-				}
302
-
303
-				// check for user
304
-				if(isset($logCondition['users'])) {
305
-					$user = \OC::$server->getUserSession()->getUser();
306
-
307
-					// if the user matches set the log condition to satisfied
308
-					if($user !== null && in_array($user->getUID(), $logCondition['users'], true)) {
309
-						$this->logConditionSatisfied = true;
310
-					}
311
-				}
312
-			}
313
-		}
314
-
315
-		// if log condition is satisfied change the required log level to DEBUG
316
-		if($this->logConditionSatisfied) {
317
-			$minLevel = Util::DEBUG;
318
-		}
319
-
320
-		if ($level >= $minLevel) {
321
-			$logger = $this->logger;
322
-			call_user_func(array($logger, 'write'), $app, $message, $level);
323
-		}
324
-	}
325
-
326
-	/**
327
-	 * Logs an exception very detailed
328
-	 *
329
-	 * @param \Exception|\Throwable $exception
330
-	 * @param array $context
331
-	 * @return void
332
-	 * @since 8.2.0
333
-	 */
334
-	public function logException($exception, array $context = array()) {
335
-		$level = Util::ERROR;
336
-		if (isset($context['level'])) {
337
-			$level = $context['level'];
338
-			unset($context['level']);
339
-		}
340
-		$data = array(
341
-			'Exception' => get_class($exception),
342
-			'Message' => $exception->getMessage(),
343
-			'Code' => $exception->getCode(),
344
-			'Trace' => $exception->getTraceAsString(),
345
-			'File' => $exception->getFile(),
346
-			'Line' => $exception->getLine(),
347
-		);
348
-		$data['Trace'] = preg_replace('!(' . implode('|', $this->methodsWithSensitiveParameters) . ')\(.*\)!', '$1(*** sensitive parameters replaced ***)', $data['Trace']);
349
-		$msg = isset($context['message']) ? $context['message'] : 'Exception';
350
-		$msg .= ': ' . json_encode($data);
351
-		$this->log($level, $msg, $context);
352
-	}
353
-
354
-	/**
355
-	 * @param string $logType
356
-	 * @return string
357
-	 * @internal
358
-	 */
359
-	public static function getLogClass($logType) {
360
-		switch (strtolower($logType)) {
361
-			case 'errorlog':
362
-				return \OC\Log\Errorlog::class;
363
-			case 'syslog':
364
-				return \OC\Log\Syslog::class;
365
-			case 'file':
366
-				return \OC\Log\File::class;
367
-
368
-			// Backwards compatibility for old and fallback for unknown log types
369
-			case 'owncloud':
370
-			case 'nextcloud':
371
-			default:
372
-				return \OC\Log\File::class;
373
-		}
374
-	}
51
+    /** @var string */
52
+    private $logger;
53
+
54
+    /** @var SystemConfig */
55
+    private $config;
56
+
57
+    /** @var boolean|null cache the result of the log condition check for the request */
58
+    private $logConditionSatisfied = null;
59
+
60
+    /** @var Normalizer */
61
+    private $normalizer;
62
+
63
+    protected $methodsWithSensitiveParameters = [
64
+        // Session/User
65
+        'completeLogin',
66
+        'login',
67
+        'checkPassword',
68
+        'checkPasswordNoLogging',
69
+        'loginWithPassword',
70
+        'updatePrivateKeyPassword',
71
+        'validateUserPass',
72
+        'loginWithToken',
73
+        '\{closure\}',
74
+        'createSessionToken',
75
+
76
+        // TokenProvider
77
+        'getToken',
78
+        'isTokenPassword',
79
+        'getPassword',
80
+        'decryptPassword',
81
+        'logClientIn',
82
+        'generateToken',
83
+        'validateToken',
84
+
85
+        // TwoFactorAuth
86
+        'solveChallenge',
87
+        'verifyChallenge',
88
+
89
+        // ICrypto
90
+        'calculateHMAC',
91
+        'encrypt',
92
+        'decrypt',
93
+
94
+        // LoginController
95
+        'tryLogin',
96
+        'confirmPassword',
97
+
98
+        // LDAP
99
+        'bind',
100
+        'areCredentialsValid',
101
+        'invokeLDAPMethod',
102
+
103
+        // Encryption
104
+        'storeKeyPair',
105
+        'setupUser',
106
+    ];
107
+
108
+    /**
109
+     * @param string $logger The logger that should be used
110
+     * @param SystemConfig $config the system config object
111
+     * @param null $normalizer
112
+     */
113
+    public function __construct($logger = null, SystemConfig $config = null, $normalizer = null) {
114
+        // FIXME: Add this for backwards compatibility, should be fixed at some point probably
115
+        if($config === null) {
116
+            $config = \OC::$server->getSystemConfig();
117
+        }
118
+
119
+        $this->config = $config;
120
+
121
+        // FIXME: Add this for backwards compatibility, should be fixed at some point probably
122
+        if($logger === null) {
123
+            $logType = $this->config->getValue('log_type', 'file');
124
+            $this->logger = static::getLogClass($logType);
125
+            call_user_func(array($this->logger, 'init'));
126
+        } else {
127
+            $this->logger = $logger;
128
+        }
129
+        if ($normalizer === null) {
130
+            $this->normalizer = new Normalizer();
131
+        } else {
132
+            $this->normalizer = $normalizer;
133
+        }
134
+
135
+    }
136
+
137
+    /**
138
+     * System is unusable.
139
+     *
140
+     * @param string $message
141
+     * @param array $context
142
+     * @return void
143
+     */
144
+    public function emergency($message, array $context = array()) {
145
+        $this->log(Util::FATAL, $message, $context);
146
+    }
147
+
148
+    /**
149
+     * Action must be taken immediately.
150
+     *
151
+     * Example: Entire website down, database unavailable, etc. This should
152
+     * trigger the SMS alerts and wake you up.
153
+     *
154
+     * @param string $message
155
+     * @param array $context
156
+     * @return void
157
+     */
158
+    public function alert($message, array $context = array()) {
159
+        $this->log(Util::ERROR, $message, $context);
160
+    }
161
+
162
+    /**
163
+     * Critical conditions.
164
+     *
165
+     * Example: Application component unavailable, unexpected exception.
166
+     *
167
+     * @param string $message
168
+     * @param array $context
169
+     * @return void
170
+     */
171
+    public function critical($message, array $context = array()) {
172
+        $this->log(Util::ERROR, $message, $context);
173
+    }
174
+
175
+    /**
176
+     * Runtime errors that do not require immediate action but should typically
177
+     * be logged and monitored.
178
+     *
179
+     * @param string $message
180
+     * @param array $context
181
+     * @return void
182
+     */
183
+    public function error($message, array $context = array()) {
184
+        $this->log(Util::ERROR, $message, $context);
185
+    }
186
+
187
+    /**
188
+     * Exceptional occurrences that are not errors.
189
+     *
190
+     * Example: Use of deprecated APIs, poor use of an API, undesirable things
191
+     * that are not necessarily wrong.
192
+     *
193
+     * @param string $message
194
+     * @param array $context
195
+     * @return void
196
+     */
197
+    public function warning($message, array $context = array()) {
198
+        $this->log(Util::WARN, $message, $context);
199
+    }
200
+
201
+    /**
202
+     * Normal but significant events.
203
+     *
204
+     * @param string $message
205
+     * @param array $context
206
+     * @return void
207
+     */
208
+    public function notice($message, array $context = array()) {
209
+        $this->log(Util::INFO, $message, $context);
210
+    }
211
+
212
+    /**
213
+     * Interesting events.
214
+     *
215
+     * Example: User logs in, SQL logs.
216
+     *
217
+     * @param string $message
218
+     * @param array $context
219
+     * @return void
220
+     */
221
+    public function info($message, array $context = array()) {
222
+        $this->log(Util::INFO, $message, $context);
223
+    }
224
+
225
+    /**
226
+     * Detailed debug information.
227
+     *
228
+     * @param string $message
229
+     * @param array $context
230
+     * @return void
231
+     */
232
+    public function debug($message, array $context = array()) {
233
+        $this->log(Util::DEBUG, $message, $context);
234
+    }
235
+
236
+
237
+    /**
238
+     * Logs with an arbitrary level.
239
+     *
240
+     * @param mixed $level
241
+     * @param string $message
242
+     * @param array $context
243
+     * @return void
244
+     */
245
+    public function log($level, $message, array $context = array()) {
246
+        $minLevel = min($this->config->getValue('loglevel', Util::WARN), Util::FATAL);
247
+        $logCondition = $this->config->getValue('log.condition', []);
248
+
249
+        array_walk($context, [$this->normalizer, 'format']);
250
+
251
+        if (isset($context['app'])) {
252
+            $app = $context['app'];
253
+
254
+            /**
255
+             * check log condition based on the context of each log message
256
+             * once this is met -> change the required log level to debug
257
+             */
258
+            if(!empty($logCondition)
259
+                && isset($logCondition['apps'])
260
+                && in_array($app, $logCondition['apps'], true)) {
261
+                $minLevel = Util::DEBUG;
262
+            }
263
+
264
+        } else {
265
+            $app = 'no app in context';
266
+        }
267
+        // interpolate $message as defined in PSR-3
268
+        $replace = array();
269
+        foreach ($context as $key => $val) {
270
+            $replace['{' . $key . '}'] = $val;
271
+        }
272
+
273
+        // interpolate replacement values into the message and return
274
+        $message = strtr($message, $replace);
275
+
276
+        /**
277
+         * check for a special log condition - this enables an increased log on
278
+         * a per request/user base
279
+         */
280
+        if($this->logConditionSatisfied === null) {
281
+            // default to false to just process this once per request
282
+            $this->logConditionSatisfied = false;
283
+            if(!empty($logCondition)) {
284
+
285
+                // check for secret token in the request
286
+                if(isset($logCondition['shared_secret'])) {
287
+                    $request = \OC::$server->getRequest();
288
+
289
+                    if ($request->getMethod() === 'PUT' &&
290
+                        strpos($request->getHeader('Content-Type'), 'application/x-www-form-urlencoded') === false &&
291
+                        strpos($request->getHeader('Content-Type'), 'application/json') === false) {
292
+                        $logSecretRequest = '';
293
+                    } else {
294
+                        $logSecretRequest = $request->getParam('log_secret', '');
295
+                    }
296
+
297
+                    // if token is found in the request change set the log condition to satisfied
298
+                    if ($request && hash_equals($logCondition['shared_secret'], $logSecretRequest)) {
299
+                        $this->logConditionSatisfied = true;
300
+                    }
301
+                }
302
+
303
+                // check for user
304
+                if(isset($logCondition['users'])) {
305
+                    $user = \OC::$server->getUserSession()->getUser();
306
+
307
+                    // if the user matches set the log condition to satisfied
308
+                    if($user !== null && in_array($user->getUID(), $logCondition['users'], true)) {
309
+                        $this->logConditionSatisfied = true;
310
+                    }
311
+                }
312
+            }
313
+        }
314
+
315
+        // if log condition is satisfied change the required log level to DEBUG
316
+        if($this->logConditionSatisfied) {
317
+            $minLevel = Util::DEBUG;
318
+        }
319
+
320
+        if ($level >= $minLevel) {
321
+            $logger = $this->logger;
322
+            call_user_func(array($logger, 'write'), $app, $message, $level);
323
+        }
324
+    }
325
+
326
+    /**
327
+     * Logs an exception very detailed
328
+     *
329
+     * @param \Exception|\Throwable $exception
330
+     * @param array $context
331
+     * @return void
332
+     * @since 8.2.0
333
+     */
334
+    public function logException($exception, array $context = array()) {
335
+        $level = Util::ERROR;
336
+        if (isset($context['level'])) {
337
+            $level = $context['level'];
338
+            unset($context['level']);
339
+        }
340
+        $data = array(
341
+            'Exception' => get_class($exception),
342
+            'Message' => $exception->getMessage(),
343
+            'Code' => $exception->getCode(),
344
+            'Trace' => $exception->getTraceAsString(),
345
+            'File' => $exception->getFile(),
346
+            'Line' => $exception->getLine(),
347
+        );
348
+        $data['Trace'] = preg_replace('!(' . implode('|', $this->methodsWithSensitiveParameters) . ')\(.*\)!', '$1(*** sensitive parameters replaced ***)', $data['Trace']);
349
+        $msg = isset($context['message']) ? $context['message'] : 'Exception';
350
+        $msg .= ': ' . json_encode($data);
351
+        $this->log($level, $msg, $context);
352
+    }
353
+
354
+    /**
355
+     * @param string $logType
356
+     * @return string
357
+     * @internal
358
+     */
359
+    public static function getLogClass($logType) {
360
+        switch (strtolower($logType)) {
361
+            case 'errorlog':
362
+                return \OC\Log\Errorlog::class;
363
+            case 'syslog':
364
+                return \OC\Log\Syslog::class;
365
+            case 'file':
366
+                return \OC\Log\File::class;
367
+
368
+            // Backwards compatibility for old and fallback for unknown log types
369
+            case 'owncloud':
370
+            case 'nextcloud':
371
+            default:
372
+                return \OC\Log\File::class;
373
+        }
374
+    }
375 375
 }
Please login to merge, or discard this patch.