-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathindex.php
246 lines (228 loc) · 5.83 KB
/
index.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
<html>
<head>
<title>Messenger</title>
<style type="text/css">
html {
height: 100%;
}
body {
margin: 0px;
padding: 0px;
height: 100%;
font-family: Helvetica, Arial, Sans-serif;
font-size: 14px;
}
.msg-container {
width: 100%;
height: 100%;
}
.header {
width: 100%;
height: 30px;
border-bottom: 1px solid #CCC;
text-align: center;
padding: 15px 0px 5px;
font-size: 20px;
font-weight: normal;
}
.msg-area {
height: calc(100% - 102px);
width: 100%;
background-color:#FFF;
overflow-y: scroll;
}
.msginput {
padding: 5px;
margin: 10px;
font-size: 14px;
width: calc(100% - 20px);
outline: none;
}
.bottom {
width: 100%;
height: 50px;
position: fixed;
bottom: 0px;
border-top: 1px solid #CCC;
background-color: #EBEBEB;
}
#whitebg {
width: 100%;
height: 100%;
background-color: #FFF;
overflow-y: scroll;
opacity: 0.6;
display: none;
position: absolute;
top: 0px;
z-index: 1000;
}
#loginbox {
width: 600px;
height: 350px;
border: 1px solid #CCC;
background-color: #FFF;
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
z-index: 1001;
display: none;
}
h1 {
padding: 0px;
margin: 20px 0px 0px 0px;
text-align: center;
font-weight: normal;
}
button {
background-color: #43ACEC;
border: none;
color: #FFF;
font-size: 16px;
margin: 0px auto;
width: 150px;
}
.buttonp {
width: 150px;
margin: 0px auto;
}
.msg {
margin: 10px 10px;
background-color: #f1f0f0;
max-width: calc(45% - 20px);
color: #000;
padding: 10px;
font-size: 14px;
}
.msgfrom {
background-color: #0084ff;
color: #FFF;
margin: 10px 10px 10px 55%;
}
.msgarr {
width: 0;
height: 0;
border-left: 8px solid transparent;
border-right: 8px solid transparent;
border-bottom: 8px solid #f1f0f0;
transform: rotate(315deg);
margin: -12px 0px 0px 45px;
}
.msgarrfrom {
border-bottom: 8px solid #0084ff;
float: right;
margin-right: 45px;
}
.msgsentby {
color: #8C8C8C;
font-size: 12px;
margin: 4px 0px 0px 10px;
}
.msgsentbyfrom {
float: right;
margin-right: 12px;
}
</style>
</head>
<body onload="checkcookie(); update();">
<div id="whitebg"></div>
<div id="loginbox">
<h1>Pick a username:</h1>
<p><input type="text" name="pickusername" id="cusername" placeholder="Pick a username" class="msginput"></p>
<p class="buttonp"><button onclick="chooseusername()">Choose Username</button></p>
</div>
<div class="msg-container">
<div class="header">Messenger</div>
<div class="msg-area" id="msg-area"></div>
<div class="bottom"><input type="text" name="msginput" class="msginput" id="msginput" onkeydown="if (event.keyCode == 13) sendmsg()" value="" placeholder="Enter your message here ... (Press enter to send message)"></div>
</div>
<script type="text/javascript">
var msginput = document.getElementById("msginput");
var msgarea = document.getElementById("msg-area");
function chooseusername() {
var user = document.getElementById("cusername").value;
document.cookie="messengerUname=" + user
checkcookie()
}
function showlogin() {
document.getElementById("whitebg").style.display = "inline-block";
document.getElementById("loginbox").style.display = "inline-block";
}
function hideLogin() {
document.getElementById("whitebg").style.display = "none";
document.getElementById("loginbox").style.display = "none";
}
function checkcookie() {
if (document.cookie.indexOf("messengerUname") == -1) {
showlogin();
} else {
hideLogin();
}
}
function getcookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for(var i=0; i<ca.length; i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1);
if (c.indexOf(name) == 0) return c.substring(name.length,c.length);
}
return "";
}
function escapehtml(text) {
return text
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
}
function update() {
var xmlhttp=new XMLHttpRequest();
var username = getcookie("messengerUname");
var output = "";
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
var response = xmlhttp.responseText.split("\n")
var rl = response.length
var item = "";
for (var i = 0; i < rl; i++) {
item = response[i].split("\\")
if (item[1] != undefined) {
if (item[0] == username) {
output += "<div class=\"msgc\" style=\"margin-bottom: 30px;\"> <div class=\"msg msgfrom\">" + item[1] + "</div> <div class=\"msgarr msgarrfrom\"></div> <div class=\"msgsentby msgsentbyfrom\">Sent by " + item[0] + "</div> </div>";
} else {
output += "<div class=\"msgc\"> <div class=\"msg\">" + item[1] + "</div> <div class=\"msgarr\"></div> <div class=\"msgsentby\">Sent by " + item[0] + "</div> </div>";
}
}
}
msgarea.innerHTML = output;
msgarea.scrollTop = msgarea.scrollHeight;
}
}
xmlhttp.open("GET","get-messages.php?username=" + username,true);
xmlhttp.send();
}
function sendmsg() {
var message = msginput.value;
if (message != "") {
// alert(msgarea.innerHTML)
// alert(getcookie("messengerUname"))
var username = getcookie("messengerUname");
var xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
message = escapehtml(message)
msgarea.innerHTML += "<div class=\"msgc\" style=\"margin-bottom: 30px;\"> <div class=\"msg msgfrom\">" + message + "</div> <div class=\"msgarr msgarrfrom\"></div> <div class=\"msgsentby msgsentbyfrom\">Sent by " + username + "</div> </div>";
msginput.value = "";
}
}
xmlhttp.open("GET","update-messages.php?username=" + username + "&message=" + message,true);
xmlhttp.send();
}
}
setInterval(function(){ update() }, 2500);
</script>
</body>
</html>