ILIAS  trunk Revision v13.0_alpha-62-g3b72d83f2e5
EmbeddedApplication.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
31
36{
40 private const WOPI_SRC = 'WOPISrc';
41 private const SUB_THM = 'thm';
42 private const SUB_DCHAT = 'dchat';
43 private const SUB_EMBED = 'embed';
44 private int $ttl = 3600 * 8;
45 private string $token;
46 private array $substitutions = [
47 self::SUB_THM => 1,
48 self::SUB_DCHAT => 0,
49 self::SUB_EMBED => false
50 ];
52
60 public function __construct(
61 protected ResourceIdentification $identification,
62 protected ?Action $action,
63 protected ResourceStakeholder $stakeholder,
64 protected URI $back_target,
65 protected bool $inline = false,
66 ?string $ui_language = null,
67 ?bool $user_can_write = null
68 ) {
69 global $DIC;
71 $data_signer = $DIC['file_delivery.data_signer'];
72 $this->ilias_base_url = new URI(ILIAS_HTTP_PATH);
73 $editable = in_array(
74 $this->action?->getName(),
75 [ActionTarget::EDIT->value, ActionTarget::EMBED_EDIT->value, ActionTarget::EDIT_NEW->value],
76 true
77 );
78
79 $payload = [
80 'resource_id' => $this->identification->serialize(),
81 'user_id' => $DIC->user()->getId(),
82 'stakeholder' => $this->stakeholder::class,
83 'editable' => $editable,
84 'user_can_write' => $user_can_write ?? $editable
85 ];
86
87 $ui_language ??= 'en_US';
88 $this->substitutions['lang'] = $ui_language;
89 $this->substitutions['rs'] = $ui_language;
90 $this->substitutions['ui'] = $ui_language;
91 $this->substitutions[self::SUB_EMBED] = !$editable;
92 $this->token = $data_signer->sign($payload, 'wopi', new \DateTimeImmutable("now + $this->ttl seconds"));
93 }
94
95 public function getToken(): string
96 {
97 return $this->token;
98 }
99
100 public function getTTL(): int
101 {
102 return $this->ttl;
103 }
104
105 public function getBackTarget(): URI
106 {
107 return $this->back_target;
108 }
109
110 public function isInline(): bool
111 {
112 return $this->inline;
113 }
114
115 public function getActionLauncherURL(): ?URI
116 {
117 // launcher URLs stored before they have been checked while crawling
118 if ($this->action === null || !Crawler::isWebUrl($this->action->getLauncherUrl())) {
119 return null;
120 }
121
122 $appendices = $this->getAppendices();
123
124 $url = rtrim((string) $this->action->getLauncherUrl(), '/?#')
125 . '?'
126 . self::WOPI_SRC
127 . '='
128 . urlencode(
129 rtrim((string) $this->ilias_base_url, '/')
132 . '/'
133 . $this->identification->serialize()
134 );
135
136 if ($appendices !== []) {
137 $url .= '&' . implode('&', $appendices);
138 }
139
140 return new URI($url);
141 }
142
146 protected function getAppendices(): array
147 {
148 // appendix sanitizer
149 $appendix = $this->action->getUrlAppendix();
150 $appendices = [];
151 try {
152 if ($appendix !== null) {
153 preg_match_all('/([^<]*)=([^>&]*)/m', $appendix, $appendices, PREG_SET_ORDER, 0);
154
155 $appendices = array_filter($appendices, static fn(array $appendix): bool => isset($appendix[1], $appendix[2]));
156
157 // we set the wopisrc ourselves
158 $appendices = array_filter($appendices, static fn(array $appendix): bool => strtolower((string) $appendix[1]) !== 'wopisrc');
159
160 // try substitutions
161 $appendices = array_map(function (array $appendix): array {
162 $key = strtolower($appendix[1]);
163 if (isset($this->substitutions[$key])) {
164 $appendix[2] = (string) $this->substitutions[$key];
165 }
166 return $appendix;
167 }, $appendices);
168
169 // we remove all those placeholders
170 $appendices = array_filter($appendices, static fn(array $appendix): bool => $appendix[0] !== $appendix[1] . '=' . $appendix[2]);
171
172 $here = 1;
173
174 $appendices = array_map(static fn(array $appendix): string => $appendix[1] . '=' . $appendix[2], $appendices);
175 }
176 } catch (\Throwable $t) {
177 return [];
178 }
179
180 return $appendices;
181 }
182}
The scope of this class is split ilias-conform URI's into components.
Definition: URI.php:35
static isWebUrl(URI $url)
Definition: Crawler.php:62
Handles the requests of a WOPI client, see https://learn.microsoft.com/en-us/microsoft-365/cloud-stor...
__construct()
Constructor setup ILIAS global object @access public.
Definition: class.ilias.php:76
ActionTarget
Officialy supported action targets, see https://learn.microsoft.com/en-us/openspecs/office_protocols/...
global $DIC
Definition: shib_login.php:26