<?php
session_start();


header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST');
header("Access-Control-Allow-Headers: X-Requested-With");
header('Content-Type: application/json; charset=utf-8');

// Validate required parameters
if (empty($_POST['user_id']) || empty($_POST['auth'])) {
    echo json_encode([
        'status' => false,
        'success' => 'Missing required parameters',
    ]);
    exit;
}


include 'config.php';

function check_input($par) {
    $par = trim($par);
    $par = stripslashes($par);
    $par = htmlspecialchars($par);
    return $par;
}

$user_id = check_input($_POST['user_id']);
$auth = check_input($_POST['auth']);

$sql = "SELECT * FROM users WHERE (user_id = '$user_id' AND auth = '$auth')";
$result = $con->query($sql) or die("error: Server error " . mysqli_error($con));
$count = mysqli_num_rows($result);

if ($count == 1) {
    $rows = mysqli_fetch_array($result);
    $stat = $rows['sms_not2'];

    if ($stat == "ON") {
        $update = $con->query("UPDATE users SET sms_not2 = 'OFF' WHERE user_id = '$user_id' AND auth = '$auth'") or die("error: Server error " . mysqli_error($con));
       $suc = "Successful";
        $status = true;
    } 
    
    elseif ($stat == "OFF") {
     $update = $con->query("UPDATE users SET sms_not2 = 'ON' WHERE user_id = '$user_id' AND auth = '$auth'") or die("error: Server error " . mysqli_error($con));
       $suc = "Successful";
        $status = true;
        } 
        
      else {
        $update = $con->query("UPDATE users SET sms_not2 = 'ON' WHERE user_id = '$user_id' AND auth = '$auth'") or die("error: Server error " . mysqli_error($con));
        $suc = "Successful";
        $status = true;
     }
} 

$con->close();

$output = [
    'status' => $status,
    'success' => $suc,
];
echo json_encode($output);
?>
