2018-04-03 15:47:45 +02:00
|
|
|
<?php
|
|
|
|
$bot_api = require('api_key.php');
|
|
|
|
$chat_id = -1001180504638;
|
|
|
|
$mysql = require('mysql_credentials.php');
|
|
|
|
$conn = new mysqli($mysql['servername'], $mysql['username'], $mysql['password'], $mysql['database']);
|
|
|
|
if ($conn->connect_error) {
|
|
|
|
die("Connection failed: " . $conn->connect_error);
|
|
|
|
}
|
|
|
|
|
2018-04-03 16:32:23 +02:00
|
|
|
# Sends the given text wrapped in triple backticks as Markdown.
|
2018-04-04 10:17:29 +02:00
|
|
|
function send_text($post_message) {
|
2018-04-03 15:47:45 +02:00
|
|
|
global $bot_api;
|
|
|
|
global $chat_id;
|
|
|
|
$url = 'https://api.telegram.org/bot' . $bot_api . '/sendMessage';
|
2018-04-04 10:17:29 +02:00
|
|
|
$post_msg = array('chat_id' => $chat_id, 'text' => $post_message );
|
2018-04-03 15:47:45 +02:00
|
|
|
$options = array(
|
|
|
|
'http' => array(
|
|
|
|
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
|
|
|
|
'method' => 'POST',
|
|
|
|
'content' => http_build_query($post_msg)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$context = stream_context_create($options);
|
|
|
|
$result = file_get_contents($url, false, $context);
|
2018-04-05 07:51:50 +02:00
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2018-04-06 21:19:17 +02:00
|
|
|
# Pin Message
|
|
|
|
function pin_message($message_id) {
|
|
|
|
global $bot_api;
|
|
|
|
global $chat_id;
|
|
|
|
$url = 'https://api.telegram.org/bot' . $bot_api . '/pinChatMessage';
|
|
|
|
$post_msg = array('chat_id' => $chat_id, 'message_id' => $message_id, 'disable_notification' => 'true' );
|
|
|
|
$options = array(
|
|
|
|
'http' => array(
|
|
|
|
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
|
|
|
|
'method' => 'POST',
|
|
|
|
'content' => http_build_query($post_msg)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$context = stream_context_create($options);
|
|
|
|
$result = file_get_contents($url, false, $context);
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2018-04-03 16:32:23 +02:00
|
|
|
# Takes a user array as input. Checks who follows him/her and adds
|
|
|
|
# him/her to $output array. Exits when a user is followed by no one.
|
2018-04-03 15:47:45 +02:00
|
|
|
function get_chain_from_user($user) {
|
|
|
|
global $conn;
|
|
|
|
$output = array($user);
|
|
|
|
$last_user_id = $user['user_id'];
|
|
|
|
while (true) {
|
2018-04-04 10:08:31 +02:00
|
|
|
$query = "SELECT user_id, username from users where follows = $last_user_id ;";
|
2018-04-03 15:47:45 +02:00
|
|
|
$result = $conn->query($query);
|
2018-04-04 12:43:55 +02:00
|
|
|
if ($result->num_rows > 1) {
|
|
|
|
$text = "Chain is unstable. The following users are pointing to ";
|
|
|
|
$op = $conn->query("select username from users where user_id = $last_user_id;");
|
|
|
|
$text .= $op->fetch_assoc()['username'] . "\n";
|
|
|
|
while ($row = $result->fetch_assoc()) {
|
|
|
|
$text .= '@' . $row['username'] . "\n";
|
|
|
|
}
|
|
|
|
send_text($text);
|
|
|
|
mysql_data_seek($result, 0);
|
|
|
|
}
|
2018-04-03 15:47:45 +02:00
|
|
|
if ($result->num_rows > 0){
|
|
|
|
# Code executed if this isn't the last user
|
|
|
|
$details = $result->fetch_assoc();
|
|
|
|
array_push($output, $details );
|
|
|
|
$last_user_id = $details['user_id'];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
2018-04-03 16:32:23 +02:00
|
|
|
# First generates a list of people not following anyone (end_points)
|
|
|
|
# Then runs get_chain_from_user on all of them and stores them in
|
|
|
|
# $chains array. Then compares the arrays inside $chains array and
|
|
|
|
# returns the longest one.
|
|
|
|
function get_longest_chain() {
|
|
|
|
global $conn;
|
|
|
|
$query = "SELECT user_id, username FROM users WHERE follows = -1;";
|
|
|
|
$end_points = $conn->query($query);
|
|
|
|
if ($end_points->num_rows == 0) {
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
$chains = array();
|
|
|
|
while ($end_point = $end_points->fetch_assoc()){
|
2018-04-04 10:08:31 +02:00
|
|
|
$chain = get_chain_from_user($end_point);
|
2018-04-03 16:32:23 +02:00
|
|
|
array_push($chains, $chain);
|
|
|
|
}
|
|
|
|
$longest_chain_index = 0;
|
|
|
|
for ($i = 0; $i < count($chains); $i++){
|
|
|
|
if (count($chains[$i]) > count($chains[$longest_chain_index])) {
|
|
|
|
$longest_chain_index = $i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $chains[$longest_chain_index];
|
|
|
|
}
|
2018-04-03 16:45:12 +02:00
|
|
|
|
|
|
|
# Converts chain to string for sending
|
|
|
|
function chain_to_string($chain) {
|
|
|
|
$string = "";
|
|
|
|
for ($i = count($chain) - 1; $i >= 0 ; $i--) {
|
|
|
|
$string .= $chain[$i]['username'];
|
|
|
|
if ($i != 0) {
|
2018-07-06 11:32:09 +02:00
|
|
|
$string .= " → ";
|
2018-04-03 16:45:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $string;
|
|
|
|
}
|
|
|
|
|
2018-04-04 07:00:58 +02:00
|
|
|
function update_user_by_username($username) {
|
|
|
|
global $conn;
|
|
|
|
$html = file_get_contents("https://t.me/" . $username);
|
|
|
|
$dom = new domDocument;
|
|
|
|
$dom->loadHTML($html);
|
|
|
|
$dom->preserveWhiteSpace = false;
|
|
|
|
$xpath = new \DOMXPath($dom);
|
2018-04-05 12:47:52 +02:00
|
|
|
$changed = false;
|
2018-04-04 07:00:58 +02:00
|
|
|
foreach ($xpath->query("descendant-or-self::div[@class and contains(concat(' ', normalize-space(@class), ' '), ' tgme_page_description ')]/a") as $node){
|
2018-04-04 10:08:31 +02:00
|
|
|
$username_follows = preg_replace('/^@/', '', $node->nodeValue);
|
|
|
|
$query = "SELECT user_id FROM users WHERE username = '" . $username_follows . "';" ;
|
2018-04-04 07:00:58 +02:00
|
|
|
$result = $conn->query($query);
|
2018-04-04 10:08:31 +02:00
|
|
|
$query2 = "SELECT * FROM exceptions WHERE username = '" . $username . "' AND points_to = '" . $username_follows . "';";
|
|
|
|
$exceptions = $conn->query($query2);
|
|
|
|
if ($result->num_rows > 0 && $exceptions->num_rows == 0) {
|
2018-04-04 07:00:58 +02:00
|
|
|
$row = $result->fetch_assoc();
|
2018-04-04 10:08:31 +02:00
|
|
|
$query = "UPDATE users SET follows = " . $row['user_id'] . " WHERE username = '" . $username . "';" ;
|
2018-04-04 07:00:58 +02:00
|
|
|
$conn->query($query);
|
2018-04-05 12:47:52 +02:00
|
|
|
$changed = true;
|
2018-04-04 07:00:58 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2018-04-05 12:47:52 +02:00
|
|
|
if (!$changed) {
|
|
|
|
$query = "UPDATE users SET follows = -1 WHERE username = '" . $username . "';" ;
|
|
|
|
$conn->query($query);
|
|
|
|
}
|
2018-04-04 07:00:58 +02:00
|
|
|
}
|
2018-04-25 11:01:42 +02:00
|
|
|
if ($argv[1] == "all") {
|
|
|
|
$query = "SELECT user_id, username FROM users WHERE follows = -1;";
|
|
|
|
$end_points = $conn->query($query);
|
|
|
|
while ($end_point = $end_points->fetch_assoc()){
|
|
|
|
$chain = get_chain_from_user($end_point);
|
2018-07-06 11:32:09 +02:00
|
|
|
if (count($chain) == 1){
|
|
|
|
continue;
|
|
|
|
}
|
2018-04-25 11:01:42 +02:00
|
|
|
send_text(chain_to_string($chain));
|
|
|
|
}
|
|
|
|
exit();
|
|
|
|
}
|
2018-04-04 07:00:58 +02:00
|
|
|
|
2018-04-04 10:08:31 +02:00
|
|
|
# Update users
|
|
|
|
$query = "SELECT username FROM users;";
|
|
|
|
$users = $conn->query($query);
|
|
|
|
while ($user = $users->fetch_assoc()) {
|
|
|
|
update_user_by_username($user['username']);
|
|
|
|
}
|
|
|
|
|
|
|
|
# Get longest chain and compare it to old chain and send it.
|
|
|
|
$chain = get_longest_chain();
|
|
|
|
$chain_string = chain_to_string($chain);
|
|
|
|
$saved_chain = include('chain.php');
|
|
|
|
if ($saved_chain != $chain_string) {
|
2018-04-19 18:03:56 +02:00
|
|
|
$send_message = "Chain Length: " . count($chain) . "\n#Chain". count($chain) ."\n\n" . $chain_string;
|
2018-04-06 21:19:17 +02:00
|
|
|
$reply = send_text($send_message);
|
|
|
|
$json = json_decode($reply);
|
2018-04-06 21:23:59 +02:00
|
|
|
pin_message($json->{'result'}->{"message_id"});
|
2018-04-04 10:08:31 +02:00
|
|
|
$file = fopen('chain.php', 'w');
|
|
|
|
$contents = "<?php return '". $chain_string . "'; ?>";
|
|
|
|
fwrite($file, $contents);
|
|
|
|
fclose($file);
|
2018-04-04 17:24:10 +02:00
|
|
|
$file2 = fopen('lastmember.php', 'w');
|
2018-04-05 09:04:17 +02:00
|
|
|
$contents = "<?php return '" . $chain[count($chain) - 1]["username"] . "'; ?>";
|
2018-04-04 17:24:10 +02:00
|
|
|
fwrite($file2, $contents);
|
|
|
|
fclose($file2);
|
2018-04-04 10:08:31 +02:00
|
|
|
}
|
2018-04-09 18:16:41 +02:00
|
|
|
elseif ($argv[1] == "send") {
|
|
|
|
send_text("The pinned message is already the latest.");
|
|
|
|
}
|
2018-04-04 10:08:31 +02:00
|
|
|
|
|
|
|
|
2018-04-03 15:47:45 +02:00
|
|
|
$conn->close();
|
|
|
|
?>
|