-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStorageServer.php
More file actions
254 lines (223 loc) · 6.78 KB
/
StorageServer.php
File metadata and controls
254 lines (223 loc) · 6.78 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
<?php
namespace Pdsinterop\PhpSolid;
use Pdsinterop\PhpSolid\Server;
use Pdsinterop\PhpSolid\User;
use Pdsinterop\PhpSolid\Util;
class StorageServer extends Server {
public static function getFileSystem() {
$storageId = self::getStorageId();
// The internal adapter
$adapter = new \League\Flysystem\Adapter\Local(
// Determine root directory
STORAGEBASE . "$storageId/"
);
$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 getOwner() {
$storageId = self::getStorageId();
return User::getUserById($storageId);
}
public static function getOwnerWebId() {
$owner = self::getOwner();
return $owner['webId'];
}
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();
$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();
$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();
$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;
}
}