|
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($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() { |
10 | 63 | $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 | + } |
11 | 77 |
|
| 78 | + public static function getFileSystem() { |
| 79 | + $storageId = self::getStorageId(); |
| 80 | + if (!self::storageIdExists($storageId)) { |
| 81 | + throw new Exception("Storage does not exist"); |
| 82 | + } |
12 | 83 | // The internal adapter |
13 | 84 | $adapter = new \League\Flysystem\Adapter\Local( |
14 | 85 | // Determine root directory |
@@ -65,15 +136,6 @@ private static function getStorageId() { |
65 | 136 | return $storageId; |
66 | 137 | } |
67 | 138 |
|
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 | 139 |
|
78 | 140 | public static function initializeStorage() { |
79 | 141 | $filesystem = self::getFilesystem(); |
@@ -119,6 +181,9 @@ public static function initializeStorage() { |
119 | 181 |
|
120 | 182 | public static function generateDefaultAcl() { |
121 | 183 | $webId = self::getOwnerWebId(); |
| 184 | + if (!$webId) { |
| 185 | + throw new Exception("No owner found for storage"); |
| 186 | + } |
122 | 187 | $acl = <<< "EOF" |
123 | 188 | # Root ACL resource for the user account |
124 | 189 | @prefix acl: <http://www.w3.org/ns/auth/acl#>. |
@@ -150,6 +215,9 @@ public static function generateDefaultAcl() { |
150 | 215 |
|
151 | 216 | public static function generatePublicAppendAcl() { |
152 | 217 | $webId = self::getOwnerWebId(); |
| 218 | + if (!$webId) { |
| 219 | + throw new Exception("No owner found for storage ID"); |
| 220 | + } |
153 | 221 | $acl = <<< "EOF" |
154 | 222 | # Inbox ACL resource for the user account |
155 | 223 | @prefix acl: <http://www.w3.org/ns/auth/acl#>. |
@@ -179,6 +247,9 @@ public static function generatePublicAppendAcl() { |
179 | 247 |
|
180 | 248 | public static function generatePublicReadAcl() { |
181 | 249 | $webId = self::getOwnerWebId(); |
| 250 | + if (!$webId) { |
| 251 | + throw new Exception("No owner found for storage ID"); |
| 252 | + } |
182 | 253 | $acl = <<< "EOF" |
183 | 254 | # Inbox ACL resource for the user account |
184 | 255 | @prefix acl: <http://www.w3.org/ns/auth/acl#>. |
|
0 commit comments