Пример #1
0
function getChatboxData($id)
{
    global $messages;
    global $userid;
    global $chromeReorderFix;
    global $prependLimit;
    if (empty($_REQUEST['prepend'])) {
        if (USE_COMET == 1 && !empty($id)) {
            if (!empty($_SESSION['cometchat']['cometmessagesafter'])) {
                $limit = 10;
                $prelimit = ' limit ' . intval($limit);
                if (!empty($_REQUEST['callbackfn']) && $_REQUEST['callbackfn'] == 'mobileapp') {
                    $prelimit = ' limit 10';
                }
                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'];
                }
                $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 )) and cometchat.direction <> 3 order by cometchat.id desc " . mysqli_real_escape_string($GLOBALS['dbh'], $prelimit) . ";";
                $query = mysqli_query($GLOBALS['dbh'], $sql);
                while ($message = mysqli_fetch_assoc($query)) {
                    if ($message['from'] == $id && $message['sent'] >= intval($messagesafter / 1000)) {
                        $self = 0;
                        if ($message['from'] == $userid) {
                            $message['from'] = $message['to'];
                            $self = 1;
                        }
                        $moremessages[$chromeReorderFix . $message['id']] = array("id" => $message['id'], "from" => $message['from'], "message" => $message['message'], "self" => $self, "old" => 1, 'sent' => $message['sent'], 'direction' => $message['direction']);
                    }
                }
                if (!empty($id) && empty($_SESSION['cometchat']['cometchat_user_' . $id])) {
                    getPrevMessages($id);
                }
                $messages = array_merge($messages, $moremessages);
                usort($messages, "cmp");
            } else {
                if (!empty($id) && !empty($_SESSION['cometchat']['cometchat_user_' . $id])) {
                    $messages = array_merge($messages, $_SESSION['cometchat']['cometchat_user_' . $id]);
                }
            }
        } 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 {
        $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 = " . 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)) and (cometchat.id < {$prepend})  and cometchat.direction <> 3 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 )) and cometchat.direction <> 3 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'], 'direction' => $chat['direction']);
        }
        $messages = array_reverse($messages);
    }
}
Пример #2
0
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);
    }
}