-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.php
284 lines (257 loc) · 9.77 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
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
<?php
session_start();
$conn = mysqli_connect("localhost", "root", "", "livechat");
function loginForm()
{
echo '
<div class="form-group">
<div id="loginform">
<form action="index.php" method="post">
<h1>Welcome To Live Chat</h1><hr/>
<label for="name">Please Enter Your Name To Continue</label>
<input type="text" name="name" id="name" class="form-control" placeholder="Enter Your Name" required/>
<br>
<label for="name">Please Enter Your Email To Continue</label>
<input type="email" name="email" id="email" class="form-control" placeholder="Enter Your Email" required/>
<input type="submit" class="btn btn-default" name="enter" id="enter" value="Enter" />
</form>
</div>
</div>
';
}
$tkid = 0;
if (isset($_POST['enter'])) {
if ($_POST['name'] != "") {
if (!isset($_SESSION['name'])) {
// $ip = isset($_SERVER['HTTP_CLIENT_IP'])?$_SERVER['HTTP_CLIENT_IP']:isset($_SERVER['HTTP_X_FORWARDED_FOR'])?$_SERVER['HTTP_X_FORWARDED_FOR']:$_SERVER['REMOTE_ADDR'];
if (isset($_SERVER['HTTP_CLIENT_IP'])) {
$ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
$ip = $_SERVER['REMOTE_ADDR'];
}
$_SESSION['name'] = stripslashes(htmlspecialchars($_POST['name']));
$query = "insert into students(name,ip,email,status) values(?,?,?,'Available')";
$stmt = $conn->prepare($query);
$stmt->bind_param("sss", $_POST['name'], $ip, $_POST['email']);
$stmt->execute();
$stmt->close();
$query = "select id from students where email=?";
$stmt = $conn->prepare($query);
$stmt->bind_param("s", $_POST['email']);
$stmt->execute();
$stmt->bind_result($userid);
$stmt->fetch();
$_SESSION['uid'] = $userid;
$stmt->close();
}
$conn = mysqli_connect("localhost", "root", "", "livechat");
$status = "Available";
$query = "update students set status = ? where id = ?";
$stmt = $conn->prepare($query);
$stmt->bind_param("si", $status, $_SESSION['uid']);
$stmt->execute();
$stmt->close();
if (!isset($_SESSION['token'])) {
$query = "select token from tokens where uid = ?";
$stmt = $conn->prepare($query);
$stmt->bind_param("i", $_SESSION['uid']);
$stmt->execute();
$stmt->bind_result($token);
if ($stmt->fetch()) {
$_SESSION['token'] = $token;
$stmt->close();
} else {
date_default_timezone_set("Asia/Karachi");
$time = date("Y-m-d h:i:sa");
$token = md5($_SESSION['name'] . $_SERVER['REMOTE_ADDR'] . $time);
$query = "insert into tokens(uid,valid,token) values(?,'1',?)";
$stmt = $conn->prepare($query);
$stmt->bind_param("is", $_SESSION['uid'], $token);
$stmt->execute();
$stmt->close();
$_SESSION['token'] = $token;
}
}
// $cb = fopen ( "log.html", 'a' );
// fwrite ( $cb, "<div class='msgln'><i>User " . $_SESSION ['name'] . " has joined the chat session.</i><br></div>" );
// fclose ( $cb );
} else {
echo '<span class="error">Please Enter a Name</span>';
}
}
if (isset($_GET['logout'])) {
// $cb = fopen ( "log.html", 'a' );
// fwrite ( $cb, "<div class='msgln'><i>User " . $_SESSION ['name'] . " has left the chat session.</i><br></div>" );
// fclose ( $cb );
$conn = mysqli_connect("localhost", "root", "", "livechat");
$query = "update students set Status='Away' where id=?";
$stmt = $conn->prepare($query);
$stmt->bind_param("i", $_SESSION['uid']);
$stmt->execute();
$stmt->close();
session_destroy();
header("Location: index.php");
}
?>
<!DOCTYPE html>
<html>
<head>
<title>SCOCS Live Chat</title>
<link rel="stylesheet" href="css/style.css">
<link rel="stylesheet" href="bootstrap/css/bootstrap.min.css">
<script src="js/jquery.min.js"></script>
</head>
<body>
<?php
if (!isset($_SESSION['name'])) {
loginForm();
} else {
?>
<div id="wrapper">
<div id="menu">
<h1>Live Chat!</h1>
<hr />
<p class="welcome"><b>HI - <a><?php echo $_SESSION['name']; ?></a></b></p>
<p class="logout"><a id="exit" href="#" class="btn btn-default">Exit Live Chat</a></p>
<div style="clear: both"></div>
</div>
<div id="chatbox">
<!--<button onclick="scrollWin(0, 50)">Scroll down</button> -->
<?php
loadmessages();
// if (file_exists ( "log.html" ) && filesize ( "log.html" ) > 0) {
// $handle = fopen ( "log.html", "r" );
// $contents = fread ( $handle, filesize ( "log.html" ) );
// fclose ( $handle );
// echo $contents;
// }
?>
</div>
<form name="message">
<input name="usermsg" class="form-control" type="text" id="usermsg" placeholder="Create A Message" />
<input name="submitmsg" class="btn btn-default" type="submit" id="submitmsg" value="Send" />
</form>
</div>
<script>
$(document).ready(function() {});
$(document).ready(function() {
$("#exit").click(function() {
var exit = confirm("Are You Sure You Want To Leave This Page?");
if (exit == true) {
window.location = 'index.php?logout=true';
}
});
});
$("#submitmsg").click(function() {
var clientmsg = $("#usermsg").val();
if (!clientmsg == "") {
$.post("post.php", {
text: clientmsg,
side: 'std',
tkid: <?php echo gettokenid(); ?>,
senderid: <?php echo $_SESSION['uid']; ?>
});
loadmessages;
}
return false;
});
var height = 0;
$('div .msgln').each(function(i, value) {
height += parseInt($(this).height());
});
height += '';
$('div').animate({
scrollTop: height
});
</script>
<?php
}
// function loadmessages()
// {
// $conn= mysqli_connect("localhost","root","","livechat");
// $query="select id from tokens where uid=?";
// $stmt=$conn->prepare($query);
// $stmt->bind_param("i",$_SESSION['uid']);
// $stmt->execute();
// $stmt->bind_result($tkid);
// $stmt->fetch();
// $stmt->close();
// $query="select createdAt, message from chat where token=?";
// $stmt=$conn->prepare($query);
// $stmt->bind_param("i",$tkid);
// $stmt->execute();
// $stmt->bind_result($createdat,$message);
// while($stmt->fetch())
// {
// echo '<div class="msgln">'.$createdat.' <b>'.$_SESSION['name'].'</b>: '.stripslashes(htmlspecialchars($message)).'<br></div>';
// }
// $stmt->close();
// }
function gettokenid()
{
$conn = mysqli_connect("localhost", "root", "", "livechat");
$query = "select id from tokens where uid=?";
$stmt = $conn->prepare($query);
$stmt->bind_param("i", $_SESSION['uid']);
$stmt->execute();
$stmt->bind_result($tkid);
$stmt->fetch();
$stmt->close();
return $tkid;
}
function loadmessages()
{
$conn = mysqli_connect("localhost", "root", "", "livechat");
$tkid = gettokenid();
$query = "select senderid from chat where token=?";
$stmt = $conn->prepare($query);
$stmt->bind_param("i", $tkid);
$stmt->execute();
$stmt->bind_result($senderid);
while ($stmt->fetch()) {
if (!isset($supid) || !isset($stdid)) {
if (substr($senderid, 0, 3) == "sup") {
$supid = substr($senderid, 3);
} else if (substr($senderid, 0, 3) == "std") {
$stdid = substr($senderid, 3);
}
} else {
break;
}
}
$stmt->close();
$query = "select name from support where id=?";
$stmt = $conn->prepare($query);
$stmt->bind_param("i", $supid);
$stmt->execute();
$stmt->bind_result($supname);
$stmt->fetch();
$stmt->close();
$query = "select name from students where id=?";
$stmt = $conn->prepare($query);
$stmt->bind_param("i", $stdid);
$stmt->execute();
$stmt->bind_result($stdname);
$stmt->fetch();
$stmt->close();
$query = "select senderid, createdAt, message from chat where token=?";
$stmt = $conn->prepare($query);
$stmt->bind_param("i", $tkid);
$stmt->execute();
$stmt->bind_result($senderid, $createdat, $message);
while ($stmt->fetch()) {
if (substr($senderid, 0, 3) == "sup") {
$name = $supname;
}
if (substr($senderid, 0, 3) == "std") {
$name = $stdname;
}
echo '<div class="msgln">' . $createdat . ' <b>' . $name . '</b>: ' . stripslashes(htmlspecialchars($message)) . '<br></div>';
}
$stmt->close();
}
?>
</body>
</html>