mirror of
https://gitlab.com/ceda_ei/Zeeth-Naaw-Bot
synced 2025-03-12 16:26:52 +01:00
Added bug fixes for SQL queries. Added exceptions table in update_user_by_username function. Added main body, the file now queries t.me for every user to check the integrity of the chain. Then finds the longest chain, converts it to string and compares it to last sent chain. If the chains aren't same, it sends it and saves it in its place.
130 lines
4.1 KiB
PHP
130 lines
4.1 KiB
PHP
<?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);
|
|
}
|
|
|
|
# Sends the given text wrapped in triple backticks as Markdown.
|
|
function send_code($post_message) {
|
|
global $bot_api;
|
|
global $chat_id;
|
|
$url = 'https://api.telegram.org/bot' . $bot_api . '/sendMessage';
|
|
$post_msg = array('chat_id' => $chat_id, 'text' => '```\n ' . $post_message . '```', 'parse_mode' => 'markdown' );
|
|
$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);
|
|
}
|
|
|
|
# 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.
|
|
function get_chain_from_user($user) {
|
|
global $conn;
|
|
$output = array($user);
|
|
$last_user_id = $user['user_id'];
|
|
while (true) {
|
|
$query = "SELECT user_id, username from users where follows = $last_user_id ;";
|
|
$result = $conn->query($query);
|
|
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;
|
|
}
|
|
|
|
# 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()){
|
|
$chain = get_chain_from_user($end_point);
|
|
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];
|
|
}
|
|
|
|
# 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) {
|
|
$string .= " > ";
|
|
}
|
|
}
|
|
return $string;
|
|
}
|
|
|
|
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);
|
|
foreach ($xpath->query("descendant-or-self::div[@class and contains(concat(' ', normalize-space(@class), ' '), ' tgme_page_description ')]/a") as $node){
|
|
$username_follows = preg_replace('/^@/', '', $node->nodeValue);
|
|
$query = "SELECT user_id FROM users WHERE username = '" . $username_follows . "';" ;
|
|
$result = $conn->query($query);
|
|
$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) {
|
|
$row = $result->fetch_assoc();
|
|
$query = "UPDATE users SET follows = " . $row['user_id'] . " WHERE username = '" . $username . "';" ;
|
|
$conn->query($query);
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
# 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) {
|
|
send_code($chain_string);
|
|
$file = fopen('chain.php', 'w');
|
|
$contents = "<?php return '". $chain_string . "'; ?>";
|
|
fwrite($file, $contents);
|
|
fclose($file);
|
|
}
|
|
|
|
|
|
$conn->close();
|
|
?>
|