Elgg  Version 5.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 
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 
276 function elgg_extract_class(array $array, $existing = [], $extract_key = 'class'): array {
277  $existing = empty($existing) ? [] : (array) $existing;
278 
279  $merge = (array) elgg_extract($extract_key, $array, []);
280 
281  array_splice($existing, count($existing), 0, $merge);
282 
283  return array_values(array_unique($existing));
284 }
285 
299 function elgg_call(int $flags, Closure $closure) {
300  return _elgg_services()->invoker->call($flags, $closure);
301 }
302 
314 function elgg_get_ini_setting_in_bytes(string $setting): int {
315  // retrieve INI setting
316  $val = ini_get($setting);
317 
318  // convert INI setting when shorthand notation is used
319  $last = strtolower($val[strlen($val) - 1]);
320  if (in_array($last, ['g', 'm', 'k'])) {
321  $val = substr($val, 0, -1);
322  }
323 
324  $val = (int) $val;
325  switch ($last) {
326  case 'g':
327  $val *= 1024;
328  // fallthrough intentional
329  case 'm':
330  $val *= 1024;
331  // fallthrough intentional
332  case 'k':
333  $val *= 1024;
334  }
335 
336  // return byte value
337  return $val;
338 }
339 
346 function _elgg_services(): \Elgg\Di\InternalContainer {
347  // This yields a more shallow stack depth in recursive APIs like views. This aids in debugging and
348  // reduces false positives in xdebug's infinite recursion protection.
349  return \Elgg\Application::$_instance->internal_services;
350 }
$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:299
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
elgg_extract_class(array $array, $existing=[], $extract_key= 'class')
Extract class names from an array, optionally merging into a preexisting set.
Definition: elgglib.php:276
$options
Elgg admin footer.
Definition: footer.php:6
$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
elgg_get_ini_setting_in_bytes(string $setting)
Returns a PHP INI setting in bytes.
Definition: elgglib.php:314
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:346
elgg_http_validate_signed_url(string $url)
Validates if the HMAC signature of the URL is valid.
Definition: elgglib.php:224
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