Completed
Pull Request — master (#605)
by Thomas
06:32
created

ViewController::getTimezoneList()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 7
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 OCP\AppFramework\Controller;
28
use OCP\AppFramework\Http\ContentSecurityPolicy;
29
use OCP\AppFramework\Http\DataDisplayResponse;
30
use OCP\AppFramework\Http\NotFoundResponse;
31
use OCP\AppFramework\Http\TemplateResponse;
32
use OCP\IConfig;
33
use OCP\IRequest;
34
use OCP\IUserSession;
35
36
class ViewController extends Controller {
37
38
	/**
39
	 * @var IConfig
40
	 */
41
	private $config;
42
43
	/**
44
	 * @var IUserSession
45
	 */
46
	private $userSession;
47
48
	/**
49
	 * @param string $appName
50
	 * @param IRequest $request an instance of the request
51
	 * @param IConfig $config
52
	 * @param IUserSession $userSession
53 8
	 */
54
	public function __construct($appName, IRequest $request,
55 8
								IUserSession $userSession, IConfig $config) {
56 8
		parent::__construct($appName, $request);
57 8
		$this->config = $config;
58 8
		$this->userSession = $userSession;
59
	}
60
61
	/**
62
	 * @NoAdminRequired
63
	 * @NoCSRFRequired
64
	 *
65
	 * @return TemplateResponse
66 4
	 */
67 4
	public function index() {
68
		$runningOn = $this->config->getSystemValue('version');
69 4
		$runningOnServer91OrLater = version_compare($runningOn, '9.1', '>=');
70 4
71 2
		$supportsClass = $runningOnServer91OrLater;
72
		$assetPipelineBroken = !$runningOnServer91OrLater;
73
74 2
		$isAssetPipelineEnabled = $this->config->getSystemValue('asset-pipeline.enabled', false);
75 2
		if ($isAssetPipelineEnabled && $assetPipelineBroken) {
76 2
			return new TemplateResponse('calendar', 'main-asset-pipeline-unsupported');
77
		}
78 2
79 2
		$user = $this->userSession->getUser();
80
		$userId = $user->getUID();
81 2
		$emailAddress = $user->getEMailAddress();
82
83 2
		$appVersion = $this->config->getAppValue($this->appName, 'installed_version');
84 2
		$defaultView = $this->config->getUserValue($userId, $this->appName, 'currentView', 'month');
85 2
		
86 2
		return new TemplateResponse('calendar', 'main', [
87
			'appVersion' => $appVersion,
88 2
			'defaultView' => $defaultView,
89
			'emailAddress' => $emailAddress,
90
			'supportsClass' => $supportsClass,
91
			'isPublic' => false,
92
		]);
93
	}
94
95
	/**
96
	 * @PublicPage
97 4
	 * @NoCSRFRequired
98 4
	 *
99 2
	 * @return TemplateResponse
100
	 */
101
	public function publicIndex() {
102 2
		$runningOn = $this->config->getSystemValue('version');
103
		$runningOnServer91OrLater = version_compare($runningOn, '9.1', '>=');
104 2
105 2
		$supportsClass = $runningOnServer91OrLater;
106 2
		$assetPipelineBroken = !$runningOnServer91OrLater;
107
108
		$isAssetPipelineEnabled = $this->config->getSystemValue('asset-pipeline.enabled', false);
109
		if ($isAssetPipelineEnabled && $assetPipelineBroken) {
110
			return new TemplateResponse('calendar', 'main-asset-pipeline-unsupported');
111
		}
112
113
		$appVersion = $this->config->getAppValue($this->appName, 'installed_version');
114
115
		$response = new TemplateResponse('calendar', 'main', [
116
			'appVersion' => $appVersion,
117 2
			'defaultView' => 'month',
118 2
			'emailAddress' => '',
119
			'supportsClass' => $supportsClass,
120
			'isPublic' => true,
121
		], 'public');
122
		$response->addHeader('X-Frame-Options', 'ALLOW');
123
		$csp = new ContentSecurityPolicy();
124
		$csp->addAllowedScriptDomain('*');
125
		$response->setContentSecurityPolicy($csp);
126
127
		return $response;
128
	}
129
130
	/**
131
	 * @NoAdminRequired
132
	 *
133
	 * @param string $id
134
	 * @return NotFoundResponse|DataDisplayResponse
135
	 */
136
	public function getTimezone($id) {
137
		if (!in_array($id, $this->getTimezoneList())) {
138
			return new NotFoundResponse();
139
		}
140 4
141 4
		$tzData = file_get_contents(__DIR__ . '/../timezones/' . $id);
142
143 4
		return new DataDisplayResponse($tzData, HTTP::STATUS_OK, [
144 4
			'content-type' => 'text/calendar',
145 4
		]);
146
	}
147
148
149
	/**
150
	 * @NoAdminRequired
151
	 * @NoCSRFRequired
152
	 * @PublicPage
153
	 *
154
	 * @param $region
155
	 * @param $city
156
	 * @return DataDisplayResponse
157
	 */
158
	public function getTimezoneWithRegion($region, $city) {
159
		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 159 which is incompatible with the return type documented by OCA\Calendar\Controller\...::getTimezoneWithRegion of type OCP\AppFramework\Http\DataDisplayResponse.
Loading history...
160
	}
161
162
163
	/**
164
	 * @NoAdminRequired
165
	 * @PublicPage
166
	 * @NoCSRFRequired
167
	 *
168
	 * @param $region
169
	 * @param $subregion
170
	 * @param $city
171
	 * @return DataDisplayResponse
172
	 */
173
	public function getTimezoneWithSubRegion($region, $subregion, $city) {
174
		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 174 which is incompatible with the return type documented by OCA\Calendar\Controller\...etTimezoneWithSubRegion of type OCP\AppFramework\Http\DataDisplayResponse.
Loading history...
175
	}
176
177
178
	/**
179
	 * get a list of default timezones
180
	 *
181
	 * @return array
182
	 */
183
	private function getTimezoneList() {
184
		$allFiles = scandir(__DIR__ . '/../timezones/');
185
186
		return array_values(array_filter($allFiles, function($file) {
187
			return (substr($file, -4) === '.ics');
188
		}));
189
	}
190
}
191