Completed
Pull Request — master (#565)
by Georg
19:00
created

ViewController::getTemplateParams()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 28
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 28
ccs 17
cts 17
cp 1
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 22
nc 4
nop 0
crap 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 OCP\AppFramework\Controller;
27
use OCP\AppFramework\Http\ContentSecurityPolicy;
28
use OCP\AppFramework\Http\TemplateResponse;
29
use OCP\App\IAppManager;
30
use OCP\IConfig;
31
use OCP\IRequest;
32
use OCP\IURLGenerator;
33
use OCP\IUserSession;
34
use OC\AppFramework\Http\Request;
35
36
class ViewController extends Controller {
37
38
	/**
39
	 * @var IConfig
40
	 */
41
	private $config;
42
43
	/**
44
	 * @var IURLGenerator
45
	 */
46
	private $urlGenerator;
47
48
	/**
49
	 * @var IUserSession
50
	 */
51
	private $userSession;
52
53
	/**
54
	 * @var IAppManager
55
	 */
56
	private $appManager;
57
58
	/**
59 15
	 * @param string $appName
60
	 * @param IRequest $request an instance of the request
61 15
	 * @param IUserSession $userSession
62 15
	 * @param IConfig $config
63 15
	 * @param IURLGenerator $urlGenerator
64 15
	 * @param IAppManager $appManager
65 15
	 */
66
	public function __construct($appName, IRequest $request, IUserSession $userSession,
67
		IConfig $config, IURLGenerator $urlGenerator, IAppManager $appManager) {
68
		parent::__construct($appName, $request);
69
		$this->config = $config;
70
		$this->userSession = $userSession;
71
		$this->urlGenerator = $urlGenerator;
72
		$this->appManager = $appManager;
73 7
	}
74 7
75
	/**
76 7
	 * @NoAdminRequired
77 7
	 * @NoCSRFRequired
78 7
	 *
79
	 * @return TemplateResponse
80 7
	 */
81 7
	public function index() {
82 7
		$templateParameters = $this->getTemplateParams();
83 7
84
		$user = $this->userSession->getUser();
85
		$userId = $user->getUID();
86
		$emailAddress = $user->getEMailAddress();
87
88 7
		$initialView = $this->config->getUserValue($userId, $this->appName, 'currentView', null);
89 2
		$skipPopover = $this->config->getUserValue($userId, $this->appName, 'skipPopover', 'no');
90 1
		$weekNumbers = $this->config->getUserValue($userId, $this->appName, 'showWeekNr', 'no');
91 1
		$firstRun = $this->config->getUserValue($userId, $this->appName, 'firstRun', null);
92 1
93 1
		// the default view will be saved as soon as a user
94
		// opens the calendar app, therefore this is a good
95 2
		// indication if the calendar was used before
96
		if ($firstRun === null) {
97 7
			if ($initialView === null) {
98 2
				$firstRun = 'yes';
99 2
			} else {
100
				$this->config->setUserValue($userId, $this->appName, 'firstRun', 'no');
101 7
				$firstRun = 'no';
102 7
			}
103 7
		}
104 7
105 7
		if ($initialView === null) {
106 7
			$initialView = 'month';
107 7
		}
108 7
109 7
		return new TemplateResponse('calendar', 'main', array_merge($templateParameters, [
110 7
			'initialView' => $initialView,
111
			'emailAddress' => $emailAddress,
112
			'skipPopover' => $skipPopover,
113
			'weekNumbers' => $weekNumbers,
114
			'firstRun' => $firstRun,
115
			'isPublic' => false,
116
			'isEmbedded' => false,
117
			'token' => '',
118
		]));
119
	}
120
121 4
	/**
122 4
	 * @PublicPage
123 4
	 * @NoCSRFRequired
124 4
	 *
125 4
	 * @param string $token
126
	 *
127 4
	 * @return TemplateResponse
128
	 */
129
	public function publicIndexWithBranding($token) {
130
		$templateParameters = $this->getTemplateParams();
131
		$publicTemplateParameters = $this->getPublicTemplateParameters($token);
132
		$params = array_merge($templateParameters, $publicTemplateParameters);
133
		$params['isEmbedded'] = false;
134
135
		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 4
	/**
151 4
	 * @PublicPage
152 4
	 * @NoCSRFRequired
153 4
	 *
154 4
	 * @param string $token
155
	 *
156 4
	 * @return TemplateResponse
157
	 */
158 4
	public function publicIndexForEmbedding($token) {
159 4
		$templateParameters = $this->getTemplateParams();
160 4
		$publicTemplateParameters = $this->getPublicTemplateParameters($token);
161 4
		$params = array_merge($templateParameters, $publicTemplateParameters);
162
		$params['isEmbedded'] = true;
163 4
164
		$response = new TemplateResponse('calendar', 'main', $params, 'public');
165
166
		$response->addHeader('X-Frame-Options', 'ALLOW');
167
		$csp = new ContentSecurityPolicy();
168
		$csp->addAllowedScriptDomain('*');
169
		$response->setContentSecurityPolicy($csp);
170
171
		return $response;
172
	}
173
174
	/**
175
	 * @PublicPage
176
	 * @NoCSRFRequired
177
	 *
178
	 * @param string $token
179
	 *
180
	 * @return TemplateResponse
181
	 */
182 15
	public function publicIndexForEmbeddingLegacy($token) {
183 15
		return $this->publicIndexForEmbedding($token);
184 15
	}
185
186 15
	/**
187 15
	 * get common parameters used for all three routes
188
	 * @return array
189 15
	 */
190 15
	private function getTemplateParams() {
191 15
		$runningOn = $this->config->getSystemValue('version');
192 15
		$runningOnNextcloud12OrLater = version_compare($runningOn, '12', '>=');
193
194
		$shareeCanEditShares = !$runningOnNextcloud12OrLater;
195 15
		$shareeCanEditCalendarProperties = $runningOnNextcloud12OrLater;
196 15
197 15
		$appVersion = $this->config->getAppValue($this->appName, 'installed_version');
198 15
		$isIE = $this->request->isUserAgent([Request::USER_AGENT_IE]);
199 15
		$defaultColor = $this->config->getAppValue('theming', 'color', '#0082C9');
200 15
		$canSharePublicLink = $this->config->getAppValue('core', 'shareapi_allow_links', 'yes');
201 15
202
		$isSpreedAvailable = $this->appManager->isEnabledForUser('spreed', null);
203
		$spreedMeetingUrlTemplate = $this->config->getAppValue($this->appName, 'spreed_meeting_url_template', '');
204
		$createSpreedMeetingByDefault = $this->config->getAppValue($this->appName, 'create_spreed_meeting_by_default', 'no') === 'yes';
205
206
		return [
207
			'appVersion' => $appVersion,
208
			'isIE' => $isIE,
209 8
			'defaultColor' => $defaultColor,
210 8
			'shareeCanEditShares' => $shareeCanEditShares ? 'yes' : 'no',
211 8
			'shareeCanEditCalendarProperties' => $shareeCanEditCalendarProperties ? 'yes' : 'no',
212 8
			'canSharePublicLink' => $canSharePublicLink,
213
			'isSpreedAvailable' => $isSpreedAvailable,
214 8
			'spreedMeetingUrlTemplate' => $spreedMeetingUrlTemplate,
215 8
			'createSpreedMeetingByDefault' => $createSpreedMeetingByDefault,
216
		];
217 8
	}
218 8
219
	/**
220 8
	 * get common parameters for public sites
221
	 * @param string $token
222 8
	 * @return array
223 8
	 */
224
	private function getPublicTemplateParameters($token) {
225
		$shareURL = $this->request->getServerProtocol() . '://';
226 8
		$shareURL .= $this->request->getServerHost();
227 8
		$shareURL .= $this->request->getRequestUri();
228 8
229 8
		$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 8
		$downloadUrl = $this->urlGenerator->getAbsoluteURL($remoteBase);
236 8
237 8
		$protocolLength = strlen($this->request->getServerProtocol()) + 3;
238
		$webcalUrl = 'webcal://' . substr($downloadUrl, $protocolLength);
239
240
		return [
241
			'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
		];
253
	}
254
}
255