Skip to content

Instantly share code, notes, and snippets.

@ennjoy
Created May 9, 2019 15:08
Show Gist options
  • Save ennjoy/410672b7fb01c699283f3fe792177c65 to your computer and use it in GitHub Desktop.
Save ennjoy/410672b7fb01c699283f3fe792177c65 to your computer and use it in GitHub Desktop.
longpoll.php
<?php
$ts = intval($_GET['ts']);
$access_token = clean($_GET['access_token']);
$server_time = time();
$row_token = mysqli_fetch_assoc(mysqli_query($sqlConnect, "SELECT id, user_id, token FROM ". T_TOKENS ." WHERE token = '$access_token' LIMIT 1 "));
$row_ts = mysqli_fetch_assoc(mysqli_query($sqlConnect, "SELECT id, time FROM ". T_MESSAGES ." WHERE from_id = '$row_token[user_id]' AND deleted_one = '0' AND deleted_two = '0' ORDER BY time DESC "));
if (!$access_token) {
error(1, 'not found access_token');
} else if (!$ts) {
error(45, 'not found ts');
} else if ($access_token and $ts) {
if ($access_token == $row_token['token']) {
if ($ts == $row_ts['time']) {
while (true) {
$row_messages = mysqli_query($sqlConnect, "SELECT id, from_id, to_id, text, media, stickers, time, seen FROM ". T_MESSAGES ." WHERE time > '$server_time' AND to_id = '$row_token[user_id]' AND deleted_one = '0' AND deleted_two = '0' ");
$row_ts_new = mysqli_fetch_assoc(mysqli_query($sqlConnect, "SELECT id, time FROM ". T_MESSAGES ." WHERE from_id = '$row_token[user_id]' AND deleted_one = '0' AND deleted_two = '0' ORDER BY time DESC "));
if (mysqli_num_rows($row_messages)) {
while ($row = mysqli_fetch_array($row_messages)) {
$row_user = mysqli_fetch_assoc(mysqli_query($sqlConnect, "SELECT user_id, first_name, last_name FROM ". T_USERS ." WHERE user_id = '$row[from_id]' "));
if ($row['media']) {
$media = 'https://ivinete.ru/'.$row['media'];
} else {
$media = '';
}
$history[] = array($row['from_id'], $row['id'], $row['time']);
$result[] = array(
'id' => $row['id'],
'from_id' => $row['to_id'],
'peer_id' => $row['from_id'],
'text' => $row['text'],
'media' => $media,
'stickers' => $row['stickers'],
'time' => $row['time'],
'read_state' => $row['seen']
);
$profiles[] = array(
'id' => $row_user['user_id'],
'first_name' => $row_user['first_name'],
'last_name' => $row_user['last_name']
);
}
$response = array(
'response' => array(
'history' => $history,
'messages' => array(
'count' => $row_messages->num_rows,
'items' => $result
),
'profiles' => $profiles,
'new_ts' => $row_ts_new['time']
)
);
echo json_encode_cyr($response);
exit;
}
sleep(5);
}
} else {
error(46, 'ts is incorrect');
}
} else {
error(2, 'access_token is incorrect');
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment