ViewController   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

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