diff --git a/www/public/M6/egyTalk/api/auth.php b/www/public/M6/egyTalk/api/auth.php
index 902ad25..985f9ba 100644
--- a/www/public/M6/egyTalk/api/auth.php
+++ b/www/public/M6/egyTalk/api/auth.php
@@ -12,7 +12,7 @@ $response['userdata'] = null;
//$user = $db->getUserFromUid($_SESSION['uid']);
//}
if (isset($_POST['username'], $_POST['password'])) {
- $user = $db->auth($_POST['username'], $_POST['password']);
+ $user = $db->auth($_POST['username'], $_POST['password'], false);
}
if (isset($user) && !empty($user)) {
@@ -23,7 +23,6 @@ if (isset($user) && !empty($user)) {
}
if(!$response['auth']) header('HTTP/1.0 401 Unauthorized');
-
header('Access-Control-Allow-Credentials: true');
header('Content-Type: application/json');
diff --git a/www/public/M6/egyTalk/api/getPostsByUID.php b/www/public/M6/egyTalk/api/getPostsByUID.php
new file mode 100644
index 0000000..b28c72b
--- /dev/null
+++ b/www/public/M6/egyTalk/api/getPostsByUID.php
@@ -0,0 +1,26 @@
+getUserPosts($uid);
+
+ for($i = 0; $i < sizeof($response['posts']); $i++) {
+ $comments = $db->getComments($response['posts'][$i]['pid']);
+ $response['posts'][$i]['comments'] = $comments;
+ }
+}
+// Behövs för session-cookies och anger att formatet är json
+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/getUserByUID.php b/www/public/M6/egyTalk/api/getUserByUID.php
new file mode 100644
index 0000000..324e270
--- /dev/null
+++ b/www/public/M6/egyTalk/api/getUserByUID.php
@@ -0,0 +1,21 @@
+getUserByUID($uid);
+}
+// Behövs för session-cookies och anger att formatet är json
+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/controller/userManipulation.php b/www/public/M6/egyTalk/controller/userManipulation.php
index 6e8acef..93c646b 100644
--- a/www/public/M6/egyTalk/controller/userManipulation.php
+++ b/www/public/M6/egyTalk/controller/userManipulation.php
@@ -14,7 +14,7 @@ function login()
$username = filter_input(INPUT_POST, 'username', FILTER_UNSAFE_RAW);
$password = $_POST['password'];
- $result = $db->login($username, $password);
+ $result = $db->auth($username, $password, true);
if ($result == []) {
header("Location: ../login.html");
diff --git a/www/public/M6/egyTalk/index.php b/www/public/M6/egyTalk/index.php
index 5230a16..fef587d 100644
--- a/www/public/M6/egyTalk/index.php
+++ b/www/public/M6/egyTalk/index.php
@@ -18,13 +18,24 @@
echo "
Your name " . $_SESSION['name'] . "
";
?>
-
+
getPosts.php
+
+
+
+
- Home
- Post a post
diff --git a/www/public/M6/egyTalk/model/dbEgyTalk.php b/www/public/M6/egyTalk/model/dbEgyTalk.php
index 1f8e22b..8217c75 100644
--- a/www/public/M6/egyTalk/model/dbEgyTalk.php
+++ b/www/public/M6/egyTalk/model/dbEgyTalk.php
@@ -25,7 +25,7 @@ class dbEgyTalk
* @param $password Lösenord
* @return $result användardata eller tom []
*/
- function auth($username, $password)
+ function auth($username, $password, $toHash)
{
$username = trim(filter_var($username, FILTER_UNSAFE_RAW));
$result = [];
@@ -38,37 +38,34 @@ class dbEgyTalk
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'];
- }
+ if(!$toHash && $password != $user['password']) return $result;
+ if($toHash && !password_verify($password, $user['password'])) return $result;
+
+ $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)
+ function getUserByUID($uid)
{
- $username = trim(filter_var($username, FILTER_UNSAFE_RAW));
$result = [];
- $stmt = $this->db->prepare("SELECT * FROM user WHERE username = :user");
- $stmt->bindValue(":user", $username);
+ $stmt = $this->db->prepare("SELECT uid, firstname, surname, username FROM user WHERE uid = :uid");
+ $stmt->bindValue(":uid", $uid);
$stmt->execute();
/** Kontroll att resultat finns */
if ($stmt->rowCount() == 1) {
$user = $stmt->fetch(PDO::FETCH_ASSOC);
- if (password_verify($password, $user['password'])) {
- $result['uid'] = $user['uid'];
- $result['username'] = $user['username'];
- $result['firstname'] = $user['firstname'];
- $result['surname'] = $user['surname'];
- $result['password'] = $user['password'];
- }
+ $result['uid'] = $user['uid'];
+ $result['username'] = $user['username'];
+ $result['firstname'] = $user['firstname'];
+ $result['surname'] = $user['surname'];
}
return $result;
}
@@ -115,6 +112,17 @@ class dbEgyTalk
$stmt->execute();
}
+ function postComment($pid, $uid, $comment)
+ {
+ $stmt = $this->db->prepare("INSERT INTO comment (pid, uid, comment_txt, date) VALUES (:pid, :uid, :comment, NOW())");
+
+ $stmt->bindValue(":pid", $pid);
+ $stmt->bindValue(":uid", $uid);
+ $stmt->bindValue(":comment", $comment);
+
+ $stmt->execute();
+ }
+
/**
* Hämtar alla status-uppdateringar i tabellen post
*
@@ -148,21 +156,10 @@ class dbEgyTalk
function getComments($pid)
{
- $stmt = $this->db->prepare("SELECT user.username, comment.comment_txt, comment.date FROM user JOIN comment ON user.uid = comment.uid WHERE pid = :pid");
+ $stmt = $this->db->prepare("SELECT comment.cid, comment.uid, 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($pid, $uid, $comment)
- {
- $stmt = $this->db->prepare("INSERT INTO comment (pid, uid, comment_txt, date) VALUES (:pid, :uid, :comment, NOW())");
-
- $stmt->bindValue(":pid", $pid);
- $stmt->bindValue(":uid", $uid);
- $stmt->bindValue(":comment", $comment);
-
- $stmt->execute();
- }
}