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 getChatroomData($chatroomid, $prelimit = 0, $lastMessages = 0) { global $guestsMode, $crguestsMode, $guestnamePrefix; global $language; global $userid; global $cookiePrefix; $usertable = TABLE_PREFIX . DB_USERTABLE; $usertable_username = DB_USERTABLE_NAME; $usertable_userid = DB_USERTABLE_USERID; $messages = array(); $moremessages = array(); $guestpart = ''; $prependCondition = ''; $limitClause = " limit " . mysqli_real_escape_string($GLOBALS['dbh'], $lastMessages) . " "; if (empty($prelimit) && empty($lastMessages)) { if (!empty($_SESSION['cometchat']['cometchat_chatroom_' . $chatroomid])) { $moremessages = $moremessages + $_SESSION['cometchat']['cometchat_chatroom_' . $chatroomid]; } $messages = $messages + $moremessages; krsort($messages); return $messages; } else { if ($prelimit != '-1') { $prelimit = bigintval($prelimit); $prependCondition = "and (cometchat_chatroommessages.id < '" . mysqli_real_escape_string($GLOBALS['dbh'], $prelimit) . "')"; } 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 = '" . mysqli_real_escape_string($GLOBALS['dbh'], $chatroomid) . "' and cometchat_chatroommessages.message not like '%banned_%' and cometchat_chatroommessages.message not like '%kicked_%' and cometchat_chatroommessages.message not like '%deletemessage_%' " . $prependCondition; } $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 = '" . mysqli_real_escape_string($GLOBALS['dbh'], $chatroomid) . "' and cometchat_chatroommessages.message not like '%banned_%' and cometchat_chatroommessages.message not like '%kicked_%' and cometchat_chatroommessages.message not like '%deletemessage_%' " . $prependCondition . $guestpart . " order by id desc " . $limitClause; $query = mysqli_query($GLOBALS['dbh'], $sql); if (defined('DEV_MODE') && DEV_MODE == '1') { echo mysqli_error($GLOBALS['dbh']); } while ($chat = mysqli_fetch_assoc($query)) { if (function_exists('processName')) { $chat['from'] = processName($chat['from']); } if ($lastMessages == 0) { $chat['message'] = ''; } if ($userid == $chat['userid']) { $chat['from'] = $language[10]; } 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>'; } } } $messages[$chat['id']] = array('id' => $chat['id'], 'from' => $chat['from'], 'fromid' => $chat['fromid'], 'message' => $chat['message'], 'sent' => $chat['sent']); } } header('Content-type: application/json; charset=utf-8'); echo json_encode($messages); }
function fetchMessages() { global $response; global $userid; global $db; global $messages; global $cookiePrefix; global $chromeReorderFix; $timestamp = 0; if (USE_COMET == 1) { return; } $sql = "select cometchat.id, cometchat.from, cometchat.to, cometchat.message, cometchat.sent, cometchat.read, cometchat.direction from cometchat where ((cometchat.to = '" . mysqli_real_escape_string($GLOBALS['dbh'], $userid) . "' and cometchat.direction <> 2) or (cometchat.from = '" . mysqli_real_escape_string($GLOBALS['dbh'], $userid) . "' and cometchat.direction <> 1)) and (cometchat.id > '" . mysqli_real_escape_string($GLOBALS['dbh'], $_REQUEST['timestamp']) . "' or (cometchat.to = '" . mysqli_real_escape_string($GLOBALS['dbh'], $userid) . "' and cometchat.read != 1)) order by cometchat.id"; $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; } if (!empty($_COOKIE[$cookiePrefix . 'lang']) && $chat['direction'] == 0 && $self == 0 && $old == 0) { $translated = text_translate($chat['message'], '', $_COOKIE[$cookiePrefix . 'lang']); if ($translated != '') { $chat['message'] = strip_tags($translated) . ' <span class="untranslatedtext">(' . $chat['message'] . ')</span>'; } } $messages[$chromeReorderFix . $chat['id']] = array('id' => $chat['id'], 'from' => $chat['from'], 'message' => $chat['message'], 'self' => $self, 'old' => $old, 'sent' => $chat['sent']); if (empty($SESSION['cometchat']['cometchat_user' . $chat['from']][$chromeReorderFix . $chat['id']]['id'])) { $_SESSION['cometchat']['cometchat_user_' . $chat['from']][$chromeReorderFix . $chat['id']] = array('id' => $chat['id'], 'from' => $chat['from'], 'message' => $chat['message'], 'self' => $self, 'old' => 1, 'sent' => $chat['sent']); } $timestamp = $chat['id']; } if (!empty($messages) && (empty($_REQUEST['callbackfn']) || isset($_REQUEST['callbackfn']) && $_REQUEST['callbackfn'] != 'ccmobiletab')) { $sql = "update cometchat set cometchat.read = '1' where cometchat.to = '" . mysqli_real_escape_string($GLOBALS['dbh'], $userid) . "' and cometchat.id <= '" . mysqli_real_escape_string($GLOBALS['dbh'], $timestamp) . "'"; $query = mysqli_query($GLOBALS['dbh'], $sql); if (defined('DEV_MODE') && DEV_MODE == '1') { echo mysqli_error($GLOBALS['dbh']); } } }
function heartbeat() { global $response; $messages = array(); 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(); 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) { $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 where (type <> 2 or type = 2 and createdby = '" . mysql_real_escape_string($userid) . "' )and (createdby = 0 OR (createdby <> 0 ))and (createdby = 0 OR (createdby <> 0 AND ('" . mysql_real_escape_string($time) . "'-lastactivity < {$chatroomTimeout} or createdby='" . $userid . "'))) order by name asc"; $query = mysql_query($sql); while ($chatroom = mysql_fetch_array($query)) { $s = 0; if ($chatroom['createdby'] != $userid) { if (!in_array($userid, $moderatorUserIDs)) { $chatroom['password'] = ''; } else { $s = 2; } } 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 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); $users = array(); while ($chat = mysql_fetch_array($query)) { if (function_exists('processName')) { $chat['username'] = processName($chat['username']); } $avatar = ''; if ($allowAvatar) { $avatar = getAvatar($chat['avatar']); } if ($chat['userid'] == $userid) { $chat['userid'] = 0; } $users[] = array('id' => $chat['userid'], 'n' => $chat['username'], 'a' => $avatar, 'b' => $chat['isbanned']); } $uh = md5(serialize($users)); if (empty($_POST['ulh']) || !empty($_POST['ulh']) && $uh != $_POST['ulh']) { $response['ulh'] = $uh; if (!empty($users)) { $response['users'] = $users; } } $guestpart = ""; if ($guestsMode && $crguestsMode) { $guestpart = " UNION select cometchat_guests.id {$usertable_userid}, concat('" . $guestnamePrefix . " ',cometchat_guests.name) {$usertable_username} from cometchat_guests "; } $reverse = 1; $sql = "select DISTINCT cometchat_chatroommessages.id, cometchat_chatroommessages.message, cometchat_chatroommessages.sent, m.{$usertable_username} `from`, cometchat_chatroommessages.userid fromid, m.{$usertable_userid} userid from cometchat_chatroommessages, (select {$usertable}.{$usertable_userid}, {$usertable}.{$usertable_username} from {$usertable} " . $guestpart . ") m where cometchat_chatroommessages.chatroomid = '" . mysql_real_escape_string($_POST['currentroom']) . "' and m.{$usertable_userid} = cometchat_chatroommessages.userid and cometchat_chatroommessages.message not like 'banned_%' and cometchat_chatroommessages.message not like 'kicked_%' order by cometchat_chatroommessages.id desc limit {$lastMessages}"; if ($_POST['timestamp'] != 0) { $sql = "select DISTINCT cometchat_chatroommessages.id, cometchat_chatroommessages.message, cometchat_chatroommessages.sent, m.{$usertable_username} `from`, cometchat_chatroommessages.userid fromid, m.{$usertable_userid} userid from cometchat_chatroommessages, (select {$usertable}.{$usertable_userid}, {$usertable}.{$usertable_username} from {$usertable} " . $guestpart . ") 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]; } 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'])); } if (isset($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; }
echo " "; $size_for_translat = strlen($text_for_translate); $arr_for_translate_part = array(); $arr_for_translate_part[] = $text_for_translate; } } if (count($arr_for_translate_part) > 0) { array_text_translate($arr_for_translate_part, $def_lang, $mlang); } echo "-->"; $content .= "<h2>" . text_translate("Main menu", $def_lang, $mlang) . "</h2><ul>"; $li_menu = ""; $i = 0; foreach ($menus as $menu) { $let = explode("http:", $menu->link); $li_menu .= "<li><a href=\"" . (count($let) <= 1 ? $mlang . "/" : "") . $menu->link . "\" title=\"" . text_translate($s_translate[$i], $def_lang, $mlang) . "\">" . text_translate($s_translate[$i], $def_lang, $mlang) . "</a></li>"; $i++; } } catch (Exception $e) { if ($settings['debug']) { echo 'Please try translate this page late. Now happned error at translate time: ' . $e->getMessage(); print_r($e); exit; } else { echo 'Sanity check. Now happned error at translate time: ' . $e->getMessage(); exit; } } $content .= $li_menu; $content .= "</ul>"; }
function heartbeat() { global $response; global $userid; global $chatrooms_language; global $chatroomTimeout; global $lastMessages; global $cookiePrefix; global $allowAvatar; global $moderatorUserIDs; global $guestsMode, $crguestsMode, $guestnamePrefix; global $chromeReorderFix; if (!empty($guestnamePrefix)) { $guestnamePrefix .= '-'; } $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'])) { $sql = "insert into cometchat_chatrooms_users (userid,chatroomid,lastactivity,isbanned) values ('" . mysqli_real_escape_string($GLOBALS['dbh'], $userid) . "','" . mysqli_real_escape_string($GLOBALS['dbh'], $_POST['currentroom']) . "','" . mysqli_real_escape_string($GLOBALS['dbh'], $time) . "','0') on duplicate key update chatroomid = '" . mysqli_real_escape_string($GLOBALS['dbh'], $_POST['currentroom']) . "', lastactivity = '" . mysqli_real_escape_string($GLOBALS['dbh'], $time) . "'"; $query = mysqli_query($GLOBALS['dbh'], $sql); } if (empty($_SESSION['cometchat']['cometchat_chatroomslist']) || !empty($_POST['force']) || !empty($_SESSION['cometchat']['cometchat_chatroomslist']) && $time - $_SESSION['cometchat']['cometchat_chatroomslist'] > REFRESH_BUDDYLIST) { if (!is_array($cachedChatrooms = getCache('chatroom_list'))) { $cachedChatrooms = array(); $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 '" . mysqli_real_escape_string($GLOBALS['dbh'], $time) . "'-lastactivity<" . ONLINE_TIMEOUT . " and isbanned<>'1') online from cometchat_chatrooms order by name asc"; $query = mysqli_query($GLOBALS['dbh'], $sql); while ($chatroom = mysqli_fetch_assoc($query)) { $cachedChatrooms[$chromeReorderFix . $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('chatroom_list', $cachedChatrooms, 30); } foreach ($cachedChatrooms as $key => $chatroom) { if (($chatroom['createdby'] == 0 || $chatroom['createdby'] != 0 && $chatroom['type'] != 2 && $time - $chatroom['lastactivity'] < $chatroomTimeout || $chatroom['createdby'] == $userid) && $chatroom['type'] != 3) { $s = 0; if ($chatroom['createdby'] != $userid) { if (!in_array($userid, $moderatorUserIDs)) { $chatroom['password'] = ''; } else { $s = 2; } } else { $s = 1; } $chatroomList[$chromeReorderFix . $chatroom['id']] = array('id' => $chatroom['id'], 'name' => $chatroom['name'], 'online' => $chatroom['online'], 'type' => $chatroom['type'], 'i' => $chatroom['password'], 's' => $s, 'createdby' => $chatroom['createdby']); } } $_SESSION['cometchat']['cometchat_chatroomslist'] = $time; $clh = md5(serialize($chatroomList)); if (empty($_POST['clh']) || !empty($_POST['clh']) && $clh != $_POST['clh']) { $response['chatrooms'] = $chatroomList; $response['clh'] = $clh; } } if (!empty($_POST['currentroom'])) { $messages = array(); if (!is_array($users = getCache('chatrooms_users' . $_POST['currentroom']))) { $users = array(); $sql = "select DISTINCT " . TABLE_PREFIX . DB_USERTABLE . "." . DB_USERTABLE_USERID . " userid, " . TABLE_PREFIX . DB_USERTABLE . "." . DB_USERTABLE_NAME . " username, " . DB_AVATARFIELD . " avatar, cometchat_status.lastactivity lastactivity, 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 = '" . mysqli_real_escape_string($GLOBALS['dbh'], $_POST['currentroom']) . "' and ('" . mysqli_real_escape_string($GLOBALS['dbh'], $time) . "' - cometchat_chatrooms_users.lastactivity < " . ONLINE_TIMEOUT . ") order by username asc"; if ($guestsMode && $crguestsMode) { $sql = getChatroomGuests($_POST['currentroom'], $time, $sql); } $query = mysqli_query($GLOBALS['dbh'], $sql); while ($chat = mysqli_fetch_assoc($query)) { if (function_exists('processName')) { $chat['username'] = processName($chat['username']); } $avatar = ''; if ($allowAvatar) { $avatar = getAvatar($chat['avatar']); } $users[$chromeReorderFix . $chat['userid']] = array('id' => (int) $chat['userid'], 'n' => $chat['username'], 'a' => $avatar, 'b' => $chat['isbanned']); } setCache('chatrooms_users' . $_POST['currentroom'], $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; } } $limit = $lastMessages; if ($lastMessages == 0) { $limit = 1; } $guestpart = ""; $limitClause = " limit " . mysqli_real_escape_string($GLOBALS['dbh'], $limit) . " "; $timestampCondition = ""; if ($_POST['timestamp'] != 0) { $timestampCondition = " and cometchat_chatroommessages.id > '" . mysqli_real_escape_string($GLOBALS['dbh'], $_POST['timestamp']) . "' "; $limitClause = ""; } elseif (!empty($_SESSION['cometchat']['chatrooms_' . $_POST['currentroom'] . '_clearId'])) { $timestampCondition = " and cometchat_chatroommessages.sent > '" . $_SESSION['cometchat']['chatrooms_' . $_POST['currentroom'] . '_clearId'] / 1000 . "' "; } 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 = '" . mysqli_real_escape_string($GLOBALS['dbh'], $_POST['currentroom']) . "' and cometchat_chatroommessages.message not like 'banned_%' and cometchat_chatroommessages.message not like 'kicked_%' and cometchat_chatroommessages.message not like 'deletemessage_%' " . $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 = '" . mysqli_real_escape_string($GLOBALS['dbh'], $_POST['currentroom']) . "' and cometchat_chatroommessages.message not like 'banned_%' and cometchat_chatroommessages.message not like 'kicked_%' and cometchat_chatroommessages.message not like 'deletemessage_%' " . $timestampCondition . $guestpart . " order by id desc " . $limitClause; $query = mysqli_query($GLOBALS['dbh'], $sql); while ($chat = mysqli_fetch_assoc($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'])); } if (!empty($messages)) { $response['messages'] = $messages; } $sql = "select password from cometchat_chatrooms where id = '" . mysqli_real_escape_string($GLOBALS['dbh'], $_POST['currentroom']) . "'"; $query = mysqli_query($GLOBALS['dbh'], $sql); if ($room = mysqli_fetch_assoc($query)) { if (!empty($room['password']) && (empty($_POST['currentp']) || $room['password'] != $_POST['currentp'])) { $response['users'] = array(); $response['messages'] = array(); } } else { $response['error'] = "ROOM_DOES_NOT_EXISTS"; } } header('Content-type: application/json; charset=utf-8'); echo json_encode($response); }
function fetchMessages() { global $response; global $userid; global $db; global $messages; global $cookiePrefix; global $chromeReorderFix; $timestamp = 0; if (USE_COMET == 1 && empty($_REQUEST['initialize'])) { return; } $sqlpart = array('', '', '', '', ''); $whereclause = array('', ''); if (empty($_REQUEST['v3'])) { if (!empty($_REQUEST['receivedunreadmessages'])) { if (gettype($_REQUEST['receivedunreadmessages']) == 'string') { $_REQUEST['receivedunreadmessages'] = json_decode(str_replace(' ', '', $_REQUEST['receivedunreadmessages'])); } foreach ($_REQUEST['receivedunreadmessages'] as $from => $lastunreadmessageid) { $sqlpart[0] .= " (cometchat.from = '" . mysqli_real_escape_string($GLOBALS['dbh'], $from) . "' and cometchat.id > '" . mysqli_real_escape_string($GLOBALS['dbh'], $lastunreadmessageid) . "') OR "; $sqlpart[1] .= " (cometchat.to = '" . mysqli_real_escape_string($GLOBALS['dbh'], $from) . "' and cometchat.id > '" . mysqli_real_escape_string($GLOBALS['dbh'], $lastunreadmessageid) . "') OR "; $sqlpart[2] .= "'" . $from . "',"; } if (!empty($sqlpart[0])) { $sqlpart[0] = " cometchat.to = '" . mysqli_real_escape_string($GLOBALS['dbh'], $userid) . "' and cometchat.direction <> 2 and ( " . rtrim($sqlpart[0], "OR ") . " )"; $sqlpart[1] = " cometchat.from = '" . mysqli_real_escape_string($GLOBALS['dbh'], $userid) . "' and cometchat.direction <> 1 and (\n\t\t\t\t\t\t\t\t\t" . rtrim($sqlpart[1], "OR ") . " )"; $sqlpart[3] = " and cometchat.from not in (" . rtrim($sqlpart[2], ",") . ")"; $sqlpart[2] = " and cometchat.to not in (" . rtrim($sqlpart[2], ",") . ")"; $whereclause[0] = " ( " . $sqlpart[0] . " ) or ( " . $sqlpart[1] . " ) or "; } } $sqlpart[3] = " ( cometchat.to = '" . mysqli_real_escape_string($GLOBALS['dbh'], $userid) . "' and cometchat.direction <> 2 " . $sqlpart[3] . " ) "; $sqlpart[2] = " ( cometchat.from = '" . mysqli_real_escape_string($GLOBALS['dbh'], $userid) . "' and cometchat.direction = 2 " . $sqlpart[2] . " ) "; $sqlpart[4] = " cometchat.read <> 1 and "; $whereclause[1] = "( " . $sqlpart[4] . " ( " . $sqlpart[3] . " or " . $sqlpart[2] . " ) )"; $sql = "select cometchat.id, cometchat.from, cometchat.to, cometchat.message, cometchat.sent, cometchat.read, cometchat.direction from cometchat where ( " . $whereclause[0] . $whereclause[1] . " ) and cometchat.direction <> 3 order by cometchat.id"; } else { $sql = "select cometchat.id, cometchat.from, cometchat.to, cometchat.message, cometchat.sent, cometchat.read, cometchat.direction from cometchat where ((cometchat.to = '" . mysqli_real_escape_string($GLOBALS['dbh'], $userid) . "' and cometchat.direction <> 2) or (cometchat.from = '" . mysqli_real_escape_string($GLOBALS['dbh'], $userid) . "' and cometchat.direction <> 1)) and (cometchat.id > '" . mysqli_real_escape_string($GLOBALS['dbh'], $_REQUEST['timestamp']) . "' or (cometchat.to = '" . mysqli_real_escape_string($GLOBALS['dbh'], $userid) . "' and cometchat.read <> 1)) and cometchat.direction <> 3 order by cometchat.id"; } $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; } if ((!empty($_REQUEST[$cookiePrefix . 'lang']) || !empty($_COOKIE[$cookiePrefix . 'lang'])) && $self == 0 && $old == 0 && strpos($chat['message'], 'CC^CONTROL_') === false) { if (!empty($_REQUEST[$cookiePrefix . 'lang'])) { $translated = text_translate($chat['message'], '', $_REQUEST[$cookiePrefix . 'lang']); } if (!empty($_COOKIE[$cookiePrefix . 'lang'])) { $translated = text_translate($chat['message'], '', $_COOKIE[$cookiePrefix . 'lang']); } if ($translated != '') { if (!empty($_REQUEST['callbackfn']) && $_REQUEST['callbackfn'] == 'mobileapp') { $chat['message'] = strip_tags($translated) . ' (' . $chat['message'] . ')'; } else { $chat['message'] = strip_tags($translated) . ' <span class="untranslatedtext">(' . $chat['message'] . ')</span>'; } } } if (!empty($_REQUEST['callbackfn']) && $_REQUEST['callbackfn'] == 'mobileapp' && defined('TAPATALK')) { global $integration; $chat['message'] = $integration->hooks_processMessage($chat['message']); } $messages[$chromeReorderFix . $chat['id']] = array('id' => $chat['id'], 'from' => $chat['from'], 'message' => $chat['message'], 'self' => $self, 'old' => $old, 'sent' => $chat['sent']); if (empty($SESSION['cometchat']['cometchat_user' . $chat['from']][$chromeReorderFix . $chat['id']]['id'])) { $_SESSION['cometchat']['cometchat_user_' . $chat['from']][$chromeReorderFix . $chat['id']] = array('id' => $chat['id'], 'from' => $chat['from'], 'message' => $chat['message'], 'self' => $self, 'old' => 1, 'sent' => $chat['sent']); } $timestamp = $chat['id']; } if (!empty($messages) && (!empty($_REQUEST['callbackfn']) && ($_REQUEST['callbackfn'] == 'mobileapp' || $_REQUEST['callbackfn'] == 'mobilewebapp') && empty($_REQUEST['v']))) { $sql = "update cometchat set cometchat.read = '1' where cometchat.to = '" . mysqli_real_escape_string($GLOBALS['dbh'], $userid) . "' and cometchat.id <= '" . mysqli_real_escape_string($GLOBALS['dbh'], $timestamp) . "'"; $query = mysqli_query($GLOBALS['dbh'], $sql); if (defined('DEV_MODE') && DEV_MODE == '1') { echo mysqli_error($GLOBALS['dbh']); } } }
function heartbeat() { global $response; global $userid; global $chatrooms_language; global $chatroomTimeout; global $lastMessages; global $cookiePrefix; global $allowAvatar; global $moderatorUserIDs; global $guestsMode, $crguestsMode, $guestnamePrefix; global $chromeReorderFix; global $showChatroomUsers; $usertable = TABLE_PREFIX . DB_USERTABLE; $usertable_username = DB_USERTABLE_NAME; $usertable_userid = DB_USERTABLE_USERID; $time = getTimeStamp(); $chatroomList = array(); $crreadmessages = array(); $joinedrooms = array(); if (!isset($_REQUEST['force'])) { $force = 0; } else { $force = $_REQUEST['force']; } if (isset($_REQUEST['crreadmessages'])) { $crreadmessages = $_REQUEST['crreadmessages']; } if (!empty($_REQUEST['v']) && !empty($crreadmessages)) { $crreadmessages = json_decode($crreadmessages, true); } if (!empty($joinedrooms)) { foreach ($crreadmessages as $chatroomid => $unreadMessages) { if (!in_array($chatroomid, $joinedrooms)) { unset($crreadmessages[$chatroomid]); } } } if (empty($_SESSION['cometchat']['cometchat_lastlactivity']) || $time - $_SESSION['cometchat']['cometchat_lastlactivity'] >= REFRESH_BUDDYLIST / 4) { $sql = updateLastActivity($userid); if (function_exists('hooks_updateLastActivity')) { hooks_updateLastActivity($userid); } $query = mysqli_query($GLOBALS['dbh'], $sql); if (defined('DEV_MODE') && DEV_MODE == '1') { echo mysqli_error($GLOBALS['dbh']); } $_SESSION['cometchat']['cometchat_lastlactivity'] = $time; } if (empty($_SESSION['cometchat']['cometchat_chatroomslist']) || $force == 1 || !empty($_SESSION['cometchat']['cometchat_chatroomslist']) && $time - $_SESSION['cometchat']['cometchat_chatroomslist'] > REFRESH_BUDDYLIST) { if (!is_array($cachedChatrooms = getCache('chatroom_list')) || $force == 1) { $cachedChatrooms = array(); if ($showChatroomUsers == 1) { $sqlPart = "(SELECT count(cometchat_chatrooms_users.userid) online FROM cometchat_chatrooms_users where cometchat_chatrooms_users.chatroomid = cometchat_chatrooms.id and isbanned<>'1')"; } else { $sqlPart = '0'; } $sql = "select DISTINCT cometchat_chatrooms.id, cometchat_chatrooms.name, cometchat_chatrooms.type, cometchat_chatrooms.password, cometchat_chatrooms.lastactivity, cometchat_chatrooms.invitedusers, cometchat_chatrooms.createdby, " . $sqlPart . " online from cometchat_chatrooms order by name asc"; $query = mysqli_query($GLOBALS['dbh'], $sql); while ($chatroom = mysqli_fetch_assoc($query)) { $cachedChatrooms[$chromeReorderFix . $chatroom['id']] = array('id' => $chatroom['id'], 'name' => urldecode($chatroom['name']), 'online' => $chatroom['online'], 'type' => $chatroom['type'], 'password' => $chatroom['password'], 'lastactivity' => $chatroom['lastactivity'], 'createdby' => $chatroom['createdby'], 'invitedusers' => $chatroom['invitedusers']); } setCache('chatroom_list', $cachedChatrooms, 30); } foreach ($cachedChatrooms as $key => $chatroom) { if (($chatroom['createdby'] == 0 || $chatroom['createdby'] != 0 && $time - $chatroom['lastactivity'] < $chatroomTimeout || $chatroom['createdby'] == $userid) && $chatroom['type'] != 3) { $userList = explode(',', $chatroom['invitedusers']); $s = 0; if ($chatroom['createdby'] != $userid) { if (in_array($userid, $moderatorUserIDs)) { $s = 2; } } else { $s = 1; } if ($chatroom['type'] == 2 && !in_array($userid, $userList) && $chatroom['createdby'] != $userid) { continue; } else { $chatroomList[$chromeReorderFix . $chatroom['id']] = array('id' => $chatroom['id'], 'name' => $chatroom['name'], 'online' => $chatroom['online'], 'type' => $chatroom['type'], 'i' => $chatroom['password'], 's' => $s, 'createdby' => $chatroom['createdby']); } } } $_SESSION['cometchat']['cometchat_chatroomslist'] = $time; $clh = md5(serialize($chatroomList)); if (empty($_POST['clh']) || !empty($_POST['clh']) && $clh != $_POST['clh'] || $force == 1) { $response['chatrooms'] = $chatroomList; $response['clh'] = $clh; } } if (!empty($_REQUEST['initialize']) && $_REQUEST['initialize'] == '1') { unset($_SESSION['cometchat']['cometchat_joinedchatroomids']); } if ($force == 1 && empty($_SESSION['cometchat']['cometchat_joinedchatroomids'])) { $joinedChatroomIds = array(); $sql = "select DISTINCT cometchat_chatrooms.id from cometchat_chatrooms where cometchat_chatrooms.id IN (select cometchat_chatrooms_users.chatroomid from cometchat_chatrooms_users where cometchat_chatrooms_users.userid = '" . mysqli_real_escape_string($GLOBALS['dbh'], $userid) . "')"; $query = mysqli_query($GLOBALS['dbh'], $sql); if (defined('DEV_MODE') && DEV_MODE == '1') { echo mysqli_error($GLOBALS['dbh']); } while ($result = mysqli_fetch_assoc($query)) { $joinedChatroomIds[] = $result['id']; } $_SESSION['cometchat']['cometchat_joinedchatroomids'] = $joinedChatroomIds; } if (isset($_SESSION['cometchat']['cometchat_joinedchatroomids'])) { $joinedrooms = $_SESSION['cometchat']['cometchat_joinedchatroomids']; } if (!empty($_REQUEST['initialize']) && $_REQUEST['initialize'] == 1 && !empty($joinedrooms) || !empty($_POST['currentroom']) && $force == 1 && USE_COMET == 1 && COMET_CHATROOMS == 1) { $LastMessageIdList = array(); $implodedChatrooms = implode(',', $joinedrooms); $sql = "select max(cometchat_chatroommessages.id) id, cometchat_chatroommessages.chatroomid from cometchat_chatroommessages where cometchat_chatroommessages.chatroomid IN (" . mysqli_real_escape_string($GLOBALS['dbh'], $implodedChatrooms) . ") group by cometchat_chatroommessages.chatroomid"; $query = mysqli_query($GLOBALS['dbh'], $sql); while ($result = mysqli_fetch_assoc($query)) { $LastMessageIdList[$chromeReorderFix . $result['chatroomid']] = $result['id']; if (!isset($crreadmessages[$result['chatroomid']])) { $crreadmessages[$result['chatroomid']] = $result['id']; } } if (!empty($LastMessageIdList)) { $response['chatroomList'] = $LastMessageIdList; } if (USE_COMET == 1 && COMET_CHATROOMS == 1) { $cometresponse = array(); foreach ($joinedrooms as $key => $chatroomid) { $key = ''; if (defined('KEY_A') && defined('KEY_B') && defined('KEY_C')) { $key = KEY_A . KEY_B . KEY_C; } $cometresponsedata = array('chatroomid' => $chatroomid, 'cometid' => md5('chatroom_' . $chatroomid . $key), 'userid' => $userid); array_push($cometresponse, $cometresponsedata); } $response['subscribeChatrooms'] = $cometresponse; } } if (count($joinedrooms) > 0 || !empty($_REQUEST['callbackfn']) && $_REQUEST['callbackfn'] == 'mobileapp') { $messages = array(); $moremessages = array(); if (!is_array($users = getCache('chatrooms_users' . $_POST['currentroom'])) || $force == 1) { $users = array(); $sql = "select DISTINCT " . TABLE_PREFIX . DB_USERTABLE . "." . DB_USERTABLE_USERID . " userid, " . TABLE_PREFIX . DB_USERTABLE . "." . DB_USERTABLE_NAME . " username, " . DB_AVATARFIELD . " avatar, cometchat_status.lastactivity lastactivity, cometchat_status.isdevice isdevice, 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 = '" . mysqli_real_escape_string($GLOBALS['dbh'], $_POST['currentroom']) . "' and ('" . mysqli_real_escape_string($GLOBALS['dbh'], $time) . "' - cometchat_status.lastactivity < " . ONLINE_TIMEOUT . ") group by userid order by username asc"; if ($guestsMode && $crguestsMode) { $sql = getChatroomGuests($_POST['currentroom'], $time, $sql); } $query = mysqli_query($GLOBALS['dbh'], $sql); while ($chat = mysqli_fetch_assoc($query)) { if (function_exists('processName')) { $chat['username'] = processName($chat['username']); } $avatar = ''; if ($allowAvatar) { $avatar = getAvatar($chat['avatar']); } $users[$chromeReorderFix . $chat['userid']] = array('id' => (int) $chat['userid'], 'n' => $chat['username'], 'a' => $avatar, 'b' => $chat['isbanned']); } setCache('chatrooms_users' . $_POST['currentroom'], $users, 30); } if (empty($_SESSION['cometchat']['cometchat_chatroom_' . $_POST['currentroom']])) { $_SESSION['cometchat']['cometchat_chatroom_' . $_POST['currentroom']] = array(); } $ulh = md5(serialize($users)); if (empty($_POST['ulh']) || !empty($_POST['ulh']) && $ulh != $_POST['ulh']) { $response['ulh'] = $ulh; if (!empty($users)) { $response['users'] = $users; } } $limit = $lastMessages; if (!empty($crreadmessages)) { foreach ($joinedrooms as $key => $chatroomid) { if (!isset($crreadmessages[$chatroomid])) { $crreadmessages[$chatroomid] = 0; } } foreach ($crreadmessages as $key => $value) { if (!in_array($key, $joinedrooms)) { unset($crreadmessages[$key]); } } } if ($force == 1 && !empty($_SESSION['cometchat']['cometchat_chatroom_' . $_POST['currentroom']]) && !(USE_COMET == 1 && COMET_CHATROOMS == 1)) { $messages = getChatroomData($_POST['currentroom']); $messages = array_reverse($messages); } else { $guestpart = ""; $limitClause = " limit " . mysqli_real_escape_string($GLOBALS['dbh'], $limit) . " "; $timestampCondition = ""; foreach ($crreadmessages as $chatroomid => $lastmessageid) { if ((!empty($_REQUEST['initialize']) && $_REQUEST['initialize'] == 1 || !empty($lastmessageid) && $force == 1) && (USE_COMET == 1 && COMET_CHATROOMS == 1)) { $lastmessageid = $lastmessageid - $lastMessages; } if (!empty($_SESSION['cometchat']['chatrooms_' . $chatroomid . '_clearId']) && empty($_SESSION['cometchat']['cometchat_chatroom_' . $chatroomid])) { $timestampCondition .= " (cometchat_chatroommessages.chatroomid = '" . mysqli_real_escape_string($GLOBALS['dbh'], $chatroomid) . "' and cometchat_chatroommessages.id > '" . mysqli_real_escape_string($GLOBALS['dbh'], $_SESSION['cometchat']['chatrooms_' . $chatroomid . '_clearId']) . "') or"; } else { $timestampCondition .= " (cometchat_chatroommessages.chatroomid = '" . mysqli_real_escape_string($GLOBALS['dbh'], $chatroomid) . "' and cometchat_chatroommessages.id > '" . mysqli_real_escape_string($GLOBALS['dbh'], $lastmessageid) . "') or"; } } if (count($crreadmessages) > 0) { $timestampCondition = rtrim($timestampCondition, "or"); $timestampCondition = $timestampCondition . " and "; $limitClause = ''; } if (!empty($_REQUEST['callbackfn']) && $_REQUEST['callbackfn'] == 'mobileapp' && empty($_REQUEST['v']) || !empty($_REQUEST['currentroom'])) { $timestampCondition = ""; if ($_POST['timestamp'] != 0) { $timestampCondition = " cometchat_chatroommessages.chatroomid = '" . mysqli_real_escape_string($GLOBALS['dbh'], $_POST['currentroom']) . "' and cometchat_chatroommessages.id > '" . mysqli_real_escape_string($GLOBALS['dbh'], $_POST['timestamp']) . "' and "; $limitClause = ""; } else { $timestampCondition = "cometchat_chatroommessages.chatroomid = '" . mysqli_real_escape_string($GLOBALS['dbh'], $_POST['currentroom']) . "' and "; $limitClause = " limit " . mysqli_real_escape_string($GLOBALS['dbh'], $limit) . " "; } } if ($guestsMode && $crguestsMode) { $guestpart = " UNION select DISTINCT cometchat_chatroommessages.id id, cometchat_chatroommessages.message, cometchat_chatroommessages.chatroomid, 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 " . $timestampCondition . " cometchat_chatroommessages.message not like 'banned_%' and cometchat_chatroommessages.message not like 'kicked_%' and cometchat_chatroommessages.message not like 'deletemessage_%' "; } if (empty($crreadmessages) && empty($_POST['currentroom'])) { $sql = "select cometchat_chatroommessages.id id from cometchat_chatroommessages where false"; } else { $sql = "select DISTINCT cometchat_chatroommessages.id id, cometchat_chatroommessages.message, cometchat_chatroommessages.chatroomid, 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 " . $timestampCondition . " cometchat_chatroommessages.message not like 'banned_%' and cometchat_chatroommessages.message not like 'kicked_%' and cometchat_chatroommessages.message not like 'deletemessage_%' " . $guestpart . " order by id desc " . $limitClause; } $query = mysqli_query($GLOBALS['dbh'], $sql); if (mysqli_num_rows($query) > 0) { while ($chat = mysqli_fetch_assoc($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'], 'chatroomid' => $chat['chatroomid'], 'fromid' => $chat['fromid'], 'message' => $chat['message'], 'sent' => $chat['sent'])); $_SESSION['cometchat']['cometchat_chatroom_' . $chat['chatroomid']][$chat['id']] = array('id' => $chat['id'], 'chatroomid' => $chat['chatroomid'], 'from' => $chat['from'], 'fromid' => $chat['fromid'], 'message' => $chat['message'], 'sent' => $chat['sent']); } } } if (!empty($messages)) { $response['messages'] = $messages; } $sql = ''; if (!empty($_REQUEST['callbackfn']) && $_REQUEST['callbackfn'] == 'mobileapp' && !empty($_POST['currentroom'])) { $sql = "select password from cometchat_chatrooms where id = '" . mysqli_real_escape_string($GLOBALS['dbh'], $_POST['currentroom']) . "'"; } else { if (!empty($_COOKIE[$cookiePrefix . 'crstate'])) { $cr_state = json_decode($_COOKIE[$cookiePrefix . 'crstate'], true); if ($cr_state['open'] != '') { $sql = "select password from cometchat_chatrooms where id = '" . mysqli_real_escape_string($GLOBALS['dbh'], $cr_state['open']) . "'"; } } } if ($sql && $_POST['currentroom'] > 0) { $query = mysqli_query($GLOBALS['dbh'], $sql); if ($room = mysqli_fetch_assoc($query)) { if (!empty($room['password']) && (empty($_POST['currentp']) || $room['password'] != $_POST['currentp'])) { $response['users'] = array(); $response['messages'] = array(); } } else { $response['error'] = "ROOM_DOES_NOT_EXISTS"; } } } header('Content-type: application/json; charset=utf-8'); echo json_encode($response); }