Elgg  Version 4.x
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 
31 function parse_urls($text) {
32  return _elgg_services()->html_formatter->parseUrls($text);
33 }
34 
45  return _elgg_services()->html_formatter->parseEmails($text);
46 }
47 
55 function elgg_autop($string) {
56  return _elgg_services()->html_formatter->addParagaraphs($string);
57 }
58 
71 function elgg_get_excerpt($text, $num_chars = 250) {
72  $view = 'output/excerpt';
73  $vars = [
74  'text' => $text,
75  'num_chars' => $num_chars,
76  ];
77  $viewtype = elgg_view_exists($view) ? '' : 'default';
78 
80 }
81 
91 function elgg_format_bytes($size, $precision = 2) {
92  $size = (int) $size;
93  if ($size < 0) {
94  return false;
95  }
96 
97  $precision = (int) $precision;
98  if ($precision < 0) {
99  $precision = 2;
100  }
101 
102  $base = log($size) / log(1024);
103  $suffixes = ['B', 'kB', 'MB', 'GB', 'TB'];
104 
105  return round(pow(1024, $base - floor($base)), $precision) . ' ' . $suffixes[floor($base)];
106 }
107 
134 function elgg_format_element($tag_name, array $attributes = [], $text = '', array $options = []) {
135  return _elgg_services()->html_formatter->formatElement($tag_name, $attributes, $text, $options);
136 }
137 
153  if (!isset($url)) {
154  $url = '';
155  }
156 
157  $url = str_replace(' ', '%20', $url);
158 
160  // fix invalid scheme in site url
161  $protocol_less_site_url = preg_replace('/^https?:/i', ':', elgg_get_site_url());
162  $protocol_less_site_url = rtrim($protocol_less_site_url, '/');
163  $protocol_less_site_url = str_replace('/', '\/', $protocol_less_site_url);
164 
165  return preg_replace("/^https?{$protocol_less_site_url}\/?/i", elgg_get_site_url(), $url);
166  }
167 
168  $matches = [];
169  if (preg_match("#^([a-z]+)\\:#", $url, $matches)) {
170  // we don't let http/https: URLs fail filter_var(), but anything else starting with a protocol
171  // is OK
172  if ($matches[1] !== 'http' && $matches[1] !== 'https') {
173  return $url;
174  }
175  }
176 
177  if (preg_match("#^(\\#|\\?|//)#", $url)) {
178  // starts with '//' (protocol-relative link), query, or fragment
179  return $url;
180  }
181 
182  if (preg_match("#^[^/]*\\.php(\\?.*)?$#", $url)) {
183  // root PHP scripts: 'install.php', 'install.php?step=step'. We don't want to confuse these
184  // for domain names.
185  return elgg_get_site_url() . $url;
186  }
187 
188  if (preg_match("#^[^/?]*\\.#", $url)) {
189  // URLs starting with domain: 'example.com', 'example.com/subpage'
190  return "http://$url";
191  }
192 
193  // 'page/handler', 'mod/plugin/file.php'
194  // trim off any leading / because the site URL is stored
195  // with a trailing /
196  return elgg_get_site_url() . ltrim($url, '/');
197 }
198 
208 function elgg_normalize_site_url($unsafe_url) {
209  if (!is_string($unsafe_url)) {
210  return false;
211  }
212 
213  $unsafe_url = elgg_normalize_url($unsafe_url);
214  if (0 === elgg_strpos($unsafe_url, elgg_get_site_url())) {
215  return $unsafe_url;
216  }
217 
218  return false;
219 }
220 
230 
231  // return a URL friendly title to short circuit normal title formatting
232  $params = ['title' => $title];
233  $result = elgg_trigger_plugin_hook('format', 'friendly:title', $params, null);
234  if ($result) {
235  return $result;
236  }
237 
238  // titles are often stored HTML encoded
239  $title = html_entity_decode($title ?? '', ENT_QUOTES, 'UTF-8');
240 
242 
243  // limit length to prevent issues with too long URLS (Request-URI Too Large)
244  return elgg_substr($title, 0, 100);
245 }
246 
258 function elgg_get_friendly_time($time, $current_time = null) {
259 
260  if (!isset($current_time)) {
261  $current_time = time();
262  }
263 
264  // return a time string to short circuit normal time formatting
265  $params = ['time' => $time, 'current_time' => $current_time];
266  $result = elgg_trigger_plugin_hook('format', 'friendly:time', $params, null);
267  if ($result) {
268  return $result;
269  }
270 
271  $diff = abs((int) $current_time - (int) $time);
272 
273  $minute = 60;
274  $hour = $minute * 60;
275  $day = $hour * 24;
276 
277  if ($diff < $minute) {
278  return elgg_echo("friendlytime:justnow");
279  }
280 
281  if ($diff < $hour) {
282  $granularity = ':minutes';
283  $diff = round($diff / $minute);
284  } else if ($diff < $day) {
285  $granularity = ':hours';
286  $diff = round($diff / $hour);
287  } else {
288  $granularity = ':days';
289  $diff = round($diff / $day);
290  }
291 
292  if ($diff == 0) {
293  $diff = 1;
294  }
295 
296  $future = ((int) $current_time - (int) $time < 0) ? ':future' : '';
297  $singular = ($diff == 1) ? ':singular' : '';
298 
299  return elgg_echo("friendlytime{$future}{$granularity}{$singular}", [$diff]);
300 }
301 
308 function elgg_get_friendly_upload_error($error_code) {
309  switch ($error_code) {
310  case UPLOAD_ERR_OK:
311  return '';
312 
313  case UPLOAD_ERR_INI_SIZE:
314  $key = 'ini_size';
315  break;
316 
317  case UPLOAD_ERR_FORM_SIZE:
318  $key = 'form_size';
319  break;
320 
321  case UPLOAD_ERR_PARTIAL:
322  $key = 'partial';
323  break;
324 
325  case UPLOAD_ERR_NO_FILE:
326  $key = 'no_file';
327  break;
328 
329  case UPLOAD_ERR_NO_TMP_DIR:
330  $key = 'no_tmp_dir';
331  break;
332 
333  case UPLOAD_ERR_CANT_WRITE:
334  $key = 'cant_write';
335  break;
336 
337  case UPLOAD_ERR_EXTENSION:
338  $key = 'extension';
339  break;
340 
341  default:
342  $key = 'unknown';
343  break;
344  }
345 
346  return elgg_echo("upload:error:$key");
347 }
348 
359 function elgg_strip_tags($string, $allowable_tags = null) {
360  return _elgg_services()->html_formatter->stripTags($string, $allowable_tags);
361 }
362 
390 function elgg_html_decode($string) {
391  return _elgg_services()->html_formatter->decode($string);
392 }
393 
403 function _elgg_get_display_query($string) {
404  if (empty($string)) {
405  return $string;
406  }
407 
408  //encode <,>,&, quotes and characters above 127
409  if (function_exists('mb_convert_encoding')) {
410  $display_query = mb_convert_encoding($string, 'HTML-ENTITIES', 'UTF-8');
411  } else {
412  // if no mbstring extension, we just strip characters
413  $display_query = preg_replace("/[^\x01-\x7F]/", "", $string);
414  }
415 
416  return htmlspecialchars($display_query, ENT_QUOTES, 'UTF-8', false);
417 }
418 
427  // based on http://php.net/manual/en/function.filter-var.php#104160
428  $res = filter_var($url, FILTER_VALIDATE_URL);
429  if ($res) {
430  return $res;
431  }
432 
433  // Check if it has unicode chars.
434  $l = elgg_strlen($url);
435  if (strlen($url) == $l) {
436  return $res;
437  }
438 
439  // Replace wide chars by X
440  $s = '';
441  for ($i = 0; $i < $l; ++$i) {
442  $ch = elgg_substr($url, $i, 1);
443  $s .= (strlen($ch) > 1) ? 'X' : $ch;
444  }
445 
446  // Re-check now.
447  return filter_var($s, FILTER_VALIDATE_URL) ? $url : false;
448 }
elgg_view_exists($view, $viewtype= '', $recurse=true)
Returns whether the specified view exists.
Definition: views.php:178
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
parse_urls($text)
Takes a string and turns any URLs into formatted links.
Definition: output.php:31
elgg_normalize_url($url)
Definition: output.php:152
_elgg_sane_validate_url($url)
Use a "fixed" filter_var() with FILTER_VALIDATE_URL that handles multi-byte chars.
Definition: output.php:426
elgg_html_decode($string)
Decode HTML markup into a raw text string.
Definition: output.php:390
elgg_strip_tags($string, $allowable_tags=null)
Strip tags and offer plugins the chance.
Definition: output.php:359
$title
Definition: generic.php:50
elgg_normalize_site_url($unsafe_url)
From untrusted input, get a site URL safe for forwarding.
Definition: output.php:208
_elgg_get_display_query($string)
Prepares query string for output to prevent CSRF attacks.
Definition: output.php:403
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_get_friendly_upload_error($error_code)
Returns a human-readable message for PHP&#39;s upload error codes.
Definition: output.php:308
$options
Elgg admin footer.
Definition: footer.php:6
elgg_strlen()
Wrapper function for mb_strlen().
Definition: mb_wrapper.php:52
$html
Definition: section.php:10
elgg_autop($string)
Create paragraphs from text with line spacing.
Definition: output.php:55
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:134
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:44
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:91
$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:258
elgg_get_excerpt($text, $num_chars=250)
Returns an excerpt.
Definition: output.php:71
foreach($plugin_guids as $guid) if(empty($deactivated_plugins)) $url
Definition: deactivate.php:39
_elgg_services()
Get the global service provider.
Definition: elgglib.php:777
elgg_get_friendly_title($title)
When given a title, returns a version suitable for inclusion in a URL.
Definition: output.php:229
$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:1513
$text
Definition: button.php:32
$attributes
Elgg AJAX loader.
Definition: ajax_loader.php:10