You are here

function _pathfilter_internalize in Path Filter 6.2

Same name and namespace in other branches
  1. 5.2 pathfilter.module \_pathfilter_internalize()
  2. 7 pathfilter.module \_pathfilter_internalize()

Internalizes all urls in a string automatically, doing the user's job for them.

1 string reference to '_pathfilter_internalize'
pathfilter_nodeapi in ./pathfilter.module
Modifies submitted node body values. Replaces base_path() in images, hrefs, etc with 'internal:' on save. When editing a node, the body's 'internal:' strings are replaced with 'base_path()'.

File

./pathfilter.module, line 273
This filter takes internal Drupal paths in double quotes, written as e.g. "internal:node/99", and replaces them with the appropriate absolute http URL using Drupal's url() function [1]. E.g. for a site located at http://example.com/mysite

Code

function _pathfilter_internalize($format, &$item) {

  // First we find all of the items that look like they need to be replaced.
  $pattern = '/(<img|<a|<script|<object|<form|<param)[^\\>]*>/i';
  preg_match_all($pattern, $item, $matches);

  // Then we normalize the links of the items that matched and do
  // 'files:' replacement and then 'internal:' replacement.
  foreach ($matches[0] as $match) {

    // Obtain the URL out of the html tag.
    preg_match('/(src=|href=|action=|data=|value=)(\'|")([^\'"]*)(\'|"|>)/', $match, $url);

    // Do replacement with 'files:' if appropriate, 'internal:' otherwise.
    $base_files_url_re = _pathfilter_base_files_url_re();
    $replaced_path = preg_replace($base_files_url_re, 'files:', $url[3], 1);
    if ($replaced_path == $url[3]) {

      // 'files:' was not found, try 'internal:'.
      // remove the base path
      $base_url_re = _pathfilter_base_url_re();
      $tmp_replaced_path = preg_replace($base_url_re, '', $url[3], 1);

      // remove any language prefix or q= string
      $tmp_replaced_path = preg_replace('#^[^/](.*/?)??(node/[0-9]*)$#', "\$2", $tmp_replaced_path);

      // ensure we have an internal path
      $tmp_replaced_path = drupal_get_normal_path($tmp_replaced_path);

      // If this link is to a node
      preg_match('#^node/[0-9]*$#', $tmp_replaced_path, $matches);
      if ($matches) {

        // add the internal: prefix
        $replaced_path = 'internal:' . $tmp_replaced_path;
      }
    }

    // Update original string with changes, if needed.
    if ($replaced_path != $url[3]) {
      $item = preg_replace('/(src=|href=|action=|data=|value=)(\'|")(' . preg_quote($url[3], '/') . ')(\'|"|>)/', '$1$2' . $replaced_path . '$4', $item, 1);
    }
  }
}