Elgg  Version 6.1
elgglib.php
Go to the documentation of this file.
1 <?php
12 function elgg(): \Elgg\Di\PublicContainer {
13  return \Elgg\Application::$_instance->public_services;
14 }
15 
26 function elgg_set_http_header(string $header, bool $replace = true): void {
27  if (!preg_match('~^HTTP/\\d\\.\\d~', $header)) {
28  list($name, $value) = explode(':', $header, 2);
29  _elgg_services()->responseFactory->setHeader($name, trim($value), $replace);
30  }
31 }
32 
43 function elgg_register_success_message(string|array $options): void {
44  if (!is_array($options)) {
45  $options = ['message' => $options];
46  }
47 
48  $options['type'] = 'success';
49  _elgg_services()->system_messages->addMessage($options);
50 }
51 
62 function elgg_register_error_message(string|array $options): void {
63  if (!is_array($options)) {
64  $options = ['message' => $options];
65  }
66 
67  $options['type'] = 'error';
68  _elgg_services()->system_messages->addMessage($options);
69 }
70 
88 function elgg_log($message, $level = \Psr\Log\LogLevel::NOTICE): void {
89  _elgg_services()->logger->log($level, $message);
90 }
91 
104 function elgg_dump($value): void {
105  _elgg_services()->logger->dump($value);
106 }
107 
117 function elgg_deprecated_notice(string $msg, string $dep_version): void {
118  _elgg_services()->logger->warning("Deprecated in {$dep_version}: {$msg}");
119 }
120 
133 function elgg_http_build_url(array $parts, bool $html_encode = true): string {
134  return _elgg_services()->urls->buildUrl($parts, $html_encode);
135 }
136 
154 function elgg_add_action_tokens_to_url(string $url, bool $html_encode = false): string {
155  return _elgg_services()->urls->addActionTokensToUrl($url, $html_encode);
156 }
157 
169 function elgg_http_remove_url_query_element(string $url, string $element): string {
170  return _elgg_services()->urls->addQueryElementsToUrl($url, [$element => null]);
171 }
172 
183 function elgg_http_add_url_query_elements(string $url, array $elements): string {
184  return _elgg_services()->urls->addQueryElementsToUrl($url, $elements);
185 }
186 
201 function elgg_http_url_is_identical(string $url1, string $url2, array $ignore_params = ['offset', 'limit']): bool {
202  return _elgg_services()->urls->isUrlIdentical($url1, $url2, (array) $ignore_params);
203 }
204 
216 function elgg_http_get_signed_url(string $url, string $expires = null): string {
217  return _elgg_services()->urlSigner->sign($url, $expires);
218 }
219 
226 function elgg_http_validate_signed_url(string $url): bool {
227  return _elgg_services()->urlSigner->isValid($url);
228 }
229 
237 function elgg_get_http_client(array $options = []): \Elgg\Http\Client {
238  return new \Elgg\Http\Client($options);
239 }
240 
256 function elgg_extract($key, $array, $default = null, bool $strict = true) {
257  if (!is_array($array) && !$array instanceof ArrayAccess) {
258  return $default;
259  }
260 
261  if ($strict) {
262  return $array[$key] ?? $default;
263  }
264 
265  return (isset($array[$key]) && !empty($array[$key])) ? $array[$key] : $default;
266 }
267 
279 function elgg_extract_class(array $array, array|string $existing = [], string $extract_key = 'class'): array {
280  $existing = empty($existing) ? [] : (array) $existing;
281 
282  $merge = (array) elgg_extract($extract_key, $array, []);
283 
284  array_splice($existing, count($existing), 0, $merge);
285 
286  return array_values(array_filter(array_unique($existing)));
287 }
288 
306 function elgg_call(int $flags, Closure $closure) {
307  return _elgg_services()->invoker->call($flags, $closure);
308 }
309 
321 function elgg_get_ini_setting_in_bytes(string $setting): int {
322  // retrieve INI setting
323  $val = ini_get($setting);
324 
325  // convert INI setting when shorthand notation is used
326  $last = strtolower($val[strlen($val) - 1]);
327  if (in_array($last, ['g', 'm', 'k'])) {
328  $val = substr($val, 0, -1);
329  }
330 
331  $val = (int) $val;
332  switch ($last) {
333  case 'g':
334  $val *= 1024;
335  // fallthrough intentional
336  case 'm':
337  $val *= 1024;
338  // fallthrough intentional
339  case 'k':
340  $val *= 1024;
341  }
342 
343  // return byte value
344  return $val;
345 }
346 
353 function _elgg_services(): \Elgg\Di\InternalContainer {
354  // This yields a more shallow stack depth in recursive APIs like views. This aids in debugging and
355  // reduces false positives in xdebug's infinite recursion protection.
356  return \Elgg\Application::$_instance->internal_services;
357 }
$default
Definition: checkbox.php:30
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:306
elgg_deprecated_notice(string $msg, string $dep_version)
Log a notice about deprecated use of a function, view, etc.
Definition: elgglib.php:117
if(!$user||!$user->canDelete()) $name
Definition: delete.php:22
elgg_register_success_message(string|array $options)
Registers a success system message.
Definition: elgglib.php:43
elgg_add_action_tokens_to_url(string $url, bool $html_encode=false)
Adds action tokens to URL.
Definition: elgglib.php:154
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_http_remove_url_query_element(string $url, string $element)
Removes an element from a URL&#39;s query string.
Definition: elgglib.php:169
$element
Definition: section.php:29
$value
Definition: generic.php:51
elgg_extract($key, $array, $default=null, bool $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
Definition: elgglib.php:256
elgg_set_http_header(string $header, bool $replace=true)
Set a response HTTP header.
Definition: elgglib.php:26
if($who_can_change_language=== 'nobody') elseif($who_can_change_language=== 'admin_only'&&!elgg_is_admin_logged_in()) $options
Definition: language.php:20
elgg_get_ini_setting_in_bytes(string $setting)
Returns a PHP INI setting in bytes.
Definition: elgglib.php:321
elgg_log($message, $level=\Psr\Log\LogLevel::NOTICE)
Log a message.
Definition: elgglib.php:88
elgg_http_build_url(array $parts, bool $html_encode=true)
Builds a URL from the a parts array like one returned by parse_url().
Definition: elgglib.php:133
elgg_http_add_url_query_elements(string $url, array $elements)
Sets elements in a URL&#39;s query string.
Definition: elgglib.php:183
$expires
elgg_dump($value)
Logs $value to PHP&#39;s error_log().
Definition: elgglib.php:104
elgg_register_error_message(string|array $options)
Registers a error system message.
Definition: elgglib.php:62
elgg_http_url_is_identical(string $url1, string $url2, array $ignore_params=['offset', 'limit'])
Test if two URLs are functionally identical.
Definition: elgglib.php:201
if($container instanceof ElggGroup &&$container->guid!=elgg_get_page_owner_guid()) $key
Definition: summary.php:44
foreach($periods as $period) $header
Definition: cron.php:76
foreach($plugin_guids as $guid) if(empty($deactivated_plugins)) $url
Definition: deactivate.php:39
_elgg_services()
Get the global service provider.
Definition: elgglib.php:353
elgg_http_validate_signed_url(string $url)
Validates if the HMAC signature of the URL is valid.
Definition: elgglib.php:226
elgg_extract_class(array $array, array|string $existing=[], string $extract_key= 'class')
Extract class names from an array, optionally merging into a preexisting set.
Definition: elgglib.php:279
elgg_get_http_client(array $options=[])
Returns a Guzzle HTTP client.
Definition: elgglib.php:237
elgg_http_get_signed_url(string $url, string $expires=null)
Signs provided URL with a SHA256 HMAC key.
Definition: elgglib.php:216