You are here

function spam_filter_custom_spam_filter in Spam 6

Apply enabled custom filter rules against content.

1 call to spam_filter_custom_spam_filter()
spam_filter_custom_spamapi in filters/spam_filter_custom/spam_filter_custom.module
Spam API Hook

File

filters/spam_filter_custom/spam_filter_custom.module, line 474
Custom spam filter module Copyright(c) 2007-2009 Jeremy Andrews <jeremy@tag1consulting.com>.

Code

function spam_filter_custom_spam_filter($content, $type, $fields, $extra = array(), $filter_test = FALSE) {
  $probably = $probably_not = 0;
  $id = spam_invoke_module($type, 'content_id', $content, $extra);
  $result = db_query('SELECT cid, filter, style, status, scan, action FROM {spam_filter_custom} WHERE status <> %d ORDER BY weight ASC', SPAM_FILTER_CUSTOM_STATUS_DISABLED);
  while ($spam_filter_custom = db_fetch_object($result)) {
    $scan = '';
    if ($spam_filter_custom->scan & SPAM_FILTER_CUSTOM_SCAN_CONTENT) {

      // scan content
      if (is_object($content)) {
        $content = (array) $content;
      }
      $scan .= spam_get_text($content, $type, $fields, $extra);
      spam_log(SPAM_DEBUG, 'spam_filter_custom_spam_filter', t('scanning content with %filter.', array(
        '%filter' => $spam_filter_custom->filter,
      )), $type, $id);
    }
    if ($spam_filter_custom->scan & SPAM_FILTER_CUSTOM_SCAN_REFERRER) {

      // scan referrer
      // TODO: Determine if this is a live scan.  If not, don't scan referrer.
      $scan .= $_SERVER['HTTP_REFERER'];
      spam_log(SPAM_DEBUG, 'spam_filter_custom_spam_filter', t('scanning referrer with %filter.', array(
        '%filter' => $spam_filter_custom->filter,
      )), $type, $id);
    }
    if ($spam_filter_custom->scan & SPAM_FILTER_CUSTOM_SCAN_USERAGENT) {

      // scan user agent
      // TODO: Determine if this is a live scan.  If not, don't scan user agent.
      $scan .= $_SERVER['HTTP_USER_AGENT'];
      spam_log(SPAM_DEBUG, 'spam_filter_custom_spam_filter', t('scanning user agent with %filter.', array(
        '%filter' => $spam_filter_custom->filter,
      )), $type, $id);
    }
    switch ($spam_filter_custom->style) {
      case SPAM_FILTER_CUSTOM_STYLE_PLAIN:
        $match = preg_match_all('/' . preg_quote($spam_filter_custom->filter, '/') . '/', $scan, $matches);
        break;
      case SPAM_FILTER_CUSTOM_STYLE_REGEX:
        $match = preg_match_all($spam_filter_custom->filter, $scan, $matches);
        break;
    }
    if ($match) {

      // Record that we've had one or more matches.
      db_query('UPDATE {spam_filter_custom} SET matches = matches + %d, last = %d WHERE cid = %d', $match, time(), $spam_filter_custom->cid);
      spam_log(SPAM_VERBOSE, 'spam_filter_custom_spam_filter', t('matched with %filter.', array(
        '%filter' => $spam_filter_custom->filter,
      )), $type, $id);
      $action['spam_filter_custom'][] = array(
        'filter' => $spam_filter_custom->filter,
        'status' => $spam_filter_custom->status,
        'style' => $spam_filter_custom->style,
        'scan' => $spam_filter_custom->scan,
        'extra' => $spam_filter_custom->extra,
      );
      switch ($spam_filter_custom->status) {
        case SPAM_FILTER_CUSTOM_STATUS_SPAM:
          spam_log(SPAM_VERBOSE, 'spam_filter_custom_spam_filter', t('content is spam.'), $type, $id);

          // no need to scan any more, we've found spam
          $action['total'] = 99;
          return $action;
        case SPAM_FILTER_CUSTOM_STATUS_NOTSPAM:
          spam_log(SPAM_VERBOSE, 'spam_filter_custom_spam_filter', t('content is not spam.'), $type, $id);

          // no need to scan any more, we've found non-spam
          $action['total'] = 1;
          return $action;
        case SPAM_FILTER_CUSTOM_STATUS_PROBABLYNOT:
          spam_log(SPAM_DEBUG, 'spam_filter_custom_spam_filter', t('content is probably not spam.'), $type, $id);

          // maintain internal counter that this is probably not spam
          $probably_not += $match;
          break;
        case SPAM_FILTER_CUSTOM_STATUS_PROBABLY:
          spam_log(SPAM_DEBUG, 'spam_filter_custom_spam_filter', t('content is probably spam.'), $type, $id);

          // maintain internal counter that this is probably spam
          $probably += $match;
          break;
      }
    }
  }
  if ($probably && $probably_not) {
    if ($probably >= $probably_not) {
      $probably -= $probably_not;
      $probably_not = 0;
    }
    else {
      $probably_not -= $probably;
      $probably = 0;
    }
  }
  if ($probably) {
    spam_log(SPAM_VERBOSE, 'spam_filter_custom_spam_filter', t('matched adjusted total of !number probably spam rule(s).', array(
      '!number' => $probably,
    )), $type, $id);
    if ($probably >= variable_get('spam_filter_custom_probably', 3)) {
      $action['total'] = 99;
    }
    else {
      $action['total'] = variable_get('spam_filter_custom_probably_value', variable_get('spam_threshold', SPAM_DEFAULT_THRESHOLD));
    }
  }
  else {
    if ($probably_not) {
      spam_log(SPAM_VERBOSE, 'spam_filter_custom_spam_filter', t('matched adjusted total of !number probably-not spam rule(s).', array(
        '!number' => $probably_not,
      )), $type, $id);
      if ($probably_not >= variable_get('spam_filter_custom_probablynot', 3)) {
        $action['total'] = 1;
      }
      else {
        $action['total'] = variable_get('spam_filter_custom_probablynot_value', 40);
      }
    }
    else {

      // No matched filters, so don't change the overall spam score.
      $action['total'] = 0;
    }
  }
  return $action;
}