Elgg  Version 7.1
WalledGarden.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Router\Middleware;
4 
6 use Elgg\Request;
7 
11 class WalledGarden {
12 
21  public function __invoke(Request $request) {
22  if ($request->elgg()->session_manager->isLoggedIn()) {
23  return;
24  }
25 
26  if (!$request->elgg()->config->walled_garden) {
27  return;
28  }
29 
30  $url = $request->getURL();
31 
32  if ($this->isPublicPage($url)) {
33  return;
34  }
35 
36  _elgg_services()->redirects->setLastForwardFrom();
37 
38  throw new WalledGardenException();
39  }
40 
51  protected function isPublicPage(string $url): bool {
52  $parts = parse_url($url);
53  unset($parts['query']);
54  unset($parts['fragment']);
55  $url = elgg_http_build_url($parts);
56  $url = rtrim($url, '/') . '/';
57 
58  $site_url = _elgg_services()->config->wwwroot;
59 
60  if ($url == $site_url) {
61  // always allow index page
62  return true;
63  }
64 
65  // default public pages
66  $defaults = [
67  'ajax/view/languages.js',
68  'css/.*',
69  'js/.*',
70  'cache/[0-9]+/\w+/.*',
71  'serve-file/.*',
72  ];
73 
74  $params = [
75  'url' => $url,
76  ];
77 
78  $public_routes = _elgg_services()->events->triggerResults('public_pages', 'walled_garden', $params, $defaults);
79 
80  $site_url = preg_quote($site_url);
81  foreach ($public_routes as $public_route) {
82  $pattern = "`^{$site_url}{$public_route}/*$`i";
83  if (preg_match($pattern, $url)) {
84  return true;
85  }
86  }
87 
88  // non-public page
89  return false;
90  }
91 }
$params
Saves global plugin settings.
Definition: save.php:13
Thrown when walled garden gatekeeper prevents access.
Request container.
Definition: Request.php:12
Protects a route from non-authenticated users in a walled garden mode.
isPublicPage(string $url)
Checks if the page should be allowed to be served in a walled garden mode.
__invoke(Request $request)
Gatekeeper.
foreach($plugin_guids as $guid) if(empty($deactivated_plugins)) $url
Definition: deactivate.php:39
elgg_http_build_url(array $parts, bool $html_encode=true)
Builds a URL from the a parts array like one returned by parse_url().
Definition: elgglib.php:123
_elgg_services()
Get the global service provider.
Definition: elgglib.php:347
$defaults
Generic entity header upload helper.
Definition: header.php:6
$request
Definition: livesearch.php:12
$site_url
Definition: upgrade.php:3