You are here

function custom_spam_filter in Spam 5.3

Apply enabled custom filter rules against content.

1 call to custom_spam_filter()
custom_spamapi in filters/custom/custom.module
Spam API Hook

File

filters/custom/custom.module, line 461

Code

function 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_custom} WHERE status != %d ORDER BY weight ASC', SPAM_CUSTOM_STATUS_DISABLED);
  while ($custom = db_fetch_object($result)) {
    $scan = '';
    if ($custom->scan & SPAM_CUSTOM_SCAN_CONTENT) {

      // scan content
      if (is_object($content)) {
        $content = (array) $content;
      }
      $scan .= spam_get_text($content, $type, $fields, $extra);
      spam_log(SPAM_DEBUG, 'custom_spam_filter', t('scanning content with %filter.', array(
        '%filter' => $custom->filter,
      )), $type, $id);
    }
    if ($custom->scan & SPAM_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, 'custom_spam_filter', t('scanning referrer with %filter.', array(
        '%filter' => $custom->filter,
      )), $type, $id);
    }
    if ($custom->scan & SPAM_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, 'custom_spam_filter', t('scanning user agent with %filter.', array(
        '%filter' => $custom->filter,
      )), $type, $id);
    }
    switch ($custom->style) {
      case SPAM_CUSTOM_STYLE_PLAIN:
        $match = preg_match_all("/{$custom->filter}/", $scan, $matches);
        break;
      case SPAM_CUSTOM_STYLE_REGEX:
        $match = preg_match_all($custom->filter, $scan, $matches);
        break;
    }
    if ($match) {

      // Record that we've had one or more matches.
      db_query('UPDATE {spam_custom} SET matches = matches + %d, last = %d WHERE cid = %d', $match, time(), $custom->cid);
      spam_log(SPAM_VERBOSE, 'custom_spam_filter', t('matched with %filter.', array(
        '%filter' => $custom->filter,
      )), $type, $id);
      $action['custom'][] = array(
        'filter' => $custom->filter,
        'status' => $custom->status,
        'style' => $custom->style,
        'scan' => $custom->scan,
        'extra' => $custom->extra,
      );
      switch ($custom->status) {
        case SPAM_CUSTOM_STATUS_SPAM:
          spam_log(SPAM_VERBOSE, '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_CUSTOM_STATUS_NOTSPAM:
          spam_log(SPAM_VERBOSE, '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_CUSTOM_STATUS_PROBABLYNOT:
          spam_log(SPAM_DEBUG, '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_CUSTOM_STATUS_PROBABLY:
          spam_log(SPAM_DEBUG, '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, 'custom_spam_filter', t('matched adjusted total of !number probably spam rule(s).', array(
      '!number' => $probably,
    )), $type, $id);
    if ($probably >= variable_get('spam_custom_probably', 3)) {
      $action['total'] = 99;
    }
    else {
      $action['total'] = variable_get('spam_custom_probably_value', variable_get('spam_threshold', SPAM_DEFAULT_THRESHOLD));
    }
  }
  else {
    spam_log(SPAM_VERBOSE, '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_custom_probablynot', 3)) {
      $action['total'] = 1;
    }
    else {
      $action['total'] = variable_get('spam_custom_probablynot_value', 40);
    }
  }
  return $action;
}