function heartbeat() { $response = array(); $messages = array(); global $userid; global $db; global $chatrooms_language; global $chatroomTimeout; global $lastMessages; global $cookiePrefix; $usertable = TABLE_PREFIX . DB_USERTABLE; $usertable_username = DB_USERTABLE_NAME; $usertable_userid = DB_USERTABLE_USERID; $time = getTimeStamp(); $chatroomList = array(); if (isset($_POST['popout']) && $_POST['popout'] == 0) { $_SESSION['cometchat']['cometchat_chatroomspopout'] = $time; } if (!empty($_POST['currentroom']) && $_POST['currentroom'] != 0) { $sql = "insert into cometchat_chatrooms_users (userid,chatroomid,lastactivity) values ('" . mysql_real_escape_string($userid) . "','" . mysql_real_escape_string($_POST['currentroom']) . "','" . mysql_real_escape_string($time) . "') on duplicate key update chatroomid = '" . mysql_real_escape_string($_POST['currentroom']) . "', lastactivity = '" . mysql_real_escape_string($time) . "'"; $query = mysql_query($sql); } if (empty($_SESSION['cometchat']['cometchat_chatroomslist']) || !empty($_POST['force']) || !empty($_SESSION['cometchat']['cometchat_chatroomslist']) && $time - $_SESSION['cometchat']['cometchat_chatroomslist'] > REFRESH_BUDDYLIST) { $sql = "select cometchat_chatrooms.id, cometchat_chatrooms.name, cometchat_chatrooms.type, cometchat_chatrooms.password, cometchat_chatrooms.lastactivity, cometchat_chatrooms.createdby, (SELECT count(userid) online FROM cometchat_chatrooms_users where cometchat_chatrooms_users.chatroomid = cometchat_chatrooms.id and '{$time}'-lastactivity<" . ONLINE_TIMEOUT . ") online from cometchat_chatrooms where (type <> 2 or type = 2 and createdby = '" . mysql_real_escape_string($userid) . "' )and (createdby = 0 OR (createdby <> 0 AND ('" . mysql_real_escape_string($time) . "'-lastactivity < {$chatroomTimeout}))) order by name asc"; $query = mysql_query($sql); while ($chatroom = mysql_fetch_array($query)) { $s = 0; if ($chatroom['createdby'] != $userid) { $chatroom['password'] = ''; } else { $s = 1; } $chatroomList[] = array('id' => $chatroom['id'], 'name' => $chatroom['name'], 'online' => $chatroom['online'], 'type' => $chatroom['type'], 'i' => $chatroom['password'], 's' => $s); } $_SESSION['cometchat']['cometchat_chatroomslist'] = $time; $ch = md5(serialize($chatroomList)); if (empty($_POST['clh']) || !empty($_POST['clh']) && $ch != $_POST['clh']) { if (!empty($chatroomList)) { $response['chatrooms'] = $chatroomList; } $response['clh'] = $ch; } } if (!empty($_POST['currentroom']) && $_POST['currentroom'] != 0) { $sql = "select {$usertable}.{$usertable_userid} as userid, {$usertable}.{$usertable_username} as username from {$usertable}, cometchat_chatrooms_users where {$usertable}.{$usertable_userid} = cometchat_chatrooms_users.userid and chatroomid = '" . mysql_real_escape_string($_POST['currentroom']) . "' and ('{$time}' - cometchat_chatrooms_users.lastactivity < " . ONLINE_TIMEOUT . ") order by {$usertable}.{$usertable_username} asc"; $query = mysql_query($sql); $users = array(); while ($chat = mysql_fetch_array($query)) { if (function_exists('processName')) { $chat['username'] = processName($chat['username']); } if ($userid == $chat['userid']) { $chat['userid'] = 0; } $users[] = array('id' => $chat['userid'], 'n' => $chat['username']); } $uh = md5(serialize($users)); if (empty($_POST['ulh']) || !empty($_POST['ulh']) && $uh != $_POST['ulh']) { $response['ulh'] = $uh; if (!empty($users)) { $response['users'] = $users; } } if (USE_COMET != 1 || COMET_CHATROOMS != 1) { $reverse = 1; $sql = "select cometchat_chatroommessages.id, cometchat_chatroommessages.message, cometchat_chatroommessages.sent, m.{$usertable_username} `from`, cometchat_chatroommessages.userid fromid, m.{$usertable_userid} userid from cometchat_chatroommessages, {$usertable} m where cometchat_chatroommessages.chatroomid = '" . mysql_real_escape_string($_POST['currentroom']) . "' and m.{$usertable_userid} = cometchat_chatroommessages.userid order by cometchat_chatroommessages.id desc limit {$lastMessages}"; if ($_POST['timestamp'] != 0) { $sql = "select cometchat_chatroommessages.id, cometchat_chatroommessages.message, cometchat_chatroommessages.sent, m.{$usertable_username} `from`, cometchat_chatroommessages.userid fromid, m.{$usertable_userid} userid from cometchat_chatroommessages, {$usertable} m where cometchat_chatroommessages.chatroomid = '" . mysql_real_escape_string($_POST['currentroom']) . "' and m.{$usertable_userid} = cometchat_chatroommessages.userid and cometchat_chatroommessages.id > '" . mysql_real_escape_string($_POST['timestamp']) . "' order by cometchat_chatroommessages.id desc"; $reverse = 0; } $query = mysql_query($sql); while ($chat = mysql_fetch_array($query)) { if (function_exists('processName')) { $chat['from'] = processName($chat['from']); } if ($userid == $chat['userid']) { $chat['from'] = $chatrooms_language[6]; $chat['fromid'] = 0; } else { if (!empty($_COOKIE[$cookiePrefix . 'lang'])) { $translated = translate($chat['message'], '', $_COOKIE[$cookiePrefix . 'lang']); if ($translated != '') { $chat['message'] = strip_tags($translated) . ' <span class="untranslatedtext">(' . $chat['message'] . ')</span>'; } } } array_unshift($messages, array('id' => $chat['id'], 'from' => $chat['from'], 'fromid' => $chat['fromid'], 'message' => $chat['message'], 'sent' => $chat['sent'] + $_SESSION['cometchat']['timedifference'])); } } else { if ($_POST['timestamp'] == 0) { $comet = new Comet(KEY_A, KEY_B); $history = $comet->history(array('channel' => md5('chatroom_' . $_POST['currentroom'] . KEY_A . KEY_B . KEY_C), 'limit' => $lastMessages)); $moremessages = array(); if (!empty($history)) { foreach ($history as $message) { $moremessages[] = array("id" => $message['sent'] . rand(0, 1000000), "from" => $message['from'], "fromid" => "0", "message" => $message['message'], "old" => 1, 'sent' => $message['sent'] + $_SESSION['cometchat']['timedifference']); } $messages = array_merge($messages, $moremessages); usort($messages, 'comparetime'); } } } if (!empty($messages)) { $response['messages'] = $messages; } $sql = "select password from cometchat_chatrooms where id = '" . mysql_real_escape_string($_POST['currentroom']) . "' limit 1"; $query = mysql_query($sql); $room = mysql_fetch_array($query); if (!empty($room['password']) && (empty($_POST['currentp']) || $room['password'] != $_POST['currentp'])) { $response['users'] = array(); $response['messages'] = array(); } } header('Content-type: application/json; charset=utf-8'); echo json_encode($response); exit; }
function sendChatroomMessage($to = 0, $message = '', $notsilent = 1) { global $userid; global $cookiePrefix; global $bannedUserIDs; if ($to == 0 && empty($_POST['currentroom']) || $message == '' && $notsilent == 0 || isset($_POST['message']) && $_POST['message'] == '' || empty($userid) || in_array($userid, $bannedUserIDs)) { return; } if (isset($_POST['message']) && !empty($_POST['currentroom'])) { $to = mysqli_real_escape_string($GLOBALS['dbh'], $_POST['currentroom']); //$message = mysqli_real_escape_string($GLOBALS['dbh'],$_POST['message']); $message = $_POST['message']; } if (isset($message) && $message != '') { if (strpos($message, 'CC^CONTROL_') !== false) { $message = str_ireplace('CC^CONTROL_', '', $message); $message = sanitize($message); $controlparameters = json_decode($message, true); switch ($controlparameters['name']) { case 'avchat': $grp = $controlparameters['params']['grp']; switch ($controlparameters['method']) { case 'endcall': $message = 'CC^CONTROL_AVCHAT_END_CHATROOM_CALL' . $grp; break; case 'rejectcall': $message = 'CC^CONTROL_AVCHAT_REJECT_CHATROOM_CALL' . $grp; break; case 'noanswer': $message = 'CC^CONTROL_AVCHAT_NO_ANSWER_CHATROOM' . $grp; break; case 'canceloutgoingcall': $message = 'CC^CONTROL_AVCHAT_CANCEL_CALL' . $grp; break; case 'busycall': $message = 'CC^CONTROL_AVCHAT_BUSY_CALL' . $grp; break; default: $message = ''; break; } break; case 'audiochat': $grp = $controlparameters['params']['grp']; switch ($controlparameters['method']) { case 'endcall': $message = 'CC^CONTROL_AUDIOCHAT_END_CHATROOM_CALL' . $grp; break; case 'rejectcall': $message = 'CC^CONTROL_AUDIOCHAT_REJECT_CHATROOM_CALL' . $grp; break; case 'noanswer': $message = 'CC^CONTROL_AUDIOCHAT_NO_ANSWER_CHATROOM' . $grp; break; case 'canceloutgoingcall': $message = 'CC^CONTROL_AUDIOCHAT_CANCEL_CALL' . $grp; break; case 'busycall': $message = 'CC^CONTROL_AUDIOCHAT_BUSY_CALL' . $grp; break; default: $message = ''; break; } break; case 'broadcast': $grp = $controlparameters['params']['grp']; switch ($controlparameters['method']) { case 'endcall': $message = 'CC^CONTROL_BROADCAST_END_CHATROOM_CALL' . $grp; break; default: $message = ''; break; } break; case 'chatroom': $delid = $controlparameters['params']['id']; switch ($controlparameters['method']) { case 'deletemessage': $message = 'CC^CONTROL_deletemessage_' . $delid; break; case 'kicked': $message = 'CC^CONTROL_kicked_' . $delid; break; case 'banned': $message = 'CC^CONTROL_banned_' . $delid; break; default: $message = ''; break; } break; default: break; } } } if ($notsilent !== 0) { $message = str_ireplace('CC^CONTROL_', '', $message); $message = sanitize($message); } $styleStart = ''; $styleEnd = ''; if (!empty($_COOKIE[$cookiePrefix . 'chatroomcolor']) && preg_match('/^[a-f0-9]{6}$/i', $_COOKIE[$cookiePrefix . 'chatroomcolor']) && $notsilent == 1) { $styleStart = '<span style="color:#' . mysqli_real_escape_string($GLOBALS['dbh'], $_COOKIE[$cookiePrefix . 'chatroomcolor']) . '">'; $styleEnd = '</span>'; } if (USE_COMET == 1 && COMET_CHATROOMS == 1) { $comet = new Comet(KEY_A, KEY_B); if (empty($_SESSION['cometchat']['username'])) { $name = ''; $sql = getUserDetails($userid); if ($userid > 10000000) { $sql = getGuestDetails($userid); } $result = mysqli_query($GLOBALS['dbh'], $sql); if ($row = mysqli_fetch_assoc($result)) { if (function_exists('processName')) { $row['username'] = processName($row['username']); } $name = $row['username']; } $_SESSION['cometchat']['username'] = $name; } else { $name = $_SESSION['cometchat']['username']; } if (!empty($name)) { $sql = "insert into cometchat_chatroommessages (userid,chatroomid,message,sent) values ('" . mysqli_real_escape_string($GLOBALS['dbh'], $userid) . "', '" . mysqli_real_escape_string($GLOBALS['dbh'], $to) . "','" . mysqli_real_escape_string($GLOBALS['dbh'], $styleStart) . mysqli_real_escape_string($GLOBALS['dbh'], $message) . mysqli_real_escape_string($GLOBALS['dbh'], $styleEnd) . "','" . getTimeStamp() . "')"; $query = mysqli_query($GLOBALS['dbh'], $sql); $insertedid = mysqli_insert_id($GLOBALS['dbh']); if (defined('DEV_MODE') && DEV_MODE == '1') { echo mysqli_error($GLOBALS['dbh']); } $timestamp = getTimeStamp(); $info = $comet->publish(array('channel' => md5('chatroom_' . $to . KEY_A . KEY_B . KEY_C), 'message' => array("id" => $insertedid, "from" => $name, "fromid" => $userid, "message" => $styleStart . $message . $styleEnd, "sent" => getTimeStamp()))); if ($notsilent == 1) { sendCCResponse(json_encode(array("id" => $insertedid, "m" => $styleStart . $message . $styleEnd))); } } } else { $sql = "insert into cometchat_chatroommessages (userid,chatroomid,message,sent) values ('" . mysqli_real_escape_string($GLOBALS['dbh'], $userid) . "', '" . mysqli_real_escape_string($GLOBALS['dbh'], $to) . "','" . mysqli_real_escape_string($GLOBALS['dbh'], $styleStart) . mysqli_real_escape_string($GLOBALS['dbh'], $message) . mysqli_real_escape_string($GLOBALS['dbh'], $styleEnd) . "','" . mysqli_real_escape_string($GLOBALS['dbh'], getTimeStamp()) . "')"; $query = mysqli_query($GLOBALS['dbh'], $sql); $insertedid = mysqli_insert_id($GLOBALS['dbh']); if (defined('DEV_MODE') && DEV_MODE == '1') { echo mysqli_error($GLOBALS['dbh']); } if ($notsilent == 1) { sendCCResponse(json_encode(array("id" => $insertedid, "m" => $styleStart . $message . $styleEnd))); } } parsePusher($to, $insertedid, $message, '1'); $sql = "update cometchat_chatrooms set lastactivity = '" . mysqli_real_escape_string($GLOBALS['dbh'], getTimeStamp()) . "' where id = '" . mysqli_real_escape_string($GLOBALS['dbh'], $to) . "'"; $query = mysqli_query($GLOBALS['dbh'], $sql); if ($notsilent == 0) { return $insertedid; } }
function heartbeat() { global $response; global $userid; global $db; global $chatrooms_language; global $chatroomTimeout; global $lastMessages; global $cookiePrefix; global $allowAvatar; global $moderatorUserIDs; global $guestsMode, $crguestsMode, $guestnamePrefix; $usertable = TABLE_PREFIX . DB_USERTABLE; $usertable_username = DB_USERTABLE_NAME; $usertable_userid = DB_USERTABLE_USERID; $time = getTimeStamp(); $chatroomList = array(); $cachedChatrooms = array(); if (isset($_POST['popout']) && $_POST['popout'] == 0) { $_SESSION['cometchat']['cometchat_chatroomspopout'] = $time; } if (!empty($_POST['currentroom']) && $_POST['currentroom'] != 0) { $sql = "insert into cometchat_chatrooms_users (userid,chatroomid,lastactivity,isbanned) values ('" . mysql_real_escape_string($userid) . "','" . mysql_real_escape_string($_POST['currentroom']) . "','" . mysql_real_escape_string($time) . "','0') on duplicate key update chatroomid = '" . mysql_real_escape_string($_POST['currentroom']) . "', lastactivity = '" . mysql_real_escape_string($time) . "'"; $query = mysql_query($sql); } if (empty($_SESSION['cometchat']['cometchat_chatroomslist']) || !empty($_POST['force']) || !empty($_SESSION['cometchat']['cometchat_chatroomslist']) && $time - $_SESSION['cometchat']['cometchat_chatroomslist'] > REFRESH_BUDDYLIST) { if ($cachedChatrooms = getCache($cookiePrefix . 'chatroom_list', 30)) { $cachedChatrooms = unserialize($cachedChatrooms); } else { $sql = "select DISTINCT cometchat_chatrooms.id, cometchat_chatrooms.name, cometchat_chatrooms.type, cometchat_chatrooms.password, cometchat_chatrooms.lastactivity, cometchat_chatrooms.createdby, (SELECT count(userid) online FROM cometchat_chatrooms_users where cometchat_chatrooms_users.chatroomid = cometchat_chatrooms.id and '{$time}'-lastactivity<" . ONLINE_TIMEOUT . " and isbanned<>'1') online from cometchat_chatrooms order by name asc"; $query = mysql_query($sql); while ($chatroom = mysql_fetch_array($query)) { $cachedChatrooms[$chatroom['id']] = array('id' => $chatroom['id'], 'name' => $chatroom['name'], 'online' => $chatroom['online'], 'type' => $chatroom['type'], 'password' => $chatroom['password'], 'lastactivity' => $chatroom['lastactivity'], 'createdby' => $chatroom['createdby']); } setCache($cookiePrefix . 'chatroom_list', serialize($cachedChatrooms), 30); } foreach ($cachedChatrooms as $key => $chatroom) { if ($chatroom['createdby'] == 0 || $chatroom['createdby'] != 0 && $chatroom['type'] != 2 && $time - $chatroom['lastactivity'] < $chatroomTimeout || $chatroom['createdby'] == $userid) { $s = 0; if ($chatroom['createdby'] != $userid) { if (!in_array($userid, $moderatorUserIDs)) { $chatroom['password'] = ''; } else { $s = 2; } } else { $s = 1; } $chatroomList[$chatroom['id']] = array('id' => $chatroom['id'], 'name' => $chatroom['name'], 'online' => $chatroom['online'], 'type' => $chatroom['type'], 'i' => $chatroom['password'], 's' => $s); } } $_SESSION['cometchat']['cometchat_chatroomslist'] = $time; $clh = md5(serialize($chatroomList)); if (empty($_POST['clh']) || !empty($_POST['clh']) && $clh != $_POST['clh']) { if (!empty($chatroomList)) { $response['chatrooms'] = $chatroomList; } $response['clh'] = $clh; } } if (!empty($_POST['currentroom']) && $_POST['currentroom'] != 0) { $users = array(); $messages = array(); if ($cachedUsers = getCache($cookiePrefix . 'chatrooms_users' . $_POST['currentroom'], 30)) { $users = unserialize($cachedUsers); } else { $sql = "select DISTINCT " . TABLE_PREFIX . DB_USERTABLE . "." . DB_USERTABLE_USERID . " userid, " . TABLE_PREFIX . DB_USERTABLE . "." . DB_USERTABLE_NAME . " username, " . TABLE_PREFIX . DB_USERTABLE . "." . DB_USERTABLE_LASTACTIVITY . " lastactivity, " . DB_AVATARFIELD . " avatar, cometchat_chatrooms_users.isbanned from " . TABLE_PREFIX . DB_USERTABLE . " left join cometchat_status on " . TABLE_PREFIX . DB_USERTABLE . "." . DB_USERTABLE_USERID . " = cometchat_status.userid inner join cometchat_chatrooms_users on " . TABLE_PREFIX . DB_USERTABLE . "." . DB_USERTABLE_USERID . " = cometchat_chatrooms_users.userid " . DB_AVATARTABLE . " where chatroomid = '" . mysql_real_escape_string($_POST['currentroom']) . "' and ('" . mysql_real_escape_string($time) . "' - cometchat_chatrooms_users.lastactivity < " . ONLINE_TIMEOUT . ") order by username asc"; if ($guestsMode && $crguestsMode) { $sql = getChatroomGuests($_POST['currentroom'], $time, $sql); } $query = mysql_query($sql); while ($chat = mysql_fetch_array($query)) { if (function_exists('processName')) { $chat['username'] = processName($chat['username']); } $avatar = ''; if ($allowAvatar) { $avatar = getAvatar($chat['avatar']); } $users[] = array('id' => $chat['userid'], 'n' => $chat['username'], 'a' => $avatar, 'b' => $chat['isbanned']); } setCache($cookiePrefix . 'chatrooms_users' . $_POST['currentroom'], serialize($users), 30); } $ulh = md5(serialize($users)); if (empty($_POST['ulh']) || !empty($_POST['ulh']) && $ulh != $_POST['ulh']) { $response['ulh'] = $ulh; if (!empty($users)) { $response['users'] = $users; } } if (USE_COMET != 1 || COMET_CHATROOMS != 1) { $limit = $lastMessages; if ($lastMessages == 0) { $limit = 1; } $guestpart = ""; $limitClause = " limit " . $limit . " "; $timestampCondition = ""; if ($_POST['timestamp'] != 0) { $timestampCondition = " and cometchat_chatroommessages.id > '" . mysql_real_escape_string($_POST['timestamp']) . "' "; $limitClause = ""; } if ($guestsMode && $crguestsMode) { $guestpart = " UNION select DISTINCT cometchat_chatroommessages.id id, cometchat_chatroommessages.message, cometchat_chatroommessages.sent, CONCAT('" . $guestnamePrefix . "-',m.name) `from`, cometchat_chatroommessages.userid fromid, m.id userid from cometchat_chatroommessages join cometchat_guests m on m.id = cometchat_chatroommessages.userid where cometchat_chatroommessages.chatroomid = '" . mysql_real_escape_string($_POST['currentroom']) . "' and cometchat_chatroommessages.message not like 'banned_%' and cometchat_chatroommessages.message not like 'kicked_%' " . $timestampCondition; } $sql = "select DISTINCT cometchat_chatroommessages.id id, cometchat_chatroommessages.message, cometchat_chatroommessages.sent, m.{$usertable_username} `from`, cometchat_chatroommessages.userid fromid, m.{$usertable_userid} userid from cometchat_chatroommessages join {$usertable} m on m.{$usertable_userid} = cometchat_chatroommessages.userid where cometchat_chatroommessages.chatroomid = '" . mysql_real_escape_string($_POST['currentroom']) . "' and cometchat_chatroommessages.message not like 'banned_%' and cometchat_chatroommessages.message not like 'kicked_%' " . $timestampCondition . $guestpart . " order by id desc " . $limitClause; $query = mysql_query($sql); while ($chat = mysql_fetch_array($query)) { if (function_exists('processName')) { $chat['from'] = processName($chat['from']); } if ($lastMessages == 0 && $_POST['timestamp'] == 0) { $chat['message'] = ''; } if ($userid == $chat['userid']) { $chat['from'] = $chatrooms_language[6]; } else { if (!empty($_COOKIE[$cookiePrefix . 'lang']) && !(strpos($chat['message'], "CC^CONTROL_") > -1)) { $translated = text_translate($chat['message'], '', $_COOKIE[$cookiePrefix . 'lang']); if ($translated != '') { $chat['message'] = strip_tags($translated) . ' <span class="untranslatedtext">(' . $chat['message'] . ')</span>'; } } } array_unshift($messages, array('id' => $chat['id'], 'from' => $chat['from'], 'fromid' => $chat['fromid'], 'message' => $chat['message'], 'sent' => $chat['sent'] + $_SESSION['cometchat']['timedifference'])); } } else { if ($_POST['timestamp'] == 0) { $comet = new Comet(KEY_A, KEY_B); $history = $comet->history(array('channel' => md5('chatroom_' . $_POST['currentroom'] . KEY_A . KEY_B . KEY_C), 'limit' => $lastMessages + 5)); $moremessages = array(); $count_msg = 0; $i = 0; if (!empty($history)) { foreach ($history as $message) { if (strpos($message['message'], 'CC^CONTROL_') > -1) { continue; } $moremessages[$message['sent']] = array("id" => $message['sent'], "from" => $message['from'], "fromid" => "0", "message" => $message['message'], "old" => 1, 'sent' => round($message['sent'] / 1000) + $_SESSION['cometchat']['timedifference']); } $messages = array_merge($messages, $moremessages); $count_msg = count($messages); usort($messages, 'comparetime'); $messages = $lastMessages > $count_msg ? $messages : array_slice($messages, -$lastMessages); } } } if (!empty($messages)) { $response['messages'] = $messages; } $sql = "select password from cometchat_chatrooms where id = '" . mysql_real_escape_string($_POST['currentroom']) . "' limit 1"; $query = mysql_query($sql); $room = mysql_fetch_array($query); if (!empty($room['password']) && (empty($_POST['currentp']) || $room['password'] != $_POST['currentp'])) { $response['users'] = array(); $response['messages'] = array(); } } header('Content-type: application/json; charset=utf-8'); echo json_encode($response); }
function getChatboxData($id) { global $messages; global $userid; if (!empty($id) && USE_COMET == 1) { if (!empty($_SESSION['cometchat']['cometmessagesafter'])) { $comet = new Comet(KEY_A, KEY_B); $history = $comet->history(array('channel' => md5($userid . KEY_A . KEY_B . KEY_C), 'limit' => COMET_HISTORY_LIMIT)); if (!empty($_SESSION['cometchat']['cometchat_user_' . $id])) { $messages = array_merge($messages, $_SESSION['cometchat']['cometchat_user_' . $id]); } $moremessages = array(); $messagesafter = $_SESSION['cometchat']['cometmessagesafter']; if (!empty($_SESSION['cometchat']['cometchat_user_' . $id . '_clear']) && $_SESSION['cometchat']['cometchat_user_' . $id . '_clear']['timestamp'] > $_SESSION['cometchat']['cometmessagesafter']) { $messagesafter = $_SESSION['cometchat']['cometchat_user_' . $id . '_clear']['timestamp']; } if (!empty($history)) { foreach ($history as $message) { if ($message['from'] == $id && $message['sent'] >= $messagesafter) { $moremessages[] = array("id" => $message['sent'] . rand(0, 1000000), "from" => $message['from'], "message" => $message['message'], "self" => $message['self'], "old" => 1, 'sent' => $message['sent'] + $_SESSION['cometchat']['timedifference']); } } } $messages = array_merge($messages, $moremessages); usort($messages, 'comparetime'); } } else { if (!empty($id) && !empty($_SESSION['cometchat']['cometchat_user_' . $id])) { $messages = array_merge($messages, $_SESSION['cometchat']['cometchat_user_' . $id]); } } }
function getChatboxData($id) { global $messages; global $userid; if (!empty($id) && USE_COMET == 1) { if (!empty($_SESSION['cometchat']['cometmessagesafter'])) { $key = KEY_A . KEY_B . KEY_C; $channel = md5($userid . $key); if (function_exists('mcrypt_encrypt')) { $channel = md5(base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5($key), $userid, MCRYPT_MODE_CBC, md5(md5($key)))) . $key); } $comet = new Comet(KEY_A, KEY_B); $history = $comet->history(array('channel' => $channel, 'limit' => COMET_HISTORY_LIMIT)); if (!empty($_SESSION['cometchat']['cometchat_user_' . $id])) { $messages = array_merge($messages, $_SESSION['cometchat']['cometchat_user_' . $id]); } $moremessages = array(); $messagesafter = $_SESSION['cometchat']['cometmessagesafter']; if (!empty($_SESSION['cometchat']['cometchat_user_' . $id . '_clear']) && $_SESSION['cometchat']['cometchat_user_' . $id . '_clear']['timestamp'] > $_SESSION['cometchat']['cometmessagesafter']) { $messagesafter = $_SESSION['cometchat']['cometchat_user_' . $id . '_clear']['timestamp']; } if (!empty($history)) { foreach ($history as $key => $message) { if ($message['from'] == $id && $message['sent'] >= $messagesafter) { $moremessages['_' . $message['sent']] = array("id" => $message['sent'], "from" => $message['from'], "message" => $message['message'], "self" => $message['self'], "old" => 1, 'sent' => $message['sent'] / 1000); } } } $messages = array_merge($messages, $moremessages); usort($messages, 'comparetime'); } } else { if (!empty($id) && !empty($_SESSION['cometchat']['cometchat_user_' . $id])) { $messages = array_merge($messages, $_SESSION['cometchat']['cometchat_user_' . $id]); } } }
if (isset($_GET['callback'])) { header('content-type: application/json; charset=utf-8'); echo $_GET['callback'] . '(' . json_encode($response) . ')'; } else { echo json_encode($response); } $size = ob_get_length(); header("Content-Length: {$size}"); ob_end_flush(); flush(); $key = KEY_A . KEY_B . KEY_C; $channel = md5($to . $key); if (function_exists('mcrypt_encrypt')) { $channel = md5(base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5($key), $to, MCRYPT_MODE_CBC, md5(md5($key)))) . $key); } $comet = new Comet(KEY_A, KEY_B); $info = $comet->publish(array('channel' => $channel, 'message' => array("from" => $userid, "message" => $message, "sent" => $insertedid, "self" => 0))); if (defined('SAVE_LOGS') && SAVE_LOGS == 1) { $sql = "insert into cometchat (cometchat.from,cometchat.to,cometchat.message,cometchat.sent,cometchat.read) values ('" . mysqli_real_escape_string($GLOBALS['dbh'], $userid) . "', '" . mysqli_real_escape_string($GLOBALS['dbh'], $to) . "','" . mysqli_real_escape_string($GLOBALS['dbh'], $message) . "','" . getTimeStamp() . "',1)"; $query = mysqli_query($GLOBALS['dbh'], $sql); } } else { $sql = "insert into cometchat (cometchat.from,cometchat.to,cometchat.message,cometchat.sent,cometchat.read) values ('" . mysqli_real_escape_string($GLOBALS['dbh'], $userid) . "', '" . mysqli_real_escape_string($GLOBALS['dbh'], $to) . "','" . mysqli_real_escape_string($GLOBALS['dbh'], $message) . "','" . getTimeStamp() . "',0)"; $query = mysqli_query($GLOBALS['dbh'], $sql); $insertedid = mysqli_insert_id($GLOBALS['dbh']); if (defined('DEV_MODE') && DEV_MODE == '1') { echo mysqli_error($GLOBALS['dbh']); } $response = array("insertedid" => $insertedid, "message" => $message); if (isset($_REQUEST['callbackfn']) && $_REQUEST['callbackfn'] == 'mobileapp' && empty($_REQUEST['v'])) { $response = $insertedid;
$to = $_REQUEST['to']; $message = $_REQUEST['message']; if ($userid != '') { if (function_exists('hooks_message')) { hooks_message($userid, $to, $message); } if (!in_array($userid, $bannedUserIDs)) { if (in_array('block', $plugins)) { $sql = "select * from cometchat_block where (fromid = '" . mysql_real_escape_string($to) . "' and toid ='" . mysql_real_escape_string($userid) . "') OR (fromid = '" . mysql_real_escape_string($userid) . "' and toid ='" . mysql_real_escape_string($to) . "')"; $query = mysql_query($sql); if (mysql_num_rows($query) > 0) { return; } } if (USE_COMET == 1) { $comet = new Comet(KEY_A, KEY_B); $info = $comet->publish(array('channel' => md5($to . KEY_A . KEY_B . KEY_C), 'message' => array("from" => $userid, "message" => sanitize($message), "sent" => getTimeStamp(), "self" => 0))); $insertedid = getTimeStamp() . rand(0, 1000000); if (defined('SAVE_LOGS') && SAVE_LOGS == 1) { $sql = "insert into cometchat (cometchat.from,cometchat.to,cometchat.message,cometchat.sent,cometchat.read) values ('" . mysql_real_escape_string($userid) . "', '" . mysql_real_escape_string($to) . "','" . mysql_real_escape_string(sanitize($message)) . "','" . getTimeStamp() . "',1)"; $query = mysql_query($sql); $insertedid = mysql_insert_id(); } } else { $sql = "insert into cometchat (cometchat.from,cometchat.to,cometchat.message,cometchat.sent,cometchat.read) values ('" . mysql_real_escape_string($userid) . "', '" . mysql_real_escape_string($to) . "','" . mysql_real_escape_string(sanitize($message)) . "','" . getTimeStamp() . "',0)"; $query = mysql_query($sql); $insertedid = mysql_insert_id(); if (defined('DEV_MODE') && DEV_MODE == '1') { echo mysql_error(); } }
function getChatboxData($id) { global $messages; global $userid; global $chromeReorderFix; global $prependLimit; if (empty($_REQUEST['prepend'])) { if (USE_COMET == 1) { if (!empty($_SESSION['cometchat']['cometmessagesafter']) || empty($id)) { $key = ''; if (defined('KEY_A') && defined('KEY_B') && defined('KEY_C')) { $key = KEY_A . KEY_B . KEY_C; } $limit = COMET_HISTORY_LIMIT; if (!empty($_REQUEST['callbackfn']) && $_REQUEST['callbackfn'] == 'mobileapp') { $limit = 10; } $channel = md5($userid . $key); $comet = new Comet(KEY_A, KEY_B); $history = $comet->history(array('channel' => $channel, 'limit' => $limit)); if (!empty($id) && !empty($_SESSION['cometchat']['cometchat_user_' . $id])) { $messages = array_replace($messages, $_SESSION['cometchat']['cometchat_user_' . $id]); } $moremessages = array(); if (!empty($_SESSION['cometchat']['cometmessagesafter'])) { $messagesafter = $_SESSION['cometchat']['cometmessagesafter']; } if (!empty($_SESSION['cometchat']['cometchat_user_' . $id . '_clear']) && $_SESSION['cometchat']['cometchat_user_' . $id . '_clear']['timestamp'] > $_SESSION['cometchat']['cometmessagesafter']) { $messagesafter = $_SESSION['cometchat']['cometchat_user_' . $id . '_clear']['timestamp']; } if (!empty($history)) { foreach ($history as $key => $message) { if ($message['from'] == $id && $message['sent'] >= $messagesafter || empty($id)) { $moremessages[$chromeReorderFix . $message['sent']] = array("id" => $message['sent'], "from" => $message['from'], "message" => $message['message'], "self" => $message['self'], "old" => 1, 'sent' => $message['sent'] / 1000); } } } if (!empty($id) && empty($_SESSION['cometchat']['cometchat_user_' . $id])) { getPrevMessages($id); } if (!empty($_REQUEST['callbackfn']) && $_REQUEST['callbackfn'] == 'mobileapp') { $messages = $moremessages; } else { $messages = array_replace($messages, $moremessages); usort($messages, 'comparetime'); } } } else { if (!empty($id) && !empty($_SESSION['cometchat']['cometchat_user_' . $id])) { $messages = array_replace($messages, $_SESSION['cometchat']['cometchat_user_' . $id]); } if (!empty($id) && empty($_SESSION['cometchat']['cometchat_user_' . $id])) { getPrevMessages($id); $messages = array_reverse($messages); } } } else { if (USE_COMET == 1 && SAVE_LOGS == 0) { return; } $prelimit = intval($prependLimit); $messages = array(); if ($_REQUEST['prepend'] != '-1') { $prepend = bigintval($_REQUEST['prepend']); $sql = "select * from cometchat where ((cometchat.from = " . mysqli_real_escape_string($GLOBALS['dbh'], $userid) . " and cometchat.to = " . $id . " and direction <>1) or ( cometchat.from = " . mysqli_real_escape_string($GLOBALS['dbh'], $id) . " and cometchat.to = " . mysqli_real_escape_string($GLOBALS['dbh'], $userid) . " and direction <> 2)) and (cometchat.id < {$prepend}) order by cometchat.id desc limit {$prelimit};"; } else { $sql = "select * from cometchat where ((cometchat.from = " . mysqli_real_escape_string($GLOBALS['dbh'], $userid) . " and cometchat.to = " . mysqli_real_escape_string($GLOBALS['dbh'], $id) . " and direction <>1) or ( cometchat.from = " . mysqli_real_escape_string($GLOBALS['dbh'], $id) . " and cometchat.to = " . mysqli_real_escape_string($GLOBALS['dbh'], $userid) . " and direction <>2 )) order by cometchat.id desc limit {$prelimit};"; } $query = mysqli_query($GLOBALS['dbh'], $sql); if (defined('DEV_MODE') && DEV_MODE == '1') { echo mysqli_error($GLOBALS['dbh']); } while ($chat = mysqli_fetch_assoc($query)) { $self = 0; $old = 0; if ($chat['from'] == $userid) { $chat['from'] = $chat['to']; $self = 1; $old = 1; } if ($chat['read'] == 1) { $old = 1; } $messages[$chromeReorderFix . $chat['id']] = array('id' => $chat['id'], 'from' => $chat['from'], 'message' => $chat['message'], 'self' => $self, 'old' => $old, 'sent' => $chat['sent']); } $messages = array_reverse($messages); } }