57 lines
1.6 KiB
PHP
57 lines
1.6 KiB
PHP
<?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();
|
|
} |