Example #1
0
         getLastTimestamp();
     }
     if (defined('DISABLE_ISTYPING') && DISABLE_ISTYPING != 1 && $processFurther) {
         typingTo();
     }
     if (defined('DISABLE_ANNOUNCEMENTS') && DISABLE_ANNOUNCEMENTS != 1 && $processFurther) {
         checkAnnoucements();
     }
     if ($processFurther) {
         fetchMessages();
     }
 }
 $time = getTimeStamp();
 if ($processFurther) {
     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($_REQUEST['typingto']) && $_REQUEST['typingto'] != 0 && DISABLE_ISTYPING != 1) {
         $sql = "insert into cometchat_status (userid,typingto,typingtime) values ('" . mysqli_real_escape_string($GLOBALS['dbh'], $userid) . "','" . mysqli_real_escape_string($GLOBALS['dbh'], $_REQUEST['typingto']) . "','" . getTimeStamp() . "') on duplicate key update typingto = '" . mysqli_real_escape_string($GLOBALS['dbh'], $_REQUEST['typingto']) . "', typingtime = '" . getTimeStamp() . "'";
         $query = mysqli_query($GLOBALS['dbh'], $sql);
         if (defined('DEV_MODE') && DEV_MODE == '1') {
             echo mysqli_error($GLOBALS['dbh']);
         }
Example #2
0
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);
}