-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessages.php
79 lines (65 loc) · 2.7 KB
/
messages.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Copyright 2024 PianoMan0
<?php
session_start();
// Require users to log in.
if (!isset($_SESSION['user_id'])) {
header('Location: login.php');
exit;
}
// Connect to the database
$db = new PDO('sqlite:posts.db');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$profile_id = $_SESSION['user_id'];
// Get direct messages between the logged-in user and the user whose profile is being viewed
$stmt = $db->prepare("
SELECT messages.*, t1.username AS from_username, t2.username AS to_username FROM messages
JOIN users t1 ON t1.id = messages.from_user_id
JOIN users t2 ON t2.id = messages.to_user_id
WHERE (to_user_id = :profile_id) OR (from_user_id = :profile_id)
ORDER BY timestamp DESC
");
$stmt->bindParam(':profile_id', $profile_id);
$stmt->execute();
$messages = $stmt->fetchAll(PDO::FETCH_ASSOC);
?><!DOCTYPE html>
<html lang="en">
<head>
<meta name='viewport' content='width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0, target-densityDpi=device-dpi, minimal-ui' />
<title>Billion - Messages for <?=$profile['username'];?></title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="logout"><a href="index.php?action=logout">Logout</a></div>
<a href="index.php"><img src="billion_small.png" height=100 style="margin-bottom:15px"></a><br>
<h2>Direct Messages</h2>
<style>
.self-bg {
background-color: #f1e6ff;
}
</style>
<?php if (!empty($messages)): ?>
<ul>
<?php foreach ($messages as $message): ?>
<li <?php if ($message['from_user_id'] == $_SESSION['user_id']) { ?>class="self-bg" <?php } ?>>
<div class="post-content">
<?php echo htmlspecialchars($message['message']); ?>
</div>
<div class="post-footer">
<strong>
<?php if ($message['from_user_id'] == $_SESSION['user_id']) { ?>
To <a href="profile.php?id=<?=$message['to_user_id'];?>"><?php echo htmlspecialchars($message['to_username']); ?></a>
<?php } else { ?>
From <a href="profile.php?id=<?=$message['from_user_id'];?>"><?php echo htmlspecialchars($message['from_username']); ?></a>
<?php } ?>
</strong>
<em>(<?php echo $message['timestamp']; ?>)</em>
</div>
</li>
<?php endforeach; ?>
</ul>
<?php else: ?>
<p>No messages yet.</p>
<?php endif; ?>
<div style="margin-top:50px"><a href="index.php">« Back to News Feed</a></div>
</body>
</html>