Completed
Pull Request — master (#929)
by
unknown
1261:04 queued 1258:27
created

ViewController::getTemplateParams()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 16
cts 16
cp 1
rs 9.568
c 0
b 0
f 0
cc 3
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 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
	 * @NoSameSiteCookieRequired
148
	 *
149
	 * @param string $token
150
	 *
151
	 * @return TemplateResponse
152
	 */
153 4
	public function publicIndexForEmbedding($token) {
154 4
		$templateParameters = $this->getTemplateParams();
155 4
		$publicTemplateParameters = $this->getPublicTemplateParameters($token);
156 4
		$params = array_merge($templateParameters, $publicTemplateParameters);
157 4
		$params['isEmbedded'] = true;
158
159 4
		$response = new TemplateResponse('calendar', 'main', $params, 'base');
160
161 4
		$response->addHeader('X-Frame-Options', 'ALLOW');
162 4
		$csp = new ContentSecurityPolicy();
163 4
		$csp->addAllowedScriptDomain('*');
164 4
		$response->setContentSecurityPolicy($csp);
165
166 4
		return $response;
167
	}
168
169
	/**
170
	 * @PublicPage
171
	 * @NoCSRFRequired
172
	 *
173
	 * @param string $token
174
	 *
175
	 * @return TemplateResponse
176
	 */
177
	public function publicIndexForEmbeddingLegacy($token) {
178
		return $this->publicIndexForEmbedding($token);
179
	}
180
181
	/**
182
	 * get common parameters used for all three routes
183
	 * @return array
184
	 */
185 15
	private function getTemplateParams() {
186 15
		$runningOn = $this->config->getSystemValue('version');
187 15
		$runningOnNextcloud12OrLater = version_compare($runningOn, '12', '>=');
188
189 15
		$shareeCanEditShares = !$runningOnNextcloud12OrLater;
190 15
		$shareeCanEditCalendarProperties = $runningOnNextcloud12OrLater;
191
192 15
		$appVersion = $this->config->getAppValue($this->appName, 'installed_version');
193 15
		$isIE = $this->request->isUserAgent([Request::USER_AGENT_IE]);
194 15
		$defaultColor = $this->config->getAppValue('theming', 'color', '#0082C9');
195 15
		$canSharePublicLink = $this->config->getAppValue('core', 'shareapi_allow_links', 'yes');
196
197
		return [
198 15
			'appVersion' => $appVersion,
199 15
			'isIE' => $isIE,
200 15
			'defaultColor' => $defaultColor,
201 15
			'shareeCanEditShares' => $shareeCanEditShares ? 'yes' : 'no',
202 15
			'shareeCanEditCalendarProperties' => $shareeCanEditCalendarProperties ? 'yes' : 'no',
203 15
			'canSharePublicLink' => $canSharePublicLink,
204 15
			'timezone' => 'automatic',
205
		];
206
	}
207
208
	/**
209
	 * get common parameters for public sites
210
	 * @param string $token
211
	 * @return array
212
	 */
213 8
	private function getPublicTemplateParameters($token) {
214 8
		$shareURL = $this->request->getServerProtocol() . '://';
215 8
		$shareURL .= $this->request->getServerHost();
216 8
		$shareURL .= $this->request->getRequestUri();
217
218 8
		$relativeImagePath = $this->urlGenerator->imagePath('core', 'favicon-touch.png');
219 8
		$previewImage = $this->urlGenerator->getAbsoluteURL($relativeImagePath);
220
221 8
		$remoteBase = $this->urlGenerator->linkTo('', 'remote.php');
222 8
		$remoteBase .= '/dav/public-calendars/' . $token . '?export';
223
224 8
		$downloadUrl = $this->urlGenerator->getAbsoluteURL($remoteBase);
225
226 8
		$protocolLength = strlen($this->request->getServerProtocol()) + 3;
227 8
		$webcalUrl = 'webcal://' . substr($downloadUrl, $protocolLength);
228
229
		return [
230 8
			'initialView' => 'month',
231 8
			'emailAddress' => '',
232 8
			'skipPopover' => 'no',
233 8
			'weekNumbers' => 'no',
234 8
			'firstRun' => 'no',
235
			'isPublic' => true,
236 8
			'shareURL' => $shareURL,
237 8
			'previewImage' => $previewImage,
238 8
			'webcalURL' => $webcalUrl,
239 8
			'downloadURL' => $downloadUrl,
240 8
			'token' => $token,
241 8
			'timezone' => 'automatic',
242
		];
243
	}
244
}
245