Elgg  Version 2.3
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()->redirects->setLastForwardFrom();
61  system_message(elgg_echo('loggedinrequired'));
62  forward('/login', 'login');
63  }
64 }
65 
73 function gatekeeper() {
75 }
76 
85 
86  if (!elgg_is_admin_logged_in()) {
87  _elgg_services()->redirects->setLastForwardFrom();
88  register_error(elgg_echo('adminrequired'));
89  forward('', 'admin');
90  }
91 }
92 
100 function 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()->redirects->setLastForwardFrom();
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) {
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  'forward' => $forward,
218  ];
219  if (!elgg_trigger_plugin_hook('gatekeeper', $hook_type, $hook_params, true)) {
220  if ($forward) {
221  forward('', '403');
222  } else {
223  return false;
224  }
225  }
226 
227  return true;
228 }
229 
238  if (!elgg_is_xhr()) {
239  register_error(_elgg_services()->translator->translate('ajax:not_is_xhr'));
240  forward(null, '400');
241  }
242 }
243 
250  return elgg_ok_response(elgg_view_resource('index'));
251 }
252 
268  elgg_deprecated_notice(__FUNCTION__ . ' is deprecated. Error pages are drawn by resource views without "forward" hook.', '2.3');
269 
270  // This draws an error page, and sometimes there's another 40* forward() call made during that
271  // process (usually due to the pagesetup event). We want to allow the 2nd call to pass through,
272  // but draw the appropriate page for the first call.
273 
274  static $vars;
275  if ($vars === null) {
276  // keep first vars for error page
277  $vars = [
278  'type' => $type,
279  'params' => $params,
280  ];
281  }
282 
283  static $calls = 0;
284  $calls++;
285  if ($calls < 3) {
286  echo elgg_view_resource('error', $vars);
287  exit;
288  }
289 
290  // uh oh, may be infinite loop
291  register_error(elgg_echo('error:404:content'));
292  header('Location: ' . elgg_get_site_url());
293  exit;
294 }
295 
310 function elgg_ok_response($content = '', $message = '', $forward_url = null, $status_code = ELGG_HTTP_OK) {
311  if ($message) {
313  }
314  return new \Elgg\Http\OkResponse($content, $status_code, $forward_url);
315 
316 }
317 
333 function elgg_error_response($error = '', $forward_url = REFERRER, $status_code = ELGG_HTTP_OK) {
334  if ($error) {
336  }
337  return new \Elgg\Http\ErrorResponse($error, $status_code, $forward_url);
338 }
339 
354  return new Elgg\Http\RedirectResponse($forward_url, $status_code);
355 }
356 
364  elgg_register_page_handler('', 'elgg_front_page_handler');
365 }
366 
367 return function(\Elgg\EventsService $events, \Elgg\HooksRegistrationService $hooks) {
368  $events->registerHandler('init', 'system', '_elgg_page_handler_init');
369 };
_elgg_page_handler_init()
Initializes the page handler/routing system.
if(!$owner||!($owner instanceof ElggUser)||!$owner->canEdit()) $error
Definition: upload.php:14
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:237
const ELGG_HTTP_FOUND
Definition: elgglib.php:2143
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:510
elgg_redirect_response($forward_url=REFERRER, $status_code=ELGG_HTTP_FOUND)
Prepare a silent redirect response to be returned by a page or an action handler. ...
elgg_group_gatekeeper($forward=true, $group_guid=null)
May the current user access item(s) on this page? If the page owner is a group, membership, visibility, and logged in status are taken into account.
$subtype
Definition: delete.php:28
admin_gatekeeper()
Alias of elgg_admin_gatekeeper()
elgg_entity_exists($guid)
Does an entity exist?
Definition: entities.php:221
$guid
Removes an admin notice.
elgg forward
Meant to mimic the php forward() function by simply redirecting the user to another page...
Definition: elgglib.js:425
$vars['entity']
$params
Definition: login.php:72
const REFERRER
Definition: elgglib.php:2113
elgg_entity_gatekeeper($guid, $type=null, $subtype=null, $forward=true)
Can the viewer see this entity?
elgg_error_page_handler($hook, $type, $result, $params)
Serve an error page.
group_gatekeeper($forward=true, $page_owner_guid=null)
May the current user access item(s) on this page? If the page owner is a group, membership, visibility, and logged in status are taken into account.
elgg_gatekeeper()
Used at the top of a page to mark it as logged in users only.
Definition: pagehandler.php:58
elgg_instanceof($entity, $type=null, $subtype=null, $class=null)
Checks if $entity is an and optionally for type and subtype.
Definition: entities.php:736
elgg_error_response($error= '', $forward_url=REFERRER, $status_code=ELGG_HTTP_OK)
Prepare an error response to be returned by a page or an action handler.
$forward
Definition: delete.php:35
elgg echo
Translates a string.
Definition: languages.js:48
elgg_trigger_plugin_hook($hook, $type, $params=null, $returnvalue=null)
Definition: elgglib.php:826
elgg_register_page_handler($identifier, $function)
Registers a page handler for a particular identifier.
Definition: pagehandler.php:34
elgg_ajax_gatekeeper()
Require that the current request be an XHR.
elgg_deprecated_notice($msg, $dep_version, $backtrace_level=1)
Log a notice about deprecated use of a function, view, etc.
Definition: elgglib.php:1098
elgg_get_site_url($site_guid=0)
Get the URL for the current (or specified) site.
clearfix elgg elgg elgg elgg page header
Definition: admin.css.php:127
_elgg_services(\Elgg\Di\ServiceProvider $services=null)
Get the global service provider.
Definition: autoloader.php:17
Redirect response builder.
elgg system_message
Wrapper function for system_messages.
Definition: elgglib.js:390
elgg_unregister_page_handler($identifier)
Unregister a page handler for an identifier.
Definition: pagehandler.php:48
elgg_ok_response($content= '', $message= '', $forward_url=null, $status_code=ELGG_HTTP_OK)
Prepares a successful response to be returned by a page or an action handler.
static factory($container_guid, $use_cache=true)
Determine visibility of items within a container for the current user.
elgg_admin_gatekeeper()
Used at the top of a page to mark it as admin only.
Definition: pagehandler.php:83
elgg register_error
Wrapper function for system_messages.
Definition: elgglib.js:399
$content
Set robots.txt action.
Definition: set_robots.php:6
const ELGG_HTTP_OK
Definition: elgglib.php:2131
$entity
Definition: delete.php:7
$page_owner_guid
Definition: add.php:9
exit
Definition: autoloader.php:34
elgg_get_page_owner_guid($guid=0)
Gets the guid of the entity that owns the current page.
Definition: pageowner.php:22
gatekeeper()
Alias of elgg_gatekeeper()
Definition: pagehandler.php:73
elgg_front_page_handler()
Front page handler.
get_entity($guid)
Loads and returns an entity object from a guid.
Definition: entities.php:204
if(!$display_name) $type
Definition: delete.php:27