<?php
function bot($method, $datas = [])
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://api.telegram.org/bot' . API_KEY . '/' . $method);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $datas);
    return json_decode(curl_exec($ch));
}
/*
نویسنده : 
@SeniorMehdy
اپن شده در :
@MahdyBots
اگر برای ناموست ارزش قائلی منبع بزن
*/
function PassWo(){
    $numerics   = 'QWERTYUIOPLKJHGFDSAZCXVBNM#$%^&*()+_-';
    $container = $numerics;
    $key = '';
    for($i = 0;$i < 10;$i++){
    $_rand = rand(0,strlen($container)-1);
    $key .= substr($container, $_rand, 1);
    }
    return $key;
    }
function is_join($id)
{
    global $Channels;
    $in_ch = [];
    foreach ($Channels as $ch_id)
    {
        $type = bot("getChatMember", ["chat_id" => "@$ch_id", "user_id" => $id]);
        $type = (is_object($type)) ? $type->result->status : $type['result']['status'];
        
        if ($type == 'creator' || $type == 'administrator' || $type == 'member')
        {
            $in_ch[$ch_id] = $type;
        }
        else
        {
            $keyboard = [];
            foreach ($Channels as $ch_id)
            {
                $ch_info = bot("getChat", ["chat_id" => "@$ch_id"]); //get channel
                $ch_name = (is_object($ch_info)) ? $ch_info->result->title : $ch_info['result']['title']; //channel name
                $keyboard[] = [['text' => $ch_name, 'url' => "https://t.me/$ch_id"]];
            }
            $keyboard[] = [['text' => '✅ تایید عضویت', 'callback_data' => 'join']];
            $text = 'لطفا در کانال های زیر عضو شده و سپس دکمه تایید عضویت را انتخاب کنید';
            bot('sendmessage', ['chat_id' => $id, 'text' => "$text", 'parse_mode' => 'MarkDown', 'reply_markup' => json_encode(['inline_keyboard' => $keyboard]) ]);
            break;
        }
    }
    return true;
}
/*
نویسنده : 
@SeniorMehdy
اپن شده در :
@MahdyBots
اگر برای ناموست ارزش قائلی منبع بزن
*/
function check_join($id)
{
    global $Channels;
    $in_ch = [];
    foreach ($Channels as $ch_id)
    {
        $type = bot("getChatMember", ["chat_id" => "@$ch_id", "user_id" => $id]);
        $type = (is_object($type)) ? $type->result->status : $type['result']['status'];
        if ($type == 'creator' || $type == 'administrator' || $type == 'member')
        {
            $in_ch[$ch_id] = $type;
        }
        else
        {
            return false;
            break;
        }
    }
    return true;
}
function convert($string)
{
    $persian = ['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹'];
    $arabic = ['٩', '٨', '٧', '٦', '٥', '٤', '٣', '٢', '١', '٠'];
    $num = range(0, 9);
    $convertedPersianNums = str_replace($persian, $num, $string);
    $englishNumbersOnly = str_replace($arabic, $num, $convertedPersianNums);
    return $englishNumbersOnly;
}
function Spam($from_id)
{
    global $connect;
    $spam_status = explode('|', mysqli_fetch_assoc($connect->query("SELECT * FROM `user` WHERE `id` = '$from_id' ")) ['spam']);
    if ($spam_status)
    {
        if (strpos($spam_status[0], "spam") !== false)
        {
            if ($spam_status[1] >= time()) exit(false);
            else $spam_status = [1, time() + 2];
        }
        elseif (time() < $spam_status[1])
        {
            if ($spam_status[0] + 1 > 4)
            {
                $time = time() + 300;
                $connect->query("UPDATE `user` SET `spam` = 'spam|$time' WHERE `id` = '$from_id'");
                bot('sendmessage', ['chat_id' => $from_id, 'text' => "جهت جلوگیری از اسپم ربات شما به مدت 5 دقیقه از ربات ما بلاک  شدید.⏳", ]);
                exit(false);
            }
            else $spam_status = [$spam_status[0] + 1, $spam_status[1]];
        }
        else $spam_status = [1, time() + 2];
        $connect->query("UPDATE `user` SET `spam` = '$spam_status[0]|$spam_status[1]' WHERE `id` = '$from_id'");
    }
    else
    {
        $time = time() + 2;
        $connect->query("UPDATE `user` SET `spam` = '0|$time' WHERE `id` = '$from_id'");
    }
}
/*
نویسنده : 
@SeniorMehdy
اپن شده در :
@MahdyBots
اگر برای ناموست ارزش قائلی منبع بزن
*/
 function files_get($url2) {
    $ch2 = curl_init();
    curl_setopt($ch2, CURLOPT_AUTOREFERER, TRUE);
    curl_setopt($ch2, CURLOPT_HEADER, 0);
    curl_setopt($ch2, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch2, CURLOPT_URL, $url2);
    curl_setopt($ch2, CURLOPT_FOLLOWLOCATION, TRUE);
    $data = curl_exec($ch2);
    curl_close($ch2);
    return $data;
}