Passed
Push — master ( 3ac45b...28dc8b )
by Georg
08:50 queued 04:16
created

ViewController   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 209
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 95.56%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 0
dl 0
loc 209
ccs 86
cts 90
cp 0.9556
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
B index() 0 41 4
A publicIndexWithBranding() 0 8 1
A publicIndexWithBrandingAndFancyName() 0 3 1
A publicIndexForEmbedding() 0 15 1
A publicIndexForEmbeddingLegacy() 0 3 1
A getTemplateParams() 0 22 3
B getPublicTemplateParameters() 0 31 1
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 15
	public function __construct($appName, IRequest $request, IUserSession $userSession,
60
								IConfig $config, IURLGenerator $urlGenerator) {
61 15
		parent::__construct($appName, $request);
62 15
		$this->config = $config;
63 15
		$this->userSession = $userSession;
64 15
		$this->urlGenerator = $urlGenerator;
65 15
	}
66
67
	/**
68
	 * @NoAdminRequired
69
	 * @NoCSRFRequired
70
	 *
71
	 * @return TemplateResponse
72
	 */
73 7
	public function index() {
74 7
		$templateParameters = $this->getTemplateParams();
75
76 7
		$user = $this->userSession->getUser();
77 7
		$userId = $user->getUID();
78 7
		$emailAddress = $user->getEMailAddress();
79
80 7
		$initialView = $this->config->getUserValue($userId, $this->appName, 'currentView', null);
81 7
		$skipPopover = $this->config->getUserValue($userId, $this->appName, 'skipPopover', 'no');
82 7
		$weekNumbers = $this->config->getUserValue($userId, $this->appName, 'showWeekNr', 'no');
83 7
		$firstRun = $this->config->getUserValue($userId, $this->appName, 'firstRun', null);
84 7
		$timezone = $this->config->getUserValue($userId, $this->appName, 'timezone', 'automatic');
85
86
		// the default view will be saved as soon as a user
87
		// opens the calendar app, therefore this is a good
88
		// indication if the calendar was used before
89 7
		if ($firstRun === null) {
90 2
			if ($initialView === null) {
91 1
				$firstRun = 'yes';
92
			} else {
93 1
				$this->config->setUserValue($userId, $this->appName, 'firstRun', 'no');
94 1
				$firstRun = 'no';
95
			}
96
		}
97
98 7
		if ($initialView === null) {
99 2
			$initialView = 'month';
100
		}
101
102 7
		return new TemplateResponse('calendar', 'main', array_merge($templateParameters, [
103 7
			'initialView' => $initialView,
104 7
			'emailAddress' => $emailAddress,
105 7
			'skipPopover' => $skipPopover,
106 7
			'weekNumbers' => $weekNumbers,
107 7
			'firstRun' => $firstRun,
108
			'isPublic' => false,
109
			'isEmbedded' => false,
110 7
			'token' => '',
111 7
			'timezone' => $timezone,
112
		]));
113
	}
114
115
	/**
116
	 * @PublicPage
117
	 * @NoCSRFRequired
118
	 *
119
	 * @param string $token
120
	 *
121
	 * @return TemplateResponse
122
	 */
123 4
	public function publicIndexWithBranding($token) {
124 4
		$templateParameters = $this->getTemplateParams();
125 4
		$publicTemplateParameters = $this->getPublicTemplateParameters($token);
126 4
		$params = array_merge($templateParameters, $publicTemplateParameters);
127 4
		$params['isEmbedded'] = false;
128
129 4
		return new TemplateResponse('calendar', 'public', $params, 'base');
130
	}
131
132
	/**
133
	 * @PublicPage
134
	 * @NoCSRFRequired
135
	 *
136
	 * @param string $token
137
	 *
138
	 * @return TemplateResponse
139
	 */
140
	public function publicIndexWithBrandingAndFancyName($token) {
141
		return $this->publicIndexWithBranding($token);
142
	}
143
144
	/**
145
	 * @PublicPage
146
	 * @NoCSRFRequired
147
	 *
148
	 * @param string $token
149
	 *
150
	 * @return TemplateResponse
151
	 */
152 4
	public function publicIndexForEmbedding($token) {
153 4
		$templateParameters = $this->getTemplateParams();
154 4
		$publicTemplateParameters = $this->getPublicTemplateParameters($token);
155 4
		$params = array_merge($templateParameters, $publicTemplateParameters);
156 4
		$params['isEmbedded'] = true;
157
158 4
		$response = new TemplateResponse('calendar', 'main', $params, 'public');
159
160 4
		$response->addHeader('X-Frame-Options', 'ALLOW');
161 4
		$csp = new ContentSecurityPolicy();
162 4
		$csp->addAllowedScriptDomain('*');
163 4
		$response->setContentSecurityPolicy($csp);
164
165 4
		return $response;
166
	}
167
168
	/**
169
	 * @PublicPage
170
	 * @NoCSRFRequired
171
	 *
172
	 * @param string $token
173
	 *
174
	 * @return TemplateResponse
175
	 */
176
	public function publicIndexForEmbeddingLegacy($token) {
177
		return $this->publicIndexForEmbedding($token);
178
	}
179
180
	/**
181
	 * get common parameters used for all three routes
182
	 * @return array
183
	 */
184 15
	private function getTemplateParams() {
185 15
		$runningOn = $this->config->getSystemValue('version');
186 15
		$runningOnNextcloud12OrLater = version_compare($runningOn, '12', '>=');
187
188 15
		$shareeCanEditShares = !$runningOnNextcloud12OrLater;
189 15
		$shareeCanEditCalendarProperties = $runningOnNextcloud12OrLater;
190
191 15
		$appVersion = $this->config->getAppValue($this->appName, 'installed_version');
192 15
		$isIE = $this->request->isUserAgent([Request::USER_AGENT_IE]);
193 15
		$defaultColor = $this->config->getAppValue('theming', 'color', '#0082C9');
194 15
		$canSharePublicLink = $this->config->getAppValue('core', 'shareapi_allow_links', 'yes');
195
196
		return [
197 15
			'appVersion' => $appVersion,
198 15
			'isIE' => $isIE,
199 15
			'defaultColor' => $defaultColor,
200 15
			'shareeCanEditShares' => $shareeCanEditShares ? 'yes' : 'no',
201 15
			'shareeCanEditCalendarProperties' => $shareeCanEditCalendarProperties ? 'yes' : 'no',
202 15
			'canSharePublicLink' => $canSharePublicLink,
203 15
			'timezone' => 'automatic',
204
		];
205
	}
206
207
	/**
208
	 * get common parameters for public sites
209
	 * @param string $token
210
	 * @return array
211
	 */
212 8
	private function getPublicTemplateParameters($token) {
213 8
		$shareURL = $this->request->getServerProtocol() . '://';
214 8
		$shareURL .= $this->request->getServerHost();
215 8
		$shareURL .= $this->request->getRequestUri();
216
217 8
		$relativeImagePath = $this->urlGenerator->imagePath('core', 'favicon-touch.png');
218 8
		$previewImage = $this->urlGenerator->getAbsoluteURL($relativeImagePath);
219
220 8
		$remoteBase = $this->urlGenerator->linkTo('', 'remote.php');
221 8
		$remoteBase .= '/dav/public-calendars/' . $token . '?export';
222
223 8
		$downloadUrl = $this->urlGenerator->getAbsoluteURL($remoteBase);
224
225 8
		$protocolLength = strlen($this->request->getServerProtocol()) + 3;
226 8
		$webcalUrl = 'webcal://' . substr($downloadUrl, $protocolLength);
227
228
		return [
229 8
			'initialView' => 'month',
230 8
			'emailAddress' => '',
231 8
			'skipPopover' => 'no',
232 8
			'weekNumbers' => 'no',
233 8
			'firstRun' => 'no',
234
			'isPublic' => true,
235 8
			'shareURL' => $shareURL,
236 8
			'previewImage' => $previewImage,
237 8
			'webcalURL' => $webcalUrl,
238 8
			'downloadURL' => $downloadUrl,
239 8
			'token' => $token,
240 8
			'timezone' => 'automatic',
241
		];
242
	}
243
}
244