You are here

function _pathfilter_internalize in Path Filter 5.2

Same name and namespace in other branches
  1. 6.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 256
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)[^\\>]*>/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=)(\'|")([^\'"]*)(\'|"|>)/', $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:'.
      $base_url_re = _pathfilter_base_url_re();
      $replaced_path = preg_replace($base_url_re, '', $url[3], 1);
      $replaced_path = drupal_get_normal_path($replaced_path);
      $replaced_path = 'internal:' . $replaced_path;
    }

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