19declare(strict_types=1);
64 private \ILIAS\ResourceStorage\Services
$irss;
75 $this->
http = $DIC->http();
76 $this->data_signer =
$DIC[
'file_delivery.data_signer'];
77 $this->irss =
$DIC->resourceStorage();
78 $this->saving_interval = (
int)
$DIC->settings()->get(
'saving_interval');
83 $auth = $this->
http->request()->getHeader(self::HEADER_AUTHORIZATION)[0] ??
'';
85 $bearer = explode(
' ', $auth);
86 if ($auth !==
'' && ($bearer[0] ??
'') !== self::HEADER_AUTHORIZATION_BEARER) {
87 throw new \InvalidArgumentException();
89 $bearer_token = $bearer[1] ??
'';
90 if ($auth ===
'' && $bearer_token ===
'') {
92 $bearer_token = $this->
http->request()->getQueryParams()[
'access_token'] ??
'';
94 if ($bearer_token ===
'') {
95 throw new \InvalidArgumentException(
'No access token provided');
97 if (($token_data = $this->data_signer->verify($bearer_token,
'wopi')) ===
null) {
98 throw new \InvalidArgumentException(
'Token verification failed');
101 $this->token_user_id = (
int) ($token_data[
'user_id'] ?? 0);
102 $this->token_resource_id = (string) ($token_data[
'resource_id'] ??
'');
103 $this->editable = (bool) ($token_data[
'editable'] ??
'');
108 $this->stakeholder->init(
$stakeholder, $this->token_user_id);
109 }
catch (\Throwable) {
123 $uri = $this->
http->request()->getUri()->getPath();
124 $request = substr($uri, strpos($uri, self::WOPI_BASE_URL) + strlen(self::WOPI_BASE_URL));
125 $request = explode(
'/', $request);
126 $method = $this->
http->request()->getMethod();
128 $resource_id = $request[1];
129 $action = $request[2] ??
'';
132 if ($this->token_resource_id !== $resource_id) {
133 $this->
http->close();
136 $resource_id = $this->irss->manage()->find($resource_id);
138 $this->
http->close();
140 $resource = $this->irss->manage()->getResource($resource_id);
141 $current_revision = $this->editable ? $resource->getCurrentRevisionIncludingDraft() : $resource->getCurrentRevision();
143 $method_override = $this->
http->request()->getHeader(self::HEADER_X_WOPI_OVERRIDE)[0] ??
null;
144 $is_file_convertion = (bool) ($this->
http->request()->getHeader(
145 self::HEADER_X_WOPI_FILE_CONVERSION
149 switch ($method_override ?? $method) {
156 $this->token_user_id,
159 $this->
http->saveResponse(
160 $this->
http->response()->withBody(
168 $stream = $this->irss->consume()->stream($resource_id)->setRevisionNumber(
169 $current_revision->getVersionNumber()
171 $this->
http->saveResponse(
172 $this->
http->response()->withBody($stream)
179 if (!$is_file_convertion) {
180 throw new \InvalidArgumentException();
187 $body_stream = $this->
http->request()->getBody();
188 $body = $body_stream->getContents();
193 if ($this->saving_interval > 0) {
194 $latest_revision = $resource->getCurrentRevision();
195 $creation_time = $latest_revision->getInformation()->getCreationDate()->getTimestamp();
196 $current_time = time();
197 $time_diff = $current_time - $creation_time;
198 if ($time_diff > $this->saving_interval) {
199 $this->irss->manage()->publish($resource_id);
203 $new_revision = $this->irss->manage()->appendNewRevisionFromStream(
207 $current_revision->getTitle(),
216 $this->
http->saveResponse(
217 $this->
http->response()->withBody(
230 $this->
http->saveResponse(
231 $this->
http->response()->withBody(
239 $lock = $this->
http->request()->getHeader(self::HEADER_X_WOPI_LOCK)[0] ??
null;
240 $this->
http->saveResponse(
241 $this->
http->response()->withBody(
248 }
catch (\Throwable $t) {
252 static fn(array $trace):
string => $trace[
'file'] .
':' . $trace[
'line'],
256 $message .=
"\n" . implode(
"\n", $trace);
258 $this->
http->saveResponse(
259 $this->
http->response()
262 ->withHeader(
'X-WOPI-ServerError', $t->getMessage())
265 $this->
http->sendResponse();
266 $this->
http->close();
Stream factory which enables the user to create streams without the knowledge of the concrete class.
static ofString(string $string)
Creates a new stream with an initial value.
Class ResourceIdentification.
const HEADER_X_WOPI_OVERRIDE
ResourceStakeholder $stakeholder
const HEADER_AUTHORIZATION_BEARER
ILIAS ResourceStorage Services $irss
const HEADER_X_WOPI_FILE_CONVERSION
const HEADER_AUTHORIZATION
string $token_resource_id
Interface ResourceStakeholder.
static http()
Fetches the global http state from ILIAS.