Newer
Older
<?php
// get-chat.php
session_start();
include_once "key/key.php"; // Include the configuration file
if (isset($_SESSION['unique_id'])) {
include_once "config.php";
$outgoing_id = mysqli_real_escape_string($conn, $_POST['outgoing_id']);
$incoming_id = mysqli_real_escape_string($conn, $_POST['incoming_id']);
$output = "";
$sql = "SELECT * FROM messages
LEFT JOIN users ON users.unique_id = messages.outgoing_msg_id
WHERE (outgoing_msg_id = {$outgoing_id} AND incoming_msg_id = {$incoming_id})
OR (outgoing_msg_id = {$incoming_id} AND incoming_msg_id = {$outgoing_id}) ORDER BY msg_id ";//ASC";
$query = mysqli_query($conn, $sql);
if (mysqli_num_rows($query) > 0) {
while ($row = mysqli_fetch_assoc($query)) {
// Decrypt the message before displaying it
$decrypted_message = decryptMessage($row['msg'], $encryptionKey);
if ($row['outgoing_msg_id'] === $outgoing_id) {
$output .= '<div class="chat-outgoing">
<div class="details">
<p>' . $decrypted_message . '</p>
</div>
</div>';
}
else {
// he is a message receiver
$output .= '<div class="chat-incoming">
<img src="php/images/' . $row['img'] . '" alt="">
} else {
header("Location: ../login.php"); // Fix the header function call
}
// Function to decrypt a message using OpenSSL
function decryptMessage($encrypted_message, $encryptionKey)
{
// Split IV and encrypted message
$data = base64_decode($encrypted_message);
$iv = substr($data, 0, 16);
$encrypted_message = substr($data, 16);
return openssl_decrypt($encrypted_message, 'aes-256-cbc', $encryptionKey, 0, $iv);
}
?>