You are here

function _antispam_comment_form_submit in AntiSpam 6

Comment form submit callback; check for spam.

1 string reference to '_antispam_comment_form_submit'
antispam_form_alter in ./antispam.module
Implementation of hook_form_alter().

File

./antispam.module, line 980

Code

function _antispam_comment_form_submit($form, &$form_state, $original_submit_callback = NULL) {

  // Our default destination. It doesn't need to override the original.
  $goto = NULL;

  // We need to get the comment id ($cid) to load the comment.
  if (isset($form_state['values']['cid'])) {
    $cid = $form_state['values']['cid'];
  }
  else {
    $goto = $form_state['redirect'];
    if (is_array($goto) && isset($goto[2]) && preg_match('#^comment-([0-9]+)$#', $goto[2], $match)) {
      $cid = $match[1];
      $goto[2] = NULL;
    }
  }

  //---- #658990 ----

  // Quit asap, if current user has administration permission
  // or permission to post without spam checking.
  if (antispam_is_spam_moderator('comments') || user_access('post with no antispam checking')) {
    antispam_notify_moderators('comment', $comment, $comment->status == COMMENT_PUBLISHED ? TRUE : FALSE, FALSE);

    // Return NULL or the destination returned by the original #submit callback.
    return $goto;
  }

  // Once we have a $cid, we can (try to) load the comment with all relevant
  // information that we need to make the AntiSpam request to check for spam.
  if ($cid) {
    $comment = antispam_content_load('comment', $cid);

    // If we got a comment, send query to AntiSpam.
    if ($comment) {
      $api_result = antispam_api_cmd_comment_check('comment', $comment);
      if ($api_result[0] == ANTISPAM_API_RESULT_IS_HAM) {
        antispam_notify_moderators('comment', $comment, $comment->status == COMMENT_PUBLISHED ? TRUE : FALSE, FALSE);

        // Increment ham counter
        antispam_increase_counter(ANTISPAM_COUNT_HAM_DETECTED);
      }
      else {
        if ($api_result[0] == ANTISPAM_API_RESULT_IS_SPAM) {
          $comment->signature = $api_result[1];
          $comment->spaminess = $api_result[2];

          // Oops! We got spammed, let's mark the comment as such.
          antispam_content_spam_operation('comment', $comment, 'submit-spam', FALSE);
          antispam_increase_counter(ANTISPAM_COUNT_SPAM_DETECTED);
          antispam_notify_moderators('comment', $comment, FALSE, TRUE);
        }
        else {
          antispam_notify_moderators('comment', $comment, FALSE, FALSE);
        }

        // Unpublish the comment, if necessary.
        if ($comment->status == COMMENT_PUBLISHED) {
          antispam_content_publish_operation('comment', $comment, 'unpublish', FALSE);
        }

        // Since users won't see their replies published, show them a polite explanation on why.
        drupal_set_message(t('Your comment has been queued for moderation by site administrators and will be published after approval.'));

        // go back to the node page since the comment will not be published
        $form_state['redirect'] = $goto;

        // Record the event to watchdog.
        if ($api_result[0] == ANTISPAM_API_RESULT_ERROR) {
          watchdog('content', 'AntiSpam service seems to be down, comment queued for manual approval: %subject', array(
            '%subject' => $comment->subject,
          ), WATCHDOG_WARNING, l(t('view'), 'node/' . $comment->nid, array(
            'fragment' => 'comment-' . $comment->cid,
          )));
        }
        else {
          watchdog('content', 'Spam detected by AntiSpam in comment: %subject', array(
            '%subject' => $comment->subject,
          ), WATCHDOG_WARNING, l(t('view'), 'node/' . $comment->nid, array(
            'fragment' => 'comment-' . $comment->cid,
          )));

          // If requested to, generate a delay so the spammer has to wait for a while.
          if (($seconds = variable_get('antispam_antispambot_delay', 60)) > 0) {
            sleep($seconds);
          }
        }
      }
    }
  }

  // Return NULL or the destination returned by the original #submit callback.
  return $goto;
}