ILIAS  trunk Revision v12.0_alpha-1540-g00f839d5fa1
UriBuilder.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21namespace ILIAS\WebDAV\Mount;
22
23use Psr\Http\Message\ServerRequestInterface;
24use Psr\Http\Message\UriInterface;
26
31{
35 private const array SCHEMAS = [
36 'default' => 'http',
37 'konqueror' => 'webdav',
38 'nautilus' => 'dav',
39 ];
40
41 private ?UriInterface $uri = null;
42 private ?string $host = null;
43 private ?string $base_path = null;
44
45 public function __construct(
46 private ServerRequestInterface $request,
47 private Config $config
48 ) {
49 }
50
51 private function uri(): UriInterface
52 {
53 return $this->uri ??= $this->request->getUri();
54 }
55
56 private function host(): string
57 {
58 if ($this->host !== null) {
59 return $this->host;
60 }
61 $host = $this->uri()->getHost();
62 $port = $this->uri()->getPort();
63 if ($port !== null && !in_array($port, [80, 443], true)) {
64 $host .= ':' . $port;
65 }
66 return $this->host = $host;
67 }
68
69 private function basePath(): string
70 {
71 return $this->base_path ??= $this->resolveBasePath($this->uri()->getPath());
72 }
73
74 private function resolveBasePath(string $original_path): string
75 {
76 $endpoint = $this->config->getEndpoint();
77 $segments = explode('/', $original_path);
78
79 if (in_array($endpoint, $segments, true)) {
80 $kept = [];
81 foreach ($segments as $segment) {
82 $kept[] = $segment;
83 if ($segment === $endpoint) {
84 break;
85 }
86 }
87 return implode('/', $kept);
88 }
89
90 $kept = array_slice($segments, 0, -1);
91 return implode('/', $kept) . '/' . $endpoint;
92 }
93
94 public function getWebDavDefaultUri(int $ref_id): string
95 {
96 return $this->buildSchemedUri('default', $this->getWebDavPathToRef($ref_id));
97 }
98
99 public function getWebDavKonquerorUri(int $ref_id): string
100 {
101 return $this->buildSchemedUri('konqueror', $this->getWebDavPathToRef($ref_id));
102 }
103
104 public function getWebDavNautilusUri(int $ref_id): string
105 {
106 return $this->buildSchemedUri('nautilus', $this->getWebDavPathToRef($ref_id));
107 }
108
109 public function getUriToMountInstructionModalByRef(int $ref_id): string
110 {
111 return $this->getWebDavPathToRef($ref_id) . '?' . $this->config->getMountInstructionsQuery();
112 }
113
114 public function getUriToMountInstructionModalByLanguage(string $language): string
115 {
116 return $this->getWebDavBasePath() . '/' . $language . '?' . $this->config->getMountInstructionsQuery();
117 }
118
119 private function getWebDavPathToRef(int $ref_id): string
120 {
121 return $this->getWebDavBasePath() . '/ref_' . $ref_id;
122 }
123
124 private function getWebDavBasePath(): string
125 {
126 if ($this->config->prependClientName() && $this->config->getClientId() !== '') {
127 return $this->basePath() . '/' . $this->config->getClientId();
128 }
129 return $this->basePath();
130 }
131
132 private function buildSchemedUri(string $placeholder, string $path): string
133 {
134 $scheme = self::SCHEMAS[$placeholder];
135 if ($this->uri()->getScheme() === 'https') {
136 $scheme .= 's';
137 }
138 return $scheme . '://' . $this->host() . $path;
139 }
140}
__construct(private ServerRequestInterface $request, private Config $config)
Definition: UriBuilder.php:45
buildSchemedUri(string $placeholder, string $path)
Definition: UriBuilder.php:132
getUriToMountInstructionModalByLanguage(string $language)
Definition: UriBuilder.php:114
resolveBasePath(string $original_path)
Definition: UriBuilder.php:74
getWebDavDefaultUri(int $ref_id)
Definition: UriBuilder.php:94
getUriToMountInstructionModalByRef(int $ref_id)
Definition: UriBuilder.php:109
getWebDavKonquerorUri(int $ref_id)
Definition: UriBuilder.php:99
$ref_id
Definition: ltiauth.php:66
$path
Definition: ltiservices.php:30