Elgg  Version master
ExternalFiles.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Assets;
4 
7 use Elgg\Config;
11 
19 
20  protected array $files = [];
21 
27  protected $sri;
28 
38  public function __construct(
39  protected Config $config,
40  protected Urls $urls,
41  protected ViewsService $views,
42  protected SimpleCache $simpleCache,
43  protected ServerCache $serverCache
44  ) {
45  }
46 
58  public function register(string $type, string $name, string $url, string $location): void {
59  $name = trim(strtolower($name));
60  if (empty($name) || empty($url)) {
61  throw new InvalidArgumentException('$name and $url are not allowed to be empty');
62  }
63 
64  $url = $this->urls->normalizeUrl($url);
65 
66  $this->setupType($type);
67 
68  $item = elgg_extract($name, $this->files[$type]);
69 
70  if ($item) {
71  // updating a registered item
72  // don't update loaded because it could already be set
73  $item->url = $url;
74  $item->location = $location;
75  } else {
76  $item = (object) [
77  'loaded' => false,
78  'url' => $url,
79  'location' => $location,
80  ];
81  }
82 
83  $this->files[$type][$name] = $item;
84  }
85 
94  public function unregister(string $type, string $name): void {
95  $this->setupType($type);
96 
97  $name = trim(strtolower($name));
98 
99  unset($this->files[$type][$name]);
100  }
101 
110  public function load(string $type, string $name): void {
111  $this->setupType($type);
112 
113  $name = trim(strtolower($name));
114 
115  $item = elgg_extract($name, $this->files[$type]);
116 
117  if ($item) {
118  // update a registered item
119  $item->loaded = true;
120  } else {
121  $item = (object) [
122  'loaded' => true,
123  'url' => '',
124  'location' => '',
125  ];
126  if ($this->views->viewExists($name)) {
127  $item->url = $this->simpleCache->getUrl($name);
128  $item->location = ($type === 'js') ? 'footer' : 'head';
129  }
130  }
131 
132  $this->files[$type][$name] = $item;
133  }
134 
143  public function getLoadedResources(string $type, string $location): array {
144  if (!isset($this->files[$type])) {
145  return [];
146  }
147 
148  $items = $this->files[$type];
149 
150  // only return loaded files for this location
151  $items = array_filter($items, function($v) use ($location) {
152  return $v->loaded == true && $v->location == $location;
153  });
154 
155  $cache_ts = $this->config->lastcache;
156  $cache_url = $this->config->wwwroot . "cache/{$cache_ts}/default/";
157 
158  // check if SRI data is available
159  array_walk($items, function(&$v, $k) use ($type, $cache_url) {
160  $view = str_replace($cache_url, '', $v->url);
161  $v->integrity = $this->getSubResourceIntegrity($type, $view);
162  });
163 
164  return $items;
165  }
166 
172  public function reset(): void {
173  $this->files = [];
174  }
175 
182  protected function setupType(string $type): void {
183  if (!isset($this->files[$type])) {
184  $this->files[$type] = [];
185  }
186  }
187 
195  public function getSubResourceIntegrity(string $type, string $resource): ?string {
196  if (!$this->config->subresource_integrity_enabled) {
197  return null;
198  }
199 
200  if (!isset($this->sri)) {
201  $this->sri = $this->serverCache->load('sri') ?? [];
202  }
203 
204  return $this->sri[$type][$resource] ?? null;
205  }
206 }
if(! $user||! $user->canDelete()) $name
Definition: delete.php:22
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
$type
Definition: delete.php:21
if(! $items) $item
Definition: delete.php:13
$items
Definition: delete.php:8
External files service.
unregister(string $type, string $name)
Unregister an external file.
__construct(protected Config $config, protected Urls $urls, protected ViewsService $views, protected SimpleCache $simpleCache, protected ServerCache $serverCache)
Constructor.
load(string $type, string $name)
Load an external resource for use on this page.
setupType(string $type)
Bootstraps the externals data structure.
getLoadedResources(string $type, string $location)
Get external resource descriptors.
getSubResourceIntegrity(string $type, string $resource)
Returns the integrity related to the resource file.
reset()
Unregister all files.
Simple cache service.
Definition: SimpleCache.php:15
Exception thrown if an argument is not of the expected type.
Create, sanitize and compare urls.
Definition: Urls.php:11
Views service.
foreach($plugin_guids as $guid) if(empty($deactivated_plugins)) $url
Definition: deactivate.php:39
$config
Advanced site settings, debugging section.
Definition: debugging.php:6
$views
Definition: item.php:17
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:246
$resource
$location
Definition: member.php:29
if(parse_url(elgg_get_site_url(), PHP_URL_PATH) !=='/') if(file_exists(elgg_get_root_path() . 'robots.txt'))
Set robots.txt.
Definition: robots.php:10