-
-
Notifications
You must be signed in to change notification settings - Fork 109
Expand file tree
/
Copy pathPublicUtilsController.php
More file actions
279 lines (255 loc) Β· 6.73 KB
/
PublicUtilsController.php
File metadata and controls
279 lines (255 loc) Β· 6.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
<?php
/**
* Nextcloud - maps
*
* This file is licensed under the Affero General Public License version 3 or
* later. See the COPYING file.
*
* @author Julien Veyssier <eneiluj@posteo.net>
* @copyright Julien Veyssier 2019
* @copyright Benstone Zhang <benstonezhang@gmail.com> 2023
*/
namespace OCA\Maps\Controller;
use OCP\AppFramework\Http\DataResponse;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\GenericFileException;
use OCP\Files\InvalidPathException;
use OCP\Files\IRootFolder;
use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
use OCP\IConfig;
use OCP\IInitialStateService;
use OCP\IRequest;
use OCP\ISession;
use OCP\IURLGenerator;
use OCP\IUserManager;
use OCP\Lock\LockedException;
use OCP\Share\Exceptions\ShareNotFound;
use OCP\Share\IManager as ShareManager;
class PublicUtilsController extends PublicPageController {
protected IRootFolder $root;
public function __construct(
string $appName,
IRequest $request,
ISession $session,
IURLGenerator $urlGenerator,
IConfig $config,
IInitialStateService $initialStateService,
IUserManager $userManager,
ShareManager $shareManager,
IRootFolder $root,
IEventDispatcher $eventDispatcher,
) {
parent::__construct($appName, $request, $session, $urlGenerator, $eventDispatcher, $config, $initialStateService, $shareManager, $userManager);
$this->root = $root;
}
/**
* Validate the permissions of the share
*
* @return bool
*/
private function validateShare(\OCP\Share\IShare $share) {
// If the owner is disabled no access to the link is granted
$owner = $this->userManager->get($share->getShareOwner());
if ($owner === null || !$owner->isEnabled()) {
return false;
}
// If the initiator of the share is disabled no access is granted
$initiator = $this->userManager->get($share->getSharedBy());
if ($initiator === null || !$initiator->isEnabled()) {
return false;
}
return $share->getNode()->isReadable() && $share->getNode()->isShareable();
}
/**
* @return \OCP\Share\IShare
* @throws NotFoundException
*/
private function getShare() {
// Check whether share exists
try {
$share = $this->shareManager->getShareByToken($this->getToken());
} catch (ShareNotFound $e) {
// The share does not exists, we do not emit an ShareLinkAccessedEvent
throw new NotFoundException();
}
if (!$this->validateShare($share)) {
throw new NotFoundException();
}
return $share;
}
/**
* @return \OCP\Files\File|\OCP\Files\Folder
* @throws NotFoundException
*/
private function getShareNode() {
\OC_User::setIncognitoMode(true);
$share = $this->getShare();
return $share->getNode();
}
/**
* Save options values to the DB for current user
*
* @PublicPage
* @param $options
* @param null $myMapId
* @return DataResponse
* @throws NotFoundException
* @throws GenericFileException
* @throws InvalidPathException
* @throws NotPermittedException
*/
public function saveOptionValue($options, $myMapId = null): DataResponse {
$share = $this->getShare();
$permissions = $share->getPermissions();
$folder = $this->getShareNode();
$isCreatable = ($permissions & (1 << 2)) && $folder->isCreatable();
try {
$file = $folder->get('.index.maps');
} catch (NotFoundException $e) {
if ($isCreatable) {
$file = $folder->newFile('.index.maps', $content = '{}');
} else {
throw new NotFoundException();
}
}
$isUpdateable = ($permissions & (1 << 1)) && $file->isUpdateable();
if (!$isUpdateable) {
throw new NotPermittedException();
}
try {
$ov = json_decode($file->getContent(), true, 512);
foreach ($options as $key => $value) {
$ov[$key] = $value;
}
$file->putContent(json_encode($ov, JSON_PRETTY_PRINT));
} catch (LockedException $e) {
return new DataResponse('File is locked', 500);
}
return new DataResponse(['done' => 1]);
}
/**
* get options values from the config for current user
*
* @PublicPage
* @return DataResponse
* @throws InvalidPathException
* @throws LockedException
* @throws NotFoundException
* @throws NotPermittedException
*/
public function getOptionsValues(): DataResponse {
$ov = [];
$share = $this->getShare();
$permissions = $share->getPermissions();
$folder = $this->getShareNode();
$isCreatable = ($permissions & (1 << 2)) && $folder->isCreatable();
try {
$file = $folder->get('.index.maps');
} catch (NotFoundException $e) {
if ($isCreatable) {
$file = $folder->newFile('.index.maps', $content = '{}');
} else {
throw new NotFoundException();
}
}
$ov = json_decode($file->getContent(), true, 512);
// Maps content can be read mostly from the folder
$ov['isReadable'] = ($permissions & (1 << 0)) && $folder->isReadable();
//Saving maps information in the file
$ov['isUpdateable'] = ($permissions & (1 << 1)) && $file->isUpdateable();
$ov['isCreatable'] = ($permissions & (1 << 2)) && $folder->isCreatable();
//We can delete the map by deleting the folder or the .index.maps file
$ov['isDeletable'] = ($permissions & (1 << 3)) && ($folder->isDeletable() || $file->isDeletable());
// Share map by sharing the folder
$ov['isShareable'] = ($permissions & (1 << 4)) && $folder->isShareable();
// get routing-specific admin settings values
$settingsKeys = [
'osrmCarURL',
'osrmBikeURL',
'osrmFootURL',
'osrmDEMO',
'graphhopperAPIKEY',
'mapboxAPIKEY',
'maplibreStreetStyleURL',
'maplibreStreetStyleAuth',
'maplibreStreetStylePmtiles',
'graphhopperURL'
];
foreach ($settingsKeys as $k) {
$v = $this->config->getAppValue('maps', $k);
$ov[$k] = $v;
}
return new DataResponse(['values' => $ov]);
}
/**
* get content of mapbox traffic style
* @PublicPage
*
* @return DataResponse
*/
public function getTrafficStyle(): DataResponse {
$style = [
'version' => 8,
'name' => 'Mapbox Traffic tileset v1',
'sources' => [
'mapbox-traffic' => [
'url' => 'mapbox://mapbox.mapbox-traffic-v1',
'type' => 'vector'
]
],
'layers' => [
[
'id' => 'traffic',
'source' => 'mapbox-traffic',
'source-layer' => 'traffic',
'type' => 'line',
'paint' => [
'line-width' => 2.0,
'line-color' => [
'case',
[
'==',
'low',
[
'get',
'congestion'
]
],
'#00ff00',
[
'==',
'moderate',
[
'get',
'congestion'
]
],
'#ffad00',
[
'==',
'heavy',
[
'get',
'congestion'
]
],
'#ff0000',
[
'==',
'severe',
[
'get',
'congestion'
]
],
'#b43b71',
'#000000'
]
]
]
]
];
return new DataResponse($style);
}
}