Completed
Push — master ( 95b48a...17e695 )
by Georg
11s
created

ViewController   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 205
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 95.35%

Importance

Changes 0
Metric Value
wmc 13
c 0
b 0
f 0
lcom 1
cbo 0
dl 0
loc 205
ccs 82
cts 86
cp 0.9535
rs 10

8 Methods

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