forked from lonalore/fb_chat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfb_chat.php
345 lines (292 loc) · 10.4 KB
/
fb_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
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
<?php
require_once("../../class2.php");
if (!plugInstalled('fb_chat')) {
exit;
}
require_once("classes/fb_chat_main.class.php");
// [PLUGINS]/fb_chat/languages/[LANGUAGE]/[LANGUAGE]_front.php
e107::lan('fb_chat', false, true);
//start session if required
if (!session_id()) {
session_start();
}
/**
* fb_chat class
*
* Handling Ajax Requests arrive from Frontend
*/
class fb_chat extends fb_chat_main {
protected $plugPrefs = array();
public function __construct() {
$this->plugPrefs = e107::getPlugConfig('fb_chat')->getPref();
if (!$this->check_permission()) {
exit;
}
if (!isset($_SESSION['chatHistory'])) {
$_SESSION['chatHistory'] = array();
}
if (!isset($_SESSION['openChatBoxes'])) {
$_SESSION['openChatBoxes'] = array();
}
/**
* 1 - startchatsession
* 2 - chatheartbeat
* 3 - closechat
* 4 - sendchat
* 5 - get username
* 6 - get (normal) menu content
* 7 - get (floating) menu content
* 8 - Turn chat on
* 9 - Turn chat off
*/
$action = (int) $_GET['a'];
switch ($action) {
case 1:
$this->chat_start_session();
break;
case 2:
$this->chat_heartbeat();
break;
case 3:
$this->chat_close();
break;
case 4:
$this->chat_send((int) $_POST['to'], $_POST['message']);
break;
case 5:
$this->get_user_name((int) $_POST['tid']);
break;
case 6:
$this->get_online_list(0);
break;
case 7:
$this->get_online_list(1);
break;
case 8:
$this->turn_chat_on();
break;
case 9:
$this->turn_chat_off();
break;
default:
exit;
}
}
/**
* Start Chat Session
* - Get opened chat windows from session and push them as json output
* with the current user details.
*
* @param string $items
*/
public function chat_start_session($items = "") {
if (!empty($_SESSION['openChatBoxes'])) {
foreach ($_SESSION['openChatBoxes'] as $chatbox => $void) {
$items .= $this->chat_box_session($chatbox);
}
}
if (!empty($items)) {
$items = substr($items, 0, -1);
}
$row = get_user_data(USERID);
$name = $this->get_user_name_by_names($row['user_name'], $row['user_login']);
header('Content-type: application/json');
echo '{ "user": { "id": "' . USERID . '", "name": "' . $name . '" }, "items": [' . $items . '] }';
exit;
}
/**
* Chat Heartbeat
* - Get unread messages from DB and append them to the output string
* and to session history
* - Create last message item (after a specified inactivity) and append
* it to the output string and to session history
* - Update messages in DB
* - Push output string as json
*
* @param string $items
*/
public function chat_heartbeat($items = "") {
$rows = $this->get_new_messages(USERID);
foreach ($rows as $row) {
$fid = $row['fb_chat_from'];
$fnm = $this->get_user_name_by_names($row['user_name'], $row['user_login']);
$msg = $this->handle_output($row['fb_chat_msg']);
$snt = $row['fb_chat_sent'];
if (!isset($_SESSION['openChatBoxes'][$fid]) && isset($_SESSION['chatHistory'][$fid])) {
$items = $_SESSION['chatHistory'][$fid];
}
$items .= '{ "s": "0", "f": { "id": "' . $fid . '", "name": "' . $fnm . '" }, "m": "' . $msg . '" },';
if (!isset($_SESSION['chatHistory'][$fid])) {
$_SESSION['chatHistory'][$fid] = '';
}
$_SESSION['chatHistory'][$fid] .= '{ "s": "0", "f": { "id": "' . $fid . '", "name": "' . $fnm . '" }, "m": "' . $msg . '" },';
unset($_SESSION['tsChatBoxes'][$fid]);
$_SESSION['openChatBoxes'][$fid] = $snt;
}
// Last message...
if (!empty($_SESSION['openChatBoxes'])) {
foreach ($_SESSION['openChatBoxes'] as $chatbox => $time) {
if (!isset($_SESSION['tsChatBoxes'][$chatbox])) {
$now = time() - $time;
$gen = e107::getDate();
$df = vartrue($this->plugPrefs['fb_chat_date_format'], "short");
$time_s = $gen->convert_date($time, $df);
$message = LANF_FB_CHAT_03 . " " . $time_s;
if ($now > 180) {
$items .= '{ "s": "2", "f": { "id": "' . $chatbox . '" }, "m": "' . $message . '" },';
if (!isset($_SESSION['chatHistory'][$chatbox])) {
$_SESSION['chatHistory'][$chatbox] = '';
}
$_SESSION['chatHistory'][$chatbox] .= '{ "s": "2", "f": { "id": "' . $chatbox . '" }, "m": "' . $message . '" },';
$_SESSION['tsChatBoxes'][$chatbox] = 1;
}
}
}
}
e107::getDb()->update("fb_chat", "fb_chat_rcd = 1 WHERE fb_chat_to = " . USERID . " AND fb_chat_rcd = 0 ");
if (!empty($items)) {
$items = substr($items, 0, -1);
}
header('Content-type: application/json');
echo '{"items": [' . $items . ']}';
exit;
}
/**
* Send chat message to a user
* - Parse the message with toDB method
* - Insert the message into DB
* - Create output HTML from parsed message
* - Append message details to session and push output as json
*
* @param int $to
* User ID addressed to.
* @param string $msg
* The message that will be sent.
* @param int $from (optional)
* User ID of the sender
*/
public function chat_send($to = 0, $msg = "", $from = USERID) {
if ((int) $to === 0 || (int) $from === 0) {
exit;
}
// Parse input string
$msg = e107::getParser()->toDB($msg);
// Insert parsed message into DB.
$arg = array(
"fb_chat_from" => (int) $from,
"fb_chat_to" => $to,
"fb_chat_msg" => $msg,
"fb_chat_sent" => time(),
);
e107::getDb()->insert('fb_chat', $arg);
// Create output HTML message
$message = $this->handle_output($msg);
// Get display name of the sender
$row = get_user_data((int) $from);
$name = $this->get_user_name_by_names($row['user_name'], $row['user_login']);
$_SESSION['openChatBoxes'][$to] = time();
if (!isset($_SESSION['chatHistory'][$to])) {
$_SESSION['chatHistory'][$to] = '';
}
$_SESSION['chatHistory'][$to] .= '{ "s": "1", "f": { "id": "' . $to . '", "name": "' . $name . '" } , "m": "' . $message . '" },';
unset($_SESSION['tsChatBoxes'][$to]);
header('Content-type: application/json');
echo '{ "f": "' . $name . '", "m": "' . $message . '" }';
exit;
}
/**
* Close chat session
*/
public function chat_close() {
unset($_SESSION['openChatBoxes'][$_POST['chatbox']]);
echo "1";
exit;
}
/**
* Get chat history from session
*
* @param int $chatbox
* Chat partner (User ID) of the current user.
* @param string $items
* Chat history, json formatted string
* @return string $items
* Chat history, json formatted string
*/
public function chat_box_session($chatbox, $items = "") {
if (isset($_SESSION['chatHistory'][$chatbox])) {
$items = $_SESSION['chatHistory'][$chatbox];
}
return $items;
}
/**
* Get the user display name (user_name or user_login)
*
* @param int $uid
* User ID
*/
public function get_user_name($uid = 0) {
if ((int) $uid === 0) {
exit;
}
$link = vartrue($this->plugPrefs['fb_chat_title_link'], 1);
$row = get_user_data(intval($uid));
$name = $this->get_user_name_by_names($row['user_name'], $row['user_login']);
if ((boolean) $link) {
$name = "<a href='" . SITEURL . "user.php?id." . (int) $uid . "' target='_self'>" . $name . "</a>";
}
header('Content-type: application/json');
echo '{"name": "' . $name . '"}';
exit;
}
/**
* Get a HTML list with online users
* - Get online user from DB
* - Create HTML list
* - Push output HTML
*/
public function get_online_list($mode = 0) {
$template = e107::getTemplate('fb_chat');
$sc = e107::getScBatch('fb_chat', TRUE);
$tp = e107::getParser();
$users = $this->get_online_users();
$menu = "";
if (!empty($users)) {
if ((int) $mode === 0) {
$menu .= $tp->parseTemplate($template['MENU_START']);
foreach ($users as $user) {
$sc->setVars($user);
$menu .= $tp->parseTemplate($template['MENU_ITEM'], TRUE, $sc);
}
$menu .= $tp->parseTemplate($template['MENU_END']);
}
if ((int) $mode === 1) {
$menu .= $tp->parseTemplate($template['FLOAT_MENU_START']);
foreach ($users as $user) {
$sc->setVars($user);
$menu .= $tp->parseTemplate($template['FLOAT_MENU_ITEM'], TRUE, $sc);
}
$menu .= $tp->parseTemplate($template['FLOAT_MENU_END']);
}
}
header('Content-Type: text/html; charset=utf-8');
echo $menu;
exit;
}
/**
* Turn on chat by current user.
* Delete record from "turned off" table.
*/
public function turn_chat_on() {
e107::getDb()->delete("fb_chat_turnedoff", "fb_chat_turnedoff_uid = " . USERID);
}
/**
* Turn off chat by crrent user.
* Insert row into "turned off" table.
*/
public function turn_chat_off() {
$arg = array("fb_chat_turnedoff_uid" => USERID);
e107::getDb()->insert('fb_chat_turnedoff', $arg);
}
}
new fb_chat;
?>