You are here

function _pathologic_filter in Pathologic 8

Same name and namespace in other branches
  1. 7.3 pathologic.module \_pathologic_filter()
  2. 7.2 pathologic.module \_pathologic_filter()

Pathologic filter callback.

@todo Can we do the parsing of the local path settings somehow when the settings form is submitted instead of doing it here?

1 call to _pathologic_filter()
FilterPathologic::process in src/Plugin/Filter/FilterPathologic.php
Performs the filter processing.
1 string reference to '_pathologic_filter'
_pathologic_replace in ./pathologic.module
Process and replace paths. preg_replace_callback() callback.

File

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

Code

function _pathologic_filter($text, $settings, $hash) {

  // Get the base URL and explode it into component parts. We add these parts
  // to the exploded local paths settings later.
  global $base_url;
  $base_url_parts = parse_url($base_url . '/');

  // Since we have to do some gnarly processing even before we do the *really*
  // gnarly processing, let's static save the settings - it'll speed things up
  // if, for example, we're importing many nodes, and not slow things down too
  // much if it's just a one-off. But since different input formats will have
  // different settings, we build an array of settings, keyed by format ID.
  $cached_settings =& drupal_static(__FUNCTION__, []);
  if (!isset($cached_settings[$hash])) {
    $settings['local_paths_exploded'] = [];
    if ($settings['local_paths'] !== '') {

      // Build an array of the exploded local paths for this format's settings.
      // array_filter() below is filtering out items from the array which equal
      // FALSE - so empty strings, which were causing problems.
      // @see http://drupal.org/node/1727492
      $local_paths = array_filter(array_map('trim', explode("\n", $settings['local_paths'])));
      foreach ($local_paths as $local) {
        $parts = parse_url($local);

        // Okay, what the hellish "if" statement is doing below is checking to
        // make sure we aren't about to add a path to our array of exploded
        // local paths which matches the current "local" path. We consider it
        // not a match, if…
        // @todo: This is pretty horrible. Can this be simplified?
        if (isset($parts['host']) && ($parts['host'] !== $base_url_parts['host'] || ((isset($parts['path']) xor isset($base_url_parts['path'])) || isset($parts['path']) && isset($base_url_parts['path']) && $parts['path'] !== $base_url_parts['path'])) || !isset($parts['host']) && (!isset($parts['path']) || !isset($base_url_parts['path']) || $parts['path'] !== $base_url_parts['path'])) {

          // Add it to the list.
          $settings['local_paths_exploded'][] = $parts;
        }
      }
    }

    // Now add local paths based on "this" server URL.
    $settings['local_paths_exploded'][] = [
      'path' => $base_url_parts['path'],
    ];
    $settings['local_paths_exploded'][] = [
      'path' => $base_url_parts['path'],
      'host' => $base_url_parts['host'],
    ];

    // We'll also just store the host part separately for easy access.
    $settings['base_url_host'] = $base_url_parts['host'];
    $cached_settings[$hash] = $settings;
  }

  // Take note of which settings in the settings array should apply.
  $cached_settings['current_settings'] =& $cached_settings[$hash];

  // Now that we have all of our settings prepared, attempt to process all
  // paths in href, src, action or longdesc HTML attributes. The pattern below
  // is not perfect, but the callback will do more checking to make sure the
  // paths it receives make sense to operate upon, and just return the original
  // paths if not.
  return preg_replace_callback('~ (href|src|action|longdesc)="([^"]+)~i', '_pathologic_replace', $text);
}