Elgg  Version 5.1
ExternalFiles.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Assets;
4 
7 use Elgg\Config;
10 
18 
22  protected $files = [];
23 
29  protected $sri;
30 
34  protected $config;
35 
39  protected $urls;
40 
44  protected $views;
45 
49  protected $simpleCache;
50 
54  protected $serverCache;
55 
66  $this->config = $config;
67  $this->urls = $urls;
68  $this->views = $views;
69  $this->simpleCache = $simpleCache;
70  $this->serverCache = $serverCache;
71  }
72 
83  public function register(string $type, string $name, string $url, string $location): bool {
84  $name = trim(strtolower($name));
85  if (empty($name) || empty($url)) {
86  return false;
87  }
88 
89  $url = $this->urls->normalizeUrl($url);
90 
91  $this->setupType($type);
92 
93  $item = elgg_extract($name, $this->files[$type]);
94 
95  if ($item) {
96  // updating a registered item
97  // don't update loaded because it could already be set
98  $item->url = $url;
99  $item->location = $location;
100  } else {
101  $item = (object) [
102  'loaded' => false,
103  'url' => $url,
104  'location' => $location,
105  ];
106  }
107 
108  $this->files[$type][$name] = $item;
109 
110  return true;
111  }
112 
121  public function unregister(string $type, string $name): bool {
122  $this->setupType($type);
123 
124  $name = trim(strtolower($name));
125 
126  if (!isset($this->files[$type][$name])) {
127  return false;
128  }
129 
130  unset($this->files[$type][$name]);
131  return true;
132  }
133 
142  public function load(string $type, string $name): void {
143  $this->setupType($type);
144 
145  $name = trim(strtolower($name));
146 
147  $item = elgg_extract($name, $this->files[$type]);
148 
149  if ($item) {
150  // update a registered item
151  $item->loaded = true;
152  } else {
153  $item = (object) [
154  'loaded' => true,
155  'url' => '',
156  'location' => '',
157  ];
158  if ($this->views->viewExists($name)) {
159  $item->url = $this->simpleCache->getUrl($name);
160  $item->location = ($type === 'js') ? 'footer' : 'head';
161  }
162  }
163 
164  $this->files[$type][$name] = $item;
165  }
166 
175  public function getLoadedResources(string $type, string $location): array {
176  if (!isset($this->files[$type])) {
177  return [];
178  }
179 
180  $items = $this->files[$type];
181 
182  // only return loaded files for this location
183  $items = array_filter($items, function($v) use ($location) {
184  return $v->loaded == true && $v->location == $location;
185  });
186 
187  $cache_ts = $this->config->lastcache;
188  $cache_url = $this->config->wwwroot . "cache/{$cache_ts}/default/";
189 
190  // check if SRI data is available
191  array_walk($items, function(&$v, $k) use ($type, $cache_url) {
192  $view = str_replace($cache_url, '', $v->url);
193  $v->integrity = $this->getSubResourceIntegrity($type, $view);
194  });
195 
196  return $items;
197  }
198 
204  public function reset(): void {
205  $this->files = [];
206  }
207 
214  protected function setupType(string $type): void {
215  if (!isset($this->files[$type])) {
216  $this->files[$type] = [];
217  }
218  }
219 
227  protected function getSubResourceIntegrity(string $type, string $resource): ?string {
228  if (!$this->config->subresource_integrity_enabled) {
229  return null;
230  }
231 
232  if (!isset($this->sri)) {
233  $this->sri = $this->serverCache->load('sri') ?? [];
234  }
235 
236  return $this->sri[$type][$resource] ?? null;
237  }
238 }
if(!$items) $item
Definition: delete.php:13
if(!$user||!$user->canDelete()) $name
Definition: delete.php:22
c Accompany it with the information you received as to the offer to distribute corresponding source complete source code means all the source code for all modules it plus any associated interface definition plus the scripts used to control compilation and installation of the executable as a special the source code distributed need not include anything that is normally and so on of the operating system on which the executable unless that component itself accompanies the executable If distribution of executable or object code is made by offering access to copy from a designated then offering equivalent access to copy the source code from the same place counts as distribution of the source even though third parties are not compelled to copy the source along with the object code You may not or distribute the Program except as expressly provided under this License Any attempt otherwise to sublicense or distribute the Program is void
Definition: LICENSE.txt:215
$type
Definition: delete.php:22
$resource
reset()
Unregister all files.
$items
Definition: delete.php:8
elgg_extract($key, $array, $default=null, bool $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
Definition: elgglib.php:254
load(string $type, string $name)
Load an external resource for use on this page.
c Accompany it with the information you received as to the offer to distribute corresponding source complete source code means all the source code for all modules it plus any associated interface definition files
Definition: LICENSE.txt:210
if(!empty($avatar)&&!$avatar->isValid()) elseif(empty($avatar)) if(!$owner->saveIconFromUploadedFile('avatar')) if(!elgg_trigger_event('profileiconupdate', $owner->type, $owner)) $view
Definition: upload.php:39
unregister(string $type, string $name)
Unregister an external file.
Views service.
setupType(string $type)
Bootstraps the externals data structure.
getSubResourceIntegrity(string $type, string $resource)
Returns the integrity related to the resource file.
__construct(Config $config, Urls $urls, ViewsService $views, SimpleCache $simpleCache, SystemCache $serverCache)
Constructor.
$location
Definition: member.php:29
Simple cache service.
Definition: SimpleCache.php:15
foreach($plugin_guids as $guid) if(empty($deactivated_plugins)) $url
Definition: deactivate.php:39
getLoadedResources(string $type, string $location)
Get external resource descriptors.
External files service.
Create, sanitize and compare urls.
Definition: Urls.php:11