You are here

function _linkchecker_link_replace in Link checker 7

Same name and namespace in other branches
  1. 5.2 linkchecker.module \_linkchecker_link_replace()
  2. 6.2 linkchecker.module \_linkchecker_link_replace()

Replaces old link with new link in text.

Parameters

string $text: The text a link is inside. Passed in as a reference.

string $old_link_fqdn: The old link to search for in strings.

string $new_link_fqdn: The old link should be overwritten with this new link.

2 calls to _linkchecker_link_replace()
_linkchecker_replace_fields in ./linkchecker.module
Replace the old url by a new url on 301 status codes.
_linkchecker_status_handling in ./linkchecker.module
Status code handling.

File

./linkchecker.module, line 2125
This module periodically check links in given node types, blocks etc.

Code

function _linkchecker_link_replace(&$text, $old_link_fqdn = '', $new_link_fqdn = '') {

  // Don't do any string replacement if one of the values is empty.
  if (!empty($text) && !empty($old_link_fqdn) && !empty($new_link_fqdn)) {

    // Remove protocols and hostname from local URLs.
    $base_roots = array(
      drupal_strtolower('http://' . $_SERVER['HTTP_HOST']),
      drupal_strtolower('https://' . $_SERVER['HTTP_HOST']),
    );
    $old_link = str_replace($base_roots, '', $old_link_fqdn);
    $new_link = str_replace($base_roots, '', $new_link_fqdn);

    // Build variables with all URLs and run check_url() only once.
    $old_html_link_fqdn = check_url($old_link_fqdn);
    $new_html_link_fqdn = check_url($new_link_fqdn);
    $old_html_link = check_url($old_link);
    $new_html_link = check_url($new_link);

    // Replace links in link fields and text and Links weblink fields.
    if (in_array($text, array(
      $old_html_link_fqdn,
      $old_html_link,
      $old_link_fqdn,
      $old_link,
    ))) {

      // Keep old and new links in the same encoding and format and short or
      // fully qualified.
      $text = str_replace($old_html_link_fqdn, $new_html_link_fqdn, $text);
      $text = str_replace($old_html_link, $new_html_link, $text);
      $text = str_replace($old_link_fqdn, $new_link_fqdn, $text);
      $text = str_replace($old_link, $new_link, $text);
    }
    else {

      // Create an array of links with HTML decoded and encoded URLs.
      $old_links = array(
        $old_html_link_fqdn,
        $old_html_link,
        $old_link,
      );

      // Remove duplicate URLs from array if URLs do not have URL parameters.
      // If more than one URL parameter exists - one URL in the array will have
      // an unencoded ampersand "&" and a second URL will have an HTML encoded
      // ampersand "&".
      $old_links = array_unique($old_links);

      // Load HTML code into DOM.
      $html_dom = filter_dom_load($text);

      // Finds all hyperlinks in the content.
      if (variable_get('linkchecker_extract_from_a', 1) == 1) {
        $links = $html_dom
          ->getElementsByTagName('a');
        foreach ($links as $link) {
          if (in_array($link
            ->getAttribute('href'), $old_links)) {
            $link
              ->setAttribute('href', $new_html_link);
          }

          // Replace link text, if same like the URL. If a link text contains
          // other child tags like <img> it will be skipped.
          if (in_array($link->nodeValue, $old_links)) {
            $link->nodeValue = $new_html_link;
          }
        }
        $links = $html_dom
          ->getElementsByTagName('area');
        foreach ($links as $link) {
          if (in_array($link
            ->getAttribute('href'), $old_links)) {
            $link
              ->setAttribute('href', $new_html_link);
          }
        }
      }

      // Finds all audio links in the content.
      if (variable_get('linkchecker_extract_from_audio', 0) == 1) {
        $audios = $html_dom
          ->getElementsByTagName('audio');
        foreach ($audios as $audio) {
          if (in_array($audio
            ->getAttribute('src'), $old_links)) {
            $audio
              ->setAttribute('src', $new_html_link);
          }

          // Finds source tags with links in the audio tag.
          $sources = $audio
            ->getElementsByTagName('source');
          foreach ($sources as $source) {
            if (in_array($source
              ->getAttribute('src'), $old_links)) {
              $source
                ->setAttribute('src', $new_html_link);
            }
          }

          // Finds track tags with links in the audio tag.
          $tracks = $audio
            ->getElementsByTagName('track');
          foreach ($tracks as $track) {
            if (in_array($track
              ->getAttribute('src'), $old_links)) {
              $track
                ->setAttribute('src', $new_html_link);
            }
          }
        }
      }

      // Finds embed tags with links in the content.
      if (variable_get('linkchecker_extract_from_embed', 0) == 1) {
        $embeds = $html_dom
          ->getElementsByTagName('embed');
        foreach ($embeds as $embed) {
          if (in_array($embed
            ->getAttribute('src'), $old_links)) {
            $embed
              ->setAttribute('src', $new_html_link);
          }
          if (in_array($embed
            ->getAttribute('pluginurl'), $old_links)) {
            $embed
              ->setAttribute('pluginurl', $new_html_link);
          }
          if (in_array($embed
            ->getAttribute('pluginspage'), $old_links)) {
            $embed
              ->setAttribute('pluginspage', $new_html_link);
          }
        }
      }

      // Finds iframe tags with links in the content.
      if (variable_get('linkchecker_extract_from_iframe', 0) == 1) {
        $iframes = $html_dom
          ->getElementsByTagName('iframe');
        foreach ($iframes as $iframe) {
          if (in_array($iframe
            ->getAttribute('src'), $old_links)) {
            $iframe
              ->setAttribute('src', $new_html_link);
          }
        }
      }

      // Finds img tags with links in the content.
      if (variable_get('linkchecker_extract_from_img', 0) == 1) {
        $imgs = $html_dom
          ->getElementsByTagName('img');
        foreach ($imgs as $img) {
          if (in_array($img
            ->getAttribute('src'), $old_links)) {
            $img
              ->setAttribute('src', $new_html_link);
          }
          if (in_array($img
            ->getAttribute('longdesc'), $old_links)) {
            $img
              ->setAttribute('longdesc', $new_html_link);
          }
        }
      }

      // Finds object/param tags with links in the content.
      if (variable_get('linkchecker_extract_from_object', 0) == 1) {
        $objects = $html_dom
          ->getElementsByTagName('object');
        foreach ($objects as $object) {
          if (in_array($object
            ->getAttribute('data'), $old_links)) {
            $object
              ->setAttribute('data', $new_html_link);
          }
          if (in_array($object
            ->getAttribute('codebase'), $old_links)) {
            $object
              ->setAttribute('codebase', $new_html_link);
          }

          // Finds param tags with links in the object tag.
          $params = $object
            ->getElementsByTagName('param');
          foreach ($params as $param) {

            // @todo
            // - Try to replace links in unkown "flashvars" values
            //   (e.g., file=http://, data=http://).
            $names = array(
              'archive',
              'filename',
              'href',
              'movie',
              'src',
              'url',
            );
            if ($param
              ->hasAttribute('name') && in_array($param
              ->getAttribute('name'), $names)) {
              if (in_array($param
                ->getAttribute('value'), $old_links)) {
                $param
                  ->setAttribute('value', $new_html_link);
              }
            }
            $srcs = array(
              'movie',
            );
            if ($param
              ->hasAttribute('src') && in_array($param
              ->getAttribute('src'), $srcs)) {
              if (in_array($param
                ->getAttribute('value'), $old_links)) {
                $param
                  ->setAttribute('value', $new_html_link);
              }
            }
          }
        }
      }

      // Finds video tags with links in the content.
      if (variable_get('linkchecker_extract_from_video', 0) == 1) {
        $videos = $html_dom
          ->getElementsByTagName('video');
        foreach ($videos as $video) {
          if (in_array($video
            ->getAttribute('poster'), $old_links)) {
            $video
              ->setAttribute('poster', $new_html_link);
          }
          if (in_array($video
            ->getAttribute('src'), $old_links)) {
            $video
              ->setAttribute('src', $new_html_link);
          }

          // Finds source tags with links in the video tag.
          $sources = $video
            ->getElementsByTagName('source');
          foreach ($sources as $source) {
            if (in_array($source
              ->getAttribute('src'), $old_links)) {
              $source
                ->setAttribute('src', $new_html_link);
            }
          }

          // Finds track tags with links in the audio tag.
          $tracks = $video
            ->getElementsByTagName('track');
          foreach ($tracks as $track) {
            if (in_array($track
              ->getAttribute('src'), $old_links)) {
              $track
                ->setAttribute('src', $new_html_link);
            }
          }
        }
      }

      // Set the updated $text for the calling function.
      $text = filter_dom_serialize($html_dom);
    }
  }
}