You are here

public function AntiCrawler::check in Anti Spam by CleanTalk 8.4

Same name and namespace in other branches
  1. 9.1.x src/lib/Cleantalk/Common/Firewall/Modules/AntiCrawler.php \Cleantalk\Common\Firewall\Modules\AntiCrawler::check()

Use this method to execute main logic of the module.

Return value

array Array of the check results

Overrides FirewallModule::check

File

src/lib/Cleantalk/Common/Firewall/Modules/AntiCrawler.php, line 67

Class

AntiCrawler

Namespace

Cleantalk\Common\Firewall\Modules

Code

public function check() {
  $results = array();
  foreach ($this->ip_array as $current_ip) {

    // Skip by 301 response code
    if ($this
      ->is_redirected()) {
      $results[] = array(
        'ip' => $current_ip,
        'is_personal' => false,
        'status' => 'PASS_ANTICRAWLER',
      );
      return $results;
    }

    // UA check
    $ua_bl_results = $this->db
      ->fetch_all("SELECT * FROM " . $this->ua_bl_table_name . " ORDER BY `ua_status` DESC;");
    if (!empty($ua_bl_results)) {
      $is_blocked = false;
      foreach ($ua_bl_results as $ua_bl_result) {
        if (!empty($ua_bl_result['ua_template']) && preg_match("%" . str_replace('"', '', $ua_bl_result['ua_template']) . "%i", Server::get('HTTP_USER_AGENT'))) {
          if ($ua_bl_result['ua_status'] == 1) {

            // Whitelisted
            $results[] = array(
              'ip' => $current_ip,
              'is_personal' => false,
              'status' => 'PASS_ANTICRAWLER_UA',
            );
            return $results;
          }
          else {

            // Blacklisted
            $results[] = array(
              'ip' => $current_ip,
              'is_personal' => false,
              'status' => 'DENY_ANTICRAWLER_UA',
            );
            $is_blocked = true;
            break;
          }
        }
      }
      if (!$is_blocked) {
        $results[] = array(
          'ip' => $current_ip,
          'is_personal' => false,
          'status' => 'PASS_ANTICRAWLER_UA',
        );
      }
    }

    // Skip by cookie
    if (CleantalkFuncs::apbct_getcookie('apbct_antibot') == $this->antibot_cookie_value) {
      if (CleantalkFuncs::apbct_getcookie('apbct_anticrawler_passed') == 1) {
        if (!headers_sent()) {
          CleantalkFuncs::apbct_setcookie('apbct_anticrawler_passed', '0');
        }
      }
      $results[] = array(
        'ip' => $current_ip,
        'is_personal' => false,
        'status' => 'PASS_ANTICRAWLER',
      );
      return $results;
    }
  }

  // Common check
  foreach ($this->ip_array as $current_ip) {

    // IP check
    $result = $this->db
      ->fetch("SELECT ip" . ' FROM `' . $this->db__table__ac_logs . '`' . " WHERE `ip` = '{$current_ip}'" . " AND `ua` = '{$this->sign}' AND " . rand(1, 100000) . ";");
    if (isset($result['ip'])) {
      if (CleantalkFuncs::apbct_getcookie('apbct_antibot') !== $this->antibot_cookie_value) {
        $results[] = array(
          'ip' => $current_ip,
          'is_personal' => false,
          'status' => 'DENY_ANTICRAWLER',
        );
      }
      else {
        if (CleantalkFuncs::apbct_getcookie('apbct_anticrawler_passed') === '1') {
          if (!headers_sent()) {
            CleantalkFuncs::apbct_setcookie('apbct_anticrawler_passed', '0');
          }

          // Do logging an one passed request
          $this
            ->update_log($current_ip, 'PASS_ANTICRAWLER');
          $results[] = array(
            'ip' => $current_ip,
            'is_personal' => false,
            'status' => 'PASS_ANTICRAWLER',
          );
          return $results;
        }
      }
    }
    else {
      if (!CleantalkFuncs::apbct_getcookie('apbct_antibot')) {
        $this
          ->update_ac_log();
      }
    }
  }
  return $results;
}