You are here

public function AntiFlood::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/AntiFlood.php \Cleantalk\Common\Firewall\Modules\AntiFlood::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/AntiFlood.php, line 52

Class

AntiFlood

Namespace

Cleantalk\Common\Firewall\Modules

Code

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

    // UA check
    $ua_bl_results = $this->db
      ->fetch_all("SELECT * FROM " . $this->db__table__ac_ua_bl . " ORDER BY `ua_status` DESC;");
    if (!empty($ua_bl_results)) {
      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_ANTIFLOOD_UA',
            );
            return $results;
          }
        }
      }
    }

    // Passed
    if (CleantalkFuncs::apbct_getcookie('apbct_antiflood_passed') === md5($current_ip . $this->api_key)) {
      if (!headers_sent()) {
        CleantalkFuncs::apbct_setcookie('apbct_antiflood_passed', '0');
      }

      // Do logging an one passed request
      $this
        ->update_log($current_ip, 'PASS_ANTIFLOOD');
      $results[] = array(
        'ip' => $current_ip,
        'is_personal' => false,
        'status' => 'PASS_ANTIFLOOD',
      );
      return $results;
    }

    // @todo Rename ip column to sign. Use IP + UserAgent for it.
    $result = $this->db
      ->fetch("SELECT SUM(entries) as total_count" . ' FROM ' . $this->db__table__ac_logs . '' . " WHERE ip = '{$current_ip}' AND interval_start > '{$time}' AND " . rand(1, 100000) . ";");
    if (!empty($result) && isset($result['total_count']) && $result['total_count'] >= $this->view_limit) {
      $results[] = array(
        'ip' => $current_ip,
        'is_personal' => false,
        'status' => 'DENY_ANTIFLOOD',
      );
    }
  }
  if (!empty($results)) {

    // Do block page
    return $results;
  }
  else {
    $this
      ->update_ac_log();
  }
  return $results;
}