Completed
Push — master ( 125f9f...2c140b )
by Julius
02:26 queued 10s
created

Application::updateCSP()   B

Complexity

Conditions 8
Paths 36

Size

Total Lines 37

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 0
Metric Value
dl 0
loc 37
ccs 0
cts 28
cp 0
rs 8.0835
c 0
b 0
f 0
cc 8
nc 36
nop 0
crap 72
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\Capabilities;
31
use OCA\Richdocuments\Preview\MSExcel;
32
use OCA\Richdocuments\Preview\MSWord;
33
use OCA\Richdocuments\Preview\OOXML;
34
use OCA\Richdocuments\Preview\OpenDocument;
35
use OCA\Richdocuments\Preview\Pdf;
36
use OCA\Richdocuments\Service\FederationService;
37
use OCP\AppFramework\App;
38
use OCP\IPreview;
39
40
class Application extends App {
41
42
	const APPNAME = 'richdocuments';
43
44
	public function __construct(array $urlParams = array()) {
45
		parent::__construct(self::APPNAME, $urlParams);
46
47
		$this->getContainer()->registerCapability(Capabilities::class);
48
	}
49
50
	public function registerProvider() {
51
		$container = $this->getContainer();
52
53
		// Register mimetypes
54
		/** @var Detection $detector */
55
		$detector = $container->query(\OCP\Files\IMimeTypeDetector::class);
56
		$detector->getAllMappings();
57
		$detector->registerType('ott','application/vnd.oasis.opendocument.text-template');
58
		$detector->registerType('ots', 'application/vnd.oasis.opendocument.spreadsheet-template');
59
		$detector->registerType('otp', 'application/vnd.oasis.opendocument.presentation-template');
60
61
		/** @var IPreview $previewManager */
62
		$previewManager = $container->query(IPreview::class);
63
64
		$previewManager->registerProvider('/application\/vnd.ms-excel/', function() use ($container) {
65
			return $container->query(MSExcel::class);
66
		});
67
68
		$previewManager->registerProvider('/application\/msword/', function() use ($container) {
69
			return $container->query(MSWord::class);
70
		});
71
72
		$previewManager->registerProvider('/application\/vnd.openxmlformats-officedocument.*/', function() use ($container) {
73
			return $container->query(OOXML::class);
74
		});
75
76
		// \OC::$server->getLogger()->debug('==== Richdocuments Application registerProvider: calling manager registerProvider:');
77
		$previewManager->registerProvider('/application\/vnd.oasis.opendocument.*/', function() use ($container) {
78
			// \OC::$server->getLogger()->debug('==== Richdocuments Application registerProvider lambda. OpenDocument::class=' . OpenDocument::class);
79
			return $container->query(OpenDocument::class);
80
		});
81
82
		$previewManager->registerProvider('/application\/pdf/', function() use ($container) {
83
			return $container->query(Pdf::class);
84
		});
85
86
	}
87
88
	public function updateCSP(): void {
89
		$container = $this->getContainer();
90
91
		$publicWopiUrl = $container->getServer()->getConfig()->getAppValue('richdocuments', 'public_wopi_url', '');
92
		$publicWopiUrl = $publicWopiUrl === '' ? \OC::$server->getConfig()->getAppValue('richdocuments', 'wopi_url') : $publicWopiUrl;
93
		$cspManager = $container->getServer()->getContentSecurityPolicyManager();
94
		$policy = new ContentSecurityPolicy();
95
		if ($publicWopiUrl !== '') {
96
			$policy->addAllowedFrameDomain($publicWopiUrl);
97
			if (method_exists($policy, 'addAllowedFormActionDomain')) {
98
				$policy->addAllowedFormActionDomain($publicWopiUrl);
99
			}
100
		}
101
102
		/**
103
		 * Dynamically add CSP for federated editing
104
		 */
105
		$path = '';
106
		try {
107
			$path = $container->getServer()->getRequest()->getPathInfo();
108
		} catch (\Exception $e) {}
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
109
		if (strpos($path, '/apps/files') === 0) {
110
			/** @var TrustedServers $trustedServers */
111
			$trustedServers = $container->query(TrustedServers::class);
112
			/** @var FederationService $federationService */
113
			$federationService = $container->query(FederationService::class);
114
			$remoteAccess = $container->getServer()->getRequest()->getParam('richdocuments_remote_access');
115
116
			if ($remoteAccess && $trustedServers->isTrustedServer($remoteAccess)) {
117
				$remoteCollabora = $federationService->getRemoteCollaboraURL($remoteAccess);
118
				$policy->addAllowedFrameDomain($remoteAccess);
119
				$policy->addAllowedFrameDomain($remoteCollabora);
120
			}
121
		}
122
123
		$cspManager->addDefaultPolicy($policy);
124
	}
125
}
126