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);
}
global $CONFIG;
$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');
};
$params
Definition: login.php:72
$type
Definition: add.php:8
$title
Definition: save.php:24
$attributes
Definition: ajax_loader.php:13
$attrs
Definition: ajax_loader.php:30
_elgg_services()
Definition: autoloader.php:14
$size
Definition: view.php:10
static urlize($string, $separator='-')
Create a version of a string for embedding in a URL.
Definition: Translit.php:40
elgg_get_site_url($site_guid=0)
Get the URL for the current (or specified) site.
$text
Definition: default.php:25
$r
elgg_register_plugin_hook_handler($hook, $type, $callback, $priority=500)
Definition: elgglib.php:703
elgg_trigger_plugin_hook($hook, $type, $params=null, $returnvalue=null)
Definition: elgglib.php:775
elgg_deprecated_notice($msg, $dep_version, $backtrace_level=1)
Log a notice about deprecated use of a function, view, etc.
Definition: elgglib.php:1006
elgg_echo($message_key, $args=array(), $language="")
Given a message key, returns an appropriately translated full-text string.
Definition: languages.php:21
$url
Definition: exceptions.php:24
$value
Definition: longtext.php:26
elgg_substr()
Wrapper function for mb_substr().
Definition: mb_wrapper.php:225
elgg_strrpos()
Wrapper function for mb_strrpos().
Definition: mb_wrapper.php:140
elgg_strlen()
Wrapper function for mb_strlen().
Definition: mb_wrapper.php:72
elgg_format_url($url)
Handles formatting of ampersands in urls.
Definition: output.php:105
elgg_get_friendly_title($title)
When given a title, returns a version suitable for inclusion in a URL.
Definition: output.php:369
elgg_format_bytes($size, $precision=2)
Format bytes to a human readable format.
Definition: output.php:119
_elgg_html_decode($string)
Apply html_entity_decode() to a string while re-entitising HTML special char entities to prevent them...
Definition: output.php:536
_elgg_clean_vars(array $vars=array())
Preps an associative array for use in elgg_format_attributes().
Definition: output.php:265
elgg_strip_tags($string, $allowable_tags=null)
Strip tags and offer plugins the chance.
Definition: output.php:499
elgg_get_friendly_upload_error($error_code)
Returns a human-readable message for PHP's upload error codes.
Definition: output.php:447
_elgg_output_init()
Initialize the output subsystem.
Definition: output.php:593
elgg_format_attributes(array $attrs=array())
Converts an associative array into a string of well-formed attributes.
Definition: output.php:139
_elgg_get_display_query($string)
Prepares query string for output to prevent CSRF attacks.
Definition: output.php:559
elgg_format_element($tag_name, array $attributes=array(), $text='', array $options=array())
Format an HTML element.
Definition: output.php:215
elgg_get_excerpt($text, $num_chars=250)
Returns an excerpt.
Definition: output.php:72
parse_urls($text)
Takes a string and turns any URLs into formatted links.
Definition: output.php:17
_elgg_output_unit_test($hook, $type, $value, $params)
Unit tests for Output.
Definition: output.php:581
elgg_normalize_url($url)
Definition: output.php:311
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:397
elgg_autop($string)
Create paragraphs from text with line spacing.
Definition: output.php:56
$options
Definition: index.php:14
$key
Definition: summary.php:34
$string
global $CONFIG
if(file_exists($welcome)) $vars
Definition: upgrade.php:93