From fa39901f8e684b8436a35f9445b0707a233f58dd Mon Sep 17 00:00:00 2001 From: Ceda EI Date: Tue, 3 Apr 2018 19:17:45 +0530 Subject: [PATCH] Add update_chain.php and mysql_credentials.php.sample Added send_code, get_chain_from_user functions. The get_chain_from_user generates a chain from a given user and returns an array in reverse sequential order. --- mysql_credentials.php.sample | 8 ++++++ update_chain.php | 56 ++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 mysql_credentials.php.sample create mode 100644 update_chain.php diff --git a/mysql_credentials.php.sample b/mysql_credentials.php.sample new file mode 100644 index 0000000..a88b072 --- /dev/null +++ b/mysql_credentials.php.sample @@ -0,0 +1,8 @@ + "localhost", + "username" => "username_goes_here", + "password" => "password_goes_here", + "database" => "database_name_goes_here" +); +?> diff --git a/update_chain.php b/update_chain.php new file mode 100644 index 0000000..50d118a --- /dev/null +++ b/update_chain.php @@ -0,0 +1,56 @@ +connect_error) { + die("Connection failed: " . $conn->connect_error); +} + +function send_code($post_message, $reply=false) { + global $decoded; + 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' ); + if ($reply != false) { + if ($reply === true){ + $post_msg['reply_to_message_id'] = $decoded->{'message'}->{'message_id'}; + } + else { + $post_msg['reply_to_message_id'] = $reply; + } + } + $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); +} + +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; +} + +$conn->close(); +?>