Elgg  Version 4.x
elgglib.php
Go to the documentation of this file.
1 <?php
12 function elgg() {
13  return \Elgg\Application::$_instance->public_services;
14 }
15 
26 function elgg_set_http_header($header, $replace = true) {
27  if (!preg_match('~^HTTP/\\d\\.\\d~', $header)) {
28  list($name, $value) = explode(':', $header, 2);
29  _elgg_services()->responseFactory->setHeader($name, ltrim($value), $replace);
30  }
31 }
32 
44  if (!is_array($options)) {
45  $options = ['message' => $options];
46  }
47 
48  $options['type'] = 'success';
49  _elgg_services()->system_messages->addMessage($options);
50 }
51 
63  if (!is_array($options)) {
64  $options = ['message' => $options];
65  }
66 
67  $options['type'] = 'error';
68  _elgg_services()->system_messages->addMessage($options);
69 }
70 
126 function elgg_register_event_handler($event, $object_type, $callback, $priority = 500) {
127  return _elgg_services()->events->registerHandler($event, $object_type, $callback, $priority);
128 }
129 
140 function elgg_unregister_event_handler($event, $object_type, $callback) {
141  return _elgg_services()->events->unregisterHandler($event, $object_type, $callback);
142 }
143 
153 function elgg_clear_event_handlers($event, $object_type) {
154  _elgg_services()->events->clearHandlers($event, $object_type);
155 }
156 
190 function elgg_trigger_event($event, $object_type, $object = null) {
191  return _elgg_services()->events->trigger($event, $object_type, $object);
192 }
193 
211 function elgg_trigger_before_event($event, $object_type, $object = null) {
212  return _elgg_services()->events->triggerBefore($event, $object_type, $object);
213 }
214 
230 function elgg_trigger_after_event($event, $object_type, $object = null) {
231  return _elgg_services()->events->triggerAfter($event, $object_type, $object);
232 }
233 
294 function elgg_register_plugin_hook_handler($hook, $type, $callback, $priority = 500) {
295  return _elgg_services()->hooks->registerHandler($hook, $type, $callback, $priority);
296 }
297 
309 function elgg_unregister_plugin_hook_handler($hook, $entity_type, $callback) {
310  _elgg_services()->hooks->unregisterHandler($hook, $entity_type, $callback);
311 }
312 
323  _elgg_services()->hooks->clearHandlers($hook, $type);
324 }
325 
380 function elgg_trigger_plugin_hook($hook, $type, $params = null, $returnvalue = null) {
381  return _elgg_services()->hooks->trigger($hook, $type, $params, $returnvalue);
382 }
383 
399 function elgg_log($message, $level = \Psr\Log\LogLevel::NOTICE) {
400  return _elgg_services()->logger->log($level, $message);
401 }
402 
415 function elgg_dump($value) {
416  _elgg_services()->logger->dump($value);
417 }
418 
432 function elgg_http_build_url(array $parts, $html_encode = true) {
433  // build only what's given to us.
434  $scheme = isset($parts['scheme']) ? "{$parts['scheme']}://" : '';
435  $host = isset($parts['host']) ? "{$parts['host']}" : '';
436  $port = isset($parts['port']) ? ":{$parts['port']}" : '';
437  $path = isset($parts['path']) ? "{$parts['path']}" : '';
438  $query = isset($parts['query']) ? "?{$parts['query']}" : '';
439  $fragment = isset($parts['fragment']) ? "#{$parts['fragment']}" : '';
440 
441  $string = $scheme . $host . $port . $path . $query . $fragment;
442 
443  if ($html_encode) {
444  return htmlspecialchars($string, ENT_QUOTES, 'UTF-8', false);
445  } else {
446  return $string;
447  }
448 }
449 
467 function elgg_add_action_tokens_to_url($url, $html_encode = false) {
469  $components = parse_url($url);
470 
471  if (isset($components['query'])) {
472  $query = elgg_parse_str($components['query']);
473  } else {
474  $query = [];
475  }
476 
477  if (isset($query['__elgg_ts']) && isset($query['__elgg_token'])) {
478  return $url;
479  }
480 
481  // append action tokens to the existing query
482  $query['__elgg_ts'] = _elgg_services()->csrf->getCurrentTime()->getTimestamp();
483  $query['__elgg_token'] = _elgg_services()->csrf->generateActionToken($query['__elgg_ts']);
484  $components['query'] = http_build_query($query);
485 
486  // rebuild the full url
487  return elgg_http_build_url($components, $html_encode);
488 }
489 
502  return elgg_http_add_url_query_elements($url, [$element => null]);
503 }
504 
515 function elgg_http_add_url_query_elements($url, array $elements) {
516  $url_array = parse_url($url);
517 
518  if (isset($url_array['query'])) {
519  $query = elgg_parse_str($url_array['query']);
520  } else {
521  $query = [];
522  }
523 
524  foreach ($elements as $k => $v) {
525  if ($v === null) {
526  unset($query[$k]);
527  } else {
528  $query[$k] = $v;
529  }
530  }
531 
532  // why check path? A: if no path, this may be a relative URL like "?foo=1". In this case,
533  // the output "" would be interpreted the current URL, so in this case we *must* set
534  // a query to make sure elements are removed.
535  if ($query || empty($url_array['path'])) {
536  $url_array['query'] = http_build_query($query);
537  } else {
538  unset($url_array['query']);
539  }
540  $string = elgg_http_build_url($url_array, false);
541 
542  // Restore relative protocol to url if missing and is provided as part of the initial url (see #9874)
543  if (!isset($url['scheme']) && (substr($url, 0, 2) == '//')) {
544  $string = "//{$string}";
545  }
546 
547  return $string;
548 }
549 
564 function elgg_http_url_is_identical($url1, $url2, $ignore_params = ['offset', 'limit']) {
565  if (!is_string($url1) || !is_string($url2)) {
566  return false;
567  }
568 
569  $url1 = elgg_normalize_url($url1);
570  $url2 = elgg_normalize_url($url2);
571 
572  if ($url1 == $url2) {
573  return true;
574  }
575 
576  $url1_info = parse_url($url1);
577  $url2_info = parse_url($url2);
578 
579  if (isset($url1_info['path'])) {
580  $url1_info['path'] = trim($url1_info['path'], '/');
581  }
582  if (isset($url2_info['path'])) {
583  $url2_info['path'] = trim($url2_info['path'], '/');
584  }
585 
586  // compare basic bits
587  $parts = ['scheme', 'host', 'path'];
588 
589  foreach ($parts as $part) {
590  if ((isset($url1_info[$part]) && isset($url2_info[$part]))
591  && $url1_info[$part] != $url2_info[$part]) {
592  return false;
593  } elseif (isset($url1_info[$part]) && !isset($url2_info[$part])) {
594  return false;
595  } elseif (!isset($url1_info[$part]) && isset($url2_info[$part])) {
596  return false;
597  }
598  }
599 
600  // quick compare of get params
601  if (isset($url1_info['query']) && isset($url2_info['query'])
602  && $url1_info['query'] == $url2_info['query']) {
603  return true;
604  }
605 
606  // compare get params that might be out of order
607  $url1_params = [];
608  $url2_params = [];
609 
610  if (isset($url1_info['query'])) {
611  if ($url1_info['query'] = html_entity_decode($url1_info['query'])) {
612  $url1_params = elgg_parse_str($url1_info['query']);
613  }
614  }
615 
616  if (isset($url2_info['query'])) {
617  if ($url2_info['query'] = html_entity_decode($url2_info['query'])) {
618  $url2_params = elgg_parse_str($url2_info['query']);
619  }
620  }
621 
622  // drop ignored params
623  foreach ($ignore_params as $param) {
624  if (isset($url1_params[$param])) {
625  unset($url1_params[$param]);
626  }
627  if (isset($url2_params[$param])) {
628  unset($url2_params[$param]);
629  }
630  }
631 
632  // array_diff_assoc only returns the items in arr1 that aren't in arrN
633  // but not the items that ARE in arrN but NOT in arr1
634  // if arr1 is an empty array, this function will return 0 no matter what.
635  // since we only care if they're different and not how different,
636  // add the results together to get a non-zero (ie, different) result
637  $diff_count = count(_elgg_array_diff_assoc_recursive($url1_params, $url2_params));
638  $diff_count += count(_elgg_array_diff_assoc_recursive($url2_params, $url1_params));
639  if ($diff_count > 0) {
640  return false;
641  }
642 
643  return true;
644 }
645 
657 function elgg_http_get_signed_url($url, $expires = false) {
658  return _elgg_services()->urlSigner->sign($url, $expires);
659 }
660 
668  return _elgg_services()->urlSigner->isValid($url);
669 }
670 
686 function elgg_extract($key, $array, $default = null, $strict = true) {
687  if (!is_array($array) && !$array instanceof ArrayAccess) {
688  return $default;
689  }
690 
691  if ($strict) {
692  return (isset($array[$key])) ? $array[$key] : $default;
693  } else {
694  return (isset($array[$key]) && !empty($array[$key])) ? $array[$key] : $default;
695  }
696 }
697 
708 function elgg_extract_class(array $array, $existing = [], $extract_key = 'class') {
709  $existing = empty($existing) ? [] : (array) $existing;
710 
711  $merge = (array) elgg_extract($extract_key, $array, []);
712 
713  array_splice($existing, count($existing), 0, $merge);
714 
715  return array_values(array_unique($existing));
716 }
717 
731 function elgg_call(int $flags, Closure $closure) {
732  return _elgg_services()->invoker->call($flags, $closure);
733 }
734 
746 function elgg_get_ini_setting_in_bytes($setting) {
747  // retrieve INI setting
748  $val = ini_get($setting);
749 
750  // convert INI setting when shorthand notation is used
751  $last = strtolower($val[strlen($val) - 1]);
752  if (in_array($last, ['g', 'm', 'k'])) {
753  $val = substr($val, 0, -1);
754  }
755  $val = (int) $val;
756  switch ($last) {
757  case 'g':
758  $val *= 1024;
759  // fallthrough intentional
760  case 'm':
761  $val *= 1024;
762  // fallthrough intentional
763  case 'k':
764  $val *= 1024;
765  }
766 
767  // return byte value
768  return $val;
769 }
770 
777 function _elgg_services() {
778  // This yields a more shallow stack depth in recursive APIs like views. This aids in debugging and
779  // reduces false positives in xdebug's infinite recursion protection.
780  return \Elgg\Application::$_instance->internal_services;
781 }
782 
794  $args = func_get_args();
795  $diff = [];
796 
797  foreach (array_shift($args) as $key => $val) {
798  for ($i = 0, $j = 0, $tmp = [$val], $count = count($args); $i < $count; $i++) {
799  if (is_array($val)) {
800  if (!isset($args[$i][$key]) || !is_array($args[$i][$key]) || empty($args[$i][$key])) {
801  $j++;
802  } else {
803  $tmp[] = $args[$i][$key];
804  }
805  } elseif (!array_key_exists($key, $args[$i]) || $args[$i][$key] !== $val) {
806  $j++;
807  }
808  }
809 
810  if (is_array($val)) {
811  $tmp = call_user_func_array ( __FUNCTION__, $tmp);
812  if (!empty($tmp)) {
813  $diff[$key] = $tmp;
814  } elseif ($j == $count) {
815  $diff[$key] = $val;
816  }
817  } elseif ($j == $count && $count) {
818  $diff[$key] = $val;
819  }
820  }
821 
822  return $diff;
823 }
$default
Definition: checkbox.php:31
elgg_call(int $flags, Closure $closure)
Calls a callable autowiring the arguments using public DI services and applying logic based on flags...
Definition: elgglib.php:731
elgg_http_add_url_query_elements($url, array $elements)
Sets elements in a URL&#39;s query string.
Definition: elgglib.php:515
elgg_parse_str($str)
Elgg UTF-8 string functions.
Definition: mb_wrapper.php:16
elgg_unregister_plugin_hook_handler($hook, $entity_type, $callback)
Unregister a callback as a plugin hook.
Definition: elgglib.php:309
if(!$user||!$user->canDelete()) $name
Definition: delete.php:22
elgg_add_action_tokens_to_url($url, $html_encode=false)
Adds action tokens to URL.
Definition: elgglib.php:467
$params
Saves global plugin settings.
Definition: save.php:13
elgg_set_http_header($header, $replace=true)
Set a response HTTP header.
Definition: elgglib.php:26
elgg_normalize_url($url)
Definition: output.php:152
_elgg_array_diff_assoc_recursive()
Computes the difference of arrays with additional index check.
Definition: elgglib.php:793
elgg_clear_plugin_hook_handlers($hook, $type)
Clears all callback registrations for a plugin hook.
Definition: elgglib.php:322
c Accompany it with the information you received as to the offer to distribute corresponding source complete source code means all the source code for all modules it plus any associated interface definition plus the scripts used to control compilation and installation of the executable as a special the source code distributed need not include anything that is normally and so on of the operating system on which the executable unless that component itself accompanies the executable If distribution of executable or object code is made by offering access to copy from a designated then offering equivalent access to copy the source code from the same place counts as distribution of the source even though third parties are not compelled to copy the source along with the object code You may not or distribute the Program except as expressly provided under this License Any attempt otherwise to sublicense or distribute the Program is void
Definition: LICENSE.txt:215
elgg()
Bootstrapping and helper procedural code available for use in Elgg core and plugins.
Definition: elgglib.php:12
elgg parse_url
Parse a URL into its parts.
Definition: elgglib.js:135
$args
Some servers don&#39;t allow PHP to check the rewrite, so try via AJAX.
$type
Definition: delete.php:21
elgg_register_plugin_hook_handler($hook, $type, $callback, $priority=500)
Definition: elgglib.php:294
elgg_extract_class(array $array, $existing=[], $extract_key= 'class')
Extract class names from an array, optionally merging into a preexisting set.
Definition: elgglib.php:708
elgg_trigger_before_event($event, $object_type, $object=null)
Trigger a "Before event" indicating a process is about to begin.
Definition: elgglib.php:211
$options
Elgg admin footer.
Definition: footer.php:6
$value
Definition: generic.php:51
$header
Definition: database.php:26
$path
Definition: details.php:68
elgg_get_ini_setting_in_bytes($setting)
Returns a PHP INI setting in bytes.
Definition: elgglib.php:746
elgg_http_url_is_identical($url1, $url2, $ignore_params=['offset', 'limit'])
Test if two URLs are functionally identical.
Definition: elgglib.php:564
elgg_http_remove_url_query_element($url, $element)
Removes an element from a URL&#39;s query string.
Definition: elgglib.php:501
elgg_log($message, $level=\Psr\Log\LogLevel::NOTICE)
Log a message.
Definition: elgglib.php:399
elgg_trigger_plugin_hook($hook, $type, $params=null, $returnvalue=null)
Definition: elgglib.php:380
$count
Definition: ban.php:24
elgg_extract($key, $array, $default=null, $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
Definition: elgglib.php:686
elgg_dump($value)
Logs $value to PHP&#39;s error_log().
Definition: elgglib.php:415
elgg_register_event_handler($event, $object_type, $callback, $priority=500)
Definition: elgglib.php:126
elgg_register_error_message($options)
Registers a error system message.
Definition: elgglib.php:62
if($container instanceof ElggGroup &&$container->guid!=elgg_get_page_owner_guid()) $key
Definition: summary.php:44
if($email instanceof\Elgg\Email) $object
Definition: body.php:16
if($item instanceof\ElggEntity) elseif($item instanceof\ElggRiverItem) elseif($item instanceof ElggRelationship) elseif(is_callable([$item, 'getType']))
Definition: item.php:48
elgg_http_build_url(array $parts, $html_encode=true)
Builds a URL from the a parts array like one returned by parse_url().
Definition: elgglib.php:432
$query
foreach($plugin_guids as $guid) if(empty($deactivated_plugins)) $url
Definition: deactivate.php:39
elgg_clear_event_handlers($event, $object_type)
Clears all callback registrations for a event.
Definition: elgglib.php:153
_elgg_services()
Get the global service provider.
Definition: elgglib.php:777
elgg_trigger_after_event($event, $object_type, $object=null)
Trigger an "After event" indicating a process has finished.
Definition: elgglib.php:230
elgg_http_get_signed_url($url, $expires=false)
Signs provided URL with a SHA256 HMAC key.
Definition: elgglib.php:657
elgg_register_success_message($options)
Registers a success system message.
Definition: elgglib.php:43
elgg_trigger_event($event, $object_type, $object=null)
Definition: elgglib.php:190
elgg_unregister_event_handler($event, $object_type, $callback)
Unregisters a callback for an event.
Definition: elgglib.php:140
elgg_http_validate_signed_url($url)
Validates if the HMAC signature of the URL is valid.
Definition: elgglib.php:667
$priority