ILIAS  trunk Revision v13.0_alpha-62-g3b72d83f2e5
Services.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21namespace ILIAS\FileDelivery;
22
32
37{
38 public const DELIVERY_ENDPOINT = '/deliver.php/';
39
41 private array $base_uris = [];
42
43 public function __construct(
44 private StreamDelivery $delivery,
45 private LegacyDelivery $legacy_delivery,
46 private DataSigning $data_signer,
47 private GlobalHttpState $http,
48 private IsolationConfig $isolation = new IsolationConfig(false, null, null),
49 private ?HttpPathProvider $http_path = null
50 ) {
51 }
52
53 public function delivery(): StreamDelivery
54 {
55 return $this->delivery;
56 }
57
58 public function legacyDelivery(): LegacyDelivery
59 {
60 return $this->legacy_delivery;
61 }
62
63 public function buildTokenURL(
64 FileStream $stream,
65 string $filename,
66 Disposition $disposition,
67 int $user_id,
68 int $valid_for_at_least_hours
69 ): URI {
70 $token = $this->data_signer->getSignedStreamToken(
71 $stream,
73 $disposition,
75 $this->validUntil($valid_for_at_least_hours)
76 );
77
78 return $this->buildURL($token, $filename, true);
79 }
80
85 FileStream $stream,
86 string $filename,
87 Disposition $disposition,
88 int $user_id,
89 int $valid_for_at_least_hours
90 ): URI {
91 $token = $this->data_signer->getSignedStreamTokenWithoutIsolation(
92 $stream,
94 $disposition,
96 $this->validUntil($valid_for_at_least_hours)
97 );
98
99 return $this->buildURL($token, $filename, false);
100 }
101
102 private function validUntil(int $valid_for_at_least_hours): \DateTimeImmutable
103 {
104 // a new DateTimeImmutable which is set to the end of now + $valid_for_at_least_hours hours
105 $valid_for_at_least_hours++;
106 return new \DateTimeImmutable(
107 (new \DateTimeImmutable("now +$valid_for_at_least_hours hours"))->format('Y-m-d H:00')
108 );
109 }
110
111 private function buildURL(string $token, string $filename, bool $isolated): URI
112 {
113 // maybe add suffix
114 $suffix = '';
115 // try to determine suffix from filename
116 $pathinfo = pathinfo($filename);
117 if (isset($pathinfo['extension']) && $pathinfo['extension'] !== '') {
118 $suffix = '.' . $pathinfo['extension'];
119 }
120
121 return new URI(
122 $this->getBaseURI($isolated) . self::DELIVERY_ENDPOINT . $token . $suffix
123 );
124 }
125
126 protected function getBaseURI(bool $isolated = true): string
127 {
128 return $this->base_uris[$isolated ? 'content' : 'ilias'] ??= $this->buildBaseURI($isolated);
129 }
130
131 private function buildBaseURI(bool $isolated): string
132 {
133 $request_uri = $this->http->request()->getUri();
134 $http_path = $this->http_path?->getHttpPath();
135
136 // CLI contexts such as cron jobs have no request to derive the URL from,
137 // see https://mantis.ilias.de/view.php?id=47807
138 $is_without_request = $request_uri->getHost() === '';
139
140 // the path is the one ILIAS is reached under, which dirname() of the request
141 // path does not give on every page, see https://mantis.ilias.de/view.php?id=48124
142 $path = match (true) {
143 $http_path !== null => rtrim('/' . ($http_path->getPath() ?? ''), '/'),
144 $is_without_request => '',
145 default => rtrim(dirname($request_uri->getPath()), '/'),
146 };
147
148 if ($isolated
149 && $this->isolation->isActivated()
150 && ($content_domain = $this->isolation->getContentDomain()) !== null
151 ) {
152 return rtrim($content_domain, '/') . $path;
153 }
154
155 if ($is_without_request) {
156 return $http_path?->getBaseURI() ?? '';
157 }
158
159 // the host stays with the request, since allowed_hosts permits more than one
160 return rtrim(
161 $request_uri->getScheme()
162 . '://' . $request_uri->getHost()
163 . ($request_uri->getPort() ? ':' . $request_uri->getPort() : '')
164 . $path,
165 '/'
166 );
167 }
168}
$filename
Definition: buildRTE.php:78
The scope of this class is split ilias-conform URI's into components.
Definition: URI.php:35
Read-only configuration of the IRSS User Content Isolation feature.
buildTokenURL(FileStream $stream, string $filename, Disposition $disposition, int $user_id, int $valid_for_at_least_hours)
Definition: Services.php:63
buildTokenURLWithoutIsolation(FileStream $stream, string $filename, Disposition $disposition, int $user_id, int $valid_for_at_least_hours)
Definition: Services.php:84
buildURL(string $token, string $filename, bool $isolated)
Definition: Services.php:111
__construct(private StreamDelivery $delivery, private LegacyDelivery $legacy_delivery, private DataSigning $data_signer, private GlobalHttpState $http, private IsolationConfig $isolation=new IsolationConfig(false, null, null), private ?HttpPathProvider $http_path=null)
Definition: Services.php:43
getBaseURI(bool $isolated=true)
Definition: Services.php:126
buildBaseURI(bool $isolated)
Definition: Services.php:131
validUntil(int $valid_for_at_least_hours)
Definition: Services.php:102
The base interface for all filesystem streams.
Definition: FileStream.php:32
Interface GlobalHttpState.
Tells under which URL this ILIAS installation is reached, e.g.
static http()
Fetches the global http state from ILIAS.
$token
Definition: xapitoken.php:69