You are here

function link_cleanup_url in Link 7

Same name and namespace in other branches
  1. 5 link.module \link_cleanup_url()
  2. 6.2 link.inc \link_cleanup_url()
  3. 6 link.module \link_cleanup_url()

Forms a valid URL if possible from an entered address.

Trims whitespace and automatically adds an http:// to addresses without a protocol specified.

Parameters

string $url: The url entered by the user.

string $protocol: The protocol to be prepended to the url if one is not specified.

2 calls to link_cleanup_url()
link_validate_url in ./link.module
Validates a URL.
_link_sanitize in ./link.module
Clean up user-entered values for a link field according to field settings.

File

./link.module, line 1567
Defines simple link field types.

Code

function link_cleanup_url($url, $protocol = 'http') {
  try {
    link_ensure_valid_default_protocol();
  } catch (Exception $e) {
    watchdog('link', $e
      ->getMessage(), array(), WATCHDOG_ERROR);
  }
  $url = trim($url);
  $type = link_url_type($url);
  if ($type === LINK_EXTERNAL) {

    // Check if there is no protocol specified.
    $protocol_match = preg_match("/^([a-z0-9][a-z0-9\\.\\-_]*:\\/\\/)/i", $url);
    if (empty($protocol_match)) {

      // But should there be? Add an automatic http:// if it starts with a
      // domain name.
      $link_domains = _link_domains();
      $domain_match = preg_match('/^(([a-z0-9]([a-z0-9\\-_]*\\.)+)(' . $link_domains . '|[a-z]{2}))/i', $url);
      if (!empty($domain_match)) {
        $url = $protocol . "://" . $url;
      }
    }
  }
  return $url;
}