Ejemplo n.º 1
0
<?php

require_once 'app/autoload.php';
// Checks if is logged user
if (isset($_SESSION['chat_login']['id_user'])) {
    // Return
    $return = '';
    // Disables users stranded
    UsersModel::userDeleteIdle();
    // Condition
    $condition = '(to_user = 0 OR id_user = '******'chat_login']['id_user'] . ' ';
    $condition .= 'OR to_user = '******'chat_login']['id_user'] . ' ';
    $condition .= 'OR (to_user != ' . $_SESSION['chat_login']['id_user'] . ' AND reserved = 0) ';
    $condition .= 'OR (id_user = 0 AND to_user = 0)) ';
    // Load messages
    $messages = new MessagesModel();
    $messages->setCond($condition);
    $messages->setCond('id_room = ' . $_SESSION['chat_login']['id_room']);
    $messages->setCond('timestamp >= "' . date('Y-m-d H:i:s', $_SESSION['chat_login']['last']) . '"');
    $messages->setOrderBy('timestamp ASC');
    $load = $messages->load_all();
    $_SESSION['chat_login']['last'] = $_SERVER['REQUEST_TIME'];
    $_SESSION['chat_login']['messages'] = isset($_SESSION['chat_login']['messages']) ? $_SESSION['chat_login']['messages'] : array();
    $_SESSION['chat_login']['last_message'] = isset($_SESSION['chat_login']['last_message']) ? $_SESSION['chat_login']['last_message'] : $_SERVER['REQUEST_TIME'];
    foreach ($load as $line) {
        if (in_array($line['id'], $_SESSION['chat_login']['messages'])) {
            continue;
        } else {
            $_SESSION['chat_login']['messages'][] = $line['id'];
            if ($_SERVER['REQUEST_TIME'] > $_SESSION['chat_login']['last_message'] + 120) {
                $_SESSION['chat_login']['last_message'] = $_SERVER['REQUEST_TIME'];