forked from GNUsocial/gnu-social
All tests pass except for those that require matching parens or brackets
This commit is contained in:
parent
6a3a25b5a2
commit
210bc4248b
38
lib/util.php
38
lib/util.php
@ -413,45 +413,55 @@ function common_replace_urls_callback($text, $callback, $notice_id = null) {
|
||||
// Start off with a regex
|
||||
$regex = '#'.
|
||||
'(?:^|[\s\(\)\[\]\{\}\\\'\\\";]+)(?![\@\!\#])'.
|
||||
'('.
|
||||
'(?P<url>'.
|
||||
'(?:'.
|
||||
'(?:'. //Known protocols
|
||||
'(?:'.
|
||||
'(?:https?|ftps?|mms|rtsp|gopher|news|nntp|telnet|wais|file|prospero|webcal|irc)://'.
|
||||
'(?:(?:https?|ftps?|mms|rtsp|gopher|news|nntp|telnet|wais|file|prospero|webcal|irc)://)'.
|
||||
'|'.
|
||||
'(?:mailto|aim|tel|xmpp):'.
|
||||
'(?:(?:mailto|aim|tel|xmpp):)'.
|
||||
')'.
|
||||
'(?:[\pN\pL\-\_\+]+(?:\:[\pN\pL\-\_\+]+)?\@)?'. //user:pass@
|
||||
'(?:[\pN\pL\-\_\+]+(?::[\pN\pL\-\_\+]+)?\@)?'. //user:pass@
|
||||
'(?:'.
|
||||
'(?:'.
|
||||
'\[[\pN\pL\-\_\:\.]+(?<![\.\:])\]'. //[dns]
|
||||
')|(?:'.
|
||||
'[\pN\pL\-\_\:\.]+(?<![\.\:])'. //dns
|
||||
')'.
|
||||
')'.
|
||||
')'.
|
||||
'|(?:(?: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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2})(?:\.(?:25[0-5]|2[0-4]\d|[01]?\d{1,2})){3})?)|(?:(?::[0-9A-Fa-f]{1,4}){1,2})))|(?:(?:(?:25[0-5]|2[0-4]\d|[01]?\d{1,2})(?:\.(?:25[0-5]|2[0-4]\d|[01]?\d{1,2})){3})))\]?'.
|
||||
')|(?:'. //DNS
|
||||
'(?:[\pN\pL\-\_\+]+(?:\:[\pN\pL\-\_\+]+)?\@)?'. //user:pass@
|
||||
'[\pN\pL\-\_]+(?:\.[\pN\pL\-\_]+)*\.(?:museum|travel|onion|local|[a-z]{2,4})'.
|
||||
'[\pN\pL\-\_]+(?:\.[\pN\pL\-\_]+)*\.'.
|
||||
//tld list from http://data.iana.org/TLD/tlds-alpha-by-domain.txt, also added local, loc, and onion
|
||||
'(?:AC|AD|AE|AERO|AF|AG|AI|AL|AM|AN|AO|AQ|AR|ARPA|AS|ASIA|AT|AU|AW|AX|AZ|BA|BB|BD|BE|BF|BG|BH|BI|BIZ|BJ|BM|BN|BO|BR|BS|BT|BV|BW|BY|BZ|CA|CAT|CC|CD|CF|CG|CH|CI|CK|CL|CM|CN|CO|COM|COOP|CR|CU|CV|CX|CY|CZ|DE|DJ|DK|DM|DO|DZ|EC|EDU|EE|EG|ER|ES|ET|EU|FI|FJ|FK|FM|FO|FR|GA|GB|GD|GE|GF|GG|GH|GI|GL|GM|GN|GOV|GP|GQ|GR|GS|GT|GU|GW|GY|HK|HM|HN|HR|HT|HU|ID|IE|IL|IM|IN|INFO|INT|IO|IQ|IR|IS|IT|JE|JM|JO|JOBS|JP|KE|KG|KH|KI|KM|KN|KP|KR|KW|KY|KZ|LA|LB|LC|LI|LK|LR|LS|LT|LU|LV|LY|MA|MC|MD|ME|MG|MH|MIL|MK|ML|MM|MN|MO|MOBI|MP|MQ|MR|MS|MT|MU|MUSEUM|MV|MW|MX|MY|MZ|NA|NAME|NC|NE|NET|NF|NG|NI|NL|NO|NP|NR|NU|NZ|OM|ORG|PA|PE|PF|PG|PH|PK|PL|PM|PN|PR|PRO|PS|PT|PW|PY|QA|RE|RO|RS|RU|RW|SA|SB|SC|SD|SE|SG|SH|SI|SJ|SK|SL|SM|SN|SO|SR|ST|SU|SV|SY|SZ|TC|TD|TEL|TF|TG|TH|TJ|TK|TL|TM|TN|TO|TP|TR|TRAVEL|TT|TV|TW|TZ|UA|UG|UK|US|UY|UZ|VA|VC|VE|VG|VI|VN|VU|WF|WS|XN--0ZWM56D|XN--11B5BS3A9AJ6G|XN--80AKHBYKNJ4F|XN--9T4B11YI5A|XN--DEBA0AD|XN--G6W251D|XN--HGBK6AJ7F53BBA|XN--HLCJ6AYA9ESC7A|XN--JXALPDLP|XN--KGBECHTV|XN--ZCKZAH|YE|YT|YU|ZA|ZM|ZW|local|loc|onion)'.
|
||||
')(?![\pN\pL\-\_])'.
|
||||
')'.
|
||||
'(?:'.
|
||||
'(?:\:\d+)?'. //:port
|
||||
'(?:/[\pN\pL$\!\(\)\.\-\_\+\/\=\&\;]*)?'. // /path
|
||||
'(?:\?[\pN\pL\$\!\(\)\.\-\_\+\/\=\&\;\/]*)?'. // ?query string
|
||||
'(?:\#[\pN\pL$\!\(\)\.\-\_\+\/\=\&\;\/\?\#]*)?'. // #fragment
|
||||
')(?<![\?\.\,\#\)])'.
|
||||
'(?:/[\pN\pL$\[\]\,\!\(\)\.\-\_\+\/\=\&\;]*)?'. // /path
|
||||
'(?:\?[\pN\pL\$\[\]\,\!\(\)\.\-\_\+\/\=\&\;\/]*)?'. // ?query string
|
||||
'(?:\#[\pN\pL$\[\]\,\!\(\)\.\-\_\+\/\=\&\;\/\?\#]*)?'. // #fragment
|
||||
')(?<![\?\.\,\#\)\]\,])'.
|
||||
')'.
|
||||
'#ixu';
|
||||
preg_match_all($regex,$text,$matches);
|
||||
//print_r($matches);
|
||||
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]);
|
||||
$pos = strpos($matches[0],$matches['url']);
|
||||
$left = substr($matches[0],0,$pos);
|
||||
$right = substr($matches[0],$pos+strlen($matches[1]));
|
||||
$right = substr($matches[0],$pos+strlen($matches['url']));
|
||||
|
||||
if(empty($notice_id)){
|
||||
$result = call_user_func_array($callback,$matches[1]);
|
||||
$result = call_user_func_array($callback,$matches['url']);
|
||||
}else{
|
||||
$result = call_user_func_array($callback, array($matches[1],$notice_id) );
|
||||
$result = call_user_func_array($callback, array($matches['url'],$notice_id) );
|
||||
}
|
||||
return $left . $result . $right;
|
||||
}
|
||||
|
@ -25,16 +25,24 @@ class URLDetectionTest extends PHPUnit_Framework_TestCase
|
||||
static public function provider()
|
||||
{
|
||||
return array(
|
||||
array('http://127.0.0.1',
|
||||
'<a href="http://127.0.0.1/" rel="external">http://127.0.0.1</a>'),
|
||||
array('127.0.0.1',
|
||||
'<a href="http://127.0.0.1/" rel="external">127.0.0.1</a>'),
|
||||
array('127.0.0.1:99',
|
||||
'<a href="http://127.0.0.1:99/" rel="external">127.0.0.1:99</a>'),
|
||||
array('127.0.0.1/test.php',
|
||||
'<a href="http://127.0.0.1/test.php" rel="external">127.0.0.1/test.php</a>'),
|
||||
array('http://[::1]:99/test.php',
|
||||
'<a href="http://[::1]:99/test.php" rel="external">http://[::1]:99/test.php</a>'),
|
||||
array('http://::1/test.php',
|
||||
'<a href="http://::1/test.php" rel="external">http://::1/test.php</a>'),
|
||||
array('http://::1',
|
||||
'<a href="http://::1/" rel="external">http://::1</a>'),
|
||||
array('2001:4978:1b5:0:21d:e0ff:fe66:59ab/test.php',
|
||||
'<a href="http://2001:4978:1b5:0:21d:e0ff:fe66:59ab/test.php" rel="external">2001:4978:1b5:0:21d:e0ff:fe66:59ab/test.php</a>'),
|
||||
array('[2001:4978:1b5:0:21d:e0ff:fe66:59ab]:99/test.php',
|
||||
'<a href="http://[2001:4978:1b5:0:21d:e0ff:fe66:59ab]:99/test.php" rel="external">[2001:4978:1b5:0:21d:e0ff:fe66:59ab]:99/test.php</a>'),
|
||||
array('2001:4978:1b5:0:21d:e0ff:fe66:59ab',
|
||||
'<a href="http://2001:4978:1b5:0:21d:e0ff:fe66:59ab/" rel="external">2001:4978:1b5:0:21d:e0ff:fe66:59ab</a>'),
|
||||
array('http://127.0.0.1',
|
||||
@ -145,14 +153,13 @@ class URLDetectionTest extends PHPUnit_Framework_TestCase
|
||||
'<<a href="http://example.com/" rel="external">http://example.com</a>>'),
|
||||
array('http://example.com/path/(foo)/bar',
|
||||
'<a href="http://example.com/path/(foo)/bar" rel="external">http://example.com/path/(foo)/bar</a>'),
|
||||
//Not a valid url - urls cannot contain unencoded square brackets
|
||||
array('http://example.com/path/[foo]/bar',
|
||||
'<a href="http://example.com/path/[foo]/bar" rel="external">http://example.com/path/[foo]/bar</a>'),
|
||||
array('http://example.com/path/foo/(bar)',
|
||||
'<a href="http://example.com/path/foo/(bar)" rel="external">http://example.com/path/foo/(bar)</a>'),
|
||||
//Not a valid url - urls cannot contain unencoded square brackets
|
||||
//array('http://example.com/path/foo/[bar]',
|
||||
// '<a href="http://example.com/path/foo/[bar]" rel="external">http://example.com/path/foo/[bar]</a>'),
|
||||
array('http://example.com/path/foo/[bar]',
|
||||
'<a href="http://example.com/path/foo/[bar]" rel="external">http://example.com/path/foo/[bar]</a>'),
|
||||
array('Hey, check out my cool site http://example.com okay?',
|
||||
'Hey, check out my cool site <a href="http://example.com/" rel="external">http://example.com</a> okay?'),
|
||||
array('What about parens (e.g. http://example.com/path/foo/(bar))?',
|
||||
|
Loading…
Reference in New Issue
Block a user