Elgg  Version master
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  $precision = (int) $precision;
109  if ($precision < 0) {
110  $precision = 2;
111  }
112 
113  $base = log($size) / log(1024);
114  $suffixes = ['B', 'kB', 'MB', 'GB', 'TB'];
115 
116  return round(pow(1024, $base - floor($base)), $precision) . ' ' . $suffixes[floor($base)];
117 }
118 
142 function elgg_format_element(string $tag_name, array $attributes = [], string $text = '', array $options = []): string {
143  return _elgg_services()->html_formatter->formatElement($tag_name, $attributes, $text, $options);
144 }
145 
160 function elgg_normalize_url(string $url): string {
161  return _elgg_services()->urls->normalizeUrl($url);
162 }
163 
172 function elgg_normalize_site_url(string $unsafe_url): ?string {
173  $unsafe_url = _elgg_services()->urls->normalizeUrl($unsafe_url);
174  if (elgg_strpos($unsafe_url, elgg_get_site_url()) === 0) {
175  return $unsafe_url;
176  }
177 
178  return null;
179 }
180 
189 function elgg_get_friendly_title(string $title): string {
190  // return a URL friendly title to short circuit normal title formatting
191  $params = ['title' => $title];
192  $result = elgg_trigger_event_results('format', 'friendly:title', $params, null);
193  if (is_string($result)) {
194  return $result;
195  }
196 
197  // titles are often stored HTML encoded
198  $title = html_entity_decode($title ?? '', ENT_QUOTES, 'UTF-8');
199 
200  $title = \Elgg\Translit::urlize($title);
201 
202  // limit length to prevent issues with too long URLS (Request-URI Too Large)
203  return elgg_substr($title, 0, 100);
204 }
205 
217 function elgg_get_friendly_time(int $time, int $current_time = null): string {
218 
219  if (!isset($current_time)) {
220  $current_time = time();
221  }
222 
223  // return a time string to short circuit normal time formatting
224  $params = ['time' => $time, 'current_time' => $current_time];
225  $result = elgg_trigger_event_results('format', 'friendly:time', $params, null);
226  if (is_string($result)) {
227  return $result;
228  }
229 
230  $diff = abs($current_time - $time);
231 
232  $minute = 60;
233  $hour = $minute * 60;
234  $day = $hour * 24;
235 
236  if ($diff < $minute) {
237  return elgg_echo('friendlytime:justnow');
238  }
239 
240  if ($diff < $hour) {
241  $granularity = ':minutes';
242  $diff = round($diff / $minute);
243  } else if ($diff < $day) {
244  $granularity = ':hours';
245  $diff = round($diff / $hour);
246  } else {
247  $granularity = ':days';
248  $diff = round($diff / $day);
249  }
250 
251  if ($diff == 0) {
252  $diff = 1;
253  }
254 
255  $future = ($current_time - $time < 0) ? ':future' : '';
256  $singular = ($diff == 1) ? ':singular' : '';
257 
258  return elgg_echo("friendlytime{$future}{$granularity}{$singular}", [$diff]);
259 }
260 
268 function elgg_get_friendly_upload_error(int $error_code): string {
269  switch ($error_code) {
270  case UPLOAD_ERR_OK:
271  return '';
272 
273  case UPLOAD_ERR_INI_SIZE:
274  $key = 'ini_size';
275  break;
276 
277  case UPLOAD_ERR_FORM_SIZE:
278  $key = 'form_size';
279  break;
280 
281  case UPLOAD_ERR_PARTIAL:
282  $key = 'partial';
283  break;
284 
285  case UPLOAD_ERR_NO_FILE:
286  $key = 'no_file';
287  break;
288 
289  case UPLOAD_ERR_NO_TMP_DIR:
290  $key = 'no_tmp_dir';
291  break;
292 
293  case UPLOAD_ERR_CANT_WRITE:
294  $key = 'cant_write';
295  break;
296 
297  case UPLOAD_ERR_EXTENSION:
298  $key = 'extension';
299  break;
300 
301  default:
302  $key = 'unknown';
303  break;
304  }
305 
306  return elgg_echo("upload:error:{$key}");
307 }
308 
319 function elgg_strip_tags(string $string, string $allowable_tags = null): string {
320  return _elgg_services()->html_formatter->stripTags($string, $allowable_tags);
321 }
322 
350 function elgg_html_decode(string $string): string {
351  return _elgg_services()->html_formatter->decode($string);
352 }
353 
362 function _elgg_get_display_query(string $string): string {
363  if (empty($string)) {
364  return $string;
365  }
366 
367  $string = htmlentities($string, ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401, 'UTF-8');
368 
369  return htmlspecialchars($string, ENT_QUOTES, 'UTF-8', false);
370 }
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:217
$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:362
elgg_normalize_site_url(string $unsafe_url)
From untrusted input, get a site URL safe for forwarding.
Definition: output.php:172
$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
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
$options
Elgg admin footer.
Definition: footer.php:6
$html
Definition: section.php:10
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:189
elgg_strpos()
Wrapper function for mb_strpos().
Definition: mb_wrapper.php:71
static urlize($string, $separator= '-')
Create a version of a string for embedding in a URL.
Definition: Translit.php:41
$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:268
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:230
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:346
$vars['head']
Definition: html.php:24
elgg_format_element(string $tag_name, array $attributes=[], string $text= '', array $options=[])
Format an HTML element.
Definition: output.php:142
elgg_normalize_url(string $url)
Definition: output.php:160
_elgg_view_under_viewtype(string $view, array $vars, string $viewtype)
Render a view while the global viewtype is temporarily changed.
Definition: views.php:1445
$text
Definition: button.php:32
$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:152
elgg_html_decode(string $string)
Decode HTML markup into a raw text string.
Definition: output.php:350
elgg_strip_tags(string $string, string $allowable_tags=null)
Strip tags and offer plugins the chance.
Definition: output.php:319