forked from HowTommy/mycryptochat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
getMessages.php
69 lines (60 loc) · 2.09 KB
/
getMessages.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
require 'inc/conf.php';
require 'inc/constants.php';
require 'inc/init.php';
require 'inc/functions.php';
require 'inc/classes.php';
require 'inc/dbmanager.php';
$dbManager = new DbManager();
$chatRoom = $dbManager->GetChatroom($_POST['roomId']);
$dateLastNewMessage = $_POST['dateLastGetMessages'];
$nbIps = $_POST['nbIps'];
if(!is_null($chatRoom)) {
$userHash = getHashForIp();
$time = $_SERVER['REQUEST_TIME'];
if($chatRoom->dateEnd != 0 && $chatRoom->dateEnd <= $time) {
echo 'noRoom';
exit;
}
$currentUser = null;
foreach($chatRoom->users as $key => $user) {
if(!is_null($user)) {
if($user['id'] == $userHash) {
$currentUser = $user;
}
if(!$chatRoom->noMoreThanOneVisitor && $user['dateLastSeen'] + NB_SECONDS_USER_TO_BE_DISCONNECTED < $time) {
unset($chatRoom->users[$key]);
}
}
}
if(is_null($currentUser)) {
$currentUser = array();
$currentUser['id'] = $userHash;
$currentUser['dateLastSeen'] = $time;
array_push($chatRoom->users, $currentUser);
} else {
$currentUser['dateLastSeen'] = $time;
}
if($chatRoom->noMoreThanOneVisitor && count($chatRoom->users) > 2) {
$dbManager->DeleteChatroom($_POST['roomId']);
echo 'destroyed';
exit;
}
if($dateLastNewMessage < $chatRoom->dateLastNewMessage) {
$dbManager->UpdateChatRoomUsers($chatRoom);
$messages = $dbManager->GetLastMessages($chatRoom->id, NB_MESSAGES_TO_KEEP);
header('Content-Type: application/json');
echo '{ "dateLastGetMessages": ',$time,', "chatLines": ',json_encode($messages),', "nbIps": ',count($chatRoom->users),' }';
exit;
} else if ($nbIps != count($chatRoom->users)) {
$dbManager->UpdateChatRoomUsers($chatRoom);
header('Content-Type: application/json');
echo '{ "nbIps": ',count($chatRoom->users),' }';
exit;
} else {
echo 'noNew';
exit;
}
}
echo 'noRoom';
?>