You are here

cleantalk.module in Anti Spam by CleanTalk 8.4

File

cleantalk.module
View source
<?php

use Drupal\Core\Language\Language;
use Drupal\cleantalk\CleantalkFuncs;

// Autoload
require_once dirname(__FILE__) . '/src/lib/autoload.php';

//Common classes
use Cleantalk\ApbctDrupal\Helper as CleantalkHelper;
$cleantalk_executed = false;

/**
 * Implements hook_page_attachments_alter()
 */
function cleantalk_page_attachments_alter(array &$page) {
  if (\Drupal::request()->query
    ->get('search')) {
    if (\Drupal::config('cleantalk.settings')
      ->get('cleantalk_search_noindex')) {
      $skip_index_results = [
        '#tag' => 'meta',
        '#attributes' => [
          'name' => 'robots',
          'content' => 'noindex',
        ],
      ];
      $page['#attached']['html_head'][] = [
        $skip_index_results,
        'skip_index_results',
      ];
    }
  }
  if (!\Drupal::request()->query
    ->has('amp') && !\Drupal::service('router.admin_context')
    ->isAdminRoute()) {
    $js_template = "var ct_check_js_val = '%s';";
    $js_addon_body = sprintf($js_template, CleantalkFuncs::_cleantalk_get_checkjs_value());

    # set AntiCrawler cookie
    $drupal_ac_antibot_cookie_value = "var drupal_ac_antibot_cookie_value = '%s';";
    $drupal_ac_antibot_js_body = sprintf($drupal_ac_antibot_cookie_value, CleantalkFuncs::create_ac_cookie_value());

    # used alt cookies
    $use_alt_cookies = \Drupal::config('cleantalk.settings')
      ->get('cleantalk_alternative_cookies_session') ? 1 : 0;
    $use_alt_cookies_marker = "var ct_use_alt_cookies = " . $use_alt_cookies . ";";
    $page['#attached']['html_head'][] = array(
      array(
        '#tag' => 'script',
        '#value' => $js_addon_body . $drupal_ac_antibot_js_body . $use_alt_cookies_marker,
      ),
      'ga_scripts',
    );
    $page['#attached']['library'][] = 'cleantalk/apbct-public';
  }

  // If external checking is enabled, add the external JS library to the page.
  if (\Drupal::config('cleantalk.settings')
    ->get('cleantalk_check_external')) {
    $page['#attached']['library'][] = 'cleantalk/apbct-external';
  }
}

/**
 * Implements hook_form_BASE_FORM_ID_alter()
 */
function cleantalk_form_alter(&$form, &$form_state, $form_id) {
  _cleantalk_form_alter($form, $form_state, $form_id);
}

/**
 * Cleantalk inner function - registration validation function.
 */
function cleantalk_validate_register(&$form, &$form_state) {
  if (\Drupal::config('cleantalk.settings')
    ->get('cleantalk_check_register') && CleantalkFuncs::_cleantalk_check_form_submit_handlers($form_state
    ->getSubmitHandlers())) {
    $spam_check = array();
    $spam_check['type'] = 'register';
    $spam_check['sender_email'] = !empty($form_state
      ->getValue('mail')) ? $form_state
      ->getValue('mail') : '';
    $spam_check['sender_nickname'] = !empty($form_state
      ->getValue('name')) ? $form_state
      ->getValue('name') : '';
    $spam_check['timezone'] = !empty($form_state
      ->getValue('timezone')) ? $form_state
      ->getValue('timezone') : '';
    $spam_result = CleantalkFuncs::_cleantalk_check_spam($spam_check, $form_state
      ->getErrors());
    if (isset($spam_result) && is_array($spam_result) && $spam_result['errno'] == 0 && $spam_result['allow'] != 1) {

      // Value of ct_result_comment is sanitized already (before storing).
      $form_state
        ->setErrorByName('mail', strip_tags($spam_result['ct_result_comment'], '<p><a>'));
    }
  }
}

/**
 * Cleantalk inner function - comment validation function.
 */
function cleantalk_validate_comment(&$form, &$form_state) {
  if (\Drupal::config('cleantalk.settings')
    ->get('cleantalk_check_comments') && CleantalkFuncs::_cleantalk_check_form_submit_handlers($form_state
    ->getSubmitHandlers())) {
    $current_user = \Drupal::currentUser();
    $ct_temp_msg_data = CleantalkHelper::get_fields_any(\Drupal::request()->request
      ->all(), \Drupal::config('cleantalk.settings')
      ->get('cleantalk_fields_exclusions'));
    $comment_lang = !empty($form_state
      ->getValue('language')) ? $form_state
      ->getValue('language') : Language::LANGCODE_NOT_SPECIFIED;
    $spam_check = array();
    $spam_check['type'] = 'comment';
    if ($current_user
      ->id()) {
      $user = \Drupal\user\Entity\User::load($current_user
        ->id());
      $spam_check['sender_nickname'] = !empty($user
        ->get('name')) ? $user
        ->get('name')->value : '';
      $spam_check['sender_email'] = !empty($user
        ->get('mail')->value) ? $user
        ->get('mail')->value : '';
    }
    else {
      if (empty($form_state
        ->getValue('name'))) {
        $spam_check['sender_nickname'] = $ct_temp_msg_data['nickname'] ? $ct_temp_msg_data['nickname'] : '';
      }
      else {
        $spam_check['sender_nickname'] = $form_state
          ->getValue('name');
      }
      if (empty($form_state
        ->getValue('mail'))) {
        $spam_check['sender_email'] = $ct_temp_msg_data['email'] ? $ct_temp_msg_data['email'] : '';
      }
      else {
        $spam_check['sender_email'] = $form_state
          ->getValue('mail');
      }
    }
    if (empty($form_state
      ->getValue('subject')[0]['value'])) {
      $spam_check['message_title'] = $ct_temp_msg_data['subject'] ? $ct_temp_msg_data['subject'] : '';
    }
    else {
      $spam_check['message_title'] = $form_state
        ->getValue('subject')[0]['value'];
    }
    if (empty($form_state
      ->getValue('comment_body')[0]['value'])) {
      $spam_check['message_body'] = $ct_temp_msg_data['message'] ? $ct_temp_msg_data['message'] : '';
    }
    else {
      $spam_check['message_body'] = $form_state
        ->getValue('comment_body')[0]['value'];
    }
    $spam_result = CleantalkFuncs::_cleantalk_check_spam($spam_check, $form_state
      ->getErrors());
    if (isset($spam_result) && is_array($spam_result) && $spam_result['errno'] == 0 && $spam_result['allow'] != 1) {

      // Value of ct_result_comment is sanitized already (before storing).
      if (!\Drupal::config('cleantalk.settings')
        ->get('cleantalk_check_comments_automod') || $spam_result['stop_queue'] == 1) {
        $form_state
          ->setErrorByName('comment_body', strip_tags($spam_result['ct_result_comment'], '<p><a>'));
      }
    }
  }
}

/**
 * Cleantalk inner function - contact message validation function.
 */
function cleantalk_validate_contact_message(&$form, &$form_state) {
  if (\Drupal::config('cleantalk.settings')
    ->get('cleantalk_check_contact_forms') && CleantalkFuncs::_cleantalk_check_form_submit_handlers($form_state
    ->getSubmitHandlers())) {
    $current_user = \Drupal::currentUser();
    $ct_temp_msg_data = CleantalkHelper::get_fields_any(\Drupal::request()->request
      ->all(), \Drupal::config('cleantalk.settings')
      ->get('cleantalk_fields_exclusions'));
    $spam_check = array();
    $spam_check['type'] = 'contact';
    if ($current_user
      ->id()) {
      $user = \Drupal\user\Entity\User::load($current_user
        ->id());
      $spam_check['sender_nickname'] = !empty($user
        ->get('name')) ? $user
        ->get('name')->value : '';
    }
    else {
      $spam_check['sender_nickname'] = $ct_temp_msg_data['nickname'] ? $ct_temp_msg_data['nickname'] : '';
    }
    $spam_check['sender_email'] = $ct_temp_msg_data['email'] ? $ct_temp_msg_data['email'] : '';
    $spam_check['message_title'] = $ct_temp_msg_data['subject'] ? $ct_temp_msg_data['subject'] : '';
    $spam_check['message_body'] = $ct_temp_msg_data['message'] ? $ct_temp_msg_data['message'] : '';
    $spam_result = CleantalkFuncs::_cleantalk_check_spam($spam_check, $form_state
      ->getErrors());
    if (isset($spam_result) && is_array($spam_result) && $spam_result['errno'] == 0 && $spam_result['allow'] != 1) {

      // Value of ct_result_comment is sanitized already (before storing).
      $form_state
        ->setErrorByName('message', strip_tags($spam_result['ct_result_comment'], '<p><a>'));
    }
  }
}

/**
 * Cleantalk inner function - forum topic validation function.
 */
function cleantalk_validate_forum_topic(&$form, &$form_state) {
  if (\Drupal::config('cleantalk.settings')
    ->get('cleantalk_check_forum_topics') && CleantalkFuncs::_cleantalk_check_form_submit_handlers($form_state
    ->getSubmitHandlers())) {
    $current_user = \Drupal::currentUser();
    $ct_temp_msg_data = CleantalkHelper::get_fields_any(\Drupal::request()->request
      ->all(), \Drupal::config('cleantalk.settings')
      ->get('cleantalk_fields_exclusions'));
    $spam_check = array();
    $spam_check['type'] = 'forum_topic';
    if ($current_user
      ->id()) {
      $user = \Drupal\user\Entity\User::load($current_user
        ->id());
      $spam_check['sender_nickname'] = !empty($user
        ->get('name')) ? $user
        ->get('name')->value : '';
    }
    else {
      $spam_check['sender_nickname'] = $ct_temp_msg_data['nickname'] ? $ct_temp_msg_data['nickname'] : '';
    }
    $spam_check['sender_email'] = $ct_temp_msg_data['email'] ? $ct_temp_msg_data['email'] : '';
    $spam_check['message_title'] = $ct_temp_msg_data['subject'] ? $ct_temp_msg_data['subject'] : '';
    $spam_check['message_body'] = $ct_temp_msg_data['message'] ? $ct_temp_msg_data['message'] : '';
    $spam_result = CleantalkFuncs::_cleantalk_check_spam($spam_check, $form_state
      ->getErrors());
    if (isset($spam_result) && is_array($spam_result) && $spam_result['errno'] == 0 && $spam_result['allow'] != 1) {

      // Value of ct_result_comment is sanitized already (before storing).
      $form_state
        ->setErrorByName('message', strip_tags($spam_result['ct_result_comment'], '<p><a>'));
    }
  }
}

/**
 * Cleantalk inner function - webform validation function.
 */
function cleantalk_validate_webform(&$form, &$form_state) {
  if (\Drupal::config('cleantalk.settings')
    ->get('cleantalk_check_webforms')) {
    if (in_array('::submit', $form_state
      ->getSubmitHandlers()) || CleantalkFuncs::_cleantalk_check_form_submit_handlers($form_state
      ->getSubmitHandlers())) {
      $current_user = \Drupal::currentUser();
      $spam_check = array();
      $spam_check['type'] = 'webform';
      if (!empty($form_state
        ->get('multistep'))) {
        $ct_temp_msg_data = CleantalkHelper::get_fields_any($form_state
          ->get('multistep'), \Drupal::config('cleantalk.settings')
          ->get('cleantalk_fields_exclusions'));
        $spam_check['multistep_submit_time'] = $form_state
          ->get('multistep_submit_time');
      }
      else {
        $ct_temp_msg_data = CleantalkHelper::get_fields_any(\Drupal::request()->request
          ->all(), \Drupal::config('cleantalk.settings')
          ->get('cleantalk_fields_exclusions'));
      }
      if ($current_user
        ->id()) {
        $user = \Drupal\user\Entity\User::load($current_user
          ->id());
        $spam_check['sender_nickname'] = !empty($user
          ->get('name')) ? $user
          ->get('name')->value : '';
      }
      else {
        $spam_check['sender_nickname'] = $ct_temp_msg_data['nickname'] ? $ct_temp_msg_data['nickname'] : '';
      }
      $spam_check['sender_email'] = $ct_temp_msg_data['email'] ? $ct_temp_msg_data['email'] : '';
      $spam_check['message_title'] = $ct_temp_msg_data['subject'] ? $ct_temp_msg_data['subject'] : '';
      $spam_check['message_body'] = $ct_temp_msg_data['message'] ? $ct_temp_msg_data['message'] : '';
      $spam_result = CleantalkFuncs::_cleantalk_check_spam($spam_check, $form_state
        ->getErrors());
      if (isset($spam_result) && is_array($spam_result) && $spam_result['errno'] == 0 && $spam_result['allow'] != 1) {

        // Value of ct_result_comment is sanitized already (before storing).
        $form_state
          ->setErrorByName('message', strip_tags($spam_result['ct_result_comment'], '<p><a>'));
      }
    }
    else {
      if (empty($form_state
        ->get('multistep'))) {
        $form_state
          ->set('multistep', \Drupal::request()->request
          ->all());
        $form_state
          ->set('multistep_submit_time', CleantalkFuncs::apbct_getcookie('apbct_timestamp'));
      }
      else {
        $previous_storage = $form_state
          ->get('multistep');
        $form_state
          ->set('multistep', array_merge($previous_storage, \Drupal::request()->request
          ->all()));
      }
    }
  }
}

/**
 * Implements hook_comment_presave().
 */
function cleantalk_comment_presave(\Drupal\comment\CommentInterface $comment) {
  $ct_result = CleantalkFuncs::_cleantalk_ct_result();
  if (!empty($ct_result['ct_request_id'])) {
    if ($ct_result['ct_result_allow'] === 0 && \Drupal::config('cleantalk.settings')
      ->get('cleantalk_check_comments_automod')) {
      if (version_compare(\Drupal::VERSION, '8.3.0') >= 0) {
        $comment
          ->setUnpublished();
      }
      else {
        $comment
          ->setPublished(false);
      }
    }
  }
}

/**
 * Cleantalk inner function - alters needed form.
 */
function _cleantalk_form_alter(&$form, &$form_state, $form_id) {
  $url_exclusion = explode(",", \Drupal::config('cleantalk.settings')
    ->get('cleantalk_url_exclusions'));
  if (is_array($url_exclusion) && count($url_exclusion)) {
    $check_type = \Drupal::config('cleantalk.settings')
      ->get('cleantalk_url_regexp');
    foreach ($url_exclusion as $key => $value) {
      if (!empty($value)) {
        if ($check_type == 1) {

          // If RegExp
          if (@preg_match('/' . trim($value) . '/', $_SERVER['REQUEST_URI'])) {
            return;
          }
        }
        else {
          if (strpos($_SERVER['REQUEST_URI'], $value) !== false) {

            // Simple string checking
            return;
          }
        }
      }
    }
  }
  if (\Drupal::currentUser()
    ->hasPermission('access administration menu')) {
    return;
  }
  if (\Drupal::config('cleantalk.settings')
    ->get('cleantalk_link') && $form_id != 'search_form' && $form_id != 'user_login_form' && $form_id != 'search_block_form') {
    $form['cleantalk_link'] = array(
      '#type' => 'item',
      '#markup' => t("<a href='https://cleantalk.org/drupal-anti-spam-module-wo-captcha'>Drupal spam</a> blocked by CleanTalk."),
      '#required' => FALSE,
      '#weight' => 999,
    );
  }
  if ($form_id == 'user_register_form') {
    $form['#validate'][] = 'cleantalk_validate_register';
  }
  else {
    if (preg_match('|comment(.*?)_form|', $form_id)) {
      $form['#validate'][] = 'cleantalk_validate_comment';
    }
    if (preg_match('|contact(.*?)_form|', $form_id)) {
      $form['#validate'][] = 'cleantalk_validate_contact_message';
    }
    if (preg_match('|webform(.*?)_form|', $form_id)) {
      $form['#validate'][] = 'cleantalk_validate_webform';
    }
    if (preg_match('|node_forum(.*?)_form|', $form_id)) {
      $form['#validate'][] = 'cleantalk_validate_forum_topic';
    }
    if ($form_id == 'search_form' || $form_id == 'search_block_form') {

      // No special handler is set
      // $form['#validate'][] = 'cleantalk_validate_search_form';
    }
  }
}

/*
* implemments hook_uc_order - UberCart hook for order processing
*/
function cleantalk_uc_order($op, $order, $arg2) {
  global $cleantalk_executed;
  $ct_temp_msg_data = CleantalkHelper::get_fields_any(\Drupal::request()->request
    ->all(), \Drupal::config('cleantalk.settings')
    ->get('cleantalk_fields_exclusions'));
  $spam_check = array();
  $spam_check['type'] = 'comment';
  $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'] != '' && !$cleantalk_executed && $op != 'total' && $op != 'presave' && $op != 'save') {
    $spam_result = CleantalkFuncs::_cleantalk_check_spam($spam_check);
    $cleantalk_executed = true;
    if (isset($spam_result) && is_array($spam_result) && $spam_result['errno'] == 0 && $spam_result['allow'] != 1 && $spam_result['stop_queue'] == 1) {
      CleantalkFuncs::_cleantalk_die($spam_result['ct_result_comment']);
    }
  }
}

Functions

Namesort descending Description
cleantalk_comment_presave Implements hook_comment_presave().
cleantalk_form_alter Implements hook_form_BASE_FORM_ID_alter()
cleantalk_page_attachments_alter Implements hook_page_attachments_alter()
cleantalk_uc_order
cleantalk_validate_comment Cleantalk inner function - comment validation function.
cleantalk_validate_contact_message Cleantalk inner function - contact message validation function.
cleantalk_validate_forum_topic Cleantalk inner function - forum topic validation function.
cleantalk_validate_register Cleantalk inner function - registration validation function.
cleantalk_validate_webform Cleantalk inner function - webform validation function.
_cleantalk_form_alter Cleantalk inner function - alters needed form.