From 56b53d4ed729a0101bcf647850a2ca0ccc5a6f26 Mon Sep 17 00:00:00 2001 From: Noel De Martin Date: Fri, 28 Apr 2023 09:39:48 +0200 Subject: [PATCH 1/2] Clean up spacing in controllers --- solid/lib/Controller/CalendarController.php | 30 ++++++++--------- solid/lib/Controller/ContactsController.php | 28 ++++++++-------- solid/lib/Controller/ProfileController.php | 36 ++++++++++----------- solid/lib/Controller/StorageController.php | 28 ++++++++-------- 4 files changed, 61 insertions(+), 61 deletions(-) diff --git a/solid/lib/Controller/CalendarController.php b/solid/lib/Controller/CalendarController.php index 0d262f9..6833fc9 100644 --- a/solid/lib/Controller/CalendarController.php +++ b/solid/lib/Controller/CalendarController.php @@ -26,7 +26,7 @@ class CalendarController extends Controller { /* @var ISession */ private $session; - + public function __construct( $AppName, IRequest $request, @@ -50,7 +50,7 @@ public function __construct( private function getFileSystem($userId) { // Make sure the root folder has an acl file, as is required by the spec; - // Generate a default file granting the owner full access. + // Generate a default file granting the owner full access. $defaultAcl = $this->generateDefaultAcl($userId); // Create the Nextcloud Calendar Adapter @@ -74,7 +74,7 @@ private function getFileSystem($userId) { $filesystem = new \League\Flysystem\Filesystem($rdfAdapter); $filesystem->addPlugin(new \Pdsinterop\Rdf\Flysystem\Plugin\AsMime($formats)); - + $plugin = new \Pdsinterop\Rdf\Flysystem\Plugin\ReadRdf($graph); $filesystem->addPlugin($plugin); @@ -122,18 +122,18 @@ private function getCalendarUrl($userId) { * @NoCSRFRequired */ public function handleRequest($userId, $path) { - $this->calendarUserId = $userId; - + $this->calendarUserId = $userId; + $this->rawRequest = \Laminas\Diactoros\ServerRequestFactory::fromGlobals($_SERVER, $_GET, $_POST, $_COOKIE, $_FILES); $this->response = new \Laminas\Diactoros\Response(); $this->filesystem = $this->getFileSystem($userId); - $this->resourceServer = new ResourceServer($this->filesystem, $this->response); + $this->resourceServer = new ResourceServer($this->filesystem, $this->response); $this->WAC = new WAC($this->filesystem); $request = $this->rawRequest; - $baseUrl = $this->getCalendarUrl($userId); + $baseUrl = $this->getCalendarUrl($userId); $this->resourceServer->setBaseUrl($baseUrl); $this->WAC->setBaseUrl($baseUrl); $notifications = new SolidNotifications(); @@ -148,26 +148,26 @@ public function handleRequest($userId, $path) { ->withStatus(Http::STATUS_CONFLICT, "Invalid token " . $e->getMessage()); return $this->respond($response); } - + if (!$this->WAC->isAllowed($request, $webId)) { $response = $this->resourceServer->getResponse()->withStatus(403, "Access denied"); return $this->respond($response); } - $response = $this->resourceServer->respondToRequest($request); + $response = $this->resourceServer->respondToRequest($request); $response = $this->WAC->addWACHeaders($request, $response, $webId); return $this->respond($response); } - + /** * @PublicPage * @NoAdminRequired * @NoCSRFRequired */ - public function handleGet($userId, $path) { + public function handleGet($userId, $path) { return $this->handleRequest($userId, $path); } - + /** * @PublicPage * @NoAdminRequired @@ -184,7 +184,7 @@ public function handlePost($userId, $path) { public function handlePut() { // $userId, $path) { // FIXME: Adding the correct variables in the function name will make nextcloud // throw an error about accessing put twice, so we will find out the userId and path from $_SERVER instead; - + // because we got here, the request uri should look like: // /index.php/apps/solid/@{userId}/storage{path} $pathInfo = explode("@", $_SERVER['REQUEST_URI']); @@ -192,7 +192,7 @@ public function handlePut() { // $userId, $path) { $userId = $pathInfo[0]; $path = $pathInfo[1]; $path = preg_replace("/^calendar/", "", $path); - + return $this->handleRequest($userId, $path); } /** @@ -237,7 +237,7 @@ private function respond($response) { foreach ($headers as $header => $values) { $result->addHeader($header, implode(", ", $values)); } - + $result->setStatus($statusCode); return $result; } diff --git a/solid/lib/Controller/ContactsController.php b/solid/lib/Controller/ContactsController.php index cc07f7d..d3848b2 100644 --- a/solid/lib/Controller/ContactsController.php +++ b/solid/lib/Controller/ContactsController.php @@ -27,7 +27,7 @@ class ContactsController extends Controller /* @var ISession */ private $session; - + public function __construct( $AppName, IRequest $request, @@ -51,7 +51,7 @@ public function __construct( private function getFileSystem($userId) { // Make sure the root folder has an acl file, as is required by the spec; - // Generate a default file granting the owner full access. + // Generate a default file granting the owner full access. $defaultAcl = $this->generateDefaultAcl($userId); // Create the Nextcloud Contacts Adapter @@ -75,7 +75,7 @@ private function getFileSystem($userId) { $filesystem = new \League\Flysystem\Filesystem($rdfAdapter); $filesystem->addPlugin(new \Pdsinterop\Rdf\Flysystem\Plugin\AsMime($formats)); - + $plugin = new \Pdsinterop\Rdf\Flysystem\Plugin\ReadRdf($graph); $filesystem->addPlugin($plugin); @@ -123,18 +123,18 @@ private function getContactsUrl($userId) { * @NoCSRFRequired */ public function handleRequest($userId, $path) { - $this->contactsUserId = $userId; - + $this->contactsUserId = $userId; + $this->rawRequest = \Laminas\Diactoros\ServerRequestFactory::fromGlobals($_SERVER, $_GET, $_POST, $_COOKIE, $_FILES); $this->response = new \Laminas\Diactoros\Response(); $this->filesystem = $this->getFileSystem($userId); - $this->resourceServer = new ResourceServer($this->filesystem, $this->response); + $this->resourceServer = new ResourceServer($this->filesystem, $this->response); $this->WAC = new WAC($this->filesystem); $request = $this->rawRequest; - $baseUrl = $this->getContactsUrl($userId); + $baseUrl = $this->getContactsUrl($userId); $this->resourceServer->setBaseUrl($baseUrl); $this->WAC->setBaseUrl($baseUrl); $notifications = new SolidNotifications(); @@ -149,26 +149,26 @@ public function handleRequest($userId, $path) { ->withStatus(Http::STATUS_CONFLICT, "Invalid token " . $e->getMessage()); return $this->respond($response); } - + if (!$this->WAC->isAllowed($request, $webId)) { $response = $this->resourceServer->getResponse()->withStatus(403, "Access denied"); return $this->respond($response); } - $response = $this->resourceServer->respondToRequest($request); + $response = $this->resourceServer->respondToRequest($request); $response = $this->WAC->addWACHeaders($request, $response, $webId); return $this->respond($response); } - + /** * @PublicPage * @NoAdminRequired * @NoCSRFRequired */ - public function handleGet($userId, $path) { + public function handleGet($userId, $path) { return $this->handleRequest($userId, $path); } - + /** * @PublicPage * @NoAdminRequired @@ -185,7 +185,7 @@ public function handlePost($userId, $path) { public function handlePut() { // $userId, $path) { // FIXME: Adding the correct variables in the function name will make nextcloud // throw an error about accessing put twice, so we will find out the userId and path from $_SERVER instead; - + // because we got here, the request uri should look like: // /index.php/apps/solid/@{userId}/storage{path} $pathInfo = explode("@", $_SERVER['REQUEST_URI']); @@ -193,7 +193,7 @@ public function handlePut() { // $userId, $path) { $userId = $pathInfo[0]; $path = $pathInfo[1]; $path = preg_replace("/^contacts/", "", $path); - + return $this->handleRequest($userId, $path); } /** diff --git a/solid/lib/Controller/ProfileController.php b/solid/lib/Controller/ProfileController.php index 65cf0fb..323b523 100644 --- a/solid/lib/Controller/ProfileController.php +++ b/solid/lib/Controller/ProfileController.php @@ -27,7 +27,7 @@ class ProfileController extends Controller { /* @var ISession */ private $session; - + public function __construct( $AppName, IRequest $request, @@ -54,7 +54,7 @@ public function __construct( private function getFileSystem($userId) { // Make sure the root folder has an acl file, as is required by the spec; - // Generate a default file granting the owner full access. + // Generate a default file granting the owner full access. $defaultAcl = $this->generateDefaultAcl($userId); $profile = $this->generateTurtleProfile($userId); @@ -78,7 +78,7 @@ private function getFileSystem($userId) { $filesystem = new \League\Flysystem\Filesystem($rdfAdapter); $filesystem->addPlugin(new \Pdsinterop\Rdf\Flysystem\Plugin\AsMime($formats)); - + $plugin = new \Pdsinterop\Rdf\Flysystem\Plugin\ReadRdf($graph); $filesystem->addPlugin($plugin); @@ -93,12 +93,12 @@ private function generateDefaultAcl($userId) { # The profile is readable by the public <#public> - a acl:Authorization; - acl:agentClass foaf:Agent; + a acl:Authorization; + acl:agentClass foaf:Agent; acl:accessTo <./>; acl:default <./>; acl:mode acl:Read. - + # The owner has full access to every resource in their pod. # Other agents have no access rights, # unless specifically authorized in other .acl resources. @@ -139,18 +139,18 @@ private function getStorageUrl($userId) { * @NoCSRFRequired */ public function handleRequest($userId, $path) { - $this->userId = $userId; - + $this->userId = $userId; + $this->rawRequest = \Laminas\Diactoros\ServerRequestFactory::fromGlobals($_SERVER, $_GET, $_POST, $_COOKIE, $_FILES); $this->response = new \Laminas\Diactoros\Response(); $this->filesystem = $this->getFileSystem($userId); - $this->resourceServer = new ResourceServer($this->filesystem, $this->response); + $this->resourceServer = new ResourceServer($this->filesystem, $this->response); $this->WAC = new WAC($this->filesystem); $request = $this->rawRequest; - $baseUrl = $this->getProfileUrl($userId); + $baseUrl = $this->getProfileUrl($userId); $this->resourceServer->setBaseUrl($baseUrl); $this->WAC->setBaseUrl($baseUrl); $notifications = new SolidNotifications(); @@ -175,20 +175,20 @@ public function handleRequest($userId, $path) { return $this->respond($response); } - $response = $this->resourceServer->respondToRequest($request); + $response = $this->resourceServer->respondToRequest($request); $response = $this->WAC->addWACHeaders($request, $response, $webId); return $this->respond($response); } - + /** * @PublicPage * @NoAdminRequired * @NoCSRFRequired */ - public function handleGet($userId, $path) { + public function handleGet($userId, $path) { return $this->handleRequest($userId, $path); } - + /** * @PublicPage * @NoAdminRequired @@ -205,7 +205,7 @@ public function handlePost($userId, $path) { public function handlePut() { // $userId, $path) { // FIXME: Adding the correct variables in the function name will make nextcloud // throw an error about accessing put twice, so we will find out the userId and path from $_SERVER instead; - + // because we got here, the request uri should look like: // /index.php/apps/solid/@{userId}/storage{path} $pathInfo = explode("@", $_SERVER['REQUEST_URI']); @@ -213,7 +213,7 @@ public function handlePut() { // $userId, $path) { $userId = $pathInfo[0]; $path = $pathInfo[1]; $path = preg_replace("/^profile/", "", $path); - + return $this->handleRequest($userId, $path); } /** @@ -316,9 +316,9 @@ private function generateTurtleProfile($userId) { @prefix inbox: <>. @prefix sp: . @prefix ser: <>. - + pro:card a foaf:PersonalProfileDocument; foaf:maker :me; foaf:primaryTopic :me. - + :me a schem:Person, foaf:Person; ldp:inbox inbox:; diff --git a/solid/lib/Controller/StorageController.php b/solid/lib/Controller/StorageController.php index 004ac82..d7591d2 100644 --- a/solid/lib/Controller/StorageController.php +++ b/solid/lib/Controller/StorageController.php @@ -72,7 +72,7 @@ private function getFileSystem() { $filesystem = new \League\Flysystem\Filesystem($rdfAdapter); $filesystem->addPlugin(new \Pdsinterop\Rdf\Flysystem\Plugin\AsMime($formats)); - + $plugin = new \Pdsinterop\Rdf\Flysystem\Plugin\ReadRdf($graph); $filesystem->addPlugin($plugin); @@ -127,11 +127,11 @@ private function generatePublicAppendAcl($userId) { @prefix foaf: . <#public> - a acl:Authorization; - acl:agentClass foaf:Agent; - acl:accessTo <./>; - acl:default <./>; - acl:mode + a acl:Authorization; + acl:agentClass foaf:Agent; + acl:accessTo <./>; + acl:default <./>; + acl:mode acl:Append. <#owner> @@ -295,7 +295,7 @@ public function handleRequest($userId, $path) { $this->WAC = new WAC($this->filesystem); $request = $this->rawRequest; - $baseUrl = $this->getStorageUrl($userId); + $baseUrl = $this->getStorageUrl($userId); $this->resourceServer->setBaseUrl($baseUrl); $this->WAC->setBaseUrl($baseUrl); @@ -323,20 +323,20 @@ public function handleRequest($userId, $path) { ->withStatus(403, "Access denied"); return $this->respond($response); } - $response = $this->resourceServer->respondToRequest($request); + $response = $this->resourceServer->respondToRequest($request); $response = $this->WAC->addWACHeaders($request, $response, $webId); return $this->respond($response); } - + /** * @PublicPage * @NoAdminRequired * @NoCSRFRequired */ - public function handleGet($userId, $path) { + public function handleGet($userId, $path) { return $this->handleRequest($userId, $path); } - + /** * @PublicPage * @NoAdminRequired @@ -353,7 +353,7 @@ public function handlePost($userId, $path) { public function handlePut() { // $userId, $path) { // FIXME: Adding the correct variables in the function name will make nextcloud // throw an error about accessing put twice, so we will find out the userId and path from $_SERVER instead; - + // because we got here, the request uri should look like: // /index.php/apps/solid/@{userId}/storage{path} $pathInfo = explode("@", $_SERVER['REQUEST_URI']); @@ -361,7 +361,7 @@ public function handlePut() { // $userId, $path) { $userId = $pathInfo[0]; $path = $pathInfo[1]; $path = preg_replace("/^storage/", "", $path); - + return $this->handleRequest($userId, $path); } /** @@ -406,7 +406,7 @@ private function respond($response) { // $result->addHeader('Access-Control-Allow-Credentials', 'true'); // $result->addHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization'); // $result->addHeader('Access-Control-Allow-Origin', $origin); - + $result->setStatus($statusCode); return $result; } From 3fe635a0601c1dded827ee9014d48d0eba9d9dff Mon Sep 17 00:00:00 2001 From: Noel De Martin Date: Fri, 28 Apr 2023 09:40:44 +0200 Subject: [PATCH 2/2] Use request scheme in server url --- solid/lib/Controller/CalendarController.php | 6 +++++- solid/lib/Controller/ContactsController.php | 6 +++++- solid/lib/Controller/ProfileController.php | 6 +++++- solid/lib/Controller/StorageController.php | 6 +++++- 4 files changed, 20 insertions(+), 4 deletions(-) diff --git a/solid/lib/Controller/CalendarController.php b/solid/lib/Controller/CalendarController.php index 6833fc9..3c4177f 100644 --- a/solid/lib/Controller/CalendarController.php +++ b/solid/lib/Controller/CalendarController.php @@ -61,7 +61,11 @@ private function getFileSystem($userId) { // Create Formats objects $formats = new \Pdsinterop\Rdf\Formats(); - $serverUri = "https://" . $this->rawRequest->getServerParams()["SERVER_NAME"] . $this->rawRequest->getServerParams()["REQUEST_URI"]; // FIXME: doublecheck that this is the correct url; + $serverParams = $this->rawRequest->getServerParams(); + $scheme = $serverParams['REQUEST_SCHEME']; + $domain = $serverParams['SERVER_NAME']; + $path = $serverParams['REQUEST_URI']; + $serverUri = "{$scheme}://{$domain}{$path}"; // FIXME: doublecheck that this is the correct url; // Create the RDF Adapter $rdfAdapter = new \Pdsinterop\Rdf\Flysystem\Adapter\Rdf( diff --git a/solid/lib/Controller/ContactsController.php b/solid/lib/Controller/ContactsController.php index d3848b2..d0beebc 100644 --- a/solid/lib/Controller/ContactsController.php +++ b/solid/lib/Controller/ContactsController.php @@ -62,7 +62,11 @@ private function getFileSystem($userId) { // Create Formats objects $formats = new \Pdsinterop\Rdf\Formats(); - $serverUri = "https://" . $this->rawRequest->getServerParams()["SERVER_NAME"] . $this->rawRequest->getServerParams()["REQUEST_URI"]; // FIXME: doublecheck that this is the correct url; + $serverParams = $this->rawRequest->getServerParams(); + $scheme = $serverParams['REQUEST_SCHEME']; + $domain = $serverParams['SERVER_NAME']; + $path = $serverParams['REQUEST_URI']; + $serverUri = "{$scheme}://{$domain}{$path}"; // FIXME: doublecheck that this is the correct url; // Create the RDF Adapter $rdfAdapter = new \Pdsinterop\Rdf\Flysystem\Adapter\Rdf( diff --git a/solid/lib/Controller/ProfileController.php b/solid/lib/Controller/ProfileController.php index 323b523..d365adf 100644 --- a/solid/lib/Controller/ProfileController.php +++ b/solid/lib/Controller/ProfileController.php @@ -65,7 +65,11 @@ private function getFileSystem($userId) { // Create Formats objects $formats = new \Pdsinterop\Rdf\Formats(); - $serverUri = "https://" . $this->rawRequest->getServerParams()["SERVER_NAME"] . $this->rawRequest->getServerParams()["REQUEST_URI"]; // FIXME: doublecheck that this is the correct url; + $serverParams = $this->rawRequest->getServerParams(); + $scheme = $serverParams['REQUEST_SCHEME']; + $domain = $serverParams['SERVER_NAME']; + $path = $serverParams['REQUEST_URI']; + $serverUri = "{$scheme}://{$domain}{$path}"; // FIXME: doublecheck that this is the correct url; // Create the RDF Adapter $rdfAdapter = new \Pdsinterop\Rdf\Flysystem\Adapter\Rdf( diff --git a/solid/lib/Controller/StorageController.php b/solid/lib/Controller/StorageController.php index d7591d2..87d356d 100644 --- a/solid/lib/Controller/StorageController.php +++ b/solid/lib/Controller/StorageController.php @@ -59,7 +59,11 @@ private function getFileSystem() { // Create Formats objects $formats = new \Pdsinterop\Rdf\Formats(); - $serverUri = "https://" . $this->rawRequest->getServerParams()["SERVER_NAME"] . $this->rawRequest->getServerParams()["REQUEST_URI"]; // FIXME: doublecheck that this is the correct url; + $serverParams = $this->rawRequest->getServerParams(); + $scheme = $serverParams['REQUEST_SCHEME']; + $domain = $serverParams['SERVER_NAME']; + $path = $serverParams['REQUEST_URI']; + $serverUri = "{$scheme}://{$domain}{$path}"; // FIXME: doublecheck that this is the correct url; // Create the RDF Adapter $rdfAdapter = new \Pdsinterop\Rdf\Flysystem\Adapter\Rdf(