ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
EmbeddedApplication.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
23 use ILIAS\Data\URI;
30 
35 {
39  private const WOPI_SRC = 'WOPISrc';
40  private const SUB_THM = 'thm';
41  private const SUB_DCHAT = 'dchat';
42  private const SUB_EMBED = 'embed';
43  private int $ttl = 3600;
44  private string $token;
45  private array $substitutions = [
46  self::SUB_THM => 1,
47  self::SUB_DCHAT => 0,
48  self::SUB_EMBED => false
49  ];
50 
51  public function __construct(
52  protected ResourceIdentification $identification,
53  protected Action $action,
54  protected ResourceStakeholder $stakeholder,
55  protected URI $back_target,
56  protected bool $inline = false,
57  protected ?bool $edit = null,
58  private URI $ilias_base_url = new URI(ILIAS_HTTP_PATH)
59  ) {
60  global $DIC;
62  $data_signer = $DIC['file_delivery.data_signer'];
63 
64  $editable = $edit ?? ($this->action === null
65  ? false
66  : $this->action->getName() === ActionTarget::EDIT->value);
67  $payload = [
68  'resource_id' => $this->identification->serialize(),
69  'user_id' => $DIC->user()->getId(),
70  'stakeholder' => $this->stakeholder::class,
71  'editable' => $editable
72  ];
73 
74  $this->substitutions[self::SUB_EMBED] = !$editable;
75  $this->token = $data_signer->sign($payload, 'wopi', new \DateTimeImmutable("now + $this->ttl seconds"));
76  }
77 
78  public function getToken(): string
79  {
80  return $this->token;
81  }
82 
83  public function getTTL(): int
84  {
85  return $this->ttl;
86  }
87 
88  public function getBackTarget(): URI
89  {
90  return $this->back_target;
91  }
92 
93  public function isInline(): bool
94  {
95  return $this->inline;
96  }
97 
98  public function getActionLauncherURL(): ?URI
99  {
100  if ($this->action === null) {
101  return null;
102  }
103 
104  $appendices = $this->getAppendices();
105 
106  $url = rtrim((string) $this->action->getLauncherUrl(), '/?#')
107  . '?'
108  . self::WOPI_SRC
109  . '='
110  . urlencode(
111  rtrim((string) $this->ilias_base_url, '/')
114  . '/'
115  . $this->identification->serialize()
116  );
117 
118  if ($appendices !== []) {
119  $url .= '&' . implode('&', $appendices);
120  }
121 
122  return new URI($url);
123  }
124 
128  protected function getAppendices(): array
129  {
130  // appendix sanitizer
131  $appendix = $this->action->getUrlAppendix();
132  $appendices = [];
133  try {
134  if ($appendix !== null) {
135  preg_match_all('/([^<]*)=([^>&]*)/m', $appendix, $appendices, PREG_SET_ORDER, 0);
136 
137  $appendices = array_filter($appendices, static fn($appendix): bool => isset($appendix[1], $appendix[2]));
138 
139  // we set the wopisrc ourselves
140  $appendices = array_filter($appendices, static fn($appendix): bool => strtolower((string) $appendix[1]) !== 'wopisrc');
141 
142  // try substitutions
143  $appendices = array_map(function ($appendix): array {
144  $key = strtolower((string) $appendix[1]);
145  if (isset($this->substitutions[$key])) {
146  $appendix[2] = (string) $this->substitutions[$key];
147  }
148  return $appendix;
149  }, $appendices);
150 
151  // we remove all those placeholders
152  $appendices = array_filter($appendices, static fn($appendix): bool => $appendix[0] !== $appendix[1] . '=' . $appendix[2]);
153 
154  $here = 1;
155 
156  $appendices = array_map(static fn($appendix): string => $appendix[1] . '=' . $appendix[2], $appendices);
157  }
158  } catch (\Throwable $t) {
159  return [];
160  }
161 
162  return $appendices;
163  }
164 }
if(count($parts) !=3) $payload
Definition: ltitoken.php:67
$url
Definition: shib_logout.php:68
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
__construct()
Constructor setup ILIAS global object public.
Definition: class.ilias.php:76
global $DIC
Definition: shib_login.php:26
ActionTarget
Officialy supported action targets, see https://learn.microsoft.com/en-us/openspecs/office_protocols/...