You are here

function _cleantalk_check_spam in Anti Spam by CleanTalk 8

Same name and namespace in other branches
  1. 7 cleantalk.module \_cleantalk_check_spam()
  2. 7.2 cleantalk.module \_cleantalk_check_spam()

Cleantalk inner function - performs antispam checking.

9 calls to _cleantalk_check_spam()
BootSubscriber::onEvent in src/EventSubscriber/BootSubscriber.php
cleantalk_boot in ./cleantalk.module
Implements hook_boot()
cleantalk_install in ./cleantalk.module
cleantalk_uc_order in ./cleantalk.module
cleantalk_user_presave in ./cleantalk.module
Implements hook_user_presave().

... See full list

File

./cleantalk.module, line 758
Main CleanTalk integration module functions.

Code

function _cleantalk_check_spam($spam_check) {
  global $cleantalk_executed;
  if ($cleantalk_executed) {
    return;
  }
  $ct_authkey = \Drupal::config('cleantalk.settings')
    ->get('cleantalk_authkey');
  $ct_ws = _cleantalk_get_ws();
  $checkjs = 0;
  if (!isset($_COOKIE['ct_checkjs'])) {
    $checkjs = NULL;
  }
  elseif ($_COOKIE['ct_checkjs'] == _cleantalk_get_checkjs_value()) {
    $checkjs = 1;
  }
  else {
    $checkjs = 0;
  }
  $user_agent = $_SERVER['HTTP_USER_AGENT'];
  $refferrer = $_SERVER['HTTP_REFERER'];
  $ct = new Drupal\cleantalk\Cleantalk();
  $ct->work_url = $ct_ws['work_url'];
  $ct->server_url = $ct_ws['server_url'];
  $ct->server_ttl = $ct_ws['server_ttl'];
  $ct->server_changed = $ct_ws['server_changed'];
  $ct_options = array(
    'access_key' => $ct_authkey,
    'cleantalk_automod' => \Drupal::config('cleantalk.settings')
      ->get('cleantalk_automod'),
  );
  $sender_info = \Drupal\Component\Serialization\Json::encode(array(
    'cms_lang' => 'en',
    'REFFERRER' => $refferrer,
    'post_url' => $refferrer,
    'USER_AGENT' => $user_agent,
    'ct_options' => \Drupal\Component\Serialization\Json::encode($ct_options),
  ));
  $ct_request = new Drupal\cleantalk\CleantalkRequest();
  $ct_request->auth_key = $ct_authkey;
  $ct_request->agent = CLEANTALK_USER_AGENT;
  $ct_request->response_lang = 'en';
  $ct_request->js_on = $checkjs;
  $ct_request->sender_info = $sender_info;
  $ct_request->sender_email = $spam_check['sender_email'];
  $ct_request->sender_nickname = $spam_check['sender_nickname'];
  $ct_request->sender_ip = Drupal::request()
    ->getClientIp();
  $ct_submit_time = NULL;

  //drupal_session_start();
  switch ($spam_check['type']) {
    case 'comment':
    case 'contact':
      if (isset($_SESSION['ct_submit_comment_time'])) {
        $ct_submit_time = REQUEST_TIME - $_SESSION['ct_submit_comment_time'];
      }
      $timelabels_key = 'mail_error_comment';
      $ct_request->submit_time = $ct_submit_time;
      $ct_request->message = $spam_check['message_title'] . " \n\n" . $spam_check['message_body'];

      // Additional info.
      $post_info = '';
      $a_post_info['comment_type'] = 'comment';

      // JSON format.
      $post_info = \Drupal\Component\Serialization\Json::encode($a_post_info);

      // Plain text format.
      if ($post_info === FALSE) {
        $post_info = '';
      }
      $ct_request->post_info = $post_info;

      // Example is obsolete now.
      $ct_request->example = '';
      $ct_result = $ct
        ->isAllowMessage($ct_request);
      break;
    case 'register':
      if (isset($_SESSION['ct_submit_register_time'])) {
        $ct_submit_time = REQUEST_TIME - $_SESSION['ct_submit_register_time'];
      }
      $timelabels_key = 'mail_error_reg';
      $ct_request->submit_time = $ct_submit_time;
      $ct_request->tz = $spam_check['timezone'];
      $ct_result = $ct
        ->isAllowUser($ct_request);
      break;
  }
  $cleantalk_executed = true;
  $ret_val = array();
  $ret_val['ct_request_id'] = $ct_result->id;
  if ($ct->server_change) {
    _cleantalk_set_ws($ct->work_url, $ct->server_url, $ct->server_ttl, REQUEST_TIME);
  }

  // First check errstr flag.
  if (!empty($ct_result->errstr) || !empty($ct_result->inactive) && $ct_result->inactive == 1) {

    // Cleantalk error so we go default way (no action at all).
    $ret_val['errno'] = 1;
    if ($checkjs == 0) {
      $ret_val['allow'] = 0;
    }

    // Just inform admin.
    $err_title = $_SERVER['SERVER_NAME'] . ' - CleanTalk hook error';
    if (!empty($ct_result->errstr)) {
      $ret_val['errstr'] = _cleantalk_filter_response($ct_result->errstr);
    }
    else {
      $ret_val['errstr'] = _cleantalk_filter_response($ct_result->comment);
    }
    $send_flag = FALSE;
    $result = db_select('cleantalk_timelabels', 'c')
      ->fields('c', array(
      'ct_value',
    ))
      ->condition('ct_key', $timelabels_key, '=')
      ->execute();
    $results = $result
      ->fetchCol(0);
    if (count($results) == 0) {
      $send_flag = TRUE;
    }
    elseif (REQUEST_TIME - 900 > $result
      ->fetchObject()->ct_value) {

      // 15 minutes.
      $send_flag = TRUE;
    }
    if ($send_flag) {
      db_merge('cleantalk_timelabels')
        ->key(array(
        'ct_key' => $timelabels_key,
      ))
        ->fields(array(
        'ct_value' => REQUEST_TIME,
      ))
        ->execute();

      // @FIXME
      // // @FIXME
      // // This looks like another module's variable. You'll need to rewrite this call
      // // to ensure that it uses the correct configuration object.
      // $to = variable_get('site_mail', ini_get('sendmail_from'));
      if (!empty($to)) {
        drupal_mail("cleantalk", $timelabels_key, $to, language_default(), array(
          'subject' => $err_title,
          'body' => $ret_val['errstr'],
          'headers' => array(),
        ), $to, TRUE);
      }
    }
    return $ret_val;
  }
  $ret_val['errno'] = 0;
  if ($ct_result->allow == 1) {

    // Not spammer.
    $ret_val['allow'] = 1;

    // Store request_id in globals to store it in DB later.
    _cleantalk_ct_result('set', $ct_result->id);

    // Don't store 'ct_result_comment', means good comment.
  }
  else {

    // Spammer.
    $ret_val['allow'] = 0;
    $ret_val['ct_result_comment'] = _cleantalk_filter_response($ct_result->comment);

    // Check stop_queue flag.
    if ($spam_check['type'] == 'comment' && $ct_result->stop_queue == 0) {

      // Spammer and stop_queue == 0 - to manual approvement.
      $ret_val['stop_queue'] = 0;

      // Store request_id and comment in static to store them in DB later.
      // Store 'ct_result_comment' - means bad comment.
      _cleantalk_ct_result('set', $ct_result->id, $ret_val['ct_result_comment']);
    }
    else {

      // New user or Spammer and stop_queue == 1 - display form error message.
      $ret_val['stop_queue'] = 1;
    }
  }
  return $ret_val;
}