1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @author Christoph Wurst <[email protected]> |
5
|
|
|
* |
6
|
|
|
* @license GNU AGPL version 3 or any later version |
7
|
|
|
* |
8
|
|
|
* This program is free software: you can redistribute it and/or modify |
9
|
|
|
* it under the terms of the GNU Affero General Public License as |
10
|
|
|
* published by the Free Software Foundation, either version 3 of the |
11
|
|
|
* License, or (at your option) any later version. |
12
|
|
|
* |
13
|
|
|
* This program is distributed in the hope that it will be useful, |
14
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
15
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16
|
|
|
* GNU Affero General Public License for more details. |
17
|
|
|
* |
18
|
|
|
* You should have received a copy of the GNU Affero General Public License |
19
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
20
|
|
|
* |
21
|
|
|
*/ |
22
|
|
|
|
23
|
|
|
namespace OCA\Sentry\AppInfo; |
24
|
|
|
|
25
|
|
|
use OC; |
26
|
|
|
use OCA\Sentry\Reporter\SentryReporterAdapter; |
27
|
|
|
use OCP\AppFramework\App; |
28
|
|
|
use OCP\AppFramework\Http\ContentSecurityPolicy; |
29
|
|
|
use OCP\IConfig; |
30
|
|
|
use OCP\Security\IContentSecurityPolicyManager; |
31
|
|
|
use OCP\Support\CrashReport\IRegistry; |
32
|
|
|
use Raven_Client; |
33
|
|
|
use Raven_ErrorHandler; |
34
|
|
|
|
35
|
|
|
class Application extends App { |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param array $urlParams |
39
|
|
|
*/ |
40
|
|
|
public function __construct($urlParams = []) { |
41
|
|
|
parent::__construct('sentry', $urlParams); |
42
|
|
|
|
43
|
|
|
$container = $this->getContainer(); |
44
|
|
|
|
45
|
|
|
/* @var $config IConfig */ |
46
|
|
|
$config = $container->query(IConfig::class); |
47
|
|
|
$dsn = $config->getSystemValue('sentry.dsn', null); |
48
|
|
|
if (!is_null($dsn)) { |
49
|
|
|
$this->registerClient($dsn); |
50
|
|
|
} |
51
|
|
|
$publicDsn = $config->getSystemValue('sentry.public-dsn', null); |
52
|
|
|
if (!is_null($publicDsn)) { |
53
|
|
|
$this->addCsp($publicDsn); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param string $dsn |
59
|
|
|
*/ |
60
|
|
|
private function registerClient($dsn) { |
61
|
|
|
$container = $this->getContainer(); |
62
|
|
|
/* @var $config IConfig */ |
63
|
|
|
$config = $container->query(IConfig::class); |
64
|
|
|
|
65
|
|
|
$client = new Raven_Client($dsn); |
66
|
|
|
$client->setRelease($config->getSystemValue('version', '0.0.0')); |
67
|
|
|
$container->registerService(Raven_Client::class, function() use ($client) { |
68
|
|
|
return $client; |
69
|
|
|
}); |
70
|
|
|
|
71
|
|
|
/* @var $registry IRegistry */ |
72
|
|
|
$registry = $container->query(IRegistry::class); |
73
|
|
|
$reporter = $container->query(SentryReporterAdapter::class); |
74
|
|
|
$registry->register($reporter); |
75
|
|
|
|
76
|
|
|
$this->registerErrorHandlers($client); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
private function registerErrorHandlers(Raven_Client $client) { |
80
|
|
|
$errorHandler = new Raven_ErrorHandler($client); |
81
|
|
|
$errorHandler->registerExceptionHandler(); |
82
|
|
|
$errorHandler->registerErrorHandler(); |
83
|
|
|
$errorHandler->registerShutdownFunction(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function addCsp($publicDsn) { |
87
|
|
|
$parsedUrl = parse_url($publicDsn); |
88
|
|
|
if (!isset($parsedUrl['scheme']) || !isset($parsedUrl['host'])) { |
|
|
|
|
89
|
|
|
// Misconfigured setup -> ignore |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$domain = $parsedUrl['scheme'] . '://' . $parsedUrl['host']; |
93
|
|
|
$csp = new ContentSecurityPolicy(); |
94
|
|
|
$csp->addAllowedConnectDomain($domain); |
95
|
|
|
$cspManager = OC::$server->query(IContentSecurityPolicyManager::class); |
96
|
|
|
$cspManager->addDefaultPolicy($csp); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
} |
100
|
|
|
|
This check looks for the bodies of
if
statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.These
if
bodies can be removed. If you have an empty if but statements in theelse
branch, consider inverting the condition.could be turned into
This is much more concise to read.