Completed
Pull Request — master (#605)
by Thomas
05:11
created

ViewController::sendEmailPublicLink()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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