Completed
Push — master ( 48c627...03822e )
by Julius
14s queued 10s
created

SettingsController::updateWatermarkSettings()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 42

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 42
ccs 0
cts 39
cp 0
rs 8.6257
c 0
b 0
f 0
cc 6
nc 10
nop 1
crap 42
1
<?php
2
/**
3
 * ownCloud - Richdocuments App
4
 *
5
 * @author Victor Dubiniuk
6
 * @copyright 2014 Victor Dubiniuk [email protected]
7
 *
8
 * This file is licensed under the Affero General Public License version 3 or
9
 * later.
10
 */
11
12
namespace OCA\Richdocuments\Controller;
13
14
use OCA\Richdocuments\Service\CapabilitiesService;
15
use OCA\Richdocuments\WOPI\DiscoveryManager;
16
use OCA\Richdocuments\WOPI\Parser;
17
use \OCP\AppFramework\Controller;
18
use OCP\AppFramework\Http;
19
use OCP\AppFramework\Http\DataResponse;
20
use OCP\AppFramework\Http\JSONResponse;
21
use \OCP\IRequest;
22
use \OCP\IL10N;
23
use OCA\Richdocuments\AppConfig;
24
use OCP\IConfig;
25
use OCP\PreConditionNotMetException;
26
27
class SettingsController extends Controller{
28
	/** @var IL10N */
29
	private $l10n;
30
	/** @var AppConfig */
31
	private $appConfig;
32
	/** @var IConfig */
33
	private $config;
34
	/** @var DiscoveryManager  */
35
	private $discoveryManager;
36
	/** @var Parser */
37
	private $wopiParser;
38
	/** @var string */
39
	private $userId;
40
	/** @var CapabilitiesService */
41
	private $capabilitiesService;
42
43
	/**
44
	 * @param string $appName
45
	 * @param IRequest $request
46
	 * @param IL10N $l10n
47
	 * @param AppConfig $appConfig
48
	 * @param IConfig $config
49
	 * @param DiscoveryManager $discoveryManager
50
	 * @param Parser $wopiParser
51
	 * @param string $userId
52
	 * @param CapabilitiesService $capabilitiesService
53
	 */
54 View Code Duplication
	public function __construct($appName,
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...
55
								IRequest $request,
56
								IL10N $l10n,
57
								AppConfig $appConfig,
58
								IConfig $config,
59
								DiscoveryManager $discoveryManager,
60
								Parser $wopiParser,
61
								$userId,
62
								CapabilitiesService $capabilitiesService) {
63
		parent::__construct($appName, $request);
64
		$this->l10n = $l10n;
65
		$this->appConfig = $appConfig;
66
		$this->config = $config;
67
		$this->discoveryManager = $discoveryManager;
68
		$this->wopiParser = $wopiParser;
69
		$this->userId = $userId;
70
		$this->capabilitiesService = $capabilitiesService;
71
	}
72
73
	/**
74
	 * @PublicPage
75
	 * @NoCSRFRequired
76
	 * @throws \Exception
77
	 */
78
	public function checkSettings() {
79
		try {
80
			$response = $this->discoveryManager->fetchFromRemote();
0 ignored issues
show
Unused Code introduced by
$response is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
81
		} catch (\Exception $e) {
82
			return new DataResponse([
83
				'status' => $e->getCode(),
84
				'message' => $e->getMessage()
85
			], Http::STATUS_INTERNAL_SERVER_ERROR);
86
		}
87
88
		return new DataResponse();
89
	}
90
91
	/**
92
	 * @NoAdminRequired
93
	 *
94
	 * @return JSONResponse
95
	 */
96
	public function getSettings() {
97
		return new JSONResponse([
98
			'wopi_url' => $this->appConfig->getAppValue('wopi_url'),
99
			'public_wopi_url' => $this->appConfig->getAppValue('public_wopi_url'),
100
			'disable_certificate_verification' => $this->appConfig->getAppValue('disable_certificate_verification'),
101
			'edit_groups' => $this->appConfig->getAppValue('edit_groups'),
102
			'use_groups' => $this->appConfig->getAppValue('use_groups'),
103
			'doc_format' => $this->appConfig->getAppValue('doc_format'),
104
		]);
105
	}
106
107
	/**
108
	 * @param string $wopi_url
109
	 * @param string $disable_certificate_verification
110
	 * @param string $edit_groups
111
	 * @param string $use_groups
112
	 * @param string $doc_format
113
	 * @param string $external_apps
114
	 * @param string $canonical_webroot
115
	 * @return JSONResponse
116
	 */
117
	public function setSettings($wopi_url,
118
	                            $disable_certificate_verification,
119
	                            $edit_groups,
120
	                            $use_groups,
121
	                            $doc_format,
122
	                            $external_apps,
123
	                            $canonical_webroot) {
124
		$message = $this->l10n->t('Saved');
125
126
		if ($wopi_url !== null){
127
			$this->appConfig->setAppValue('wopi_url', $wopi_url);
128
		}
129
130
		if ($disable_certificate_verification !== null) {
131
			$this->appConfig->setAppValue(
132
				'disable_certificate_verification',
133
				$disable_certificate_verification === 'true' ? 'yes' : ''
134
			);
135
		}
136
137
		if ($edit_groups !== null){
138
			$this->appConfig->setAppValue('edit_groups', $edit_groups);
139
		}
140
141
		if ($use_groups !== null){
142
			$this->appConfig->setAppValue('use_groups', $use_groups);
143
		}
144
145
		if ($doc_format !== null) {
146
			$this->appConfig->setAppValue('doc_format', $doc_format);
147
		}
148
149
		if ($external_apps !== null) {
150
			$this->appConfig->setAppValue('external_apps', $external_apps);
151
		}
152
153
		if ($canonical_webroot !== null) {
154
			$this->appConfig->setAppValue('canonical_webroot', $canonical_webroot);
155
		}
156
157
		$this->discoveryManager->refretch();
158
		try {
159
			$capaUrlSrc = $this->wopiParser->getUrlSrc('Capabilities');
160
			if (is_array($capaUrlSrc) && $capaUrlSrc['action'] === 'getinfo') {
161
				$public_wopi_url = str_replace('/hosting/capabilities', '', $capaUrlSrc['urlsrc']);
162
				if ($public_wopi_url !== null) {
163
					$this->appConfig->setAppValue('public_wopi_url', $public_wopi_url);
164
					$colon = strpos($public_wopi_url, ':', 0);
165
					if ($this->request->getServerProtocol() !== substr($public_wopi_url, 0, $colon)){
166
						$message = $this->l10n->t('Saved with error: Collabora Online should use the same protocol as the server installation.');
167
					}
168
				}
169
			}
170
		} catch (\Exception $e){
171
			// Ignore
172
		}
173
174
		$this->capabilitiesService->clear();
175
		$this->capabilitiesService->refretch();
176
177
		$response = [
178
			'status' => 'success',
179
			'data' => ['message' => $message]
180
		];
181
182
		return new JSONResponse($response);
183
	}
184
185
	public function updateWatermarkSettings($settings = []) {
186
		$supportedOptions = [
187
			'watermark_text',
188
			'watermark_enabled',
189
			'watermark_shareAll',
190
			'watermark_shareRead',
191
			'watermark_linkSecure',
192
			'watermark_linkRead',
193
			'watermark_linkAll',
194
			'watermark_linkTags',
195
			'watermark_linkTagsList',
196
			'watermark_allGroups',
197
			'watermark_allGroupsList',
198
			'watermark_allTags',
199
			'watermark_allTagsList',
200
		];
201
		$message = $this->l10n->t('Saved');
202
203
		$watermarkSettings = $settings['watermark'];
204
		foreach ($watermarkSettings as $key => $value) {
205
			$fullKey = 'watermark_' . $key;
206
			if (in_array($fullKey, $supportedOptions) !== true) {
207
				return new JSONResponse([
208
					'status' => 'error',
209
					'data' => ['message' => $this->l10n->t('Invalid config key') . ' ' . $fullKey]
210
				], Http::STATUS_BAD_REQUEST);
211
			}
212
			$value = $value === true ? 'yes' : $value;
213
			$value = $value === false ? 'no' : $value;
214
			if (AppConfig::APP_SETTING_TYPES[$fullKey] === 'array') {
215
				$value = implode(',', $value);
216
			}
217
			$this->appConfig->setAppValue($fullKey, $value);
218
		}
219
220
		$response = [
221
			'status' => 'success',
222
			'data' => ['message' => $message]
223
		];
224
225
		return new JSONResponse($response);
226
	}
227
228
	/**
229
	 * @NoAdminRequired
230
	 *
231
	 * @param $key
232
	 * @param $value
233
	 * @return JSONResponse
234
	 */
235
	public function setPersonalSettings($templateFolder) {
236
		$message = $this->l10n->t('Saved');
237
		$status = 'success';
238
239
		if ($templateFolder !== null){
240
			try {
241
				$this->config->setUserValue($this->userId, 'richdocuments', 'templateFolder', $templateFolder);
242
			} catch (PreConditionNotMetException $e) {
243
				$message = $this->l10n->t('Error when saving');
244
				$status = 'error';
245
			}
246
		}
247
248
		$response = [
249
			'status' => $status,
250
			'data' => ['message' => $message]
251
		];
252
253
		return new JSONResponse($response);
254
255
	}
256
}
257