-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchat.php
297 lines (262 loc) · 8.97 KB
/
chat.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
<?php
session_start();
include('funktionen.php');
check_session();
$user_id=$_SESSION['userid'];
//echo $user_id;
$mysqli=connect();
$result=sql("select name from msg.users where user_id=$user_id",$mysqli);
$row = $result->fetch_object(); //all chats of this person
/******************
get data about the message inside
******************/
$as=$row ->name;
?>
<!DOCTYPE html>
<html lang="en">
<!--------
BOOTSTRAP TEMPLATE
--------->
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="">
<link rel="icon" href="site_media/chat.ico">
<title><?php echo addslashes(strip_tags($_GET["name"]))?></title>
<!-- Bootstrap core CSS -->
<link href="vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
<!-- Custom styles for this template -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" crossorigin="anonymous">
</head>
<body onload="scroll();">
<!-- Navigation -->
<nav class="navbar navbar-expand-lg navbar-dark bg-dark fixed-top">
<div class="container">
<a class="navbar-brand" href="#">
<img src="http://placehold.it/300x60?text=Logo" width="150" height="30" alt="">
</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarResponsive">
<ul class="navbar-nav ml-auto">
<li class="nav-item">
<a class="nav-link" href="main.php">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="about.php">About</a>
</li>
<li class="nav-item">
<a class="nav-link" href="logout.php">Logout</a>
</li>
<li class="nav-item ">
<a class="nav-link" href="user.php?u=<?php echo $user_id; ?>"><?php echo $as; ?>
</a>
</li>
</ul>
</div>
</div>
</nav>
<script src="vendor/jquery/jquery.min.js"></script>
<script src="vendor/popper/popper.min.js"></script>
<script src="vendor/bootstrap/js/bootstrap.min.js"></script>
<!--------
BOOTSTRAP TEMPLATE
--------->
<!-- Page Content -->
<div class="container">
<h1 class="mt-5">Chat with <?php echo addslashes(strip_tags($_GET["name"])) ?></h1><?php
$go;
/******************
get variables
******************/
$userid=$_SESSION['userid'];
$chat_id=$_GET['chat_id'];
/******************
dispose userdata
******************/
$userid =addslashes(strip_tags($userid));
$chat_id=addslashes(strip_tags($chat_id));
/******************
Check user-rights
******************/
$mysqli=connect();
$result=sql("select 1 from msg.chats where user_id=$userid and chat_id=$chat_id",$mysqli);
$rows_returned = $result->num_rows;
if($rows_returned==0){//er ist nicht teil des chats, darf ihn also nicht öffnen
weiterleiten('main.php');
}
/*****************
load chat
******************/
$result=sql("select *
from msg.messages ms
join msg.chats ch on(ch.chat_id=ms.chat_id)
join msg.message_status mst on(mst.message_id=ms.message_id)
where ms.chat_id=$chat_id
and exists(select 1
from msg.chats ch2
where ch2.chat_id=ch.chat_id
and ch2.user_id=$userid
)
and mst.user_id=$userid
and ch.user_id=$userid
",$mysqli);
$rows_returned = $result->num_rows;
?>
<div id="result">
<?php
$message_id=0;//falls er keine fi ndet ist die msg_id=0;
while($row = $result->fetch_object()){ //alle nachrichten im chat
/******************
get data about the message
******************/
$message_id=$row ->message_id;
$status=$row ->status;
$text=$row ->text;
$from=$row ->from_user;
$sent=$row ->sent;
/*****************
processing & visualizing
******************/
if($status!=='seen'){//ungesehene nachrichten müssen hervorgehoben werden!
sql("update msg.message_status set status='seen' , pinged=true where message_id=$message_id and user_id=$userid",$mysqli);
}
if($from==$user_id){
$result2=sql("select * from msg.message_status where message_id=$message_id and user_id!=$user_id",$mysqli);
$row2 = $result2->fetch_object();
$status=$row2 ->status;
}
show_msg($text,$from,$status,$sent) ;
}
$param="chat_id=$chat_id&nm=1";
function show_msg($text,$from,$status,$sent){
if($from==$_SESSION['userid']){
echo'<div style="width: 200px; padding-top: 0; padding-bottom: 0; " class="alert alert-success">
<span style="margin-bottom: 0" class="text-left">
'.$text.'
</span>
<p style="margin-top:0;margin-bottom:0;" class="text-right text-top font-italic">
<small>sent '.$sent." ".$status.'</small>
</p>
</div>';
}else{//nachricht von wem anders
echo'<div class="alert alert-warning" style="width: 200px; padding-top: 0; padding-bottom: 0; ">
<span style="margin-bottom: 0" class="text-left">
'.$text.'
</span>
<p style="margin-top:0;margin-bottom:0;" class="text-right text-top font-italic">
<small>sent '.$sent.'</small>
</p>
</div><p>';
}
}
?>
<script type="text/javascript" src="jquery.js"></script>
<script charset="UTF-8">
function scroll(){
window.scrollTo(0,document.body.scrollHeight);
}
var container_content="";
var highest_message=0;
var msg_id='';
(function($)
{
$(document).ready(function()
{
$("#message").keypress(function (e) {
if(e.which==13){
send_msg();
}
});
$.ajaxSetup(
{
cache: false,
beforeSend: function() {
//$('#content').hide();
$('#loading').show();
},
complete: function() {
//$('#loading').hide();
$('#content').show();
},
success: function() {
//$('#loading').hide();
$('#content').show();
}
});
var $container = $("#content");
var refreshId = setInterval(function()
{
$.get("demo_sse.php?c=<?php echo$chat_id;?>&m=<?php echo $message_id; ?>&h_m="+highest_message, function(data){
if(data.search("<msg>">(-1))){
var dauer_load="";
var messages = data.split('<msg>');
for (var i = 0; i < messages.length-1; i++) {
messages[i] = messages[i].split('<attribute>');
var message_id=messages[i][0].trim();
var status =messages[i][1].trim();
var text =messages[i][2].trim();
var cfrom =messages[i][3].trim();
var type =messages[i][4].trim();
var sent =messages[i][5].trim();
if(status=="seen"){
highest_message=message_id;
if(cfrom!=<?php echo $user_id; ?>){
container_content=container_content+foreign_msg(text,sent);
}else{
container_content=container_content+my_msg(text,sent,status);
}
}else{
if(cfrom!=<?php echo $user_id; ?>){
dauer_load=dauer_load+foreign_msg(text,sent);
}else{
dauer_load=dauer_load+my_msg(text,sent,status);
}
}
}
}
/*if(container_content.length()>2000){
//you should consider refreshing the site
//force refresh?
}*/
$container.html(container_content+dauer_load);
},"text");
}, 5000);
});
})(jQuery);
function send_msg()
{
// wert aus dem input feld mit dem namen "message" auslesen
var message = $('input[name=message]').val();
if (message!=""){
// phpscript.php aufrufen über POST aufrufen
// entspricht: phpscript.php?a=callDoAnything&b=[Inhalt von message]
$.post("send_msg.php?c=<?php echo $chat_id; ?>",
{
a: 'callDoAnything',
b: message
},
function (data) {
$('input[name=message]').val('')
});
}
}
function my_msg(text,time,status){
return "<div style='width: 200px; padding-top: 0; padding-bottom: 0; align: left' class='alert alert-success'><span style='margin-bottom: 0' class='text-left'>"+
text+
"</span><p style='margin-top: 0; margin-bottom: 0'class='text-right text-top font-italic'><small>sent "+time+" "+status+"</small></p></div><p>";
}
function foreign_msg(text,time){
return "<div style='width: 200px; padding-top: 0; padding-bottom: 0; align: left' class='alert alert-warning'><span style='margin-bottom: 0' class='text-left'>"+
text+
"</span><p style='margin-top: 0; margin-bottom: 0'class='text-right text-top font-italic'><small>sent "+time+"</small></p></div><p>";
}
</script>
<div id="content"></div>
</div>
<input type ="Text" name="message" id="message" placeholder="new Message" >
<input type="button" id="snd" class="btn btn-secondary" value="Send" onclick="send_msg()" />
</div>
</body>