forked from aorti017/CS180-Project
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessages.php
More file actions
327 lines (303 loc) · 13 KB
/
Copy pathmessages.php
File metadata and controls
327 lines (303 loc) · 13 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
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
<?php
// starts login session for user
// need a session start in every file
session_start();
// this cookie checks for if a user has two tabs open and logs out in one tab
// refreshing the page on the non-logged out page will redirect to index.php
// since the user logged out
if (!isset($_COOKIE['username'])) {
// redirects to index.php
header('Location: index.php');
}
// sets the username cookie so that the user is logged in across all open tabs
// 0 is the timeout time so that if the browser closes, the user is logged out
setcookie('username', $_SESSION['username'], 0);
// echo $_SESSION['username'];
?>
<html>
<head>
<meta charset="UTF-8">
<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<script src="javascript.js"></script>
<link rel="stylesheet" href="navigation.css">
<link rel="stylesheet" href="messageDesign.css">
<title>Messages</title>
</head>
<body>
<script>
window.fbAsyncInit = function() {
FB.init({
appId : '411711342356748',
xfbml : true,
version : 'v2.5'
});
};
(function(d, s, id){
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) {return;}
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/sdk.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
</script>
<div>
<nav class="main-menu">
<ul>
<li><a onclick = "FB.logout()" href="logout.php">Logout</a></li>
<li><a href="contacts.php">Contacts</a></li>
<li id="currentpage"><a href="">Messages</a></li>
<li><a id="userProf">Profile</a></li>
</ul>
<br>
</nav>
</div>
<form action="./messages.php" type="GET" style="display: inline-block;">
<p id="createText">Create new message:</p>
<input type="text" name="contacts">
</form>
<div id="friends">
</div>
<div style="margin: -84px 25% 0px 25%;">
<div id="currentFriendWrapper">
<a id="currentFriend"></a>
</div>
<div class="messageWrapper">
<div id="messageContainer"></div>
<div class="sendMessageDiv">
<textarea id="newMessage" rows="5" style="width: 100%;" onkeydown="if (event.keyCode==13) {document.getElementById('send').click(); return false;}"></textarea>
<button class="sendMessage" id="send">Send</button>
</div>
</div>
</div>
<script type="text/javascript">
function getMessageList(){
var username = parse();
$.ajax({
type: 'GET',
url: './messageList.php',
data: {username: username},
success: function(data){
var obj = jQuery.parseJSON(data);
var senders = obj.sender;
var receivers = obj.receiver;
var timestamps = obj.timestamp;
// if the logged in person has no contacts, then senders will be size 1 with a value of null
if (!(senders.length == 1 && senders[0] == null)) {
for(var i = 0; i < senders.length; i++)
{
if (i == 0) {
if (senders[i] == username) {
if (window.location.href.indexOf("=") == -1) {
window.location.replace("./messages.php?contact=" + receivers[i]);
}
}
else {
if (window.location.href.indexOf("=") == -1) {
window.location.replace("./messages.php?contact=" + senders[i]);
}
}
}
if (senders[i] == username) {
var btn = document.createElement("BUTTON");
btn.setAttribute("id", receivers[i]);
btn.setAttribute("value", receivers[i]);
btn.onclick=function(){
window.location.replace("./messages.php?contact=" + this.value);
};
var t = document.createTextNode(receivers[i]);
btn.appendChild(t);
document.getElementById("friends").appendChild(btn);
}
else {
var btn = document.createElement("BUTTON");
btn.setAttribute("id", senders[i]);
btn.setAttribute("value", senders[i]);
btn.onclick=function(){
window.location.replace("./messages.php?contact=" + this.value);
document.getElementById("currentFriend").innerHTML = this.value;
};
var t = document.createTextNode(senders[i]);
btn.appendChild(t);
document.getElementById("friends").appendChild(btn);
}
}
}
}
});
}
//used to get the GET variables
function getUrlVars() {
var parts = window.location.href;
var vars = parts.substring(parts.indexOf("=")+1, parts.length);
return vars;
}
// type = sent or received
// message = content of the message
// time = time of message delivery
function Message(type, message, time){
this.type = type;
this.message = message;
this.time = time;
}
function getNewMessages(t, recpUser){
var username = parse();
if (window.location.href.indexOf("=") != -1) {
document.getElementById("currentFriend").innerHTML = recpUser;
document.getElementById("currentFriend").href = "./profile.php?userVar=" + recpUser;
}
$.ajax({
type: 'GET',
url: './getMessages.php',
data: {username:username, recpUser: recpUser, time:t},
success: function(data){
/*TODO*/
//instead of outputting to console,
//output to html elements in order they were sent
//and user colors or identifiers to indicate who
//sent and who received
var obj = jQuery.parseJSON(data);
var messages = [];
if(t == null){
var messSent = obj.messageSent;
var messSentTime = obj.messageSentTime;
for(i = 0; i < messSent.length; i++) {
messages.push(new Message('s', messSent[i], messSentTime[i]));
}
var messRec = obj.messageReceived;
var messRecTime = obj.messageReceivedTime;
for(i = 0; i < messRec.length; i++){
messages.push(new Message('r', messRec[i], messRecTime[i]));
}
messages.sort(function(a, b){
return a.time - b.time;
});
for(i = 0; i < messages.length; i++){
if(messages[i].type == 'r'){
//$('#messageContainer').append(obj.receiver + ": " + messages[i].message + "<br>");
var format = "<div class='bubbleFrom'><p>" + messages[i].message + "</p></div>";
$('#messageContainer').append(format);
}
else{
//$('#messageContainer').append(obj.sender + ": " + messages[i].message + "<br>");
var format = "<div class='bubbleTo'><p>" + messages[i].message + "</p></div>";
$('#messageContainer').append(format);
}
}
var bottomOfMessages = document.getElementById("messageContainer");
bottomOfMessages.scrollTop = bottomOfMessages.scrollHeight;
}
else{
var messageSent = obj.messageSent;
var messageReceived = obj.messageReceived;
if(messageSent == "" && messageReceived != ""){
//$('#messageContainer').append(obj.receiver + ": " + messageReceived[0] + "<br>");
var format = "<div class='bubbleFrom'><p>" + messageReceived[0] + "</p></div>";
$('#messageContainer').append(format);
var bottomOfMessages = document.getElementById("messageContainer");
bottomOfMessages.scrollTop = bottomOfMessages.scrollHeight;
}
else if(messageReceived == "" && messageSent != ""){
//$('#messageContainer').append(obj.sender + ": " + messageSent[0] + "<br>");
var format = "<div class='bubbleTo'><p>" + messageSent[0] + "</p></div>";
$('#messageContainer').append(format);
var bottomOfMessages = document.getElementById("messageContainer");
bottomOfMessages.scrollTop = bottomOfMessages.scrollHeight;
}
}
getNewMessages(obj.timestamp, recpUser);
}
});
}
$('#send').click(function()
{
var message = $('#newMessage').val();
if(message == ""){
return;
}
//clears the textarea after a message is sent
document.getElementById("newMessage").value="";
var username = parse();
var recpUser = getUrlVars();
var t = (new Date).getTime();
$.ajax(
{
type: 'GET',
url: './sendMessage.php',
data: {username:username, recpUser:recpUser, time:t , message:message},
success: function(data){
var obj = jQuery.parseJSON(data);
if(obj.status != ""){
var format = "<div class='bubbleTo'><p style='color:red'>" + obj.status + "</p></div>";
$('#messageContainer').append(format);
var bottomOfMessages = document.getElementById("messageContainer");
bottomOfMessages.scrollTop = bottomOfMessages.scrollHeight;
}
}
});
});
document.addEventListener('DOMContentLoaded', function(){
if(Notification.permission != "granted"){
Notification.requestPermission();
}
});
function initNotifications(x, tracked, runCount){
var username = parse();
$.ajax({
type: 'GET',
url: './notifications.php',
data: {username: username, time: x},
success: function(data){
var obj = jQuery.parseJSON(data);
var messages = obj.message;
var senders = obj.sender;
var times = obj.times;
if(messages.length > 0 && senders.length > 0){
console.log(messages);
}
for(i = 0; i < messages.length; i++){
//make sure the array doesnt grow too large
if(runCount >= 500){
tracked = []
}
if(tracked.indexOf(times[i]) > -1 || senders[i] == getUrlVars()){
continue;
}
else{
runCount += 1;
tracked.push(times[i]);
}
temp = document.cookie;
document.cookie = "sender="+senders[i];
document.cookie = "message="+messages[i];
if (Notification.permission != "granted")
Notification.requestPermission();
else {
var notification = new Notification(parseSender(), {body: parseMessage()});
notification.onclick = function () {
window.location.replace("./messages.php?contacts="+parseSender());};
setTimeout(function(){
notification.close();
}, 5000);
}
document.cookie = temp;
}
initNotifications(obj.time, tracked, runCount);
}
});
}
$(function(){
document.getElementById("userProf").setAttribute("href", "profile.php?userVar="+parse());
var time = null;
var recpUser = getUrlVars();
getMessageList();
getNewMessages(time, recpUser);
var d = new Date();
var tracked = [];
initNotifications(d.getTime(), tracked, 0);
});
</script>
</body>
</html>