You are here

function spam_content_filter in Spam 5.3

Same name and namespace in other branches
  1. 5 spam.module \spam_content_filter()
  2. 6 spam.module \spam_content_filter()

API call to determine the likeliness that a given piece of content is spam, returning a rating from 1% likelihood to 99% likelihood. It is unlikely that you want to call this function directly.

Parameters

$content: An array holding the complete content.

$type: A string naming the type of content to be filtered.

3 calls to spam_content_filter()
spam_content_insert in ./spam.module
This function is called when new content is first posted to your website.
spam_content_is_spam in ./spam.module
API call to simply test if content is spam or not. No action is taken.
spam_content_update in ./spam.module
This function is called when content on your website is updated.

File

./spam.module, line 119

Code

function spam_content_filter($content, $type, $extra, $filter_test = FALSE) {
  if (user_access('bypass filters')) {
    spam_log(SPAM_DEBUG, 'spam_content_filter', t('bypassing filters'), $type, $id);
    return;
  }
  if (!spam_filter_content_type($content, $type, $extra)) {
    return;
  }
  static $scores = array();
  $id = spam_invoke_module($type, 'content_id', $content, $extra);
  spam_log(SPAM_DEBUG, 'spam_content_filter', t('invoking content filters'), $type, $id);
  if (!$id || !$scores["{$type}-{$id}"]) {

    // Determine which fields we need to run through the spam filter.
    $fields = spam_invoke_module($type, 'filter_fields', $content, $extra);
    if (!empty($fields) && is_array($fields['main'])) {

      // TODO: Once content-type groups are implemented, this query will
      // determine  which group to filter the given piece of content with.  It
      // will default to a gid of 0 if undefined.

      //$gid = (int)db_result(db_query("SELECT gid FROM {spam_filters_groups_data} WHERE content_type = '%s'", $type));
      $gid = $score = $total = 0;
      $filters = db_query('SELECT name, module, gain FROM {spam_filters} WHERE gid = %d AND status = %d ORDER BY weight', $gid, SPAM_FILTER_ENABLED);
      $counter = 0;
      while ($filter = db_fetch_object($filters)) {
        $counter++;
        spam_log(SPAM_DEBUG, 'spam_content_filter', t('invoking @filter [@counter], gain = @gain', array(
          '@filter' => $filter->name,
          '@counter' => $counter,
          '@gain' => $filter->gain,
        )), $type, $id);
        $actions[$filter->module] = spam_invoke_module($filter->module, 'filter', $type, $content, $fields, $extra, $filter_test);
        spam_log(SPAM_VERBOSE, 'spam_content_filter', t('@filter: total(@total) redirect(@redirect) gain(@gain)', array(
          '@filter' => $filter->name,
          '@total' => $actions[$filter->module]['total'],
          '@redirect' => $actions[$filter->module]['redirect'],
          '@gain' => $filter->gain,
        )), $type, $id);
        if ($actions[$filter->module]['total']) {
          $score += $actions[$filter->module]['total'] * $filter->gain;
          $total += $filter->gain;
          spam_log(SPAM_DEBUG, 'spam_content_filter', t('current score(@score) current total(@total) average(@average)', array(
            '@score' => $score,
            '@total' => $total,
            '@average' => spam_sanitize_score($score / $total),
          )), $type, $id);
          if ($actions[$filter->module]['redirect']) {
            $redirect = $actions[$filter->module]['redirect'];
            break;
          }
        }
      }
      if ($id) {
        if ($total) {
          $scores["{$type}-{$id}"] = spam_sanitize_score($score / $total);
        }
        else {
          $scores["{$type}-{$id}"] = 1;
        }
      }
    }
    if (isset($redirect)) {
      if ($id) {

        // A filter has us redirecting to an error screen, but this content
        // has an id so we need to update its spam status in the database first.
        if ($scores["{$type}-{$id}"] >= variable_get('spam_threshold', SPAM_DEFAULT_THRESHOLD)) {
          spam_mark_as_spam($type, $id);
        }
        else {
          spam_mark_as_not_spam($type, $id);
        }
      }
      else {
        spam_update_statistics(t('prevented spam @type', array(
          '@type' => $type,
        )));
      }
      spam_update_statistics(t('detected spam'));
      spam_update_statistics(t('content_filter redirect'));
      drupal_goto($redirect);
    }
  }
  if ($id) {
    $score = $scores["{$type}-{$id}"];
  }
  else {
    if ($total) {
      $score = spam_sanitize_score($score / $total);
    }
    else {
      $score = 1;
    }
  }
  spam_log(SPAM_VERBOSE, 'spam_content_filter', t('final average(@score)', array(
    '@score' => $score,
  )), $type, $id);
  return $score;
}