You are here

function _htmlpurifier_process_text in HTML Purifier 7

Same name and namespace in other branches
  1. 7.2 htmlpurifier.module \_htmlpurifier_process_text()

Processes HTML according to a format and returns purified HTML. Makes a cache pass if possible.

@note We ignore $delta because the only difference it makes is in the configuration screen.

Parameters

string $text: Text to purify

object $filter: The filter object containing settings for the given format.

object $format: The format object of the text to be filtered.

string $langcode: The language code of the text to be filtered.

boolean $cache: Whether or not to check the cache.

2 calls to _htmlpurifier_process_text()
_htmlpurifier_process in ./htmlpurifier.module
Process callback, used by htmlpurifier_filter_info().
_htmlpurifier_settings in ./htmlpurifier.module
Generates a settings form for configuring HTML Purifier.

File

./htmlpurifier.module, line 247
Implements HTML Purifier as a Drupal filter.

Code

function _htmlpurifier_process_text($text, $filter, $format, $langcode, $cache = TRUE) {

  // No need to run the filter if there isn't anything to filter
  // See https://drupal.org/node/1821178
  if ($text === '') {
    return;
  }
  if ($cache) {
    $cid = $format->format . ':' . $langcode . ':' . hash('sha256', $text);
    $old = cache_get($cid, 'cache_htmlpurifier');
    if ($old) {
      return $old->data;
    }
  }
  _htmlpurifier_load();
  $config = _htmlpurifier_get_config($format->format);

  // If ExtractStyleBlocks is enabled, we'll need to do a bit more for CSSTidy
  $config_extractstyleblocks = $config
    ->get('Filter.ExtractStyleBlocks');

  // Maybe this works if CSSTidy is at root? CSSTidy could be other places though
  if ($config_extractstyleblocks == TRUE) {
    _htmlpurifier_load_csstidy();
  }
  $purifier = new HTMLPurifier($config);
  $ret = $purifier
    ->purify($text);

  // If using Filter.ExtractStyleBlocks we need to handle the CSSTidy output
  if ($config_extractstyleblocks == TRUE) {

    // We're only going to bother if we're caching! - no caching? no style blocks!
    if ($cache) {

      // Get style blocks, cache them, and help hook_nodeapi find the cache
      $styles = $purifier->context
        ->get('StyleBlocks');
      cache_set('css:' . $cid, $styles, 'cache_htmlpurifier', CACHE_PERMANENT);
      $ret = '<!-- HTML Purifier Cache #' . $cid . ' -->' . $ret;
    }
  }
  if ($cache) {
    cache_set($cid, $ret, 'cache_htmlpurifier', CACHE_PERMANENT);
  }
  return $ret;
}