$IpCnt = 0;
if (ValidIp($IP)) {
    $IpCnt = $Db->ReturnValue("SELECT COUNT(*) FROM " . PFX . "_tracker_ip WHERE IP = '{$IP}'");
    if ($IpCnt == 1) {
        $Query = "\n\t\t\tSELECT V.ID \n\t\t\t\tFROM " . PFX . "_tracker_ip I\n\t\t\t\tINNER JOIN " . PFX . "_tracker_visitor V\n\t\t\t\t\tON V.LAST_IP_ID=I.ID\n\t\t\t\tWHERE IP = '{$IP}' \n\t\t\t\tORDER BY V.ID DESC";
        $VisId = $Db->ReturnValue($Query);
        $nsProduct->Redir("visitor_path", "VisId={$VisId}&SiteId={$SiteId}&CpId={$CpId}", "report");
    }
    if ($IpCnt > 1) {
        $nsProduct->Redir("visitor_path", "IP={$IP}&SiteId={$SiteId}&CpId={$CpId}", "report");
    }
    if ($IpCnt == 0) {
        $Logs->Msg(str_replace("{IP}", $IP, $Lang['NoSuchIp']));
    }
}
if (ValidVar($IP) && !ValidIp($IP)) {
    $Logs->Err($Lang['IpErr']);
}
$SitesArr = array();
if (!ValidId($SiteId) && !ValidId($CpId)) {
    $nsProduct->Redir("default");
}
if (ValidId($CpId)) {
    $Query = "SELECT * FROM " . PFX . "_tracker_client WHERE ID = {$CpId}";
    $Comp = $Db->Select($Query);
    $PageTitle = $Comp->NAME;
    $Query = "SELECT ID, HOST FROM " . PFX . "_tracker_site WHERE COMPANY_ID={$CpId}";
    $Sql = new Query($Query);
    while ($Row = $Sql->Row()) {
        $SitesArr[] = $Row;
    }
if ($AllSites && ValidId($CpId)) {
    $SiteId = false;
}
if ($AllSites && !ValidId($CpId)) {
    $CpId = $Db->ReturnValue("SELECT COMPANY_ID FROM " . PFX . "_tracker_site WHERE ID = {$SiteId}");
    $SiteId = false;
}
$HostsArr = array();
$MenuSection = "logs";
$IpCnt = 0;
if ($IP) {
    $Filter = $IP;
    $FilterFor = "IP";
}
if ($Filter && $FilterFor == "IP") {
    if (!ValidIp($Filter)) {
        $Logs->Err($Lang['InvalidIP']);
        $IP = false;
        $Filter = false;
    } else {
        $IP = $Filter;
        $Query = "\n\t\t\tSELECT COUNT(V.ID) FROM " . PFX . "_tracker_visitor V\n\t\t\t\tINNER JOIN " . PFX . "_tracker_ip I\n\t\t\t\t\tON I.ID=V.LAST_IP_ID\n\t\t\t\tWHERE I.IP = '{$IP}'\n\t\t";
        $IpCnt = $Db->ReturnValue($Query);
        if ($IpCnt == 1) {
            $Query = "\n\t\t\t\tSELECT V.ID FROM " . PFX . "_tracker_visitor V \n\t\t\t\tINNER JOIN " . PFX . "_tracker_ip I\n\t\t\t\t\tON I.ID=V.LAST_IP_ID\n\t\t\t\tWHERE I.IP = '{$IP}' ORDER BY ID DESC\n\t\t\t";
            $VisId = $Db->ReturnValue($Query);
        } else {
            $IpId = $Db->ReturnValue("SELECT ID FROM " . PFX . "_tracker_ip WHERE IP = '{$IP}'");
        }
    }
}
$MenuSection = "admin";
$NewIp = ValidVar($_GP['NewIp']);
$NewIpDescr = ValidVar($_GP['NewIpDescr']);
if ($NewIp && !ValidIP($NewIp) && !ValidIpTempl($NewIp)) {
    $Logs->Err($Lang['WrongIp']);
    $NewIp = false;
}
$RemoveIP = ValidVar($_GP['RemoveIP']);
if (!ValidArr($RemoveIp)) {
    $RemoveIp = false;
}
$MyIP = $_SERVER['REMOTE_ADDR'];
/////////////////////////////////////////////
///////// call any process functions
if (!$nsUser->DEMO) {
    if ($NewIp && ValidIp($NewIp)) {
        AddNewIp($NewIp, $NewIpDescr);
    }
    if ($NewIp && ValidIpTempl($NewIp)) {
        AddNewIpTempl($NewIp, $NewIpDescr);
    }
    if ($RemoveIP) {
        RemoveIp($RemoveIP);
    }
}
/////////////////////////////////////////////
///////// display section here
$IpList = array();
$Query = "SELECT * FROM " . PFX . "_tracker_ip WHERE IGNORED = '1' ORDER BY IP ASC";
$Sql = new Query($Query);
$Sql->ReadSkinConfig();
$FormClicked = ValidVar($_GP['FormClicked']) ? true : false;
$DeleteId = ValidVar($_GP['DeleteId']) ? $_GP['DeleteId'] : false;
$UserOrder = ValidVar($_GET['UserOrder'], false);
$UserOrder = PrepareUserOrder($nsUser->DEF_LOGS_ORDER, $UserOrder, $Mode);
$ProgPath = array();
$ProgPath[0]['Name'] = $Lang['MLogs'];
$ProgPath[0]['Url'] = getUrl("reports", "CpId={$CpId}&SiteId={$SiteId}", "admin");
$Settings = GetSettings();
$AllSites = !ValidId($SiteId);
$MenuSection = "logs";
if (!$ViewDate && !$StartDate && !$EndDate && !$FormClicked) {
    $ViewDate = UserDate();
}
$StartDay = false;
$EndDay = false;
if ($FilterFor == "IP" && !ValidIp($Filter)) {
    $Filter = false;
    $Logs->Err($Lang['IpErr']);
}
if ($Mode == "Hits") {
    $PageTitle = $Lang['LogHits'];
}
if ($Mode == "Clicks") {
    $PageTitle = $Lang['LogClicks'];
}
if ($Mode == "Actions") {
    $PageTitle = $Lang['LogActions'];
}
if ($Mode == "Sales") {
    $PageTitle = $Lang['LogSales'];
}