Transfer to git
This commit is contained in:
23
includes/chat_reading.inc.php
Normal file
23
includes/chat_reading.inc.php
Normal file
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
if ($_SERVER["REQUEST_METHOD"] == "GET") {
|
||||
try {
|
||||
require_once "dbh.inc.php";
|
||||
require_once "chat_reading_model.inc.php";
|
||||
require_once "chat_reading_view.inc.php";
|
||||
require_once "chat_reading_contr.inc.php";
|
||||
|
||||
$chat_history = get_chat_history($pdo);
|
||||
|
||||
display_history($chat_history);
|
||||
|
||||
} catch (PDOException $e) {
|
||||
echo "An error has ocurred: " . $e->getMessage();
|
||||
exit();
|
||||
}
|
||||
} else {
|
||||
echo "Wrong request type, please check your request.";
|
||||
exit();
|
||||
}
|
||||
16
includes/chat_reading_contr.inc.php
Normal file
16
includes/chat_reading_contr.inc.php
Normal file
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
function get_chat_history(object $pdo) {
|
||||
$chat_history = retrieve_chat_history($pdo);
|
||||
return $chat_history;
|
||||
}
|
||||
|
||||
function display_history(array $chat_history) {
|
||||
render_history($chat_history);
|
||||
}
|
||||
|
||||
function print_errors(){
|
||||
check_errors();
|
||||
}
|
||||
16
includes/chat_reading_model.inc.php
Normal file
16
includes/chat_reading_model.inc.php
Normal file
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
function retrieve_chat_history(object $pdo){
|
||||
$query = "SELECT username, content, date_sent FROM comments;";
|
||||
|
||||
$stmt = $pdo->prepare($query);
|
||||
$stmt->execute();
|
||||
$chat_history = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
|
||||
$stmt = null;
|
||||
$query = null;
|
||||
|
||||
return $chat_history;
|
||||
}
|
||||
16
includes/chat_reading_view.inc.php
Normal file
16
includes/chat_reading_view.inc.php
Normal file
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
function render_history(array $chat_history){
|
||||
$chat_history = array_reverse($chat_history,true); // Reverse display order to fix scroll direction
|
||||
foreach ($chat_history as $message) {
|
||||
?>
|
||||
<div class="message">
|
||||
<span class="user"><?php echo htmlspecialchars($message["username"]); ?></span>
|
||||
<span class="content"><?php echo htmlspecialchars($message["content"]); ?></span>
|
||||
<p class="date_sent"><?php echo htmlspecialchars($message["date_sent"]); ?></p>
|
||||
</div>
|
||||
<?php
|
||||
}
|
||||
}
|
||||
57
includes/chat_send.inc.php
Normal file
57
includes/chat_send.inc.php
Normal file
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
if ($_SERVER["REQUEST_METHOD"] == "POST") {
|
||||
try {
|
||||
require_once "dbh.inc.php";
|
||||
require_once "chat_send_model.inc.php";
|
||||
require_once "chat_send_view.inc.php";
|
||||
require_once "chat_send_contr.inc.php";
|
||||
require_once "config_session.inc.php";
|
||||
|
||||
$errors = [];
|
||||
|
||||
// Check if user is logged in, if not do not check for other errors.
|
||||
if (is_user_logged_in()){
|
||||
$inputs = ["username"=>$_SESSION["username"],"message_content"=>$_POST["message_content"]];
|
||||
|
||||
if (isEmpty($inputs)) {
|
||||
$errors = ["null_field" => "Form contains blank characters."];
|
||||
}
|
||||
if (!user_exists($pdo, $inputs["username"])) {
|
||||
$errors = ["inexistent_user" => "User does not exist on db."];
|
||||
}
|
||||
} else {
|
||||
$errors = ["not_logged_in" => "You haven't logged in."];
|
||||
}
|
||||
|
||||
if ($errors) {
|
||||
$_SESSION["errors"] = $errors;
|
||||
|
||||
// Send back form
|
||||
$entered_data = [
|
||||
"message_content" => $inputs["message_content"]
|
||||
];
|
||||
|
||||
$_SESSION["entered_chat_data"] = $entered_data;
|
||||
|
||||
header("Location: ../chat.php");
|
||||
$inputs = null;
|
||||
die();
|
||||
}
|
||||
|
||||
send_message($pdo, $inputs["username"], $inputs["message_content"]);
|
||||
|
||||
header("Location: ../chat.php");
|
||||
$inputs = null;
|
||||
die();
|
||||
} catch (PDOException $e) {
|
||||
echo "An error has ocurred: " . $e->getMessage();
|
||||
exit();
|
||||
}
|
||||
|
||||
} else {
|
||||
echo "Wrong request type, please check your request.";
|
||||
exit();
|
||||
}
|
||||
43
includes/chat_send_contr.inc.php
Normal file
43
includes/chat_send_contr.inc.php
Normal file
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
function isEmpty(array $array){
|
||||
foreach ($array as $value => $content) {
|
||||
if (empty($content)){
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function user_exists(object $pdo, string $username){
|
||||
if (username_search($pdo, $username)) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
function send_message(object $pdo, string $username, string $content) {
|
||||
function get_users_id(object $pdo, string $username){
|
||||
$user_id = retrieve_user_id($pdo, $username);
|
||||
return $user_id;
|
||||
}
|
||||
|
||||
$users_id = get_users_id($pdo, $username);
|
||||
write_message($pdo, $username, $users_id, $content);
|
||||
}
|
||||
|
||||
function print_errors(){
|
||||
check_errors();
|
||||
}
|
||||
|
||||
function is_user_logged_in() {
|
||||
if (isset($_SESSION["username"])) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
49
includes/chat_send_model.inc.php
Normal file
49
includes/chat_send_model.inc.php
Normal file
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
// Remove after sessions are secured
|
||||
function username_search(object $pdo, string $username) {
|
||||
$query = "SELECT username FROM users WHERE username = :username;";
|
||||
|
||||
// Prepare and check if user already exists
|
||||
$stmt = $pdo->prepare($query);
|
||||
$stmt->bindParam(":username",$username);
|
||||
$stmt->execute();
|
||||
$result = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
|
||||
$stmt = null;
|
||||
$query = null;
|
||||
|
||||
// True if something is found, false if not
|
||||
return $result;
|
||||
}
|
||||
|
||||
function retrieve_user_id(object $pdo, string $username) {
|
||||
$query = "SELECT id FROM users WHERE username = :username;";
|
||||
|
||||
// Prepare and check if user already exists
|
||||
$stmt = $pdo->prepare($query);
|
||||
$stmt->bindParam(":username",$username);
|
||||
$stmt->execute();
|
||||
$result = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
|
||||
$stmt = null;
|
||||
$query = null;
|
||||
|
||||
// True if something is found, false if not
|
||||
return $result["id"];
|
||||
}
|
||||
|
||||
function write_message(object $pdo, string $username, int $users_id, string $content) {
|
||||
$query = "INSERT INTO comments (username, users_id, content) VALUES (:username, :users_id, :content);";
|
||||
|
||||
$stmt = $pdo->prepare($query);
|
||||
$stmt->bindParam(':username',$username);
|
||||
$stmt->bindParam(':users_id',$users_id);
|
||||
$stmt->bindParam(':content',$content);
|
||||
$stmt->execute();
|
||||
|
||||
$stmt = null;
|
||||
$query = null;
|
||||
}
|
||||
15
includes/chat_send_view.inc.php
Normal file
15
includes/chat_send_view.inc.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
function render_chat_send_form(){
|
||||
?>
|
||||
<input required type="text" id="message_content" name="message_content" placeholder="Message" value=
|
||||
<?php if(isset($_SESSION["entered_chat_data"])){
|
||||
echo $_SESSION["entered_chat_data"]["message_content"];
|
||||
unset($_SESSION["entered_chat_data"]);
|
||||
}
|
||||
?>>
|
||||
</input>
|
||||
<?php
|
||||
}
|
||||
9
includes/comments_db.sql
Normal file
9
includes/comments_db.sql
Normal file
@@ -0,0 +1,9 @@
|
||||
CREATE TABLE comments (
|
||||
id INT(11) NOT NULL AUTO_INCREMENT,
|
||||
username VARCHAR(255) NOT NULL,
|
||||
content TEXT NOT NULL,
|
||||
date_sent DATETIME DEFAULT CURRENT_TIME,
|
||||
users_id INT(11) NOT NULL,
|
||||
PRIMARY KEY (id),
|
||||
FOREIGN KEY (users_id) REFERENCES users(id) ON DELETE NO ACTION
|
||||
);
|
||||
31
includes/config_session.inc.php
Normal file
31
includes/config_session.inc.php
Normal file
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
ini_set('session.use_only_cookies' , 1);
|
||||
ini_set('session.use_strict_mode' , 1);
|
||||
|
||||
session_set_cookie_params([
|
||||
'lifetime' => 1800,
|
||||
'domain' => 'ethernal.win',
|
||||
'path' => '/',
|
||||
'secure' => true,
|
||||
'httponly' => true
|
||||
]);
|
||||
|
||||
session_start();
|
||||
|
||||
if ( !isset($_SESSION["last_regen"]) ){
|
||||
session_regen();
|
||||
|
||||
} else {
|
||||
$timeout_m = 60 * 30; // 30 minutes
|
||||
if ((time() - $_SESSION["last_regen"]) > $timeout_m) {
|
||||
session_regen();
|
||||
}
|
||||
}
|
||||
|
||||
function session_regen() {
|
||||
session_regenerate_id();
|
||||
$_SESSION["last_regen"] = time();
|
||||
}
|
||||
16
includes/error_handling.inc.php
Normal file
16
includes/error_handling.inc.php
Normal file
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
if (isset($_SESSION["errors"])) {
|
||||
$errors = $_SESSION["errors"];
|
||||
|
||||
foreach ($errors as $error => $error_reading) {
|
||||
?>
|
||||
<br>
|
||||
<p class="error"><?php echo $error_reading ?></p>
|
||||
<?php
|
||||
}
|
||||
|
||||
unset($_SESSION["errors"]);
|
||||
}
|
||||
65
includes/login.inc.php
Normal file
65
includes/login.inc.php
Normal file
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
if ($_SERVER["REQUEST_METHOD"] == "POST") {
|
||||
|
||||
$inputs = [
|
||||
"username" => $_POST["username"],
|
||||
"pwd" => $_POST["pwd"]
|
||||
];
|
||||
|
||||
try {
|
||||
require_once "dbh.inc.php";
|
||||
require_once "login_model.inc.php";
|
||||
require_once "login_view.inc.php";
|
||||
require_once "login_contr.inc.php";
|
||||
|
||||
$errors = [];
|
||||
|
||||
if (isEmpty($inputs)) {
|
||||
$errors = ["null_field" => "Form contains blank characters."];
|
||||
}
|
||||
|
||||
if (!does_user_exist($pdo, $inputs["username"])) {
|
||||
$errors = ["incorrect_credentials" => "Incorrect credentials"];
|
||||
} else {
|
||||
if (!does_Password_match($pdo, $inputs["username"], $inputs["pwd"])){
|
||||
$errors = ["incorrect_credentials" => "Incorrect credentials"];
|
||||
}
|
||||
}
|
||||
|
||||
require_once "config_session.inc.php";
|
||||
|
||||
if ($errors) {
|
||||
|
||||
$_SESSION["errors"] = $errors;
|
||||
|
||||
// Send back form
|
||||
$entered_data = [
|
||||
"username" => $inputs["username"]
|
||||
];
|
||||
|
||||
$_SESSION["entered_login_data"] = $entered_data;
|
||||
|
||||
header("Location: ../chat.php");
|
||||
|
||||
$inputs = null;
|
||||
die();
|
||||
}
|
||||
|
||||
$_SESSION["username"] = htmlspecialchars($inputs["username"]);
|
||||
|
||||
$inputs = null;
|
||||
header("Location: ../chat.php");
|
||||
die();
|
||||
|
||||
} catch (PDOException $e) {
|
||||
echo "An error has ocurred: " . $e->getMessage();
|
||||
exit();
|
||||
}
|
||||
} else {
|
||||
echo("Wrong request type, please check your request.");
|
||||
header("Location: ../chat.php");
|
||||
exit();
|
||||
}
|
||||
42
includes/login_contr.inc.php
Normal file
42
includes/login_contr.inc.php
Normal file
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
function isEmpty(array $array){
|
||||
foreach ($array as $value => $content) {
|
||||
if (empty($content)){
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function does_Password_match(object $pdo, string $username, string $pwd) {
|
||||
// Get hashed password from db
|
||||
function get_hashed_password(object $pdo, string $username) {
|
||||
$stored_pwd = retrieve_hashed_pwd($pdo, $username);
|
||||
|
||||
return $stored_pwd;
|
||||
}
|
||||
|
||||
$stored_pwd = get_hashed_password($pdo, $username);
|
||||
|
||||
if (password_verify($pwd,$stored_pwd)) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
function does_user_exist(object $pdo, string $username){
|
||||
if (search_user($pdo, $username)){
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
function print_errors(){
|
||||
check_errors();
|
||||
}
|
||||
34
includes/login_model.inc.php
Normal file
34
includes/login_model.inc.php
Normal file
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
function retrieve_hashed_pwd(object $pdo, string $username) {
|
||||
$query = "SELECT pwd FROM users WHERE username = :username;";
|
||||
|
||||
$stmt = $pdo->prepare($query);
|
||||
$stmt->bindParam(":username",$username);
|
||||
$stmt->execute();
|
||||
|
||||
$hashed_pwd = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
|
||||
$stmt = null;
|
||||
$query = null;
|
||||
|
||||
return $hashed_pwd["pwd"];
|
||||
}
|
||||
|
||||
function search_user(object $pdo, string $username) {
|
||||
$query = "SELECT * FROM users WHERE username = :username;";
|
||||
|
||||
$stmt = $pdo->prepare($query);
|
||||
$stmt->bindParam(":username",$username);
|
||||
$stmt->execute();
|
||||
|
||||
$result = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
|
||||
$stmt = null;
|
||||
$query = null;
|
||||
|
||||
return $result;
|
||||
|
||||
}
|
||||
3
includes/login_view.inc.php
Normal file
3
includes/login_view.inc.php
Normal file
@@ -0,0 +1,3 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
7
includes/logout.inc.php
Normal file
7
includes/logout.inc.php
Normal file
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
session_start();
|
||||
session_unset();
|
||||
session_destroy();
|
||||
|
||||
header("Location: ../chat.php");
|
||||
53
includes/register.inc.php
Normal file
53
includes/register.inc.php
Normal file
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
if ($_SERVER["REQUEST_METHOD"] == "POST") {
|
||||
$inputs = ["username"=>$_POST["username"],"pwd"=>$_POST["pwd"]];
|
||||
|
||||
try {
|
||||
require_once "dbh.inc.php";
|
||||
require_once "register_model.inc.php";
|
||||
require_once "register_view.inc.php";
|
||||
require_once "register_contr.inc.php";
|
||||
|
||||
// Error handlers
|
||||
$errors = [];
|
||||
if (isEmpty($inputs)) {
|
||||
$errors = ["null_field" => "Form contains blank characters."];
|
||||
}
|
||||
if (username_taken($pdo, $inputs["username"])) {
|
||||
$errors = ["username_taken" => "Username alredy taken"];
|
||||
}
|
||||
|
||||
require_once "config_session.inc.php";
|
||||
|
||||
if ($errors) {
|
||||
$_SESSION["errors"] = $errors;
|
||||
|
||||
// Send back form
|
||||
$entered_data = [
|
||||
"username" => $inputs["username"]
|
||||
];
|
||||
|
||||
$_SESSION["entered_register_data"] = $entered_data;
|
||||
|
||||
header("Location: ../chat.php");
|
||||
$inputs = null;
|
||||
die();
|
||||
}
|
||||
|
||||
user_register($pdo, $inputs["username"], $inputs["pwd"]);
|
||||
|
||||
header("Location: ../chat.php");
|
||||
$inputs = null;
|
||||
die();
|
||||
} catch (PDOException $e) {
|
||||
echo "An error has ocurred: " . $e->getMessage();
|
||||
exit();
|
||||
}
|
||||
|
||||
} else {
|
||||
echo "Wrong request type, please check your request.";
|
||||
exit();
|
||||
}
|
||||
34
includes/register_contr.inc.php
Normal file
34
includes/register_contr.inc.php
Normal file
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
function isEmpty(array $array){
|
||||
foreach ($array as $value => $content) {
|
||||
if (empty($content)){
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function username_taken(object $pdo, string $username){
|
||||
if (username_search($pdo, $username)) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
function hash_password(string $pwd) {
|
||||
$hashed_pw = password_hash($pwd,PASSWORD_BCRYPT,["cost"=>12]);
|
||||
return $hashed_pw;
|
||||
}
|
||||
|
||||
function user_register(object $pdo, string $username, string $pwd) {
|
||||
user_write($pdo, $username, hash_password($pwd));
|
||||
}
|
||||
|
||||
function print_errors(){
|
||||
check_errors();
|
||||
}
|
||||
31
includes/register_model.inc.php
Normal file
31
includes/register_model.inc.php
Normal file
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
function username_search(object $pdo, string $username) {
|
||||
$query = "SELECT username FROM users WHERE username = :username;";
|
||||
|
||||
// Prepare and check if user already exists
|
||||
$stmt = $pdo->prepare($query);
|
||||
$stmt->bindParam(":username",$username);
|
||||
$stmt->execute();
|
||||
$result = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
|
||||
$stmt = null;
|
||||
$query = null;
|
||||
|
||||
// True if something is found, false if not
|
||||
return $result;
|
||||
}
|
||||
|
||||
function user_write(object $pdo, string $username, string $pwd) {
|
||||
$query = "INSERT INTO users (username, pwd) VALUES (:username, :pwd)";
|
||||
|
||||
$stmt = $pdo->prepare($query);
|
||||
$stmt->bindParam(":username",$username);
|
||||
$stmt->bindParam(":pwd",$pwd);
|
||||
$stmt->execute();
|
||||
|
||||
$stmt = null;
|
||||
$query = null;
|
||||
}
|
||||
17
includes/register_view.inc.php
Normal file
17
includes/register_view.inc.php
Normal file
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
function render_register_form(){
|
||||
?>
|
||||
<input required type="text" id="username" name="username" placeholder="User" value=
|
||||
<?php if(isset($_SESSION["entered_register_data"])){
|
||||
echo $_SESSION["entered_register_data"]["username"];
|
||||
unset($_SESSION["entered_register_data"]);
|
||||
}
|
||||
?>>
|
||||
</input>
|
||||
<input required type="password" id="password" name="pwd" placeholder="Password"></input>
|
||||
<?php
|
||||
}
|
||||
|
||||
7
includes/users_db.sql
Normal file
7
includes/users_db.sql
Normal file
@@ -0,0 +1,7 @@
|
||||
CREATE TABLE users (
|
||||
id INT(11) NOT NULL AUTO_INCREMENT,
|
||||
username VARCHAR(255) NOT NULL,
|
||||
pwd VARCHAR(255) NOT NULL,
|
||||
created_at DATETIME DEFAULT CURRENT_TIME,
|
||||
PRIMARY KEY (id)
|
||||
);
|
||||
Reference in New Issue
Block a user