Elgg  Version 3.0
output.php
Go to the documentation of this file.
1 <?php
23 function elgg_format_html($html, array $options = []) {
24  return _elgg_services()->html_formatter->formatBlock($html, $options);
25 }
26 
34 function parse_urls($text) {
35  return _elgg_services()->html_formatter->parseUrls($text);
36 }
37 
48  return _elgg_services()->html_formatter->parseEmails($text);
49 }
50 
58 function elgg_autop($string) {
59  return _elgg_services()->html_formatter->addParagaraphs($string);
60 }
61 
74 function elgg_get_excerpt($text, $num_chars = 250) {
75  $view = 'output/excerpt';
76  $vars = [
77  'text' => $text,
78  'num_chars' => $num_chars,
79  ];
80  $viewtype = elgg_view_exists($view) ? '' : 'default';
81 
83 }
84 
94 function elgg_format_bytes($size, $precision = 2) {
95  $size = (int) $size;
96  if ($size < 0) {
97  return false;
98  }
99 
100  $precision = (int) $precision;
101  if ($precision < 0) {
102  $precision = 2;
103  }
104 
105  $base = log($size) / log(1024);
106  $suffixes = ['B', 'kB', 'MB', 'GB', 'TB'];
107 
108  return round(pow(1024, $base - floor($base)), $precision) . ' ' . $suffixes[floor($base)];
109 }
110 
134 function elgg_format_attributes(array $attrs = []) {
135  return _elgg_services()->html_formatter->formatAttributes($attrs);
136 }
137 
168 function elgg_format_element($tag_name, array $attributes = [], $text = '', array $options = []) {
169  return _elgg_services()->html_formatter->formatElement($tag_name, $attributes, $text, $options);
170 }
171 
187  $url = str_replace(' ', '%20', $url);
188 
190  // fix invalid scheme in site url
191  $protocol_less_site_url = preg_replace('/^https?:/i', ':', elgg_get_site_url());
192  $protocol_less_site_url = rtrim($protocol_less_site_url, '/');
193  $protocol_less_site_url = str_replace('/', '\/', $protocol_less_site_url);
194 
195  return preg_replace("/^https?{$protocol_less_site_url}\/?/i", elgg_get_site_url(), $url);
196  }
197 
198  if (preg_match("#^([a-z]+)\\:#", $url, $m)) {
199  // we don't let http/https: URLs fail filter_var(), but anything else starting with a protocol
200  // is OK
201  if ($m[1] !== 'http' && $m[1] !== 'https') {
202  return $url;
203  }
204  }
205 
206  if (preg_match("#^(\\#|\\?|//)#", $url)) {
207  // starts with '//' (protocol-relative link), query, or fragment
208  return $url;
209  }
210 
211  if (preg_match("#^[^/]*\\.php(\\?.*)?$#", $url)) {
212  // root PHP scripts: 'install.php', 'install.php?step=step'. We don't want to confuse these
213  // for domain names.
214  return elgg_get_site_url() . $url;
215  }
216 
217  if (preg_match("#^[^/?]*\\.#", $url)) {
218  // URLs starting with domain: 'example.com', 'example.com/subpage'
219  return "http://$url";
220  }
221 
222  // 'page/handler', 'mod/plugin/file.php'
223  // trim off any leading / because the site URL is stored
224  // with a trailing /
225  return elgg_get_site_url() . ltrim($url, '/');
226 }
227 
237 function elgg_normalize_site_url($unsafe_url) {
238  if (!is_string($unsafe_url)) {
239  return false;
240  }
241 
242  $unsafe_url = elgg_normalize_url($unsafe_url);
243  if (0 === strpos($unsafe_url, elgg_get_site_url())) {
244  return $unsafe_url;
245  }
246 
247  return false;
248 }
249 
259 
260  // return a URL friendly title to short circuit normal title formatting
261  $params = ['title' => $title];
262  $result = elgg_trigger_plugin_hook('format', 'friendly:title', $params, null);
263  if ($result) {
264  return $result;
265  }
266 
267  // titles are often stored HTML encoded
268  $title = html_entity_decode($title, ENT_QUOTES, 'UTF-8');
269 
271 
272  return $title;
273 }
274 
286 function elgg_get_friendly_time($time, $current_time = null) {
287 
288  if (!isset($current_time)) {
289  $current_time = time();
290  }
291 
292  // return a time string to short circuit normal time formatting
293  $params = ['time' => $time, 'current_time' => $current_time];
294  $result = elgg_trigger_plugin_hook('format', 'friendly:time', $params, null);
295  if ($result) {
296  return $result;
297  }
298 
299  $diff = abs((int) $current_time - (int) $time);
300 
301  $minute = 60;
302  $hour = $minute * 60;
303  $day = $hour * 24;
304 
305  if ($diff < $minute) {
306  return elgg_echo("friendlytime:justnow");
307  }
308 
309  if ($diff < $hour) {
310  $granularity = ':minutes';
311  $diff = round($diff / $minute);
312  } else if ($diff < $day) {
313  $granularity = ':hours';
314  $diff = round($diff / $hour);
315  } else {
316  $granularity = ':days';
317  $diff = round($diff / $day);
318  }
319 
320  if ($diff == 0) {
321  $diff = 1;
322  }
323 
324  $future = ((int) $current_time - (int) $time < 0) ? ':future' : '';
325  $singular = ($diff == 1) ? ':singular' : '';
326 
327  return elgg_echo("friendlytime{$future}{$granularity}{$singular}", [$diff]);
328 }
329 
336 function elgg_get_friendly_upload_error($error_code) {
337  switch ($error_code) {
338  case UPLOAD_ERR_OK:
339  return '';
340 
341  case UPLOAD_ERR_INI_SIZE:
342  $key = 'ini_size';
343  break;
344 
345  case UPLOAD_ERR_FORM_SIZE:
346  $key = 'form_size';
347  break;
348 
349  case UPLOAD_ERR_PARTIAL:
350  $key = 'partial';
351  break;
352 
353  case UPLOAD_ERR_NO_FILE:
354  $key = 'no_file';
355  break;
356 
357  case UPLOAD_ERR_NO_TMP_DIR:
358  $key = 'no_tmp_dir';
359  break;
360 
361  case UPLOAD_ERR_CANT_WRITE:
362  $key = 'cant_write';
363  break;
364 
365  case UPLOAD_ERR_EXTENSION:
366  $key = 'extension';
367  break;
368 
369  default:
370  $key = 'unknown';
371  break;
372  }
373 
374  return elgg_echo("upload:error:$key");
375 }
376 
377 
388 function elgg_strip_tags($string, $allowable_tags = null) {
389  return _elgg_services()->html_formatter->stripTags($string, $allowable_tags);
390 }
391 
419 function elgg_html_decode($string) {
420  return _elgg_services()->html_formatter->decode($string);
421 }
422 
431 function _elgg_get_display_query($string) {
432  //encode <,>,&, quotes and characters above 127
433  if (function_exists('mb_convert_encoding')) {
434  $display_query = mb_convert_encoding($string, 'HTML-ENTITIES', 'UTF-8');
435  } else {
436  // if no mbstring extension, we just strip characters
437  $display_query = preg_replace("/[^\x01-\x7F]/", "", $string);
438  }
439  return htmlspecialchars($display_query, ENT_QUOTES, 'UTF-8', false);
440 }
441 
450  // based on http://php.net/manual/en/function.filter-var.php#104160
451  $res = filter_var($url, FILTER_VALIDATE_URL);
452  if ($res) {
453  return $res;
454  }
455 
456  // Check if it has unicode chars.
457  $l = elgg_strlen($url);
458  if (strlen($url) == $l) {
459  return $res;
460  }
461 
462  // Replace wide chars by “X”.
463  $s = '';
464  for ($i = 0; $i < $l; ++$i) {
465  $ch = elgg_substr($url, $i, 1);
466  $s .= (strlen($ch) > 1) ? 'X' : $ch;
467  }
468 
469  // Re-check now.
470  return filter_var($s, FILTER_VALIDATE_URL) ? $url : false;
471 }
elgg_view_exists($view, $viewtype= '', $recurse=true)
Returns whether the specified view exists.
Definition: views.php:205
elgg_format_html($html, array $options=[])
Prepare HTML output.
Definition: output.php:23
log($msg, $level=LogLevel::NOTICE)
Log a message.
Definition: Seeding.php:762
elgg_format_attributes(array $attrs=[])
Converts an associative array into a string of well-formed HTML/XML attributes Returns a concatenated...
Definition: output.php:134
$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:34
elgg_normalize_url($url)
Definition: output.php:186
_elgg_sane_validate_url($url)
Use a "fixed" filter_var() with FILTER_VALIDATE_URL that handles multi-byte chars.
Definition: output.php:449
elgg_html_decode($string)
Decode HTML markup into a raw text string.
Definition: output.php:419
elgg_strip_tags($string, $allowable_tags=null)
Strip tags and offer plugins the chance.
Definition: output.php:388
$size
Definition: remove.php:24
elgg_normalize_site_url($unsafe_url)
From untrusted input, get a site URL safe for forwarding.
Definition: output.php:237
_elgg_get_display_query($string)
Prepares query string for output to prevent CSRF attacks.
Definition: output.php:431
$num_chars
Definition: excerpt.php:16
elgg_echo($message_key, array $args=[], $language="")
Given a message key, returns an appropriately translated full-text string.
Definition: languages.php:21
elgg_get_friendly_upload_error($error_code)
Returns a human-readable message for PHP&#39;s upload error codes.
Definition: output.php:336
$options
Elgg admin footer.
Definition: footer.php:6
elgg_strlen()
Wrapper function for mb_strlen().
Definition: mb_wrapper.php:72
$html
Definition: section.php:10
$text
Definition: default.php:28
if(!$owner||!$owner->canEdit()) if(!$owner->hasIcon('master')) if(!$owner->saveIconFromElggFile($owner->getIcon('master'), 'icon', $coords)) $view
Definition: crop.php:30
$title
Definition: user.php:20
elgg_autop($string)
Create paragraphs from text with line spacing.
Definition: output.php:58
if(!$entity instanceof ElggEntity) $time
Definition: time.php:21
elgg_format_element($tag_name, array $attributes=[], $text= '', array $options=[])
Format an HTML element.
Definition: output.php:168
static urlize($string, $separator= '-')
Create a version of a string for embedding in a URL.
Definition: Translit.php:42
elgg_parse_emails($text)
Takes a string and turns any email addresses into formatted links.
Definition: output.php:47
$url
Definition: default.php:33
elgg_trigger_plugin_hook($hook, $type, $params=null, $returnvalue=null)
Definition: elgglib.php:720
elgg_format_bytes($size, $precision=2)
Format bytes to a human readable format.
Definition: output.php:94
$viewtype
Definition: default.php:11
elgg_get_site_url()
Get the URL for the current (or specified) site, ending with "/".
if($container instanceof ElggGroup &&$container->guid!=elgg_get_page_owner_guid()) $key
Definition: summary.php:55
elgg_substr()
Wrapper function for mb_substr().
Definition: mb_wrapper.php:239
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:286
$vars['type']
Definition: save.php:11
elgg_get_excerpt($text, $num_chars=250)
Returns an excerpt.
Definition: output.php:74
if(elgg_extract('hidden', $vars, true)) $attrs
Definition: ajax_loader.php:25
_elgg_services()
Get the global service provider.
Definition: elgglib.php:1292
elgg_get_friendly_title($title)
When given a title, returns a version suitable for inclusion in a URL.
Definition: output.php:258
_elgg_view_under_viewtype($view, $vars, $viewtype)
Render a view while the global viewtype is temporarily changed.
Definition: views.php:1926
$attributes
Definition: ajax_loader.php:13