Elgg  Version 1.9
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 
116 function validate_action_token($visible_errors = true, $token = null, $ts = null) {
117  return _elgg_services()->actions->validateActionToken($visible_errors, $token, $ts);
118 }
119 
135  return _elgg_services()->actions->gatekeeper($action);
136 }
137 
157  return _elgg_services()->actions->generateActionToken($timestamp);
158 }
159 
171 function init_site_secret() {
172  $secret = 'z' . _elgg_services()->crypto->getRandomString(31);
173 
174  if (datalist_set('__site_secret__', $secret)) {
175  return $secret;
176  }
177 
178  return false;
179 }
180 
190 function get_site_secret() {
191  $secret = datalist_get('__site_secret__');
192  if (!$secret) {
193  $secret = init_site_secret();
194  }
195 
196  return $secret;
197 }
198 
206  $secret = get_site_secret();
207  if ($secret[0] !== 'z') {
208  $rand_max = getrandmax();
209  if ($rand_max < pow(2, 16)) {
210  return 'weak';
211  }
212  if ($rand_max < pow(2, 32)) {
213  return 'moderate';
214  }
215  }
216  return 'strong';
217 }
218 
228  return _elgg_services()->actions->exists($action);
229 }
230 
237 function elgg_is_xhr() {
238  return _elgg_services()->request->isXmlHttpRequest();
239 }
240 
267 function ajax_forward_hook($hook, $type, $reason, $params) {
268  _elgg_services()->actions->ajaxForwardHook($hook, $type, $reason, $params);
269 }
270 
276 function ajax_action_hook() {
277  _elgg_services()->actions->ajaxActionHook();
278 }
279 
286 
287  if (!elgg_is_xhr()) {
288  return false;
289  }
290 
291  $ts = time();
293  $data = array(
294  '__elgg_ts' => $ts,
295  '__elgg_token' => $token,
296  'logged_in' => elgg_is_logged_in(),
297  );
298 
299  header("Content-Type: application/json");
300  echo json_encode($data);
301 
302  return true;
303 }
304 
309 function actions_init() {
310  elgg_register_page_handler('action', '_elgg_action_handler');
311  elgg_register_page_handler('refresh_token', '_elgg_csrf_token_refresh');
312 
313  elgg_register_simplecache_view('js/languages/en');
314 
315  elgg_register_plugin_hook_handler('action', 'all', 'ajax_action_hook');
316  elgg_register_plugin_hook_handler('forward', 'all', 'ajax_forward_hook');
317 }
318 
319 elgg_register_event_handler('init', 'system', 'actions_init');
elgg_is_logged_in()
Returns whether or not the user is currently logged in.
Definition: sessions.php:56
elgg_is_xhr()
Checks whether the request was requested via ajax.
Definition: actions.php:237
action($action, $forwarder="")
Perform an action.
Definition: actions.php:47
action_gatekeeper($action)
Validates the presence of action tokens.
Definition: actions.php:134
elgg_action_exists($action)
Check if an action is registered and its script exists.
Definition: actions.php:227
elgg_register_simplecache_view($view_name)
Registers a view to simple cache.
Definition: cache.php:136
$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:171
elgg_register_plugin_hook_handler($hook, $type, $callback, $priority=500)
Register a callback as a plugin hook handler.
Definition: elgglib.php:853
$timestamp
Definition: date.php:35
$action
$params
Definition: login.php:72
generate_action_token($timestamp)
Generate an action token.
Definition: actions.php:156
datalist_get($name)
Get the value of a datalist element.
actions_init()
Initialize some ajaxy actions features private.
Definition: actions.php:309
_elgg_services()
Definition: autoloader.php:14
datalist_set($name, $value)
Set the value for a datalist element.
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:267
$type
Definition: add.php:8
get_site_secret()
Returns the site secret.
Definition: actions.php:190
$token
elgg_register_event_handler($event, $object_type, $callback, $priority=500)
Register a callback as an Elgg event handler.
Definition: elgglib.php:669
_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:116
_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.
Definition: actions.php:285
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
$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:276
elgg_unregister_action($action)
Unregisters an action.
Definition: actions.php:96