19 declare(strict_types=1);
52 private \ILIAS\ResourceStorage\Services
$irss;
66 $this->
http = $DIC->http();
67 $this->data_signer = $DIC[
'file_delivery.data_signer'];
68 $this->irss = $DIC->resourceStorage();
73 $auth = $this->
http->request()->getHeader(self::HEADER_AUTHORIZATION)[0] ??
'';
75 $bearer = explode(
' ', $auth);
76 if ($auth !==
'' && ($bearer[0] ??
'') !== self::HEADER_AUTHORIZATION_BEARER) {
77 throw new \InvalidArgumentException();
79 $bearer_token = $bearer[1] ??
'';
80 if ($auth ===
'' && $bearer_token ===
'') {
82 $bearer_token = $this->
http->request()->getQueryParams()[
'access_token'] ??
'';
84 if ($bearer_token ===
'') {
85 throw new \InvalidArgumentException(
'No access token provided');
87 if (($token_data = $this->data_signer->verify($bearer_token,
'wopi')) === null) {
88 throw new \InvalidArgumentException(
'Token verification failed');
91 $this->token_user_id = (
int) ($token_data[
'user_id'] ?? 0);
92 $this->token_resource_id = (string) ($token_data[
'resource_id'] ??
'');
93 $this->editable = (bool) ($token_data[
'editable'] ??
'');
96 $stakeholder = $token_data[
'stakeholder'] ?? WOPIUnknownStakeholder::class;
99 $this->stakeholder->init(
new $stakeholder(), $this->token_user_id);
113 $uri = $this->
http->request()->getUri()->getPath();
114 $request = substr($uri, strpos($uri, self::WOPI_BASE_URL) + strlen(self::WOPI_BASE_URL));
115 $request = explode(
'/', $request);
116 $method = $this->
http->request()->getMethod();
118 $resource_id = $request[1];
119 $action = $request[2] ??
'';
122 if ($this->token_resource_id !== $resource_id) {
123 $this->
http->close();
126 $resource_id = $this->irss->manage()->find($resource_id);
128 $this->
http->close();
130 $resource = $this->irss->manage()->getResource($resource_id);
131 if ($this->editable) {
132 $current_revision = $resource->getCurrentRevisionIncludingDraft();
134 $current_revision = $resource->getCurrentRevision();
137 $method_override = $this->
http->request()->getHeader(self::HEADER_X_WOPI_OVERRIDE)[0] ?? null;
138 $is_file_convertion = (bool) ($this->
http->request()->getHeader(
139 self::HEADER_X_WOPI_FILE_CONVERSION
143 switch ($method_override ?? $method) {
150 $this->token_user_id,
153 $this->
http->saveResponse(
154 $this->
http->response()->withBody(
162 $stream = $this->irss->consume()->stream($resource_id)->setRevisionNumber(
163 $current_revision->getVersionNumber()
165 $this->
http->saveResponse(
166 $this->
http->response()->withBody($stream)
173 if (!$is_file_convertion) {
174 throw new \InvalidArgumentException();
181 $body_stream = $this->
http->request()->getBody();
182 $body = $body_stream->getContents();
187 if ($this->saving_interval > 0) {
188 $latest_revision = $resource->getCurrentRevision();
189 $creation_time = $latest_revision->getInformation()->getCreationDate()->getTimestamp();
190 $current_time = time();
191 $time_diff = $current_time - $creation_time;
192 if ($time_diff > $this->saving_interval) {
193 $this->irss->manage()->publish($resource_id);
197 $new_revision = $this->irss->manage()->appendNewRevisionFromStream(
201 $current_revision->getTitle(),
210 $this->
http->saveResponse(
211 $this->
http->response()->withBody(
224 $this->
http->saveResponse(
225 $this->
http->response()->withBody(
233 $lock = $this->
http->request()->getHeader(self::HEADER_X_WOPI_LOCK)[0] ?? null;
234 $this->
http->saveResponse(
235 $this->
http->response()->withBody(
246 static fn(array $trace):
string => $trace[
'file'] .
':' . $trace[
'line'],
250 $message .=
"\n" . implode(
"\n", $trace);
252 $this->
http->saveResponse(
253 $this->
http->response()
256 ->withHeader(
'X-WOPI-ServerError', $t->getMessage())
259 $this->
http->sendResponse();
260 $this->
http->close();
ILIAS ResourceStorage Services $irss
const HEADER_AUTHORIZATION_BEARER
ResourceStakeholder $stakeholder
string $token_resource_id
const HEADER_AUTHORIZATION
static http()
Fetches the global http state from ILIAS.
Class ResourceIdentification.
const HEADER_X_REQUEST_ID
static ofString(string $string)
Creates a new stream with an initial value.
const HEADER_X_WOPI_OVERRIDE
const HEADER_X_WOPI_FILE_CONVERSION
Interface ResourceStakeholder.