You are here

public function Firewall::sendLogs in Anti Spam by CleanTalk 8.4

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

Sends and wipe SFW log

Return value

array|bool array('error' => STRING)

File

src/lib/Cleantalk/Common/Firewall/Firewall.php, line 304

Class

Firewall

Namespace

Cleantalk\Common\Firewall

Code

public function sendLogs() {

  //Getting logs
  $query = "SELECT * FROM " . $this->log_table_name . ";";
  $this->db
    ->fetch_all($query);
  if (count($this->db->result)) {

    //Compile logs
    $data = array();
    foreach ($this->db->result as $key => $value) {

      // Converting statuses to API format
      $value['status'] = $value['status'] === 'DENY_ANTICRAWLER' ? 'BOT_PROTECTION' : $value['status'];
      $value['status'] = $value['status'] === 'PASS_ANTICRAWLER' ? 'BOT_PROTECTION' : $value['status'];
      $value['status'] = $value['status'] === 'DENY_ANTICRAWLER_UA' ? 'BOT_PROTECTION' : $value['status'];
      $value['status'] = $value['status'] === 'PASS_ANTICRAWLER_UA' ? 'BOT_PROTECTION' : $value['status'];
      $value['status'] = $value['status'] === 'DENY_ANTIFLOOD' ? 'FLOOD_PROTECTION' : $value['status'];
      $value['status'] = $value['status'] === 'PASS_ANTIFLOOD' ? 'FLOOD_PROTECTION' : $value['status'];
      $value['status'] = $value['status'] === 'PASS_SFW__BY_COOKIE' ? null : $value['status'];
      $value['status'] = $value['status'] === 'PASS_SFW' ? null : $value['status'];
      $value['status'] = $value['status'] === 'DENY_SFW' ? null : $value['status'];
      $data[] = array(
        trim($value['ip']),
        // IP
        $value['blocked_entries'],
        // Count showing of block pages
        $value['all_entries'] - $value['blocked_entries'],
        // Count passed requests after block pages
        $value['entries_timestamp'],
        // Last timestamp
        $value['status'],
        // Status
        $value['ua_name'],
        // User-Agent name
        $value['ua_id'],
      );
    }
    unset($key, $value);

    //Sending the request
    $api = $this->api;
    $result = $api::method__sfw_logs($this->api_key, $data);

    //Checking answer and deleting all lines from the table
    if (empty($result['error'])) {
      if ($result['rows'] == count($data)) {
        $this->db
          ->execute("TRUNCATE TABLE " . $this->log_table_name . ";");
        return $result;
      }
      return array(
        'error' => 'SENT_AND_RECEIVED_LOGS_COUNT_DOESNT_MACH',
      );
    }
    return $result;
  }
  return array(
    'rows' => 0,
  );
}