forked from Mayuresh-22/Minglr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
message.php
250 lines (227 loc) · 10.5 KB
/
message.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
<?php
include("db/connection.php");
include("back/env.php");
// starting session
session_start();
// redirecting user if not logged-in
if(!isset($_SESSION['username']) || !isset($_GET['recp2']) || !isset($_SESSION['id'])){
echo "
<script>
alert('Error occurred please Login to continue');
window.location = '$home_page';
</script>
";
}
// getting GET & SESSION variables
$recp1 = $_SESSION['id'];
$recp2 = $_GET['recp2'];
// $recp1 = 7;
// $recp2 = 1;
//set $username for both the recipients
// creating associative array to access username with id
$username = array();
$name = array();
$status = array();
$sql = "SELECT `username` FROM `users` WHERE `id` = $recp1";
$result = mysqli_query($connection, $sql);
$rows = mysqli_fetch_all($result);
// setting username and name for recipient 1
$username[$recp1] = $rows[0][0];
$name[$recp1] = "Me";
$status[$recp1] = 1;
$sql = "SELECT `username`, `fname`, `status` FROM `users` WHERE `id` = $recp2";
$result = mysqli_query($connection, $sql);
$rows = mysqli_fetch_all($result);
// setting username and name for recipient 2
$username[$recp2] = $rows[0][0];
$name[$recp2] = $rows[0][1];
$status[$recp2] = $rows[0][2];
// checking whether the room with recipient already exits or not
$sql = "SELECT * FROM `rooms` WHERE (`recp1` = ".$recp1." OR `recp1` = ".$recp2.") AND (`recp2` = ".$recp1." OR `recp2` = ".$recp2.");";
$result = mysqli_query($connection, $sql);
if(mysqli_num_rows($result) == 1){
$rows = mysqli_fetch_assoc($result);
// set $room_id
$room_id = $rows['room_id'];
} else if(mysqli_num_rows($result) == 0){
// creating new room for the recipients
$sql = "INSERT INTO `rooms` (`room_id`, `recp1`, `recp2`) VALUES (NULL, '".$recp1."', '".$recp2."');";
$result = mysqli_query($connection, $sql);
// set $room_id
$sql = "SELECT `room_id` FROM `rooms` WHERE `recp1` = ".$recp1." AND `recp2` = ".$recp2.";";
$result = mysqli_query($connection, $sql);
$rows = mysqli_fetch_assoc($result);
$room_id = $rows['room_id'];
}
?>
<html>
<title>Minglr - Get Connected</title>
<head>
<!-- <meta charset="UTF-8"> -->
<!-- <meta http-equiv="X-UA-Compatible" content="IE=edge"> -->
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- <link rel="stylesheet" href="style/style.css">
<link rel="stylesheet" href="style/message.css"> -->
<!-- Dark theme css -->
<link rel="stylesheet" href="style/lighttheme_css/light_style.css?t=<?php echo time();?>">
<link rel="stylesheet" href="style/lighttheme_css/light_message.css?t=<?php echo time();?>" id="theme">
<!-- favicon -->
<link rel="shortcut icon" href="logo/minglr logo4.png" type="image/png">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Poppins">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css" integrity="sha512-ZvHjXoebDRUrTnKh9WKpWV/A0Amd+fjub5TkBXrPxe5F7WfDZL0slJ6a0mvg7VSN3qdpgqq2y1blz06Q8W2Y8A==" crossorigin="anonymous" referrerpolicy="no-referrer" />
<script src="https://kit.fontawesome.com/17a4e5185f.js" crossorigin="anonymous"></script>
</head>
<body>
<nav>
<div class="menu-btn">
<div class="bar bar1"></div>
<div class="bar bar2"></div>
<div class="bar bar3"></div>
</div>
<label class="logo"><a href="/"><img class="logo" src="logo/Minglr logo1.png"></a></label>
<ul>
<img src="img/dark_img/MoonIcon.svg" alt="Theme Icon" height="19" width="19" id="theme-icon" id="theme-toggle" class="theme-button" onclick="changeTheme()">
</ul>
<ul class="menu-items">
<li class="menu-items-li"><a class="navv-item" href="feed.php">Feed</a></li>
<li class="menu-items-li">
<?php
if(isset($_SESSION['username'])){
echo '<a class="navv-item" href="account.php?username='.$_SESSION['username'].'" ">Account</a>';
}else{
echo '<a class="navv-item" href="account.php">Account</a>';
}
?>
</li>
<li class="menu-items-li">
<?php
if(!isset($_SESSION['username'])){
echo '<a class="navv-item" href="index.php">Login</a>';
}
else{
echo '<a class="navv-item" href="back/logout.php">Logout</a>';
}
?>
</li>
<li class="menu-items-li"><a class="navv-item" href="about-us.php">About Us</a></li>
</ul>
</nav>
<div class="seperate_header"></div>
<!-- <div class="navbar">
<ul>
<li>
<a href="<?php echo $home_page; ?>"><img class="logo" src="logo/logo.png"></a>
</li>
<li class="nav-item">
<a href="feed.php" style="text-decoration: none">Feed</a>
</li>
<li class="nav-item">
<?php
if(!isset($_SESSION['username'])){
echo '<a href="account.php" style="text-decoration: none;">Account</a>';
}
else{
echo '<a href="account.php?username='.$_SESSION["username"].'" style="text-decoration: none;">Account</a>';
}
?>
</li>
<li class="nav-item">
<?php
if(!isset($_SESSION['username'])){
echo '<a href="'.$home_page.'" style="text-decoration: none;">Login</a>';
}
else{
echo '<a href="back/logout.php" style="text-decoration: none;">Logout</a>';
}
?>
</li>
</ul>
</div> -->
<div class="message-body">
<div class="message-window">
<div class="message-window-head">
<ul>
<li>
<a href="account.php?username=<?php if($username[$recp1] != $_SESSION['username']){echo $username[$recp1];} else{echo $username[$recp2];} ?>" style="text-decoration: none;"><img src="img/user.png" alt="profile" class="<?php if($status[$recp2]==1){echo "message-account-profpic-online";} else{echo "message-account-profpic-offline";}?>"></a>
</li>
<li>
<?php if($username[$recp1] != $_SESSION['username']){echo "<b>".$name[$recp1]."</b>";} else{echo "<b>".$name[$recp2]."</b>";} ?>
<?php if($status[$recp2]==1){echo "<small><br>online</small>";} else{echo "<small><br>offline</small>";} ?>
</li>
</ul>
</div>
<div class="message-window-message-box" id="message-window-message-box">
<?php
// fetching all the messages for room_id
$sql = "SELECT `author`, `message`, `dos` FROM `messages` WHERE `room_id` = ".$room_id.";";
$result = mysqli_query($connection, $sql);
if(mysqli_num_rows($result) > 0){
$rows = mysqli_fetch_all($result);
foreach($rows as $row){
echo '<div class="message-window-message-display-box">
<div class="message-window-message-display-box-head">
<ul>
<li style="padding-left: 0px; padding-right: 10px;">
<a href="account.php?username='.$username[$row[0]].'" style="text-decoration: none;">'.$name[$row[0]].'</a>
</li>
</ul>
</div>
<div class="message-window-message-display-box-message">
'.str_replace("\n", "<br>", $row[1]).'
</div>
</div>';
}
} else{
echo "<center><small>Start new Conversation</small></center>";
}
?>
</div>
<script>
document.getElementById("message-window-message-box").scrollTo(0, document.getElementById("message-window-message-box").scrollHeight);
</script>
<div class="message-window-input-message">
<form action="pvtmsg.php" method="POST">
<input type="text" name="message" id="message" placeholder="type message..." required>
<input type="hidden" name="author" id="author" value="<?php echo $recp1; ?>">
<input type="hidden" name="room" id="room" value="<?php echo $room_id; ?>">
<input type="hidden" name="recp1" id="recp1" value="<?php echo $recp1; ?>">
<input type="hidden" name="recp2" id="recp2" value="<?php echo $recp2; ?>">
</form>
</div>
</div>
</div>
<div class="footer" style="height:16rem">
<p style=" font-size: x-large;margin-top:0;">Minglr</p>
<ul class="footer-icons">
<li class="foot-item">
<a href="#" class="foot-link"><i class="fab fa-facebook"></i></a>
</li>
<li class="foot-item">
<a href="#" class="foot-link"><i class="fab fa-twitter"></i></a>
</li>
<li class="foot-item">
<a href="#" class="foot-link"><i class="fab fa-instagram"></i></a>
</li>
<li class="foot-item">
<a href="#" class="foot-link"><i class="fab fa-youtube"></i></a>
</li>
</ul>
<ul class="footer-links">
<li class="foot-item" style="margin-right:3rem;">
<a href="" class="foot-link">Home</a>
</li>
<li class="foot-item" style="margin-right:3rem;">
<a href="feed.php" class="foot-link">Feed</a>
</li>
<li class="foot-item" style="margin-right:3rem;">
<a href="account.php" class="foot-link">Account</a>
</li>
<li class="foot-item" style="margin-right:3rem;">
<a href="about-us.php" class="foot-link">About us</a>
</li>
</ul>
<p style="font-size:0.9rem;">This website is only for educational purposes and does not try to replicate any institution/entity/company - by Mayuresh Choudhary</p>
</div>
<script src="js/script.js"></script>
</body>
</html>