ILIAS  trunk Revision v12.0_alpha-1540-g00f839d5fa1
AbstractCollection.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
23use Iterator;
25
29abstract class AbstractCollection
30{
31 public const VERSION_PARAMETER = "_v";
35 protected array $items = [];
39 protected array $version_parameter_filters = [];
40
44 public function __construct(
45 protected string $resource_version,
46 protected bool $append_resource_version = false,
47 protected bool $strip_queries = true,
48 protected bool $allow_external = false,
49 protected bool $allow_non_existing = false,
51 ) {
52 foreach ($version_parameter_filters as $filter) {
53 $this->addVersionParameterFilter($filter);
54 }
55 }
56
58 {
59 $this->version_parameter_filters[] = $filter;
60 }
61
62 public function clear(): void
63 {
64 $this->items = [];
65 }
66
67 protected function isURI(string $content): bool
68 {
69 if (realpath($content) !== false) {
70 return false;
71 }
72
73 try {
74 new URI($content);
75 return true;
76 } catch (\Throwable) {
77 return false;
78 }
79 }
80
81 protected function isExternalURI(string $content): bool
82 {
83 if (!$this->isURI($content)) {
84 return false;
85 }
86
87 try {
88 if ((new URI($content))->getHost() !== (new URI(ILIAS_HTTP_PATH))->getHost()) {
89 return true;
90 }
91 } catch (\Throwable) {
92 return false;
93 }
94
95 return false;
96 }
97
101 public function getItems(): Iterator
102 {
103 foreach ($this->items as $path => $item) {
104 yield $path => $this->handleParameters($item);
105 }
106 }
107
109 {
110 if (!$media instanceof AbstractMediaWithPath) {
111 return $media;
112 }
113 if (!$this->append_resource_version && !$this->strip_queries) {
114 return $media;
115 }
116 $content = $media->getContent();
117 if ($this->isContentDataUri($content)) {
118 return $media;
119 }
120
121 $content = $media->getContent();
122
123 $content_array = explode('?', $content);
124 if ($this->strip_queries) {
125 $content = $content_array[0] ?? $content;
126 }
127 if ($this->append_resource_version && $this->passesVersionParameterFilters($content)) {
128 if ($this->hasContentParameters($content)) {
129 $content = rtrim($content, "&") . "&" . self::VERSION_PARAMETER . "=" . $this->resource_version;
130 } else {
131 $content = rtrim($content, "?") . "?" . self::VERSION_PARAMETER . "=" . $this->resource_version;
132 }
133 }
134
135 return $media->withContent($content);
136 }
137
138 protected function isContentDataUri(string $content): bool
139 {
140 // regex pattern matches if a string follows the data uri syntax.
141 // https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs#syntax
142
143 return (bool) preg_match('/^(data:)([a-z\/]*)((;base64)?)(,?)([A-z0-9=\/\+]*)$/', $content);
144 }
145
146 protected function hasContentParameters(string $content): bool
147 {
148 return (str_contains($content, "?"));
149 }
150
151 protected function passesVersionParameterFilters(string $content): bool
152 {
153 foreach ($this->version_parameter_filters as $filter) {
154 if (!$filter->shouldAppend($content)) {
155 return false;
156 }
157 }
158 return true;
159 }
160
164 public function getItemsInOrderOfDelivery(): array
165 {
166 return iterator_to_array($this->getItems());
167 }
168
169 protected function stripPath(string $path): string
170 {
171 if (str_contains($path, '?')) {
172 return parse_url($path, PHP_URL_PATH);
173 }
174
175 return $path;
176 }
177}
The scope of this class is split ilias-conform URI's into components.
Definition: URI.php:35
__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, array $version_parameter_filters=[],)
$path
Definition: ltiservices.php:30