-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTwChat.php
124 lines (113 loc) · 3.53 KB
/
TwChat.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
<?php
require_once(__DIR__ . "/global.php");
session_start();
function loginForm() {
echo '
<div id="loginform">
<form action="TwChat.php" method="post">
<p>Please enter your name to continue:</p>
<label for="name">Name:</label>
<input type="text" name="name" id="name" />
<input type="submit" name="enter" id="enter" value="Enter" />
</form>
</div>
';
}
function check_logfile_permissions($fp) {
if (!$fp) {
echo '<br/><span>Error: failed to open log. Try this permissions fix:</span><br/>';
echo '<code>cd ' . __DIR__ . ' && touch log.html && chown www-data:www-data log.html</code><br/>';
fok();
}
return $fp;
}
if (isset($_POST['enter'])) {
if ($_POST['name'] != "") {
$_SESSION ['name'] = stripslashes(htmlspecialchars($_POST ['name']));
$fp = fopen('log.html', 'a');
check_logfile_permissions($fp);
fwrite($fp, "<div class='msgln'><i>User " . htmlspecialchars($_SESSION ['name']) . " has joined the chat session.</i><br></div>" );
fclose($fp);
} else {
echo '<span class="error">Please type in a name</span>';
}
}
if (isset($_GET['logout'])) {
// Simple exit message
$fp = fopen("log.html", 'a');
check_logfile_permissions($fp);
fwrite ($fp, "<div class='msgln'><i>User " . htmlspecialchars($_SESSION ['name']) . " has left the chat session.</i><br></div>" );
fclose ($fp);
session_destroy ();
header("Location: TwChat.php"); // Redirect the user
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link type="text/css" rel="stylesheet" href="style.css" />
<title>Teeworlds chat</title>
</head>
<body>
<?php
if (!isset($_SESSION['name'])) {
loginForm ();
} else {
?>
<div id="wrapper">
<div id="menu">
<p class="welcome">
Welcome, <b><?php echo htmlspecialchars($_SESSION['name']); ?></b>
</p>
<p class="logout">
<button id="exit" onclick="window.location = 'TwChat.php?logout=true'">Exit Chat</button>
</p>
<div style="clear: both"></div>
</div>
<div id="chatbox"><?php
if (file_exists('log.html') && filesize('log.html') > 0) {
$handle = fopen('log.html', 'r' );
check_logfile_permissions($handle);
$contents = fread($handle, filesize('log.html'));
fclose($handle);
echo $contents;
}
?></div>
<form name="chat-form" id="chat-form" action="">
<input name="message" type="text" id="message" size="63" />
<input name="send" type="submit" id="send" value="Send" />
</form>
</div>
<script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
<script type="text/javascript">
// jQuery Document
$(document).ready(function(){
});
function loadLog(){
var oldscrollHeight = $("#chatbox").attr("scrollHeight") - 20; //Scroll height before the request
$.ajax({
url: "log.html",
cache: false,
success: function(html){
$("#chatbox").html(html); //Insert chat log into the #chatbox div
//Auto-scroll
var newscrollHeight = $("#chatbox").attr("scrollHeight") - 20; //Scroll height after the request
if(newscrollHeight > oldscrollHeight){
$("#chatbox").animate({ scrollTop: newscrollHeight }, 'normal'); //Autoscroll to bottom of div
}
},
});
}
setInterval(loadLog, 1000);
</script>
<?php
}
?>
<script src="js/tw_chat.js"></script>
<script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
<script type="text/javascript">
</script>
</body>
</html>