<?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");
}

@set_time_limit(600);
if(!$ab_config['page_delay'] OR $ab_config['page_delay'] < 1) { $pagedelay = 5; } else { $pagedelay = $ab_config['page_delay']; }
$perpage = 50;
$totalselected = $db->sql_numrows($db->sql_query("SELECT `ip_lo` FROM `".$prefix."_nsnst_ip2country`"));
if (!isset($min)) {
  $min=0;
  $pagesint = ($totalselected / $perpage);
  $pages = ceil($pagesint);
  $pagetitle = _AB_NUKESENTINEL.": "._AB_IP2COVERLAPCHECK;
  incoh();
  title($pagetitle);
  Max_OpenTable();
  OpenMenu();
  ipbanmenu();
  CarryMenu();
  ip2cmenu();
  CloseMenu();
  Max_CloseTable();
  br('1');
  Max_OpenTable();
  echo _AB_IP2COVERLAPCHECK01."<br />\n";
  echo _AB_IP2COVERLAPCHECK02."<br />\n";
  echo _AB_INSECTIONS."<br />\n";
  echo _AB_YOUHAVE."$pages"._AB_SECTIONSTOGO."<br />\n";
  echo "<br />\n";
  echo "<form method='post' action='".$admin_file.".php?op=ABIP2CountryOverlapCheck'>\n";
  echo "<input type='hidden' name='min' value='$min'>\n";
  echo "<input type='submit' value=\""._AB_LETSGETSTART."\">\n";
  echo "</form>\n";
  Max_CloseTable();
  incof();
} else if($min < $totalselected) {

  $max=$min+$perpage;
  $pagesint = ($totalselected / $perpage);
  $pages = ceil($pagesint);
  $currentpage = ($max / $perpage);
  $pagetitle = _AB_NUKESENTINEL.": "._AB_IP2COVERLAPCHECK;
  incoh();
  title($pagetitle);
  Max_OpenTable();
  $testmessage = "";
  $result = $db->sql_query("SELECT `ip_lo`, `ip_hi` FROM `".$prefix."_nsnst_ip2country` ORDER BY ip_lo LIMIT $min, $perpage");
  while(list($xip_lo, $xip_hi) = $db->sql_fetchrow($result)) {
    $test1 = $db->sql_query("SELECT * FROM ".$prefix."_nsnst_ip2country WHERE (ip_lo<='$xip_lo' AND ip_hi>='$xip_lo') AND (`ip_lo`!='$xip_lo' AND `ip_hi`!='$xip_hi') ORDER BY `ip_lo`");
    $test2 = $db->sql_query("SELECT * FROM ".$prefix."_nsnst_ip2country WHERE (ip_lo<='$xip_hi' AND ip_hi>='$xip_hi') AND (`ip_lo`!='$xip_lo' AND `ip_hi`!='$xip_hi') ORDER BY `ip_lo`");
    $test3 = $db->sql_query("SELECT * FROM ".$prefix."_nsnst_ip2country WHERE (ip_lo>='$xip_lo' AND ip_hi<='$xip_hi') AND (`ip_lo`!='$xip_lo' AND `ip_hi`!='$xip_hi') ORDER BY `ip_lo`");
    $test4 = $db->sql_query("SELECT * FROM ".$prefix."_nsnst_ip2country WHERE (ip_lo<='$xip_lo' AND ip_hi>='$xip_hi') AND (`ip_lo`!='$xip_lo' AND `ip_hi`!='$xip_hi') ORDER BY `ip_lo`");
    $testnum1 = $db->sql_numrows($test1);
    $testnum2 = $db->sql_numrows($test2);
    $testnum3 = $db->sql_numrows($test3);
    $testnum4 = $db->sql_numrows($test4);
    if($testnum1 > 0) {
      $testmessage .= "<table align='center' border='0' cellpadding='2' cellspacing='2' bgcolor='$bgcolor2' width='100%'>\n";
      $testmessage .= "<tr bgcolor='$bgcolor1'><td align='center' colspan='6'><b>".long2ip($xip_lo)." "._AB_IN.":</b></td></tr>\n";
      $testmessage .= "<tr bgcolor='$bgcolor2'>\n";
      $testmessage .= "<td width='15%'><b>"._AB_IPLO."</b></td>\n";
      $testmessage .= "<td width='15%'><b>"._AB_IPHI."</b></td>\n";
      $testmessage .= "<td align='center' width='10%'><b>"._AB_FLAG."</b></td>\n";
      $testmessage .= "<td align='center' width='10%'><b>"._AB_CODE."</b></td>\n";
      $testmessage .= "<td align='center' width='35%'><b>"._AB_COUNTRY."</b></td>\n";
      $testmessage .= "<td align='center' width='15%'><b>"._AB_FUNCTIONS."</b></td>\n";
      $testmessage .= "</tr>\n";
      while($testrow1 = $db->sql_fetchrow($test1)) {
        $testrow1['ip_lo_ip'] = long2ip($testrow1['ip_lo']);
        $testrow1['ip_hi_ip'] = long2ip($testrow1['ip_hi']);
        $testrow1['c2c'] = strtoupper($testrow1['c2c']);
        $testrow1['flag_img'] = flag_img($testrow1['c2c']);
        $testmessage .= "<tr onmouseover=\"this.style.backgroundColor='$bgcolor2'\" onmouseout=\"this.style.backgroundColor='$bgcolor1'\" bgcolor='$bgcolor1'>\n";
        $testmessage .= "<td><a href=\"".$ab_config['lookup_link'].$testrow1['ip_lo_ip']."\" target=\"_blank\">".$testrow1['ip_lo_ip']."</a></td>\n";
        $testmessage .= "<td><a href=\"".$ab_config['lookup_link'].$testrow1['ip_hi_ip']."\" target=\"_blank\">".$testrow1['ip_hi_ip']."</a></td>\n";
        $testmessage .= "<td align='center'>".$testrow1['flag_img']."</td>\n";
        $testmessage .= "<td align='center'>".$testrow1['c2c']."</td>\n";
        $testmessage .= "<td align='center'>".$testrow1['country']."</td>\n";
        $testmessage .= "<td align='center'>&nbsp;<a href='".$admin_file.".php?op=ABIP2CountryEdit&amp;ip_lo=".$testrow1['ip_lo']."&amp;ip_hi=".$testrow1['ip_hi']."&amp;xop=ABMain' target='_blank'><img src='images/nukesentinel/edit.png' border='0' alt='"._AB_EDIT."' title='"._AB_EDIT."' height='16' width='16'></a>&nbsp;<a ";
        $testmessage .= "href='".$admin_file.".php?op=ABIP2CountryDelete&amp;ip_lo=".$testrow1['ip_lo']."&amp;ip_hi=".$testrow1['ip_hi']."&amp;xop=ABMain' target='_blank'><img src='images/nukesentinel/delete.png' border='0' alt='"._AB_DELETE."' title='"._AB_DELETE."' height='16' width='16'></a>&nbsp;</td>\n";
        $testmessage .= "</tr>\n";
      }
      $testmessage .= "</table>\n";
      $testmessage .= "<br />\n";
    }
    if($testnum2 > 0) {
      $testmessage .= "<table align='center' border='0' cellpadding='2' cellspacing='2' bgcolor='$bgcolor2' width='100%'>\n";
      $testmessage .= "<tr bgcolor='$bgcolor1'><td align='center' colspan='6'><b>".long2ip($xip_hi)." "._AB_IN.":</b></td></tr>\n";
      $testmessage .= "<tr bgcolor='$bgcolor2'>\n";
      $testmessage .= "<td width='15%'><b>"._AB_IPLO."</b></td>\n";
      $testmessage .= "<td width='15%'><b>"._AB_IPHI."</b></td>\n";
      $testmessage .= "<td align='center' width='10%'><b>"._AB_FLAG."</b></td>\n";
      $testmessage .= "<td align='center' width='10%'><b>"._AB_CODE."</b></td>\n";
      $testmessage .= "<td align='center' width='35%'><b>"._AB_COUNTRY."</b></td>\n";
      $testmessage .= "<td align='center' width='15%'><b>"._AB_FUNCTIONS."</b></td>\n";
      $testmessage .= "</tr>\n";
      while($testrow2 = $db->sql_fetchrow($test2)) {
        $testrow2['ip_lo_ip'] = long2ip($testrow2['ip_lo']);
        $testrow2['ip_hi_ip'] = long2ip($testrow2['ip_hi']);
        $testrow2['c2c'] = strtoupper($testrow2['c2c']);
        $testrow2['flag_img'] = flag_img($testrow2['c2c']);
        $testmessage .= "<tr onmouseover=\"this.style.backgroundColor='$bgcolor2'\" onmouseout=\"this.style.backgroundColor='$bgcolor1'\" bgcolor='$bgcolor1'>\n";
        $testmessage .= "<td><a href=\"".$ab_config['lookup_link'].$testrow2['ip_lo_ip']."\" target=\"_blank\">".$testrow2['ip_lo_ip']."</a></td>\n";
        $testmessage .= "<td><a href=\"".$ab_config['lookup_link'].$testrow2['ip_hi_ip']."\" target=\"_blank\">".$testrow2['ip_hi_ip']."</a></td>\n";
        $testmessage .= "<td align='center'>".$testrow2['flag_img']."</td>\n";
        $testmessage .= "<td align='center'>".$testrow2['c2c']."</td>\n";
        $testmessage .= "<td align='center'>".$testrow2['country']."</td>\n";
        $testmessage .= "<td align='center'>&nbsp;<a href='".$admin_file.".php?op=ABIP2CountryEdit&amp;ip_lo=".$testrow2['ip_lo']."&amp;ip_hi=".$testrow2['ip_hi']."&amp;xop=ABMain' target='_blank'><img src='images/nukesentinel/edit.png' border='0' alt='"._AB_EDIT."' title='"._AB_EDIT."' height='16' width='16'></a>&nbsp;<a ";
        $testmessage .= "href='".$admin_file.".php?op=ABIP2CountryDelete&amp;ip_lo=".$testrow2['ip_lo']."&amp;ip_hi=".$testrow2['ip_hi']."&amp;xop=ABMain' target='_blank'><img src='images/nukesentinel/delete.png' border='0' alt='"._AB_DELETE."' title='"._AB_DELETE."' height='16' width='16'></a>&nbsp;</td>\n";
        $testmessage .= "</tr>\n";
      }
      $testmessage .= "</table>\n";
      $testmessage .= "<br />\n";
    }
    if($testnum3 > 0) {
      $testmessage .= "<table align='center' border='0' cellpadding='2' cellspacing='2' bgcolor='$bgcolor2' width='100%'>\n";
      $testmessage .= "<tr bgcolor='$bgcolor1'><td align='center' colspan='6'><b>".long2ip($xip_lo)." - ".long2ip($xip_hi)." "._AB_COVERS.":</b></td></tr>\n";
      $testmessage .= "<tr bgcolor='$bgcolor2'>\n";
      $testmessage .= "<td width='15%'><b>"._AB_IPLO."</b></td>\n";
      $testmessage .= "<td width='15%'><b>"._AB_IPHI."</b></td>\n";
      $testmessage .= "<td align='center' width='10%'><b>"._AB_FLAG."</b></td>\n";
      $testmessage .= "<td align='center' width='10%'><b>"._AB_CODE."</b></td>\n";
      $testmessage .= "<td align='center' width='35%'><b>"._AB_COUNTRY."</b></td>\n";
      $testmessage .= "<td align='center' width='15%'><b>"._AB_FUNCTIONS."</b></td>\n";
      $testmessage .= "</tr>\n";
      while($testrow3 = $db->sql_fetchrow($test3)) {
        $testrow3['ip_lo_ip'] = long2ip($testrow3['ip_lo']);
        $testrow3['ip_hi_ip'] = long2ip($testrow3['ip_hi']);
        $testrow3['c2c'] = strtoupper($testrow3['c2c']);
        $testrow3['flag_img'] = flag_img($testrow3['c2c']);
        $testmessage .= "<tr onmouseover=\"this.style.backgroundColor='$bgcolor2'\" onmouseout=\"this.style.backgroundColor='$bgcolor1'\" bgcolor='$bgcolor1'>\n";
        $testmessage .= "<td><a href=\"".$ab_config['lookup_link'].$testrow3['ip_lo_ip']."\" target=\"_blank\">".$testrow3['ip_lo_ip']."</a></td>\n";
        $testmessage .= "<td><a href=\"".$ab_config['lookup_link'].$testrow3['ip_hi_ip']."\" target=\"_blank\">".$testrow3['ip_hi_ip']."</a></td>\n";
        $testmessage .= "<td align='center'>".$testrow3['flag_img']."</td>\n";
        $testmessage .= "<td align='center'>".$testrow3['c2c']."</td>\n";
        $testmessage .= "<td align='center'>".$testrow3['country']."</td>\n";
        $testmessage .= "<td align='center'>&nbsp;<a href='".$admin_file.".php?op=ABIP2CountryEdit&amp;ip_lo=".$testrow3['ip_lo']."&amp;ip_hi=".$testrow3['ip_hi']."&amp;xop=ABMain' target='_blank'><img src='images/nukesentinel/edit.png' border='0' alt='"._AB_EDIT."' title='"._AB_EDIT."' height='16' width='16'></a>&nbsp;<a ";
        $testmessage .= "href='".$admin_file.".php?op=ABIP2CountryDelete&amp;ip_lo=".$testrow3['ip_lo']."&amp;ip_hi=".$testrow3['ip_hi']."&amp;xop=ABMain' target='_blank'><img src='images/nukesentinel/delete.png' border='0' alt='"._AB_DELETE."' title='"._AB_DELETE."' height='16' width='16'></a>&nbsp;</td>\n";
        $testmessage .= "</tr>\n";
      }
      $testmessage .= "</table>\n";
      $testmessage .= "<br />\n";
    }
    if($testnum4 > 0) {
      $testmessage .= "<table align='center' border='0' cellpadding='2' cellspacing='2' bgcolor='$bgcolor2' width='100%'>\n";
      $testmessage .= "<tr bgcolor='$bgcolor1'><td align='center' colspan='6'><b>".long2ip($xip_lo)." - ".long2ip($xip_hi)." "._AB_ISCOVERED.":</b></td></tr>\n";
      $testmessage .= "<tr bgcolor='$bgcolor2'>\n";
      $testmessage .= "<td width='15%'><b>"._AB_IPLO."</b></td>\n";
      $testmessage .= "<td width='15%'><b>"._AB_IPHI."</b></td>\n";
      $testmessage .= "<td align='center' width='10%'><b>"._AB_FLAG."</b></td>\n";
      $testmessage .= "<td align='center' width='10%'><b>"._AB_CODE."</b></td>\n";
      $testmessage .= "<td align='center' width='35%'><b>"._AB_COUNTRY."</b></td>\n";
      $testmessage .= "<td align='center' width='15%'><b>"._AB_FUNCTIONS."</b></td>\n";
      $testmessage .= "</tr>\n";
      while($testrow4 = $db->sql_fetchrow($test4)) {
        $testrow4['ip_lo_ip'] = long2ip($testrow4['ip_lo']);
        $testrow4['ip_hi_ip'] = long2ip($testrow4['ip_hi']);
        $testrow4['c2c'] = strtoupper($testrow4['c2c']);
        $testrow4['flag_img'] = flag_img($testrow4['c2c']);
        $testmessage .= "<tr onmouseover=\"this.style.backgroundColor='$bgcolor2'\" onmouseout=\"this.style.backgroundColor='$bgcolor1'\" bgcolor='$bgcolor1'>\n";
        $testmessage .= "<td><a href=\"".$ab_config['lookup_link'].$testrow4['ip_lo_ip']."\" target=\"_blank\">".$testrow4['ip_lo_ip']."</a></td>\n";
        $testmessage .= "<td><a href=\"".$ab_config['lookup_link'].$testrow4['ip_hi_ip']."\" target=\"_blank\">".$testrow4['ip_hi_ip']."</a></td>\n";
        $testmessage .= "<td align='center'>".$testrow4['flag_img']."</td>\n";
        $testmessage .= "<td align='center'>".$testrow4['c2c']."</td>\n";
        $testmessage .= "<td align='center'>".$testrow4['country']."</td>\n";
        $testmessage .= "<td align='center'>&nbsp;<a href='".$admin_file.".php?op=ABIP2CountryEdit&amp;ip_lo=".$testrow4['ip_lo']."&amp;ip_hi=".$testrow4['ip_hi']."&amp;xop=ABMain' target='_blank'><img src='images/nukesentinel/edit.png' border='0' alt='"._AB_EDIT."' title='"._AB_EDIT."' height='16' width='16'></a>&nbsp;<a ";
        $testmessage .= "href='".$admin_file.".php?op=ABIP2CountryDelete&amp;ip_lo=".$testrow4['ip_lo']."&amp;ip_hi=".$testrow4['ip_hi']."&amp;xop=ABMain' target='_blank'><img src='images/nukesentinel/delete.png' border='0' alt='"._AB_DELETE."' title='"._AB_DELETE."' height='16' width='16'></a>&nbsp;</td>\n";
        $testmessage .= "</tr>\n";
      }
      $testmessage .= "</table>\n";
      $testmessage .= "<br />\n";
    }
  }
  $pdone = round(($currentpage / $pages) * 100, 2);
  if($testmessage != "") {
    echo "<b>"._AB_SECTION." $currentpage "._AB_OF." $pages (".$pdone."%)</b><br />\n";
    echo "$testmessage";
    echo "<table align='center' border='0' cellpadding='2' cellspacing='2'>\n";
    echo "<tr>\n";
    echo "<form action='".$admin_file.".php?op=ABIP2CountryOverlapCheck' method='post'>\n";
    echo "<td>\n";
    echo "<input type='hidden' name='min' value='$min'>\n";
    echo "<input type='submit' value='"._AB_RECHECKSECTION." ".$currentpage."'>\n";
    echo "</td>\n";
    echo "</form>\n";
    if($currentpage < $pages) {
      echo "<form action='".$admin_file.".php?op=ABIP2CountryOverlapCheck' method='post'>\n";
      echo "<td>\n";
      echo "<input type='hidden' name='min' value='$max'>\n";
      echo "<input type='submit' value='"._AB_CHECKSECTION." ".($currentpage+1)."'>\n";
      echo "</td>\n";
      echo "</form>\n";
    } else {
      echo "<form action='".$admin_file.".php?op=ABBlockedOverlapCheck' method='post'>\n";
      echo "<td>\n";
      echo "<input type='hidden' name='min' value='$max'>\n";
      echo "<input type='submit' value='"._AB_FINISHCHECK."'>\n";
      echo "</td>\n";
      echo "</form>\n";
    }
    echo "</tr>\n";
    echo "</table>\n";
  } else {
    echo "<META HTTP-EQUIV=\"refresh\" content=\"$pagedelay;URL=".$admin_file.".php?op=ABIP2CountryOverlapCheck&min=$max\">";
    echo "<b>"._AB_SECTION." $currentpage "._AB_OF." $pages (".$pdone."%) "._AB_COMPLETED."</b><br />\n";
    echo "<b>No conflicts were found in this section</b><br />\n";
    if($currentpage < $pages) {
      echo "<b>"._AB_SECTION." ".($currentpage+1)." "._AB_OF." $pages "._AB_WILLSTART."</b><br />\n";
    }
  }
  Max_CloseTable();
  incof();

} else {
  $pagetitle = _AB_NUKESENTINEL.": "._AB_IP2COVERLAPCHECK;
  incoh();
  title($pagetitle);
  Max_OpenTable();
  OpenMenu();
  ipbanmenu();
  CarryMenu();
  ip2cmenu();
  CloseMenu();
  Max_CloseTable();
  br('1');
  Max_OpenTable();
  echo "<center><b>"._AB_IP2COVERLAPCHECK." "._AB_COMPLETED."</b><br />\n";
  Max_CloseTable();
  incof();
}
