-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStorageServer.php
More file actions
334 lines (300 loc) · 9.28 KB
/
StorageServer.php
File metadata and controls
334 lines (300 loc) · 9.28 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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
<?php
namespace Pdsinterop\PhpSolid;
use Pdsinterop\PhpSolid\Server;
use Pdsinterop\PhpSolid\User;
use Pdsinterop\PhpSolid\Util;
use Pdsinterop\PhpSolid\Db;
class StorageServer extends Server {
public static function getStorage($storageId) {
Db::connect();
$query = Db::$pdo->prepare(
'SELECT * FROM storage WHERE storage_id=:storageId'
);
$query->execute([
':storageId' => $storageId
]);
return $query->fetchAll();
}
public static function setStorageOwner($storageId, $owner) {
Db::connect();
$query = Db::$pdo->prepare(
'UPDATE storage SET owner=:owner WHERE storage_id=:storageId'
);
$query->execute([
':storageId' => $storageId,
':owner' => $owner
]);
}
public static function createStorage($ownerWebId) {
$generatedStorageId = bin2hex(random_bytes(16));
while (self::storageIdExists($generatedStorageId)) {
$generatedStorageId = bin2hex(random_bytes(16));
}
Db::connect();
$query = Db::$pdo->prepare(
'INSERT OR REPLACE INTO storage VALUES(:storageId, :owner)'
);
$query->execute([
':storageId' => $generatedStorageId,
':owner' => $ownerWebId
]);
return [
"storageId" => $generatedStorageId,
"storageUrl" => "https://storage-" . $generatedStorageId . "." . BASEDOMAIN . "/"
];
}
public static function storageIdExists($storageId) {
Db::connect();
$query = Db::$pdo->prepare(
'SELECT storage_id FROM storage WHERE storage_id=:storageId'
);
$query->execute([
':storageId' => $storageId
]);
$result = $query->fetchAll();
if (sizeof($result) === 1) {
return true;
}
return false;
}
public static function getOwnerWebId() {
$storageId = self::getStorageId();
Db::connect();
$query = Db::$pdo->prepare(
'SELECT owner FROM storage WHERE storage_id=:storageId'
);
$query->execute([
':storageId' => $storageId
]);
$result = $query->fetchAll();
if (sizeof($result) === 1) {
return $result[0]['owner'];
}
return false;
}
public static function getFileSystem() {
$storageId = self::getStorageId();
if (!self::storageIdExists($storageId)) {
throw new \Exception("Storage does not exist");
}
if (is_dir(STORAGEBASE . "$storageId/")) { // backwards compatiblity check
$storagePath = $storageId;
} else {
$storagePath = implode("/", str_split($storageId, 4));
}
// The internal adapter
$adapter = new \League\Flysystem\Adapter\Local(
// Determine root directory
STORAGEBASE . "$storagePath/"
);
$graph = new \EasyRdf\Graph();
// Create Formats objects
$formats = new \Pdsinterop\Rdf\Formats();
$serverUri = Util::getServerUri();
// Create the RDF Adapter
$rdfAdapter = new \Pdsinterop\Rdf\Flysystem\Adapter\Rdf($adapter, $graph, $formats, $serverUri);
$filesystem = new \League\Flysystem\Filesystem($rdfAdapter);
$filesystem->addPlugin(new \Pdsinterop\Rdf\Flysystem\Plugin\AsMime($formats));
$plugin = new \Pdsinterop\Rdf\Flysystem\Plugin\ReadRdf($graph);
$filesystem->addPlugin($plugin);
return $filesystem;
}
public static function respond($response) {
$statusCode = $response->getStatusCode();
$response->getBody()->rewind();
$headers = $response->getHeaders();
$body = $response->getBody()->getContents();
header("HTTP/1.1 $statusCode");
foreach ($headers as $header => $values) {
foreach ($values as $value) {
if ($header == "Location") {
$value = preg_replace("|%26%2334%3B|", "%22", $value); // odoo weird encoding
}
header($header . ":" . $value);
}
}
echo $body;
}
public static function getWebId($rawRequest) {
$dpop = self::getDpop();
$webId = $dpop->getWebId($rawRequest);
if (!isset($webId)) {
$bearer = self::getBearer();
$webId = $bearer->getWebId($rawRequest);
}
return $webId;
}
private static function getStorageId() {
$serverName = Util::getServerName();
$idParts = explode(".", $serverName, 2);
$storageId = preg_replace("/^storage-/", "", $idParts[0]);
return $storageId;
}
public static function initializeStorage() {
$filesystem = self::getFilesystem();
if (!$filesystem->has("/.acl")) {
$defaultAcl = self::generateDefaultAcl();
$filesystem->write("/.acl", $defaultAcl);
}
// Generate default folders and ACLs:
if (!$filesystem->has("/inbox")) {
$filesystem->createDir("/inbox");
}
if (!$filesystem->has("/inbox/.acl")) {
$inboxAcl = self::generatePublicAppendAcl();
$filesystem->write("/inbox/.acl", $inboxAcl);
}
if (!$filesystem->has("/settings")) {
$filesystem->createDir("/settings");
}
if (!$filesystem->has("/settings/privateTypeIndex.ttl")) {
$privateTypeIndex = self::generateDefaultPrivateTypeIndex();
$filesystem->write("/settings/privateTypeIndex.ttl", $privateTypeIndex);
}
if (!$filesystem->has("/settings/publicTypeIndex.ttl")) {
$publicTypeIndex = self::generateDefaultPublicTypeIndex();
$filesystem->write("/settings/publicTypeIndex.ttl", $publicTypeIndex);
}
if (!$filesystem->has("/settings/preferences.ttl")) {
$preferences = self::generateDefaultPreferences();
$filesystem->write("/settings/preferences.ttl", $preferences);
}
if (!$filesystem->has("/public")) {
$filesystem->createDir("/public");
}
if (!$filesystem->has("/public/.acl")) {
$publicAcl = self::generatePublicReadAcl();
$filesystem->write("/public/.acl", $publicAcl);
}
if (!$filesystem->has("/private")) {
$filesystem->createDir("/private");
}
}
public static function generateDefaultAcl() {
$webId = self::getOwnerWebId();
if (!$webId) {
throw new \Exception("No owner found for storage");
}
$acl = <<< "EOF"
# Root ACL resource for the user account
@prefix acl: <http://www.w3.org/ns/auth/acl#>.
@prefix foaf: <http://xmlns.com/foaf/0.1/>.
# The homepage is readable by the public
<#public>
a acl:Authorization;
acl:agentClass foaf:Agent;
acl:accessTo <./>;
acl:mode acl:Read.
# The owner has full access to every resource in their pod.
# Other agents have no access rights,
# unless specifically authorized in other .acl resources.
<#owner>
a acl:Authorization;
acl:agent <$webId>;
# Set the access to the root storage folder itself
acl:accessTo <./>;
# All resources will inherit this authorization, by default
acl:default <./>;
# The owner has all of the access modes allowed
acl:mode
acl:Read, acl:Write, acl:Control.
EOF;
return $acl;
}
public static function generatePublicAppendAcl() {
$webId = self::getOwnerWebId();
if (!$webId) {
throw new \Exception("No owner found for storage ID");
}
$acl = <<< "EOF"
# Inbox ACL resource for the user account
@prefix acl: <http://www.w3.org/ns/auth/acl#>.
@prefix foaf: <http://xmlns.com/foaf/0.1/>.
<#public>
a acl:Authorization;
acl:agentClass foaf:Agent;
acl:accessTo <./>;
acl:default <./>;
acl:mode
acl:Append.
<#owner>
a acl:Authorization;
acl:agent <$webId>;
# Set the access to the root storage folder itself
acl:accessTo <./>;
# All resources will inherit this authorization, by default
acl:default <./>;
# The owner has all of the access modes allowed
acl:mode
acl:Read, acl:Write, acl:Control.
EOF;
return $acl;
}
public static function generatePublicReadAcl() {
$webId = self::getOwnerWebId();
if (!$webId) {
throw new \Exception("No owner found for storage ID");
}
$acl = <<< "EOF"
# Inbox ACL resource for the user account
@prefix acl: <http://www.w3.org/ns/auth/acl#>.
@prefix foaf: <http://xmlns.com/foaf/0.1/>.
<#public>
a acl:Authorization;
acl:agentClass foaf:Agent;
acl:accessTo <./>;
acl:default <./>;
acl:mode
acl:Read.
<#owner>
a acl:Authorization;
acl:agent <$webId>;
# Set the access to the root storage folder itself
acl:accessTo <./>;
# All resources will inherit this authorization, by default
acl:default <./>;
# The owner has all of the access modes allowed
acl:mode
acl:Read, acl:Write, acl:Control.
EOF;
return $acl;
}
public static function generateDefaultPrivateTypeIndex() {
$typeIndex = <<< "EOF"
# Private type index
@prefix : <#>.
@prefix solid: <http://www.w3.org/ns/solid/terms#>.
<>
a solid:UnlistedDocument, solid:TypeIndex.
EOF;
return $typeIndex;
}
public static function generateDefaultPublicTypeIndex() {
$typeIndex = <<< "EOF"
# Public type index
@prefix : <#>.
@prefix solid: <http://www.w3.org/ns/solid/terms#>.
<>
a solid:ListedDocument, solid:TypeIndex.
EOF;
return $typeIndex;
}
public static function generateDefaultPreferences() {
$webId = self::getOwnerWebId();
$preferences = <<< "EOF"
# Preferences
@prefix : <#>.
@prefix sp: <http://www.w3.org/ns/pim/space#>.
@prefix dct: <http://purl.org/dc/terms/>.
@prefix solid: <http://www.w3.org/ns/solid/terms#>.
<>
a sp:ConfigurationFile;
dct:title "Preferences file".
<$webId>
a solid:Developer;
solid:privateTypeIndex <privateTypeIndex.ttl>;
solid:publicTypeIndex <publicTypeIndex.ttl>.
EOF;
return $preferences;
}
}