Completed
Push — master ( d92879...e9431c )
by Julius
02:33 queued 11s
created

Application   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 138
Duplicated Lines 5.07 %

Coupling/Cohesion

Components 1
Dependencies 10

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 21
lcom 1
cbo 10
dl 7
loc 138
ccs 0
cts 84
cp 0
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A domainOnly() 7 7 4
A __construct() 0 11 1
A registerProvider() 0 37 1
B updateCSP() 0 38 9
B checkAndEnableCODEServer() 0 30 6

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
class Application extends App {
46
47
	const APPNAME = 'richdocuments';
48
49
	/**
50
	 * Strips the path and query parameters from the URL.
51
	 *
52
	 * @param string $url
53
	 * @return string
54
	 */
55 View Code Duplication
	private function domainOnly($url) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
56
		$parsed_url = parse_url($url);
57
		$scheme = isset($parsed_url['scheme']) ? $parsed_url['scheme'] . '://' : '';
58
		$host	= isset($parsed_url['host']) ? $parsed_url['host'] : '';
59
		$port	= isset($parsed_url['port']) ? ':' . $parsed_url['port'] : '';
60
		return "$scheme$host$port";
61
	}
62
63
	public function __construct(array $urlParams = array()) {
64
		parent::__construct(self::APPNAME, $urlParams);
65
66
		/** @var IEventDispatcher $eventDispatcher */
67
		$eventDispatcher = $this->getContainer()->getServer()->query(IEventDispatcher::class);
68
		$eventDispatcher->addListener(LoadViewer::class, function () {
69
			\OCP\Util::addScript('richdocuments', 'viewer');
70
		});
71
72
		$this->getContainer()->registerCapability(Capabilities::class);
73
	}
74
75
	public function registerProvider() {
76
		$container = $this->getContainer();
77
78
		// Register mimetypes
79
		/** @var Detection $detector */
80
		$detector = $container->query(\OCP\Files\IMimeTypeDetector::class);
81
		$detector->getAllMappings();
82
		$detector->registerType('ott','application/vnd.oasis.opendocument.text-template');
83
		$detector->registerType('ots', 'application/vnd.oasis.opendocument.spreadsheet-template');
84
		$detector->registerType('otp', 'application/vnd.oasis.opendocument.presentation-template');
85
86
		/** @var IPreview $previewManager */
87
		$previewManager = $container->query(IPreview::class);
88
89
		$previewManager->registerProvider('/application\/vnd.ms-excel/', function() use ($container) {
90
			return $container->query(MSExcel::class);
91
		});
92
93
		$previewManager->registerProvider('/application\/msword/', function() use ($container) {
94
			return $container->query(MSWord::class);
95
		});
96
97
		$previewManager->registerProvider('/application\/vnd.openxmlformats-officedocument.*/', function() use ($container) {
98
			return $container->query(OOXML::class);
99
		});
100
101
		// \OC::$server->getLogger()->debug('==== Richdocuments Application registerProvider: calling manager registerProvider:');
102
		$previewManager->registerProvider('/application\/vnd.oasis.opendocument.*/', function() use ($container) {
103
			// \OC::$server->getLogger()->debug('==== Richdocuments Application registerProvider lambda. OpenDocument::class=' . OpenDocument::class);
104
			return $container->query(OpenDocument::class);
105
		});
106
107
		$previewManager->registerProvider('/application\/pdf/', function() use ($container) {
108
			return $container->query(Pdf::class);
109
		});
110
111
	}
112
113
	public function updateCSP() {
114
		$container = $this->getContainer();
115
116
		$publicWopiUrl = $container->getServer()->getConfig()->getAppValue('richdocuments', 'public_wopi_url', '');
117
		$publicWopiUrl = $publicWopiUrl === '' ? \OC::$server->getConfig()->getAppValue('richdocuments', 'wopi_url') : $publicWopiUrl;
118
		$cspManager = $container->getServer()->getContentSecurityPolicyManager();
119
		$policy = new ContentSecurityPolicy();
120
		if ($publicWopiUrl !== '') {
121
			$policy->addAllowedFrameDomain('\'self\'');
122
			$policy->addAllowedFrameDomain($this->domainOnly($publicWopiUrl));
123
			if (method_exists($policy, 'addAllowedFormActionDomain')) {
124
				$policy->addAllowedFormActionDomain($this->domainOnly($publicWopiUrl));
125
			}
126
		}
127
128
		/**
129
		 * Dynamically add CSP for federated editing
130
		 */
131
		$path = '';
132
		try {
133
			$path = $container->getServer()->getRequest()->getPathInfo();
134
		} catch (\Exception $e) {}
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
135
		if (strpos($path, '/apps/files') === 0 && $container->getServer()->getAppManager()->isEnabledForUser('federation')) {
136
			/** @var TrustedServers $trustedServers */
137
			$trustedServers = $container->query(TrustedServers::class);
138
			/** @var FederationService $federationService */
139
			$federationService = $container->query(FederationService::class);
140
			$remoteAccess = $container->getServer()->getRequest()->getParam('richdocuments_remote_access');
141
142
			if ($remoteAccess && $trustedServers->isTrustedServer($remoteAccess)) {
143
				$remoteCollabora = $federationService->getRemoteCollaboraURL($remoteAccess);
144
				$policy->addAllowedFrameDomain($remoteAccess);
145
				$policy->addAllowedFrameDomain($remoteCollabora);
146
			}
147
		}
148
149
		$cspManager->addDefaultPolicy($policy);
150
	}
151
152
	public function checkAndEnableCODEServer() {
153
		// Supported only on Linux OS, and x86_64 platform
154
		if (PHP_OS_FAMILY !== 'Linux' || php_uname('m') !== 'x86_64')
155
			return;
156
157
		if ($this->getContainer()->getServer()->getAppManager()->isEnabledForUser('richdocumentscode')) {
158
			$appConfig = $this->getContainer()->query(AppConfig::class);
159
			$wopi_url = $appConfig->getAppValue('wopi_url');
160
161
			// Check if we have the wopi_url set currently
162
			if ($wopi_url !== null && $wopi_url !== '') {
163
				return;
164
			}
165
166
			$urlGenerator = \OC::$server->getURLGenerator();
167
			$relativeUrl = $urlGenerator->linkTo('richdocumentscode', '') . 'proxy.php';
168
			$absoluteUrl = $urlGenerator->getAbsoluteURL($relativeUrl);
169
			$wopi_url = $absoluteUrl . '?req=';
170
171
			$appConfig->setAppValue('wopi_url', $wopi_url);
172
			$appConfig->setAppValue('disable_certificate_verification', 'yes');
173
174
			$discoveryManager = $this->getContainer()->query(DiscoveryManager::class);
175
			$capabilitiesService = $this->getContainer()->query(CapabilitiesService::class);
176
177
			$discoveryManager->refretch();
178
			$capabilitiesService->clear();
179
			$capabilitiesService->refretch();
180
		}
181
	}
182
}
183