<?php

defined('MAXIMUS') or die ('Access denied write ban MAXIMUS directly...');

function write_ban($banip, $htip, $blocker_row) {
  global $ab_config, $prefix, $admin, $nsnst_const;
  if(isset($admin) && !empty($admin)) {
    if(!is_array($admin)) {
      $abadmin = base64_decode($admin);
      $abadmin = explode(":", $abadmin);
      $a_aid = "$abadmin[0]";
    } else {
      $a_aid = "$admin[0]";
    }
  }
  $admin_row = abget_admin($a_aid);
  if((!isset($admin) || empty($admin)) || $admin_row['protected'] < 1) {
    if($blocker_row['activate'] > 3 AND $blocker_row['activate'] < 6) {
      if($blocker_row['duration'] > 0) {
        $abexpires = $blocker_row['duration'] + $nsnst_const['ban_time'];
      } else {
        $abexpires = 0;
      }
      if(!empty($nsnst_const['query_string'])) {
        $query_url = $nsnst_const['http_host'].$nsnst_const['script_name']."?".$nsnst_const['query_string'];
      } else {
        $query_url = $nsnst_const['http_host'].$nsnst_const['script_name'];
      }
      if(!empty($nsnst_const['get_string'])) {
        $get_url = $nsnst_const['http_host'].$nsnst_const['script_name']."?".$nsnst_const['get_string'];
      } else {
        $get_url = $nsnst_const['http_host'].$nsnst_const['script_name'];
      }
      if(!empty($nsnst_const['post_string'])) {
        $post_url = $nsnst_const['http_host'].$nsnst_const['script_name']."?".$nsnst_const['post_string'];
      } else {
        $post_url = $nsnst_const['http_host'].$nsnst_const['script_name'];
      }
      $addby = _AB_ADDBY." "._AB_NUKESENTINEL;
      $querystring = base64_encode($query_url);
      $getstring = base64_encode($get_url);
      $poststring = base64_encode($post_url);
      $checkrow = $GLOBALS['db']->sql_numrows($GLOBALS['db']->sql_query("SELECT * FROM `".$prefix."_nsnst_ip2country`"));
      if($checkrow > 0) {
        $remote_long = sprintf("%u", ip2long($nsnst_const['remote_ip']));
        $rowy = $GLOBALS['db']->sql_fetchrow($GLOBALS['db']->sql_query("SELECT `c2c` FROM `".$prefix."_nsnst_ip2country` WHERE `ip_lo`<='$remote_long' AND `ip_hi`>='$remote_long'"));
        $c2c = $rowy['c2c'];
      }
      if(!$c2c) { $c2c = "00"; }
      if (!get_magic_quotes_runtime()) {
        $addby = addslashes($addby);
        $ban_username = addslashes($nsnst_const['ban_username']);
        $user_agent = addslashes($nsnst_const['user_agent']);
      }
      $GLOBALS['db']->sql_query("INSERT INTO `".TABLE_NSNST_BLOCKED_IPS."` VALUES ('$banip', '".$nsnst_const['ban_user_id']."', '$ban_username', '$user_agent', '".$nsnst_const['ban_time']."', '$addby', '".$blocker_row['blocker']."', '$querystring', '$getstring', '$poststring', '".$nsnst_const['forward_ip']."', '".$nsnst_const['client_ip']."', '".$nsnst_const['remote_addr']."', '".$nsnst_const['remote_port']."', '".$nsnst_const['request_method']."', '$abexpires', '$c2c')");
      if (!empty($ab_config['htaccess_path']) AND $blocker_row['htaccess'] > 0 AND file_exists($ab_config['htaccess_path'])) {
        $ipfile = file($ab_config['htaccess_path']);
        $ipfile = implode("", $ipfile);
        if(!stristr($ipfile, $htip)) {
          $doit = @fopen($ab_config['htaccess_path'], "a");
          @fwrite($doit, $htip);
          @fclose($doit);
        }
      }
    }
  }
}