Elgg  Version master
ExternalFiles.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Assets;
4 
7 use Elgg\Config;
10 
18 
19  protected array $files = [];
20 
26  protected $sri;
27 
37  public function __construct(
38  protected Config $config,
39  protected Urls $urls,
40  protected ViewsService $views,
41  protected SimpleCache $simpleCache,
42  protected ServerCache $serverCache
43  ) {
44  }
45 
56  public function register(string $type, string $name, string $url, string $location): bool {
57  $name = trim(strtolower($name));
58  if (empty($name) || empty($url)) {
59  return false;
60  }
61 
62  $url = $this->urls->normalizeUrl($url);
63 
64  $this->setupType($type);
65 
66  $item = elgg_extract($name, $this->files[$type]);
67 
68  if ($item) {
69  // updating a registered item
70  // don't update loaded because it could already be set
71  $item->url = $url;
72  $item->location = $location;
73  } else {
74  $item = (object) [
75  'loaded' => false,
76  'url' => $url,
77  'location' => $location,
78  ];
79  }
80 
81  $this->files[$type][$name] = $item;
82 
83  return true;
84  }
85 
94  public function unregister(string $type, string $name): bool {
95  $this->setupType($type);
96 
97  $name = trim(strtolower($name));
98 
99  if (!isset($this->files[$type][$name])) {
100  return false;
101  }
102 
103  unset($this->files[$type][$name]);
104  return true;
105  }
106 
115  public function load(string $type, string $name): void {
116  $this->setupType($type);
117 
118  $name = trim(strtolower($name));
119 
120  $item = elgg_extract($name, $this->files[$type]);
121 
122  if ($item) {
123  // update a registered item
124  $item->loaded = true;
125  } else {
126  $item = (object) [
127  'loaded' => true,
128  'url' => '',
129  'location' => '',
130  ];
131  if ($this->views->viewExists($name)) {
132  $item->url = $this->simpleCache->getUrl($name);
133  $item->location = ($type === 'js') ? 'footer' : 'head';
134  }
135  }
136 
137  $this->files[$type][$name] = $item;
138  }
139 
148  public function getLoadedResources(string $type, string $location): array {
149  if (!isset($this->files[$type])) {
150  return [];
151  }
152 
153  $items = $this->files[$type];
154 
155  // only return loaded files for this location
156  $items = array_filter($items, function($v) use ($location) {
157  return $v->loaded == true && $v->location == $location;
158  });
159 
160  $cache_ts = $this->config->lastcache;
161  $cache_url = $this->config->wwwroot . "cache/{$cache_ts}/default/";
162 
163  // check if SRI data is available
164  array_walk($items, function(&$v, $k) use ($type, $cache_url) {
165  $view = str_replace($cache_url, '', $v->url);
166  $v->integrity = $this->getSubResourceIntegrity($type, $view);
167  });
168 
169  return $items;
170  }
171 
177  public function reset(): void {
178  $this->files = [];
179  }
180 
187  protected function setupType(string $type): void {
188  if (!isset($this->files[$type])) {
189  $this->files[$type] = [];
190  }
191  }
192 
200  protected function getSubResourceIntegrity(string $type, string $resource): ?string {
201  if (!$this->config->subresource_integrity_enabled) {
202  return null;
203  }
204 
205  if (!isset($this->sri)) {
206  $this->sri = $this->serverCache->load('sri') ?? [];
207  }
208 
209  return $this->sri[$type][$resource] ?? null;
210  }
211 }
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
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:256
$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