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  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  $title = \Elgg\Translit::urlize($title);
204 
205  // limit length to prevent issues with too long URLS (Request-URI Too Large)
206  return elgg_substr($title, 0, 100);
207 }
208 
220 function elgg_get_friendly_time(int $time, int $current_time = null): string {
221 
222  if (!isset($current_time)) {
223  $current_time = time();
224  }
225 
226  // return a time string to short circuit normal time formatting
227  $params = ['time' => $time, 'current_time' => $current_time];
228  $result = elgg_trigger_event_results('format', 'friendly:time', $params, null);
229  if (is_string($result)) {
230  return $result;
231  }
232 
233  $diff = abs($current_time - $time);
234 
235  $minute = 60;
236  $hour = $minute * 60;
237  $day = $hour * 24;
238 
239  if ($diff < $minute) {
240  return elgg_echo('friendlytime:justnow');
241  }
242 
243  if ($diff < $hour) {
244  $granularity = ':minutes';
245  $diff = round($diff / $minute);
246  } else if ($diff < $day) {
247  $granularity = ':hours';
248  $diff = round($diff / $hour);
249  } else {
250  $granularity = ':days';
251  $diff = round($diff / $day);
252  }
253 
254  if ($diff == 0) {
255  $diff = 1;
256  }
257 
258  $future = ($current_time - $time < 0) ? ':future' : '';
259  $singular = ($diff == 1) ? ':singular' : '';
260 
261  return elgg_echo("friendlytime{$future}{$granularity}{$singular}", [$diff]);
262 }
263 
271 function elgg_get_friendly_upload_error(int $error_code): string {
272  switch ($error_code) {
273  case UPLOAD_ERR_OK:
274  return '';
275 
276  case UPLOAD_ERR_INI_SIZE:
277  $key = 'ini_size';
278  break;
279 
280  case UPLOAD_ERR_FORM_SIZE:
281  $key = 'form_size';
282  break;
283 
284  case UPLOAD_ERR_PARTIAL:
285  $key = 'partial';
286  break;
287 
288  case UPLOAD_ERR_NO_FILE:
289  $key = 'no_file';
290  break;
291 
292  case UPLOAD_ERR_NO_TMP_DIR:
293  $key = 'no_tmp_dir';
294  break;
295 
296  case UPLOAD_ERR_CANT_WRITE:
297  $key = 'cant_write';
298  break;
299 
300  case UPLOAD_ERR_EXTENSION:
301  $key = 'extension';
302  break;
303 
304  default:
305  $key = 'unknown';
306  break;
307  }
308 
309  return elgg_echo("upload:error:{$key}");
310 }
311 
322 function elgg_strip_tags(string $string, string $allowable_tags = null): string {
323  return _elgg_services()->html_formatter->stripTags($string, $allowable_tags);
324 }
325 
353 function elgg_html_decode(string $string): string {
354  return _elgg_services()->html_formatter->decode($string);
355 }
356 
365 function _elgg_get_display_query(string $string): string {
366  if (empty($string)) {
367  return $string;
368  }
369 
370  $string = htmlentities($string, ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401, 'UTF-8');
371 
372  return htmlspecialchars($string, ENT_QUOTES, 'UTF-8', false);
373 }
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:220
$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:365
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
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
A wrapper to render a section of the page shell.
Definition: section.php:9
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
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:271
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
$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:346
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:353
elgg_strip_tags(string $string, string $allowable_tags=null)
Strip tags and offer plugins the chance.
Definition: output.php:322