-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolidStorage.php
More file actions
79 lines (63 loc) · 2.37 KB
/
SolidStorage.php
File metadata and controls
79 lines (63 loc) · 2.37 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
<?php
namespace Pdsinterop\PhpSolid\Routes;
use Pdsinterop\PhpSolid\User;
use Pdsinterop\PhpSolid\StorageServer;
use Pdsinterop\PhpSolid\ClientRegistration;
use Pdsinterop\PhpSolid\SolidNotifications;
use Pdsinterop\PhpSolid\Util;
use Pdsinterop\Solid\Auth\WAC;
use Pdsinterop\Solid\Resources\Server as ResourceServer;
use Laminas\Diactoros\ServerRequestFactory;
use Laminas\Diactoros\Response;
class SolidStorage {
public static function respondToStorage() {
$requestFactory = new ServerRequestFactory();
$rawRequest = $requestFactory->fromGlobals($_SERVER, $_GET, $_POST, $_COOKIE, $_FILES);
try {
StorageServer::initializeStorage();
$filesystem = StorageServer::getFileSystem();
} catch (\Exception $e) {
$response = new Response();
$response = $response->withStatus(404, "Not found");
StorageServer::respond($response);
exit();
}
$resourceServer = new ResourceServer($filesystem, new Response(), null);
$solidNotifications = new SolidNotifications();
$resourceServer->setNotifications($solidNotifications);
$wac = new WAC($filesystem);
$baseUrl = Util::getServerBaseUrl();
$resourceServer->setBaseUrl($baseUrl);
$wac->setBaseUrl($baseUrl);
$webId = StorageServer::getWebId($rawRequest);
if (!isset($webId)) {
$response = $resourceServer->getResponse()
->withStatus(409, "Invalid token");
StorageServer::respond($response);
exit();
}
$origin = $rawRequest->getHeaderLine("Origin");
// FIXME: Read allowed clients from the profile instead;
$ownerWebId = StorageServer::getOwnerWebId();
$owner = User::getUserByWebId($ownerWebId);
$allowedClients = $owner['allowedClients'] ?? [];
$allowedOrigins = array_merge(
($owner['allowedOrigins'] ?? []),
(TRUSTED_APPS ?? [])
);
$allowedOrigins = array_unique($allowedOrigins);
if (!isset($origin) || ($origin === "")) {
$allowedOrigins[] = "app://unset"; // FIXME: this should not be here.
$origin = "app://unset";
}
if (!$wac->isAllowed($rawRequest, $webId, $origin, $allowedOrigins)) {
$response = new Response();
$response = $response->withStatus(403, "Access denied!");
StorageServer::respond($response);
exit();
}
$response = $resourceServer->respondToRequest($rawRequest);
$response = $wac->addWACHeaders($rawRequest, $response, $webId);
StorageServer::respond($response);
}
}