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