<?php
// Start or resume the session
session_start();

/*
Used to enhance security by preventing browsers and proxies from caching pages.
When a user logs out of a web application, you wouldn't want the next person using the same device to simply press the back button in the browser
and access the previous user's session or sensitive information.
This is particularly important in scenarios like shared devices, where multiple users might use the same browser sequentially.
However, it's noted that this method does not work for iOS users, which is be due to iOS's handling of cache control headers and
its aggressive caching strategies to improve user experience and performance.
*/
header("Cache-Control: no-cache, no-store, must-revalidate"); // HTTP 1.1.
header("Pragma: no-cache"); // HTTP 1.0.
header("Expires: 0"); // Proxies.

// We verify that the user is logged in, if not we redirect the user to index.php the log in page.
if (!isset($_SESSION['username'])) {
  header("Location: index.php");
  exit;
}

// Check if the request method is not POST
if ($_SERVER["REQUEST_METHOD"] != "POST") {
  // If the request method is not POST, exit the script with an error message indicating unauthorized access
  exit('Unauthorized access');
}

// Connecting to the database
require_once '/my_custom_url/PRIVATE/db_config.php';
$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
  die("Connection failure:" . $conn->connect_error);
}

// Retrieve form data
$messageContent = $_POST['message-to-send'];
$initials = $_POST['initials'];
$id_activ = $_POST['Beginning_activity_id'];
$id_activ2 = $_POST['Cease_activity_id'];

// Decode HTML entities in the message content, converting them back to their corresponding characters.
$messageContent = html_entity_decode($messageContent, ENT_QUOTES, 'UTF-8');

// Decode HTML entities in the initials, converting them back to their corresponding characters.
$initials = html_entity_decode($initials, ENT_QUOTES, 'UTF-8');

// Checks if the required data are present
if (!$messageContent || !$initials || !$id_activ) {
  // Handle the error - missing data
  die("Missing or incorrect data.");
}

// Prepare an SQL statement to insert data into the MESSAGES table, specifying placeholders for the values to be inserted
$sql = "INSERT INTO MESSAGES (content, initials, id_activ) VALUES (?, ?, ?)";
//  The prepared statement is stored in the variable $stmt for executio
$stmt = $conn->prepare($sql);

// Checks if the prepared statement ($stmt) is false or null.
if (!$stmt) {
  die("Error preparing statement: " . $conn->error);
}

// By using bind_param(), the code ensures that the data is properly sanitized and prevents SQL injection attacks.
$stmt->bind_param("ssi", $messageContent, $initials, $id_activ);

// Checks if the execution of the prepared statement ($stmt->execute()) fails.
if (!$stmt->execute()) {
  die("Error executing statement: " . $stmt->error);
}

/*
Dynamically generates a form with hidden input fields and submits it using JavaScript without user intervention.
*/
echo '<form id="postForm" action="stops_details.php" method="post">';
echo '<input type="hidden" name="Beginning_activity_id" value="' . htmlspecialchars($id_activ) . '">';
echo '<input type="hidden" name="Cease_activity_id" value="' . htmlspecialchars($id_activ2) . '">';
echo '</form>';
echo '<script>document.getElementById("postForm").submit();</script>';

// Releases the resources associated with the prepared statement
$stmt->close();
// Terminates the connection to the database server
$conn->close();

?>
