ILIAS  trunk Revision v11.0_alpha-1753-gb21ca8c4367
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
AbstractCollection.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
23 use Iterator;
24 use ILIAS\Data\URI;
25 
29 abstract class AbstractCollection
30 {
34  protected array $items = [];
35 
36  public function __construct(
37  protected string $resource_version,
38  protected bool $append_resource_version = false,
39  protected bool $strip_queries = true,
40  protected bool $allow_external = false,
41  protected bool $allow_non_existing = false,
42  ) {
43  }
44 
45  public function clear(): void
46  {
47  $this->items = [];
48  }
49 
50  protected function isURI(string $content): bool
51  {
52  if (realpath($content) !== false) {
53  return false;
54  }
55 
56  try {
57  new URI($content);
58  return true;
59  } catch (\Throwable) {
60  return false;
61  }
62  }
63 
64  protected function isExternalURI(string $content): bool
65  {
66  if (!$this->isURI($content)) {
67  return false;
68  }
69 
70  try {
71  if ((new URI($url))->getHost() !== (new URI(ILIAS_HTTP_PATH))->getHost()) {
72  return true;
73  }
74  } catch (\Throwable) {
75  return false;
76  }
77 
78  return false;
79  }
80 
84  public function getItems(): Iterator
85  {
86  foreach ($this->items as $path => $item) {
87  yield $path => $this->handleParameters($item);
88  }
89  }
90 
91  private function handleParameters(AbstractMedia $media): AbstractMedia
92  {
93  if (!$media instanceof AbstractMediaWithPath) {
94  return $media;
95  }
96  if (!$this->append_resource_version && !$this->strip_queries) {
97  return $media;
98  }
99  $content = $media->getContent();
100  if ($this->isContentDataUri($content)) {
101  return $media;
102  }
103 
104  $content = $media->getContent();
105 
106  $content_array = explode('?', $content);
107  if ($this->strip_queries) {
108  $content = $content_array[0] ?? $content;
109  }
110  if ($this->append_resource_version) {
111  if ($this->hasContentParameters($content)) {
112  $content = rtrim($content, "&") . "&version=" . $this->resource_version;
113  } else {
114  $content = rtrim($content, "?") . "?version=" . $this->resource_version;
115  }
116  }
117 
118  return $media->withContent($content);
119  }
120 
121  protected function isContentDataUri(string $content): bool
122  {
123  // regex pattern matches if a string follows the data uri syntax.
124  // https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs#syntax
125 
126  return (bool) preg_match('/^(data:)([a-z\/]*)((;base64)?)(,?)([A-z0-9=\/\+]*)$/', $content);
127  }
128 
129  protected function hasContentParameters(string $content): bool
130  {
131  return (str_contains($content, "?"));
132  }
133 
137  public function getItemsInOrderOfDelivery(): array
138  {
139  return iterator_to_array($this->getItems());
140  }
141 
146  protected function stripPath(string $path): string
147  {
148  if (str_contains($path, '?')) {
149  return parse_url($path, PHP_URL_PATH);
150  }
151 
152  return $path;
153  }
154 }
__construct(protected string $resource_version, protected bool $append_resource_version=false, protected bool $strip_queries=true, protected bool $allow_external=false, protected bool $allow_non_existing=false,)
$url
Definition: shib_logout.php:66
$path
Definition: ltiservices.php:29