<?php

// --- Function bot ---
function bot($method, $datas = [])
{
    global $botToken;
    $url = "https://api.telegram.org/bot" . $botToken . "/" . $method;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    
    $hasFile = false;
    if (is_array($datas)) {
        foreach ($datas as $value) {
            if ($value instanceof CURLFile) {
                $hasFile = true;
                break;
            }
        }
    }
    
    if ($hasFile) {
        curl_setopt($ch, CURLOPT_POSTFIELDS, $datas);
    } else {
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($datas));
    }
    
    $res = curl_exec($ch);
    if (curl_error($ch)) {
        var_dump(curl_error($ch));
    }
    else {
        return json_decode($res);
    }
}

// --- Function sendMessage ---
function sendMessage($txt, $key = null, $parse = "MarkDown", $ci = null, $msg = null)
{
    global $from_id;
    $ci = $ci ?? $from_id;
    return bot('sendMessage', [
        'chat_id' => $ci,
        'text' => $txt,
        'reply_to_message_id' => $msg,
        'reply_markup' => $key,
        'parse_mode' => $parse
    ]);
}

// --- Function editKeys ---
function editKeys($keys = null, $msgId = null, $ci = null)
{
    global $from_id, $message_id;
    $ci = $ci ?? $from_id;
    $msgId = $msgId ?? $message_id;

    bot('editMessageReplyMarkup', [
        'chat_id' => $ci,
        'message_id' => $msgId,
        'reply_markup' => $keys
    ]);
}

// --- Function editText ---
function editText($msgId, $txt, $key = null, $parse = null, $ci = null)
{
    global $from_id;
    $ci = $ci ?? $from_id;

    return bot('editMessageText', [
        'chat_id' => $ci,
        'message_id' => $msgId,
        'text' => $txt,
        'parse_mode' => $parse,
        'reply_markup' => $key
    ]);
}

// --- Function delMessage ---
function delMessage($msg = null, $chat_id = null)
{
    global $from_id, $message_id;
    $msg = $msg ?? $message_id;
    $chat_id = $chat_id ?? $from_id;

    return bot('deleteMessage', [
        'chat_id' => $chat_id,
        'message_id' => $msg
    ]);
}

// --- Function sendAction ---
function sendAction($action, $ci = null)
{
    global $from_id;
    $ci = $ci ?? $from_id;

    return bot('sendChatAction', [
        'chat_id' => $ci,
        'action' => $action
    ]);
}

// --- Function forwardmessage ---
function forwardmessage($tochatId, $fromchatId, $message_id)
{
    return bot('forwardMessage', [
        'chat_id' => $tochatId,
        'from_chat_id' => $fromchatId,
        'message_id' => $message_id
    ]);
}

// --- Function sendPhoto ---
function sendPhoto($photo, $caption = null, $keyboard = null, $parse = "MarkDown", $ci = null)
{
    global $from_id;
    $ci = $ci ?? $from_id;
    if (is_string($photo) && file_exists($photo)) {
        $photo = new CURLFile($photo);
    }
    return bot('sendPhoto', [
        'chat_id' => $ci,
        'caption' => $caption,
        'reply_markup' => $keyboard,
        'photo' => $photo,
        'parse_mode' => $parse
    ]);
}

// --- Function getFileUrl ---
function getFileUrl($fileid)
{
    $res = bot('getFile', ['file_id' => $fileid]);
    $filePath = $res->result->file_path ?? null;
    if ($filePath) {
        global $botToken;
        return "https://api.telegram.org/file/bot" . $botToken . "/" . $filePath;
    }
    return null;
}

// --- Function alert ---
function alert($txt, $type = false, $callid = null)
{
    global $callbackId;
    $callid = $callid ?? $callbackId;
    return bot('answercallbackquery', [
        'callback_query_id' => $callid,
        'text' => $txt,
        'show_alert' => $type
    ]);
}

// --- Function check ---
function check($return = false)
{
    global $range;
    foreach ($range as $rg) {
        if (ip_in_range($_SERVER['REMOTE_ADDR'], $rg)) {
            return true;
        }
    }
    if ($return == true) {
        return false;
    }

    die('You do not have access');

}

// --- Function curl_get_file_contents ---
function curl_get_file_contents($URL)
{
    $c = curl_init();
    curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($c, CURLOPT_URL, $URL);
    $contents = curl_exec($c);
    curl_close($c);

    if ($contents)
        return $contents;
    else
        return FALSE;
}

// --- Function ip_in_range ---
function ip_in_range($ip, $range)
{
    if (strpos($range, '/') == false) {
        $range .= '/32';
    }
    // $range is in IP/CIDR format eg 127.0.0.1/24
    list($range, $netmask) = explode('/', $range, 2);
    $range_decimal = ip2long($range);
    $ip_decimal = ip2long($ip);
    $wildcard_decimal = pow(2, (32 - $netmask)) - 1;
    $netmask_decimal = ~$wildcard_decimal;
    return (($ip_decimal& $netmask_decimal) == ($range_decimal& $netmask_decimal));
}
