You are here

public function BootSubscriber::handle in Anti Spam by CleanTalk 8.3

Same name and namespace in other branches
  1. 8.4 src/EventSubscriber/BootSubscriber.php \Drupal\cleantalk\EventSubscriber\BootSubscriber::handle()
  2. 9.1.x src/EventSubscriber/BootSubscriber.php \Drupal\cleantalk\EventSubscriber\BootSubscriber::handle()

File

src/EventSubscriber/BootSubscriber.php, line 51

Class

BootSubscriber

Namespace

Drupal\cleantalk\EventSubscriber

Code

public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = TRUE) {
  if (strpos($request->server
    ->get('REQUEST_URI'), '/admin/') === false) {

    // If Cookies are enabled and headers not sent
    if (\Drupal::config('cleantalk.settings')
      ->get('cleantalk_set_cookies') && !headers_sent()) {

      // Cookie names to validate
      $cookie_test_value = array(
        'cookies_names' => array(),
        'check_value' => trim(\Drupal::config('cleantalk.settings')
          ->get('cleantalk_authkey')),
      );

      // Submit time
      $apbct_timestamp = time();

      // Fix for submit_time = 0
      if (\Drupal::config('cleantalk.settings')
        ->get('cleantalk_alternative_cookies_session')) {

        // by database
        $prev_time = CleantalkFuncs::apbct_getcookie('apbct_prev_timestamp');
        if (is_null($prev_time)) {
          CleantalkFuncs::apbct_setcookie('apbct_timestamp', $apbct_timestamp);
          CleantalkFuncs::apbct_setcookie('apbct_prev_timestamp', $apbct_timestamp);
          $cookie_test_value['check_value'] .= $apbct_timestamp;
        }
        else {
          CleantalkFuncs::apbct_setcookie('apbct_timestamp', $prev_time);
          CleantalkFuncs::apbct_setcookie('apbct_prev_timestamp', $apbct_timestamp);
          $cookie_test_value['check_value'] .= $prev_time;
        }
      }
      else {

        // by cookies
        CleantalkFuncs::apbct_setcookie('apbct_timestamp', $apbct_timestamp);
        $cookie_test_value['check_value'] .= $apbct_timestamp;
      }
      $cookie_test_value['cookies_names'][] = 'apbct_timestamp';

      //Previous referer
      if (!empty($_SERVER['HTTP_REFERER'])) {
        CleantalkFuncs::apbct_setcookie('apbct_prev_referer', $_SERVER['HTTP_REFERER']);
        $cookie_test_value['cookies_names'][] = 'apbct_prev_referer';
        $cookie_test_value['check_value'] .= $_SERVER['HTTP_REFERER'];
      }

      // Cookies test
      $cookie_test_value['check_value'] = md5($cookie_test_value['check_value']);
      CleantalkFuncs::apbct_setcookie('apbct_cookies_test', json_encode($cookie_test_value));
    }

    // ENDIF: If Cookies are enabled and headers not sent
    // Remote calls
    if (isset($_GET['spbc_remote_call_token'], $_GET['spbc_remote_call_action'], $_GET['plugin_name']) && in_array($_GET['plugin_name'], array(
      'antispam',
      'anti-spam',
      'apbct',
    ))) {
      CleantalkFuncs::_cleantalk_apbct_remote_call__perform();
    }
    if (\Drupal::config('cleantalk.settings')
      ->get('cleantalk_sfw')) {
      $ct_key = trim(\Drupal::config('cleantalk.settings')
        ->get('cleantalk_authkey'));
      if ($ct_key != '') {
        $sfw = new CleantalkSFW($ct_key);
        $sfw
          ->check_ip();
        if (time() - \Drupal::state()
          ->get('cleantalk_sfw_last_send_log') > 3600) {
          CleantalkFuncs::_cleantalk_sfw_send_logs($ct_key);
        }
        if (time() - \Drupal::state()
          ->get('cleantalk_sfw_last_check') > 86400) {
          CleantalkFuncs::_cleantalk_sfw_update($ct_key);
        }
      }
    }

    //Custom Contact forms
    if (sizeof($_POST) > 0 && !$request
      ->get('form_build_id') && !$request
      ->get('form_id') && \Drupal::config('cleantalk.settings')
      ->get('cleantalk_check_ccf')) {
      $ct_temp_msg_data = CleantalkHelper::get_fields_any($request);
      $spam_check = array();
      $spam_check['type'] = 'custom_contact_form';
      $spam_check['sender_email'] = $ct_temp_msg_data['email'] ? $ct_temp_msg_data['email'] : '';
      $spam_check['sender_nickname'] = $ct_temp_msg_data['nickname'] ? $ct_temp_msg_data['nickname'] : '';
      $spam_check['message_title'] = $ct_temp_msg_data['subject'] ? $ct_temp_msg_data['subject'] : '';
      $spam_check['message_body'] = $ct_temp_msg_data['message'] ? implode("\n", $ct_temp_msg_data['message']) : '';
      if ($spam_check['sender_email'] != '' || $spam_check['message_title'] != '' || $spam_check['message_body'] != '') {
        $result = CleantalkFuncs::_cleantalk_check_spam($spam_check);
        if (isset($result) && is_array($result) && $result['errno'] == 0 && $result['allow'] != 1) {
          $this->messenger
            ->addError(HTML::escape($result['ct_result_comment']));
        }
      }
    }

    // Search
    if ($request->server
      ->get('REQUEST_URI') && strpos($request->server
      ->get('REQUEST_URI'), 'search') !== false && $request
      ->get('keys')) {
      if (\Drupal::config('cleantalk.settings')
        ->get('cleantalk_check_search_form')) {
        global $user;
        $get_query = $request
          ->get('keys');
        $spam_check['type'] = 'site_search_drupal8';
        $spam_check['sender_email'] = !empty($user->mail) ? $user->mail : '';
        $spam_check['sender_nickname'] = !empty($user->name) ? $user->name : '';
        $spam_check['message_title'] = '';
        $spam_check['message_body'] = $get_query;
        if ($spam_check['sender_email'] != '' || $spam_check['message_body']) {
          $spam_result = CleantalkFuncs::_cleantalk_check_spam($spam_check);
          if (isset($spam_result) && is_array($spam_result) && $spam_result['errno'] == 0 && $spam_result['allow'] != 1) {
            CleantalkFuncs::_cleantalk_die($spam_result['ct_result_comment']);
          }
        }
      }
    }
  }
  return $this->httpKernel
    ->handle($request, $type, $catch);
}