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'])) { + + + +