BootSubscriber.php in Anti Spam by CleanTalk 8.3
File
src/EventSubscriber/BootSubscriber.php
View source
<?php
namespace Drupal\cleantalk\EventSubscriber;
use Drupal\Core\Messenger\MessengerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Drupal\cleantalk\lib\Cleantalk\ApbctDrupal\SFW as CleantalkSFW;
use Drupal\cleantalk\lib\Cleantalk\Common\Helper as CleantalkHelper;
use Drupal\cleantalk\CleantalkFuncs;
use Drupal\Component\Utility\Html;
class BootSubscriber implements HttpKernelInterface {
protected $httpKernel;
protected $messenger;
public function __construct(HttpKernelInterface $kernel, MessengerInterface $messenger = null) {
$this->httpKernel = $kernel;
$this->messenger = $messenger;
}
public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = TRUE) {
if (strpos($request->server
->get('REQUEST_URI'), '/admin/') === false) {
if (\Drupal::config('cleantalk.settings')
->get('cleantalk_set_cookies') && !headers_sent()) {
$cookie_test_value = array(
'cookies_names' => array(),
'check_value' => trim(\Drupal::config('cleantalk.settings')
->get('cleantalk_authkey')),
);
$apbct_timestamp = time();
if (\Drupal::config('cleantalk.settings')
->get('cleantalk_alternative_cookies_session')) {
$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 {
CleantalkFuncs::apbct_setcookie('apbct_timestamp', $apbct_timestamp);
$cookie_test_value['check_value'] .= $apbct_timestamp;
}
$cookie_test_value['cookies_names'][] = 'apbct_timestamp';
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'];
}
$cookie_test_value['check_value'] = md5($cookie_test_value['check_value']);
CleantalkFuncs::apbct_setcookie('apbct_cookies_test', json_encode($cookie_test_value));
}
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);
}
}
}
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']));
}
}
}
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);
}
}