-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchat.php
More file actions
119 lines (108 loc) · 3.15 KB
/
chat.php
File metadata and controls
119 lines (108 loc) · 3.15 KB
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
<?php
ob_start();
session_start();
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
if(!$_SESSION['valid'])
header('Location: index.php');
if(file_exists('passwd'))
$dict = json_decode(file_get_contents('passwd'), true);
else
$dict = array();
if(!array_key_exists($_POST['to_user'], $dict)){
$_SESSION['to_user_DNE'] = true;
header('Location: homepage.php');
return;
}
if(file_exists('friends/'.$_SESSION['username']))
$friend = json_decode(file_get_contents('friends/'.$_SESSION['username']), true);
else
$friend = array();
if(!in_array($_POST['to_user'], $friend, true)){
array_push($friend, $_POST['to_user']);
file_put_contents('friends/'.$_SESSION['username'], json_encode($friend));
}
?>
<html>
<head>
<title>CN Project</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script>
function press_enter(event){
if (event.keyCode === 13) {
event.preventDefault();
$("#send").click();
}
}
var sent = true;
function send() {
sent = false;
$.post('send.php', { from_user: "<?php echo $_SESSION['username']?>", to_user: "<?php echo $_POST['to_user'];?>", message: $('#message').val()},
function(response){
sent = true;
}
);
$('#message').val('');
}
var pos = 0;
function get_msg(){
if(sent === false)
return;
sent = false;
$.post('server.php', { from_user: "<?php echo $_SESSION['username']?>", to_user: "<?php echo $_POST['to_user'];?>", pos: pos},
function(response){
sent = true;
if(response === undefined || response === '')
return;
var json = JSON.parse(response);
var lines = json.message.split("\n");
for (var i = 0; i<lines.length - 1; i++){
var user = lines[i].slice(0, lines[i].indexOf(' '));
var msg = decodeURIComponent(lines[i].slice(lines[i].indexOf(' ')+1).replace(/[0-9a-f]{2}/g, '%$&'));
//console.log(msg);
$('#history').append('<li>'+user+': '+msg+'</li>');
}
pos = json.pos;
}
);
}
setInterval(get_msg, 1000);
function upload(files){
if(files === undefined)
return;
console.log(files);
sent = false;
for (var i = 0; i< files.length; i++){
let form = new FormData();
form.append('from_user', "<?php echo $_SESSION['username']?>");
form.append('to_user', "<?php echo $_POST['to_user']?>");
form.append('file', files[i]);
$.ajax({
url:'upload.php',
type: 'POST',
contentType: false,
processData: false,
data: form,
success: function(response){
sent = true;
},
error: function(response){
alert(response.responseText);
sent = true;
}
});
}
}
</script>
</head>
<body>
<h2><?php echo 'Chat with '.$_POST['to_user']; ?></h2>
<button onclick="window.location.assign('homepage.php')">back</button>
<ul id='history'></ul>
<input type='text' name='message' id='message' onkeyup="press_enter(event)">
<button id='send' onclick="send()">send</button>
<input type='file' id='uploader' onchange="upload(this.files)" multiple/>
</body>
</html>