-
Notifications
You must be signed in to change notification settings - Fork 4
/
messages.php
306 lines (245 loc) · 12.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
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
<?php
ob_start();
session_start();
$title = "Messages";
include "init.php";
if(isset($_SESSION["username"])){
if(isset($_GET["target"]) && !empty(trim($_GET["target"]))){
$tmp0 = $_GET["target"];
$tmp1 = strtolower($_SESSION["username"]);
$checkTarget = getAllFrom("username","users","WHERE username = '$tmp0' AND username != '$tmp1'");
if(!empty($checkTarget))
$_SESSION["other_user"] = $_GET["target"];
else
{
$user1=$_SESSION["username"];
$user_array=array();
$other_user_selected = getMessages($user1);
foreach($other_user_selected as $row){
if(strcmp($user1,$row["user1"])==0){
array_push($user_array,$row["user2"]);
}else{
array_push($user_array,$row["user1"]);
}
}
$user_array=array_unique($user_array);
$_SESSION["user_array"]=$user_array;
$user= array_shift($user_array);
array_unshift($user_array,$user);
$_SESSION["other_user"]=$user;
}
}
else{
$user1=$_SESSION["username"];
$user_array=array();
$other_user_selected = getMessages($user1);
foreach($other_user_selected as $row){
if(strcmp($user1,$row["user1"])==0){
array_push($user_array,$row["user2"]);
}else{
array_push($user_array,$row["user1"]);
}
}
$user_array=array_unique($user_array);
$_SESSION["user_array"]=$user_array;
$user= array_shift($user_array);
array_unshift($user_array,$user);
$_SESSION["other_user"]=$user;
}
$target = $_SESSION["other_user"];
?>
<div id="frame" class="w3-margin-bottom w3-card">
<div id="sidepanel">
<div id="search">
<label for=""><i class="fa fa-search" aria-hidden="true"></i></label>
<input id="search_input" type="text" placeholder="Rechercher contacts..." />
</div>
<div id="contacts">
<ul id="ul_messages" style="padding:2px;">
<?php
$connectedUser = $_SESSION["username"]; /*user*/
$user_array=array();
$getMessages = getMessages($connectedUser);
foreach($getMessages as $message){
if(strcmp($connectedUser,$message["user1"])==0){
array_push($user_array,$message["user2"]);
}else{
array_push($user_array,$message["user1"]);
};
}
$user_array=array_unique($user_array);
if(!empty($user_array)){
foreach($user_array as $user){
if (strcmp($_SESSION["username"], $user) > 0) {
$user1 = $user;
$user = $_SESSION["username"];
}
else
{
$user1 = $_SESSION["username"];
}
$rows = getLatestMessage($user1, $user);
if (strcmp($_SESSION["username"], $rows[0]["user1"]) == 0) {
$username = $user;
}
else
{
$username = $rows[0]["user1"];
};
$image = getAllFrom("*","users","WHERE username ='$username'");
echo "<a class='li-messages' class='w3-hover-text-white' style='text-decoration:none' href='messages.php?target=" . $username . "' ><li id='li_messages' name=\"". $username ."\" class=\"contact"; echo"\" >
<div class='wrap'>";
if(isset($image[0]["image"]) && !empty(trim($image[0]["image"])))
echo "<img src='uploads/profile_pictures/".$image[0]["image"]."' alt=\"\" />";
else
echo "<img src='uploads/profile_pictures/user.png' alt=\"\" />";
echo "<div class=\"meta\">
<p class=\"name\">".$username."</p>";
if(($rows[0]["lue"]==0) && $user ==$rows[0]["sender"]){echo "<p class=\"previews\" style='font-weight: bolder;font-size: 18px'>".$rows[0]['message']."</p>";}
else {echo"<p class=\"previews\">".$rows[0]['message']."</p>";};
echo"
</div>
</div>
</li><a>";
};
}
?>
</ul>
</div>
</div>
<div class="content w3-white">
<?php $userImage = getAllFrom("*","users","WHERE username = '$target'");
if(!empty($userImage)){
?>
<div class="contact-profile">
<a href="profile.php?do=show&userID=<?php echo $userImage[0]["userID"] ?>"><img src="uploads/profile_pictures/<?php if(!empty($userImage[0]["image"])){ echo $userImage[0]["image"];} else echo "user.png" ?>" alt="profile picture" />
<p><?php echo $_SESSION["other_user"];?></p></a>
</div>
<?php } ?>
<div class="messages" style="width:100%">
<ul>
<?php
$user1=$_SESSION["username"];
$user2=$_SESSION["other_user"];
$other_user=$_SESSION["other_user"];
if(strcmp($user1,$user2)>0){
$user2=$_SESSION["username"];
$user1=$_SESSION["other_user"];
};
updateLue($user1, $user2, $other_user);
$rows = getPersonalMessages($user1, $user2);
if(!empty($rows)){
foreach($rows as $row){
if($row['message_type']==0){
if($row['sender'] == $_SESSION['username']){
if(!empty(trim($_SESSION['image'])))
echo " <li class=\"sent\"> <img src=\"uploads/profile_pictures/".$_SESSION['image']."\" alt='me'/> <p class=\"chat-message\">".$row["message"]."</p> </li>";
else
echo " <li class=\"sent\"> <img src=\"uploads/profile_pictures/user.png\" alt='me'/> <p class=\"chat-message\">".$row["message"]."</p> </li>";
}
else{
if(!empty(trim($userImage[0]["image"])))
echo " <li class=\"replies\"> <img src=\"uploads/profile_pictures/".$userImage[0]["image"]."\" alt='him'/> <p class=\"chat-message\">".$row["message"]."</p> </li>";
else
echo " <li class=\"replies\"> <img src=\"uploads/profile_pictures/user.png\" alt='him'/> <p class=\"chat-message\">".$row["message"]."</p> </li>";
}
}
else
{
if($row['sender'] == $_SESSION['username']){
if(!empty(trim($_SESSION['image'])))
echo " <li class=\"sent\"> <img src=\"uploads/profile_pictures/".$_SESSION['image']."\" alt='me'/> <p class=\"chat-message\"><img id='chat_image' style='cursor: pointer;border-radius:0;width: -webkit-fill-available; height: -webkit-fill-available;' src='uploads/sent_pictures/".$row["message"]."' alt='me'></p> </li>";
else
echo " <li class=\"sent\"> <img src=\"uploads/profile_pictures/user.png\" alt='me'/> <p class=\"chat-message\"><img id='chat_image' style='cursor: pointer;border-radius:0;width: -webkit-fill-available; height: -webkit-fill-available;' src='uploads/sent_pictures/".$row["message"]."' alt='me'></p> </li>";
}
else{
if(!empty(trim($userImage[0]["image"])))
echo " <li class=\"replies\"> <img src=\"uploads/profile_pictures/".$userImage[0]["image"]."\" alt='him'/> <p class=\"chat-message\"><img id='chat_image' style='cursor: pointer;border-radius:0;width: -webkit-fill-available; height: -webkit-fill-available;' src='uploads/sent_pictures/".$row["message"]."' alt='me'></p> </li>";
else
echo " <li class=\"replies\"> <img src=\"uploads/profile_pictures/user.png\" alt='him'/> <p class=\"chat-message\"><img id='chat_image' style='cursor: pointer;border-radius:0;width: -webkit-fill-available; height: -webkit-fill-available;' src='uploads/sent_pictures/".$row["message"]."' alt='me'></p> </li>";
}
}
}
}
else { ?>
<div class="w3-container w3-white">
<div class="alert alert-info"><?php echo lang("NO-CONV") ?></div>
</div>
<?php }
?>
</ul>
</div>
<div class="message-input w3-card w3-light-grey">
<form method="post" action="save_chat.php" class="wrap" enctype="multipart/form-data" >
<input type="text" placeholder="Write your message..." name="txtmessage"/>
<input type="file" name="file" id="file" hidden="hidden">
<i id="uploadBTN" class="fa fa-paperclip attachment" aria-hidden="true">
</i>
<button type="submit" name="send_message" class="submit">
<i class="fa fa-paper-plane" aria-hidden="true" >
</i>
</button>
</form>
</div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
const realFileBtn=document.getElementById("file");
const fakeBtn=document.getElementById("uploadBTN");
fakeBtn.addEventListener("click",function () {
realFileBtn.click();
});
$(document).ready(function () {
$(document).on('change','#file',function () {
var property=document.getElementById("file").files[0];
var image_ex=property.name.split(".").pop().toLowerCase();
if(jQuery.inArray(image_ex,["jpg","png","gif","jpeg"]) == -1) {
alert("<?php echo lang("NOT-ALLOWED-EXT") ?>");
}else {
var image_size = property.size;
if (image_size > 200000) {
alert("<?php echo lang("BIG-SIZE") ?>");
}else{
var form_data=new FormData();
form_data.append("file",property);
$.ajax({
url:"save_image.php",
method:"POST",
data:form_data,
contentType:false,
cache:false,
processData:false,
success:function () {
window.location.href = '<?php echo "messages.php?target=" . $_SESSION["other_user"] ?>' ;
}
});
}
}
});
});
</script>
<script>
$(document).ready(function () {
$(document).on("click","#chat_image",function () {
var img=$(this).attr('src');
var appear_image="<div id='modal' class='w3-modal w3-black' style='display:block'><div class='w3-modal-content w3-animate-zoom w3-center w3-transparent w3-padding-64' id='appear_image_div' onclick='closeImage()'><img class='w3-image' alt='image' id='appear_image' src='"+img+"'></div></div>";
$('body').append(appear_image);
});
});
function closeImage() {
$('#modal').remove();
$('#appear_image_div').remove();
$('#appear_image').remove();
}
</script>
</div>
</div>
<?php
}
else
{
header("location:login.php");
exit();
}
include $tmp . "footer.php";
ob_end_flush();
?>