summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/common.php3
-rw-r--r--lib/util.php102
2 files changed, 59 insertions, 46 deletions
diff --git a/lib/common.php b/lib/common.php
index 6c4b856e0..72c093bf3 100644
--- a/lib/common.php
+++ b/lib/common.php
@@ -47,6 +47,9 @@ require_once('PEAR.php');
require_once('DB/DataObject.php');
require_once('DB/DataObject/Cast.php'); # for dates
+if (!function_exists('gettext')) {
+ require_once("php-gettext/gettext.inc");
+}
require_once(INSTALLDIR.'/lib/language.php');
// This gets included before the config file, so that admin code and plugins
diff --git a/lib/util.php b/lib/util.php
index 748c8332f..2be4213e7 100644
--- a/lib/util.php
+++ b/lib/util.php
@@ -412,57 +412,62 @@ function common_render_text($text)
function common_replace_urls_callback($text, $callback, $notice_id = null) {
// Start off with a regex
$regex = '#'.
- '(?:^|\s+)('.
+ '(?:^|[\s\(\)\[\]\{\}]+)'.
+ '('.
'(?:'.
- '(?:https?|ftps?|mms|rtsp|gopher|news|nntp|telnet|wais|file|prospero|webcal|irc)://'.
- '|'.
- '(?:mailto|aim|tel|xmpp):'.
- ')?'.
+ '(?:'. //Known protocols
+ '(?:'.
+ '(?:https?|ftps?|mms|rtsp|gopher|news|nntp|telnet|wais|file|prospero|webcal|irc)://'.
+ '|'.
+ '(?:mailto|aim|tel|xmpp):'.
+ ')[^\s\/]+'.
+ ')'.
+ '|(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)'. //IPv4
+ '|(?:'. //IPv6
+ '(?:(?:(?:[0-9A-Fa-f]{1,4}:){7}(?:(?:[0-9A-Fa-f]{1,4})|:))|(?:(?:[0-9A-Fa-f]{1,4}:){6}(?::|(?:(?:25[0-5]|2[0-4]\d|[01]?\d{1,2})(?:\.(?:25[0-5]|2[0-4]\d|[01]?\d{1,2})){3})|(?::[0-9A-Fa-f]{1,4})))|(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?\d)(?:\.(?:25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}|'.
- '(?:[0-9a-f]{1,4}:){1,3}(?::[0-9a-f]{1,4}){1,2}:(?:25[0-5]|2[0-4]\d|[0-1]?\d?\d)(?:\.(?:25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}|'.
- '(?:[0-9a-f]{1,4}:){1,4}(?::[0-9a-f]{1,4}){1,1}:(?:25[0-5]|2[0-4]\d|[0-1]?\d?\d)(?:\.(?:25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}|'.
- '(?:(?:[0-9a-f]{1,4}:){1,5}|:):(?:25[0-5]|2[0-4]\d|[0-1]?\d?\d)(?:\.(?:25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}|'.
- ':(?::[0-9a-f]{1,4}){1,5}:(?:25[0-5]|2[0-4]\d|[0-1]?\d?\d)(?:\.(?:25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}'.
- ')|'.
- '(?:[^.\s/:]+\.)+'. //DNS
- '(?:museum|travel|onion|[a-z]{2,4})'.
+ '$|(?:'.
+ '/[^\s\(\)\[\]\{\}]*'.
+ ')'.
')'.
- '(?:[:/][^\s]*)?'.
')'.
'#ix';
- preg_match_all($regex, $text, $matches);
- // Then clean up what the regex left behind
- $offset = 0;
- foreach($matches[1] as $url) {
- // Call user specified func
- if (empty($notice_id)) {
- $modified_url = call_user_func($callback, $url);
- } else {
- $modified_url = call_user_func($callback, array($url, $notice_id));
- }
-
- // Replace it!
- $start = mb_strpos($text, $url, $offset);
- $text = mb_substr($text, 0, $start).$modified_url.mb_substr($text, $start + mb_strlen($url), mb_strlen($text));
- $offset = $start + mb_strlen($modified_url);
- }
-
- return $text;
+ return preg_replace_callback($regex, curry(callback_helper,$callback,$notice_id) ,$text);
+}
+
+function callback_helper($matches, $callback, $notice_id) {
+ $pos = strpos($matches[0],$matches[1]);
+ $left = substr($matches[0],0,$pos);
+ $right = substr($matches[0],$pos+strlen($matches[1]));
+
+ if(empty($notice_id)){
+ $result = call_user_func_array($callback,$matches[1]);
+ }else{
+ $result = call_user_func_array($callback, array($matches[1],$notice_id) );
+ }
+ return $left . $result . $right;
+}
+
+function curry($fn) {
+ //TODO switch to a PHP 5.3 function closure based approach if PHP 5.3 is used
+ $args = func_get_args();
+ array_shift($args);
+ $id = uniqid('_partial');
+ $GLOBALS[$id] = array($fn, $args);
+ return create_function(
+ '',
+ '
+ $args = func_get_args();
+ return call_user_func_array(
+ $GLOBALS["'.$id.'"][0],
+ array_merge(
+ $args,
+ $GLOBALS["'.$id.'"][1]));
+ ');
}
function common_linkify($url) {
@@ -470,6 +475,11 @@ function common_linkify($url) {
// functions
$url = htmlspecialchars_decode($url);
+ if(strpos($url, '@')!==false && strpos($url, ':')===false){
+ //url is an email address without the mailto: protocol
+ return XMLStringer::estring('a', array('href' => "mailto:$url", 'rel' => 'external'), $url);
+ }
+
$canon = File_redirection::_canonUrl($url);
$longurl_data = File_redirection::where($url);