forked from TheCheat/CTRev
/
chat.php
227 lines (210 loc) · 7.54 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
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
<?php
/**
* Project: CTRev
* @file modules/chat.php
*
* @page http://ctrev.cyber-tm.ru/
* @copyright (c) 2008-2012, Cyber-Team
* @author The Cheat <cybertmdev@gmail.com>
* @name Чат
* @version 1.00
*/
if (!defined('INSITE'))
die("Remote access denied!");
class chat {
/**
* Инициализация Ajax-части чата
* @return null
*/
public function init() {
lang::o()->get("blocks/chat");
$id = (int) $_GET['id'];
switch ($_GET["act"]) {
case "text":
$this->get_text($id);
die();
break;
case "delete":
$this->delete($id);
ok();
break;
case "truncate":
$this->truncate();
die(lang::o()->v('chat_no_messages'));
break;
case "save":
$this->save($_POST['text'], $id);
ok();
break;
default:
$this->show((int) $_GET['time'], (bool) $_GET['prev']);
break;
}
}
/**
* Удаление сообщения из чата
* @param int $id ID сообщения
* @return null
* @throws EngineException
*/
public function delete($id) {
$id = (int) $id;
check_formkey();
users::o()->check_perms('del_chat');
db::o()->p($id, users::o()->v('id'))->delete('chat', 'WHERE id=?' .
(!users::o()->perm('del_chat', 2) ? " AND poster_id=?" : "") . ' LIMIT 1');
$this->delete_logs($id);
}
/**
* Запись в логи чата
* @param int $id ID сообщения
* @return null
*/
protected function delete_logs($id = 0) {
$id = (int) $id;
if (!db::o()->affected_rows() || !config::o()->v('chat_clearlogs'))
return;
$t = time() - intval(config::o()->v('chat_clearlogs'));
db::o()->p($t)->delete('chat_deleted', 'WHERE time<=?');
db::o()->insert(array('id' => $id, 'time' => time()), 'chat_deleted');
if (!$id && db::o()->affected_rows() < 1)
db::o()->update(array('time' => time()), 'chat_deleted', 'WHERE id=0 LIMIT 1');
}
/**
* Очистка сообщений чата
* @return null
* @throws EngineException
*/
public function truncate() {
check_formkey();
users::o()->check_perms('del_chat', 3);
db::o()->truncate_table('chat');
$this->delete_logs();
}
/**
* Сохранение сообщения чата
* @param string $text
* @param int $id ID сообщения
* @return null
* @throws EngineException
*/
public function save($text, $id = null) {
check_formkey();
$text = trim($text);
if (!$text)
return;
$id = (int) $id;
if (!$id)
users::o()->check_perms('chat', 2, 2);
else
users::o()->check_perms('edit_chat');
$update = array();
$update["text"] = $text;
$update["edited_time"] = time();
try {
plugins::o()->pass_data(array('update' => &$update,
'id' => $id), true)->run_hook('chat_save');
} catch (PReturn $e) {
return $e->r();
}
if (!$id) {
$update["poster_id"] = users::o()->v('id') ? users::o()->v('id') : -1;
$update["posted_time"] = time();
db::o()->delete("chat_deleted", "WHERE id=0");
db::o()->insert($update, 'chat');
}
else
db::o()->p($id, users::o()->v('id'))->update($update, 'chat', 'WHERE id=?' .
(!users::o()->perm('edit_chat', 2) ? " AND poster_id=?" : "") . ' LIMIT 1');
}
/**
* Вывод неформатированного текста сообщения для редактирования
* @param int $id ID сообщения
* @return null
*/
protected function get_text($id) {
users::o()->check_perms('edit_chat');
$id = (int) $id;
$q = db::o()->p($id)->query("SELECT text FROM chat WHERE id=? LIMIT 1");
list ($text) = db::o()->fetch_row($q);
print($text);
}
/**
* Форматирование сообщения
* @param array $row массив данных сообщения
* @return null
*/
public function chat_mf(&$row) {
$t = $row["text"];
$cmds = 'private|me|hello|bye';
preg_match('/^\/(' . $cmds . ')\s*(?:\((.+)(?:,\s*([0-9]+))?\)\s+)?(.*)$/siu', $t, $matches);
if (!$matches)
return;
list(, $cmd, $p1, $p2, $t) = $matches;
$u = null;
try {
plugins::o()->pass_data(array('row' => &$row,
'matches' => $matches), true)->run_hook('chat_formatting');
} catch (PReturn $e) {
return $e->r();
}
switch ($cmd) {
case "private":
if (!users::o()->check_login($p1))
return;
if (!$row['poster_id'] || !$p1 || mb_strtolower($row['username']) == mb_strtolower($p1))
return;
if (!users::o()->perm('chat_sprivate') &&
users::o()->v('username_lower') != mb_strtolower($p1) &&
users::o()->v('id') != $row['poster_id']) {
$row["text"] = '';
return;
}
if (users::o()->v('id') == $row['poster_id']) {
$cmd = 'fprivate';
$u = $p1;
}
case "me":
if (!$u)
$u = smarty_group_color_link($row["username"], $row["group"]);
$row["text"] = sprintf(lang::o()->v("chat_" . $cmd . "_saying"), $u, $t);
break;
case "hello":
case "bye":
$row["text"] = sprintf(lang::o()->v("chat_" . $cmd . "_saying"), smarty_group_color_link($row["username"], $row["group"]));
break;
}
$row["spec"] = true;
}
/**
* Вывод сообщений чата
* @param int $time время последней проверки или ID сообщения
* @param bool $prev показать пред. сообщения, до этого ID
* @return null
*/
public function show($time, $prev = false) {
$time = (int) $time;
users::o()->check_perms('chat', 2, 2);
if ($time && !$prev) {
$r = db::o()->p($time)->query('SELECT id FROM chat_deleted WHERE time>=?');
$del = "";
while (list($i) = db::o()->fetch_row($r))
$del .= ( $del ? "," : "") . $i;
tpl::o()->assign('deleted', $del);
}
$orderby = " ORDER BY c.posted_time DESC ";
$limit = $orderby . (config::o()->v('chat_maxmess') ? " LIMIT " . config::o()->v('chat_maxmess') : "");
if ($prev) {
$where = ' WHERE c.id < ?' . $limit;
tpl::o()->assign('prev', true);
}
else
$where = $time ? ' WHERE c.edited_time>=?' . $orderby : $limit;
$r = db::o()->p($time)->query('SELECT c.*, u.username, u.group FROM chat AS c
LEFT JOIN users AS u ON u.id=c.poster_id ' . $where);
tpl::o()->assign('rows', array_reverse(db::o()->fetch2array($r)));
tpl::o()->register_modifier('chat_mf', array($this, 'chat_mf'));
tpl::o()->display('chat/chat.tpl');
}
}
?>