You are here

function _pathologic in Pathologic 6.3

Same name and namespace in other branches
  1. 7 pathologic.module \_pathologic()

Pathologic filter callback.

1 call to _pathologic()
pathologic_filter in ./pathologic.module
Implementation of hook_filter_info().

File

./pathologic.module, line 52
Pathologic text filter for Drupal.

Code

function _pathologic($text, $format) {
  static $statics = array();
  if (!isset($statics[$format])) {

    // Parse the list of the paths also considered local.
    $paths_text = trim(variable_get('filter_pathologic_local_paths_' . $format, ''));
    if ($paths_text === '') {
      $paths = array();
    }
    else {
      $paths = array_map('trim', explode("\n", $paths_text));
    }

    // Add "this" path
    $paths[] = url('<front>', array(
      'absolute' => TRUE,
    ));

    // Remove duplicates, since it's possible "this" path was already in the list;
    // also do escaping
    $paths = array_map('_pathologic_escape', array_unique($paths));

    // We need to account for the fact that
    // http://example.com/foo
    // http://example.com/?q=foo
    // http://example.com/index.php?q=foo
    // …are all valid.
    $statics[$format] = array(
      // The pattern is gonna look like:
      // ~(href|src|HREF|SRC)="
      // (internal:|https?://example\.com|https?://example\.org)?
      // (
      //   /(#.*)?|
      //   (?!(#|/|mailto:|[a-z]+:/))
      //   ((index\.php)?(\?q=)?)
      //   ([^"]*)
      // )"~
      'pattern' => '~(href|src|HREF|SRC)="(internal:|' . implode('|', $paths) . ')?(/(#.*)?|(?!(#|/|mailto:|[a-z]+:/))((index\\.php)?(\\?q=)?)([^"]*))"~',
      // create_funtion() lets us do lambdas in a really crappy but pre-PHP 5.3-
      // compatible way. We're using it here so we can pass the value of
      // $filter->settings['absolute'] to the replacement function. We could
      // just put the whole replacement function here, but that would just be
      // silly.
      'callback' => create_function('$matches', 'return _pathologic_replace($matches, ' . (variable_get('filter_pathologic_absolute_' . $format, TRUE) ? 'TRUE' : 'FALSE') . ');'),
    );
  }
  return preg_replace_callback($statics[$format]['pattern'], $statics[$format]['callback'], $text);
}