diff --git a/www/public/M6/02/index.php b/www/public/M6/02/index.php index 47cee25..6b4d79a 100644 --- a/www/public/M6/02/index.php +++ b/www/public/M6/02/index.php @@ -3,7 +3,7 @@ - M6 | 01 + M6 | 02
@@ -14,7 +14,7 @@
- M6 | 01 + M6 | 03
diff --git a/www/public/M6/04/index.php b/www/public/M6/04/index.php index 406e557..84b358a 100644 --- a/www/public/M6/04/index.php +++ b/www/public/M6/04/index.php @@ -10,9 +10,15 @@ if (session_status() == PHP_SESSION_NONE) { session_start(); } - if(isset($_SESSION['logged_in']) && $_SESSION['logged_in'] == true) echo "

Good

"; + if(isset($_SESSION['logged_in']) && $_SESSION['logged_in'] == true) { + echo "

Good

"; + echo $_SESSION['username']; + echo "
"; + echo $_SESSION['name']; + } else echo "

No good

"; ?> +

Log In \ No newline at end of file diff --git a/www/public/M6/04/login.php b/www/public/M6/04/login.php index 96d7c89..54d21c4 100644 --- a/www/public/M6/04/login.php +++ b/www/public/M6/04/login.php @@ -3,7 +3,7 @@ - M6 | 01 + M6 | 04 diff --git a/www/public/M6/05/index.php b/www/public/M6/05/index.php new file mode 100644 index 0000000..f5040d4 --- /dev/null +++ b/www/public/M6/05/index.php @@ -0,0 +1,11 @@ +prepare("SELECT Name, Population, Code FROM country"); +$stmt->execute(); + +$result = $stmt->fetchAll(PDO::FETCH_ASSOC); + +header('Content-Type: application/json'); + +echo json_encode($result, JSON_UNESCAPED_UNICODE); diff --git a/www/public/M6/06/api.php b/www/public/M6/06/api.php new file mode 100644 index 0000000..0d866a7 --- /dev/null +++ b/www/public/M6/06/api.php @@ -0,0 +1,13 @@ +prepare("SELECT Name, Population, Code FROM country WHERE name LIKE :country"); +$stmt->bindValue(":country", "$country%"); +$stmt->execute(); + +$result = $stmt->fetchAll(PDO::FETCH_ASSOC); + +header('Content-Type: application/json'); + +echo json_encode($result, JSON_UNESCAPED_UNICODE); diff --git a/www/public/M6/06/index.html b/www/public/M6/06/index.html new file mode 100644 index 0000000..3e6de4c --- /dev/null +++ b/www/public/M6/06/index.html @@ -0,0 +1,15 @@ + + + + + + M6 | 05 + + + + + +

+
+ + \ No newline at end of file diff --git a/www/public/M6/07/api.php b/www/public/M6/07/api.php new file mode 100644 index 0000000..1e5c097 --- /dev/null +++ b/www/public/M6/07/api.php @@ -0,0 +1,13 @@ +prepare("SELECT Name, Population FROM city WHERE CountryCode LIKE :code"); +$stmt->bindValue(":code", "$code"); +$stmt->execute(); + +$result = $stmt->fetchAll(PDO::FETCH_ASSOC); + +header('Content-Type: application/json'); + +echo json_encode($result, JSON_UNESCAPED_UNICODE); diff --git a/www/public/M6/07/index.html b/www/public/M6/07/index.html new file mode 100644 index 0000000..5527a64 --- /dev/null +++ b/www/public/M6/07/index.html @@ -0,0 +1,15 @@ + + + + + + M6 | 07 + + +
+ + +

+
+ + \ No newline at end of file diff --git a/www/public/M6/egyTalk/dbFunctions.php b/www/public/M6/egyTalk/dbFunctions.php new file mode 100644 index 0000000..b6fbac3 --- /dev/null +++ b/www/public/M6/egyTalk/dbFunctions.php @@ -0,0 +1,51 @@ +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 new file mode 100644 index 0000000..25689e7 --- /dev/null +++ b/www/public/M6/egyTalk/inc/comment.html @@ -0,0 +1,5 @@ +
+ + + +
\ No newline at end of file diff --git a/www/public/M6/egyTalk/model/dbEgyTalk.php b/www/public/M6/egyTalk/model/dbEgyTalk.php new file mode 100644 index 0000000..fa8da36 --- /dev/null +++ b/www/public/M6/egyTalk/model/dbEgyTalk.php @@ -0,0 +1,102 @@ +db + */ + private $db; + public function __construct() + { + // Definierar konstanter med användarinformation. + define('DB_USER', 'egytalk'); + define('DB_PASSWORD', '12345'); + define('DB_HOST', 'mariadb'); + define('DB_NAME', 'egytalk'); + + $dsn = 'mysql:host=' . DB_HOST . ';dbname=' . DB_NAME . ';charset=utf8'; + $this->db = new PDO($dsn, DB_USER, DB_PASSWORD); + } + /** + * Kontrollerar av användare och lösen. + * Skapar global sessions-array med användarinformation. + * + * @param $username Användarnamn + * @param $password Lösenord + * @return $response användardata eller tom [] + */ + function auth($username, $password) + { + $username = trim(filter_var($username, FILTER_UNSAFE_RAW)); + $response = []; + + $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_verify($password, $user['password'])) { + $response['uid'] = $user['uid']; + $response['username'] = $user['username']; + $response['firstname'] = $user['firstname']; + $response['surname'] = $user['surname']; + } + } + return $response; + } + + /** + * Hämtar alla status-uppdateringar i tabellen post + * + * @return array med alla status-uppdateringar + */ + function getAllPosts() + { + $stmt = $this->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 getUserPosts($uid) + { + $stmt = $this->db->prepare("SELECT post_txt, date FROM post WHERE uid = :uid ORDER By date DESC"); + + $stmt->bindValue(":uid", $uid); + + $stmt->execute(); + return $stmt->fetchAll(PDO::FETCH_ASSOC); + } + + function getPost($pid) + { + $stmt = $this->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) + { + $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->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(); + } + +} diff --git a/www/public/M6/egyTalk/model/userManipulation.php b/www/public/M6/egyTalk/model/userManipulation.php new file mode 100644 index 0000000..d73885a --- /dev/null +++ b/www/public/M6/egyTalk/model/userManipulation.php @@ -0,0 +1,90 @@ +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/index.php b/www/public/M6/egyTalk/view/index.php new file mode 100644 index 0000000..0993421 --- /dev/null +++ b/www/public/M6/egyTalk/view/index.php @@ -0,0 +1,128 @@ + + + + + + + M6 | EgyTalk + + + + Log in + Hello " . $_SESSION['username'] . ""; + echo "

Your name " . $_SESSION['name'] . "

"; + ?> + + + +

+ + +
+ Post + +

+ +
+ + '; + break; + + case 'userPosts': + $posts = $db->getUserPosts($_SESSION['uid']); + + echo "
+ Your Posts + "; + + foreach ($posts as $post) { + echo "

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

"; + echo "

" . $post['post_txt'] . "


"; + echo "

" . $post['date'] . "

"; + include("../inc/comment.html"); + } + + echo "
"; + break; + + case 'allPosts': + $posts = $db->getAllPosts(); + + echo "
+ All Posts + "; + + foreach ($posts as $post) { + echo "

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

"; + echo "

" . $post['post_txt'] . "


"; + echo "

" . $post['date'] . "

"; + echo "Interact"; + } + + echo "
"; + break; + + case 'postInteract': + $_SESSION['pid'] = $_GET['pid']; + $post = $db->getPost($_GET['pid']); + $comments = $db->getComments($_GET['pid']); + + echo "
+ Post + "; + + echo "

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

"; + echo "

" . $post['post_txt'] . "


"; + echo "

" . $post['date'] . "

"; + + echo "
+ All comments + "; + + foreach ($comments as $comment) { + echo "

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

"; + echo "

" . $comment['comment_txt'] . "

"; + echo "
" . $comment['date'] . "
"; + } + + echo "

"; + + include("../inc/comment.html"); + + echo "
"; + break; + + case 'comment': + if($_POST['comment'] != '') $db->postComment($_SESSION['pid'], $_SESSION['uid'], $_POST['comment']); + header("Location: index.php?action=postInteract&pid=" . $_SESSION['pid']); + break; + + default: + } + } + } else echo "

No good

"; + ?> + + + \ No newline at end of file diff --git a/www/public/M6/egyTalk/view/login.php b/www/public/M6/egyTalk/view/login.php new file mode 100644 index 0000000..9a2c38c --- /dev/null +++ b/www/public/M6/egyTalk/view/login.php @@ -0,0 +1,62 @@ + + + + + + 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/M6/inc/egytalk_connect.php b/www/public/M6/inc/egytalk_connect.php index a50c0fa..d0eddf9 100644 --- a/www/public/M6/inc/egytalk_connect.php +++ b/www/public/M6/inc/egytalk_connect.php @@ -1,11 +1,22 @@ setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // Enable exception handling + } + return $db; ?> \ No newline at end of file diff --git a/www/public/index.php.save b/www/public/index.php.save new file mode 100644 index 0000000..385fd74 --- /dev/null +++ b/www/public/index.php.save @@ -0,0 +1,12 @@ + + + + + + WebServ + + +

Webbservern fungerar!

+ + +