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\DataDisplayResponse; |
29
|
|
|
use OCP\AppFramework\Http\NotFoundResponse; |
30
|
|
|
use OCP\AppFramework\Http\TemplateResponse; |
31
|
|
|
use OCP\IConfig; |
32
|
|
|
use OCP\IRequest; |
33
|
|
|
use OCP\IUserSession; |
34
|
|
|
|
35
|
|
|
class ViewController extends Controller { |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var IConfig |
39
|
|
|
*/ |
40
|
|
|
private $config; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var IUserSession |
44
|
|
|
*/ |
45
|
|
|
private $userSession; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param string $appName |
49
|
|
|
* @param IRequest $request an instance of the request |
50
|
|
|
* @param IConfig $config |
51
|
|
|
* @param IUserSession $userSession |
52
|
|
|
*/ |
53
|
|
|
public function __construct($appName, IRequest $request, |
54
|
|
|
IUserSession $userSession, IConfig $config) { |
55
|
|
|
parent::__construct($appName, $request); |
56
|
|
|
$this->config = $config; |
57
|
|
|
$this->userSession = $userSession; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @NoAdminRequired |
62
|
|
|
* @NoCSRFRequired |
63
|
|
|
* |
64
|
|
|
* @return TemplateResponse |
65
|
|
|
*/ |
66
|
|
|
public function index() { |
67
|
|
|
$runningOn = $this->config->getSystemValue('version'); |
68
|
|
|
$runningOnServer91OrLater = version_compare($runningOn, '9.1', '>='); |
69
|
|
|
|
70
|
|
|
$supportsClass = $runningOnServer91OrLater; |
71
|
|
|
$assetPipelineBroken = !$runningOnServer91OrLater; |
72
|
|
|
|
73
|
|
|
$isAssetPipelineEnabled = $this->config->getSystemValue('asset-pipeline.enabled', false); |
74
|
|
|
if ($isAssetPipelineEnabled && $assetPipelineBroken) { |
75
|
|
|
return new TemplateResponse('calendar', 'main-asset-pipeline-unsupported'); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$user = $this->userSession->getUser(); |
79
|
|
|
$userId = $user->getUID(); |
80
|
|
|
$emailAddress = $user->getEMailAddress(); |
81
|
|
|
|
82
|
|
|
$appVersion = $this->config->getAppValue($this->appName, 'installed_version'); |
83
|
|
|
$defaultView = $this->config->getUserValue($userId, $this->appName, 'currentView', 'month'); |
84
|
|
|
|
85
|
|
|
return new TemplateResponse('calendar', 'main', [ |
86
|
|
|
'appVersion' => $appVersion, |
87
|
|
|
'defaultView' => $defaultView, |
88
|
|
|
'emailAddress' => $emailAddress, |
89
|
|
|
'supportsClass' => $supportsClass, |
90
|
|
|
'isPublic' => false, |
91
|
|
|
]); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @PublicPage |
96
|
|
|
* @NoCSRFRequired |
97
|
|
|
* |
98
|
|
|
* @return TemplateResponse |
99
|
|
|
*/ |
100
|
|
|
public function publicIndex() { |
101
|
|
|
$runningOn = $this->config->getSystemValue('version'); |
102
|
|
|
$runningOnServer91OrLater = version_compare($runningOn, '9.1', '>='); |
103
|
|
|
|
104
|
|
|
$supportsClass = $runningOnServer91OrLater; |
105
|
|
|
$assetPipelineBroken = !$runningOnServer91OrLater; |
106
|
|
|
|
107
|
|
|
$isAssetPipelineEnabled = $this->config->getSystemValue('asset-pipeline.enabled', false); |
108
|
|
|
if ($isAssetPipelineEnabled && $assetPipelineBroken) { |
109
|
|
|
return new TemplateResponse('calendar', 'main-asset-pipeline-unsupported'); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
$appVersion = $this->config->getAppValue($this->appName, 'installed_version'); |
113
|
|
|
|
114
|
|
|
$response = new TemplateResponse('calendar', 'main', [ |
115
|
|
|
'appVersion' => $appVersion, |
116
|
|
|
'defaultView' => 'month', |
117
|
|
|
'emailAddress' => '', |
118
|
|
|
'supportsClass' => $supportsClass, |
119
|
|
|
'isPublic' => true, |
120
|
|
|
], 'public'); |
121
|
|
|
$response->addHeader('X-Frame-Options', 'ALLOW'); |
122
|
|
|
$csp = new ContentSecurityPolicy(); |
123
|
|
|
$csp->addAllowedScriptDomain('*'); |
124
|
|
|
$response->setContentSecurityPolicy($csp); |
125
|
|
|
|
126
|
|
|
return $response; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @NoAdminRequired |
131
|
|
|
* |
132
|
|
|
* @param string $id |
133
|
|
|
* @return DataDisplayResponse |
134
|
|
|
*/ |
135
|
|
|
public function getTimezone($id) { |
136
|
|
|
if (!in_array($id, $this->getTimezoneList())) { |
137
|
|
|
return new NotFoundResponse(); |
|
|
|
|
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
$tzData = file_get_contents(__DIR__ . '/../timezones/' . $id); |
141
|
|
|
|
142
|
|
|
return new DataDisplayResponse($tzData, HTTP::STATUS_OK, [ |
143
|
|
|
'content-type' => 'text/calendar', |
144
|
|
|
]); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @NoAdminRequired |
150
|
|
|
* @NoCSRFRequired |
151
|
|
|
* @PublicPage |
152
|
|
|
* |
153
|
|
|
* @param $region |
154
|
|
|
* @param $city |
155
|
|
|
* @return DataDisplayResponse |
156
|
|
|
*/ |
157
|
|
|
public function getTimezoneWithRegion($region, $city) { |
158
|
|
|
return $this->getTimezone($region . '-' . $city); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @NoAdminRequired |
164
|
|
|
* @PublicPage |
165
|
|
|
* @NoCSRFRequired |
166
|
|
|
* |
167
|
|
|
* @param $region |
168
|
|
|
* @param $subregion |
169
|
|
|
* @param $city |
170
|
|
|
* @return DataDisplayResponse |
171
|
|
|
*/ |
172
|
|
|
public function getTimezoneWithSubRegion($region, $subregion, $city) { |
173
|
|
|
return $this->getTimezone($region . '-' . $subregion . '-' . $city); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* get a list of default timezones |
179
|
|
|
* |
180
|
|
|
* @return array |
181
|
|
|
*/ |
182
|
|
|
private function getTimezoneList() { |
183
|
|
|
$allFiles = scandir(__DIR__ . '/../timezones/'); |
184
|
|
|
|
185
|
|
|
return array_values(array_filter($allFiles, function($file) { |
186
|
|
|
return (substr($file, -4) === '.ics'); |
187
|
|
|
})); |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.