-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathindex.php
More file actions
160 lines (147 loc) · 5.64 KB
/
index.php
File metadata and controls
160 lines (147 loc) · 5.64 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
<?php
function loginForm() {
echo'
<div id="loginform">
<form action="." method="post">
<p>Please enter your name to continue:</p>
<label for="name">Name:</label>
<input type="text" name="name" id="name" autofocus />
<input type="submit" name="enter" id="enter" value="Enter" />
</form>
</div>
';
}
function getSetup($key = null) {
$arr = parse_ini_file('setup.ini');
return isset($key) ? $arr[$key] : $arr;
}
function deleteOldHistory() {
$expireHistory = getSetup('expire_history');
$expireDate = date('Y-m-d', strtotime("-$expireHistory day"));
foreach (glob('./history/*') as $f) {
if (basename($f) < $expireDate) {
unlink($f);
}
}
}
//-------------------------
session_start();
if (isset($_GET['logout'])) {
session_destroy();
header("Location: ./"); //Redirect the user
}
if (isset($_POST['enter'])) {
if ($_POST['name'] != "") {
$_SESSION['name'] = stripslashes(htmlspecialchars($_POST['name']));
} else {
echo '<span class="error">Please type in a name</span>';
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Chat - Customer Module</title>
<meta charset="UTF-8" />
<link type="text/css" rel="stylesheet" href="style.css" />
</head>
<?php
if (!isset($_SESSION['name'])) {
loginForm();
deleteOldHistory();
} else {
?>
<div id="wrapper">
<div id="menu">
<p class="welcome">Welcome, <b><?php echo $_SESSION['name']; ?></b></p>
<p class="logout"><a id="exit" href="#">Exit Chat</a></p>
<p class="logout" style="margin-right: 1em;"><a target="_blank" href="history.php">History</a></p>
<div style="clear:both"></div>
</div>
<div id="chatbox"><?php
?></div>
<form name="message" action=".">
<input name="usermsg" type="text" id="usermsg" size="63" autocomplete="off" autofocus />
<input name="submitmsg" type="submit" id="submitmsg" value="Send" />
</form>
</div>
<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript">
// jQuery Document
$(document).ready(function() {
var id = 'undefined';
var oldId = null;
var isRunLoadLog = false;
//If user submits the form
$("#submitmsg").click(function() {
var clientmsg = $("#usermsg").val();
$("#usermsg").val('');
$("#usermsg").focus();
$.ajax({
type: 'POST',
url: 'post.php',
data: {text: clientmsg},
//cache: false,
async: true,
success: function(data) {
if (!isRunLoadLog) {
loadLog();
}
},
error: function(request, status, error) {
$("#usermsg").val(clientmsg);
},
});
return false;
});
//Load the file containing the chat log
function loadLog() {
isRunLoadLog = true;
var oldscrollHeight = $("#chatbox")[0].scrollHeight;
$.ajax({
type: 'POST',
url: 'server.php',
data: {id: id},
dataType: 'json',
//cache: false,
async: true,
success: function(data) {
id = data.id;
if (oldId !== id) {
oldId = id;
var html = '';
var date;
for (var k in data.data.reverse()) {
date = new Date(parseInt(data.data[k][0])*1000);
date = date.toLocaleTimeString();
date = date.replace(/([\d]+\D+[\d]{2})\D+[\d]{2}(.*)/, '$1$2');
html = html
+"<div class='msgln'>("+date+") <b>"
+data.data[k][1]+"</b>: "+data.data[k][2]+"<br></div>";
}
$("#chatbox").append(html); //Insert chat messages into the #chatbox div
var newscrollHeight = $("#chatbox")[0].scrollHeight;
if (newscrollHeight > oldscrollHeight) {
$("#chatbox").scrollTop($("#chatbox")[0].scrollHeight);
}
}
isRunLoadLog = false;
},
});
}
loadLog();
setInterval(loadLog, <?php echo getSetup('interval') ?>); //Reload file every 2.5 seconds
//If user wants to end session
$("#exit").click(function() {
var exit = confirm("Are you sure you want to end the session?");
if (exit == true) {
window.location = 'index.php?logout=true';
}
});
});
</script>
<?php
}
?>
</body>
</html>