Elgg  Version 2.2
 All Classes Namespaces Files Functions Variables Pages
pagehandler.php
Go to the documentation of this file.
1 <?php
34 function elgg_register_page_handler($identifier, $function) {
35  return _elgg_services()->router->registerPageHandler($identifier, $function);
36 }
37 
48 function elgg_unregister_page_handler($identifier) {
49  _elgg_services()->router->unregisterPageHandler($identifier);
50 }
51 
58 function elgg_gatekeeper() {
59  if (!elgg_is_logged_in()) {
60  _elgg_services()->session->set('last_forward_from', current_page_url());
61  system_message(elgg_echo('loggedinrequired'));
62  forward('/login', 'login');
63  }
64 }
65 
73 function gatekeeper() {
74  elgg_gatekeeper();
75 }
76 
83 function elgg_admin_gatekeeper() {
84  elgg_gatekeeper();
85 
86  if (!elgg_is_admin_logged_in()) {
87  _elgg_services()->session->set('last_forward_from', current_page_url());
88  register_error(elgg_echo('adminrequired'));
89  forward('', 'admin');
90  }
91 }
92 
100 function admin_gatekeeper() {
101  elgg_admin_gatekeeper();
102 }
103 
117 function elgg_group_gatekeeper($forward = true, $group_guid = null) {
118  if (null === $group_guid) {
119  $group_guid = elgg_get_page_owner_guid();
120  }
121 
122  if (!$group_guid) {
123  return true;
124  }
125 
126  // this handles non-groups and invisible groups
127  $visibility = \Elgg\GroupItemVisibility::factory($group_guid);
128 
129  if (!$visibility->shouldHideItems) {
130  return true;
131  }
132  if ($forward) {
133  // only forward to group if user can see it
134  $group = get_entity($group_guid);
135  $forward_url = $group ? $group->getURL() : '';
136 
137  if (!elgg_is_logged_in()) {
138  _elgg_services()->session->set('last_forward_from', current_page_url());
139  $forward_reason = 'login';
140  } else {
141  $forward_reason = 'member';
142  }
143 
144  $msg_keys = array(
145  'non_member' => 'membershiprequired',
146  'logged_out' => 'loggedinrequired',
147  'no_access' => 'noaccess',
148  );
149  register_error(elgg_echo($msg_keys[$visibility->reasonHidden]));
150  forward($forward_url, $forward_reason);
151  }
152 
153  return false;
154 }
155 
168 function group_gatekeeper($forward = true, $page_owner_guid = null) {
169  return elgg_group_gatekeeper($forward, $page_owner_guid);
170 }
171 
187 function elgg_entity_gatekeeper($guid, $type = null, $subtype = null, $forward = true) {
189  if (!$entity && $forward) {
190  if (!elgg_entity_exists($guid)) {
191  // entity doesn't exist
192  forward('', '404');
193  } elseif (!elgg_is_logged_in()) {
194  // entity requires at least a logged in user
195  elgg_gatekeeper();
196  } else {
197  // user is logged in but still does not have access to it
198  register_error(elgg_echo('limited_access'));
199  forward();
200  }
201  } else if (!$entity) {
202  return false;
203  }
204 
206  // entity is of wrong type/subtype
207  if ($forward) {
208  forward('', '404');
209  } else {
210  return false;
211  }
212  }
213 
214  $hook_type = "{$entity->getType()}:{$entity->getSubtype()}";
215  $hook_params = [
216  'entity' => $entity,
217  ];
218  if (!elgg_trigger_plugin_hook('gatekeeper', $hook_type, $hook_params, true)) {
219  if ($forward) {
220  forward('', '403');
221  } else {
222  return false;
223  }
224  }
225 
226  return true;
227 }
228 
236 function elgg_ajax_gatekeeper() {
237  if (!elgg_is_xhr()) {
238  register_error(_elgg_services()->translator->translate('ajax:not_is_xhr'));
239  forward(null, '400');
240  }
241 }
242 
248 function elgg_front_page_handler() {
249  echo elgg_view_resource('index');
250  return true;
251 }
252 
266 function elgg_error_page_handler($hook, $type, $result, $params) {
267  // This draws an error page, and sometimes there's another 40* forward() call made during that
268  // process (usually due to the pagesetup event). We want to allow the 2nd call to pass through,
269  // but draw the appropriate page for the first call.
270 
271  static $vars;
272  if ($vars === null) {
273  // keep first vars for error page
274  $vars = [
275  'type' => $type,
276  'params' => $params,
277  ];
278  }
279 
280  static $calls = 0;
281  $calls++;
282  if ($calls < 3) {
283  echo elgg_view_resource('error', $vars);
284  exit;
285  }
286 
287  // uh oh, may be infinite loop
288  register_error(elgg_echo('error:404:content'));
289  header('Location: ' . elgg_get_site_url());
290  exit;
291 }
292 
299 function _elgg_page_handler_init() {
300  elgg_register_page_handler('', 'elgg_front_page_handler');
301  // Registered at 600 so that plugins can register at the default 500 and get to run first
302  elgg_register_plugin_hook_handler('forward', '400', 'elgg_error_page_handler', 600);
303  elgg_register_plugin_hook_handler('forward', '403', 'elgg_error_page_handler', 600);
304  elgg_register_plugin_hook_handler('forward', '404', 'elgg_error_page_handler', 600);
305 }
306 
307 return function(\Elgg\EventsService $events, \Elgg\HooksRegistrationService $hooks) {
308  $events->registerHandler('init', 'system', '_elgg_page_handler_init');
309 };
elgg_is_logged_in()
Returns whether or not the user is currently logged in.
Definition: sessions.php:51
elgg_is_xhr()
Checks whether the request was requested via ajax.
Definition: actions.php:226
if(!$entity->delete()) $forward_url
Definition: delete.php:37
elgg_is_admin_logged_in()
Returns whether or not the viewer is currently logged in and an admin user.
Definition: sessions.php:60
elgg_echo($message_key, $args=array(), $language="")
Given a message key, returns an appropriately translated full-text string.
Definition: languages.php:21
elgg_view_resource($name, array $vars=[])
Render a resource view.
Definition: views.php:500
$subtype
Definition: delete.php:28
current_page_url()
Returns the current page's complete URL.
Definition: input.php:65
elgg_entity_exists($guid)
Does an entity exist?
Definition: entities.php:221
$guid
Removes an admin notice.
elgg_register_plugin_hook_handler($hook, $type, $callback, $priority=500)
Definition: elgglib.php:715
$vars['entity']
register_error($error)
Display an error on next page load.
Definition: elgglib.php:452
$params
Definition: login.php:72
elgg_instanceof($entity, $type=null, $subtype=null, $class=null)
Checks if $entity is an and optionally for type and subtype.
Definition: entities.php:716
$forward
Definition: delete.php:35
elgg_trigger_plugin_hook($hook, $type, $params=null, $returnvalue=null)
Definition: elgglib.php:801
elgg_get_site_url($site_guid=0)
Get the URL for the current (or specified) site.
_elgg_services(\Elgg\Di\ServiceProvider $services=null)
Get the global service provider.
Definition: autoloader.php:17
static factory($container_guid, $use_cache=true)
Determine visibility of items within a container for the current user.
forward($location="", $reason= 'system')
Forward to $location.
Definition: elgglib.php:93
$entity
Definition: delete.php:7
$page_owner_guid
Definition: add.php:9
exit
Definition: autoloader.php:34
system_message($message)
Display a system message on next page load.
Definition: elgglib.php:438
elgg_get_page_owner_guid($guid=0)
Gets the guid of the entity that owns the current page.
Definition: pageowner.php:22
get_entity($guid)
Loads and returns an entity object from a guid.
Definition: entities.php:204
if(!$display_name) $type
Definition: delete.php:27