ILIAS  trunk Revision v11.0_alpha-1769-g99a433fe2dc
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
class.ilWACPath.php
Go to the documentation of this file.
1 <?php
2 
19 // declare(strict_types=1);
26 class ilWACPath
27 {
28  public const DIR_DATA = "data";
29  public const DIR_SEC = "sec";
33  public const REGEX = "(?<prefix>.*?)(?<path>(?<path_without_query>(?<secure_path_id>(?<module_path>\/data\/(?<client>[\w\-\.]*)\/(?<sec>sec\/|)(?<module_type>.*?)\/(?<module_identifier>.*\/|)))(?<appendix>[^\?\n]*)).*)";
37  protected static array $image_suffixes = [
38  'png',
39  'jpg',
40  'jpeg',
41  'gif',
42  'svg',
43  ];
47  protected static array $video_suffixes = [
48  'mp4',
49  'm4v',
50  'mov',
51  'wmv',
52  'webm',
53  ];
57  protected static array $audio_suffixes = [
58  'mp3',
59  'aiff',
60  'aif',
61  'm4a',
62  'wav',
63  ];
64 
65  protected string $client = '';
69  protected array $parameters = [];
70  protected bool $in_sec_folder = false;
71  protected string $token = '';
72  protected int $timestamp = 0;
73  protected int $ttl = 0;
74  protected string $secure_path = '';
75  protected string $secure_path_id = '';
76  protected string $original_request = '';
77  protected string $file_name = '';
78  protected string $query = '';
79  protected string $suffix = '';
80  protected string $prefix = '';
81  protected string $appendix = '';
82  protected string $module_path = '';
83  protected string $path = '';
84  protected string $module_type = '';
85  protected string $module_identifier = '';
86  protected string $path_without_query = '';
87 
88  public function __construct(string $path)
89  {
90  $path = $this->normalizePath($path);
91 
92  $this->setOriginalRequest($path);
93  $re = '/' . self::REGEX . '/';
94  preg_match($re, $path, $result);
95 
96  $result['path_without_query'] = strstr(
97  parse_url($path)['path'],
98  '/data/',
99  false
100  );
101 
102 
103  foreach (array_keys(array_keys($result)) as $k) {
104  if (is_numeric($k)) {
105  unset($result[$k]);
106  }
107  }
108 
109  $moduleId = strstr(
110  !isset($result['module_identifier']) || is_null($result['module_identifier']) ? '' : $result['module_identifier'],
111  '/',
112  true
113  );
114  $moduleId = $moduleId === false ? '' : $moduleId;
115 
116  $this->setPrefix(!isset($result['prefix']) || is_null($result['prefix']) ? '' : $result['prefix']);
117  $this->setClient(!isset($result['client']) || is_null($result['client']) ? '' : $result['client']);
118  $this->setAppendix(!isset($result['appendix']) || is_null($result['appendix']) ? '' : $result['appendix']);
119  $this->setModuleIdentifier($moduleId);
120  $this->setModuleType(!isset($result['module_type']) || is_null($result['module_type']) ? '' : $result['module_type']);
121 
122  if ($this->getModuleIdentifier() !== '' && $this->getModuleIdentifier() !== '0') {
123  $module_path = strstr(
124  !isset($result['module_path']) || is_null($result['module_path']) ? '' : $result['module_path'],
125  $this->getModuleIdentifier(),
126  true
127  );
128  $module_path = '.' . ($module_path === false ? '' : $module_path);
129  } else {
130  $module_path = ('.' . (!isset($result['module_path']) || is_null($result['module_path']) ? '' : $result['module_path']));
131  }
132 
133  $this->setModulePath($module_path);
134  $this->setInSecFolder(isset($result['sec']) && $result['sec'] === 'sec/');
135  $this->setPathWithoutQuery(
136  '.' . (!isset($result['path_without_query']) || is_null($result['path_without_query']) ? '' : $result['path_without_query'])
137  );
138  $this->setPath('.' . (!isset($result['path']) || is_null($result['path']) ? '' : $result['path']));
139  $this->setSecurePath(
140  '.' . (!isset($result['secure_path_id']) || is_null($result['secure_path_id']) ? '' : $result['secure_path_id'])
141  );
142  $this->setSecurePathId(!isset($result['module_type']) || is_null($result['module_type']) ? '' : $result['module_type']);
143  // Pathinfo
144  $parts = parse_url($path);
145  $this->setFileName(basename($parts['path']));
146  if (isset($parts['query'])) {
147  $parts_query = $parts['query'];
148  $this->setQuery($parts_query);
149  parse_str($parts_query, $query);
150  $this->setParameters($query);
151  }
152  $this->setSuffix(pathinfo($parts['path'], PATHINFO_EXTENSION));
153  $this->handleParameters();
154  }
155 
156  protected function handleParameters(): void
157  {
158  $param = $this->getParameters();
160  $this->setToken($param[ilWACSignedPath::WAC_TOKEN_ID]);
161  }
163  $this->setTimestamp((int) $param[ilWACSignedPath::WAC_TIMESTAMP_ID]);
164  }
165  if (isset($param[ilWACSignedPath::WAC_TTL_ID])) {
166  $this->setTTL((int) $param[ilWACSignedPath::WAC_TTL_ID]);
167  }
168  }
169 
173  public function getParameters(): array
174  {
175  return $this->parameters;
176  }
177 
181  public function setParameters(array $parameters): void
182  {
183  $this->parameters = $parameters;
184  }
185 
189  public static function getAudioSuffixes(): array
190  {
191  return self::$audio_suffixes;
192  }
193 
197  public static function setAudioSuffixes(array $audio_suffixes): void
198  {
199  self::$audio_suffixes = $audio_suffixes;
200  }
201 
205  public static function getImageSuffixes(): array
206  {
207  return self::$image_suffixes;
208  }
209 
213  public static function setImageSuffixes(array $image_suffixes): void
214  {
215  self::$image_suffixes = $image_suffixes;
216  }
217 
221  public static function getVideoSuffixes(): array
222  {
223  return self::$video_suffixes;
224  }
225 
229  public static function setVideoSuffixes(array $video_suffixes): void
230  {
231  self::$video_suffixes = $video_suffixes;
232  }
233 
234  protected function normalizePath(string $path): string
235  {
236  $path = ltrim($path, '.');
237  $path = rawurldecode($path);
238 
239  // cut everything before "data/" (for installations using a subdirectory)
240  $path = strstr($path, '/' . self::DIR_DATA . '/') ?: $path;
241  $path = ltrim($path, '/');
242 
243  $original_path = parse_url($path, PHP_URL_PATH);
244  $query = parse_url($path, PHP_URL_QUERY);
245 
246  $real_data_dir = realpath("./" . self::DIR_DATA);
247  $realpath = realpath("./" . $original_path);
248 
249  if (!str_starts_with($realpath, $real_data_dir)) {
250  throw new ilWACException(ilWACException::NOT_FOUND, "Path is not in data directory");
251  }
252 
253  $normalized_path = ltrim(
254  str_replace(
255  $real_data_dir,
256  '',
257  $realpath
258  ),
259  '/'
260  );
261 
262  return "/" . self::DIR_DATA . '/' . $normalized_path . (empty($query) ? '' : '?' . $query);
263  }
264 
265  public function getPrefix(): string
266  {
267  return $this->prefix;
268  }
269 
270  public function setPrefix(string $prefix): void
271  {
272  $this->prefix = $prefix;
273  }
274 
275  public function getAppendix(): string
276  {
277  return $this->appendix;
278  }
279 
280  public function setAppendix(string $appendix): void
281  {
282  $this->appendix = $appendix;
283  }
284 
285  public function getModulePath(): string
286  {
287  return $this->module_path;
288  }
289 
290  public function setModulePath(string $module_path): void
291  {
292  $this->module_path = $module_path;
293  }
294 
295  public function getDirName(): string
296  {
297  return dirname($this->getPathWithoutQuery());
298  }
299 
300  public function getPathWithoutQuery(): string
301  {
303  }
304 
305  public function setPathWithoutQuery(string $path_without_query): void
306  {
307  $this->path_without_query = $path_without_query;
308  }
309 
310  public function isImage(): bool
311  {
312  return in_array(strtolower($this->getSuffix()), self::$image_suffixes);
313  }
314 
315  public function getSuffix(): string
316  {
317  return $this->suffix;
318  }
319 
320  public function setSuffix(string $suffix): void
321  {
322  $this->suffix = $suffix;
323  }
324 
325  public function isStreamable(): bool
326  {
327  if ($this->isAudio()) {
328  return true;
329  }
330  return $this->isVideo();
331  }
332 
333  public function isAudio(): bool
334  {
335  return in_array(strtolower($this->getSuffix()), self::$audio_suffixes);
336  }
337 
338  public function isVideo(): bool
339  {
340  return in_array(strtolower($this->getSuffix()), self::$video_suffixes);
341  }
342 
343  public function fileExists(): bool
344  {
345  return is_file($this->getPathWithoutQuery());
346  }
347 
348  public function hasToken(): bool
349  {
350  return ($this->token !== '');
351  }
352 
353  public function hasTimestamp(): bool
354  {
355  return ($this->timestamp !== 0);
356  }
357 
358  public function hasTTL(): bool
359  {
360  return ($this->ttl !== 0);
361  }
362 
363  public function getToken(): string
364  {
365  return $this->token;
366  }
367 
368  public function setToken(string $token): void
369  {
371  $this->token = $token;
372  }
373 
374  public function getTimestamp(): int
375  {
376  return $this->timestamp;
377  }
378 
379  public function setTimestamp(int $timestamp): void
380  {
382  $this->timestamp = $timestamp;
383  }
384 
385  public function getTTL(): int
386  {
387  return $this->ttl;
388  }
389 
390  public function setTTL(int $ttl): void
391  {
393  $this->ttl = $ttl;
394  }
395 
396  public function getClient(): string
397  {
398  return $this->client;
399  }
400 
401  public function setClient(string $client): void
402  {
403  $this->client = $client;
404  }
405 
406  public function getSecurePathId(): string
407  {
408  return $this->secure_path_id;
409  }
410 
411  public function setSecurePathId(string $secure_path_id): void
412  {
413  $this->secure_path_id = $secure_path_id;
414  }
415 
416  public function getPath(): string
417  {
418  return $this->path;
419  }
420 
424  public function getCleanURLdecodedPath(): string
425  {
426  $string = $this->getPathWithoutQuery();
427  return rawurldecode($string);
428  }
429 
430  public function setPath(string $path): void
431  {
432  $this->path = $path;
433  }
434 
435  public function getQuery(): string
436  {
437  return $this->query;
438  }
439 
440  public function setQuery(string $query): void
441  {
442  $this->query = $query;
443  }
444 
445  public function getFileName(): string
446  {
447  return $this->file_name;
448  }
449 
450  public function setFileName(string $file_name): void
451  {
452  $this->file_name = $file_name;
453  }
454 
455  public function getOriginalRequest(): string
456  {
458  }
459 
460  public function setOriginalRequest(string $original_request): void
461  {
462  $this->original_request = $original_request;
463  }
464 
465  public function getSecurePath(): string
466  {
467  return $this->secure_path;
468  }
469 
470  public function setSecurePath(string $secure_path): void
471  {
472  $this->secure_path = $secure_path;
473  }
474 
475  public function isInSecFolder(): bool
476  {
477  return $this->in_sec_folder;
478  }
479 
480  public function setInSecFolder(bool $in_sec_folder): void
481  {
482  $this->in_sec_folder = $in_sec_folder;
483  }
484 
485  public function getModuleType(): string
486  {
487  return $this->module_type;
488  }
489 
490  public function setModuleType(string $module_type): void
491  {
492  $this->module_type = $module_type;
493  }
494 
495  public function getModuleIdentifier(): string
496  {
498  }
499 
500  public function setModuleIdentifier(string $module_identifier): void
501  {
502  $this->module_identifier = $module_identifier;
503  }
504 }
static array $video_suffixes
setOriginalRequest(string $original_request)
setClient(string $client)
bool $in_sec_folder
setPath(string $path)
parameters()
description: > This shows how different states are being used in the same Prompt according to parame...
Definition: parameters.php:39
static setAudioSuffixes(array $audio_suffixes)
string $suffix
if($clientAssertionType !='urn:ietf:params:oauth:client-assertion-type:jwt-bearer'|| $grantType !='client_credentials') $parts
Definition: ltitoken.php:61
string $module_identifier
string $prefix
array $parameters
static setVideoSuffixes(array $video_suffixes)
setTTL(int $ttl)
string $module_type
string $secure_path_id
string $secure_path
setParameters(array $parameters)
setModulePath(string $module_path)
static array $audio_suffixes
setPathWithoutQuery(string $path_without_query)
string $path_without_query
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
string $appendix
static getAudioSuffixes()
setSecurePath(string $secure_path)
string $original_request
__construct(string $path)
string $file_name
string $module_path
setTimestamp(int $timestamp)
setSuffix(string $suffix)
$param
Definition: xapitoken.php:46
string $client
static getVideoSuffixes()
static setImageSuffixes(array $image_suffixes)
normalizePath(string $path)
static getImageSuffixes()
setAppendix(string $appendix)
setInSecFolder(bool $in_sec_folder)
setPrefix(string $prefix)
setQuery(string $query)
client()
description: > This example shows how a Progress Bar can be rendered and used on the client...
Definition: client.php:37
const REGEX
Copy this without to regex101.com and test with some URL of files.
setSecurePathId(string $secure_path_id)
getCleanURLdecodedPath()
Returns a clean (everything behind ? is removed and rawurldecoded path.
setToken(string $token)
setModuleIdentifier(string $module_identifier)
setModuleType(string $module_type)
static array $image_suffixes
setFileName(string $file_name)