User login progress

This commit is contained in:
Daniel Mason 2017-01-01 22:06:48 +13:00
parent 4b01dc70ec
commit 0c6a5f3833
7 changed files with 158 additions and 51 deletions

View File

@ -24,6 +24,16 @@ html, body, #container {
margin: 0 auto 0 auto; margin: 0 auto 0 auto;
} }
#user {
position: absolute;
top: 0;
right: 0;
margin-right: 15px;
margin-top: 10px;
width: 250px;
text-align:right;
}
html { html {
color: #222; color: #222;
font-size: 1em; font-size: 1em;

View File

@ -2,12 +2,6 @@
class DataItem { class DataItem {
protected $_db;
function __construct() {
$this->_db = new db(); //Will optimise this to get existing conn at some point.
}
public static function getById($id) { public static function getById($id) {
$db = new db(); $db = new db();
$db->query("SELECT * FROM `".static::_getType()."` WHERE ".static::_getType()."_id = :id"); $db->query("SELECT * FROM `".static::_getType()."` WHERE ".static::_getType()."_id = :id");
@ -22,15 +16,14 @@ class DataItem {
return $obj; return $obj;
} }
private function _getAllWhere($where = false, $orderBy = false, $join = false, $limit = false) { public static function getAllWhere($where = false, $orderBy = false, $join = false, $limit = false) {
$Objarr = array(); $Objarr = [];
$typeName = static::_getType(); $db = new db();
$className = static::_getClass(); $db->query("SELECT * FROM `".static::_getType()."` ".($join?$join:"").($where?" WHERE ".$where."":"")." ".($orderBy?" ".$orderBy." ":" ").($limit?"LIMIT ".$limit:""));
$this->_db->query("SELECT * FROM `".$typeName."` ".($join?$join:"").($where?" WHERE ".$where."":"")." ".($orderBy?" ".$orderBy." ":" ").($limit?"LIMIT ".$limit:"")); $results = $db->resultset();
$results = $this->_db->resultset();
if(!$results) return false; if(!$results) return false;
foreach ($results as $result) { foreach ($results as $result) {
$obj = new $className(); $obj = new static();
foreach ($result as $key=>$val) { foreach ($result as $key=>$val) {
$obj->$key = $val; $obj->$key = $val;
} }
@ -39,30 +32,18 @@ class DataItem {
return $Objarr; return $Objarr;
} }
public static function getAllWhere($where = false, $orderBy = false, $join = false, $limit = false) { public static function getWhere($where = false, $orderBy = false, $join = false, $limit = false) {
$me = new static(); $db = new db();
return $me->_getAllWhere($where, $orderBy, $join, $limit); $db->query("SELECT * FROM `".static::_getType()."` ".($join?$join:"").($where?" WHERE ".$where."":"")." ".($orderBy?" ".$orderBy."":"").($limit?"LIMIT ".$limit:""));
} $result = $db->single();
private function _getWhere($where = false, $orderBy = false, $join = false, $limit = false) {
$typeName = static::_getType();
$className = static::_getClass();
$this->_db = new db();
$this->_db->query("SELECT * FROM `".$typeName."` ".($join?$join:"").($where?" WHERE ".$where."":"")." ".($orderBy?" ".$orderBy."":"").($limit?"LIMIT ".$limit:""));
$result = $this->_db->single();
if(!$result) return false; if(!$result) return false;
$obj = new $className(); $obj = new static();
foreach ($result as $key=>$val) { foreach ($result as $key=>$val) {
$obj->$key = $val; $obj->$key = $val;
} }
return $obj; return $obj;
} }
public static function getWhere($where = false, $orderBy = false, $join = false, $limit = false) {
$me = new static();
return $me->_getWhere($where, $orderBy, $join, $limit);
}
public function getId() { public function getId() {
$typeName = static::_getType(); $typeName = static::_getType();
$id = $typeName."_id"; $id = $typeName."_id";

View File

@ -0,0 +1,41 @@
<?php
class Result extends DataItem {
public static function _getClass()
{
return "Result";
}
public static function _getType()
{
return "result";
}
public function setResult($result)
{
$this->resultdata_result = json_encode($result);
}
public function getResult()
{
return json_decode($this->resultdata_result, true);
}
public function setUser($userId)
{
$this->resultdata_user = $userId;
}
public function save()
{
$db = new db();
$db->query("INSERT INTO result(result_time, resultdata_user, resultdata_result)
VALUES(:qTime, :qUser, :qContent)");
$db->bind("qTime", time());
$db->bind("qUser", $this->resultdata_user);
$db->bind("qContent", $this->resultdata_result);
return $db->execute();
}
}

View File

@ -14,25 +14,58 @@ class User extends DataItem {
return "user"; return "user";
} }
public static function loginOrRegister($data)
{
if(isset($data['register'])) {
self::register($data['username'], $data['password']);
} elseif(isset($data['login'])) {
self::login($data['username'], $data['password']);
}
}
public static function register($userName, $password)
{
if(isset($userName) && isset($password)) {
return self::_register($userName, $password);
}
return false; }
public static function login($userName = false, $password = false) public static function login($userName = false, $password = false)
{ {
if(isset($_SESSION['userName']) && isset($_SESSION['userId'])) return true;
if(isset($userName) && isset($password)) { if(isset($userName) && isset($password)) {
return self::_login($userName, $password, false); return self::_login($userName, $password);
} }
return false; return false;
} }
private static function _login($userName = false, $password = false, $token = false) { private static function _register($userName = false, $password = false)
if($token && !$password) { {
//query token if ($userName && $password) {
} else if ($userName && $password) { $user = User::getWhere("userdata_username = '".$userName."'");
if (!is_object($user)) {
$hash = password_hash($password, PASSWORD_BCRYPT);
$db = new db();
$db->query("INSERT INTO user(user_time, userdata_username, userdata_password)
VALUES(:qTime, :qUser, :qPassword)");
$db->bind("qTime", time());
$db->bind("qUser", $userName);
$db->bind("qPassword", $hash);
if($db->execute()) {
$_SESSION['username'] = $userName;
$_SESSION['userId'] = $db->lastInsertId();
return true;
}
}
}
return false;
}
private static function _login($userName = false, $password = false)
{
if ($userName && $password) {
$user = User::getWhere("userdata_username = '".$userName."'"); $user = User::getWhere("userdata_username = '".$userName."'");
if (is_object($user)) { if (is_object($user)) {
if (password_verify($password, $user->_getHash())) { if (password_verify($password, $user->_getHash())) {
setcookie("userName", $user->getUserName(), COOKIE_EXPIRY);
$_SESSION['username'] = $user->getUserName(); $_SESSION['username'] = $user->getUserName();
$_SESSION['userId'] = $user->getId(); $_SESSION['userId'] = $user->getId();
return true; return true;
@ -42,17 +75,19 @@ class User extends DataItem {
return false; return false;
} }
public static function logout()
{
session_destroy();
header("Location: //".$_SERVER['HTTP_HOST']);
exit();
}
public static function getUserFromSession() public static function getUserFromSession()
{ {
return self::getWhere("user_id = '".$_SESSION['userId']."'"); return self::getWhere("user_id = '".$_SESSION['userId']."'");
} }
public static function register() protected function _getHash()
{
}
public function _getHash()
{ {
return $this->userdata_password; return $this->userdata_password;
} }
@ -61,4 +96,12 @@ class User extends DataItem {
{ {
return $this->userdata_username; return $this->userdata_username;
} }
public function storeuser($dataArray)
{
$user = new user();
$user->setuser($dataArray);
$user->setUser($this->getId());
$user->save();
}
} }

View File

@ -1,8 +1,11 @@
<?php <?php
session_start();
require_once('config.php'); //DB CREDENTIALS require_once('config.php'); //DB CREDENTIALS
require_once('classes/class.db.php'); require_once('classes/class.db.php');
require_once('classes/class.dataitem.php'); require_once('classes/class.dataitem.php');
require_once('classes/class.user.php'); require_once('classes/class.user.php');
require_once('classes/class.question.php'); require_once('classes/class.question.php');
require_once('classes/class.answer.php'); require_once('classes/class.answer.php');
require_once('classes/class.result.php');

View File

@ -1,5 +1,14 @@
<?php <?php
require_once('includes/include.php'); require_once('includes/include.php');
if(isset($_REQUEST['login']) || isset($_REQUEST['register'])) {
User::loginOrRegister($_REQUEST);
}
if(isset($_REQUEST['logout'])) User::logout();
$loggedIn = $user = false;
if(isset($_SESSION['userId'])) {
$user = User::getById($_SESSION['userId']);
}
$questions = []; $questions = [];
if(!isset($_POST['mark'])) { if(!isset($_POST['mark'])) {
if(isset($_GET['questions'])) { if(isset($_GET['questions'])) {
@ -26,6 +35,10 @@ if(!isset($_POST['mark'])) {
$wrong++; $wrong++;
} }
} }
if(is_object($user)) {
$user->storeResult($_POST);
}
} }
?><!doctype html> ?><!doctype html>
<html class="no-js" lang=""> <html class="no-js" lang="">
@ -40,9 +53,26 @@ if(!isset($_POST['mark'])) {
<link rel="stylesheet" href="css/normalize.css"> <link rel="stylesheet" href="css/normalize.css">
<link rel="stylesheet" href="css/main.css"> <link rel="stylesheet" href="css/main.css">
<script src="https://code.jquery.com/jquery-1.12.0.min.js"></script>
<script>window.jQuery || document.write('<script src="js/vendor/jquery-1.12.0.min.js"><\/script>')</script>
<script src="js/plugins.js"></script>
<script src="js/main.js"></script>
</head> </head>
<body> <body>
<div id="container"> <div id="container">
<div id="user"><?php if(is_object($user)) {
echo "Welcome Back ".$_SESSION['username'].". <a href='index.php/logout=1'>Logout</a>";
} else {
echo "<div id='loginTrigger' onclick='showLoginBox()'>Login or Register</div>";
} ?></div>
<div id="cover" style="display:none;"></div>
<div id="login" style="display:none;">
<form method="post">
<label>Username<input type="text" name="username"><br/></label>
<label>Password<input type="text" name="password"><br/></label>
<button type="submit" class="loginbutton" value="login">Login</button>
</form>
</div>
<div id="header"><h1>Unofficial NZART Practice Exam</h1></div> <div id="header"><h1>Unofficial NZART Practice Exam</h1></div>
<div id="body" class="center"> <div id="body" class="center">
New Exam: <a href="/index.php?questions=10">10 Questions</a> - New Exam: <a href="/index.php?questions=10">10 Questions</a> -
@ -93,10 +123,5 @@ if(!isset($_POST['mark'])) {
Last updated 31-12-2016. Last updated 31-12-2016.
</div> </div>
</div> </div>
<script src="https://code.jquery.com/jquery-1.12.0.min.js"></script>
<script>window.jQuery || document.write('<script src="js/vendor/jquery-1.12.0.min.js"><\/script>')</script>
<script src="js/plugins.js"></script>
<script src="js/main.js"></script>
</body> </body>
</html> </html>

View File

@ -0,0 +1,4 @@
function showLoginBox()
{
alert("login");
}