Skip to content

Commit 92b9898

Browse files
committed
add db calls
1 parent d06c521 commit 92b9898

1 file changed

Lines changed: 81 additions & 10 deletions

File tree

lib/StorageServer.php

Lines changed: 81 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,82 @@
44
use Pdsinterop\PhpSolid\Server;
55
use Pdsinterop\PhpSolid\User;
66
use Pdsinterop\PhpSolid\Util;
7+
use Pdsinterop\PhpSolid\Db;
78

89
class StorageServer extends Server {
9-
public static function getFileSystem() {
10+
public static function getStorage($storageId) {
11+
Db::connect();
12+
$query = Db::$pdo->prepare(
13+
'SELECT * FROM storage WHERE storage_id=:storageId'
14+
);
15+
$query->execute([
16+
':storageId' => $storageId
17+
]);
18+
return $query->fetchAll();
19+
}
20+
21+
public static function setStorageOwner($storageId, $owner) {
22+
Db::connect();
23+
$query = Db::$pdo->prepare(
24+
'UPDATE storage SET owner=:owner WHERE storage_id=:storageId'
25+
);
26+
$query->execute([
27+
':storageId' => $storageId,
28+
':owner' => $owner
29+
]);
30+
}
31+
32+
public static function createStorage($ownerWebId) {
33+
$generatedStorageId = bin2hex(random_bytes(16));
34+
while (self::storageIdExists($generatedsStorageId)) {
35+
$generatedStorageId = bin2hex(random_bytes(16));
36+
}
37+
Db::connect();
38+
$query = Db::$pdo->prepare(
39+
'INSERT OR REPLACE INTO storage VALUES(:storageId, :owner)'
40+
);
41+
$query->execute([
42+
':storageId' => $storageId,
43+
':owner' => $ownerWebId
44+
]);
45+
}
46+
47+
public static function storageIdExists($storageId) {
48+
Db::connect();
49+
$query = Db::$pdo->prepare(
50+
'SELECT storage_id FROM storage WHERE storage_id=:storageId'
51+
);
52+
$query->execute([
53+
':storageId' => $storageId
54+
]);
55+
$result = $query->fetchAll();
56+
if (sizeof($result) === 1) {
57+
return true;
58+
}
59+
return false;
60+
}
61+
62+
public static function getOwnerWebId() {
1063
$storageId = self::getStorageId();
64+
Db::connect();
65+
$query = Db::$pdo->prepare(
66+
'SELECT owner FROM storage WHERE storage_id=:storageId'
67+
);
68+
$query->execute([
69+
':storageId' => $storageId
70+
]);
71+
$result = $query->fetchAll();
72+
if (sizeof($result) === 1) {
73+
return $result[0]['owner'];
74+
}
75+
return false;
76+
}
1177

78+
public static function getFileSystem() {
79+
$storageId = self::getStorageId();
80+
if (!self::storageIdExists($storageId)) {
81+
throw new Exception("Storage does not exist");
82+
}
1283
// The internal adapter
1384
$adapter = new \League\Flysystem\Adapter\Local(
1485
// Determine root directory
@@ -65,15 +136,6 @@ private static function getStorageId() {
65136
return $storageId;
66137
}
67138

68-
public static function getOwner() {
69-
$storageId = self::getStorageId();
70-
return User::getUserById($storageId);
71-
}
72-
73-
public static function getOwnerWebId() {
74-
$owner = self::getOwner();
75-
return $owner['webId'];
76-
}
77139

78140
public static function initializeStorage() {
79141
$filesystem = self::getFilesystem();
@@ -119,6 +181,9 @@ public static function initializeStorage() {
119181

120182
public static function generateDefaultAcl() {
121183
$webId = self::getOwnerWebId();
184+
if (!$webId) {
185+
throw new Exception("No owner found for storage");
186+
}
122187
$acl = <<< "EOF"
123188
# Root ACL resource for the user account
124189
@prefix acl: <http://www.w3.org/ns/auth/acl#>.
@@ -150,6 +215,9 @@ public static function generateDefaultAcl() {
150215

151216
public static function generatePublicAppendAcl() {
152217
$webId = self::getOwnerWebId();
218+
if (!$webId) {
219+
throw new Exception("No owner found for storage ID");
220+
}
153221
$acl = <<< "EOF"
154222
# Inbox ACL resource for the user account
155223
@prefix acl: <http://www.w3.org/ns/auth/acl#>.
@@ -179,6 +247,9 @@ public static function generatePublicAppendAcl() {
179247

180248
public static function generatePublicReadAcl() {
181249
$webId = self::getOwnerWebId();
250+
if (!$webId) {
251+
throw new Exception("No owner found for storage ID");
252+
}
182253
$acl = <<< "EOF"
183254
# Inbox ACL resource for the user account
184255
@prefix acl: <http://www.w3.org/ns/auth/acl#>.

0 commit comments

Comments
 (0)