ViewController   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 235
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 96.04%

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 0
dl 0
loc 235
ccs 97
cts 101
cp 0.9604
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
B index() 0 43 5
A publicIndexWithBranding() 0 12 2
A publicIndexWithBrandingAndFancyName() 0 3 1
A publicIndexForEmbedding() 0 19 2
A publicIndexForEmbeddingLegacy() 0 3 1
A getTemplateParams() 0 26 4
A getPublicTemplateParameters() 0 31 1
A needsAssetPipelineWarning() 0 7 2
1
<?php
2
/**
3
 * Calendar App
4
 *
5
 * @author Georg Ehrke
6
 * @copyright 2016 Georg Ehrke <[email protected]>
7
 * @author Raghu Nayyar
8
 * @copyright 2016 Raghu Nayyar <[email protected]>
9
 *
10
 * This library is free software; you can redistribute it and/or
11
 * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
12
 * License as published by the Free Software Foundation; either
13
 * version 3 of the License, or any later version.
14
 *
15
 * This library 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
21
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
22
 *
23
 */
24
namespace OCA\Calendar\Controller;
25
26
use OC\AppFramework\Http\Request;
27
use OCP\AppFramework\Controller;
28
use OCP\AppFramework\Http\ContentSecurityPolicy;
29
use OCP\AppFramework\Http\TemplateResponse;
30
use OCP\IConfig;
31
use OCP\IRequest;
32
use OCP\IUserSession;
33
use OCP\IURLGenerator;
34
35
class ViewController extends Controller {
36
37
	/**
38
	 * @var IConfig
39
	 */
40
	private $config;
41
42
	/**
43
	 * @var IURLGenerator
44
	 */
45
	private $urlGenerator;
46
47
	/**
48
	 * @var IUserSession
49
	 */
50
	private $userSession;
51
52
	/**
53
	 * @param string $appName
54
	 * @param IRequest $request an instance of the request
55
	 * @param IUserSession $userSession
56
	 * @param IConfig $config
57
	 * @param IURLGenerator $urlGenerator
58
	 */
59 24
	public function __construct($appName, IRequest $request, IUserSession $userSession,
60
								IConfig $config, IURLGenerator $urlGenerator) {
61 24
		parent::__construct($appName, $request);
62 24
		$this->config = $config;
63 24
		$this->userSession = $userSession;
64 24
		$this->urlGenerator = $urlGenerator;
65 24
	}
66
67
	/**
68
	 * @NoAdminRequired
69
	 * @NoCSRFRequired
70
	 *
71
	 * @return TemplateResponse
72
	 */
73 8
	public function index() {
74 8
		if ($this->needsAssetPipelineWarning()) {
75 1
			return new TemplateResponse('calendar', 'main-asset-pipeline-unsupported');
76
		}
77
78 7
		$templateParameters = $this->getTemplateParams();
79
80 7
		$user = $this->userSession->getUser();
81 7
		$userId = $user->getUID();
82 7
		$emailAddress = $user->getEMailAddress();
83
84 7
		$initialView = $this->config->getUserValue($userId, $this->appName, 'currentView', null);
85 7
		$skipPopover = $this->config->getUserValue($userId, $this->appName, 'skipPopover', 'no');
86 7
		$weekNumbers = $this->config->getUserValue($userId, $this->appName, 'showWeekNr', 'no');
87 7
		$firstRun = $this->config->getUserValue($userId, $this->appName, 'firstRun', null);
88
89
		// the default view will be saved as soon as a user
90
		// opens the calendar app, therefore this is a good
91
		// indication if the calendar was used before
92 7
		if ($firstRun === null) {
93 2
			if ($initialView === null) {
94 1
				$firstRun = 'yes';
95
			} else {
96 1
				$this->config->setUserValue($userId, $this->appName, 'firstRun', 'no');
97 1
				$firstRun = 'no';
98
			}
99
		}
100
101 7
		if ($initialView === null) {
102 1
			$initialView = 'month';
103
		}
104
105 7
		return new TemplateResponse('calendar', 'main', array_merge($templateParameters, [
106 7
			'initialView' => $initialView,
107 7
			'emailAddress' => $emailAddress,
108 7
			'skipPopover' => $skipPopover,
109 7
			'weekNumbers' => $weekNumbers,
110 7
			'firstRun' => $firstRun,
111
			'isPublic' => false,
112
			'isEmbedded' => false,
113 7
			'token' => '',
114
		]));
115
	}
116
117
	/**
118
	 * @PublicPage
119
	 * @NoCSRFRequired
120
	 *
121
	 * @param string $token
122
	 *
123
	 * @return TemplateResponse
124
	 */
125 8
	public function publicIndexWithBranding($token) {
126 8
		if ($this->needsAssetPipelineWarning()) {
127 1
			return new TemplateResponse('calendar', 'main-asset-pipeline-unsupported');
128
		}
129
130 7
		$templateParameters = $this->getTemplateParams();
131 7
		$publicTemplateParameters = $this->getPublicTemplateParameters($token);
132 7
		$params = array_merge($templateParameters, $publicTemplateParameters);
133 7
		$params['isEmbedded'] = false;
134
135 7
		return new TemplateResponse('calendar', 'public', $params, 'base');
136
	}
137
138
	/**
139
	 * @PublicPage
140
	 * @NoCSRFRequired
141
	 *
142
	 * @param string $token
143
	 *
144
	 * @return TemplateResponse
145
	 */
146
	public function publicIndexWithBrandingAndFancyName($token) {
147
		return $this->publicIndexWithBranding($token);
148
	}
149
150
	/**
151
	 * @PublicPage
152
	 * @NoCSRFRequired
153
	 *
154
	 * @param string $token
155
	 *
156
	 * @return TemplateResponse
157
	 */
158 8
	public function publicIndexForEmbedding($token) {
159 8
		if ($this->needsAssetPipelineWarning()) {
160 1
			return new TemplateResponse('calendar', 'main-asset-pipeline-unsupported');
161
		}
162
163 7
		$templateParameters = $this->getTemplateParams();
164 7
		$publicTemplateParameters = $this->getPublicTemplateParameters($token);
165 7
		$params = array_merge($templateParameters, $publicTemplateParameters);
166 7
		$params['isEmbedded'] = true;
167
168 7
		$response = new TemplateResponse('calendar', 'main', $params, 'public');
169
170 7
		$response->addHeader('X-Frame-Options', 'ALLOW');
171 7
		$csp = new ContentSecurityPolicy();
172 7
		$csp->addAllowedScriptDomain('*');
173 7
		$response->setContentSecurityPolicy($csp);
174
175 7
		return $response;
176
	}
177
178
	/**
179
	 * @PublicPage
180
	 * @NoCSRFRequired
181
	 *
182
	 * @param string $token
183
	 *
184
	 * @return TemplateResponse
185
	 */
186
	public function publicIndexForEmbeddingLegacy($token) {
187
		return $this->publicIndexForEmbedding($token);
188
	}
189
190
	/**
191
	 * get common parameters used for all three routes
192
	 * @return array
193
	 */
194 21
	private function getTemplateParams() {
195 21
		$runningOn = $this->config->getSystemValue('version');
196 21
		$runningOnServer91OrLater = version_compare($runningOn, '9.1', '>=');
197
198 21
		$supportsClass = $runningOnServer91OrLater;
199
200 21
		$shareeCanEditShares = false;
201 21
		$shareeCanEditCalendarProperties = false;
202
203 21
		$appVersion = $this->config->getAppValue($this->appName, 'installed_version');
204 21
		$webCalWorkaround = $runningOnServer91OrLater ? 'no' : 'yes';
205 21
		$isIE = $this->request->isUserAgent([Request::USER_AGENT_IE]);
206
//		$defaultColor = $this->config->getAppValue('theming', 'color', '#1d2d44');
207 21
		$canSharePublicLink = $this->config->getAppValue('core', 'shareapi_allow_links', 'yes');
208
209
		return [
210 21
			'appVersion' => $appVersion,
211 21
			'supportsClass' => $supportsClass,
212 21
			'isIE' => $isIE,
213 21
			'webCalWorkaround' => $webCalWorkaround,
214 21
			'defaultColor' => '#1d2d44',
215 21
			'shareeCanEditShares' => $shareeCanEditShares ? 'yes' : 'no',
216 21
			'shareeCanEditCalendarProperties' => $shareeCanEditCalendarProperties ? 'yes' : 'no',
217 21
			'canSharePublicLink' => $canSharePublicLink,
218
		];
219
	}
220
221
	/**
222
	 * get common parameters for public sites
223
	 * @param string $token
224
	 * @return array
225
	 */
226 14
	private function getPublicTemplateParameters($token) {
227 14
		$shareURL = $this->request->getServerProtocol() . '://';
228 14
		$shareURL .= $this->request->getServerHost();
229 14
		$shareURL .= $this->request->getRequestUri();
230
231 14
		$relativeImagePath = $this->urlGenerator->imagePath('core', 'favicon-touch.png');
232 14
		$previewImage = $this->urlGenerator->getAbsoluteURL($relativeImagePath);
233
234 14
		$remoteBase = $this->urlGenerator->linkTo('', 'remote.php');
235 14
		$remoteBase .= '/dav/public-calendars/' . $token . '?export';
236
237 14
		$downloadUrl = $this->urlGenerator->getAbsoluteURL($remoteBase);
238
239 14
		$protocolLength = strlen($this->request->getServerProtocol()) + 3;
240 14
		$webcalUrl = 'webcal://' . substr($downloadUrl, $protocolLength);
241
242
		return [
243 14
			'initialView' => 'month',
244 14
			'emailAddress' => '',
245 14
			'skipPopover' => 'no',
246 14
			'weekNumbers' => 'no',
247 14
			'firstRun' => 'no',
248 14
			'webCalWorkaround' => 'no',
249
			'isPublic' => true,
250 14
			'shareURL' => $shareURL,
251 14
			'previewImage' => $previewImage,
252 14
			'webcalURL' => $webcalUrl,
253 14
			'downloadURL' => $downloadUrl,
254 14
			'token' => $token,
255
		];
256
	}
257
258
	/**
259
	 * check if we need to show the asset pipeline warning
260
	 * @return bool
261
	 */
262 24
	private function needsAssetPipelineWarning() {
263 24
		$runningOn = $this->config->getSystemValue('version');
264 24
		$assetPipelineBroken = version_compare($runningOn, '9.1', '<');
265 24
		$isAssetPipelineEnabled = $this->config->getSystemValue('asset-pipeline.enabled', false);
266
267 24
		return ($isAssetPipelineEnabled && $assetPipelineBroken);
268
	}
269
}
270