Elgg  Version master
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 
86 function elgg_log($message, $level = \Psr\Log\LogLevel::NOTICE): void {
87  _elgg_services()->logger->log($level, $message);
88 }
89 
102 function elgg_dump($value): void {
103  _elgg_services()->logger->dump($value);
104 }
105 
115 function elgg_deprecated_notice(string $msg, string $dep_version): void {
116  _elgg_services()->logger->warning("Deprecated in {$dep_version}: {$msg}");
117 }
118 
131 function elgg_http_build_url(array $parts, bool $html_encode = true): string {
132  return _elgg_services()->urls->buildUrl($parts, $html_encode);
133 }
134 
152 function elgg_add_action_tokens_to_url(string $url, bool $html_encode = false): string {
153  return _elgg_services()->urls->addActionTokensToUrl($url, $html_encode);
154 }
155 
167 function elgg_http_remove_url_query_element(string $url, string $element): string {
168  return _elgg_services()->urls->addQueryElementsToUrl($url, [$element => null]);
169 }
170 
181 function elgg_http_add_url_query_elements(string $url, array $elements): string {
182  return _elgg_services()->urls->addQueryElementsToUrl($url, $elements);
183 }
184 
199 function elgg_http_url_is_identical(string $url1, string $url2, array $ignore_params = ['offset', 'limit']): bool {
200  return _elgg_services()->urls->isUrlIdentical($url1, $url2, (array) $ignore_params);
201 }
202 
214 function elgg_http_get_signed_url(string $url, string $expires = null): string {
215  return _elgg_services()->urlSigner->sign($url, $expires);
216 }
217 
224 function elgg_http_validate_signed_url(string $url): bool {
225  return _elgg_services()->urlSigner->isValid($url);
226 }
227 
235 function elgg_get_http_client(array $options = []): \Elgg\Http\Client {
236  return new \Elgg\Http\Client($options);
237 }
238 
254 function elgg_extract($key, $array, $default = null, bool $strict = true) {
255  if (!is_array($array) && !$array instanceof ArrayAccess) {
256  return $default;
257  }
258 
259  if ($strict) {
260  return $array[$key] ?? $default;
261  }
262 
263  return (isset($array[$key]) && !empty($array[$key])) ? $array[$key] : $default;
264 }
265 
277 function elgg_extract_class(array $array, array|string $existing = [], string $extract_key = 'class'): array {
278  $existing = empty($existing) ? [] : (array) $existing;
279 
280  $merge = (array) elgg_extract($extract_key, $array, []);
281 
282  array_splice($existing, count($existing), 0, $merge);
283 
284  return array_values(array_filter(array_unique($existing)));
285 }
286 
304 function elgg_call(int $flags, Closure $closure) {
305  return _elgg_services()->invoker->call($flags, $closure);
306 }
307 
319 function elgg_get_ini_setting_in_bytes(string $setting): int {
320  // retrieve INI setting
321  $val = ini_get($setting);
322 
323  // convert INI setting when shorthand notation is used
324  $last = strtolower($val[strlen($val) - 1]);
325  if (in_array($last, ['g', 'm', 'k'])) {
326  $val = substr($val, 0, -1);
327  }
328 
329  $val = (int) $val;
330  switch ($last) {
331  case 'g':
332  $val *= 1024;
333  // fallthrough intentional
334  case 'm':
335  $val *= 1024;
336  // fallthrough intentional
337  case 'k':
338  $val *= 1024;
339  }
340 
341  // return byte value
342  return $val;
343 }
344 
351 function _elgg_services(): \Elgg\Di\InternalContainer {
352  // This yields a more shallow stack depth in recursive APIs like views. This aids in debugging and
353  // reduces false positives in xdebug's infinite recursion protection.
354  return \Elgg\Application::$_instance->internal_services;
355 }
$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:304
elgg_deprecated_notice(string $msg, string $dep_version)
Log a notice about deprecated use of a function, view, etc.
Definition: elgglib.php:115
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:152
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:167
$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:254
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:319
elgg_log($message, $level=\Psr\Log\LogLevel::NOTICE)
Log a message.
Definition: elgglib.php:86
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:131
elgg_http_add_url_query_elements(string $url, array $elements)
Sets elements in a URL&#39;s query string.
Definition: elgglib.php:181
$expires
elgg_dump($value)
Logs $value to PHP&#39;s error_log().
Definition: elgglib.php:102
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:199
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:351
elgg_http_validate_signed_url(string $url)
Validates if the HMAC signature of the URL is valid.
Definition: elgglib.php:224
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:277
elgg_get_http_client(array $options=[])
Returns a Guzzle HTTP client.
Definition: elgglib.php:235
elgg_http_get_signed_url(string $url, string $expires=null)
Signs provided URL with a SHA256 HMAC key.
Definition: elgglib.php:214