You are here

class CleanTalkSFW in Anti Spam by CleanTalk 7.2

Same name and namespace in other branches
  1. 7.5 src/CleantalkSFW.php \CleantalkSFW
  2. 7.4 src/CleantalkSFW.php \CleantalkSFW

Cleantalk Spam FireWall class

Hierarchy

Expanded class hierarchy of CleanTalkSFW

File

./cleantalk.module, line 19
Main CleanTalk integration module functions.

View source
class CleanTalkSFW {
  public $ip = 0;
  public $ip_str = '';
  public $ip_array = array();
  public $ip_str_array = array();
  public $blocked_ip = '';
  public $passed_ip = '';
  public $result = false;
  public function cleantalk_get_real_ip() {
    if (function_exists('apache_request_headers')) {
      $headers = apache_request_headers();
    }
    else {
      $headers = $_SERVER;
    }
    if (array_key_exists('X-Forwarded-For', $headers)) {
      $the_ip = explode(",", trim($headers['X-Forwarded-For']));
      $the_ip = trim($the_ip[0]);
      $this->ip_str_array[] = $the_ip;
      $this->ip_array[] = sprintf("%u", ip2long($the_ip));
    }
    if (array_key_exists('HTTP_X_FORWARDED_FOR', $headers)) {
      $the_ip = explode(",", trim($headers['HTTP_X_FORWARDED_FOR']));
      $the_ip = trim($the_ip[0]);
      $this->ip_str_array[] = $the_ip;
      $this->ip_array[] = sprintf("%u", ip2long($the_ip));
    }
    $the_ip = filter_var($_SERVER['REMOTE_ADDR'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4);
    $this->ip_str_array[] = $the_ip;
    $this->ip_array[] = sprintf("%u", ip2long($the_ip));
    if (isset($_GET['sfw_test_ip'])) {
      $the_ip = $_GET['sfw_test_ip'];
      $this->ip_str_array[] = $the_ip;
      $this->ip_array[] = sprintf("%u", ip2long($the_ip));
    }
  }
  public function check_ip() {
    $n = 1;
    $r = db_query("SHOW TABLES LIKE 'cleantalk_sfw'");
    $ob = $r
      ->fetchObject();
    if (isset($ob) && is_object($ob)) {
      for ($i = 0; $i < sizeof($this->ip_array); $i++) {
        $r = db_query("select count(network) as cnt from `cleantalk_sfw` where network = " . $this->ip_array[$i] . " & mask;");
        $cnt = intval($r
          ->fetchObject()->cnt);
        if ($cnt > 0) {
          $this->result = true;
          $this->blocked_ip = $this->ip_str_array[$i];
        }
        else {
          $this->passed_ip = $this->ip_str_array[$i];
        }
      }
    }
    if ($this->passed_ip != '') {
      $key = variable_get('cleantalk_authkey', '');
      @setcookie('ct_sfw_pass_key', md5($this->passed_ip . $key), 0, "/");
    }
  }
  public function sfw_die() {
    $key = variable_get('cleantalk_authkey', '');
    $sfw_die_page = file_get_contents(dirname(__FILE__) . "/sfw_die_page.html");
    $sfw_die_page = str_replace("{REMOTE_ADDRESS}", $this->blocked_ip, $sfw_die_page);
    $sfw_die_page = str_replace("{REQUEST_URI}", $_SERVER['REQUEST_URI'], $sfw_die_page);
    $sfw_die_page = str_replace("{SFW_COOKIE}", md5($this->blocked_ip . $key), $sfw_die_page);
    @setcookie('ct_sfw_passed', '0');
    @header('HTTP/1.0 403 Forbidden');
    print $sfw_die_page;
    die;
  }
  public static function sfw_update($ct_key) {
    $data = array(
      'auth_key' => $ct_key,
      'method_name' => '2s_blacklists_db',
    );
    $result = sendRawRequest('https://api.cleantalk.org/2.1', $data, false);
    $result = json_decode($result, true);
    if (isset($result['data'])) {
      if (count($result['data']) > 30) {
        db_truncate('cleantalk_sfw')
          ->execute();
        $result = $result['data'];
        $query = "INSERT INTO `cleantalk_sfw` VALUES ";
        for ($i = 0; $i < sizeof($result); $i++) {
          if ($i == sizeof($result) - 1) {
            $query .= "(" . $result[$i][0] . "," . $result[$i][1] . ");";
          }
          else {
            $query .= "(" . $result[$i][0] . "," . $result[$i][1] . "), ";
          }
        }
        $result = db_query($query);
      }
      variable_set('ct_sfw_last_updated', time());
    }
  }

  //Add entries to SFW log
  public static function sfw_update_logs($ip, $result) {
    if ($ip === NULL || $result === NULL) {
      error_log('SFW log update failed');
      return;
    }
    $blocked = $result == 'blocked' ? ' + 1' : '';
    $time = time();
    $query = "INSERT INTO `cleantalk_sfw_logs`\n\t\tSET \n\t\t\t`ip` = '{$ip}',\n\t\t\t`all` = 1,\n\t\t\t`blocked` = 1,\n\t\t\t`timestamp` = '" . $time . "'\n\t\tON DUPLICATE KEY \n\t\tUPDATE \n\t\t\t`all` = `all` + 1,\n\t\t\t`blocked` = `blocked`" . $blocked . ",\n\t\t\t`timestamp` = '" . $time . "'";
    $result = db_query($query);
  }

  //Send and wipe SFW log
  public static function send_logs($ct_key) {

    //Getting logs
    $tmp = db_query("SELECT * FROM `cleantalk_sfw_logs`");
    $result = $tmp
      ->fetchAll(PDO::FETCH_ASSOC);
    if (!empty($result)) {

      //Compile logs
      $data = array();
      foreach ($result as $key => $value) {
        $data[] = array(
          trim($value['ip']),
          $value['all'],
          $value['blocked'],
          $value['timestamp'],
        );
      }
      unset($key, $value, $result);

      //Final compile
      $qdata = array(
        'data' => json_encode($data),
        'rows' => count($data),
        'timestamp' => time(),
      );

      //Sendings request
      $result = sendRawRequest('https://api.cleantalk.org/?method_name=sfw_logs&auth_key=' . $ct_key, $qdata, false);
      $result = json_decode($result);

      //Checking answer and truncate table, reset counter
      if (isset($result->data) && isset($result->data->rows)) {
        if ($result->data->rows == count($data)) {
          db_query("TRUNCATE TABLE `cleantalk_sfw_logs`");
          variable_set('ct_sfw_last_logs_sent', time());
        }
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CleanTalkSFW::$blocked_ip public property
CleanTalkSFW::$ip public property
CleanTalkSFW::$ip_array public property
CleanTalkSFW::$ip_str public property
CleanTalkSFW::$ip_str_array public property
CleanTalkSFW::$passed_ip public property
CleanTalkSFW::$result public property
CleanTalkSFW::check_ip public function
CleanTalkSFW::cleantalk_get_real_ip public function
CleanTalkSFW::send_logs public static function
CleanTalkSFW::sfw_die public function
CleanTalkSFW::sfw_update public static function
CleanTalkSFW::sfw_update_logs public static function