From 0c6a5f38338705415d0ade91a67f4a93fcb56286 Mon Sep 17 00:00:00 2001 From: Daniel Mason Date: Sun, 1 Jan 2017 22:06:48 +1300 Subject: [PATCH] User login progress --- css/main.css | 10 ++++ includes/classes/class.dataitem.php | 41 +++++----------- includes/classes/class.result.php | 41 ++++++++++++++++ includes/classes/class.user.php | 73 +++++++++++++++++++++++------ includes/include.php | 5 +- index.php | 35 ++++++++++++-- js/main.js | 4 ++ 7 files changed, 158 insertions(+), 51 deletions(-) create mode 100644 includes/classes/class.result.php diff --git a/css/main.css b/css/main.css index 9606fce..024e91f 100755 --- a/css/main.css +++ b/css/main.css @@ -24,6 +24,16 @@ html, body, #container { margin: 0 auto 0 auto; } +#user { + position: absolute; + top: 0; + right: 0; + margin-right: 15px; + margin-top: 10px; + width: 250px; + text-align:right; +} + html { color: #222; font-size: 1em; diff --git a/includes/classes/class.dataitem.php b/includes/classes/class.dataitem.php index e3a81c4..e120989 100755 --- a/includes/classes/class.dataitem.php +++ b/includes/classes/class.dataitem.php @@ -2,12 +2,6 @@ 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) { $db = new db(); $db->query("SELECT * FROM `".static::_getType()."` WHERE ".static::_getType()."_id = :id"); @@ -22,15 +16,14 @@ class DataItem { return $obj; } - private function _getAllWhere($where = false, $orderBy = false, $join = false, $limit = false) { - $Objarr = array(); - $typeName = static::_getType(); - $className = static::_getClass(); - $this->_db->query("SELECT * FROM `".$typeName."` ".($join?$join:"").($where?" WHERE ".$where."":"")." ".($orderBy?" ".$orderBy." ":" ").($limit?"LIMIT ".$limit:"")); - $results = $this->_db->resultset(); + public static function getAllWhere($where = false, $orderBy = false, $join = false, $limit = false) { + $Objarr = []; + $db = new db(); + $db->query("SELECT * FROM `".static::_getType()."` ".($join?$join:"").($where?" WHERE ".$where."":"")." ".($orderBy?" ".$orderBy." ":" ").($limit?"LIMIT ".$limit:"")); + $results = $db->resultset(); if(!$results) return false; foreach ($results as $result) { - $obj = new $className(); + $obj = new static(); foreach ($result as $key=>$val) { $obj->$key = $val; } @@ -39,30 +32,18 @@ class DataItem { return $Objarr; } - public static function getAllWhere($where = false, $orderBy = false, $join = false, $limit = false) { - $me = new static(); - return $me->_getAllWhere($where, $orderBy, $join, $limit); - } - - 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(); + public static function getWhere($where = false, $orderBy = false, $join = false, $limit = false) { + $db = new db(); + $db->query("SELECT * FROM `".static::_getType()."` ".($join?$join:"").($where?" WHERE ".$where."":"")." ".($orderBy?" ".$orderBy."":"").($limit?"LIMIT ".$limit:"")); + $result = $db->single(); if(!$result) return false; - $obj = new $className(); + $obj = new static(); foreach ($result as $key=>$val) { $obj->$key = $val; } 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() { $typeName = static::_getType(); $id = $typeName."_id"; diff --git a/includes/classes/class.result.php b/includes/classes/class.result.php new file mode 100644 index 0000000..46a0a7a --- /dev/null +++ b/includes/classes/class.result.php @@ -0,0 +1,41 @@ +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(); + } + +} \ No newline at end of file diff --git a/includes/classes/class.user.php b/includes/classes/class.user.php index cef6aec..5bf1f91 100755 --- a/includes/classes/class.user.php +++ b/includes/classes/class.user.php @@ -14,25 +14,58 @@ class User extends DataItem { 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) { - if(isset($_SESSION['userName']) && isset($_SESSION['userId'])) return true; - if(isset($userName) && isset($password)) { - return self::_login($userName, $password, false); + return self::_login($userName, $password); } - return false; } - private static function _login($userName = false, $password = false, $token = false) { - if($token && !$password) { - //query token - } else if ($userName && $password) { + private static function _register($userName = false, $password = false) + { + 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."'"); if (is_object($user)) { if (password_verify($password, $user->_getHash())) { - setcookie("userName", $user->getUserName(), COOKIE_EXPIRY); $_SESSION['username'] = $user->getUserName(); $_SESSION['userId'] = $user->getId(); return true; @@ -42,17 +75,19 @@ class User extends DataItem { return false; } + public static function logout() + { + session_destroy(); + header("Location: //".$_SERVER['HTTP_HOST']); + exit(); + } + public static function getUserFromSession() { return self::getWhere("user_id = '".$_SESSION['userId']."'"); } - public static function register() - { - - } - - public function _getHash() + protected function _getHash() { return $this->userdata_password; } @@ -61,4 +96,12 @@ class User extends DataItem { { return $this->userdata_username; } + + public function storeuser($dataArray) + { + $user = new user(); + $user->setuser($dataArray); + $user->setUser($this->getId()); + $user->save(); + } } \ No newline at end of file diff --git a/includes/include.php b/includes/include.php index d6a08e9..d864b3f 100755 --- a/includes/include.php +++ b/includes/include.php @@ -1,8 +1,11 @@ storeResult($_POST); + } + } ?> @@ -40,9 +53,26 @@ if(!isset($_POST['mark'])) { + + + +
+
Logout"; + } else { + echo "
Login or Register
"; + } ?>
+ +
New Exam: 10 Questions - @@ -93,10 +123,5 @@ if(!isset($_POST['mark'])) { Last updated 31-12-2016.
- - - - - diff --git a/js/main.js b/js/main.js index e69de29..ef7f732 100755 --- a/js/main.js +++ b/js/main.js @@ -0,0 +1,4 @@ +function showLoginBox() +{ + alert("login"); +} \ No newline at end of file