Completed
Push — master ( eebb66...95029a )
by Julius
02:24 queued 12s
created

Application::updateCSP()   B

Complexity

Conditions 9
Paths 36

Size

Total Lines 38

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 0
Metric Value
dl 0
loc 38
ccs 0
cts 29
cp 0
rs 7.7564
c 0
b 0
f 0
cc 9
nc 36
nop 0
crap 90
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016 Lukas Reschke <[email protected]>
4
 *
5
 * @author Lukas Reschke <[email protected]>
6
 * @author Roeland Jago Douma <[email protected]>
7
 *
8
 * @license GNU AGPL version 3 or any later version
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License as
12
 * published by the Free Software Foundation, either version 3 of the
13
 * License, or (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU Affero General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public License
21
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22
 *
23
 */
24
25
namespace OCA\Richdocuments\AppInfo;
26
27
use OC\Files\Type\Detection;
28
use OC\Security\CSP\ContentSecurityPolicy;
29
use OCA\Federation\TrustedServers;
30
use OCA\Richdocuments\AppConfig;
31
use OCA\Richdocuments\Capabilities;
32
use OCA\Richdocuments\Preview\MSExcel;
33
use OCA\Richdocuments\Preview\MSWord;
34
use OCA\Richdocuments\Preview\OOXML;
35
use OCA\Richdocuments\Preview\OpenDocument;
36
use OCA\Richdocuments\Preview\Pdf;
37
use OCA\Richdocuments\Service\CapabilitiesService;
38
use OCA\Richdocuments\Service\FederationService;
39
use OCA\Richdocuments\WOPI\DiscoveryManager;
40
use OCA\Viewer\Event\LoadViewer;
41
use OCP\AppFramework\App;
42
use OCP\EventDispatcher\IEventDispatcher;
43
use OCP\IPreview;
44
45
46
47
48
class Application extends App {
49
50
	const APPNAME = 'richdocuments';
51
52
	public function __construct(array $urlParams = array()) {
53
		parent::__construct(self::APPNAME, $urlParams);
54
55
		/** @var IEventDispatcher $eventDispatcher */
56
		$eventDispatcher = $this->getContainer()->getServer()->query(IEventDispatcher::class);
57
		$eventDispatcher->addListener(LoadViewer::class, function () {
58
			\OCP\Util::addScript('richdocuments', 'viewer');
59
		});
60
61
		$this->getContainer()->registerCapability(Capabilities::class);
62
	}
63
64
	public function registerProvider() {
65
		$container = $this->getContainer();
66
67
		// Register mimetypes
68
		/** @var Detection $detector */
69
		$detector = $container->query(\OCP\Files\IMimeTypeDetector::class);
70
		$detector->getAllMappings();
71
		$detector->registerType('ott','application/vnd.oasis.opendocument.text-template');
72
		$detector->registerType('ots', 'application/vnd.oasis.opendocument.spreadsheet-template');
73
		$detector->registerType('otp', 'application/vnd.oasis.opendocument.presentation-template');
74
75
		/** @var IPreview $previewManager */
76
		$previewManager = $container->query(IPreview::class);
77
78
		$previewManager->registerProvider('/application\/vnd.ms-excel/', function() use ($container) {
79
			return $container->query(MSExcel::class);
80
		});
81
82
		$previewManager->registerProvider('/application\/msword/', function() use ($container) {
83
			return $container->query(MSWord::class);
84
		});
85
86
		$previewManager->registerProvider('/application\/vnd.openxmlformats-officedocument.*/', function() use ($container) {
87
			return $container->query(OOXML::class);
88
		});
89
90
		// \OC::$server->getLogger()->debug('==== Richdocuments Application registerProvider: calling manager registerProvider:');
91
		$previewManager->registerProvider('/application\/vnd.oasis.opendocument.*/', function() use ($container) {
92
			// \OC::$server->getLogger()->debug('==== Richdocuments Application registerProvider lambda. OpenDocument::class=' . OpenDocument::class);
93
			return $container->query(OpenDocument::class);
94
		});
95
96
		$previewManager->registerProvider('/application\/pdf/', function() use ($container) {
97
			return $container->query(Pdf::class);
98
		});
99
100
	}
101
102
	public function updateCSP() {
103
		$container = $this->getContainer();
104
105
		$publicWopiUrl = $container->getServer()->getConfig()->getAppValue('richdocuments', 'public_wopi_url', '');
106
		$publicWopiUrl = $publicWopiUrl === '' ? \OC::$server->getConfig()->getAppValue('richdocuments', 'wopi_url') : $publicWopiUrl;
107
		$cspManager = $container->getServer()->getContentSecurityPolicyManager();
108
		$policy = new ContentSecurityPolicy();
109
		if ($publicWopiUrl !== '') {
110
			$policy->addAllowedFrameDomain('\'self\'');
111
			$policy->addAllowedFrameDomain($publicWopiUrl);
112
			if (method_exists($policy, 'addAllowedFormActionDomain')) {
113
				$policy->addAllowedFormActionDomain($publicWopiUrl);
114
			}
115
		}
116
117
		/**
118
		 * Dynamically add CSP for federated editing
119
		 */
120
		$path = '';
121
		try {
122
			$path = $container->getServer()->getRequest()->getPathInfo();
123
		} catch (\Exception $e) {}
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
124
		if (strpos($path, '/apps/files') === 0 && $container->getServer()->getAppManager()->isEnabledForUser('federation')) {
125
			/** @var TrustedServers $trustedServers */
126
			$trustedServers = $container->query(TrustedServers::class);
127
			/** @var FederationService $federationService */
128
			$federationService = $container->query(FederationService::class);
129
			$remoteAccess = $container->getServer()->getRequest()->getParam('richdocuments_remote_access');
130
131
			if ($remoteAccess && $trustedServers->isTrustedServer($remoteAccess)) {
132
				$remoteCollabora = $federationService->getRemoteCollaboraURL($remoteAccess);
133
				$policy->addAllowedFrameDomain($remoteAccess);
134
				$policy->addAllowedFrameDomain($remoteCollabora);
135
			}
136
		}
137
138
		$cspManager->addDefaultPolicy($policy);
139
	}
140
141
    public function checkAndEnableCODEServer() {
142
	    // Supported only on Linux OS, and x86_64 platform
143
        if (PHP_OS_FAMILY !== 'Linux' || php_uname('m') !== 'x86_64')
144
            return;
145
146
        if ($this->getContainer()->getServer()->getAppManager()->isEnabledForUser('richdocumentscode')) {
147
            $appConfig = $this->getContainer()->query(AppConfig::class);
148
            $wopi_url = $appConfig->getAppValue('wopi_url');
149
150
            // Check if we have the wopi_url set currently
151
            if ($wopi_url !== null && $wopi_url !== '') {
152
                return;
153
            }
154
155
            $urlGenerator = \OC::$server->getURLGenerator();
156
            $relativeUrl = $urlGenerator->linkTo('richdocumentscode', '') . 'proxy.php';
157
            $absoluteUrl = $urlGenerator->getAbsoluteURL($relativeUrl);
158
            $wopi_url = $absoluteUrl . '?req=';
159
160
            $appConfig->setAppValue('wopi_url', $wopi_url);
161
            $appConfig->setAppValue('disable_certificate_verification', 'yes');
162
163
            $discoveryManager = $this->getContainer()->query(DiscoveryManager::class);
164
            $capabilitiesService = $this->getContainer()->query(CapabilitiesService::class);
165
166
            $discoveryManager->refretch();
167
            $capabilitiesService->clear();
168
            $capabilitiesService->refretch();
169
        }
170
    }
171
}
172