You are here

public static function CleantalkFuncs::_cleantalk_check_spam in Anti Spam by CleanTalk 7.5

Same name and namespace in other branches
  1. 7.4 src/CleantalkFuncs.php \CleantalkFuncs::_cleantalk_check_spam()

Cleantalk inner function - performs antispam checking.

8 calls to CleantalkFuncs::_cleantalk_check_spam()
cleantalk_init in ./cleantalk.module
Implements hook_init()
cleantalk_uc_order in ./cleantalk.module
cleantalk_validate_comment in ./cleantalk.module
Cleantalk comment validation function.
cleantalk_validate_contact_message in ./cleantalk.module
Cleantalk contact message validation function.
cleantalk_validate_forum in ./cleantalk.module
Cleantalk forum validation function.

... See full list

File

src/CleantalkFuncs.php, line 460

Class

CleantalkFuncs
Cleantalk class create request

Code

public static function _cleantalk_check_spam($spam_check, $form_errors = null) {
  global $user, $cleantalk_executed, $language;
  if (empty($spam_check) || !isset($spam_check['type'])) {
    return;
  }
  if ($cleantalk_executed) {
    return;
  }
  if (user_access('administer modules') && path_is_admin(current_path())) {
    return;
  }
  $roles = variable_get('cleantalk_roles_exclusions');
  if ($roles) {
    $set_check = true;
    foreach ($roles as $role_id) {
      if (self::_cleantalk_user_has_role_id($role_id)) {
        $set_check = false;
      }
    }
    if (!$set_check) {
      return;
    }
  }

  // Don't check reged user with >= 'cleantalk_check_comments_min_approved' approved msgs.
  if ($user->uid > 0 && module_exists('comment')) {
    $result = db_query('SELECT count(*) AS count FROM {comment} WHERE uid=:uid AND status=1', array(
      ':uid' => $user->uid,
    ));
    $count = intval($result
      ->fetchObject()->count);
    $ct_comments = variable_get('cleantalk_check_comments_min_approved', 3);
    if ($count >= $ct_comments) {
      return;
    }
  }
  $url_check = true;
  if (variable_get('cleantalk_url_exclusions', '')) {
    $url_exclusion = explode(',', variable_get('cleantalk_url_exclusions', ''));
    if ($url_exclusion && is_array($url_exclusion) && count($url_exclusion) > 0) {
      $check_type = variable_get('cleantalk_url_exclusions_regexp', 0);
      foreach ($url_exclusion as $key => $value) {
        if ($check_type == 1) {

          // If RegExp
          if (@preg_match('/' . $value . '/', $_SERVER['REQUEST_URI'])) {
            $url_check = false;
          }
        }
        else {
          if ($_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'] === $value) {

            // Simple string checking
            $url_check = false;
          }
        }
        if (strpos($value, 'node') !== false && strpos($_SERVER['REQUEST_URI'], 'q=comment/reply/') !== false) {
          $get_node = array_values(array_slice(explode('/', $value), -1))[0];
          $current_reply_id = array_values(array_slice(explode('/', $_SERVER['REQUEST_URI']), -1))[0];
          if ($get_node == $current_reply_id) {
            $url_check = false;
          }
        }
        if (strpos($_SERVER['REQUEST_URI'], $value) !== false) {
          $url_check = false;
        }
      }
    }
  }
  if (!$url_check) {
    return;
  }
  $ct_authkey = variable_get('cleantalk_authkey', '');
  $ct_ws = self::_cleantalk_get_ws();
  $ct = new 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_check_comments' => variable_get('cleantalk_check_comments', ''),
    'cleantalk_check_comments_automod' => variable_get('cleantalk_check_comments_automod', ''),
    'cleantalk_check_comments_min_approved' => variable_get('cleantalk_check_comments_min_approved', 3),
    'cleantalk_check_register' => variable_get('cleantalk_check_register', ''),
    'cleantalk_check_webforms' => variable_get('cleantalk_check_webforms', ''),
    'cleantalk_check_contact_forms' => variable_get('cleantalk_check_contact_forms', ''),
    'cleantalk_check_forum_topics' => variable_get('cleantalk_check_forum_topics', ''),
    'cleantalk_check_ccf' => variable_get('cleantalk_check_ccf', ''),
    'cleantalk_check_search_form' => variable_get('cleantalk_check_search_form', 1),
    'cleantalk_add_search_noindex' => variable_get('cleantalk_add_search_noindex', 0),
    'cleantalk_url_exclusions' => variable_get('cleantalk_url_exclusions', ''),
    'cleantalk_url_exclusions_regexp' => variable_get('cleantalk_url_exclusions_regexp', 0),
    'cleantalk_fields_exclusions' => variable_get('cleantalk_fields_exclusions', ''),
    'cleantalk_roles_exclusions' => variable_get('cleantalk_roles_exclusions') ? implode(',', variable_get('cleantalk_roles_exclusions')) : '',
    'cleantalk_set_cookies' => variable_get('cleantalk_set_cookies', 1),
    'cleantalk_alternative_cookies_session' => variable_get('cleantalk_alternative_cookies_session', 0),
    'cleantalk_sfw' => variable_get('cleantalk_sfw', ''),
    'cleantalk_ssl' => variable_get('cleantalk_ssl', ''),
    'cleantalk_link' => variable_get('cleantalk_link', ''),
  );
  $ct_request = new CleantalkRequest();
  $ct_request->auth_key = $ct_authkey;
  $ct_request->agent = CLEANTALK_USER_AGENT;
  $ct_request->response_lang = $language->language;
  $ct_request->js_on = isset($_COOKIE['ct_check_js']) && $_COOKIE['ct_check_js'] == self::_cleantalk_get_checkjs_value() ? 1 : 0;
  $ct_request->sender_info = drupal_json_encode(array(
    'cms_lang' => $language->language,
    'REFFERRER' => isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : null,
    'page_url' => isset($_SERVER['SERVER_NAME'], $_SERVER['REQUEST_URI']) ? htmlspecialchars($_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']) : null,
    'USER_AGENT' => isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : null,
    'ct_options' => drupal_json_encode($ct_options),
    'js_timezone' => isset($_COOKIE['ct_timezone']) ? $_COOKIE['ct_timezone'] : '',
    'mouse_cursor_positions' => isset($_COOKIE['ct_pointer_data']) ? json_decode($_COOKIE['ct_pointer_data']) : '',
    'key_press_timestamp' => isset($_COOKIE['ct_fkp_timestamp']) ? $_COOKIE['ct_fkp_timestamp'] : '',
    'page_set_timestamp' => isset($_COOKIE['ct_ps_timestamp']) ? $_COOKIE['ct_ps_timestamp'] : 0,
    'REFFERRER_PREVIOUS' => self::_apbct_getcookie('apbct_prev_referer'),
    'cookies_enabled' => self::_cleantalk_apbct_cookies_test(),
    'form_validation' => $form_errors && is_array($form_errors) ? json_encode(array(
      'validation_notice' => json_encode($form_errors),
      'page_url' => $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'],
    )) : null,
  ));
  $ct_request->post_info = drupal_json_encode(array(
    'comment_type' => $spam_check['type'],
    'post_url' => isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : null,
  ));
  $ct_request->sender_email = isset($spam_check['sender_email']) ? $spam_check['sender_email'] : '';
  $ct_request->sender_nickname = isset($spam_check['sender_nickname']) ? $spam_check['sender_nickname'] : '';
  $ct_request->sender_user_role = implode(',', $user->roles);
  $ct_request->sender_ip = CleantalkHelper::ip_get(array(
    'real',
  ), false);
  $ct_request->x_forwarded_for = CleantalkHelper::ip_get(array(
    'x_forwarded_for',
  ), false);
  $ct_request->x_real_ip = CleantalkHelper::ip_get(array(
    'x_real_ip',
  ), false);
  $ct_request->submit_time = self::_cleantalk_get_submit_time();
  if ($spam_check['type'] == 'custom_contact_form' && isset($_SERVER['HTTP_REFERER']) && htmlspecialchars($_SERVER['HTTP_REFERER']) === 'https://www.google.com/') {
    $spam_check['type'] = 'site_search_drupal7';
  }
  switch ($spam_check['type']) {
    case 'comment':
    case 'contact':
    case 'webform':
    case 'custom_contact_form':
    case 'site_search_drupal7':
      $timelabels_key = 'mail_error_comment';
      if (isset($spam_check['message_title']) && is_array($spam_check['message_title'])) {
        $spam_check['message_title'] = implode("\n\n", $spam_check['message_title']);
      }
      if (isset($spam_check['message_body']) && is_array($spam_check['message_body'])) {
        $spam_check['message_body'] = implode("\n\n", $spam_check['message_body']);
      }
      $ct_request->message = $spam_check['message_title'] . " \n\n" . preg_replace('/\\s+/', ' ', str_replace("<br />", " ", $spam_check['message_body']));
      $ct_result = $ct
        ->isAllowMessage($ct_request);
      break;
    case 'register':
      $timelabels_key = 'mail_error_reg';
      $ct_request->tz = !empty($spam_check['timezone']) ? $spam_check['timezone'] : '';

      // Set JS test enabled if REST API request
      if (arg(0) != 'user') {
        $ct_request->js_on = 1;
      }
      $ct_result = $ct
        ->isAllowUser($ct_request);
      break;
  }
  $cleantalk_executed = true;
  $ret_val = array();
  if ($ct_result) {
    $ret_val['ct_request_id'] = $ct_result->id;
    if ($ct->server_change) {
      self::_cleantalk_set_ws($ct->work_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 ($ct_request->js_on == 0) {
        $ret_val['allow'] = 0;
      }

      // Just inform admin.
      $err_title = $_SERVER['SERVER_NAME'] . ' - CleanTalk hook error';
      if (!empty($ct_result->errstr)) {
        $ret_val['errstr'] = self::_cleantalk_filter_response($ct_result->errstr);
      }
      else {
        $ret_val['errstr'] = self::_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();
      if ($result
        ->rowCount() == 0) {
        $send_flag = TRUE;
      }
      elseif (REQUEST_TIME - 900 > $result
        ->fetchObject()->ct_value) {
        $send_flag = TRUE;
      }
      if ($send_flag) {
        db_merge('cleantalk_timelabels')
          ->key(array(
          'ct_key' => $timelabels_key,
        ))
          ->fields(array(
          'ct_value' => REQUEST_TIME,
        ))
          ->execute();
        $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.
      self::_cleantalk_ct_result('set', $ct_result->id, $ret_val['allow']);

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

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

      // Check stop_queue flag.
      if ($spam_check['type'] == 'comment') {

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