Completed
Pull Request — master (#1)
by Thomas
05:10
created

ViewController::index()   B

Complexity

Conditions 3
Paths 2

Size

Total Lines 33
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 25
nc 2
nop 0
dl 0
loc 33
rs 8.8571
c 0
b 0
f 0
ccs 9
cts 9
cp 1
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;
27
use OC\L10N\L10N;
28
use OCP\AppFramework\Controller;
29
use OCP\AppFramework\Http\ContentSecurityPolicy;
30
use OCP\AppFramework\Http\DataDisplayResponse;
31
use OCP\AppFramework\Http\JSONResponse;
32
use OCP\AppFramework\Http\NotFoundResponse;
33
use OCP\AppFramework\Http\TemplateResponse;
34
use OCP\Defaults;
35
use OCP\IConfig;
36
use OCP\IRequest;
37
use OCP\IUserSession;
38
use OCP\Mail\IMailer;
39
use OCP\IURLGenerator;
40
41
class ViewController extends Controller {
42
43
	/**
44
	 * @var IURLGenerator
45
	 */
46
	private $urlGenerator;
47
48
	/**
49
	 * @var IConfig
50
	 */
51
	private $config;
52
53 8
	/**
54
	 * @var IUserSession
55 8
	 */
56 8
	private $userSession;
57 8
58 8
	/**
59
	 * @var IMailer
60
	 */
61
	private $mailer;
62
63
	/**
64
	 * @var L10N
65
	 */
66 4
	private $l10n;
67 4
68 4
	/**
69
	 * @var Defaults
70 4
	 */
71 4
	private $defaults;
72
73 4
	/**
74 4
	 * @param string $appName
75 1
	 * @param IRequest $request an instance of the request
76
	 * @param IUserSession $userSession
77
	 * @param IConfig $config
78 3
	 * @param IMailer $mailer
79 3
	 * @param L10N $l10N
80 3
	 * @param Defaults $defaults
81
	 * @param IURLGenerator $urlGenerator
82 3
	 */
83 3
	public function __construct($appName, IRequest $request,
84 3
								IUserSession $userSession, IConfig $config, IMailer $mailer, L10N $l10N, Defaults $defaults, IURLGenerator $urlGenerator) {
85 3
		parent::__construct($appName, $request);
86 3
		$this->config = $config;
87
		$this->userSession = $userSession;
88 3
		$this->mailer = $mailer;
89 3
		$this->l10n = $l10N;
90 3
		$this->defaults = $defaults;
91 3
		$this->urlGenerator = $urlGenerator;
92 3
	}
93 3
94 3
	/**
95 3
	 * @NoAdminRequired
96 3
	 * @NoCSRFRequired
97
	 *
98
	 * @return TemplateResponse
99
	 */
100
	public function index() {
101
		$runningOn = $this->config->getSystemValue('version');
102
		$runningOnServer91OrLater = version_compare($runningOn, '9.1', '>=');
103
104
		$supportsClass = $runningOnServer91OrLater;
105 4
		$assetPipelineBroken = !$runningOnServer91OrLater;
106 4
107 2
		$isAssetPipelineEnabled = $this->config->getSystemValue('asset-pipeline.enabled', false);
108
		if ($isAssetPipelineEnabled && $assetPipelineBroken) {
109
			return new TemplateResponse('calendar', 'main-asset-pipeline-unsupported');
110 2
		}
111
112 2
		$user = $this->userSession->getUser();
113 2
		$userId = $user->getUID();
114 2
		$emailAddress = $user->getEMailAddress();
115
116
		$appVersion = $this->config->getAppValue($this->appName, 'installed_version');
117
		$defaultView = $this->config->getUserValue($userId, $this->appName, 'currentView', 'month');
118
		$skipPopover = $this->config->getUserValue($userId, $this->appName, 'skipPopover', 'no');
119
		$weekNumbers = $this->config->getUserValue($userId, $this->appName, 'showWeekNr', 'no');
120
		$defaultColor = $this->config->getAppValue('theming', 'color', '#0082C9');
121
122
		return new TemplateResponse('calendar', 'main', [
123
			'appVersion' => $appVersion,
124
			'defaultView' => $defaultView,
125 2
			'emailAddress' => $emailAddress,
126 2
			'skipPopover' => $skipPopover,
127
			'weekNumbers' => $weekNumbers,
128
			'supportsClass' => $supportsClass,
129
			'defaultColor' => $defaultColor,
130
			'isPublic' => false,
131
		]);
132
	}
133
134
	/**
135
	 * @PublicPage
136
	 * @NoCSRFRequired
137
	 *
138
	 * @return TemplateResponse
139
	 */
140
	public function publicIndex() {
141
		$runningOn = $this->config->getSystemValue('version');
142
		$runningOnServer91OrLater = version_compare($runningOn, '9.1', '>=');
143
144
		$supportsClass = $runningOnServer91OrLater;
145
		$assetPipelineBroken = !$runningOnServer91OrLater;
146
147
		$isAssetPipelineEnabled = $this->config->getSystemValue('asset-pipeline.enabled', false);
148 4
		if ($isAssetPipelineEnabled && $assetPipelineBroken) {
149 4
			return new TemplateResponse('calendar', 'main-asset-pipeline-unsupported');
150
		}
151 4
152 4
		$appVersion = $this->config->getAppValue($this->appName, 'installed_version');
153 4
154
		$response = new TemplateResponse('calendar', 'main', [
155
			'appVersion' => $appVersion,
156
			'defaultView' => 'month',
157
			'emailAddress' => '',
158
			'supportsClass' => $supportsClass,
159
			'isPublic' => true,
160
			'shareURL' => $this->request->getServerProtocol() . '://' . $this->request->getServerHost() . $this->request->getRequestUri(),
161
			'previewImage' => $this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('core', 'favicon-touch.png')),
162
		], 'public');
163
		$response->addHeader('X-Frame-Options', 'ALLOW');
164
		$csp = new ContentSecurityPolicy();
165
		$csp->addAllowedScriptDomain('*');
166
		$response->setContentSecurityPolicy($csp);
167
168
		return $response;
169
	}
170
171
	/**
172
	 * @NoAdminRequired
173
	 *
174
	 * @param string $id
175
	 * @return NotFoundResponse|DataDisplayResponse
176
	 */
177
	public function getTimezone($id) {
178
		if (!in_array($id, $this->getTimezoneList())) {
179
			return new NotFoundResponse();
180
		}
181
182
		$tzData = file_get_contents(__DIR__ . '/../timezones/' . $id);
183
184
		return new DataDisplayResponse($tzData, HTTP::STATUS_OK, [
185
			'content-type' => 'text/calendar',
186
		]);
187
	}
188
189
190
	/**
191
	 * @NoAdminRequired
192
	 * @NoCSRFRequired
193
	 * @PublicPage
194
	 *
195
	 * @param $region
196
	 * @param $city
197
	 * @return DataDisplayResponse
198
	 */
199
	public function getTimezoneWithRegion($region, $city) {
200
		return $this->getTimezone($region . '-' . $city);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->getTimezone($region . '-' . $city); of type OCP\AppFramework\Http\No...ttp\DataDisplayResponse adds the type OCP\AppFramework\Http\NotFoundResponse to the return on line 200 which is incompatible with the return type documented by OCA\Calendar\Controller\...::getTimezoneWithRegion of type OCP\AppFramework\Http\DataDisplayResponse.
Loading history...
201
	}
202
203
204
	/**
205
	 * @NoAdminRequired
206
	 * @PublicPage
207
	 * @NoCSRFRequired
208
	 *
209
	 * @param $region
210
	 * @param $subregion
211
	 * @param $city
212
	 * @return DataDisplayResponse
213
	 */
214
	public function getTimezoneWithSubRegion($region, $subregion, $city) {
215
		return $this->getTimezone($region . '-' . $subregion . '-' . $city);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->getTimezone($regi...bregion . '-' . $city); of type OCP\AppFramework\Http\No...ttp\DataDisplayResponse adds the type OCP\AppFramework\Http\NotFoundResponse to the return on line 215 which is incompatible with the return type documented by OCA\Calendar\Controller\...etTimezoneWithSubRegion of type OCP\AppFramework\Http\DataDisplayResponse.
Loading history...
216
	}
217
218
219
	/**
220
	 * get a list of default timezones
221
	 *
222
	 * @return array
223
	 */
224
	private function getTimezoneList() {
225
		$allFiles = scandir(__DIR__ . '/../timezones/');
226
227
		return array_values(array_filter($allFiles, function($file) {
228
			return (substr($file, -4) === '.ics');
229
		}));
230
	}
231
232
	/**
233
	 * @param string $to
234
	 * @param string $url
235
	 * @param string $name
236
	 * @return JSONResponse
237
	 * @NoAdminRequired
238
	 */
239
	public function sendEmailPublicLink($to, $url, $name) {
240
241
		$user = $this->userSession->getUser();
242
		$username = $user->getDisplayName();
243
244
		$subject = $this->l10n->t('%s has published the calendar "%s"', [$username, $name]);
245
246
		$emailTemplate = new TemplateResponse('calendar', 'mail.publication', ['subject' => $subject, 'username' => $username, 'calendarname' => $name, 'calendarurl' => $url, 'defaults' => $this->defaults], 'public');
247
		$body = $emailTemplate->render();
248
249
		return $this->sendEmail($to, $subject, $body, true);
250
	}
251
252
	/**
253
	 * @param string $target
254
	 * @param string $subject
255
	 * @param string $body
256
	 * @param boolean $useHTML
257
	 * @return JSONResponse
258
	 *
259
	 * TODO : This should be moved to a Tools class
260
	 *
261
	 */
262
	private function sendEmail($target, $subject, $body, $useHTML = false) {
263
		if (!$this->mailer->validateMailAddress($target)) {
264
			return new JSONResponse([], Http::STATUS_BAD_REQUEST);
265
		}
266
267
		$sendFromDomain = $this->config->getSystemValue('mail_domain', 'domain.org');
268
		$sendFromAddress = $this->config->getSystemValue('mail_from_address', 'owncloud');
269
		$sendFrom = $sendFromAddress . '@' . $sendFromDomain;
270
271
		$message = $this->mailer->createMessage();
272
		$message->setSubject($subject);
273
		$message->setFrom([$sendFrom => 'ownCloud Notifier']);
274
		$message->setTo([$target => 'Recipient']);
275
		if ($useHTML) {
276
			$message->setHtmlBody($body);
277
		} else {
278
			$message->setPlainBody($body);
279
		}
280
		$this->mailer->send($message);
281
282
		return new JSONResponse([], Http::STATUS_OK);
283
	}
284
}
285