Elgg  Version 2.3
output.php
Go to the documentation of this file.
1 <?php
17 function parse_urls($text) {
18 
19  $linkify = new \Misd\Linkify\Linkify();
20 
21  return $linkify->processUrls($text, ['attr' => ['rel' => 'nofollow']]);
22 }
23 
34  $linkify = new \Misd\Linkify\Linkify();
35 
36  return $linkify->processEmails($text, ['attr' => ['rel' => 'nofollow']]);
37 }
38 
46 function elgg_autop($string) {
47  return _elgg_services()->autoP->process($string);
48 }
49 
62 function elgg_get_excerpt($text, $num_chars = 250) {
63  $view = 'output/excerpt';
64  $vars = [
65  'text' => $text,
66  'num_chars' => $num_chars,
67  ];
68  $viewtype = elgg_view_exists($view) ? '' : 'default';
69 
70  return _elgg_view_under_viewtype($view, $vars, $viewtype);
71 }
72 
81 function elgg_format_url($url) {
82  return preg_replace('/&(?!amp;)/', '&amp;', $url);
83 }
84 
95 function elgg_format_bytes($size, $precision = 2) {
96  if (!$size || $size < 0) {
97  return false;
98  }
99 
100  $base = log($size) / log(1024);
101  $suffixes = array('B', 'kB', 'MB', 'GB', 'TB');
102 
103  return round(pow(1024, $base - floor($base)), $precision) . ' ' . $suffixes[floor($base)];
104 }
105 
129 function elgg_format_attributes(array $attrs = array()) {
130  if (!is_array($attrs) || empty($attrs)) {
131  return '';
132  }
133 
134  $attributes = [];
135 
136  foreach ($attrs as $attr => $val) {
137  if (0 !== strpos($attr, 'data-') && false !== strpos($attr, '_')) {
138  // this is probably a view $vars variable not meant for output
139  continue;
140  }
141 
142  $attr = strtolower($attr);
143 
144  if (!isset($val) || $val === false) {
145  continue;
146  }
147 
148  if ($val === true) {
149  $val = $attr; //e.g. checked => true ==> checked="checked"
150  }
151 
152  if (is_scalar($val)) {
153  $val = [$val];
154  }
155 
156  if (!is_array($val)) {
157  continue;
158  }
159 
160  // Check if array contains non-scalar values and bail if so
161  $filtered_val = array_filter($val, function($e) {
162  return is_scalar($e);
163  });
164 
165  if (count($val) != count($filtered_val)) {
166  continue;
167  }
168 
169  $val = implode(' ', $val);
170 
171  $val = htmlspecialchars($val, ENT_QUOTES, 'UTF-8', false);
172  $attributes[] = "$attr=\"$val\"";
173  }
174 
175  return implode(' ', $attributes);
176 }
177 
208 function elgg_format_element($tag_name, array $attributes = array(), $text = '', array $options = array()) {
209  if (is_array($tag_name)) {
210  $args = $tag_name;
211 
212  if ($attributes !== [] || $text !== '' || $options !== []) {
213  throw new \InvalidArgumentException('If $tag_name is an array, the other arguments must not be set');
214  }
215 
216  if (isset($args['#tag_name'])) {
217  $tag_name = $args['#tag_name'];
218  }
219  if (isset($args['#text'])) {
220  $text = $args['#text'];
221  }
222  if (isset($args['#options'])) {
223  $options = $args['#options'];
224  }
225 
226  unset($args['#tag_name'], $args['#text'], $args['#options']);
227  $attributes = $args;
228  }
229 
230  if (!is_string($tag_name) || $tag_name === '') {
231  throw new \InvalidArgumentException('$tag_name is required');
232  }
233 
234  if (isset($options['is_void'])) {
235  $is_void = $options['is_void'];
236  } else {
237  // from http://www.w3.org/TR/html-markup/syntax.html#syntax-elements
238  $is_void = in_array(strtolower($tag_name), array(
239  'area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', 'keygen', 'link', 'menuitem',
240  'meta', 'param', 'source', 'track', 'wbr'
241  ));
242  }
243 
244  if (!empty($options['encode_text'])) {
245  $double_encode = empty($options['double_encode']) ? false : true;
246  $text = htmlspecialchars($text, ENT_QUOTES, 'UTF-8', $double_encode);
247  }
248 
249  if ($attributes) {
251  if ($attrs !== '') {
252  $attrs = " $attrs";
253  }
254  } else {
255  $attrs = '';
256  }
257 
258  if ($is_void) {
259  return empty($options['is_xml']) ? "<{$tag_name}{$attrs}>" : "<{$tag_name}{$attrs} />";
260  } else {
261  return "<{$tag_name}{$attrs}>$text</$tag_name>";
262  }
263 }
264 
281  $url = str_replace(' ', '%20', $url);
282 
284  return $url;
285  }
286 
287  if (preg_match("#^([a-z]+)\\:#", $url, $m)) {
288  // we don't let http/https: URLs fail filter_var(), but anything else starting with a protocol
289  // is OK
290  if ($m[1] !== 'http' && $m[1] !== 'https') {
291  return $url;
292  }
293  }
294 
295  if (preg_match("#^(\\#|\\?|//)#", $url)) {
296  // starts with '//' (protocol-relative link), query, or fragment
297  return $url;
298  }
299 
300  if (preg_match("#^[^/]*\\.php(\\?.*)?$#", $url)) {
301  // root PHP scripts: 'install.php', 'install.php?step=step'. We don't want to confuse these
302  // for domain names.
303  return elgg_get_site_url() . $url;
304  }
305 
306  if (preg_match("#^[^/?]*\\.#", $url)) {
307  // URLs starting with domain: 'example.com', 'example.com/subpage'
308  return "http://$url";
309  }
310 
311  // 'page/handler', 'mod/plugin/file.php'
312  // trim off any leading / because the site URL is stored
313  // with a trailing /
314  return elgg_get_site_url() . ltrim($url, '/');
315 }
316 
326 function elgg_normalize_site_url($unsafe_url) {
327  if (!is_string($unsafe_url)) {
328  return false;
329  }
330 
331  $unsafe_url = elgg_normalize_url($unsafe_url);
332  if (0 === strpos($unsafe_url, elgg_get_site_url())) {
333  return $unsafe_url;
334  }
335 
336  return false;
337 }
338 
348 
349  // return a URL friendly title to short circuit normal title formatting
350  $params = array('title' => $title);
351  $result = elgg_trigger_plugin_hook('format', 'friendly:title', $params, null);
352  if ($result) {
353  return $result;
354  }
355 
356  // titles are often stored HTML encoded
357  $title = html_entity_decode($title, ENT_QUOTES, 'UTF-8');
358 
360 
361  return $title;
362 }
363 
375 function elgg_get_friendly_time($time, $current_time = null) {
376 
377  if (!$current_time) {
378  $current_time = time();
379  }
380 
381  // return a time string to short circuit normal time formatting
382  $params = array('time' => $time, 'current_time' => $current_time);
383  $result = elgg_trigger_plugin_hook('format', 'friendly:time', $params, null);
384  if ($result) {
385  return $result;
386  }
387 
388  $diff = abs((int)$current_time - (int)$time);
389 
390  $minute = 60;
391  $hour = $minute * 60;
392  $day = $hour * 24;
393 
394  if ($diff < $minute) {
395  return elgg_echo("friendlytime:justnow");
396  }
397 
398  if ($diff < $hour) {
399  $granularity = ':minutes';
400  $diff = round($diff / $minute);
401  } else if ($diff < $day) {
402  $granularity = ':hours';
403  $diff = round($diff / $hour);
404  } else {
405  $granularity = ':days';
406  $diff = round($diff / $day);
407  }
408 
409  if ($diff == 0) {
410  $diff = 1;
411  }
412 
413  $future = ((int)$current_time - (int)$time < 0) ? ':future' : '';
414  $singular = ($diff == 1) ? ':singular' : '';
415 
416  return elgg_echo("friendlytime{$future}{$granularity}{$singular}", array($diff));
417 }
418 
425 function elgg_get_friendly_upload_error($error_code) {
426  switch ($error_code) {
427  case UPLOAD_ERR_OK:
428  return '';
429 
430  case UPLOAD_ERR_INI_SIZE:
431  $key = 'ini_size';
432  break;
433 
434  case UPLOAD_ERR_FORM_SIZE:
435  $key = 'form_size';
436  break;
437 
438  case UPLOAD_ERR_PARTIAL:
439  $key = 'partial';
440  break;
441 
442  case UPLOAD_ERR_NO_FILE:
443  $key = 'no_file';
444  break;
445 
446  case UPLOAD_ERR_NO_TMP_DIR:
447  $key = 'no_tmp_dir';
448  break;
449 
450  case UPLOAD_ERR_CANT_WRITE:
451  $key = 'cant_write';
452  break;
453 
454  case UPLOAD_ERR_EXTENSION:
455  $key = 'extension';
456  break;
457 
458  default:
459  $key = 'unknown';
460  break;
461  }
462 
463  return elgg_echo("upload:error:$key");
464 }
465 
466 
477 function elgg_strip_tags($string, $allowable_tags = null) {
478  $params['original_string'] = $string;
479  $params['allowable_tags'] = $allowable_tags;
480 
481  $string = strip_tags($string, $allowable_tags);
482  $string = elgg_trigger_plugin_hook('format', 'strip_tags', $params, $string);
483 
484  return $string;
485 }
486 
515  $string = str_replace(
516  array('&gt;', '&lt;', '&amp;', '&quot;', '&#039;'),
517  array('&amp;gt;', '&amp;lt;', '&amp;amp;', '&amp;quot;', '&amp;#039;'),
518  $string
519  );
520  $string = html_entity_decode($string, ENT_NOQUOTES, 'UTF-8');
521  $string = str_replace(
522  array('&amp;gt;', '&amp;lt;', '&amp;amp;', '&amp;quot;', '&amp;#039;'),
523  array('&gt;', '&lt;', '&amp;', '&quot;', '&#039;'),
524  $string
525  );
526  return $string;
527 }
528 
541  elgg_deprecated_notice(__FUNCTION__ . ' is deprecated. Use elgg_html_decode()', '2.0');
542  return elgg_html_decode($string);
543 }
544 
554  //encode <,>,&, quotes and characters above 127
555  if (function_exists('mb_convert_encoding')) {
556  $display_query = mb_convert_encoding($string, 'HTML-ENTITIES', 'UTF-8');
557  } else {
558  // if no mbstring extension, we just strip characters
559  $display_query = preg_replace("/[^\x01-\x7F]/", "", $string);
560  }
561  return htmlspecialchars($display_query, ENT_QUOTES, 'UTF-8', false);
562 }
563 
572  // based on http://php.net/manual/en/function.filter-var.php#104160
573  $res = filter_var($url, FILTER_VALIDATE_URL);
574  if ($res) {
575  return $res;
576  }
577 
578  // Check if it has unicode chars.
579  $l = elgg_strlen($url);
580  if (strlen($url) == $l) {
581  return $res;
582  }
583 
584  // Replace wide chars by “X”.
585  $s = '';
586  for ($i = 0; $i < $l; ++$i) {
587  $ch = elgg_substr($url, $i, 1);
588  $s .= (strlen($ch) > 1) ? 'X' : $ch;
589  }
590 
591  // Re-check now.
592  return filter_var($s, FILTER_VALIDATE_URL) ? $url : false;
593 }
594 
595 return function(\Elgg\EventsService $events, \Elgg\HooksRegistrationService $hooks) {
596 
597 };
elgg_view_exists($view, $viewtype= '', $recurse=true)
Returns whether the specified view exists.
Definition: views.php:293
$view
Definition: crop.php:34
parse_urls($text)
Takes a string and turns any URLs into formatted links.
Definition: output.php:17
$m
Definition: metadata.php:11
elgg_normalize_url($url)
Definition: output.php:280
_elgg_sane_validate_url($url)
Use a "fixed" filter_var() with FILTER_VALIDATE_URL that handles multi-byte chars.
Definition: output.php:571
elgg_html_decode($string)
Decode HTML markup into a raw text string.
Definition: output.php:514
elgg_echo($message_key, $args=array(), $language="")
Given a message key, returns an appropriately translated full-text string.
Definition: languages.php:21
$e
Definition: metadata.php:12
elgg_strip_tags($string, $allowable_tags=null)
Strip tags and offer plugins the chance.
Definition: output.php:477
elgg_normalize_site_url($unsafe_url)
From untrusted input, get a site URL safe for forwarding.
Definition: output.php:326
_elgg_get_display_query($string)
Prepares query string for output to prevent CSRF attacks.
Definition: output.php:553
$args
Some servers don&#39;t allow PHP to check the rewrite, so try via AJAX.
$num_chars
Definition: excerpt.php:16
$url
Definition: exceptions.php:24
$vars['entity']
$title
Definition: save.php:22
elgg_get_friendly_upload_error($error_code)
Returns a human-readable message for PHP&#39;s upload error codes.
Definition: output.php:425
$options
Elgg admin footer.
Definition: footer.php:6
elgg_format_element($tag_name, array $attributes=array(), $text= '', array $options=array())
Format an HTML element.
Definition: output.php:208
$string
elgg_strlen()
Wrapper function for mb_strlen().
Definition: mb_wrapper.php:72
$params
Definition: login.php:72
$text
Definition: default.php:25
elgg_autop($string)
Create paragraphs from text with line spacing.
Definition: output.php:46
$key
Definition: summary.php:34
static urlize($string, $separator= '-')
Create a version of a string for embedding in a URL.
Definition: Translit.php:40
elgg_parse_emails($text)
Takes a string and turns any email addresses into formatted links.
Definition: output.php:33
elgg_trigger_plugin_hook($hook, $type, $params=null, $returnvalue=null)
Definition: elgglib.php:826
elgg_deprecated_notice($msg, $dep_version, $backtrace_level=1)
Log a notice about deprecated use of a function, view, etc.
Definition: elgglib.php:1098
elgg_format_bytes($size, $precision=2)
Format bytes to a human readable format.
Definition: output.php:95
elgg_get_site_url($site_guid=0)
Get the URL for the current (or specified) site.
_elgg_html_decode($string)
Alias of elgg_html_decode.
Definition: output.php:540
elgg_format_attributes(array $attrs=array())
Converts an associative array into a string of well-formed HTML/XML attributes Returns a concatenated...
Definition: output.php:129
_elgg_services(\Elgg\Di\ServiceProvider $services=null)
Get the global service provider.
Definition: autoloader.php:17
$attrs
Definition: ajax_loader.php:30
elgg_substr()
Wrapper function for mb_substr().
Definition: mb_wrapper.php:239
$size
Definition: default.php:20
elgg subtext time
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:375
elgg_get_excerpt($text, $num_chars=250)
Returns an excerpt.
Definition: output.php:62
elgg_format_url($url)
Handles formatting of ampersands in urls.
Definition: output.php:81
elgg_get_friendly_title($title)
When given a title, returns a version suitable for inclusion in a URL.
Definition: output.php:347
_elgg_view_under_viewtype($view, $vars, $viewtype)
Render a view while the global viewtype is temporarily changed.
Definition: views.php:2070
$attributes
Definition: ajax_loader.php:13
if(!$limit) $attr
Definition: comments.php:22