add EgyTalk structure

This commit is contained in:
vadym Novoselskyi 2024-09-24 15:06:22 +02:00
parent 9031c8596b
commit 9f543c5f61
17 changed files with 548 additions and 14 deletions

View File

@ -3,7 +3,7 @@
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>M6 | 01</title>
<title>M6 | 02</title>
</head>
<body>
<form method="post">
@ -14,7 +14,7 @@
</form>
<?php
include_once('../inc/egytalk_connect.php');
include_once('../inc/world_connect.php');
if(isset($_POST['city']) && $_POST['city'] != '') $city = filter_input(INPUT_POST, 'city', FILTER_SANITIZE_SPECIAL_CHARS);
else $city = 'Malmö';

View File

@ -3,7 +3,7 @@
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>M6 | 01</title>
<title>M6 | 03</title>
</head>
<body>
<form method="post">

View File

@ -10,9 +10,15 @@
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
if(isset($_SESSION['logged_in']) && $_SESSION['logged_in'] == true) echo "<h1>Good</h1>";
if(isset($_SESSION['logged_in']) && $_SESSION['logged_in'] == true) {
echo "<h1>Good</h1>";
echo $_SESSION['username'];
echo "<br>";
echo $_SESSION['name'];
}
else echo "<h1> No good </h1>";
?>
<br> <br>
<a href="login.php">Log In</a>
</body>
</html>

View File

@ -3,7 +3,7 @@
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>M6 | 01</title>
<title>M6 | 04</title>
</head>
<body>
<form method="post" action="userManipulation.php">

View File

@ -0,0 +1,11 @@
<?php
include_once('../inc/world_connect.php');
$stmt = $db->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);

13
www/public/M6/06/api.php Normal file
View File

@ -0,0 +1,13 @@
<?php
include_once('../inc/world_connect.php');
$country = $_GET['country']; // Läser in parameter
$stmt = $db->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);

View File

@ -0,0 +1,15 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>M6 | 05</title>
</head>
<body>
<form method="get" action="api.php">
<label>Country: </label>
<input type="text" name="country">
<input type="submit" value="Submit"> <br><br>
</form>
</body>
</html>

13
www/public/M6/07/api.php Normal file
View File

@ -0,0 +1,13 @@
<?php
include_once('../inc/world_connect.php');
$code = $_GET['code']; // Läser in parameter
$stmt = $db->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);

View File

@ -0,0 +1,15 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>M6 | 07</title>
</head>
<body>
<form method="get" action="api.php">
<label>Country code: </label>
<input type="text" name="code">
<input type="submit" value="Submit"> <br><br>
</form>
</body>
</html>

View File

@ -0,0 +1,51 @@
<?php
function getUserPosts()
{
$db = include('../inc/egytalk_connect.php');
$stmt = $db->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();
}

View File

@ -0,0 +1,5 @@
<form method="POST" action="index.php?action=comment">
<label>Text comment</label>
<input type="text" name="comment">
<input type="submit" value="Comment it!">
</form>

View File

@ -0,0 +1,102 @@
<?php
class dbEgyTalk
{
/**
* Används i metoder genom $this->db</code>
*/
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();
}
}

View File

@ -0,0 +1,90 @@
<?php
if (isset($_POST['login'])) login();
else if (isset($_POST['signup'])) signup();
else if (isset($_POST['logout'])) logout();
else header("Location: login.php");
function login()
{
if (!isset($_POST['username'], $_POST['password'])) header("Location: login.php");
include_once('../inc/egytalk_connect.php');
$username = filter_input(INPUT_POST, 'username', FILTER_UNSAFE_RAW);
$password = $_POST['password'];
$stmt = $db->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();
}

View File

@ -0,0 +1,128 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>M6 | EgyTalk</title>
</head>
<body>
<a href="login.php">Log in</a>
<?php
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
if (isset($_SESSION['logged_in']) && $_SESSION['logged_in'] == true) {
echo "<h1> Hello " . $_SESSION['username'] . "</h1>";
echo "<h3>Your name " . $_SESSION['name'] . "</h3>";
?>
<ul>
<li><a href="index.php">Home</a></li>
<li><a href="index.php?action=post">Post a post</a></li>
<li><a href="index.php?action=userPosts">Your posts</a></li>
<li><a href="index.php?action=allPosts">All posts</a></li>
</ul>
<br> <br>
<?php
if (isset($_GET['action'])) {
$page = $_GET['action'];
include('../model/dbEgyTalk.php');
$db = new dbEgyTalk();
switch ($page) {
case 'post':
echo '
<form method="get" action="post.php">
<fieldset>
<legend>Post</legend>
<label>Text post</label>
<textarea name="post" rows="6" cols="20"></textarea> <br><br>
<input type="submit" value="Post it!">
</fieldset>
</form>
';
break;
case 'userPosts':
$posts = $db->getUserPosts($_SESSION['uid']);
echo "<fieldset>
<legend><strong>Your Posts</strong></legend>
";
foreach ($posts as $post) {
echo "<hr> <h3>From: " . $_SESSION['username'] . "</h3>";
echo "<p>" . $post['post_txt'] . "</p> <br>";
echo "<h4>" . $post['date'] . "</h4>";
include("../inc/comment.html");
}
echo "</fieldset>";
break;
case 'allPosts':
$posts = $db->getAllPosts();
echo "<fieldset>
<legend><strong>All Posts</strong></legend>
";
foreach ($posts as $post) {
echo "<hr> <h3>From: " . $post['username'] . "</h3>";
echo "<p>" . $post['post_txt'] . "</p> <br>";
echo "<h4>" . $post['date'] . "</h4>";
echo "<a href='index.php?action=postInteract&pid=" . $post['pid'] . "'>Interact</a>";
}
echo "</fieldset>";
break;
case 'postInteract':
$_SESSION['pid'] = $_GET['pid'];
$post = $db->getPost($_GET['pid']);
$comments = $db->getComments($_GET['pid']);
echo "<fieldset>
<legend><strong>Post</strong></legend>
";
echo "<h3>From: " . $post['username'] . "</h3>";
echo "<p>" . $post['post_txt'] . "</p> <br>";
echo "<h4>" . $post['date'] . "</h4>";
echo "<fieldset>
<legend><strong>All comments</strong></legend>
";
foreach ($comments as $comment) {
echo "<hr> <h4>From: " . $comment['username'] . "</h4>";
echo "<p>" . $comment['comment_txt'] . "</p>";
echo "<h5>" . $comment['date'] . "</h5>";
}
echo "</fieldset> <br>";
include("../inc/comment.html");
echo "</fieldset>";
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 "<h1> No good </h1>";
?>
</body>
</html>

View File

@ -0,0 +1,62 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>M6 | EgyTalk</title>
</head>
<body>
<form method="post" action="userManipulation.php">
<input type="hidden" name="login">
<label>Username: </label>
<input type="text" name="username"> <br><br>
<label>Password: </label>
<input type="password" name="password"> <br><br>
<input type="submit" value="Log In"> <br><br><br><br>
</form>
<form method="post" action="userManipulation.php">
<input type="hidden" name="signup">
<label>Name: </label>
<input type="text" name="firstName"> <br><br>
<label>Surname: </label>
<input type="text" name="surName"> <br><br>
<label>Username: </label>
<input type="text" name="username"> <br><br>
<label>Password: </label>
<input type="password" name="password"> <br><br>
<input type="submit" value="Sign Up"> <br><br><br><br>
</form>
<form method="post" action="userManipulation.php">
<input type="hidden" name="logout">
<input type="submit" value="Log Out"> <br><br>
</form>
<?php
if(isset($_POST['firstName'],$_POST['surName'],$_POST['username'],$_POST['password'])){
include_once('../inc/egytalk_connect.php');
$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_SANITIZE_SPECIAL_CHARS);
$password = password_hash($_POST['password'], PASSWORD_DEFAULT);
$stmt= $db->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";
}
}
?>
</body>
</html>

View File

@ -1,11 +1,22 @@
<?php
// Definierar konstanter med användarinformation.
define ('DB_USER', 'egytalk');
define ('DB_PASSWORD', '12345');
define ('DB_HOST', 'mariadb'); // 'Om docker - 'mariadb', annars 127.0.0.1
define ('DB_NAME', 'egytalk');
// Skapar en anslutning till MySql och databasen world
$dsn = 'mysql:host=' . DB_HOST . ';dbname=' . DB_NAME . ';charset=utf8';
$db = new PDO($dsn, DB_USER, DB_PASSWORD);
if (!defined('DB_USER')) {
define('DB_USER', 'egytalk');
}
if (!defined('DB_PASSWORD')) {
define('DB_PASSWORD', '12345');
}
if (!defined('DB_HOST')) {
define('DB_HOST', 'mariadb'); // Use 'mariadb' for Docker, otherwise '127.0.0.1'
}
if (!defined('DB_NAME')) {
define('DB_NAME', 'egytalk');
}
// Check if the PDO connection is already created
if (!isset($db)) {
$dsn = 'mysql:host=' . DB_HOST . ';dbname=' . DB_NAME . ';charset=utf8';
$db = new PDO($dsn, DB_USER, DB_PASSWORD);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // Enable exception handling
}
return $db;
?>

12
www/public/index.php.save Normal file
View File

@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>WebServ</title>
</head>
<body>
<h1>Webbservern fungerar!</h1>
<?php include("./footer.html");?>
</body>
</html>