You are here

class CleantalkFuncs in Anti Spam by CleanTalk 8.3

Same name and namespace in other branches
  1. 8.4 src/CleantalkFuncs.php \Drupal\cleantalk\CleantalkFuncs
  2. 9.1.x src/CleantalkFuncs.php \Drupal\cleantalk\CleantalkFuncs

Cleantalk class create request

Hierarchy

Expanded class hierarchy of CleantalkFuncs

3 files declare their use of CleantalkFuncs
BootSubscriber.php in src/EventSubscriber/BootSubscriber.php
cleantalk.module in ./cleantalk.module
CleantalkSettingsForm.php in src/Form/CleantalkSettingsForm.php

File

src/CleantalkFuncs.php, line 23

Namespace

Drupal\cleantalk
View source
class CleantalkFuncs {

  /*
   * get form submit_time
   */
  public static function _cleantalk_get_submit_time($timestamp) {
    return self::_cleantalk_apbct_cookies_test() == 1 && $timestamp ? time() - (int) $timestamp : null;
  }

  /**
   * Save our variables into cookies OR sessions
   *
   * @param $name     string   Name of our variables to save
   * @param $value    string   Value of our variables to save
   */
  public static function apbct_setcookie($name, $value) {
    if (\Drupal::config('cleantalk.settings')
      ->get('cleantalk_alternative_cookies_session')) {
      self::_apbct_alt_sessions__remove_old();

      // Into database
      $connection = \Drupal::database();
      $connection
        ->query("INSERT INTO {cleantalk_sessions}\n      (id, name, value, last_update)\n      VALUES (:id, :name, :value, :last_update)\n      ON DUPLICATE KEY UPDATE\n      value = :value,\n      last_update = :last_update", array(
        ':id' => self::_apbct_alt_session__id__get(),
        ':name' => $name,
        ':value' => $value,
        ':last_update' => date('Y-m-d H:i:s'),
      ));
    }
    else {

      // Into cookies
      setcookie($name, $value, 0, '/');
    }
  }

  /**
   * Get our variables from cookies OR sessions
   *
   * @param $name     string    Name of necessary variable to get
   *
   * @return string|null
   */
  public static function apbct_getcookie($name) {
    if (\Drupal::config('cleantalk.settings')
      ->get('cleantalk_alternative_cookies_session') && substr($name, 0, strlen('apbct')) === 'apbct') {

      // From database
      $connection = \Drupal::database();
      $value = $connection
        ->query("SELECT value FROM {cleantalk_sessions} WHERE id = :id AND name = :name", array(
        ':id' => self::_apbct_alt_session__id__get(),
        ':name' => $name,
      ))
        ->fetchField();
      if (false !== $value) {
        return $value;
      }
      else {
        return null;
      }
    }
    else {

      // From cookies
      if (isset($_COOKIE[$name])) {
        return $_COOKIE[$name];
      }
      else {
        return null;
      }
    }
  }

  /**
   * Clean 'cleantalk_sessions' table
   */
  private static function _apbct_alt_sessions__remove_old() {
    if (rand(0, 1000) < APBCT_SESSION__CHANCE_TO_CLEAN) {
      $connection = \Drupal::database();
      $connection
        ->query("DELETE\n      FROM {cleantalk_sessions}\n      WHERE last_update < NOW() - INTERVAL " . APBCT_SESSION__LIVE_TIME . " SECOND\n      LIMIT 100000;");
    }
  }

  /**
   * Get hash session ID
   *
   * @return string
   */
  private static function _apbct_alt_session__id__get() {
    $id = CleantalkHelper::ip_get(array(
      'real',
    )) . filter_input(INPUT_SERVER, 'HTTP_USER_AGENT') . filter_input(INPUT_SERVER, 'HTTP_ACCEPT_LANGUAGE');
    return hash('sha256', $id);
  }

  /**
   * Cookie test
   * @return int   1|0
   */
  private static function _cleantalk_apbct_cookies_test() {
    if (\Drupal::config('cleantalk.settings')
      ->get('cleantalk_alternative_cookies_session')) {
      return 1;
    }
    $cookie_test = json_decode(stripslashes(self::apbct_getcookie('apbct_cookies_test')), true);
    if (is_null($cookie_test)) {
      return null;
    }
    $check_string = trim(\Drupal::config('cleantalk.settings')
      ->get('cleantalk_authkey'));
    foreach ($cookie_test['cookies_names'] as $cookie_name) {
      $check_string .= self::apbct_getcookie($cookie_name);
    }
    unset($cokie_name);
    if ($cookie_test['check_value'] == md5($check_string)) {
      return 1;
    }
    else {
      return 0;
    }
  }

  /**
   * Cleantalk inner function - show error message and exit.
   */
  public static function _cleantalk_die($message) {
    $output = '<!DOCTYPE html><!-- Ticket #11289, IE bug fix: always pad the error page with enough characters such that it is greater than 512 bytes, even after gzip compression abcdefghijklmnopqrstuvwxyz1234567890aabbccddeeffgghhiijjkkllmmnnooppqqrrssttuuvvwwxxyyzz11223344556677889900abacbcbdcdcededfefegfgfhghgihihjijikjkjlklkmlmlnmnmononpopoqpqprqrqsrsrtstsubcbcdcdedefefgfabcadefbghicjkldmnoepqrfstugvwxhyz1i234j567k890laabmbccnddeoeffpgghqhiirjjksklltmmnunoovppqwqrrxsstytuuzvvw0wxx1yyz2z113223434455666777889890091abc2def3ghi4jkl5mno6pqr7stu8vwx9yz11aab2bcc3dd4ee5ff6gg7hh8ii9j0jk1kl2lmm3nnoo4p5pq6qrr7ss8tt9uuvv0wwx1x2yyzz13aba4cbcb5dcdc6dedfef8egf9gfh0ghg1ihi2hji3jik4jkj5lkl6kml7mln8mnm9ono--><html xmlns="http://www.w3.org/1999/xhtml" lang="en-US"><head>    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />    <title>Blacklisted</title>    <style type="text/css">        html {            background: #f1f1f1;        }        body {            background: #fff;            color: #444;            font-family: "Open Sans", sans-serif;            margin: 2em auto;            padding: 1em 2em;            max-width: 700px;            -webkit-box-shadow: 0 1px 3px rgba(0,0,0,0.13);            box-shadow: 0 1px 3px rgba(0,0,0,0.13);        }        h1 {            border-bottom: 1px solid #dadada;            clear: both;            color: #666;            font: 24px "Open Sans", sans-serif;            margin: 30px 0 0 0;            padding: 0;            padding-bottom: 7px;        }        #error-page {            margin-top: 50px;        }        #error-page p {            font-size: 14px;            line-height: 1.5;            margin: 25px 0 20px;        }        a {            color: #21759B;            text-decoration: none;        }        a:hover {            color: #D54E21;        }            </style></head><body id="error-page">    <p><center><b style="color: #49C73B;">Clean</b><b style="color: #349ebf;">Talk.</b> Spam protection</center><br><br>' . $message . '<script>setTimeout("history.back()", 5000);</script></p><p><a href="javascript:history.back()">&laquo; Back</a></p></body></html>';
    throw new ServiceUnavailableHttpException(3, $output);
  }

  /**
   * Cleantalk inner function - gets JavaScript checking value.
   */
  public static function _cleantalk_get_checkjs_value() {
    return md5(\Drupal::config('cleantalk.settings')
      ->get("cleantalk_authkey") . '+' . \Drupal::config('system.site')
      ->get("mail"));
  }

  /**
   * Cleantalk inner function - performs antispam checking.
   */
  public static function _cleantalk_check_spam($spam_check, $form_errors = null) {
    global $cleantalk_executed;
    $curr_user = \Drupal::currentUser();

    // Exclusion. Administrator.
    if ($curr_user
      ->hasPermission('access administration menu') || $cleantalk_executed) {
      return NULL;
    }

    // Exclusion. By roles.
    $roles = \Drupal::config('cleantalk.settings')
      ->get('cleantalk_roles_exclusions');
    if (!empty($roles)) {
      foreach ($roles as $role_id) {
        if (self::_cleantalk_user_has_role_id($role_id, $curr_user)) {
          return NULL;
        }
      }
    }

    // Exclusion. By number of posted comments
    if ($curr_user
      ->id()) {
      $user = \Drupal\user\Entity\User::load($curr_user
        ->id());
      $uid = $user
        ->get('uid')->value;

      // Don't check reged user with >= 'cleantalk_check_comments_min_approved' approved msgs.
      if (is_object($user) && $user
        ->get('uid')->value > 0 && \Drupal::service('module_handler')
        ->moduleExists('comment')) {
        $result = \Drupal::database()
          ->query('SELECT count(*) AS count FROM {comment_field_data} WHERE uid=:uid AND status=1', [
          ':uid' => $uid,
        ])
          ->fetchObject()->count;
        $count = intval($result);
        $ct_comments = \Drupal::config('cleantalk.settings')
          ->get('cleantalk_check_comments_min_approved');
        if ($count >= $ct_comments) {
          return NULL;
        }
      }
    }

    // Exclusion. By URLs
    $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 NULL;
            }
          }
          else {
            if (strpos($_SERVER['REQUEST_URI'], $value) !== false) {

              // Simple string checking
              return NULL;
            }
          }
          if (strpos(trim($value), 'node') !== FALSE && strpos($_SERVER['REQUEST_URI'], 'q=comment/reply/') !== FALSE) {
            $get_node = array_values(array_slice(explode('/', trim($value)), -1))[0];
            $current_reply_id = array_values(array_slice(explode('/', $_SERVER['REQUEST_URI']), -1))[0];
            if ($get_node == $current_reply_id) {
              return NULL;
            }
          }
        }
      }
    }
    $ct_authkey = \Drupal::config('cleantalk.settings')
      ->get('cleantalk_authkey');
    $ct_ws = self::_cleantalk_get_ws();
    if (!self::apbct_getcookie('ct_check_js')) {
      $checkjs = NULL;
    }
    elseif (self::apbct_getcookie('ct_check_js') == self::_cleantalk_get_checkjs_value()) {
      $checkjs = 1;
    }
    else {
      $checkjs = 0;
    }
    $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'];
    $role_exclusions = \Drupal::config('cleantalk.settings')
      ->get('cleantalk_roles_exclusions');
    $ct_options = array(
      'access_key' => $ct_authkey,
      'cleantalk_check_comments' => \Drupal::config('cleantalk.settings')
        ->get('cleantalk_check_comments'),
      'cleantalk_check_comments_automod' => \Drupal::config('cleantalk.settings')
        ->get('cleantalk_check_comments_automod'),
      'cleantalk_check_comments_min_approved' => \Drupal::config('cleantalk.settings')
        ->get('cleantalk_check_comments_min_approved'),
      'cleantalk_check_register' => \Drupal::config('cleantalk.settings')
        ->get('cleantalk_check_register'),
      'cleantalk_check_webforms' => \Drupal::config('cleantalk.settings')
        ->get('cleantalk_check_webforms'),
      'cleantalk_check_contact_forms' => \Drupal::config('cleantalk.settings')
        ->get('cleantalk_check_contact_forms'),
      'cleantalk_check_forum_topics' => \Drupal::config('cleantalk.settings')
        ->get('cleantalk_check_forum_topics'),
      'cleantalk_check_search_form' => \Drupal::config('cleantalk.settings')
        ->get('cleantalk_check_search_form'),
      'cleantalk_url_exclusions' => \Drupal::config('cleantalk.settings')
        ->get('cleantalk_url_exclusions'),
      'cleantalk_url_regexp' => \Drupal::config('cleantalk.settings')
        ->get('cleantalk_url_regexp'),
      'cleantalk_fields_exclusions' => \Drupal::config('cleantalk.settings')
        ->get('cleantalk_fields_exclusions'),
      'cleantalk_roles_exclusions' => !empty($role_exclusions) ? implode(',', $role_exclusions) : '',
      'cleantalk_add_search_noindex' => \Drupal::config('cleantalk.settings')
        ->get('cleantalk_add_search_noindex'),
      'cleantalk_search_noindex' => \Drupal::config('cleantalk.settings')
        ->get('cleantalk_search_noindex'),
      'cleantalk_set_cookies' => \Drupal::config('cleantalk.settings')
        ->get('cleantalk_set_cookies'),
      'cleantalk_alternative_cookies_session' => \Drupal::config('cleantalk.settings')
        ->get('cleantalk_alternative_cookies_session'),
      'cleantalk_check_ccf' => \Drupal::config('cleantalk.settings')
        ->get('cleantalk_check_ccf'),
      'cleantalk_link' => \Drupal::config('cleantalk.settings')
        ->get('cleantalk_link'),
      'cleantalk_sfw' => \Drupal::config('cleantalk.settings')
        ->get('cleantalk_sfw'),
    );
    $sender_info = \Drupal\Component\Serialization\Json::encode(array(
      'cms_lang' => \Drupal::languageManager()
        ->getCurrentLanguage()
        ->getId(),
      'REFFERRER' => isset($_SERVER['HTTP_REFERER']) ? htmlspecialchars($_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']) ? htmlspecialchars($_SERVER['HTTP_USER_AGENT']) : null,
      'ct_options' => \Drupal\Component\Serialization\Json::encode($ct_options),
      'REFFERRER_PREVIOUS' => self::apbct_getcookie('apbct_prev_referer'),
      'cookies_enabled' => self::_cleantalk_apbct_cookies_test(),
      'fields_number' => sizeof($spam_check),
      'js_timezone' => self::apbct_getcookie('ct_timezone'),
      'mouse_cursor_positions' => !empty($_COOKIE['ct_pointer_data']) ? json_decode(stripslashes($_COOKIE['ct_pointer_data']), true) : null,
      'key_press_timestamp' => !empty($_COOKIE['ct_fkp_timestamp']) ? $_COOKIE['ct_fkp_timestamp'] : null,
      'page_set_timestamp' => !empty($_COOKIE['ct_ps_timestamp']) ? $_COOKIE['ct_ps_timestamp'] : null,
      'form_validation' => $form_errors && is_array($form_errors) ? json_encode(array(
        'validation_notice' => strip_tags(json_encode($form_errors)),
        'page_url' => $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'],
      )) : null,
    ));
    $post_info = \Drupal\Component\Serialization\Json::encode(array(
      'comment_type' => $spam_check['type'],
      'post_url' => isset($_SERVER['HTTP_REFERER']) ? htmlspecialchars($_SERVER['HTTP_REFERER']) : null,
    ));
    $ct_request = new CleantalkRequest();
    $ct_request->auth_key = $ct_authkey;
    $ct_request->agent = CLEANTALK_USER_AGENT;
    $ct_request->response_lang = 'en';
    $ct_request->js_on = $checkjs;
    $ct_request->sender_info = $sender_info;
    $ct_request->post_info = $post_info;
    $ct_request->sender_email = $spam_check['sender_email'];
    $ct_request->sender_nickname = $spam_check['sender_nickname'];
    $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 = isset($spam_check['multistep_submit_time']) ? self::_cleantalk_get_submit_time($spam_check['multistep_submit_time']) : self::_cleantalk_get_submit_time(self::apbct_getcookie('apbct_timestamp'));
    switch ($spam_check['type']) {
      case 'comment':
      case 'contact':
      case 'forum_topic':
      case 'webform':
      case 'site_search_drupal8':
      case 'custom_contact_form':
        $timelabels_key = 'mail_error_comment';
        if (is_array($spam_check['message_body'])) {
          $spam_check['message_body'] = isset($spam_check['message_body']['message']) ? $spam_check['message_body']['message'] : implode("\n\n", $spam_check['message_body']);
        }
        $ct_request->message = $spam_check['message_title'] . " \n\n" . strip_tags($spam_check['message_body']);
        $ct_result = $ct
          ->isAllowMessage($ct_request);
        break;
      case 'register':
        $timelabels_key = 'mail_error_reg';
        $ct_request->tz = $spam_check['timezone'];
        $ct_result = $ct
          ->isAllowUser($ct_request);
        break;
    }
    $cleantalk_executed = true;
    $ret_val = array();
    $ret_val['ct_request_id'] = $ct_result->id;
    if ($ct->server_change) {
      self::_cleantalk_set_ws($ct->work_url, $ct->server_ttl, 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 ($checkjs == 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 = \Drupal::database()
        ->select('cleantalk_timelabels', 'c')
        ->fields('c', array(
        'ct_value',
      ))
        ->condition('ct_key', $timelabels_key, '=')
        ->execute();
      $results = $result
        ->fetchCol(0);
      if (count($results) == 0) {
        $send_flag = TRUE;
      }
      elseif (\Drupal::time()
        ->getRequestTime() - 900 > $result
        ->fetchObject()->ct_value) {

        // 15 minutes.
        $send_flag = TRUE;
      }
      if ($send_flag) {
        \Drupal::database()
          ->merge('cleantalk_timelabels')
          ->key(array(
          'ct_key' => $timelabels_key,
        ))
          ->fields(array(
          'ct_value' => \Drupal::time()
            ->getRequestTime(),
        ))
          ->execute();

        // @FIXME
        // // @FIXME
        // // This looks like another module's variable. You'll need to rewrite this call
        // // to ensure that it uses the correct configuration object.
        // $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', $ret_val['allow'], $ct_result->id);

      // 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') {

        // Spammer and stop_queue == 0 - to manual approvement.
        $ret_val['stop_queue'] = $ct_result->stop_queue;

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

  /**
   * Cleantalk inner function - performs CleanTalk comment|errstr filtering.
   */
  public static function _cleantalk_filter_response($ct_response) {
    if (preg_match('//u', $ct_response)) {
      $err_str = preg_replace('/\\*\\*\\*/iu', '', $ct_response);
    }
    else {
      $err_str = preg_replace('/\\*\\*\\*/i', '', $ct_response);
    }
    return \Drupal\Component\Utility\Xss::filter($err_str, array(
      'a',
    ));
  }

  /**
   * Cleantalk inner function - stores spam checking result.
   */
  public static function _cleantalk_ct_result($cmd = 'get', $id = '', $allow = 1, $comment = '') {
    static $request_id = '';
    static $result_allow = 1;
    static $result_comment = '';
    if ($cmd == 'set') {
      $request_id = $id;
      $result_allow = $allow;
      $result_comment = $comment;
    }
    else {
      return array(
        'ct_request_id' => $request_id,
        'ct_result_allow' => $result_allow,
        'ct_result_comment' => $result_comment,
      );
    }
  }

  /**
   * Cleantalk inner function - gets working server.
   */
  public static function _cleantalk_get_ws() {
    return array(
      'work_url' => \Drupal::state()
        ->get('cleantalk_work_url'),
      'server_url' => 'http://moderate.cleantalk.org',
      'server_ttl' => \Drupal::state()
        ->get('cleantalk_server_ttl'),
      'server_changed' => \Drupal::state()
        ->get('cleantalk_server_changed'),
    );
  }

  /**
   * Cleantalk inner function - sets working server.
   */
  public static function _cleantalk_set_ws($work_url = 'http://moderate.cleantalk.org', $server_ttl = 0, $server_changed = 0) {
    \Drupal::state()
      ->set('cleantalk_work_url', $work_url);
    \Drupal::state()
      ->set('cleantalk_server_ttl', $server_ttl);
    \Drupal::state()
      ->set('cleantalk_server_changed', $server_changed);
  }

  /**
   * Cleantalk inner function - check form handlers for save to prevent checking drafts/preview.
   */
  public static function _cleantalk_check_form_submit_handlers($submitHandlers) {
    if ($submitHandlers && is_array($submitHandlers)) {
      foreach ($submitHandlers as $handler) {
        if ($handler === '::save') {
          return true;
        }
      }
    }
    return false;
  }
  public static function _cleantalk_sfw_update($access_key) {
    $sfw = new CleantalkSFW($access_key);
    $file_url_hash = isset($_GET['file_url_hash']) ? urldecode($_GET['file_url_hash']) : null;
    $file_url_nums = isset($_GET['file_url_nums']) ? urldecode($_GET['file_url_nums']) : null;
    $file_url_nums = isset($file_url_nums) ? explode(',', $file_url_nums) : null;
    if (!isset($file_url_hash, $file_url_nums)) {
      $result = $sfw
        ->sfw_update();
    }
    elseif ($file_url_hash && is_array($file_url_nums) && count($file_url_nums)) {
      $result = $sfw
        ->sfw_update($file_url_hash, $file_url_nums[0]);
      if (empty($result['error'])) {
        array_shift($file_url_nums);
        if (count($file_url_nums)) {
          CleantalkHelper::http__request((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://" . $_SERVER['HTTP_HOST'], array(
            'spbc_remote_call_token' => md5($access_key),
            'spbc_remote_call_action' => 'sfw_update__write_base',
            'plugin_name' => 'apbct',
            'file_url_hash' => $file_url_hash,
            'file_url_nums' => implode(',', $file_url_nums),
          ), array(
            'get',
            'async',
          ));
        }
        else {
          \Drupal::state()
            ->set('cleantalk_sfw_last_check', time());
        }
      }
      else {
        return array(
          'error' => 'ERROR_WHILE_INSERTING_SFW_DATA',
        );
      }
    }
    return $result;
  }
  public static function _cleantalk_sfw_send_logs($access_key) {
    $sfw = new CleantalkSFW($access_key);
    $result = $sfw
      ->send_logs();
    \Drupal::state()
      ->set('cleantalk_sfw_last_send_log', time());
  }

  /**
   * Cleantalk inner function - perform remote call
   */
  public static function _cleantalk_apbct_remote_call__perform() {
    $remote_calls_config = \Drupal::state()
      ->get('cleantalk_remote_calls');
    $remote_action = $_GET['spbc_remote_call_action'];
    $auth_key = trim(\Drupal::config('cleantalk.settings')
      ->get('cleantalk_authkey'));
    if (is_array($remote_calls_config) && array_key_exists($remote_action, $remote_calls_config)) {
      $cooldown = isset($remote_calls_config[$remote_action]['cooldown']) ? $remote_calls_config[$remote_action]['cooldown'] : 10;
      if (time() - $remote_calls_config[$remote_action]['last_call'] > $cooldown || $remote_action == 'sfw_update__write_base' && isset($_GET['file_url_hash'])) {
        $remote_calls_config[$remote_action]['last_call'] = time();
        \Drupal::state()
          ->set('cleantalk_remote_calls', $remote_calls_config);
        if (strtolower($_GET['spbc_remote_call_token']) == strtolower(md5($auth_key))) {

          // Close renew banner
          if ($remote_action == 'close_renew_banner') {
            \Drupal::state()
              ->set('cleantalk_show_renew_banner', 0);
            die('OK');

            // SFW update
          }
          elseif ($remote_action == 'sfw_update') {
            $result = self::_cleantalk_sfw_update($auth_key);
            die(empty($result['error']) ? 'OK' : 'FAIL ' . json_encode(array(
              'error' => $result['error_string'],
            )));

            // SFW send logs
          }
          elseif ($remote_action == 'sfw_update__write_base') {
            $result = self::_cleantalk_sfw_update($auth_key);
            die(empty($result['error']) ? 'OK' : 'FAIL ' . json_encode(array(
              'error' => $result['error_string'],
            )));
          }
          elseif ($remote_action == 'sfw_send_logs') {
            $result = self::_cleantalk_sfw_send_logs($auth_key);
            die(empty($result['error']) ? 'OK' : 'FAIL ' . json_encode(array(
              'error' => $result['error_string'],
            )));

            // Update plugin
          }
          elseif ($remote_action == 'update_plugin') {

            //add_action('wp', 'apbct_update', 1);
          }
          else {
            die('FAIL ' . json_encode(array(
              'error' => 'UNKNOWN_ACTION_2',
            )));
          }
        }
        else {
          die('FAIL ' . json_encode(array(
            'error' => 'WRONG_TOKEN',
          )));
        }
      }
      else {
        die('FAIL ' . json_encode(array(
          'error' => 'TOO_MANY_ATTEMPTS',
        )));
      }
    }
    else {
      die('FAIL ' . json_encode(array(
        'error' => 'UNKNOWN_ACTION',
      )));
    }
  }
  public static function _cleantalk_user_has_role_id($role_id, $user = NULL) {
    $roles = \Drupal::currentUser()
      ->getRoles();
    if (is_array($roles) && in_array($role_id, $roles)) {
      return TRUE;
    }
    return FALSE;
  }
  public static function cleantalk_get_user_roles() {
    $roles = \Drupal\user\Entity\Role::loadMultiple();
    $roles_arr = array();
    foreach ($roles as $role) {
      $roles_arr[$role
        ->get('id')] = $role
        ->get('label');
    }
    asort($roles_arr);
    return $roles_arr;
  }
  public static function cleantalk_get_user_roles_default() {
    if (empty(\Drupal::config('cleantalk.settings')
      ->get('cleantalk_roles_exclusions'))) {
      $roles = self::cleantalk_get_user_roles();
      foreach ($roles as $role_id => $role_name) {
        if (strpos('administrator', $role_id) === false) {
          unset($roles[$role_id]);
        }
      }
      return array_keys($roles);
    }
    else {
      $roles = \Drupal::config('cleantalk.settings')
        ->get('cleantalk_roles_exclusions');
      $config_roles = array();
      foreach ((array) $roles as $role => $role_name) {
        $config_roles[$role_name] = $role_name;
      }
      return $config_roles;
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CleantalkFuncs::apbct_getcookie public static function Get our variables from cookies OR sessions
CleantalkFuncs::apbct_setcookie public static function Save our variables into cookies OR sessions
CleantalkFuncs::cleantalk_get_user_roles public static function
CleantalkFuncs::cleantalk_get_user_roles_default public static function
CleantalkFuncs::_apbct_alt_sessions__remove_old private static function Clean 'cleantalk_sessions' table
CleantalkFuncs::_apbct_alt_session__id__get private static function Get hash session ID
CleantalkFuncs::_cleantalk_apbct_cookies_test private static function Cookie test
CleantalkFuncs::_cleantalk_apbct_remote_call__perform public static function Cleantalk inner function - perform remote call
CleantalkFuncs::_cleantalk_check_form_submit_handlers public static function Cleantalk inner function - check form handlers for save to prevent checking drafts/preview.
CleantalkFuncs::_cleantalk_check_spam public static function Cleantalk inner function - performs antispam checking.
CleantalkFuncs::_cleantalk_ct_result public static function Cleantalk inner function - stores spam checking result.
CleantalkFuncs::_cleantalk_die public static function Cleantalk inner function - show error message and exit.
CleantalkFuncs::_cleantalk_filter_response public static function Cleantalk inner function - performs CleanTalk comment|errstr filtering.
CleantalkFuncs::_cleantalk_get_checkjs_value public static function Cleantalk inner function - gets JavaScript checking value.
CleantalkFuncs::_cleantalk_get_submit_time public static function
CleantalkFuncs::_cleantalk_get_ws public static function Cleantalk inner function - gets working server.
CleantalkFuncs::_cleantalk_set_ws public static function Cleantalk inner function - sets working server.
CleantalkFuncs::_cleantalk_sfw_send_logs public static function
CleantalkFuncs::_cleantalk_sfw_update public static function
CleantalkFuncs::_cleantalk_user_has_role_id public static function