-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmyInfo.php
113 lines (87 loc) · 3.65 KB
/
myInfo.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
/**
* Apache License 2.0
* @author Trapenok Victor, Levhav@ya.ru, 89244269357
* I will be glad to new orders for the development of anything.
*
* Levhav@ya.ru
* Skype:Levhav
* 89244269357
*
* https://github.com/Levhav/Star.Comet-Chat
*/
include './config.php';
include './common.php';
$user_id = getUserIdOrDie();
$contacts = array();
$contacts[] = $user_id;
$contacts_type = array();
$resInfo = array();
$selfInfo = false;
$qery = "select contacts.*, new_messages.count_new_msg as newMessages
from
(
SELECT users_relations.to_user_id, users_relations.type as relation_type, last_msg_time.time as last_message_time
FROM `users_relations`
left join (SELECT from_user_id, max(time) as time FROM `messages` where to_user_id = ".$user_id." group by from_user_id) as last_msg_time
on users_relations.to_user_id = last_msg_time.from_user_id
where users_relations.user_id = ".$user_id."
) as contacts
left join
(
SELECT from_user_id, count(*) as count_new_msg FROM `messages` where read_time = 0 and to_user_id = ".$user_id." group by from_user_id
) as new_messages
on contacts.to_user_id = new_messages.from_user_id";
$result = mysqli_query(StarCometChat::conf()->getDB(), $qery);
if(mysqli_errno(StarCometChat::conf()->getDB()) != 0)
{
echo "Error code:".mysqli_errno(StarCometChat::conf()->getDB())." ".mysqli_error(StarCometChat::conf()->getDB())."";
}
else if(mysqli_num_rows($result))
{
$countContacts = 0;
while($row = mysqli_fetch_assoc($result))
{
if($countContacts < 240)
{
// The restriction does not allow you to request online / offline statuses more than the last 240 contacts
$contacts[] = $row['to_user_id'];
$countContacts++;
}
$contacts_type[$row['to_user_id']] = $row;
}
}
$info = getUsersInfo($contacts);
if($info === false || $info === NULL)
{
echo json_encode(array("success"=>false, "error" => "You do not have the right to use chat, contact the administrator"));
exit();
}
// Getting user status from comets
$result = mysqli_query(StarCometChat::conf()->getComet(), "SELECT id, time FROM users_time WHERE id IN( ".join(",", $contacts).");");
while($row = mysqli_fetch_assoc($result))
{
$contacts_type[$row['id']]['last_online_time'] = $row['time'];
}
foreach ($info as $key => &$value)
{
if($user_id == $value['user_id'])
{
$selfInfo = array_merge($value, $contacts_type[$value['user_id']]);
continue;
}
$resInfo[$value['user_id']] = array_merge($value, $contacts_type[$value['user_id']]);
}
if(!is_array($selfInfo))
{
echo json_encode(array("success"=>false, "error" => "You do not have the right to use chat"));
exit();
}
$selfInfo["is_admin"] = in_array($user_id, StarCometChat::conf()->getAdminIds());
$selfInfo['login'] = preg_replace("/^.*?([^\/]*)$/usi", "$1", $selfInfo['login']);
if(!isset($selfInfo['error']))
{
$selfInfo['error'] = "";
}
// mysqli_query(StarCometChat::conf()->getDB(), "REPLACE into `users` (`id`, `login`, `avatar_url`, `error` )VALUES('". mysqli_real_escape_string(StarCometChat::conf()->getDB(), $selfInfo['user_id'])."', '". mysqli_real_escape_string(StarCometChat::conf()->getDB(), $selfInfo['login'])."', '". mysqli_real_escape_string(StarCometChat::conf()->getDB(), $selfInfo['avatar_url'])."', '". mysqli_real_escape_string(StarCometChat::conf()->getDB(), $selfInfo['error'])."')");
echo json_encode(array("success"=>true, "contacts" => $resInfo, "myInfo" => $selfInfo));