You are here

function cleantalk_init in Anti Spam by CleanTalk 7.2

Same name and namespace in other branches
  1. 7.5 cleantalk.module \cleantalk_init()
  2. 7.4 cleantalk.module \cleantalk_init()

Implements hook_init()

File

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

Code

function cleantalk_init() {
  $ct_authkey = variable_get('cleantalk_authkey', '');
  if ($ct_authkey != '') {
    $ct_ccf = variable_get('cleantalk_ccf', 0);
    if (sizeof($_POST) > 0 && !isset($_POST['form_build_id']) && !isset($_POST['form_id']) && $ct_ccf == 1) {

      //Custom Contact forms
      $sender_email = '';
      $sender_nickname = '';
      $subject = '';
      $message = '';
      $contact_form = true;
      get_fields_any($sender_email, $message, $sender_nickname, $subject, $contact_form, $_POST);
      if ($sender_email != '') {
        if (!isset($_COOKIE['ct_checkjs'])) {
          $checkjs = NULL;
        }
        elseif ($_COOKIE['ct_checkjs'] == _cleantalk_get_checkjs_value()) {
          $checkjs = 1;
        }
        else {
          $checkjs = 0;
        }
        $data = array(
          'type' => 'comment',
          'auth_key' => $ct_authkey,
          'method_name' => 'check_message',
          'message' => $message,
          'example' => null,
          'agent' => 'drupal-209',
          'sender_ip' => $_SERVER['REMOTE_ADDR'],
          'sender_email' => $sender_email,
          'sender_nickname' => $sender_nickname,
          'message_title' => $subject,
          'message_body' => '',
          'example_title' => '',
          'example_body' => '',
          'example_comments' => '',
          'comment_body' => '',
          'js_on' => $checkjs,
        );
        if (!function_exists('drupal_json_encode')) {
          include_once DRUPAL_ROOT . '/includes/common.inc';
        }
        $result = _cleantalk_check_spam($data);
        if ($result['allow'] === 0) {
          drupal_set_message($result['ct_result_comment'], 'error');
        }
      }
    }
    if (isset($_POST['mergevars'])) {
      $sender_email = null;
      $message = '';
      foreach ($_POST['mergevars'] as $key => $value) {
        if ($sender_email === null && preg_match("/^\\S+@\\S+\\.\\S+\$/", $value)) {
          $sender_email = $value;
        }
        else {
          $message .= "{$value}\n";
        }
      }
      if ($sender_email != null) {
        $url = 'http://moderate.cleantalk.org/api2.0';
        $ct_authkey = variable_get('cleantalk_authkey', '');
        if (!isset($_COOKIE['ct_checkjs'])) {
          $checkjs = NULL;
        }
        elseif ($_COOKIE['ct_checkjs'] == _cleantalk_get_checkjs_value()) {
          $checkjs = 1;
        }
        else {
          $checkjs = 0;
        }
        $data = array(
          'type' => 'comment',
          'auth_key' => $ct_authkey,
          'method_name' => 'check_message',
          'message' => $message,
          'example' => null,
          'agent' => 'drupal-209',
          'sender_ip' => $_SERVER['REMOTE_ADDR'],
          'sender_email' => $sender_email,
          'sender_nickname' => '',
          'message_title' => '',
          'message_body' => '',
          'example_title' => '',
          'example_body' => '',
          'example_comments' => '',
          'comment_body' => '',
          'js_on' => $checkjs,
        );
        if (!function_exists('drupal_json_encode')) {
          include_once DRUPAL_ROOT . '/includes/common.inc';
        }
        $result = _cleantalk_check_spam($data);
        if ($result['allow'] === 0) {
          drupal_set_message($result['ct_result_comment'], 'error');
          $_POST['mergevars']['EMAIL'] = '';
        }
      }
    }
  }
  if (path_is_admin(current_path())) {
    if (isset($_POST['cleantalk_authkey'])) {
      $url = 'http://moderate.cleantalk.org/api2.0';
      $dt = array(
        'auth_key' => $_POST['cleantalk_authkey'],
        'method_name' => 'send_feedback',
        'feedback' => 0 . ':' . 'drupal-209',
      );
      sendRawRequest($url, $dt, true);
    }
    if (isset($_POST['cleantalk_authkey']) && isset($_POST['cleantalk_sfw']) && $_POST['cleantalk_sfw'] == 1) {
      CleanTalkSFW::sfw_update($_POST['cleantalk_authkey']);
      CleanTalkSFW::send_logs($_POST['cleantalk_authkey']);
    }
  }
}