diff --git a/www/public/M6/egyTalk/api/auth.php b/www/public/M6/egyTalk/api/auth.php new file mode 100644 index 0000000..902ad25 --- /dev/null +++ b/www/public/M6/egyTalk/api/auth.php @@ -0,0 +1,30 @@ +getUserFromUid($_SESSION['uid']); +//} +if (isset($_POST['username'], $_POST['password'])) { + $user = $db->auth($_POST['username'], $_POST['password']); +} + +if (isset($user) && !empty($user)) { + $response['auth'] = true; + $response['userdata'] = $user; + session_regenerate_id(); + $_SESSION['uid'] = $user['uid']; +} + +if(!$response['auth']) header('HTTP/1.0 401 Unauthorized'); + +header('Access-Control-Allow-Credentials: true'); +header('Content-Type: application/json'); + +echo json_encode($response, JSON_UNESCAPED_UNICODE); diff --git a/www/public/M6/egyTalk/api/getPosts.php b/www/public/M6/egyTalk/api/getPosts.php new file mode 100644 index 0000000..0f33b15 --- /dev/null +++ b/www/public/M6/egyTalk/api/getPosts.php @@ -0,0 +1,20 @@ +getAllPosts(); +} +// Behövs för session-cookies och anger att formatet är json +header('Access-Control-Allow-Credentials: true'); +header('Content-Type: application/json'); + +// Gör om arrayen till en array med json-objekt +echo json_encode($response, JSON_UNESCAPED_UNICODE); diff --git a/www/public/M6/egyTalk/controller/userManipulation.php b/www/public/M6/egyTalk/controller/userManipulation.php new file mode 100644 index 0000000..6e8acef --- /dev/null +++ b/www/public/M6/egyTalk/controller/userManipulation.php @@ -0,0 +1,75 @@ +login($username, $password); + + if ($result == []) { + header("Location: ../login.html"); + exit; + } + + $_SESSION = array(); + session_start(); + + $_SESSION['uid'] = $result['uid']; + $_SESSION['username'] = $result['username']; + $_SESSION['name'] = $result['surname'] . " " . $result['firstname']; + $_SESSION['password'] = $result['password']; + + $_SESSION['logged_in'] = true; + header("Location: ../index.php"); +} +function signup() +{ + if (!isset($_POST['firstname'], $_POST['surname'], $_POST['username'], $_POST['password'])) { + //header("Location: ../view/login.html"); + exit(); + } + + $firstname = $_POST['firstname']; + $surname = $_POST['surname']; + $username = $_POST['username']; + $password = $_POST['password']; + + include('../model/dbEgyTalk.php'); + $db = new dbEgyTalk(); + $result = $db->signup($firstname, $surname, $username, $password); + + $_SESSION = array(); + session_start(); + + $_SESSION['uid'] = $result['uid']; + $_SESSION['username'] = $result['username']; + $_SESSION['name'] = $result['firstaname'] . " " . $result['surname']; + $_SESSION['password'] = $result['password']; + + $_SESSION['logged_in'] = true; + + header("Location: ../index.php"); +} + +function logout() +{ + if (session_status() == PHP_SESSION_NONE) { + session_start(); + } + $_POST = array(); + $_SESSION = array(); // Tömmer sessionsarrayen + session_regenerate_id(true); + + header("Location: ../index.php"); + exit(); +} diff --git a/www/public/M6/egyTalk/dbFunctions.php b/www/public/M6/egyTalk/dbFunctions.php deleted file mode 100644 index b6fbac3..0000000 --- a/www/public/M6/egyTalk/dbFunctions.php +++ /dev/null @@ -1,51 +0,0 @@ -prepare("SELECT post_txt, date FROM post WHERE uid = :uid ORDER By date DESC"); - - $stmt->bindValue(":uid", $_SESSION['uid']); - - $stmt->execute(); - return $stmt->fetchAll(PDO::FETCH_ASSOC); -} - -function getAllPosts() -{ - $db = include('../inc/egytalk_connect.php'); - $stmt = $db->prepare("SELECT user.username, post.post_txt, post.date, post.pid FROM user JOIN post ON user.uid = post.uid ORDER By post.date DESC;"); - $stmt->execute(); - return $stmt->fetchAll(PDO::FETCH_ASSOC); -} - -function getPost($pid) -{ - $db = include('../inc/egytalk_connect.php'); - $stmt = $db->prepare("SELECT user.username, post.post_txt, post.date, post.pid FROM user JOIN post ON user.uid = post.uid WHERE pid = :pid"); - $stmt->bindValue(":pid", $pid); - - $stmt->execute(); - return $stmt->fetch(PDO::FETCH_ASSOC); -} - -function getComments($pid) -{ - $db = include('../inc/egytalk_connect.php'); - $stmt = $db->prepare("SELECT user.username, comment.comment_txt, comment.date FROM user JOIN comment ON user.uid = comment.uid WHERE pid = :pid"); - $stmt->bindValue(":pid", $pid); - - $stmt->execute(); - return $stmt->fetchAll(PDO::FETCH_ASSOC); -} - -function postComment($comment) -{ - $db = include('../inc/egytalk_connect.php'); - $stmt = $db->prepare("INSERT INTO comment (pid, uid, comment_txt, date) VALUES (:pid, :uid, :comment, NOW())"); - - $stmt->bindValue(":pid", $_SESSION['pid']); - $stmt->bindValue(":uid", $_SESSION['uid']); - $stmt->bindValue(":comment", $comment); - - $stmt->execute(); -} \ No newline at end of file diff --git a/www/public/M6/egyTalk/inc/comment.html b/www/public/M6/egyTalk/inc/comment.html index 25689e7..a959216 100644 --- a/www/public/M6/egyTalk/inc/comment.html +++ b/www/public/M6/egyTalk/inc/comment.html @@ -1,5 +1,5 @@
- +

\ No newline at end of file diff --git a/www/public/M6/egyTalk/view/index.php b/www/public/M6/egyTalk/index.php similarity index 80% rename from www/public/M6/egyTalk/view/index.php rename to www/public/M6/egyTalk/index.php index 0993421..5230a16 100644 --- a/www/public/M6/egyTalk/view/index.php +++ b/www/public/M6/egyTalk/index.php @@ -8,7 +8,7 @@ - Log in + Log in
Your name " . $_SESSION['name'] . ""; ?> +
+ + + +
+getPosts.php + @@ -32,13 +39,18 @@ if (isset($_GET['action'])) { $page = $_GET['action']; - include('../model/dbEgyTalk.php'); + include('model/dbEgyTalk.php'); $db = new dbEgyTalk(); switch ($page) { case 'post': + $db->post($_SESSION['uid'], filter_input(INPUT_POST, 'post', FILTER_SANITIZE_SPECIAL_CHARS)); + header('Location: index.php?action=userPosts'); + break; + + case 'writePost': echo ' -
+
Post @@ -60,7 +72,7 @@ if (isset($_GET['action'])) { echo "

From: " . $_SESSION['username'] . "

"; echo "

" . $post['post_txt'] . "


"; echo "

" . $post['date'] . "

"; - include("../inc/comment.html"); + echo "Interact"; } echo "
"; @@ -108,7 +120,7 @@ if (isset($_GET['action'])) { echo "
"; - include("../inc/comment.html"); + include("inc/comment.html"); echo ""; break; diff --git a/www/public/M6/egyTalk/login.html b/www/public/M6/egyTalk/login.html new file mode 100644 index 0000000..ead5e97 --- /dev/null +++ b/www/public/M6/egyTalk/login.html @@ -0,0 +1,36 @@ + + + + + + M6 | EgyTalk + + + + + +

+ +

+



+
+ +
+ + +

+ +

+ +

+ +

+



+
+ +
+ +

+
+ + \ No newline at end of file diff --git a/www/public/M6/egyTalk/model/dbEgyTalk.php b/www/public/M6/egyTalk/model/dbEgyTalk.php index fa8da36..1f8e22b 100644 --- a/www/public/M6/egyTalk/model/dbEgyTalk.php +++ b/www/public/M6/egyTalk/model/dbEgyTalk.php @@ -23,12 +23,36 @@ class dbEgyTalk * * @param $username Användarnamn * @param $password Lösenord - * @return $response användardata eller tom [] + * @return $result användardata eller tom [] */ function auth($username, $password) { $username = trim(filter_var($username, FILTER_UNSAFE_RAW)); - $response = []; + $result = []; + + $stmt = $this->db->prepare("SELECT * FROM user WHERE username = :user"); + $stmt->bindValue(":user", $username); + $stmt->execute(); + + /** Kontroll att resultat finns */ + if ($stmt->rowCount() == 1) { + $user = $stmt->fetch(PDO::FETCH_ASSOC); + + if ($password == $user['password']) { + $result['uid'] = $user['uid']; + $result['username'] = $user['username']; + $result['firstname'] = $user['firstname']; + $result['surname'] = $user['surname']; + $result['password'] = $user['password']; + } + } + return $result; + } + + function login($username, $password) + { + $username = trim(filter_var($username, FILTER_UNSAFE_RAW)); + $result = []; $stmt = $this->db->prepare("SELECT * FROM user WHERE username = :user"); $stmt->bindValue(":user", $username); @@ -39,13 +63,56 @@ class dbEgyTalk $user = $stmt->fetch(PDO::FETCH_ASSOC); if (password_verify($password, $user['password'])) { - $response['uid'] = $user['uid']; - $response['username'] = $user['username']; - $response['firstname'] = $user['firstname']; - $response['surname'] = $user['surname']; + $result['uid'] = $user['uid']; + $result['username'] = $user['username']; + $result['firstname'] = $user['firstname']; + $result['surname'] = $user['surname']; + $result['password'] = $user['password']; } } - return $response; + return $result; + } + + function signup($firstname, $surname, $username, $password) + { + $uid = random_bytes(16); + $uid[6] = chr((ord($uid[6]) & 0x0f) | 0x40); + $uid[8] = chr((ord($uid[8]) & 0x3f) | 0x80); + $uid = vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($uid), 4)); + + $firstname = trim(filter_var($firstname, FILTER_SANITIZE_SPECIAL_CHARS)); + $surname = trim(filter_var($surname, FILTER_SANITIZE_SPECIAL_CHARS)); + $username = trim(filter_var($username, FILTER_UNSAFE_RAW)); + $password = password_hash($password, PASSWORD_DEFAULT); + + $stmt = $this->db->prepare("INSERT INTO user(uid, firstname, surname, username, password) VALUES(:uid, :fn, :sn,:user,:pwd)"); + + $stmt->bindValue(":uid", $uid); + $stmt->bindValue(":fn", $firstname); + $stmt->bindValue(":sn", $surname); + $stmt->bindValue(":user", $username); + $stmt->bindValue(":pwd", $password); + + $stmt->execute(); + + $result = []; + $result['uid'] = $uid; + $result['username'] = $username; + $result['firstname'] = $firstname; + $result['surname'] = $surname; + $result['password'] = $password; + + return $result; + } + + + function post($uid, $post_txt) { + $stmt = $this->db->prepare("INSERT INTO post (uid, post_txt, date) VALUES (:uid, :post, NOW())"); + + $stmt->bindValue(":uid", $uid); + $stmt->bindValue(":post", $post_txt); + + $stmt->execute(); } /** @@ -62,7 +129,7 @@ class dbEgyTalk function getUserPosts($uid) { - $stmt = $this->db->prepare("SELECT post_txt, date FROM post WHERE uid = :uid ORDER By date DESC"); + $stmt = $this->db->prepare("SELECT post_txt, date, pid FROM post WHERE uid = :uid ORDER By date DESC"); $stmt->bindValue(":uid", $uid); @@ -98,5 +165,4 @@ class dbEgyTalk $stmt->execute(); } - } diff --git a/www/public/M6/egyTalk/model/userManipulation.php b/www/public/M6/egyTalk/model/userManipulation.php deleted file mode 100644 index d73885a..0000000 --- a/www/public/M6/egyTalk/model/userManipulation.php +++ /dev/null @@ -1,90 +0,0 @@ -prepare("SELECT * FROM user WHERE username = :username"); - $stmt->bindValue(":username", $username); - - $stmt->execute(); - - if ($stmt->rowCount() != 1) header("Location: login.php"); - - $user = $stmt->fetch(PDO::FETCH_ASSOC); - if (password_verify($password, $user['password'])) { - $_SESSION = array(); - session_start(); - - $_SESSION['uid'] = $user['uid']; - $_SESSION['username'] = $user['username']; - $_SESSION['name'] = $user['surname'] . " " . $user['firstname']; - - $_SESSION['logged_in'] = true; - header("Location: index.php"); - } -} -function signup() -{ - if (!isset($_POST['firstName'], $_POST['surName'], $_POST['username'], $_POST['password'])) { - header("Location: login.php"); - exit(); - } - - include_once('../inc/egytalk_connect.php'); - $uid = random_bytes(16); - $uid[6] = chr((ord($uid[6]) & 0x0f) | 0x40); - $uid[8] = chr((ord($uid[8]) & 0x3f) | 0x80); - $uid = vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($uid), 4)); - - $firstName = filter_input(INPUT_POST, 'firstName', FILTER_SANITIZE_SPECIAL_CHARS); - $surName = filter_input(INPUT_POST, 'surName', FILTER_SANITIZE_SPECIAL_CHARS); - $username = filter_input(INPUT_POST, 'username', FILTER_UNSAFE_RAW); - $password = password_hash($_POST['password'], PASSWORD_DEFAULT); - - - $stmt = $db->prepare("INSERT INTO user(uid, firstname, surname, username, password) VALUES(:uid, :fn, :sn,:user,:pwd)"); - - $stmt->bindValue(":uid", $uid); - $stmt->bindValue(":fn", $firstName); - $stmt->bindValue(":sn", $surName); - $stmt->bindValue(":user", $username); - $stmt->bindValue(":pwd", $password); - - try { - $stmt->execute(); - - $_SESSION = array(); - session_start(); - - $_SESSION['uid'] = $uuid; - $_SESSION['username'] = $username; - $_SESSION['name'] = $firstName . " " . $surName; - $_SESSION['logged_in'] = true; - header("Location: index.php"); - } catch (Exception $e) { - header("Location: login.php"); - exit(); - } -} - -function logout() -{ - if (session_status() == PHP_SESSION_NONE) { - session_start(); - } - $_POST = array(); - $_SESSION = array(); // Tömmer sessionsarrayen - session_regenerate_id(true); - - header("Location: index.php"); - exit(); -} diff --git a/www/public/M6/egyTalk/view/login.php b/www/public/M6/egyTalk/view/login.php deleted file mode 100644 index 9a2c38c..0000000 --- a/www/public/M6/egyTalk/view/login.php +++ /dev/null @@ -1,62 +0,0 @@ - - - - - - M6 | EgyTalk - - -
- - -

- -

-



-
- -
- - -

- -

- -

- -

-



-
- -
- -

-
- - prepare("INSERT INTO user(uid, firstname, surname, username, password) VALUES(UUID(), :fn, :sn,:user,:pwd)"); - - $stmt->bindValue(":fn", $firstName); - $stmt->bindValue(":sn", $surName); - $stmt->bindValue(":user", $username); - $stmt->bindValue(":pwd", $password); - - try{ - $stmt->execute(); - echo "Good"; - }catch(Exception $e){ - echo "Not good"; - } - } - ?> - - \ No newline at end of file diff --git a/www/public/index.php.save b/www/public/index.php.save deleted file mode 100644 index 385fd74..0000000 --- a/www/public/index.php.save +++ /dev/null @@ -1,12 +0,0 @@ - - - - - - WebServ - - -

Webbservern fungerar!

- - -