Elgg  Version 2.2
/root/Elgg/engine/lib/output.php

Converts shorthand urls to absolute urls.If the url is already absolute or protocol-relative, no change is made.

elgg_normalize_url(''); // 'http://my.site.com/' elgg_normalize_url('dashboard'); // 'http://my.site.com/dashboard' elgg_normalize_url('http://google.com/'); // no change elgg_normalize_url('//google.com/'); // no change

Parameters
string$urlThe URL to normalize
Returns
string The absolute url
<?php
function parse_urls($text) {
// URI specification: http://www.ietf.org/rfc/rfc3986.txt
// This varies from the specification in the following ways:
// * Supports non-ascii characters
// * Does not allow parentheses and single quotes
// * Cuts off commas, exclamation points, and periods off as last character
// @todo this causes problems with <attr = "val">
// must be in <attr="val"> format (no space).
// By default htmlawed rewrites tags to this format.
// if PHP supported conditional negative lookbehinds we could use this:
// $r = preg_replace_callback('/(?<!=)(?<![ ])?(?<!["\'])((ht|f)tps?:\/\/[^\s\r\n\t<>"\'\!\(\),]+)/i',
$r = preg_replace_callback('/(?<![=\/"\'])((ht|f)tps?:\/\/[^\s\r\n\t<>"\']+)/i',
create_function(
'$matches',
'
$url = $matches[1];
$punc = "";
$last = substr($url, -1, 1);
if (in_array($last, array(".", "!", ",", "(", ")"))) {
$punc = $last;
$url = rtrim($url, ".!,()");
}
$urltext = str_replace("/", "/<wbr />", $url);
return "<a href=\"$url\" rel=\"nofollow\">$urltext</a>$punc";
'
), $text);
return $r;
}
function elgg_autop($string) {
return _elgg_services()->autoP->process($string);
}
function elgg_get_excerpt($text, $num_chars = 250) {
$view = 'output/excerpt';
$vars = [
'text' => $text,
'num_chars' => $num_chars,
];
$viewtype = elgg_view_exists($view) ? '' : 'default';
return _elgg_view_under_viewtype($view, $vars, $viewtype);
}
function elgg_format_url($url) {
return preg_replace('/&(?!amp;)/', '&amp;', $url);
}
function elgg_format_bytes($size, $precision = 2) {
if (!$size || $size < 0) {
return false;
}
$base = log($size) / log(1024);
$suffixes = array('B', 'kB', 'MB', 'GB', 'TB');
return round(pow(1024, $base - floor($base)), $precision) . ' ' . $suffixes[floor($base)];
}
function elgg_format_attributes(array $attrs = array()) {
if (!is_array($attrs) || empty($attrs)) {
return '';
}
foreach ($attrs as $attr => $val) {
if (0 !== strpos($attr, 'data-') && false !== strpos($attr, '_')) {
// this is probably a view $vars variable not meant for output
continue;
}
$attr = strtolower($attr);
if (!isset($val) || $val === false) {
continue;
}
if ($val === true) {
$val = $attr; //e.g. checked => true ==> checked="checked"
}
if (is_scalar($val)) {
$val = [$val];
}
if (!is_array($val)) {
continue;
}
// Check if array contains non-scalar values and bail if so
$filtered_val = array_filter($val, function($e) {
return is_scalar($e);
});
if (count($val) != count($filtered_val)) {
continue;
}
$val = implode(' ', $val);
$val = htmlspecialchars($val, ENT_QUOTES, 'UTF-8', false);
$attributes[] = "$attr=\"$val\"";
}
return implode(' ', $attributes);
}
function elgg_format_element($tag_name, array $attributes = array(), $text = '', array $options = array()) {
if (is_array($tag_name)) {
$args = $tag_name;
if ($attributes !== [] || $text !== '' || $options !== []) {
throw new \InvalidArgumentException('If $tag_name is an array, the other arguments must not be set');
}
if (isset($args['#tag_name'])) {
$tag_name = $args['#tag_name'];
}
if (isset($args['#text'])) {
$text = $args['#text'];
}
if (isset($args['#options'])) {
$options = $args['#options'];
}
unset($args['#tag_name'], $args['#text'], $args['#options']);
}
if (!is_string($tag_name) || $tag_name === '') {
throw new \InvalidArgumentException('$tag_name is required');
}
if (isset($options['is_void'])) {
$is_void = $options['is_void'];
} else {
// from http://www.w3.org/TR/html-markup/syntax.html#syntax-elements
$is_void = in_array(strtolower($tag_name), array(
'area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', 'keygen', 'link', 'menuitem',
'meta', 'param', 'source', 'track', 'wbr'
));
}
if (!empty($options['encode_text'])) {
$double_encode = empty($options['double_encode']) ? false : true;
$text = htmlspecialchars($text, ENT_QUOTES, 'UTF-8', $double_encode);
}
if ($attributes) {
if ($attrs !== '') {
$attrs = " $attrs";
}
} else {
$attrs = '';
}
if ($is_void) {
return empty($options['is_xml']) ? "<{$tag_name}{$attrs}>" : "<{$tag_name}{$attrs} />";
} else {
return "<{$tag_name}{$attrs}>$text</$tag_name>";
}
}
// see https://bugs.php.net/bug.php?id=51192
// from the bookmarks save action.
$php_5_2_13_and_below = version_compare(PHP_VERSION, '5.2.14', '<');
$php_5_3_0_to_5_3_2 = version_compare(PHP_VERSION, '5.3.0', '>=') &&
version_compare(PHP_VERSION, '5.3.3', '<');
if ($php_5_2_13_and_below || $php_5_3_0_to_5_3_2) {
$tmp_address = str_replace("-", "", $url);
$validated = filter_var($tmp_address, FILTER_VALIDATE_URL);
} else {
$validated = filter_var($url, FILTER_VALIDATE_URL);
}
// work around for handling absoluate IRIs (RFC 3987) - see #4190
if (!$validated && (strpos($url, 'http:') === 0) || (strpos($url, 'https:') === 0)) {
$validated = true;
}
if ($validated) {
// all normal URLs including mailto:
return $url;
} elseif (preg_match("#^(\#|\?|//)#i", $url)) {
// '//example.com' (Shortcut for protocol.)
// '?query=test', #target
return $url;
} elseif (stripos($url, 'javascript:') === 0 || stripos($url, 'mailto:') === 0) {
// 'javascript:' and 'mailto:'
// Not covered in FILTER_VALIDATE_URL
return $url;
} elseif (preg_match("#^[^/]*\.php(\?.*)?$#i", $url)) {
// 'install.php', 'install.php?step=step'
return elgg_get_site_url() . $url;
} elseif (preg_match("#^[^/?]*\.#i", $url)) {
// 'example.com', 'example.com/subpage'
return "http://$url";
} else {
// 'page/handler', 'mod/plugin/file.php'
// trim off any leading / because the site URL is stored
// with a trailing /
return elgg_get_site_url() . ltrim($url, '/');
}
}
// return a URL friendly title to short circuit normal title formatting
$params = array('title' => $title);
$result = elgg_trigger_plugin_hook('format', 'friendly:title', $params, null);
if ($result) {
return $result;
}
// titles are often stored HTML encoded
$title = html_entity_decode($title, ENT_QUOTES, 'UTF-8');
return $title;
}
function elgg_get_friendly_time($time, $current_time = null) {
if (!$current_time) {
$current_time = time();
}
// return a time string to short circuit normal time formatting
$params = array('time' => $time, 'current_time' => $current_time);
$result = elgg_trigger_plugin_hook('format', 'friendly:time', $params, null);
if ($result) {
return $result;
}
$diff = abs((int)$current_time - (int)$time);
$minute = 60;
$hour = $minute * 60;
$day = $hour * 24;
if ($diff < $minute) {
return elgg_echo("friendlytime:justnow");
}
if ($diff < $hour) {
$granularity = ':minutes';
$diff = round($diff / $minute);
} else if ($diff < $day) {
$granularity = ':hours';
$diff = round($diff / $hour);
} else {
$granularity = ':days';
$diff = round($diff / $day);
}
if ($diff == 0) {
$diff = 1;
}
$future = ((int)$current_time - (int)$time < 0) ? ':future' : '';
$singular = ($diff == 1) ? ':singular' : '';
return elgg_echo("friendlytime{$future}{$granularity}{$singular}", array($diff));
}
function elgg_get_friendly_upload_error($error_code) {
switch ($error_code) {
case UPLOAD_ERR_OK:
return '';
case UPLOAD_ERR_INI_SIZE:
$key = 'ini_size';
break;
case UPLOAD_ERR_FORM_SIZE:
$key = 'form_size';
break;
case UPLOAD_ERR_PARTIAL:
$key = 'partial';
break;
case UPLOAD_ERR_NO_FILE:
$key = 'no_file';
break;
case UPLOAD_ERR_NO_TMP_DIR:
$key = 'no_tmp_dir';
break;
case UPLOAD_ERR_CANT_WRITE:
$key = 'cant_write';
break;
case UPLOAD_ERR_EXTENSION:
$key = 'extension';
break;
default:
$key = 'unknown';
break;
}
return elgg_echo("upload:error:$key");
}
function elgg_strip_tags($string, $allowable_tags = null) {
$params['original_string'] = $string;
$params['allowable_tags'] = $allowable_tags;
$string = strip_tags($string, $allowable_tags);
$string = elgg_trigger_plugin_hook('format', 'strip_tags', $params, $string);
return $string;
}
$string = str_replace(
array('&gt;', '&lt;', '&amp;', '&quot;', '&#039;'),
array('&amp;gt;', '&amp;lt;', '&amp;amp;', '&amp;quot;', '&amp;#039;'),
);
$string = html_entity_decode($string, ENT_NOQUOTES, 'UTF-8');
$string = str_replace(
array('&amp;gt;', '&amp;lt;', '&amp;amp;', '&amp;quot;', '&amp;#039;'),
array('&gt;', '&lt;', '&amp;', '&quot;', '&#039;'),
);
return $string;
}
elgg_deprecated_notice(__FUNCTION__ . ' is deprecated. Use elgg_html_decode()', '2.0');
}
//encode <,>,&, quotes and characters above 127
if (function_exists('mb_convert_encoding')) {
$display_query = mb_convert_encoding($string, 'HTML-ENTITIES', 'UTF-8');
} else {
// if no mbstring extension, we just strip characters
$display_query = preg_replace("/[^\x01-\x7F]/", "", $string);
}
return htmlspecialchars($display_query, ENT_QUOTES, 'UTF-8', false);
}
return function(\Elgg\EventsService $events, \Elgg\HooksRegistrationService $hooks) {
};