-
Notifications
You must be signed in to change notification settings - Fork 0
/
chat.php
191 lines (176 loc) · 9.44 KB
/
chat.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
<?php
include ('inc/config.php');
if (!isset($_SESSION['user_id'])) $user_nick = $_SERVER['REMOTE_ADDR'];
if (isset($user_name)) $user_nick = $user_name;
if (isset($_SESSION['user_nick'])) $user_nick = $_SESSION['user_nick'];
if (!isset($_SESSION['last_message_id'])) $_SESSION['last_message_id'] = 0;
$view_ip = 0;
$moder = 0;
if (isset($user_p_config) AND $user_p_config == 1) $view_ip = 1;
if (isset($user_p_mod) AND $user_p_mod == 1) $moder = 1;
Header("Cache-Control: no-cache, must-revalidate");
Header("Pragma: no-cache");
Header("Content-Type: text/javascript; charset=utf-8");
if (isset($_GET['act'])) {
switch ($_GET['act']) {
case "send" : // если она равняется send, вызываем функцию Send()
Send($user_nick,$moder);
break;
case "load" : // если она равняется load, вызываем функцию Load()
Load($user_nick);
break;
case "members" : // если она равняется load, вызываем функцию Load()
Members($view_ip);
break;
default : // если ни тому и не другому - выходим
echo time();
}
}
function Send($user_nick,$moder)
{
$text = $_GET['text'];
$srch = array("\"", "<", ">", "`", "'");
$rpls = array(""", "<", ">", "`", "‘");
$text = str_replace($srch, $rpls, $text);
//жирний текст
$text = str_replace("<b>", "<b>", $text);
$text = str_replace("</b>", "</b>", $text);
if (strlen(utf8_decode($text)) > 0)
{
$do = 0;
if (preg_match("/^nick (.{3,})/", $text, $a))
{
$do = 1;
$_SESSION['user_nick'] = $a[1];
mysql_query("INSERT INTO `messages` (`ip`, `time`, `name`, `text`) VALUES ('".$_SERVER['REMOTE_ADDR']."', '".time()."', '".$user_nick."', '<font color=\"green\">змінив нік на <b>".$a[1]."</b></font>')");
}
if (preg_match("/^me (.*)/", $text, $a))
{
$do = 1;
mysql_query("INSERT INTO `messages` (`ip`, `time`, `name`, `text`) VALUES ('".$_SERVER['REMOTE_ADDR']."', '".time()."', '".$user_nick."', '<b>".$a[1]."</b>')");
}
if (preg_match("/^red (.*)/", $text, $a) and $moder == 1)
{
$do = 1;
mysql_query("INSERT INTO `messages` (`ip`, `time`, `name`, `text`) VALUES ('".$_SERVER['REMOTE_ADDR']."', '".time()."', '".$user_nick."', '<font color=\"red\"><b>".$a[1]."</b></font>')");
}
if (preg_match("/^admin (.*)/", $text, $a) and $moder == 1)
{
$do = 1;
if (preg_match("/^[a-z0-9\. ]*$/", $a[1], $b))
{
if ($b[0] == "clean")
{
mysql_query("DELETE FROM `messages`; ");
mysql_query("INSERT INTO `messages` (`ip`, `time`, `name`, `text`) VALUES ('".$_SERVER['REMOTE_ADDR']."', '".time()."', '', 'clean')");
mysql_query("INSERT INTO `messages` (`ip`, `time`, `name`, `text`) VALUES ('".$_SERVER['REMOTE_ADDR']."', '".time()."', '', '<b>Чат було очищено модератором</b>')");
}
if (preg_match("/^ban ([0-9\.]*)$/", $b[0], $c))
{
mysql_query("INSERT INTO `banned` (`id`, `time`, `ip`, `where`) VALUES (NULL, '".time()."', '".$c[1]."' , '".$_SERVER['REQUEST_URI']."');");
mysql_query("INSERT INTO `messages` (`ip`, `time`, `name`, `text`) VALUES ('".$_SERVER['REMOTE_ADDR']."', '".time()."', '', '<font color=\"red\"><b>Модератор забанив ".$c[1]."</b></font>')");
}
}
else
{
mysql_query("INSERT INTO `messages` (`ip`, `time`, `name`, `text`) VALUES ('".$_SERVER['REMOTE_ADDR']."', '".time()."', '', '<font color=\"red\"><b>".$a[1]."</b></font>')");
}
}
if (preg_match("/^img:(.*)/", $text, $a))
{
$do = 1;
mysql_query("INSERT INTO `messages` (`ip`, `time`, `name`, `text`) VALUES ('".$_SERVER['REMOTE_ADDR']."', '".time()."', '".$user_nick."', '<img src=\"".$a[1]."\" />')");
}
if (preg_match_all("~(http://[^ ]+|https://[^ ]+|ftp://[^ ]+)~", $text, $urls))
{
for ($i=0; $i< count($urls[0]); $i++)
{
$text = str_replace($urls[0][$i], '<a href="'.$urls[0][$i].'" target="_blank"><b>'.$urls[0][$i].'</b></a>' , $text);
}
}
if ($do == 0)
{
mysql_query("INSERT INTO `messages` (`ip`, `time`, `name`, `text`) VALUES ('".$_SERVER['REMOTE_ADDR']."', '".time()."', '".$user_nick."', '".$text."')");
}
echo "OK";
$old_messages = time() - 18000;
mysql_query("DELETE FROM `messages` WHERE `time` < '".$old_messages."' ; ");
}
else
{
echo ".";
}
}
function Load($user_nick)
{
mysql_query("INSERT INTO `messages` (`ip`, `time`, `name`, `text`) VALUES ('".$_SERVER['REMOTE_ADDR']."', '".time()."', '".$user_nick."', 'renew_user')");
$query = mysql_query("SELECT * FROM `messages` WHERE `id` > ".$_SESSION['last_message_id']." ORDER BY `id` ; ");
if (mysql_num_rows($query) > 0)
{
$text = '';
while ($row = mysql_fetch_array($query))
{
$last_row = $row['id'];
for ($i = 1; $i <= 1000; $i++)
{
if (preg_match("/:(smile-[0-9]{3}):/", $row['text'], $a))
{
$row['text'] = str_replace(":".$a[1].":", "<img src=\"templates/images/smiles/".$a[1].".png\"</img>", $row['text']);
}
else
{
$i = 1000;
}
}
if ($row['text'] == "clean")
{
$text = "clean";
break;
}
else
{
if ($row['text'] != "new_user" AND $row['text'] != "renew_user")
{
$text .= "<p id=\"".$row['id']."\">[".date('H:i:s' ,$row['time'])."] <b>".$row['name']."</b> » ".$row['text']."</p>";
}
}
}
$_SESSION['last_message_id'] = $last_row;
echo $text;
}
else
{
echo "OK";
}
}
function Members($view_ip)
{
$query = mysql_query("SELECT `ip`,`name` FROM `messages` WHERE `time` >= '".(time() - 15)."' ORDER BY `name` ; ");
if (mysql_num_rows($query) > 0)
{
$text = "";
$names = array();
while ($row = mysql_fetch_array($query))
{
if (!in_array($row['name'], $names) AND $row['name'] != "")
{
$names[] = $row['name'];
if ($view_ip == 1)
{
$text .= "<a onclick=\"document.getElementById('message').value += '<b>".$row['name']."</b> '; document.getElementById('message').focus();\" class=\"btn btn-default btn-block\" role=\"button\">".$row['name']." <span class=\"label label-success\">".$row['ip']."</span></a><br>";
}
else
{
$text .= "<a onclick=\"document.getElementById('message').value += '<b>".$row['name']."</b> '; document.getElementById('message').focus();\" class=\"btn btn-default btn-block\" role=\"button\">".$row['name']."</a><br>";
}
}
}
$text .= "<a onclick=\"DoLoadMembers()\" class=\"btn btn-default btn-block\" role=\"button\">Всього: ".count($names)." (".date('H:i:s').")</a>";
echo $text;
}
else
{
echo "<a onclick=\"DoLoadMembers()\" class=\"btn btn-default btn-block\" role=\"button\">Чат пустий ".count($names)." (".date('H:i:s').")</a><br>";
}
}
?>