Elgg  Version 1.11
actions.php
Go to the documentation of this file.
1 <?php
19 function _elgg_action_handler(array $segments) {
20  _elgg_services()->actions->execute(implode('/', $segments));
21 }
22 
47 function action($action, $forwarder = "") {
48  _elgg_services()->actions->execute($action, $forwarder);
49 }
50 
85 function elgg_register_action($action, $filename = "", $access = 'logged_in') {
86  return _elgg_services()->actions->register($action, $filename, $access);
87 }
88 
97  return _elgg_services()->actions->unregister($action);
98 }
99 
108  return _elgg_services()->crypto->getHmac($data);
109 }
110 
127 function validate_action_token($visible_errors = true, $token = null, $ts = null) {
128  return _elgg_services()->actions->validateActionToken($visible_errors, $token, $ts);
129 }
130 
146  return _elgg_services()->actions->gatekeeper($action);
147 }
148 
168  return _elgg_services()->actions->generateActionToken($timestamp);
169 }
170 
182 function init_site_secret() {
183  return _elgg_services()->siteSecret->init();
184 }
185 
195 function get_site_secret() {
196  return _elgg_services()->siteSecret->get();
197 }
198 
206  return _elgg_services()->siteSecret->getStrength();
207 }
208 
218  return _elgg_services()->actions->exists($action);
219 }
220 
227 function elgg_is_xhr() {
228  return _elgg_services()->request->isXmlHttpRequest();
229 }
230 
257 function ajax_forward_hook($hook, $type, $reason, $params) {
258  _elgg_services()->actions->ajaxForwardHook($hook, $type, $reason, $params);
259 }
260 
266 function ajax_action_hook() {
267  _elgg_services()->actions->ajaxActionHook();
268 }
269 
276  if (!elgg_is_xhr()) {
277  return false;
278  }
279 
280  $actions = _elgg_services()->actions;
281 
282  // the page's session_token might have expired (not matching __elgg_session in the session), but
283  // we still allow it to be given to validate the tokens in the page.
284  $session_token = get_input('session_token', null, false);
285  $pairs = (array)get_input('pairs', array(), false);
286  $valid_tokens = (object)array();
287  foreach ($pairs as $pair) {
288  list($ts, $token) = explode(',', $pair, 2);
289  if ($actions->validateTokenOwnership($token, $ts, $session_token)) {
290  $valid_tokens->{$token} = true;
291  }
292  }
293 
294  $ts = time();
296  $data = array(
297  'token' => array(
298  '__elgg_ts' => $ts,
299  '__elgg_token' => $token,
300  'logged_in' => elgg_is_logged_in(),
301  ),
302  'valid_tokens' => $valid_tokens,
303  'session_token' => elgg_get_session()->get('__elgg_session'),
304  'user_guid' => elgg_get_logged_in_user_guid(),
305  );
306 
307  header("Content-Type: application/json");
308  echo json_encode($data);
309 
310  return true;
311 }
312 
317 function actions_init() {
318  elgg_register_page_handler('action', '_elgg_action_handler');
319  elgg_register_page_handler('refresh_token', '_elgg_csrf_token_refresh');
320 
321  elgg_register_simplecache_view('js/languages/en');
322 
323  elgg_register_plugin_hook_handler('action', 'all', 'ajax_action_hook');
324  elgg_register_plugin_hook_handler('forward', 'all', 'ajax_forward_hook');
325 }
326 
327 return function(\Elgg\EventsService $events, \Elgg\HooksRegistrationService $hooks) {
328  $events->registerHandler('init', 'system', 'actions_init');
329 };
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:227
get_input($variable, $default=null, $filter_result=true)
Get some input from variables passed submitted through GET or POST.
Definition: input.php:27
action($action, $forwarder="")
Perform an action.
Definition: actions.php:47
action_gatekeeper($action)
Validates the presence of action tokens.
Definition: actions.php:145
elgg_action_exists($action)
Check if an action is registered and its script exists.
Definition: actions.php:217
elgg_register_simplecache_view($view_name)
Registers a view to simple cache.
Definition: cache.php:98
elgg_get_session()
Gets Elgg&#39;s session object.
Definition: sessions.php:23
$data
Definition: opendd.php:13
init_site_secret()
Initialise the site secret (32 bytes: "z" to indicate format + 186-bit key in Base64 URL)...
Definition: actions.php:182
elgg_register_plugin_hook_handler($hook, $type, $callback, $priority=500)
Definition: elgglib.php:703
$timestamp
Definition: date.php:35
$action
$actions
Definition: user_hover.php:12
$params
Definition: login.php:72
generate_action_token($timestamp)
Generate an action token.
Definition: actions.php:167
actions_init()
Initialize some ajaxy actions features private.
Definition: actions.php:317
elgg_build_hmac($data)
Get an HMAC token builder/validator object.
Definition: actions.php:107
_elgg_services()
Definition: autoloader.php:14
elgg echo
Translates a string.
Definition: languages.js:43
elgg_register_page_handler($identifier, $function)
Registers a page handler for a particular identifier.
Definition: pagehandler.php:34
ajax_forward_hook($hook, $type, $reason, $params)
Catch calls to forward() in ajax request and force an exit.
Definition: actions.php:257
$type
Definition: add.php:8
get_site_secret()
Returns the site secret.
Definition: actions.php:195
$token
_elgg_action_handler(array $segments)
Handle a request for an action.
Definition: actions.php:19
validate_action_token($visible_errors=true, $token=null, $ts=null)
Validate an action token.
Definition: actions.php:127
_elgg_get_site_secret_strength()
Get the strength of the site secret.
Definition: actions.php:205
$filename
Definition: crop.php:23
_elgg_csrf_token_refresh()
Send an updated CSRF token, provided the page&#39;s current tokens were not fake.
Definition: actions.php:275
clearfix elgg elgg elgg elgg page header
Definition: admin.php:127
elgg_register_action($action, $filename="", $access= 'logged_in')
Registers an action.
Definition: actions.php:85
elgg_get_logged_in_user_guid()
Return the current logged in user by guid.
Definition: sessions.php:42
$access
Definition: save.php:15
ajax_action_hook()
Buffer all output echo&#39;d directly in the action for inclusion in the returned JSON.
Definition: actions.php:266
elgg_unregister_action($action)
Unregisters an action.
Definition: actions.php:96