Skip to content

Commit 037a0bc

Browse files
committed
centralize the use of $_SERVER to easier refactor later
1 parent a745b74 commit 037a0bc

File tree

4 files changed

+22
-9
lines changed

4 files changed

+22
-9
lines changed

lib/Routes/SolidStorage.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use Pdsinterop\PhpSolid\StorageServer;
55
use Pdsinterop\PhpSolid\ClientRegistration;
66
use Pdsinterop\PhpSolid\SolidNotifications;
7+
use Pdsinterop\PhpSolid\Util
78
use Pdsinterop\Solid\Auth\WAC;
89
use Pdsinterop\Solid\Resources\Server as ResourceServer;
910
use Laminas\Diactoros\ServerRequestFactory;
@@ -23,8 +24,7 @@ public static function respondToStorage() {
2324

2425
$wac = new WAC($filesystem);
2526

26-
$baseUrl = $_SERVER['REQUEST_SCHEME'] . "://" . $_SERVER['SERVER_NAME']; // FIXME: get this from the rawRequest instead?
27-
27+
$baseUrl = Util::getServerBaseUrl();
2828
$resourceServer->setBaseUrl($baseUrl);
2929
$wac->setBaseUrl($baseUrl);
3030

lib/Routes/SolidUserProfile.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
namespace Pdsinterop\PhpSolid\Routes;
33

44
use Pdsinterop\PhpSolid\User;
5+
use Pdsinterop\PhpSolid\Util;
56

67
class SolidUserProfile {
78
public static function respondToProfile() {
8-
$serverName = $_SERVER['SERVER_NAME'];
9+
$serverName = Util::getServerName();
910
[$idPart, $rest] = explode(".", $serverName, 2);
1011
$userId = preg_replace("/^id-/", "", $idPart);
1112

lib/StorageServer.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
use Pdsinterop\PhpSolid\Server;
55
use Pdsinterop\PhpSolid\User;
6+
use Pdsinterop\PhpSolid\Util;
67

78
class StorageServer extends Server {
89
public static function getFileSystem() {
@@ -17,11 +18,7 @@ public static function getFileSystem() {
1718
$graph = new \EasyRdf\Graph();
1819
// Create Formats objects
1920
$formats = new \Pdsinterop\Rdf\Formats();
20-
21-
$scheme = $_SERVER['REQUEST_SCHEME'];
22-
$domain = $_SERVER['SERVER_NAME'];
23-
$path = $_SERVER['REQUEST_URI'];
24-
$serverUri = "{$scheme}://{$domain}{$path}"; // FIXME: doublecheck that this is the correct url;
21+
$serverUri = Util::getServerUri();
2522

2623
// Create the RDF Adapter
2724
$rdfAdapter = new \Pdsinterop\Rdf\Flysystem\Adapter\Rdf($adapter, $graph, $formats, $serverUri);
@@ -62,7 +59,7 @@ public static function getWebId($rawRequest) {
6259
}
6360

6461
private static function getStorageId() {
65-
$serverName = $_SERVER['SERVER_NAME'];
62+
$serverName = Util::getServerName();
6663
$idParts = explode(".", $serverName, 2);
6764
$storageId = preg_replace("/^storage-/", "", $idParts[0]);
6865
return $storageId;

lib/Util.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,19 @@ public static function base64_url_encode($text) {
99
public static function base64_url_decode($text) {
1010
return base64_decode(str_replace(['-', '_', ''], ['+', '/', '='], $text));
1111
}
12+
public static function getServerName() {
13+
// FIXME: Depending on the setup, SERVER_NAME might not be the domain of the server.
14+
return $_SERVER['SERVER_NAME'];
15+
}
16+
public static function getServerUri() {
17+
$scheme = $_SERVER['REQUEST_SCHEME'];
18+
$domain = $_SERVER['SERVER_NAME'];
19+
$path = $_SERVER['REQUEST_URI'];
20+
return "{$scheme}://{$domain}{$path}";
21+
}
22+
public static function getServerBaseUrl() {
23+
$scheme = $_SERVER['REQUEST_SCHEME'];
24+
$domain = $_SERVER['SERVER_NAME'];
25+
return "{$scheme}://{$domain}";
26+
}
1227
}

0 commit comments

Comments
 (0)