You are here

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

Class

SFW

Namespace

Cleantalk\Common\Firewall\Modules

Code

public function check() {
  $results = array();
  $status = 0;

  // Skip by cookie
  foreach ($this->ip_array as $current_ip) {
    if (substr(CleantalkFuncs::apbct_getcookie('ct_sfw_pass_key'), 0, 32) == md5($current_ip . $this->api_key)) {
      if (CleantalkFuncs::apbct_getcookie('ct_sfw_passed')) {
        if (!headers_sent()) {
          CleantalkFuncs::apbct_setcookie('ct_sfw_passed', '0');
        }
        else {
          $results[] = array(
            'ip' => $current_ip,
            'is_personal' => false,
            'status' => 'PASS_SFW__BY_COOKIE',
          );
        }

        // Do logging an one passed request
        $this
          ->update_log($current_ip, 'PASS_SFW');
        if ($this->sfw_counter) {

          // @ToDo have to implement the logic of incrementing and saving count of all handled requests.
        }
      }
      if (strlen(CleantalkFuncs::apbct_getcookie('ct_sfw_pass_key')) > 32) {
        $status = substr(CleantalkFuncs::apbct_getcookie('ct_sfw_pass_key'), -1);
      }
      if ($status) {
        $results[] = array(
          'ip' => $current_ip,
          'is_personal' => false,
          'status' => 'PASS_SFW__BY_WHITELIST',
        );
      }
      return $results;
    }
  }

  // Common check
  foreach ($this->ip_array as $origin => $current_ip) {
    $result_entry = array(
      'ip' => $current_ip,
    );
    $current_ip_v4 = sprintf("%u", ip2long($current_ip));
    for ($needles = array(), $m = 6; $m <= 32; $m++) {
      $mask = str_repeat('1', $m);
      $mask = str_pad($mask, 32, '0');
      $needles[] = sprintf("%u", bindec($mask & base_convert($current_ip_v4, 10, 2)));
    }
    $needles = array_unique($needles);
    $db_results = $this->db
      ->fetch_all("SELECT\n\t\t\t\tnetwork, mask, status, source\n\t\t\t\tFROM " . $this->db_data_table_name . "\n\t\t\t\tWHERE network IN (" . implode(',', $needles) . ")\n\t\t\t\tAND\tnetwork = " . $current_ip_v4 . " & mask\n\t\t\t\tAND " . rand(1, 100000) . " <> 0\n\t\t\t\tORDER BY status DESC");
    if (!empty($db_results)) {
      foreach ($db_results as $db_result) {
        $result_entry['network'] = Helper::ip__long2ip($db_result['network']) . '/' . Helper::ip__mask__long_to_number($db_result['mask']);
        $result_entry['is_personal'] = $db_result['source'];
        if ((int) $db_result['status'] === 1) {
          $result_entry['status'] = 'PASS_SFW__BY_WHITELIST';
          break;
        }
        if ((int) $db_result['status'] === 0) {
          $result_entry['status'] = 'DENY_SFW';
        }
      }
    }
    else {
      $result_entry['is_personal'] = null;
      $result_entry['status'] = 'PASS_SFW';
    }
    $results[] = $result_entry;
  }
  return $results;
}