Elgg  Version 4.3
output.php
Go to the documentation of this file.
1 <?php
20 function elgg_format_html($html, array $options = []) {
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 
46  return _elgg_services()->html_formatter->parseEmails($text);
47 }
48 
56 function elgg_autop($string) {
57  return _elgg_services()->html_formatter->addParagaraphs($string);
58 }
59 
72 function elgg_get_excerpt($text, $num_chars = 250) {
73  $view = 'output/excerpt';
74  $vars = [
75  'text' => $text,
76  'num_chars' => $num_chars,
77  ];
78  $viewtype = elgg_view_exists($view) ? '' : 'default';
79 
81 }
82 
92 function elgg_format_bytes($size, $precision = 2) {
93  $size = (int) $size;
94  if ($size < 0) {
95  return false;
96  }
97 
98  $precision = (int) $precision;
99  if ($precision < 0) {
100  $precision = 2;
101  }
102 
103  $base = log($size) / log(1024);
104  $suffixes = ['B', 'kB', 'MB', 'GB', 'TB'];
105 
106  return round(pow(1024, $base - floor($base)), $precision) . ' ' . $suffixes[floor($base)];
107 }
108 
135 function elgg_format_element($tag_name, array $attributes = [], $text = '', array $options = []) {
136  return _elgg_services()->html_formatter->formatElement($tag_name, $attributes, $text, $options);
137 }
138 
153 function elgg_normalize_url($url): string {
154  return _elgg_services()->urls->normalizeUrl((string) $url);
155 }
156 
166 function elgg_normalize_site_url($unsafe_url) {
167  if (!is_string($unsafe_url)) {
168  return false;
169  }
170 
171  $unsafe_url = _elgg_services()->urls->normalizeUrl((string) $unsafe_url);
172  if (0 === elgg_strpos($unsafe_url, elgg_get_site_url())) {
173  return $unsafe_url;
174  }
175 
176  return false;
177 }
178 
188 
189  // return a URL friendly title to short circuit normal title formatting
190  $params = ['title' => $title];
191  $result = elgg_trigger_plugin_hook('format', 'friendly:title', $params, null);
192  if ($result) {
193  return $result;
194  }
195 
196  // titles are often stored HTML encoded
197  $title = html_entity_decode($title ?? '', ENT_QUOTES, 'UTF-8');
198 
200 
201  // limit length to prevent issues with too long URLS (Request-URI Too Large)
202  return elgg_substr($title, 0, 100);
203 }
204 
216 function elgg_get_friendly_time($time, $current_time = null) {
217 
218  if (!isset($current_time)) {
219  $current_time = time();
220  }
221 
222  // return a time string to short circuit normal time formatting
223  $params = ['time' => $time, 'current_time' => $current_time];
224  $result = elgg_trigger_plugin_hook('format', 'friendly:time', $params, null);
225  if ($result) {
226  return $result;
227  }
228 
229  $diff = abs((int) $current_time - (int) $time);
230 
231  $minute = 60;
232  $hour = $minute * 60;
233  $day = $hour * 24;
234 
235  if ($diff < $minute) {
236  return elgg_echo("friendlytime:justnow");
237  }
238 
239  if ($diff < $hour) {
240  $granularity = ':minutes';
241  $diff = round($diff / $minute);
242  } else if ($diff < $day) {
243  $granularity = ':hours';
244  $diff = round($diff / $hour);
245  } else {
246  $granularity = ':days';
247  $diff = round($diff / $day);
248  }
249 
250  if ($diff == 0) {
251  $diff = 1;
252  }
253 
254  $future = ((int) $current_time - (int) $time < 0) ? ':future' : '';
255  $singular = ($diff == 1) ? ':singular' : '';
256 
257  return elgg_echo("friendlytime{$future}{$granularity}{$singular}", [$diff]);
258 }
259 
266 function elgg_get_friendly_upload_error($error_code) {
267  switch ($error_code) {
268  case UPLOAD_ERR_OK:
269  return '';
270 
271  case UPLOAD_ERR_INI_SIZE:
272  $key = 'ini_size';
273  break;
274 
275  case UPLOAD_ERR_FORM_SIZE:
276  $key = 'form_size';
277  break;
278 
279  case UPLOAD_ERR_PARTIAL:
280  $key = 'partial';
281  break;
282 
283  case UPLOAD_ERR_NO_FILE:
284  $key = 'no_file';
285  break;
286 
287  case UPLOAD_ERR_NO_TMP_DIR:
288  $key = 'no_tmp_dir';
289  break;
290 
291  case UPLOAD_ERR_CANT_WRITE:
292  $key = 'cant_write';
293  break;
294 
295  case UPLOAD_ERR_EXTENSION:
296  $key = 'extension';
297  break;
298 
299  default:
300  $key = 'unknown';
301  break;
302  }
303 
304  return elgg_echo("upload:error:$key");
305 }
306 
317 function elgg_strip_tags($string, $allowable_tags = null) {
318  return _elgg_services()->html_formatter->stripTags($string, $allowable_tags);
319 }
320 
348 function elgg_html_decode($string) {
349  return _elgg_services()->html_formatter->decode($string);
350 }
351 
361 function _elgg_get_display_query($string) {
362  if (empty($string)) {
363  return $string;
364  }
365 
366  //encode <,>,&, quotes and characters above 127
367  if (function_exists('mb_convert_encoding')) {
368  $display_query = mb_convert_encoding($string, 'HTML-ENTITIES', 'UTF-8');
369  } else {
370  // if no mbstring extension, we just strip characters
371  $display_query = preg_replace("/[^\x01-\x7F]/", "", $string);
372  }
373 
374  return htmlspecialchars($display_query, ENT_QUOTES, 'UTF-8', false);
375 }
elgg_view_exists($view, $viewtype= '', $recurse=true)
Returns whether the specified view exists.
Definition: views.php:152
elgg_format_html($html, array $options=[])
Output functions Processing text for output such as pulling out URLs and extracting excerpts...
Definition: output.php:20
$params
Saves global plugin settings.
Definition: save.php:13
elgg_normalize_url($url)
Definition: output.php:153
elgg_html_decode($string)
Decode HTML markup into a raw text string.
Definition: output.php:348
elgg_strip_tags($string, $allowable_tags=null)
Strip tags and offer plugins the chance.
Definition: output.php:317
$title
Definition: generic.php:50
elgg_normalize_site_url($unsafe_url)
From untrusted input, get a site URL safe for forwarding.
Definition: output.php:166
_elgg_get_display_query($string)
Prepares query string for output to prevent CSRF attacks.
Definition: output.php:361
if(!$annotation instanceof ElggAnnotation) $time
Definition: time.php:20
$num_chars
Definition: excerpt.php:16
elgg_echo($message_key, array $args=[], $language="")
Elgg language module Functions to manage language and translations.
Definition: languages.php:18
elgg_parse_urls(string $text)
Takes a string and turns any URLs into formatted links.
Definition: output.php:32
elgg_get_friendly_upload_error($error_code)
Returns a human-readable message for PHP&#39;s upload error codes.
Definition: output.php:266
$options
Elgg admin footer.
Definition: footer.php:6
$html
Definition: section.php:10
elgg_autop($string)
Create paragraphs from text with line spacing.
Definition: output.php:56
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_format_element($tag_name, array $attributes=[], $text= '', array $options=[])
Format an HTML element.
Definition: output.php:135
elgg_strpos()
Wrapper function for mb_strpos().
Definition: mb_wrapper.php:69
static urlize($string, $separator= '-')
Create a version of a string for embedding in a URL.
Definition: Translit.php:41
elgg_parse_emails($text)
Takes a string and turns any email addresses into formatted links.
Definition: output.php:45
elgg_trigger_plugin_hook($hook, $type, $params=null, $returnvalue=null)
Definition: elgglib.php:380
elgg_format_bytes($size, $precision=2)
Format bytes to a human readable format.
Definition: output.php:92
$viewtype
Definition: default.php:11
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:219
elgg_get_friendly_time($time, $current_time=null)
Formats a UNIX timestamp in a friendly way (eg "less than a minute ago")
Definition: output.php:216
elgg_get_excerpt($text, $num_chars=250)
Returns an excerpt.
Definition: output.php:72
foreach($plugin_guids as $guid) if(empty($deactivated_plugins)) $url
Definition: deactivate.php:39
_elgg_services()
Get the global service provider.
Definition: elgglib.php:638
elgg_get_friendly_title($title)
When given a title, returns a version suitable for inclusion in a URL.
Definition: output.php:187
$vars['head']
Definition: html.php:24
_elgg_view_under_viewtype($view, $vars, $viewtype)
Render a view while the global viewtype is temporarily changed.
Definition: views.php:1487
$text
Definition: button.php:32
$attributes
Elgg AJAX loader.
Definition: ajax_loader.php:10