|
| 1 | +<?php |
| 2 | + namespace Pdsinterop\PhpSolid; |
| 3 | + |
| 4 | + use Pdsinterop\PhpSolid\Server; |
| 5 | + use Pdsinterop\PhpSolid\User; |
| 6 | + use Pdsinterop\PhpSolid\Util; |
| 7 | + |
| 8 | + class ProfileServer extends Server { |
| 9 | + public static function getFileSystem() { |
| 10 | + $profileId = self::getProfileId(); |
| 11 | + |
| 12 | + // The internal adapter |
| 13 | + $adapter = new \League\Flysystem\Adapter\Local( |
| 14 | + // Determine root directory |
| 15 | + PROFILEBASE . "$profileId/" |
| 16 | + ); |
| 17 | + |
| 18 | + $graph = new \EasyRdf\Graph(); |
| 19 | + // Create Formats objects |
| 20 | + $formats = new \Pdsinterop\Rdf\Formats(); |
| 21 | + $serverUri = Util::getServerUri(); |
| 22 | + |
| 23 | + // Create the RDF Adapter |
| 24 | + $rdfAdapter = new \Pdsinterop\Rdf\Flysystem\Adapter\Rdf($adapter, $graph, $formats, $serverUri); |
| 25 | + |
| 26 | + $filesystem = new \League\Flysystem\Filesystem($rdfAdapter); |
| 27 | + $filesystem->addPlugin(new \Pdsinterop\Rdf\Flysystem\Plugin\AsMime($formats)); |
| 28 | + $plugin = new \Pdsinterop\Rdf\Flysystem\Plugin\ReadRdf($graph); |
| 29 | + $filesystem->addPlugin($plugin); |
| 30 | + return $filesystem; |
| 31 | + } |
| 32 | + |
| 33 | + public static function respond($response) { |
| 34 | + $statusCode = $response->getStatusCode(); |
| 35 | + $response->getBody()->rewind(); |
| 36 | + $headers = $response->getHeaders(); |
| 37 | + |
| 38 | + $body = $response->getBody()->getContents(); |
| 39 | + header("HTTP/1.1 $statusCode"); |
| 40 | + foreach ($headers as $header => $values) { |
| 41 | + foreach ($values as $value) { |
| 42 | + if ($header == "Location") { |
| 43 | + $value = preg_replace("|%26%2334%3B|", "%22", $value); // odoo weird encoding |
| 44 | + } |
| 45 | + header($header . ":" . $value); |
| 46 | + } |
| 47 | + } |
| 48 | + echo $body; |
| 49 | + } |
| 50 | + |
| 51 | + public static function getWebId($rawRequest) { |
| 52 | + $dpop = self::getDpop(); |
| 53 | + $webId = $dpop->getWebId($rawRequest); |
| 54 | + if (!isset($webId)) { |
| 55 | + $bearer = self::getBearer(); |
| 56 | + $webId = $bearer->getWebId($rawRequest); |
| 57 | + } |
| 58 | + return $webId; |
| 59 | + } |
| 60 | + |
| 61 | + private static function getProfileId() { |
| 62 | + $serverName = Util::getServerName(); |
| 63 | + $idParts = explode(".", $serverName, 2); |
| 64 | + $profileId = preg_replace("/^id-/", "", $idParts[0]); |
| 65 | + return $profileId; |
| 66 | + } |
| 67 | + |
| 68 | + public static function getOwner() { |
| 69 | + $profileId = self::getProfileId(); |
| 70 | + return User::getUserById($profileId); |
| 71 | + } |
| 72 | + |
| 73 | + public static function getOwnerWebId() { |
| 74 | + $owner = self::getOwner(); |
| 75 | + return $owner['webId']; |
| 76 | + } |
| 77 | + |
| 78 | + public static function initializeProfile() { |
| 79 | + $filesystem = self::getFilesystem(); |
| 80 | + if (!$filesystem->has("/.acl")) { |
| 81 | + $defaultAcl = self::generateDefaultAcl(); |
| 82 | + $filesystem->write("/.acl", $defaultAcl); |
| 83 | + } |
| 84 | + |
| 85 | + // Generate default folders and ACLs: |
| 86 | + if (!$filesystem->has("/profile.ttl")) { |
| 87 | + $profile = self::generateDefaultProfile(); |
| 88 | + $filesystem->write("/profile.ttl", $profile); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + public static function generateDefaultAcl() { |
| 93 | + $webId = self::getOwnerWebId(); |
| 94 | + $acl = <<< "EOF" |
| 95 | +# Root ACL resource for the user account |
| 96 | +@prefix acl: <http://www.w3.org/ns/auth/acl#>. |
| 97 | +@prefix foaf: <http://xmlns.com/foaf/0.1/>. |
| 98 | +
|
| 99 | +# The homepage is readable by the public |
| 100 | +<#public> |
| 101 | + a acl:Authorization; |
| 102 | + acl:agentClass foaf:Agent; |
| 103 | + acl:accessTo <./>; |
| 104 | + # All resources will inherit this authorization, by default |
| 105 | + acl:default <./>; |
| 106 | + acl:mode acl:Read. |
| 107 | +
|
| 108 | +# The owner has full access to every resource in their pod. |
| 109 | +# Other agents have no access rights, |
| 110 | +# unless specifically authorized in other .acl resources. |
| 111 | +<#owner> |
| 112 | + a acl:Authorization; |
| 113 | + acl:agent <$webId>; |
| 114 | + # Set the access to the root storage folder itself |
| 115 | + acl:accessTo <./>; |
| 116 | + # All resources will inherit this authorization, by default |
| 117 | + acl:default <./>; |
| 118 | + # The owner has all of the access modes allowed |
| 119 | + acl:mode |
| 120 | + acl:Read, acl:Write, acl:Control. |
| 121 | +EOF; |
| 122 | + return $acl; |
| 123 | + } |
| 124 | + |
| 125 | + public static function generateDefaultProfile() { |
| 126 | + $user = self::getOwner(); |
| 127 | + if (!isset($user['storage']) || !$user['storage']) { |
| 128 | + $user['storage'] = "https://storage-" . self::getProfileId() . "." . BASEDOMAIN . "/"; |
| 129 | + } |
| 130 | + if (is_array($user['storage'])) { // empty array is already handled |
| 131 | + $user['storage'] = array_values($user['storage'])[0]; // FIXME: Handle multiple storage pods |
| 132 | + } |
| 133 | + if (!isset($user['issuer'])) { |
| 134 | + $user['issuer'] = BASEURL; |
| 135 | + } |
| 136 | + |
| 137 | + $profile = <<< "EOF" |
| 138 | +@prefix : <#>. |
| 139 | +@prefix acl: <http://www.w3.org/ns/auth/acl#>. |
| 140 | +@prefix foaf: <http://xmlns.com/foaf/0.1/>. |
| 141 | +@prefix ldp: <http://www.w3.org/ns/ldp#>. |
| 142 | +@prefix schema: <http://schema.org/>. |
| 143 | +@prefix solid: <http://www.w3.org/ns/solid/terms#>. |
| 144 | +@prefix space: <http://www.w3.org/ns/pim/space#>. |
| 145 | +@prefix vcard: <http://www.w3.org/2006/vcard/ns#>. |
| 146 | +@prefix pro: <./>. |
| 147 | +@prefix inbox: <{$user['storage']}inbox/>. |
| 148 | +
|
| 149 | +<> a foaf:PersonalProfileDocument; foaf:maker :me; foaf:primaryTopic :me. |
| 150 | +
|
| 151 | +:me |
| 152 | + a schema:Person, foaf:Person; |
| 153 | + ldp:inbox inbox:; |
| 154 | + space:preferencesFile <{$user['storage']}settings/preferences.ttl>; |
| 155 | + space:storage <{$user['storage']}>; |
| 156 | + solid:account <{$user['storage']}>; |
| 157 | + solid:oidcIssuer <{$user['issuer']}>; |
| 158 | + solid:privateTypeIndex <{$user['storage']}settings/privateTypeIndex.ttl>; |
| 159 | + solid:publicTypeIndex <{$user['storage']}settings/publicTypeIndex.ttl>. |
| 160 | +EOF; |
| 161 | + return $profile; |
| 162 | + } |
| 163 | + } |
0 commit comments