You are here

function _pathologic in Pathologic 7

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

Pathologic filter callback.

1 call to _pathologic()
PathologicTestCase::testPathologic in ./pathologic.test
2 string references to '_pathologic'
PathologicTestCase::testPathologic in ./pathologic.test
pathologic_filter_info in ./pathologic.module
Implements hook_filter_info().

File

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

Code

function _pathologic($text, $filter) {
  $statics =& drupal_static(__FUNCTION__);
  if (!isset($statics)) {
    $statics = array();
  }
  if (!isset($statics[$filter->format])) {

    // Parse the list of the paths also considered local.
    $paths_text = trim($filter->settings['local_paths']);
    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));

    // Create a list of protocol prefixes so we can ignore them
    $proto_prefixes = implode('|', variable_get('filter_allowed_protocols', array(
      'ftp',
      'http',
      'https',
      'irc',
      'mailto',
      'news',
      'nntp',
      'rtsp',
      'sftp',
      'ssh',
      'tel',
      'telnet',
      'webcal',
    )));

    // 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[$filter->format] = array(
      'pattern' => '
        ~(href|src|action|longdesc|HREF|SRC|ACTION|LONGDESC)=" # HTML attributes
                                                                 # to search for
        (internal:|files:|' . implode('|', $paths) . ')?      # Path prefixes to
                                                                     #search for
        (?:
          /(?:\\#.*)?|            # A / path with a possible anchor fragment, or…
          (?!(?:\\#|/|(?:' . $proto_prefixes . '):/))  # Exclude likely protocols
          (?:(?:index\\.php)?(?:\\?q=)?) # Allow for paths which include index.php
                                                                    # and/or ?q=
          (?!/")([^"]*)                                 # Finally, get the path.
        )
        (?<!"/)            # But bail out if the path we just found was just "/"
        "~x',
      // 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, ' . ($filter->settings['absolute'] ? 'TRUE' : 'FALSE') . ');'),
    );
  }
  return preg_replace_callback($statics[$filter->format]['pattern'], $statics[$filter->format]['callback'], $text);
}