From 091434893adf1e77b3fec1a40e6dc45119090df2 Mon Sep 17 00:00:00 2001
From: heddn <lucashedding@1463982.no-reply.drupal.org>
Date: Wed, 12 Nov 2014 11:21:01 -0600
Subject: [PATCH] Issue #2247261 by drupalfan81 | heddn: Fixed URLs are not
 validated.

---
 link.module | 44 +++++++++++++++++++++++++++++++++++---------
 1 file changed, 35 insertions(+), 9 deletions(-)

diff --git a/link.module b/link.module
index e17c26f..98f7da4 100644
--- a/link.module
+++ b/link.module
@@ -291,7 +291,7 @@ function link_field_validate($entity_type, $entity, $field, $instance, $langcode
  */
 function link_field_insert($entity_type, $entity, $field, $instance, $langcode, &$items) {
   foreach ($items as $delta => $value) {
-    _link_process($items[$delta], $delta, $field, $entity);
+    _link_process($items[$delta], $delta, $field, $entity, $instance);
   }
 }
 
@@ -300,7 +300,7 @@ function link_field_insert($entity_type, $entity, $field, $instance, $langcode,
  */
 function link_field_update($entity_type, $entity, $field, $instance, $langcode, &$items) {
   foreach ($items as $delta => $value) {
-    _link_process($items[$delta], $delta, $field, $entity);
+    _link_process($items[$delta], $delta, $field, $entity, $instance);
   }
 }
 
@@ -372,7 +372,7 @@ function _link_load($field, $item, $instance) {
 /**
  * Prepares the item attributes and url for storage.
  */
-function _link_process(&$item, $delta, $field, $entity) {
+function _link_process(&$item, $delta, $field, $entity, $instance) {
   // Trim whitespace from URL.
   if (!empty($item['url'])) {
     $item['url'] = trim($item['url']);
@@ -391,7 +391,8 @@ function _link_process(&$item, $delta, $field, $entity) {
 
   // Don't save an invalid default value (e.g. 'http://').
   if ((isset($field['widget']['default_value'][$delta]['url']) && $item['url'] == $field['widget']['default_value'][$delta]['url']) && is_object($entity)) {
-    if (!link_validate_url($item['url'])) {
+    $langcode = !empty($entity) ? field_language($instance['entity_type'], $entity, $instance['field_name']) : NULL;
+    if (!link_validate_url($item['url'], $langcode)) {
       unset($item['url']);
     }
   }
@@ -403,7 +404,8 @@ function _link_process(&$item, $delta, $field, $entity) {
 function _link_validate(&$item, $delta, $field, $entity, $instance, $langcode, &$optional_field_found, &$errors) {
   if ($item['url'] && !(isset($instance['default_value'][$delta]['url']) && $item['url'] === $instance['default_value'][$delta]['url'] && !$instance['required'])) {
     // Validate the link.
-    if (link_validate_url(trim($item['url'])) == FALSE) {
+    $langcode = !empty($node) ? field_language($instance['entity_type'], $node, $instance['field_name']) : NULL;
+    if (link_validate_url(trim($item['url']), $langcode) == FALSE) {
       $errors[$field['field_name']][$langcode][$delta][] = array(
         'error' => 'link_required',
         'message' => t('The value %value provided for %field is not a valid URL.', array(
@@ -488,7 +490,7 @@ function _link_sanitize(&$item, $delta, &$field, $instance, &$entity) {
     }
   }
 
-  $type = link_validate_url($item['url']);
+  $type = link_url_type($item['url']);
   // If the type of the URL cannot be determined and URL validation is disabled,
   // then assume LINK_EXTERNAL for later processing.
   if ($type == FALSE && $instance['settings']['validate_url'] === 0) {
@@ -1161,7 +1163,7 @@ function link_views_api() {
  */
 function link_cleanup_url($url, $protocol = 'http') {
   $url = trim($url);
-  $type = link_validate_url($url);
+  $type = link_url_type($url);
 
   if ($type === LINK_EXTERNAL) {
     // Check if there is no protocol specified.
@@ -1182,17 +1184,41 @@ function link_cleanup_url($url, $protocol = 'http') {
 /**
  * Validates a URL.
  *
+ * @param $text
+ *   Url to be validated.
+ *
+ * @return boolean
+ *   True if a valid link, FALSE otherwise.
+ */
+function link_validate_url($text, $langcode = NULL) {
+  $flag = valid_url($text, TRUE);
+
+  if (!$flag) {
+    $normal_path = drupal_get_normal_path($text, $langcode);
+    $parsed_link = parse_url($normal_path, PHP_URL_PATH);
+    if ($normal_path != $parsed_link) {
+      $normal_path = $parsed_link;
+    }
+    $flag = drupal_valid_path($normal_path);
+  }
+
+  return $flag;
+}
+
+/**
+ * Type check a URL.
+ *
  * Accepts all URLs following RFC 1738 standard for URL formation and all e-mail
  * addresses following the RFC 2368 standard for mailto address formation.
  *
  * @param string $text
- *   Url to be validated.
+ *   Url to be checked.
  * 
  * @return mixed
  *   Returns boolean FALSE if the URL is not valid. On success, returns one of
  *   the LINK_(linktype) constants.
  */
-function link_validate_url($text) {
+function link_url_type($text) {
   // @TODO Complete letters.
   $LINK_ICHARS_DOMAIN = (string) html_entity_decode(implode("", array(
     "&#x00E6;", // æ
-- 
2.1.3

