<?php
error_reporting(1);

$param = key($_GET);

$json = file_get_contents('php://input');
$data = json_decode($json, true); 
$id_app = $data['variables']['id'];
$sc = $_POST['sc'] ?? '123456789789456123321456987';
$salt = 'NB!@#12ZKWd';
$rand = $_POST['r'];

switch ($param) {
    case 'one':
        android();
        break;
    case 'tow':
        And09123jfi();
        break;
    case 'dns':
        dns($salt,$rand);
        break;
    default:
        main($id_app);
        break;
}


function android(){
    header('Content-Type: application/json');
    echo json_encode(["status" => false]);

}

function And09123jfi(){
    echo 'Hi!!! How are you?';
}

function dns($st,$rd){
$db = new SQLite3('./.db.db');
$res = $db->query('SELECT * FROM dns'); 
$rows = array();

while ($row = $res->fetchArray(SQLITE3_ASSOC)) {
    $rows[] = $row['url'];
}
$dns = rtrim(implode(',', $rows), ',');
$arry = str_replace("\\", "", "$dns");
$response = [
    "rtx" => true,
    "status" => true,
    "su" => $dns,
    "sc" => generateCustomString().md5($arry."*".$st."*".$rd),
    "ndd" => "",
    "download_enable" => true
];

$db->close();

$resultout = json_encode($response);
echo $resultout;
}

function generateCustomString() {
    // Step 1: Generate 3 numbers that sum to 13
    do {
        $a = rand(0, 13);
        $b = rand(0, 13 - $a);
        $c = 13 - $a - $b;
        $numbers = [$a, $b, $c];
    } while ($a > 9 || $b > 9 || $c > 9); // ensure single-digit numbers

    shuffle($numbers); // randomize digit order

    // Step 2: Generate 7 random lowercase letters
    $letters = [];
    for ($i = 0; $i < 7; $i++) {
        $letters[] = chr(rand(97, 122)); // 'a' to 'z'
    }

    // Step 3: Insert the digits at random positions between letters
    foreach ($numbers as $digit) {
        $pos = rand(0, count($letters));
        array_splice($letters, $pos, 0, strval($digit));
    }

    return implode('', $letters);
}




function main($id){
$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https://' : 'http://';
$domain = $_SERVER['HTTP_HOST'];
$path = dirname($_SERVER['SCRIPT_NAME']);
$current_url = $protocol . $domain . $path;
$data = [
    'data' => [
        'getSmartersProAll' => [
            '__typename' => 'SmartersProAll',
            'id' => $id,
            'basesbpurl' => $current_url.'/api.php',
            'securityurl' => $current_url.'/discover.php?dns',
            'clientsbpurl' => $current_url.'/api.php',
            'config1' => $current_url.'/discover.php?one',
            'config2' => 'm0BK3W4zYVL7uvx',
            'config3' => 'dp2ZcuL83mPT4AIRhMsnWGE9JkgfQ1lrKNzHa7xi5C0vFUBqeV',
            'config4' => $current_url.'/discover.php?tow',
            'config5' => '',
            'createdAt' => '2023-12-06T12:23:55.846Z',
            'updatedAt' => '2023-12-06T12:23:55.846Z'
        ]
    ]
];

header('Content-Type: application/json');
echo json_encode($data, JSON_PRETTY_PRINT); 
}


?>