You are here

function duplicate_spamapi in Spam 5.3

Spam API Hook

File

filters/duplicate/duplicate.module, line 107

Code

function duplicate_spamapi($op, $type = NULL, $content = array(), $fields = array(), $extra = NULL) {
  switch ($op) {
    case 'filter':
      if (!module_invoke('spam', 'filter_enabled', 'duplicate', $type, $content, $fields, $extra)) {
        return;
      }
      return duplicate_spam_filter($content, $type, $fields, $extra);
    case 'filter_module':
      return 'duplicate';
    case 'insert':
      if (!module_invoke('spam', 'filter_enabled', 'duplicate', $type, $content, $fields, $extra)) {
        return;
      }
      if (is_array($extra) && $extra['sid'] && $extra['host'] && !empty($content) && !empty($fields)) {
        $hash = _duplicate_content_hash($content, $fields);
        db_query("INSERT INTO {spam_duplicate} (sid, content_hash, hostname, timestamp) VALUES(%d, '%s', '%s', %d)", $extra['sid'], $hash, $extra['host'], time());
        $action = _duplicate_action();
        if (is_array($action) && !empty($action)) {
          if (isset($action['redirect'])) {
            drupal_goto($action['redirect']);
          }
        }
      }
      break;
    case 'update':
      if (!module_invoke('spam', 'filter_enabled', 'duplicate', $type, $content, $fields, $extra)) {
        return;
      }
      if (is_array($extra) && $extra['sid'] && $extra['host'] && !empty($content) && !empty($fields)) {
        $hash = _duplicate_content_hash($content, $fields);
        db_query("UPDATE {spam_duplicate} SET content_hash = '%s', hostname = '%s', timestamp = %d WHERE sid = %d", $hash, $extra['host'], time(), $extra['sid']);
        if (!db_affected_rows()) {
          db_query("INSERT INTO {spam_duplicate} (sid, content_hash, hostname, timestamp) VALUES(%d, '%s', '%s', %d)", $extra['sid'], $hash, $extra['host'], time());
        }
        $action = _duplicate_action();
        if (is_array($action) && !empty($action)) {
          if (isset($action['redirect'])) {
            drupal_goto($action['redirect']);
          }
        }
      }
      break;
    case 'delete':
      if (is_array($extra) && $extra['sid'] && !empty($content) && !empty($fields)) {
        db_query("DELETE FROM {spam_duplicate} WHERE sid = %d", $extra['sid']);
      }
      break;
    case 'filter_info':
      return array(
        'name' => t('Duplicate filter'),
        'module' => t('duplicate'),
        'description' => t('A duplication spam filter.'),
        'help' => t('The duplicate filter detects spam by detecting content duplication.'),
      );
      break;
    case 'filter_install':
      return array(
        'status' => SPAM_FILTER_ENABLED,
        'weight' => -8,
      );
    case 'mark_as_spam':
      if (!module_invoke('spam', 'filter_enabled', 'duplicate', $type, $content, $fields, $extra)) {
        return;
      }
      db_query('UPDATE {spam_duplicate} SET spam = %d WHERE sid = %d', DUPLICATE_SPAM, $extra['sid']);
      if (!db_affected_rows() && $extra['id'] && $extra['sid']) {
        $content = spam_invoke_module($type, 'load', $extra['id']);
        $fields = spam_invoke_module($type, 'filter_fields', $content);
        $hash = _duplicate_content_hash($content, $fields);
        $hostname = spam_invoke_module($type, 'hostname', $extra['id']);
        db_query("INSERT INTO {spam_duplicate} (sid, content_hash, hostname, timestamp) VALUES(%d, '%s', '%s', %d)", $extra['sid'], $hash, $hostname, time());
      }
      $action = _duplicate_action();
      if (is_array($action) && isset($action['redirect'])) {
        return $action['redirect'];
      }
      break;
    case 'mark_as_not_spam':
      if (!module_invoke('spam', 'filter_enabled', 'duplicate', $type, $content, $fields, $extra)) {
        return;
      }
      db_query('UPDATE {spam_duplicate} SET spam = %d WHERE sid = %d', DUPLICATE_NOT_SPAM, $extra['sid']);
      if (!db_affected_rows() && $extra['id'] && $extra['sid']) {

        // Updating content that we've not filtered before.  Retrive all the
        // data we need to add it to the spam_duplicate table.
        $fields = spam_invoke_module($type, 'filter_fields', $extra['content']);
        $hash = _duplicate_content_hash($extra['content'], $fields);
        $hostname = spam_invoke_module($type, 'hostname', $extra['id']);
        db_query("INSERT INTO {spam_duplicate} (sid, content_hash, hostname, timestamp) VALUES(%d, '%s', '%s', %d)", $extra['sid'], $hash, $hostname, time());
      }
      break;
  }
}