19 declare(strict_types=1);
43 private \ILIAS\HTTP\Services
$http;
44 private \ILIAS\ResourceStorage\Services
$irss;
55 $this->
http = $DIC->http();
56 $this->data_signer = $DIC[
'file_delivery.data_signer'];
57 $this->irss = $DIC->resourceStorage();
58 $this->saving_interval = (
int) $DIC->settings()->get(
'saving_interval');
63 $auth = $this->
http->request()->getHeader(self::HEADER_AUTHORIZATION)[0] ??
'';
65 $bearer = explode(
' ', $auth);
66 if ($auth !==
'' && ($bearer[0] ??
'') !== self::HEADER_AUTHORIZATION_BEARER) {
67 throw new \InvalidArgumentException();
69 $bearer_token = $bearer[1] ??
'';
70 if ($auth ===
'' && $bearer_token ===
'') {
72 $bearer_token = $this->
http->request()->getQueryParams()[
'access_token'] ??
'';
74 if ($bearer_token ===
'') {
75 throw new \InvalidArgumentException(
'No access token provided');
77 if (($token_data = $this->data_signer->verify($bearer_token,
'wopi')) === null) {
78 throw new \InvalidArgumentException(
'Token verification failed');
81 $this->token_user_id = (
int) ($token_data[
'user_id'] ?? 0);
82 $this->token_resource_id = (string) ($token_data[
'resource_id'] ??
'');
83 $this->editable = (bool) ($token_data[
'editable'] ??
'');
84 $stakeholder = $token_data[
'stakeholder'] ?? null;
85 if ($stakeholder !== null) {
88 $this->stakeholder->init($stakeholder, $this->token_user_id);
103 $uri = $this->
http->request()->getUri()->getPath();
104 $request = substr($uri, strpos($uri, self::WOPI_BASE_URL) + strlen(self::WOPI_BASE_URL));
105 $request = explode(
'/', $request);
106 $method = $this->
http->request()->getMethod();
108 $resource_id = $request[1];
109 $action = $request[2] ??
'';
112 if ($this->token_resource_id !== $resource_id) {
113 $this->
http->close();
116 $resource_id = $this->irss->manage()->find($resource_id);
118 $this->
http->close();
120 $resource = $this->irss->manage()->getResource($resource_id);
121 $current_revision = $resource->getCurrentRevisionIncludingDraft();
123 $method_override = $this->
http->request()->getHeader(self::HEADER_X_WOPI_OVERRIDE)[0] ?? null;
124 $is_file_convertion = (bool) ($this->
http->request()->getHeader(
125 self::HEADER_X_WOPI_FILE_CONVERSION
129 switch ($method_override ?? $method) {
136 $this->token_user_id,
139 $this->
http->saveResponse(
140 $this->
http->response()->withBody(
148 $stream = $this->irss->consume()->stream($resource_id)->setRevisionNumber(
149 $current_revision->getVersionNumber()
151 $this->
http->saveResponse(
152 $this->
http->response()->withBody($stream)
159 if (!$is_file_convertion) {
160 throw new \InvalidArgumentException();
167 $body_stream = $this->
http->request()->getBody();
168 $body = $body_stream->getContents();
173 if ($this->saving_interval > 0) {
174 $latest_revision = $resource->getCurrentRevision();
175 $creation_time = $latest_revision->getInformation()->getCreationDate()->getTimestamp();
176 $current_time = time();
177 $time_diff = $current_time - $creation_time;
178 if ($time_diff > $this->saving_interval) {
179 $this->irss->manage()->publish($resource_id);
183 $new_revision = $this->irss->manage()->appendNewRevisionFromStream(
187 $current_revision->getTitle(),
196 $this->
http->saveResponse(
197 $this->
http->response()->withBody(
210 $this->
http->saveResponse(
211 $this->
http->response()->withBody(
219 $lock = $this->
http->request()->getHeader(self::HEADER_X_WOPI_LOCK)[0] ?? null;
220 $this->
http->saveResponse(
221 $this->
http->response()->withBody(
229 $message = $t->getMessage();
232 static function ($trace):
string {
233 return $trace[
'file'] .
':' . $trace[
'line'];
238 $message .=
"\n" . implode(
"\n", $trace);
240 $this->
http->saveResponse(
241 $this->
http->response()
244 ->withHeader(
'X-WOPI-ServerError', $t->getMessage())
247 $this->
http->sendResponse();
248 $this->
http->close();
const HEADER_X_REQUEST_ID
const HEADER_AUTHORIZATION_BEARER
Interface Observer Contains several chained tasks and infos about them.
ResourceStakeholder $stakeholder
ILIAS HTTP Services $http
static http()
Fetches the global http state from ILIAS.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
const HEADER_AUTHORIZATION
Class ResourceIdentification.
static ofString(string $string)
Creates a new stream with an initial value.
const HEADER_X_WOPI_OVERRIDE
Interface ResourceStakeholder.
ILIAS ResourceStorage Services $irss
const HEADER_X_WOPI_FILE_CONVERSION
string $token_resource_id