Commit 3d9a40bb authored by John C Fiala's avatar John C Fiala
Browse files

support request #323879 by jcfiala:Updated link 5.x validation to use same code as 6.x branch.

bug report #436020 by jcfiala:Updated link domain names to include asia
parent 2b4c99a3
......@@ -10,7 +10,23 @@ define('LINK_EXTERNAL', 'external');
define('LINK_INTERNAL', 'internal');
define('LINK_FRONT', 'front');
define('LINK_EMAIL', 'email');
define('LINK_DOMAINS', 'aero|arpa|biz|com|cat|coop|edu|gov|info|int|jobs|mil|museum|name|nato|net|org|pro|travel|mobi|local');
define('LINK_DOMAINS', 'aero|arpa|asia|biz|com|cat|coop|edu|gov|info|int|jobs|mil|museum|name|nato|net|org|pro|travel|mobi|local');
// There are many other characters which are legal other than simply a-z - this includes them.
define('LINK_ICHARS', (string) html_entity_decode(implode("", array(
"æ", // æ
"Æ", // Æ
"ø", // ø
"Ø", // Ø
"å", // å
"Å", // Å
"ä", // ä
"Ä", // Ä
"ö", // ö
"Ö", // Ö
"ü", // ü
"Ü", // Ü
)), ENT_QUOTES, 'UTF-8'));
/**
* Implementation of hook_menu().
......@@ -843,32 +859,35 @@ function link_cleanup_url($url, $protocol = "http") {
* the following attributes: protocol, hostname, ip, and port.
*/
function link_validate_url($text) {
$allowed_protocols = variable_get('filter_allowed_protocols', array('http', 'https', 'ftp', 'news', 'nntp', 'telnet', 'mailto', 'irc', 'ssh', 'sftp', 'webcal'));
$protocol = '((' . implode("|", $allowed_protocols) . '):\/\/)';
$authentication = '([a-z0-9]+(:[a-z0-9]+)?@)';
$domain = '(([a-z0-9]([a-z0-9\-_\[\]])*)(\.(([a-z0-9\-_\[\]])+\.)*('. LINK_DOMAINS .'|[a-z]{2}))?)';
$protocol = '(('. implode("|", $allowed_protocols) .'):\/\/)';
$authentication = '(([a-z0-9%' . LINK_ICHARS . ']+(:[a-z0-9%'. LINK_ICHARS . '!]*)?)?@)';
$domain = '(([a-z0-9' . LINK_ICHARS . ']([a-z0-9'. LINK_ICHARS . '\-_\[\]])*)(\.(([a-z0-9' . LINK_ICHARS . '\-_\[\]])+\.)*('. LINK_DOMAINS .'|[a-z]{2}))?)';
$ipv4 = '([0-9]{1,3}(\.[0-9]{1,3}){3})';
$ipv6 = '([0-9a-fA-F]{1,4}(\:[0-9a-fA-F]{1,4}){7})';
$port = '(:([0-9]{1,5}))';
// Pattern specific to eternal links.
$external_pattern = '/^' . $protocol . '?' . $authentication . '?' . '(' . $domain . '|' . $ipv4 . '|' . $ipv6 . ' |localhost)' . $port . '?';
// Pattern specific to external links.
$external_pattern = '/^'. $protocol .'?'. $authentication .'?('. $domain .'|'. $ipv4 .'|'. $ipv6 .' |localhost)'. $port .'?';
// Pattern specific to internal links.
$internal_pattern = "/^([a-z0-9_\-+\[\]]+)";
$internal_pattern = "/^([a-z0-9". LINK_ICHARS ."_\-+\[\]]+)";
$directories = "(\/[a-z0-9_\-\.~+%=&,$'():;*@\[\]]*)*";
// Yes, four backslashes == a single backslash.
$query = "(\/?\?([?a-z0-9+_|\-\.\/\\\\%=&,$'():;*@\[\]]*))";
$anchor = "(#[a-z0-9_\-\.~+%=&,$'():;*@\[\]]*)";
$directories = "(\/[a-z0-9". LINK_ICHARS ."_\-\.~+%=&,$'!():;*@\[\]]*)*";
// Yes, four backslashes == a single backslash.
$query = "(\/?\?([?a-z0-9". LINK_ICHARS ."+_|\-\.\/\\\\%=&,$'():;*@\[\]{} ]*))";
$anchor = "(#[a-z0-9". LINK_ICHARS ."_\-\.~+%=&,$'():;*@\[\]\/\?]*)";
// The rest of the path for a standard URL.
$end = $directories . '?' . $query . '?' . $anchor . '?' . '$/i';
$end = $directories .'?'. $query .'?'. $anchor .'?'.'$/i';
$message_id = '[^@].*@'. $domain;
$newsgroup_name = '([0-9a-z+-]*\.)*[0-9a-z+-]*';
$news_pattern = '/^news:('. $newsgroup_name .'|'. $message_id .')$/i';
$user = '[a-zA-Z0-9_\-\.\+\^!#\$%&*+\/\=\?\`\|\{\}~\'\[\]]+';
$email_pattern = '/^mailto:' . $user . '@' . '(' . $domain . '|' . $ipv4 .'|'. $ipv6 . '|localhost)' . $query . '?$/';
$user = '[a-zA-Z0-9'. LINK_ICHARS .'_\-\.\+\^!#\$%&*+\/\=\?\`\|\{\}~\'\[\]]+';
$email_pattern = '/^mailto:'. $user .'@'.'('. $domain .'|'. $ipv4 .'|'. $ipv6 .'|localhost)'. $query .'?$/';
if (strpos($text, '<front>') === 0) {
return LINK_FRONT;
......@@ -876,6 +895,9 @@ function link_validate_url($text) {
if (in_array('mailto', $allowed_protocols) && preg_match($email_pattern, $text)) {
return LINK_EMAIL;
}
if (in_array('news', $allowed_protocols) && preg_match($news_pattern, $text)) {
return LINK_NEWS;
}
if (preg_match($internal_pattern . $end, $text)) {
return LINK_INTERNAL;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment