ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
class.ilWACPath.php
Go to the documentation of this file.
1<?php
2// declare(strict_types=1);
3
11{
12 const DIR_DATA = "data";
13 const DIR_SEC = "sec";
17 const REGEX = "(?<prefix>.*?)(?<path>(?<path_without_query>(?<secure_path_id>(?<module_path>\/data\/(?<client>[\w\-\.]*)\/(?<sec>sec\/|)(?<module_type>.*?)\/(?<module_identifier>.*\/|)))(?<appendix>[^\?\n]*)).*)";
21 protected static $image_suffixes = array(
22 'png',
23 'jpg',
24 'jpeg',
25 'gif',
26 'svg',
27 );
31 protected static $video_suffixes = array(
32 'mp4',
33 'm4v',
34 'mov',
35 'wmv',
36 'webm',
37 );
41 protected static $audio_suffixes = array(
42 'mp3',
43 'aiff',
44 'aif',
45 'm4a',
46 'wav',
47 );
51 protected $client = '';
55 protected $parameters = array();
59 protected $in_sec_folder = false;
63 protected $token = '';
67 protected $timestamp = 0;
71 protected $ttl = 0;
75 protected $secure_path = '';
79 protected $secure_path_id = '';
83 protected $original_request = '';
87 protected $file_name = '';
91 protected $query = '';
95 protected $suffix = '';
99 protected $prefix = '';
103 protected $appendix = '';
107 protected $module_path = '';
111 protected $path = '';
115 protected $module_type = '';
119 protected $module_identifier = '';
123 protected $path_without_query = '';
124
125
131 public function __construct($path)
132 {
133 assert(is_string($path));
135 $re = '/' . self::REGEX . '/';
136 preg_match($re, $path, $result);
137
138 foreach ($result as $k => $v) {
139 if (is_numeric($k)) {
140 unset($result[$k]);
141 }
142 }
143
144 $moduleId = strstr(is_null($result['module_identifier']) ? '' : $result['module_identifier'], "/", true);
145 $moduleId = $moduleId === false ? '' : $moduleId;
146
147 $this->setPrefix(is_null($result['prefix']) ? '' : $result['prefix']);
148 $this->setClient(is_null($result['client']) ? '' : $result['client']);
149 $this->setAppendix(is_null($result['appendix']) ? '' : $result['appendix']);
150 $this->setModuleIdentifier($moduleId);
151 $this->setModuleType(is_null($result['module_type']) ? '' : $result['module_type']);
152
153 $modulePath = null;
154
155 if ($this->getModuleIdentifier()) {
156 $modulePath = strstr(is_null($result['module_path']) ? '' : $result['module_path'], $this->getModuleIdentifier(), true);
157 $modulePath = '.' . ($modulePath === false ? '' : $modulePath);
158 } else {
159 $modulePath = ('.' . (is_null($result['module_path']) ? '' : $result['module_path']));
160 }
161
162 $this->setModulePath("$modulePath");
163 $this->setInSecFolder($result['sec'] === 'sec/');
164 $this->setPathWithoutQuery('.'
165 . (is_null($result['path_without_query']) ? '' : $result['path_without_query']));
166 $this->setPath('.' . (is_null($result['path']) ? '' : $result['path']));
167 $this->setSecurePath('.'
168 . (is_null($result['secure_path_id']) ? '' : $result['secure_path_id']));
169 $this->setSecurePathId(is_null($result['module_type']) ? '' : $result['module_type']);
170 // Pathinfo
171 $parts = parse_url($path);
172 $this->setFileName(basename($parts['path']));
173 if (isset($parts['query'])) {
174 $parts_query = $parts['query'];
175 $this->setQuery($parts_query);
176 parse_str($parts_query, $query);
177 $this->setParameters($query);
178 }
179 $this->setSuffix(pathinfo($parts['path'], PATHINFO_EXTENSION));
180 $this->handleParameters();
181 }
182
183
184 protected function handleParameters()
185 {
186 $param = $this->getParameters();
187 if (isset($param[ilWACSignedPath::WAC_TOKEN_ID])) {
189 }
190 if (isset($param[ilWACSignedPath::WAC_TIMESTAMP_ID])) {
191 $this->setTimestamp(intval($param[ilWACSignedPath::WAC_TIMESTAMP_ID]));
192 }
193 if (isset($param[ilWACSignedPath::WAC_TTL_ID])) {
194 $this->setTTL(intval($param[ilWACSignedPath::WAC_TTL_ID]));
195 }
196 }
197
198
202 public function getParameters()
203 {
204 return (array) $this->parameters;
205 }
206
207
211 public function setParameters(array $parameters)
212 {
213 $this->parameters = $parameters;
214 }
215
216
220 public static function getAudioSuffixes()
221 {
222 return (array) self::$audio_suffixes;
223 }
224
225
229 public static function setAudioSuffixes(array $audio_suffixes)
230 {
231 self::$audio_suffixes = $audio_suffixes;
232 }
233
234
238 public static function getImageSuffixes()
239 {
240 return (array) self::$image_suffixes;
241 }
242
243
247 public static function setImageSuffixes(array $image_suffixes)
248 {
249 self::$image_suffixes = $image_suffixes;
250 }
251
252
256 public static function getVideoSuffixes()
257 {
258 return (array) self::$video_suffixes;
259 }
260
261
265 public static function setVideoSuffixes(array $video_suffixes)
266 {
267 self::$video_suffixes = $video_suffixes;
268 }
269
270
274 public function getPrefix()
275 {
276 return (string) $this->prefix;
277 }
278
279
283 public function setPrefix($prefix)
284 {
285 assert(is_string($prefix));
286 $this->prefix = $prefix;
287 }
288
289
293 public function getAppendix()
294 {
295 return (string) $this->appendix;
296 }
297
298
302 public function setAppendix($appendix)
303 {
304 assert(is_string($appendix));
305 $this->appendix = $appendix;
306 }
307
308
312 public function getModulePath()
313 {
314 return (string) $this->module_path;
315 }
316
317
322 {
323 assert(is_string($module_path));
324 $this->module_path = $module_path;
325 }
326
327
331 public function getDirName()
332 {
333 return (string) dirname($this->getPathWithoutQuery());
334 }
335
336
340 public function getPathWithoutQuery()
341 {
342 return (string) $this->path_without_query;
343 }
344
345
350 {
351 assert(is_string($path_without_query));
352 $this->path_without_query = $path_without_query;
353 }
354
355
359 public function isImage()
360 {
361 return (bool) in_array(strtolower($this->getSuffix()), self::$image_suffixes);
362 }
363
364
368 public function getSuffix()
369 {
370 return (string) $this->suffix;
371 }
372
373
377 public function setSuffix($suffix)
378 {
379 assert(is_string($suffix));
380 $this->suffix = $suffix;
381 }
382
383
387 public function isStreamable()
388 {
389 return (bool) ($this->isAudio() || $this->isVideo());
390 }
391
392
396 public function isAudio()
397 {
398 return (bool) in_array(strtolower($this->getSuffix()), self::$audio_suffixes);
399 }
400
401
405 public function isVideo()
406 {
407 return (bool) in_array(strtolower($this->getSuffix()), self::$video_suffixes);
408 }
409
410
414 public function fileExists()
415 {
416 return (bool) is_file($this->getPathWithoutQuery());
417 }
418
419
423 public function hasToken()
424 {
425 return (bool) ($this->token !== '');
426 }
427
428
432 public function hasTimestamp()
433 {
434 return (bool) ($this->timestamp !== 0);
435 }
436
437
441 public function hasTTL()
442 {
443 return (bool) ($this->ttl !== 0);
444 }
445
446
450 public function getToken()
451 {
452 return (string) $this->token;
453 }
454
455
459 public function setToken($token)
460 {
461 assert(is_string($token));
462 $this->parameters[ilWACSignedPath::WAC_TOKEN_ID] = $token;
463 $this->token = $token;
464 }
465
466
470 public function getTimestamp()
471 {
472 return (int) $this->timestamp;
473 }
474
475
479 public function setTimestamp($timestamp)
480 {
481 assert(is_int($timestamp));
483 $this->timestamp = $timestamp;
484 }
485
486
490 public function getTTL()
491 {
492 return (int) $this->ttl;
493 }
494
495
499 public function setTTL($ttl)
500 {
501 $this->parameters[ilWACSignedPath::WAC_TTL_ID] = $ttl;
502 $this->ttl = $ttl;
503 }
504
505
509 public function getClient()
510 {
511 return (string) $this->client;
512 }
513
514
518 public function setClient($client)
519 {
520 assert(is_string($client));
521 $this->client = $client;
522 }
523
524
528 public function getSecurePathId()
529 {
530 return (string) $this->secure_path_id;
531 }
532
533
538 {
539 assert(is_string($secure_path_id));
540 $this->secure_path_id = $secure_path_id;
541 }
542
543
547 public function getPath()
548 {
549 return (string) $this->path;
550 }
551
552
558 public function getCleanURLdecodedPath()
559 {
560 $path = explode("?", (string) $this->path); // removing everything behind ?
561 $path_to_file = rawurldecode($path[0]);
562
563 return $path_to_file;
564 }
565
566
570 public function setPath($path)
571 {
572 assert(is_string($path));
573 $this->path = $path;
574 }
575
576
580 public function getQuery()
581 {
582 return (string) $this->query;
583 }
584
585
589 public function setQuery($query)
590 {
591 assert(is_string($query));
592 $this->query = $query;
593 }
594
595
599 public function getFileName()
600 {
601 return (string) $this->file_name;
602 }
603
604
608 public function setFileName($file_name)
609 {
610 assert(is_string($file_name));
611 $this->file_name = $file_name;
612 }
613
614
618 public function getOriginalRequest()
619 {
620 return (string) $this->original_request;
621 }
622
623
628 {
629 assert(is_string($original_request));
630 $this->original_request = $original_request;
631 }
632
633
637 public function getSecurePath()
638 {
639 return (string) $this->secure_path;
640 }
641
642
647 {
648 assert(is_string($secure_path));
649 $this->secure_path = $secure_path;
650 }
651
652
656 public function isInSecFolder()
657 {
658 return (bool) $this->in_sec_folder;
659 }
660
661
666 {
667 assert(is_bool($in_sec_folder));
668 $this->in_sec_folder = $in_sec_folder;
669 }
670
671
675 public function getModuleType()
676 {
677 return (string) $this->module_type;
678 }
679
680
685 {
686 assert(is_string($module_type));
687 $this->module_type = $module_type;
688 }
689
690
694 public function getModuleIdentifier()
695 {
696 return (string) $this->module_identifier;
697 }
698
699
704 {
705 assert(is_string($module_identifier));
706 $this->module_identifier = $module_identifier;
707 }
708}
$result
An exception for terminatinating execution or to throw for unit testing.
Class ilWACPath.
setAppendix($appendix)
setOriginalRequest($original_request)
setSecurePathId($secure_path_id)
static getAudioSuffixes()
setClient($client)
static $video_suffixes
static getImageSuffixes()
setQuery($query)
setToken($token)
static setAudioSuffixes(array $audio_suffixes)
getCleanURLdecodedPath()
Returns a clean (everything behind ? is removed and rawurldecoded path.
setModuleIdentifier($module_identifier)
setModulePath($module_path)
static setVideoSuffixes(array $video_suffixes)
const REGEX
Copy this without to regex101.com and test with some URL of files.
setModuleType($module_type)
__construct($path)
ilWACPath constructor.
setInSecFolder($in_sec_folder)
setSuffix($suffix)
setTimestamp($timestamp)
static $image_suffixes
setSecurePath($secure_path)
setParameters(array $parameters)
static setImageSuffixes(array $image_suffixes)
setFileName($file_name)
setPrefix($prefix)
static $audio_suffixes
setPathWithoutQuery($path_without_query)
static getVideoSuffixes()