Completed
Pull Request — master (#869)
by
unknown
18:10
created

ViewController::publicIndexForEmbeddingLegacy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

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