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