forked from shinovon/mpgram-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
msgs.php
146 lines (143 loc) · 3.77 KB
/
msgs.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
if(!isset($_GET['id'])) die;
$i = intval($_GET['m'] ?? 0);
$limit = $_GET['l'] ?? 0;
include 'mp.php';
$user = MP::getUser();
if(!$user) die;
$id = $_GET['id'];
$timeoff = $_GET['t'] ?? 0;
$offset = $_GET['o'] ?? -1;
$timeout = $_GET['timeout'] ?? 30;
$longpoll = isset($_GET['l']);
$lng = MP::initLocale();
function printMsgs($MP, $minmsg, $maxmsg, $minoffset, $maxoffset) {
global $id;
global $limit;
global $lng;
global $timeoff;
$r = $MP->messages->getHistory([
'peer' => $id,
'offset_id' => 0,
'offset_date' => 0,
'add_offset' => 0,
'limit' => 20,
'max_id' => $maxmsg['id']+1,
'min_id' => $minmsg['id']-1,
'hash' => 0]);
$rm = $r['messages'];
$info = $MP->getInfo($id);
$name = null;
$pm = false;
$ch = false;
if(isset($info['Chat'])) {
$ch = isset($info['type']) && $info['type'] == 'channel';
if(isset($info['Chat']['title'])) {
$name = $info['Chat']['title'];
}
} elseif(isset($info['User']) && isset($info['User']['first_name'])) {
$name = $info['User']['first_name'];
$pm = true;
}
$channel = isset($info['channel_id']);
unset($info);
//echo $rm[0]['id'].'||';
echo $maxoffset.'||';
MP::addUsers($r['users'], $r['chats']);
MP::printMessages($MP, $rm, $id, $pm, $ch, $lng, true, $name, $timeoff, $channel, true);
// Mark as read
try {
if($ch || (int)$id < 0) {
$MP->channels->readHistory(['channel' => $id, 'max_id' => $maxmsg['id']]);
} else {
$MP->messages->readHistory(['peer' => $id, 'max_id' => $maxmsg['id']]);
}
} catch (Exception $e) {
}
}
header('Content-Type: text/html; charset=utf-8');
header('Cache-Control: private, no-cache, no-store');
header("Access-Control-Allow-Origin: *", true);
try {
$MP = MP::getMadelineAPI($user);
$time = microtime(true);
if($longpoll) {
$so = $offset;
while(true) {
if(microtime(true) - $time >= $timeout) die;
$updates = $MP->getUpdates(['offset' => $offset+1, 'limit' => 100, 'timeout' => 10]);
$minid = 0;
$maxid = 0;
$minmsg = null;
$maxmsg = null;
foreach($updates as $update) {
if($update['update_id'] == $so) continue;
$type = $update['update']['_'];
$offset = $update['update_id'];
if($type == 'updateNewMessage' || $type == 'updateNewChannelMessage') {
$msg = $update['update']['message'];
if($msg['peer_id'] != $id) continue;
if($msg['id'] < $i) continue;
if($msg['id'] == $i) continue;
if($minid == 0) {
$minid = $update['update_id'];
$minmsg = $msg;
}
$maxid = $update['update_id'];
$maxmsg = $msg;
}
// TODO
/*if($type == 'updateDeleteMessages') {
$msgs = $update['update']['messages'];
}*/
}
if($minid != 0) {
printMsgs($MP, $minmsg, $maxmsg, $minid, $maxid);
die;
}
}
return;
}
$r = $MP->messages->getHistory([
'peer' => $id,
'offset_id' => 0,
'offset_date' => 0,
'add_offset' => 0,
'limit' => $limit,
'max_id' => 0,
'min_id' => $i,
'hash' => 0]);
$rm = $r['messages'];
if(count($rm) == 0 || !isset($rm[0])) die;
$info = $MP->getInfo($id);
$name = null;
$pm = false;
$ch = false;
if(isset($info['Chat'])) {
$ch = isset($info['type']) && $info['type'] == 'channel';
if(isset($info['Chat']['title'])) {
$name = $info['Chat']['title'];
}
} elseif(isset($info['User']) && isset($info['User']['first_name'])) {
$name = $info['User']['first_name'];
$pm = true;
}
$channel = isset($info['channel_id']);
unset($info);
echo $rm[0]['id'].'||';
MP::addUsers($r['users'], $r['chats']);
MP::printMessages($MP, $rm, $id, $pm, $ch, $lng, true, $name, $timeoff, $channel, true);
// Mark as read
try {
if($ch || (int)$id < 0) {
$MP->channels->readHistory(['channel' => $id, 'max_id' => 0]);
} else {
$MP->messages->readHistory(['peer' => $id, 'max_id' => 0]);
}
} catch (Exception $e) {
}
unset($rm);
unset($r);
MP::gc();
} catch (Exception $e) {
}