FVS-Social-Projekt-Neu/FVS-Social/chat.php

197 lines
7.4 KiB
PHP
Raw Normal View History

<?php
session_start();
if(!isset($_SESSION['user_id'])) {
die("<p>Du musst dich zuerst Einloggen oder Regestrieren <br> <a href='login/login-page.html'>Hier Einloggen</a> <br> <a href='signup/signup-page.html'>Hier Regestrieren</a>");
}
include "db_connect.php";
$username = $_GET['user'];
$userId = $_SESSION['user_id'];
try {
// Benutzerdaten abrufen
$stmt = $pdo->prepare("SELECT id, username, email, created_at FROM users WHERE id = :username");
$stmt->execute([':username' => $username]);
$selected_user = $stmt->fetch(PDO::FETCH_ASSOC);
$stmt1 = $pdo->prepare("SELECT username, email, created_at FROM users WHERE id = :id");
$stmt1->execute([':id' => $userId]);
$user = $stmt1->fetch(PDO::FETCH_ASSOC);
$stmt2 = $pdo->prepare("SELECT file_path FROM profile_pictures WHERE user_id = :user_id");
$stmt2->execute([':user_id' => $userId]);
$profile_pic2 = $stmt2->fetch(PDO::FETCH_ASSOC);
$stmt3 = $pdo->prepare("
SELECT users.id, users.username, profile_pictures.file_path
FROM users
LEFT JOIN profile_pictures ON users.id = profile_pictures.user_id
ORDER BY profile_pictures.uploaded_at DESC
");
$stmt3->execute();
$users = $stmt3->fetchAll(PDO::FETCH_ASSOC);
$stmt2 = $pdo->prepare("SELECT file_path FROM profile_pictures WHERE user_id = :user_id");
$stmt2->execute([':user_id' => $selected_user['id']]);
$profile_pic = $stmt2->fetch(PDO::FETCH_ASSOC);
if ($profile_pic) {
$selected_profile_image = $profile_pic['file_path'];
} else {
$selected_profile_image = 'profile-pics/default.jpeg';
}
if ($profile_pic2) {
$selected_profile_image1 = $profile_pic2['file_path'];
} else {
$selected_profile_image1 = 'profile-pics/default.jpeg';
}
$stmt3 = $pdo->prepare("SELECT COUNT(following_id) AS following_count FROM followers WHERE following_id = :user_id ");
$stmt3->execute([':user_id' => $selected_user['id']]);
$follower_count = $stmt3->fetch(PDO::FETCH_ASSOC)['following_count'];
$sender_id = $_SESSION['user_id'];
$receiver_id = $_GET['user'];
} catch (PDOException $e) {
die("Fehler: " . $e->getMessage());
}
?>
<!DOCTYPE html>
<html lang="de">
<head>
<script>
function loadMessages() {
let chatBox = document.querySelector('.chat-box');
// Aktuelle Scroll-Position speichern
let scrollPosition = chatBox.scrollTop;
let user = <?= json_encode($receiver_id); ?>;
fetch('get_messages.php?user=' + user)
.then(response => response.text())
.then(data => {
chatBox.innerHTML = data;
// Scroll-Position wiederherstellen
chatBox.scrollTop = scrollPosition;
});
}
// Alle 2 Sekunden neue Nachrichten abrufen
setInterval(loadMessages, 2000);
</script>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Profil von <?php echo htmlspecialchars($selected_user['username']); ?></title>
<link rel="stylesheet" href="style.css?v=<?php echo time(); ?>">
</head>
<body>
<div class="container">
<div class="col-3">
<div class="navigation-body">
<div class="profile-name-header">
<?php echo "<img class = 'profile-pictures-header' src='$selected_profile_image1' alt='pv' width='50'>";?>
<h1>
<a href="">@<?php echo htmlspecialchars($user['username']); ?></a>
</h1>
</div>
<ul class="icons">
<li><a href="index.php"><img src="icons/home.svg" alt="">Home</a></li>
<li><a href="profile.php?user=<?= htmlspecialchars($user['username']) ?>"><img src="icons/user.svg" alt="">Profil</a></li>
<li><a href="chat.php"><img src="icons/envelope.svg" alt="">Nachrichten</a></li>
<li><a href="logout.php"><img src="icons/exit.svg" alt="">Abmelden</a></li>
<a href="chat.php?user=<?= htmlspecialchars($benutzer['id']) ?>"><?= htmlspecialchars($benutzer['username']) ?></a>
</ul>
</div>
<div class="navigation-body">
<ul>
<?php foreach ($users as $benutzer):?>
<?php if($benutzer['id'] != $userId):?>
<li>
<form class="user-card" action="profile.php" method="post">
<a href="chat.php?user=<?= htmlspecialchars($benutzer['id']) ?>"> <img class="profile-pictures" src="<?= htmlspecialchars($benutzer['file_path']) ?: 'profile-pics/default.jpeg' ?>" alt="Profilbild"><?= htmlspecialchars($benutzer['username']) ?></a>
</form>
</li>
<?php endif; ?>
<?php endforeach; ?>
</ul>
</div>
</div>
<div class="profile-body col-9">
<?php if($receiver_id):?>
<div class="profile-header">
<h1>Profil von <?php echo htmlspecialchars($selected_user['username']); ?></h1>
<img src="<?php echo htmlspecialchars($selected_profile_image); ?>" alt="Profilbild" width="100">
<p>Email: <?php echo htmlspecialchars($selected_user['email']); ?></p>
<p>Registriert seit: <?php echo htmlspecialchars($selected_user['created_at']); ?></p>
<h3>Follower: <?php echo htmlspecialchars($follower_count) ?></h3>
<a href="index.php">Zurück zur Startseite</a>
</div>
<div class="chat">
<?php
$sender_id = $_SESSION['user_id'];
$receiver_id = $_GET['user'];
// Nachrichten abrufen
$stmt = $pdo->prepare("SELECT * FROM messages
WHERE (sender_id = :sender_id AND receiver_id = :receiver_id)
OR (sender_id = :receiver_id AND receiver_id = :sender_id)
ORDER BY sent_at ASC");
$stmt->execute([
':sender_id' => $sender_id,
':receiver_id' => $receiver_id
]);
$messages = $stmt->fetchAll(PDO::FETCH_ASSOC);
$stmt3324 = $pdo->prepare("SELECT * FROM users WHERE id = :user_id");
$stmt3324->execute([':user_id' => $receiver_id]);
$receiver = $stmt3324->fetch(PDO::FETCH_ASSOC);
?>
<h2>Chat mit Benutzer <?= htmlspecialchars($receiver['username']); ?></h2>
<div class="chat-box" style="border:1px solid black; height:300px; overflow-y:scroll;">
<?php foreach ($messages as $msg): ?>
<p><strong><?= ($msg['sender_id'] == $sender_id) ? "Du" : htmlspecialchars($receiver['username']); ?>:</strong> <?= htmlspecialchars($msg['message']); ?>
<small><?= $msg['sent_at']; ?></small>
</p>
<?php endforeach; ?>
</div>
<form action="send_message.php" method="post" onsubmit="clearMessageBox()">
<input type="hidden" name="receiver_id" value="<?= htmlspecialchars($receiver_id); ?>">
<textarea name="message" required placeholder="Nachricht schreiben..."></textarea>
<button type="submit">Senden</button>
</form>
</div>
<?php endif; ?>
</div>
</div>
</body>
</html>