<?php
/************************************************************************/
/* Maximus CMS: Web Portal System                                       */
/* ==============================                                       */
/*                                                                      */
/* Copyright (c) 2002 - 2006                                            */
/* http://www.php-maximus.fr                                            */
/* dev@php-maximus.org                                                  */
/*                                                                      */
/* This program is free software. You can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 2 of the License.       */
/************************************************************************/
/* Based on PHP-NUKE: Web Portal System                                 */
/************************************************************************/
/********************************************************/
/* NukeSentinel(tm)                                     */
/* By: NukeScripts Network (webmaster@nukescripts.net)  */
/* http://www.nukescripts.net                           */
/* Copyright © 2000-2005 by NukeScripts Network         */
/********************************************************/

if (!defined('ADMIN_ALLOW') && !defined('ADMIN_MAX')) {
	die ("Access Denied");
}

if(($xip_lo[0] < 0 OR $xip_lo[0] > 255 OR !is_numeric($xip_lo[0])) OR ($xip_lo[1] < 0 OR $xip_lo[1] > 255 OR !is_numeric($xip_lo[1])) OR ($xip_lo[2] < 0 OR $xip_lo[2] > 255 OR !is_numeric($xip_lo[2])) OR ($xip_lo[3] < 0 OR $xip_lo[3] > 255 OR !is_numeric($xip_lo[3]))) {
  $pagetitle = _AB_NUKESENTINEL.": "._AB_ADDRANGEERROR;
  incoh();
  title($pagetitle);
  Max_OpenTable();
  echo "<br /><center><b>"._AB_LOERROR." </b></center>\n";
  echo "<br /><center><b>"._GOBACK."</b></center><br />\n";
  Max_CloseTable();
  incof();
  die();
}
$xip_lo = implode(".", $xip_lo);
$longip_lo = sprintf("%u", ip2long($xip_lo));
if(($xip_hi[0] < 0 OR $xip_hi[0] > 255 OR !is_numeric($xip_hi[0])) OR ($xip_hi[1] < 0 OR $xip_hi[1] > 255 OR !is_numeric($xip_hi[1])) OR ($xip_hi[2] < 0 OR $xip_hi[2] > 255 OR !is_numeric($xip_hi[2])) OR ($xip_hi[3] < 0 OR $xip_hi[3] > 255 OR !is_numeric($xip_hi[3]))) {
  $pagetitle = _AB_NUKESENTINEL.": "._AB_ADDRANGEERROR;
  incoh();
  title($pagetitle);
  Max_OpenTable();
  echo "<br /><center><b>"._AB_HIERROR." </b></center>\n";
  echo "<br /><center><b>"._GOBACK."</b></center><br />\n";
  Max_CloseTable();
  incof();
  die();
}
$xip_hi = implode(".", $xip_hi);
$longip_hi = sprintf("%u", ip2long($xip_hi));
$xnotes = str_replace("<br />","\r\n",$xnotes);
$xnotes = str_replace("<br />","\r\n",$xnotes);
$xnotes = htmlentities($xnotes, ENT_QUOTES);
if (!get_magic_quotes_runtime()) {
  $xnotes = addslashes($xnotes);
}
if($xexpires>0) { $xexpires = $bantime + ($xexpires * 86400); }
$test1 = $db->sql_query("SELECT * FROM `".TABLE_NSNST_BLOCKED_RANGES."` WHERE `ip_lo`<='$longip_lo' AND `ip_hi`>='$longip_lo'");
$test2 = $db->sql_query("SELECT * FROM `".TABLE_NSNST_BLOCKED_RANGES."` WHERE `ip_lo`<='$longip_hi' AND `ip_hi`>='$longip_hi'");
$test3 = $db->sql_query("SELECT * FROM `".TABLE_NSNST_BLOCKED_RANGES."` WHERE `ip_lo`>='$longip_lo' AND `ip_hi`<='$longip_hi'");
$test4 = $db->sql_query("SELECT * FROM `".TABLE_NSNST_BLOCKED_RANGES."` WHERE `ip_lo`<='$longip_lo' AND `ip_hi`>='$longip_hi'");
$testnum1 = $db->sql_numrows($test1);
$testnum2 = $db->sql_numrows($test2);
$testnum3 = $db->sql_numrows($test3);
$testnum4 = $db->sql_numrows($test4);
if($testnum1 > 0 OR $testnum2 >0 OR $testnum3 >0 OR $testnum4 >0) {
  $pagetitle = _AB_NUKESENTINEL.": "._AB_ADDRANGEERROR;
  incoh();
  title($pagetitle);
  Max_OpenTable();
  if($testnum1 > 0) {
    $testrow1 = $db->sql_fetchrow($test1);
    echo "<br /><center><b>$xip_lo: "._AB_IN." ".long2ip($testrow1['ip_lo'])." "._AB_TO." ".long2ip($testrow1['ip_hi'])." "._AB_FROM." ".$testrow1['country']."</b></center>\n";
  }
  if($testnum2 > 0) {
    $testrow2 = $db->sql_fetchrow($test2);
    echo "<br /><center><b>$xip_hi: "._AB_IN." ".long2ip($testrow2['ip_lo'])." "._AB_TO." ".long2ip($testrow2['ip_hi'])." "._AB_FROM." ".$testrow2['country']."</b></center>\n";
  }
  if($testnum3 > 0) {
    $testrow3 = $db->sql_fetchrow($test3);
    echo "<br /><center><b>$xip_lo - $xip_hi: "._AB_COVERS." ".long2ip($testrow3['ip_lo'])." "._AB_TO." ".long2ip($testrow3['ip_hi'])." "._AB_FROM." ".$testrow2['country']."</b></center>\n";
  }
  if($testnum4 > 0) {
    $testrow4 = $db->sql_fetchrow($test4);
    echo "<br /><center><b>$xip_lo - $xip_hi: "._AB_ISCOVERED." ".long2ip($testrow4['ip_lo'])." "._AB_TO." ".long2ip($testrow4['ip_hi'])." "._AB_FROM." ".$testrow2['country']."</b></center>\n";
  }
  echo "<br /><center><b>"._GOBACK."</b></center><br />\n";
  Max_CloseTable();
  incof();
} else {
  $xtime = time();
  $new_masscidr = ABGetCIDRs($longip_lo, $longip_hi);
  if ($ab_config['htaccess_path'] != "") {
    $new_masscidr = explode("||", $new_masscidr);
    for ($i=0; $i < sizeof($new_masscidr); $i++) {
      if ($new_masscidr[$i]!="") {
        $new_masscidr[$i] = "deny from ".$new_masscidr[$i]."\n";
      }
    }
    $new_masscidr = implode("", $new_masscidr);
    $doit = fopen($ab_config['htaccess_path'], "a");
    fwrite($doit, $new_masscidr);
    fclose($doit);
  }
  $db->sql_query("INSERT INTO `".TABLE_NSNST_BLOCKED_RANGES."` VALUES ('$longip_lo', '$longip_hi', '$xtime', '$xnotes', '$xreason', '$xexpires', '$xc2c')");
  if($another == 1) {
    redirect_url("".$admin_file.".php?op=ABBlockedRangeAdd");
  }else {
    redirect_url("".$admin_file.".php?op=ABBlockedRange");
  }
}