	<?php session_start();
	//check if the user is logged in
	if (isset($_SESSION['loggedin']) && $_SESSION['loggedin'] == true) {
	} else {
	header('Location: index.php');
	}
	date_default_timezone_set("Africa/Nairobi");
	?>
	<!DOCTYPE html>
	<html lang="en">
	<head>
	<meta charset="utf-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<meta name="description" content="">
	<meta name="author" content="">
	<link rel="icon" href="../images/favicon.png">

	<title>Deadly Force::People killed by the police in Kenya</title>

	<!-- Bootstrap core CSS -->
	<link href="../css/bootstrap.css" rel="stylesheet">
	<!-- Bootstrap theme -->
	<link href="../css/bootswatch_min.css" rel="stylesheet">

	<!-- Custom styles for this template -->
	<link href="../theme.css" rel="stylesheet">

	<!-- Just for debugging purposes. Don't actually copy these 2 lines! -->
	<!--[if lt IE 9]><script src="../../assets/js/ie8-responsive-file-warning.js"></script><![endif]-->
	<script src="../../assets/js/ie-emulation-modes-warning.js"></script>

	<!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
	<!--[if lt IE 9]>
	<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
	<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
	<![endif]-->
	
	</head>

	<body role="document">

	<!-- Fixed navbar -->
	<div class="navbar navbar-default navbar-fixed-top" role="navigation">
	<div class="container">
	<div class="navbar-header">
	<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target=".navbar-collapse">
	<span class="sr-only">Toggle navigation</span>
	<span class="icon-bar"></span>
	<span class="icon-bar"></span>
	<span class="icon-bar"></span>
	</button>
	<a class="navbar-brand" href="index.php">
	<img src="../images/newsplex_logo.png" width="145" height="70" alt="nation newsplex"/>
	<div class="header-slogan">
	<span class="big-slogan">Deadly force</span><br>
	<span class="slogan">People killed by the police in Kenya</span>
	</div>
	</a>
	</div>
	<div class="navbar-collapse collapse">
	<ul class="nav navbar-nav">
	<li><a href="../tip.php">Submit tip</a></li>
	<li><a href="../index.php#database">Database</a></li>
	<li><a href="../about.php">About</a></li>
	<li><a href="../articles.php">Read articles</a></li>
	</ul>
	</li>
	</ul>
	</div><!--/.nav-collapse -->
	</div>
	</div>

	<div class="container main">

	<!-- Forms
	================================================== -->
	<div class="bs-docs-section">
	<div class="row">
	<div class="col-lg-12">
	<h1>Add user</h1>
	</div>
	</div>

	<?php

	if(isset($_POST['submit']))
	{ 
	$servername = "localhost";
	$dbuser = "plexusrs";
	$dbpass = "sTUmTaQsSsPtRJy";
	$dbname = "newsplex_db";

	$names=trim($_POST['inputnames']);
	$username=trim($_POST['inputusername']);
	$email=trim($_POST['inputemail']);
	$password=trim($_POST['inputpassword']);
	$confirmpassword=trim($_POST['inputpassword2']);
	$access=trim($_POST['lstaccess']);
	//echo "names=".$names."<br>";
	//echo "username=".$username."<br>";
	//echo "email=".$email."<br>";
	//echo "password=".$password."<br>";
	//echo "confirm password=".$confirmpassword."<br>";
	//echo "access=".$access."<br>";
	//exit();

	//check for empty values
	if (empty($names) ||   empty($username) ||empty($email) ||empty($password) ||empty($confirmpassword) ||empty($access))
	{
	echo "<div class=\"bs-component\">";
	echo "<div class=\"alert alert-dismissable alert-warning\">";
	echo "<button type=\"button\" class=\"close\" data-dismiss=\"alert\">&times;</button>";
	echo "<strong>Go back and fill all empty fields.</strong>";
	echo "</div>";
	echo "</div>";
	exit();
	}

	// Password match
	if ($password != $confirmpassword)
	{
	echo "<div class=\"bs-component\">";
	echo "<div class=\"alert alert-dismissable alert-warning\">";
	echo "<button type=\"button\" class=\"close\" data-dismiss=\"alert\">&times;</button>";
	echo "<strong>Passwords do not match!.</strong>";
	echo "</div>";
	echo "</div>";
	exit();
	}

	// Email validation
	if (!filter_var($email, FILTER_VALIDATE_EMAIL))
	{
	echo "<div class=\"bs-component\">";
	echo "<div class=\"alert alert-dismissable alert-warning\">";
	echo "<button type=\"button\" class=\"close\" data-dismiss=\"alert\">&times;</button>";
	echo "<strong>Enter a valid email!.</strong>";
	echo "</div>";
	echo "</div>";
	exit();
	}

	// Password length
	if (strlen($password) <= 6){
	echo "<div class=\"bs-component\">";
	echo "<div class=\"alert alert-dismissable alert-warning\">";
	echo "<button type=\"button\" class=\"close\" data-dismiss=\"alert\">&times;</button>";
	echo "<strong>Choose a password longer than 6 characters!.</strong>";
	echo "</div>";
	echo "</div>";
	exit();
	}

	try {
	$conn = new PDO("mysql:host=$servername;dbname=$dbname", $dbuser, $dbpass);
	// set the PDO error mode to exception
	$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
	
	// escape variables for security
     $names = mysql_real_escape_string($names);
	 $username = mysql_real_escape_string($username);
	 $email = mysql_real_escape_string($email);
	 $password = mysql_real_escape_string($password);
	 $access = mysql_real_escape_string($access);

	//check if username exists in the database
	$sthandler = $conn->prepare("select username from police_killings_users where username = :name");
	$sthandler->bindParam(':name', $username);
	$sthandler->execute();

	if($sthandler->rowCount() > 0){
	echo "<div class=\"bs-component\">";
	echo "<div class=\"alert alert-dismissable alert-warning\">";
	echo "<button type=\"button\" class=\"close\" data-dismiss=\"alert\">&times;</button>";
	echo "<strong>Username already exists in the database!.</strong>";
	echo "</div>";
	echo "</div>";
	exit();
	}

	//check if email already exists
	$sthandler = $conn->prepare("select email from police_killings_users where email = :email");
	$sthandler->bindParam(':email', $email);
	$sthandler->execute();

	if($sthandler->rowCount() > 0){
	echo "<div class=\"bs-component\">";
	echo "<div class=\"alert alert-dismissable alert-warning\">";
	echo "<button type=\"button\" class=\"close\" data-dismiss=\"alert\">&times;</button>";
	echo "<strong>Email already exists in the database!.</strong>";
	echo "</div>";
	echo "</div>";
	exit();
	}
	//hash password using md5
	$password=md5($password);

	$sql = "insert into police_killings_users (names,username,email,password,access_level) values ('$names','$username','$email','$password','$access')";
	// use exec() because no results are returned
	$conn->exec($sql);
	echo "<div class=\"bs-component\">";
	echo "<div class=\"alert alert-dismissable alert-success\">";
	echo "<button type=\"button\" class=\"close\" data-dismiss=\"alert\">&times;</button>";
	echo "<strong>User successfully added.</strong>";
	echo "</div>";
	echo "</div>";
	}
	catch(PDOException $e)
	{
	echo $sql . "<br>" . $e->getMessage();
	}

	$conn = null;
	}
	if(isset($_POST['cancel']))
	{ 
	header('Location:index.php');
	}
	?> 	
	<div class="row">
	<div class="col-lg-12">
	<div class="well bs-component">
	<form class="form-horizontal" method="post" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>">
	<fieldset>
	<legend>Fill in the details below and submit</legend>
	<div class="form-group">
	<label for="inputnames" class="col-lg-2 control-label">Names:</label>
	<div class="col-lg-10">
	<input type="text" class="form-control" id="inputnames" name="inputnames" placeholder="First and last names" required data-validation-required-message=
    "You must agree to the terms and conditions">
	</div>
	</div>
	<div class="form-group">
	<label for="inputusername" class="col-lg-2 control-label">Username:</label>
	<div class="col-lg-10">
	<input type="text" class="form-control" id="inputusername" name="inputusername" placeholder="Username" required>
	</div>
	</div>

	<div class="form-group">
	<label for="inputemail" class="col-lg-2 control-label">Email:</label>
	<div class="col-lg-10">
	<input type="email" class="form-control" id="inputemail" name="inputemail" placeholder="Email" required>
	</div>
	</div>
	<div class="form-group">
	<label for="inputPassword" class="col-lg-2 control-label">Password:</label>
	<div class="col-lg-10">
	<input type="password" class="form-control" id="inputpassword" name="inputpassword" placeholder="Password" required>
	</div>
	</div>
	<div class="form-group">
	<label for="inputPassword2" class="col-lg-2 control-label">Repeat password:</label>
	<div class="col-lg-10">
	<input type="password" class="form-control" id="inputpassword2" name="inputpassword2" placeholder="Confirm password" required>
	</div>
	</div>

	<div class="form-group">
	<label for="select" class="col-lg-2 control-label">Access:</label>
	<div class="col-lg-10">
	<select class="form-control" id="lstaccess" name="lstaccess">
	<option>User</option>
	<option>Editor</option>
	</select>
	</div>
	</div>

	<div class="form-group">
	<div class="col-lg-10 col-lg-offset-2">
	<button type="submit" name="submit" id="submit" class="btn btn-primary">Submit</button>
	<button type="submit" name="cancel" id="cancel" class="btn btn-default">Cancel</button>
	<a class= "btn btn-warning" href="logout.php">Logout</a>

	</div>
	</div>
	</fieldset>
	</form>
	</div>
	</div>

	</div> <!-- /container -->


	<!-- Bootstrap core JavaScript
	================================================== -->
	<!-- Placed at the end of the document so the pages load faster -->
	<script src="../js/jquery.min.js"></script>
	<script src="../js/bootstrap.min.js"></script>
	<script src="../js/bootswatch.js"></script>
	
	<!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
	<script src="../js/ie10-viewport-bug-workaround.js"></script>
	</body>
	</html>
