Completed
Pull Request — master (#605)
by Thomas
10:38
created

ViewController::sendEmail()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 22
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

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