|
4 | 4 | use Pdsinterop\PhpSolid\Server; |
5 | 5 | use Pdsinterop\PhpSolid\User; |
6 | 6 | use Pdsinterop\PhpSolid\Util; |
| 7 | + use Pdsinterop\PhpSolid\Db; |
7 | 8 |
|
8 | 9 | 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($generatedStorageId)) { |
| 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' => $generatedStorageId, |
| 43 | + ':owner' => $ownerWebId |
| 44 | + ]); |
| 45 | + return [ |
| 46 | + "storageId" => $generatedStorageId |
| 47 | + ]; |
| 48 | + } |
| 49 | + |
| 50 | + public static function storageIdExists($storageId) { |
| 51 | + Db::connect(); |
| 52 | + $query = Db::$pdo->prepare( |
| 53 | + 'SELECT storage_id FROM storage WHERE storage_id=:storageId' |
| 54 | + ); |
| 55 | + $query->execute([ |
| 56 | + ':storageId' => $storageId |
| 57 | + ]); |
| 58 | + $result = $query->fetchAll(); |
| 59 | + if (sizeof($result) === 1) { |
| 60 | + return true; |
| 61 | + } |
| 62 | + return false; |
| 63 | + } |
| 64 | + |
| 65 | + public static function getOwnerWebId() { |
10 | 66 | $storageId = self::getStorageId(); |
| 67 | + Db::connect(); |
| 68 | + $query = Db::$pdo->prepare( |
| 69 | + 'SELECT owner FROM storage WHERE storage_id=:storageId' |
| 70 | + ); |
| 71 | + $query->execute([ |
| 72 | + ':storageId' => $storageId |
| 73 | + ]); |
| 74 | + $result = $query->fetchAll(); |
| 75 | + if (sizeof($result) === 1) { |
| 76 | + return $result[0]['owner']; |
| 77 | + } |
| 78 | + return false; |
| 79 | + } |
11 | 80 |
|
| 81 | + public static function getFileSystem() { |
| 82 | + $storageId = self::getStorageId(); |
| 83 | + if (!self::storageIdExists($storageId)) { |
| 84 | + throw new \Exception("Storage does not exist"); |
| 85 | + } |
12 | 86 | // The internal adapter |
13 | 87 | $adapter = new \League\Flysystem\Adapter\Local( |
14 | 88 | // Determine root directory |
@@ -65,16 +139,6 @@ private static function getStorageId() { |
65 | 139 | return $storageId; |
66 | 140 | } |
67 | 141 |
|
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 | | - } |
77 | | - |
78 | 142 | public static function initializeStorage() { |
79 | 143 | $filesystem = self::getFilesystem(); |
80 | 144 | if (!$filesystem->has("/.acl")) { |
@@ -119,6 +183,9 @@ public static function initializeStorage() { |
119 | 183 |
|
120 | 184 | public static function generateDefaultAcl() { |
121 | 185 | $webId = self::getOwnerWebId(); |
| 186 | + if (!$webId) { |
| 187 | + throw new \Exception("No owner found for storage"); |
| 188 | + } |
122 | 189 | $acl = <<< "EOF" |
123 | 190 | # Root ACL resource for the user account |
124 | 191 | @prefix acl: <http://www.w3.org/ns/auth/acl#>. |
@@ -150,6 +217,9 @@ public static function generateDefaultAcl() { |
150 | 217 |
|
151 | 218 | public static function generatePublicAppendAcl() { |
152 | 219 | $webId = self::getOwnerWebId(); |
| 220 | + if (!$webId) { |
| 221 | + throw new \Exception("No owner found for storage ID"); |
| 222 | + } |
153 | 223 | $acl = <<< "EOF" |
154 | 224 | # Inbox ACL resource for the user account |
155 | 225 | @prefix acl: <http://www.w3.org/ns/auth/acl#>. |
@@ -179,6 +249,9 @@ public static function generatePublicAppendAcl() { |
179 | 249 |
|
180 | 250 | public static function generatePublicReadAcl() { |
181 | 251 | $webId = self::getOwnerWebId(); |
| 252 | + if (!$webId) { |
| 253 | + throw new \Exception("No owner found for storage ID"); |
| 254 | + } |
182 | 255 | $acl = <<< "EOF" |
183 | 256 | # Inbox ACL resource for the user account |
184 | 257 | @prefix acl: <http://www.w3.org/ns/auth/acl#>. |
|
0 commit comments