Elgg  Version 6.1
output.php
Go to the documentation of this file.
1 <?php
20 function elgg_format_html(string $html, array $options = []): string {
21  return _elgg_services()->html_formatter->formatBlock($html, $options);
22 }
23 
32 function elgg_parse_urls(string $text): string {
33  return _elgg_services()->html_formatter->parseUrls($text);
34 }
35 
44 function elgg_parse_emails(string $text): string {
45  return _elgg_services()->html_formatter->parseEmails($text);
46 }
47 
56 function elgg_parse_mentions(string $text): string {
57  return _elgg_services()->html_formatter->parseMentions($text);
58 }
59 
67 function elgg_autop(string $string): string {
68  return _elgg_services()->html_formatter->addParagaraphs($string);
69 }
70 
83 function elgg_get_excerpt(string $text, int $num_chars = 250): string {
84  $view = 'output/excerpt';
85  $vars = [
86  'text' => $text,
87  'num_chars' => $num_chars,
88  ];
89  $viewtype = elgg_view_exists($view) ? '' : 'default';
90 
92 }
93 
103 function elgg_format_bytes(int $size, int $precision = 2): string {
104  if ($size < 0) {
105  return (string) $size;
106  }
107 
108  if ($size === 0) {
109  return '0 B';
110  }
111 
112  if ($precision < 0) {
113  $precision = 2;
114  }
115 
116  $base = log($size) / log(1024);
117  $suffixes = ['B', 'kB', 'MB', 'GB', 'TB'];
118 
119  return round(pow(1024, $base - floor($base)), $precision) . ' ' . $suffixes[floor($base)];
120 }
121 
145 function elgg_format_element(string $tag_name, array $attributes = [], string $text = '', array $options = []): string {
146  return _elgg_services()->html_formatter->formatElement($tag_name, $attributes, $text, $options);
147 }
148 
163 function elgg_normalize_url(string $url): string {
164  return _elgg_services()->urls->normalizeUrl($url);
165 }
166 
175 function elgg_normalize_site_url(string $unsafe_url): ?string {
176  $unsafe_url = _elgg_services()->urls->normalizeUrl($unsafe_url);
177  if (elgg_strpos($unsafe_url, elgg_get_site_url()) === 0) {
178  return $unsafe_url;
179  }
180 
181  return null;
182 }
183 
192 function elgg_get_friendly_title(string $title): string {
193  // return a URL friendly title to short circuit normal title formatting
194  $params = ['title' => $title];
195  $result = elgg_trigger_event_results('format', 'friendly:title', $params, null);
196  if (is_string($result)) {
197  return $result;
198  }
199 
200  // titles are often stored HTML encoded
201  $title = html_entity_decode($title ?? '', ENT_QUOTES, 'UTF-8');
202 
203  // limit length to prevent issues with too long URLS (Request-URI Too Large) #13228
204  // limit the length before urlize() to prevent multibyte chars from being cut of in the middle #14577
205  $title = elgg_substr($title, 0, 100);
206 
207  return \Elgg\Translit::urlize($title);
208 }
209 
221 function elgg_get_friendly_time(int $time, int $current_time = null): string {
222 
223  if (!isset($current_time)) {
224  $current_time = time();
225  }
226 
227  // return a time string to short circuit normal time formatting
228  $params = ['time' => $time, 'current_time' => $current_time];
229  $result = elgg_trigger_event_results('format', 'friendly:time', $params, null);
230  if (is_string($result)) {
231  return $result;
232  }
233 
234  $diff = abs($current_time - $time);
235 
236  $minute = 60;
237  $hour = $minute * 60;
238  $day = $hour * 24;
239 
240  if ($diff < $minute) {
241  return elgg_echo('friendlytime:justnow');
242  }
243 
244  if ($diff < $hour) {
245  $granularity = ':minutes';
246  $diff = round($diff / $minute);
247  } else if ($diff < $day) {
248  $granularity = ':hours';
249  $diff = round($diff / $hour);
250  } else {
251  $granularity = ':days';
252  $diff = round($diff / $day);
253  }
254 
255  if ($diff == 0) {
256  $diff = 1;
257  }
258 
259  $future = ($current_time - $time < 0) ? ':future' : '';
260  $singular = ($diff == 1) ? ':singular' : '';
261 
262  return elgg_echo("friendlytime{$future}{$granularity}{$singular}", [$diff]);
263 }
264 
272 function elgg_get_friendly_upload_error(int $error_code): string {
273  switch ($error_code) {
274  case UPLOAD_ERR_OK:
275  return '';
276 
277  case UPLOAD_ERR_INI_SIZE:
278  $key = 'ini_size';
279  break;
280 
281  case UPLOAD_ERR_FORM_SIZE:
282  $key = 'form_size';
283  break;
284 
285  case UPLOAD_ERR_PARTIAL:
286  $key = 'partial';
287  break;
288 
289  case UPLOAD_ERR_NO_FILE:
290  $key = 'no_file';
291  break;
292 
293  case UPLOAD_ERR_NO_TMP_DIR:
294  $key = 'no_tmp_dir';
295  break;
296 
297  case UPLOAD_ERR_CANT_WRITE:
298  $key = 'cant_write';
299  break;
300 
301  case UPLOAD_ERR_EXTENSION:
302  $key = 'extension';
303  break;
304 
305  default:
306  $key = 'unknown';
307  break;
308  }
309 
310  return elgg_echo("upload:error:{$key}");
311 }
312 
323 function elgg_strip_tags(string $string, string $allowable_tags = null): string {
324  return _elgg_services()->html_formatter->stripTags($string, $allowable_tags);
325 }
326 
354 function elgg_html_decode(string $string): string {
355  return _elgg_services()->html_formatter->decode($string);
356 }
357 
366 function _elgg_get_display_query(string $string): string {
367  if (empty($string)) {
368  return $string;
369  }
370 
371  $string = htmlentities($string, ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401, 'UTF-8');
372 
373  return htmlspecialchars($string, ENT_QUOTES, 'UTF-8', false);
374 }
elgg_format_bytes(int $size, int $precision=2)
Format bytes to a human readable format.
Definition: output.php:103
elgg_get_friendly_time(int $time, int $current_time=null)
Formats a UNIX timestamp in a friendly way (eg "less than a minute ago")
Definition: output.php:221
$params
Saves global plugin settings.
Definition: save.php:13
_elgg_get_display_query(string $string)
Prepares query string for output to prevent CSRF attacks.
Definition: output.php:366
elgg_normalize_site_url(string $unsafe_url)
From untrusted input, get a site URL safe for forwarding.
Definition: output.php:175
$title
Definition: generic.php:50
elgg_get_excerpt(string $text, int $num_chars=250)
Returns an excerpt.
Definition: output.php:83
elgg_parse_mentions(string $text)
Takes a string and turns any @ mentions into formatted links.
Definition: output.php:56
elgg_echo(string $message_key, array $args=[], string $language= '')
Elgg language module Functions to manage language and translations.
Definition: languages.php:17
if(!$annotation instanceof ElggAnnotation) $time
Definition: time.php:20
if($alt_image) $tag_name
Definition: image_block.php:45
elgg_trigger_event_results(string $event, string $type, array $params=[], $returnvalue=null)
Triggers an event where it is expected that the mixed return value could be manipulated by event call...
Definition: events.php:117
$num_chars
Definition: excerpt.php:16
elgg_parse_emails(string $text)
Takes a string and turns any email addresses into formatted links.
Definition: output.php:44
elgg_parse_urls(string $text)
Takes a string and turns any URLs into formatted links.
Definition: output.php:32
$html
A wrapper to render a section of the page shell.
Definition: section.php:9
if($who_can_change_language=== 'nobody') elseif($who_can_change_language=== 'admin_only'&&!elgg_is_admin_logged_in()) $options
Definition: language.php:20
elgg_format_html(string $html, array $options=[])
Output functions Processing text for output such as pulling out URLs and extracting excerpts...
Definition: output.php:20
if(!empty($avatar)&&!$avatar->isValid()) elseif(empty($avatar)) if(!$owner->saveIconFromUploadedFile('avatar')) if(!elgg_trigger_event('profileiconupdate', $owner->type, $owner)) $view
Definition: upload.php:39
elgg_get_friendly_title(string $title)
When given a title, returns a version suitable for inclusion in a URL.
Definition: output.php:192
elgg_strpos()
Wrapper function for mb_strpos().
Definition: mb_wrapper.php:71
$viewtype
Definition: default.php:11
elgg_get_friendly_upload_error(int $error_code)
Returns a human-readable message for PHP&#39;s upload error codes.
Definition: output.php:272
elgg_get_site_url()
Get the URL for the current (or specified) site, ending with "/".
$size
Definition: thumb.php:23
if($container instanceof ElggGroup &&$container->guid!=elgg_get_page_owner_guid()) $key
Definition: summary.php:44
log($level, $message, array $context=[])
Log a message.
Definition: Loggable.php:58
elgg_substr()
Wrapper function for mb_substr().
Definition: mb_wrapper.php:195
$vars
Definition: theme.php:5
elgg_autop(string $string)
Create paragraphs from text with line spacing.
Definition: output.php:67
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_format_element(string $tag_name, array $attributes=[], string $text= '', array $options=[])
Format an HTML element.
Definition: output.php:145
elgg_normalize_url(string $url)
Definition: output.php:163
_elgg_view_under_viewtype(string $view, array $vars, string $viewtype)
Render a view while the global viewtype is temporarily changed.
Definition: views.php:1401
$text
Definition: button.php:33
$attributes
Elgg AJAX loader.
Definition: ajax_loader.php:10
elgg_view_exists(string $view, string $viewtype= '', bool $recurse=true)
Returns whether the specified view exists.
Definition: views.php:131
elgg_html_decode(string $string)
Decode HTML markup into a raw text string.
Definition: output.php:354
elgg_strip_tags(string $string, string $allowable_tags=null)
Strip tags and offer plugins the chance.
Definition: output.php:323