Elgg  Version 1.11
/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) {
$string_length = elgg_strlen($text);
if ($string_length <= $num_chars) {
return $text;
}
// handle cases
$excerpt = elgg_substr($text, 0, $num_chars);
$space = elgg_strrpos($excerpt, ' ', 0);
// don't crop if can't find a space.
if ($space === false) {
$space = $num_chars;
}
$excerpt = trim(elgg_substr($excerpt, 0, $space));
if ($string_length != elgg_strlen($excerpt)) {
$excerpt .= '...';
}
return $excerpt;
}
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) || !count($attrs)) {
return '';
}
$attributes = array();
if (isset($attrs['js'])) {
elgg_deprecated_notice('Use associative array of attr => val pairs instead of $vars[\'js\']', 1.8);
if (!empty($attrs['js'])) {
$attributes[] = $attrs['js'];
}
unset($attrs['js']);
}
foreach ($attrs as $attr => $val) {
$attr = strtolower($attr);
if ($val === true) {
$val = $attr; //e.g. checked => true ==> checked="checked"
}
if ($val !== NULL && $val !== false && (is_array($val) || !is_object($val))) {
if (is_array($val)) {
$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_string($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>";
}
}
function _elgg_clean_vars(array $vars = array()) {
unset($vars['config']);
unset($vars['url']);
unset($vars['user']);
// backwards compatibility code
if (isset($vars['internalname'])) {
if (!isset($vars['__ignoreInternalname'])) {
$vars['name'] = $vars['internalname'];
}
unset($vars['internalname']);
}
if (isset($vars['internalid'])) {
if (!isset($vars['__ignoreInternalid'])) {
$vars['id'] = $vars['internalid'];
}
unset($vars['internalid']);
}
if (isset($vars['__ignoreInternalid'])) {
unset($vars['__ignoreInternalid']);
}
if (isset($vars['__ignoreInternalname'])) {
unset($vars['__ignoreInternalname']);
}
return $vars;
}
// 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;
}
//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);
}
$value[] = "{$CONFIG->path}engine/tests/ElggCoreOutputAutoPTest.php";
return $value;
}
function _elgg_output_init() {
elgg_register_plugin_hook_handler('unit_test', 'system', '_elgg_output_unit_test');
}
return function(\Elgg\EventsService $events, \Elgg\HooksRegistrationService $hooks) {
$events->registerHandler('init', 'system', '_elgg_output_init');
};