20 const VIEW_HOOK =
'view';
21 const VIEW_VARS_HOOK =
'view_vars';
22 const OUTPUT_KEY =
'__view_output';
23 const BASE_VIEW_PRIORITY = 500;
28 protected array $file_exists_cache = [];
33 protected array $locations = [];
40 protected array $overrides = [];
50 protected array $fallbacks = [];
54 protected bool $locations_loaded_from_cache =
false;
81 $this->viewtype = null;
86 if ($this->isValidViewtype($viewtype)) {
101 if (!isset($this->viewtype)) {
102 $this->viewtype = $this->resolveViewtype();
114 if ($this->request) {
115 $view = $this->request->getParam(
'view',
'',
false);
116 if ($this->isValidViewtype(
$view) && !empty($this->locations[
$view])) {
121 $view = (string) $this->config->view;
122 if ($this->isValidViewtype(
$view) && !empty($this->locations[
$view])) {
137 if ($viewtype ===
'') {
155 if ($this->isViewLocationsLoadedFromCache()) {
164 if (!is_file($file)) {
168 $spec = Includer::includeFile($file);
169 if (is_array($spec)) {
171 if ($this->config->font_awesome_zip) {
175 $this->mergeViewsSpec($spec);
189 $folder = Paths::sanitize($folder);
190 $view_base = Paths::sanitize($view_base,
false);
191 $view_base = $view_base ? $view_base .
'/' : $view_base;
194 $dir = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($folder, \RecursiveDirectoryIterator::SKIP_DOTS));
195 }
catch (\Throwable $t) {
196 $this->
getLogger()->error($t->getMessage());
201 foreach ($dir as $file) {
202 $path = $file->getPath() .
'/' . $file->getBasename(
'.php');
207 $this->setViewLocation(
$view, $viewtype, $file->getPathname());
222 if (!isset($this->locations[$viewtype][$view])) {
253 return in_array($viewtype, $this->fallbacks);
269 $rendered = $this->renderView($view, $vars,
'',
false);
303 public function renderView(
string $view, array
$vars = [],
string $viewtype =
'',
bool $issue_missing_notice = null, array $extensions_tree = []): string {
305 if (str_contains($view,
'..')) {
310 if (in_array($view, $extensions_tree)) {
311 $this->
getLogger()->error(
"View {$view} is detected as an extension of itself. This is not allowed");
316 $extensions_tree[] =
$view;
319 if ($viewtype ===
'' || !$this->isValidViewtype($viewtype)) {
320 $viewtype = $this->getViewtype();
323 if (!isset($issue_missing_notice)) {
324 $issue_missing_notice = $viewtype ===
'default';
328 $vars_event_params = [
333 $vars = $this->events->triggerResults(self::VIEW_VARS_HOOK, $view, $vars_event_params,
$vars);
336 if (isset(
$vars[self::OUTPUT_KEY])) {
337 return (
string)
$vars[self::OUTPUT_KEY];
340 $viewlist = $this->getViewList($view);
343 foreach ($viewlist as
$priority => $view_name) {
344 if (
$priority !== self::BASE_VIEW_PRIORITY) {
346 $content .= $this->renderView($view_name,
$vars, $viewtype, $issue_missing_notice, $extensions_tree);
351 $rendering = $this->renderViewFile($view_name,
$vars, $viewtype, $issue_missing_notice);
352 if ($rendering !==
false) {
358 if ($viewtype !==
'default' && $this->doesViewtypeFallback($viewtype)) {
359 $rendering = $this->renderViewFile($view_name,
$vars,
'default', $issue_missing_notice);
360 if ($rendering !==
false) {
372 return (
string) $this->events->triggerResults(self::VIEW_HOOK, $view,
$params,
$content);
384 if (!isset($this->file_exists_cache[$path])) {
385 $this->file_exists_cache[
$path] = file_exists($path);
388 return $this->file_exists_cache[
$path];
402 $file = $this->findViewFile($view, $viewtype);
404 if ($issue_missing_notice) {
405 $this->
getLogger()->notice(
"{$viewtype}/{$view} view does not exist.");
411 if (pathinfo($file, PATHINFO_EXTENSION) ===
'php') {
418 return ob_get_clean();
419 }
catch (\Exception $e) {
425 return file_get_contents($file);
439 public function viewExists(
string $view,
string $viewtype =
'',
bool $recurse =
true): bool {
445 if ($viewtype ===
'' || !$this->isValidViewtype($viewtype)) {
446 $viewtype = $this->getViewtype();
450 $file = $this->findViewFile($view, $viewtype);
457 if ($recurse && isset($this->
extensions[$view])) {
458 foreach ($this->
extensions[$view] as $view_extension) {
460 if ($this->viewExists($view_extension, $viewtype,
false)) {
467 if ($viewtype !==
'default' && $this->doesViewtypeFallback($viewtype)) {
468 return $this->viewExists($view,
'default');
486 if ($view === $view_extension) {
520 unset($extensions[self::BASE_VIEW_PRIORITY]);
522 $priority = array_search($view_extension, $extensions);
540 $path = Paths::sanitize($path) .
'views/';
543 if (!is_dir($path)) {
548 $dir = new \DirectoryIterator($path);
549 }
catch (\Throwable $t) {
550 $this->
getLogger()->error($t->getMessage());
554 foreach ($dir as $folder) {
555 $folder_name = $folder->getBasename();
556 if (!$folder->isDir() || str_starts_with($folder_name,
'.')) {
560 if (!$this->autoregisterViews(
'', $folder->getPathname(), $folder_name)) {
579 foreach ($spec as $viewtype =>
$list) {
586 if (!
preg_match(
'~^([/\\\\]|[a-zA-Z]\:)~', $path)) {
591 if (str_ends_with(
$view,
'/')) {
593 $this->autoregisterViews(
$view, $path, $viewtype);
595 $this->setViewLocation(
$view, $viewtype, $path);
609 public function listViews(
string $viewtype =
'default'): array {
610 return array_keys($this->locations[$viewtype] ?? []);
619 $cached_overrides = $this->server_cache->load(
'view_overrides');
622 'locations' => $this->locations,
623 'overrides' => is_array($cached_overrides) ? $cached_overrides : $this->overrides,
625 'simplecache' =>
_elgg_services()->simpleCache->getCacheableViews(),
635 if (!$this->server_cache->isEnabled()) {
639 $data = $this->server_cache->
load(
'view_locations');
640 if (!is_array(
$data)) {
644 $this->locations =
$data[
'locations'];
645 $this->locations_loaded_from_cache =
true;
654 if (!$this->server_cache->isEnabled()) {
659 if ($this->isViewLocationsLoadedFromCache()) {
663 if (empty($this->locations)) {
664 $this->server_cache->delete(
'view_locations');
668 $this->server_cache->save(
'view_locations', [
'locations' => $this->locations]);
671 $this->server_cache->save(
'view_overrides', $this->overrides);
681 return $this->locations_loaded_from_cache;
690 $modules = $this->server_cache->load(
'esmodules');
691 if (is_array($modules)) {
696 foreach ($this->locations[
'default'] as
$name =>
$path) {
697 if (!str_ends_with(
$name,
'.mjs')) {
704 $this->server_cache->save(
'esmodules', $modules);
719 $path = strtr($path,
'\\',
'/');
721 if (isset($this->locations[$viewtype][$view]) && $path !== $this->locations[$viewtype][$view]) {
viewExists(string $view, string $viewtype= '', bool $recurse=true)
Returns whether the specified view exists.
$params
Saves global plugin settings.
$paths
We handle here two possible locations of composer-generated autoload file.
if(parse_url(elgg_get_site_url(), PHP_URL_PATH)!== '/') if(file_exists(elgg_get_root_path(). 'robots.txt'))
Set robots.txt.
isValidViewtype(string $viewtype)
Checks if $viewtype is a string suitable for use as a viewtype name.
if(!$user||!$user->canDelete()) $name
listViews(string $viewtype= 'default')
List all views in a viewtype.
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
renderDeprecatedView(string $view, array $vars, string $suggestion, string $version)
Display a view with a deprecation notice.
registerViewsFromPath(string $path)
Register all views in a given path.
cacheConfiguration()
Cache the configuration.
registerCoreViews()
Discover the core views if the system cache did not load.
renderView(string $view, array $vars=[], string $viewtype= '', bool $issue_missing_notice=null, array $extensions_tree=[])
Renders a view.
configureFromCache()
Configure locations from the cache.
$config
Advanced site settings, debugging section.
resolveViewtype()
Resolve the initial viewtype.
if(!$entity instanceof\ElggUser) $data
getInspectorData()
Get inspector data.
trait Loggable
Enables adding a logger.
getViewtype()
Get the viewtype.
unextendView(string $view, string $view_extension)
Unextends a view.
mergeViewsSpec(array $spec)
Merge a specification of absolute view paths.
setViewtype(string $viewtype= '')
Set the viewtype.
if(!empty($avatar)&&!$avatar->isValid()) elseif(empty($avatar)) if(!$owner->saveIconFromUploadedFile('avatar')) if(!elgg_trigger_event('profileiconupdate', $owner->type, $owner)) $view
load(stdClass $row)
Loads attributes from the entities table into the object.
extendView(string $view, string $view_extension, int $priority=501)
Extends a view with another view.
findViewFile(string $view, string $viewtype)
Find the view file.
isViewLocationsLoadedFromCache()
Checks if view_locations have been loaded from cache.
doesViewtypeFallback(string $viewtype)
Checks if a viewtype falls back to default.
getLogger()
Returns logger.
registerViewtypeFallback(string $viewtype)
Register a viewtype to fall back to a default view if a view isn't found for that viewtype...
getViewList(string $view)
Get the views, including extensions, used to render a view.
__construct(protected EventsService $events, protected HttpRequest $request, protected Config $config, protected ServerCache $server_cache)
Constructor.
$content
Set robots.txt action.
_elgg_services()
Get the global service provider.
autoregisterViews(string $view_base, string $folder, string $viewtype)
Auto-registers views from a location.
elgg_get_data_path()
Get the data directory path for this installation, ending with slash.
fileExists(string $path)
Wrapper for file_exists() that caches false results (the stat cache only caches true results)...
getESModules()
Returns an array of names of ES modules detected based on view location.
renderViewFile(string $view, array $vars, string $viewtype, bool $issue_missing_notice)
Includes view PHP or static file.
setViewLocation(string $view, string $viewtype, string $path)
Update the location of a view file.
logDeprecatedMessage(string $message, string $version)
Sends a message about deprecated use of a function, view, etc.