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 SystemCache $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(!$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
$config
Advanced site settings, debugging section.
Definition: debugging.php:6
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.
$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
__construct(protected Config $config, protected Urls $urls, protected ViewsService $views, protected SimpleCache $simpleCache, protected SystemCache $serverCache)
Constructor.
getLoadedResources(string $type, string $location)
Get external resource descriptors.
External files service.
Create, sanitize and compare urls.
Definition: Urls.php:11
$views
Definition: item.php:17