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