public function get()
 {
     $result = array("error" => true, "error_code" => ERROR_ACCOUNT_ID);
     $stmt = $this->db->prepare("SELECT * FROM users WHERE id = (:id) LIMIT 1");
     $stmt->bindParam(":id", $this->id, PDO::PARAM_INT);
     if ($stmt->execute()) {
         if ($stmt->rowCount() > 0) {
             $row = $stmt->fetch();
             $notifications_count = 0;
             $messages_count = 0;
             $guests_count = 0;
             $friends_count = 0;
             // Get new messages count
             $msg = new messages($this->db);
             $msg->setRequestFrom($this->id);
             $messages_count = $msg->getNewMessagesCount();
             unset($msg);
             // Get new notifications count
             $notifications = new notify($this->db);
             $notifications->setRequestFrom($this->id);
             $notifications_count = $notifications->getNewCount($row['last_notify_view']);
             unset($notifications);
             // Get new guests count
             $guests = new guests($this->db, $this->id);
             $guests->setRequestFrom($this->id);
             $guests_count = $guests->getNewCount($row['last_guests_view']);
             unset($guests);
             // Get new friends count
             $friends = new friends($this->db, $this->id);
             $friends->setRequestFrom($this->id);
             $friends_count = $friends->getNewCount($row['last_friends_view']);
             unset($friends);
             $profile = new profile($this->db, $row['id']);
             $online = false;
             $current_time = time();
             if ($row['last_authorize'] != 0 && $row['last_authorize'] > $current_time - 15 * 60) {
                 $online = true;
             }
             $time = new language($this->db);
             $result = array("error" => false, "error_code" => ERROR_SUCCESS, "id" => $row['id'], "gcm_regid" => $row['gcm_regid'], "admob" => $row['admob'], "ghost" => $row['ghost'], "vip" => $row['vip'], "gcm" => $row['gcm'], "balance" => $row['balance'], "fb_id" => $row['fb_id'], "rating" => $row['rating'], "state" => $row['state'], "regtime" => $row['regtime'], "ip_addr" => $row['ip_addr'], "username" => $row['login'], "fullname" => stripcslashes($row['fullname']), "location" => stripcslashes($row['country']), "status" => stripcslashes($row['status']), "fb_page" => stripcslashes($row['fb_page']), "instagram_page" => stripcslashes($row['my_page']), "verify" => $row['verify'], "email" => $row['email'], "emailVerify" => $row['emailVerify'], "sex" => $row['sex'], "year" => $row['bYear'], "month" => $row['bMonth'], "day" => $row['bDay'], "lat" => $row['lat'], "lng" => $row['lng'], "language" => $row['language'], "lowPhotoUrl" => $row['lowPhotoUrl'], "normalPhotoUrl" => $row['normalPhotoUrl'], "bigPhotoUrl" => $row['normalPhotoUrl'], "coverUrl" => $row['normalCoverUrl'], "originCoverUrl" => $row['originCoverUrl'], "iStatus" => $row['iStatus'], "iPoliticalViews" => $row['iPoliticalViews'], "iWorldView" => $row['iWorldView'], "iPersonalPriority" => $row['iPersonalPriority'], "iImportantInOthers" => $row['iImportantInOthers'], "iSmokingViews" => $row['iSmokingViews'], "iAlcoholViews" => $row['iAlcoholViews'], "iLooking" => $row['iLooking'], "iInterested" => $row['iInterested'], "allowComments" => $row['allowComments'], "allowMessages" => $row['allowMessages'], "allowLikesGCM" => $row['allowLikesGCM'], "allowGiftsGCM" => $row['allowGiftsGCM'], "allowCommentsGCM" => $row['allowCommentsGCM'], "allowFollowersGCM" => $row['allowFollowersGCM'], "allowMessagesGCM" => $row['allowMessagesGCM'], "allowCommentReplyGCM" => $row['allowCommentReplyGCM'], "lastAuthorize" => $row['last_authorize'], "lastAuthorizeDate" => date("Y-m-d H:i:s", $row['last_authorize']), "lastAuthorizeTimeAgo" => $time->timeAgo($row['last_authorize']), "online" => $online, "friendsCount" => $row['friends_count'], "photosCount" => $row['photos_count'], "likesCount" => $row['likes_count'], "giftsCount" => $row['gifts_count'], "notificationsCount" => $notifications_count, "guestsCount" => $guests_count, "newFriendsCount" => $friends_count, "messagesCount" => $messages_count);
             unset($profile);
             unset($time);
         }
     }
     return $result;
 }
 */
include_once $_SERVER['DOCUMENT_ROOT'] . "/core/init.inc.php";
include_once $_SERVER['DOCUMENT_ROOT'] . "/config/api.inc.php";
if (!empty($_POST)) {
    $clientId = isset($_POST['clientId']) ? $_POST['clientId'] : 0;
    $accountId = isset($_POST['accountId']) ? $_POST['accountId'] : '';
    $accessToken = isset($_POST['accessToken']) ? $_POST['accessToken'] : '';
    $profileId = isset($_POST['profileId']) ? $_POST['profileId'] : '';
    $chatId = isset($_POST['chatId']) ? $_POST['chatId'] : 0;
    $msgId = isset($_POST['msgId']) ? $_POST['msgId'] : 0;
    $clientId = helper::clearInt($clientId);
    $accountId = helper::clearInt($accountId);
    $profileId = helper::clearInt($profileId);
    $chatId = helper::clearInt($chatId);
    $msgId = helper::clearInt($msgId);
    $result = array("error" => true, "error_code" => ERROR_UNKNOWN);
    $auth = new auth($dbo);
    if (!$auth->authorize($accountId, $accessToken)) {
        api::printError(ERROR_ACCESS_TOKEN, "Error authorization.");
    }
    $messages = new messages($dbo);
    $messages->setRequestFrom($accountId);
    if ($chatId == 0) {
        $chatId = $messages->getChatId($accountId, $profileId);
    }
    if ($chatId != 0) {
        $result = $messages->getPreviousMessages($chatId, $msgId);
    }
    echo json_encode($result);
    exit;
}
 public function get($chatId, $msgId = 0)
 {
     if ($msgId == 0) {
         $msgId = $this->getMaxMessageId();
         $msgId++;
     }
     $messages = array("error" => false, "error_code" => ERROR_SUCCESS, "chatId" => $chatId, "messagesCount" => $this->messagesCountByChat($chatId), "msgId" => $msgId, "newMessagesCount" => 0, "messages" => array());
     $stmt = $this->db->prepare("SELECT id FROM messages WHERE chatId = (:chatId) AND id < (:msgId) AND removeAt = 0 ORDER BY id DESC LIMIT 20");
     $stmt->bindParam(':chatId', $chatId, PDO::PARAM_INT);
     $stmt->bindParam(':msgId', $msgId, PDO::PARAM_INT);
     if ($stmt->execute()) {
         while ($row = $stmt->fetch()) {
             $msgInfo = $this->info($row['id']);
             array_push($messages['messages'], $msgInfo);
             $messages['msgId'] = $msgInfo['id'];
             unset($msgInfo);
         }
         $chatInfo = $this->chatInfo($chatId);
         $profileId = $chatInfo['fromUserId'];
         if ($profileId == $this->getRequestFrom()) {
             $this->setChatLastView_FromId($chatId);
         } else {
             $this->setChatLastView_ToId($chatId);
         }
     }
     $messages_count = 0;
     $msg = new messages($this->db);
     $msg->setRequestFrom($this->getRequestFrom());
     $messages_count = $msg->getNewMessagesCount();
     unset($msg);
     $messages['newMessagesCount'] = $messages_count;
     return $messages;
 }