ILIAS  release_7 Revision v7.30-3-g800a261c036
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilWACPath.php
Go to the documentation of this file.
1 <?php
18 use ILIAS\Data\URI;
19 
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 $image_suffixes = ['png', 'jpg', 'jpeg', 'gif', 'svg'];
41  protected static $video_suffixes = ['mp4', 'm4v', 'mov', 'wmv', 'webm'];
45  protected static $audio_suffixes = ['mp3', 'aiff', 'aif', 'm4a', 'wav'];
49  protected $client = '';
53  protected $parameters = [];
57  protected $in_sec_folder = false;
61  protected $token = '';
65  protected $timestamp = 0;
69  protected $ttl = 0;
73  protected $secure_path = '';
77  protected $secure_path_id = '';
81  protected $original_request = '';
85  protected $file_name = '';
89  protected $query = '';
93  protected $suffix = '';
97  protected $prefix = '';
101  protected $appendix = '';
105  protected $module_path = '';
109  protected $path = '';
113  protected $module_type = '';
117  protected $module_identifier = '';
121  protected $path_without_query = '';
122 
123 
124  public function __construct(string $path, bool $normalize = true)
125  {
126  if ($normalize) {
127  $path = $this->normalizePath($path);
128  }
129 
130  $this->setOriginalRequest($path);
131 
132  $re = '/' . self::REGEX . '/';
133  preg_match($re, $path, $result);
134 
135  $result['path_without_query'] = strstr(
136  parse_url($path)['path'],
137  '/data/',
138  false
139  );
140 
141  foreach ($result as $k => $v) {
142  if (is_numeric($k)) {
143  unset($result[$k]);
144  }
145  }
146 
147  $moduleId = strstr($result['module_identifier'] ?? '', "/", true);
148  $moduleId = $moduleId === false ? '' : $moduleId;
149 
150  $this->setPrefix($result['prefix'] ?? '');
151  $this->setClient($result['client'] ?? '');
152  $this->setAppendix($result['appendix'] ?? '');
153  $this->setModuleIdentifier($moduleId);
154  $this->setModuleType($result['module_type'] ?? '');
155 
156  $modulePath = null;
157 
158  if ($this->getModuleIdentifier()) {
159  $modulePath = strstr($result['module_path'] ?? '', $this->getModuleIdentifier(), true);
160  $modulePath = '.' . ($modulePath === false ? '' : $modulePath);
161  } else {
162  $modulePath = ('.' . ($result['module_path'] ?? ''));
163  }
164 
165  $this->setModulePath((string) $modulePath);
166  $this->setInSecFolder(($result['sec'] ?? null) === 'sec/');
167  $this->setPathWithoutQuery('.' . ($result['path_without_query'] ?? ''));
168  $this->setPath('.' . ($result['path'] ?? ''));
169  $this->setSecurePath('.' . ($result['secure_path_id'] ?? ''));
170  $this->setSecurePathId($result['module_type'] ?? '');
171  // Pathinfo
172  $parts = parse_url($path);
173  $this->setFileName(basename($parts['path']));
174  if (isset($parts['query'])) {
175  $parts_query = $parts['query'];
176  $this->setQuery($parts_query);
177  parse_str($parts_query, $query);
178  $this->setParameters($query);
179  }
180  $this->setSuffix(pathinfo($parts['path'], PATHINFO_EXTENSION));
181  $this->handleParameters();
182  }
183 
184 
185  protected function handleParameters()
186  {
187  $param = $this->getParameters();
189  $this->setToken($param[ilWACSignedPath::WAC_TOKEN_ID]);
190  }
192  $this->setTimestamp(intval($param[ilWACSignedPath::WAC_TIMESTAMP_ID]));
193  }
194  if (isset($param[ilWACSignedPath::WAC_TTL_ID])) {
195  $this->setTTL(intval($param[ilWACSignedPath::WAC_TTL_ID]));
196  }
197  }
198 
199 
203  public function getParameters()
204  {
205  return (array) $this->parameters;
206  }
207 
208 
212  public function setParameters(array $parameters)
213  {
214  $this->parameters = $parameters;
215  }
216 
217 
221  public static function getAudioSuffixes()
222  {
223  return (array) self::$audio_suffixes;
224  }
225 
226 
230  public static function setAudioSuffixes(array $audio_suffixes)
231  {
232  self::$audio_suffixes = $audio_suffixes;
233  }
234 
235 
239  public static function getImageSuffixes()
240  {
241  return (array) self::$image_suffixes;
242  }
243 
244 
248  public static function setImageSuffixes(array $image_suffixes)
249  {
250  self::$image_suffixes = $image_suffixes;
251  }
252 
253 
257  public static function getVideoSuffixes()
258  {
259  return (array) self::$video_suffixes;
260  }
261 
262 
266  public static function setVideoSuffixes(array $video_suffixes)
267  {
268  self::$video_suffixes = $video_suffixes;
269  }
270 
271  protected function normalizePath(string $path) : string
272  {
273  $path = ltrim($path, '.');
274  $path = rawurldecode($path);
275 
276  // cut everything before "data/" (for installations using a subdirectory)
277  $path = strstr($path, '/' . self::DIR_DATA . '/');
278 
279  $original_path = parse_url($path, PHP_URL_PATH);
280  $query = parse_url($path, PHP_URL_QUERY);
281 
282  $real_data_dir = realpath("./" . self::DIR_DATA);
283  $realpath = realpath("." . $original_path);
284 
285  if (strpos($realpath, (string) $real_data_dir) !== 0) {
286  throw new ilWACException(ilWACException::NOT_FOUND, "Path is not in data directory");
287  }
288 
289  $normalized_path = ltrim(
290  str_replace(
291  $real_data_dir,
292  '',
293  $realpath
294  ),
295  '/'
296  );
297 
298  return "/" . self::DIR_DATA . '/' . $normalized_path . (!empty($query) ? '?' . $query : '');
299  }
300 
304  public function getPrefix()
305  {
306  return (string) $this->prefix;
307  }
308 
309 
313  public function setPrefix($prefix)
314  {
315  assert(is_string($prefix));
316  $this->prefix = $prefix;
317  }
318 
319 
323  public function getAppendix()
324  {
325  return (string) $this->appendix;
326  }
327 
328 
332  public function setAppendix($appendix)
333  {
334  assert(is_string($appendix));
335  $this->appendix = $appendix;
336  }
337 
338 
342  public function getModulePath()
343  {
344  return (string) $this->module_path;
345  }
346 
347 
351  public function setModulePath($module_path)
352  {
353  assert(is_string($module_path));
354  $this->module_path = $module_path;
355  }
356 
357 
361  public function getDirName()
362  {
363  return (string) dirname($this->getPathWithoutQuery());
364  }
365 
366 
370  public function getPathWithoutQuery()
371  {
372  return (string) $this->path_without_query;
373  }
374 
375 
380  {
381  assert(is_string($path_without_query));
382  $this->path_without_query = $path_without_query;
383  }
384 
385 
389  public function isImage()
390  {
391  return (bool) in_array(strtolower($this->getSuffix()), self::$image_suffixes);
392  }
393 
394 
398  public function getSuffix()
399  {
400  return (string) $this->suffix;
401  }
402 
403 
407  public function setSuffix($suffix)
408  {
409  assert(is_string($suffix));
410  $this->suffix = $suffix;
411  }
412 
413 
417  public function isStreamable()
418  {
419  return (bool) ($this->isAudio() || $this->isVideo());
420  }
421 
422 
426  public function isAudio()
427  {
428  return (bool) in_array(strtolower($this->getSuffix()), self::$audio_suffixes);
429  }
430 
431 
435  public function isVideo()
436  {
437  return (bool) in_array(strtolower($this->getSuffix()), self::$video_suffixes);
438  }
439 
440 
444  public function fileExists()
445  {
446  return (bool) is_file($this->getPathWithoutQuery());
447  }
448 
449 
453  public function hasToken()
454  {
455  return (bool) ($this->token !== '');
456  }
457 
458 
462  public function hasTimestamp()
463  {
464  return (bool) ($this->timestamp !== 0);
465  }
466 
467 
471  public function hasTTL()
472  {
473  return (bool) ($this->ttl !== 0);
474  }
475 
476 
480  public function getToken()
481  {
482  return (string) $this->token;
483  }
484 
485 
489  public function setToken($token)
490  {
491  assert(is_string($token));
492  $this->parameters[ilWACSignedPath::WAC_TOKEN_ID] = $token;
493  $this->token = $token;
494  }
495 
496 
500  public function getTimestamp()
501  {
502  return (int) $this->timestamp;
503  }
504 
505 
509  public function setTimestamp($timestamp)
510  {
511  assert(is_int($timestamp));
512  $this->parameters[ilWACSignedPath::WAC_TIMESTAMP_ID] = $timestamp;
513  $this->timestamp = $timestamp;
514  }
515 
516 
520  public function getTTL()
521  {
522  return (int) $this->ttl;
523  }
524 
525 
529  public function setTTL($ttl)
530  {
531  $this->parameters[ilWACSignedPath::WAC_TTL_ID] = $ttl;
532  $this->ttl = $ttl;
533  }
534 
535 
539  public function getClient()
540  {
541  return (string) $this->client;
542  }
543 
544 
548  public function setClient($client)
549  {
550  assert(is_string($client));
551  $this->client = $client;
552  }
553 
554 
558  public function getSecurePathId()
559  {
560  return (string) $this->secure_path_id;
561  }
562 
563 
568  {
569  assert(is_string($secure_path_id));
570  $this->secure_path_id = $secure_path_id;
571  }
572 
573 
577  public function getPath()
578  {
579  return (string) $this->path;
580  }
581 
582 
588  public function getCleanURLdecodedPath()
589  {
590  return rawurldecode($this->getPathWithoutQuery());
591  }
592 
593 
597  public function setPath($path)
598  {
599  assert(is_string($path));
600  $this->path = $path;
601  }
602 
603 
607  public function getQuery()
608  {
609  return (string) $this->query;
610  }
611 
612 
616  public function setQuery($query)
617  {
618  assert(is_string($query));
619  $this->query = $query;
620  }
621 
622 
626  public function getFileName()
627  {
628  return (string) $this->file_name;
629  }
630 
631 
635  public function setFileName($file_name)
636  {
637  assert(is_string($file_name));
638  $this->file_name = $file_name;
639  }
640 
641 
645  public function getOriginalRequest()
646  {
647  return (string) $this->original_request;
648  }
649 
650 
655  {
656  assert(is_string($original_request));
657  $this->original_request = $original_request;
658  }
659 
660 
664  public function getSecurePath()
665  {
666  return (string) $this->secure_path;
667  }
668 
669 
673  public function setSecurePath($secure_path)
674  {
675  assert(is_string($secure_path));
676  $this->secure_path = $secure_path;
677  }
678 
679 
683  public function isInSecFolder()
684  {
685  return (bool) $this->in_sec_folder;
686  }
687 
688 
693  {
694  assert(is_bool($in_sec_folder));
695  $this->in_sec_folder = $in_sec_folder;
696  }
697 
698 
702  public function getModuleType()
703  {
704  return (string) $this->module_type;
705  }
706 
707 
711  public function setModuleType($module_type)
712  {
713  assert(is_string($module_type));
714  $this->module_type = $module_type;
715  }
716 
717 
721  public function getModuleIdentifier()
722  {
723  return (string) $this->module_identifier;
724  }
725 
726 
731  {
732  assert(is_string($module_identifier));
733  $this->module_identifier = $module_identifier;
734  }
735 }
setPathWithoutQuery($path_without_query)
static setAudioSuffixes(array $audio_suffixes)
__construct(string $path, bool $normalize=true)
static $image_suffixes
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
setQuery($query)
$result
static setVideoSuffixes(array $video_suffixes)
setModulePath($module_path)
setClient($client)
setParameters(array $parameters)
static $audio_suffixes
setModuleType($module_type)
static $video_suffixes
Class ilWACPath.
static getAudioSuffixes()
setSuffix($suffix)
setSecurePath($secure_path)
setToken($token)
$param
Definition: xapitoken.php:29
setModuleIdentifier($module_identifier)
setPrefix($prefix)
static getVideoSuffixes()
static setImageSuffixes(array $image_suffixes)
setTimestamp($timestamp)
normalizePath(string $path)
static getImageSuffixes()
const REGEX
Copy this without to regex101.com and test with some URL of files.
getCleanURLdecodedPath()
Returns a clean (everything behind ? is removed and rawurldecoded path.
setSecurePathId($secure_path_id)
setFileName($file_name)
setAppendix($appendix)
setOriginalRequest($original_request)
setInSecFolder($in_sec_folder)