<?php
error_reporting(0);
const ALLOWED_CHARACTERS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";


function getDecodedString($str) {
    $encryptKeyPosition = getEncryptKeyPosition(substr($str, -2, 1));
    $encryptKeyPosition2 = getEncryptKeyPosition(substr($str, -1));
    $substring = substr($str, 0, -2);
    return trim(utf8_decode(base64_decode(substr($substring, 0, $encryptKeyPosition) . substr($substring, $encryptKeyPosition + $encryptKeyPosition2))));
}

function getEncryptKeyPosition($str) {
    return strpos(ALLOWED_CHARACTERS, $str);
}

function getEncodedString($str) {
    $encryptKeyPosition = getEncryptKeyPosition(substr($str, -2, 1));
    $encryptKeyPosition2 = getEncryptKeyPosition(substr($str, -1));
    $encodedString = base64_encode(utf8_encode($str));
    $substring = substr($encodedString, 0, $encryptKeyPosition) . substr($encodedString, $encryptKeyPosition + $encryptKeyPosition2);
    return $substring . substr(ALLOWED_CHARACTERS, $encryptKeyPosition, 1) . substr(ALLOWED_CHARACTERS, $encryptKeyPosition2, 1);
}

function themes(){
    $db = new SQLite3('./.eggziedb.db');
    $themes_query = $db->query('SELECT * FROM theme');
    $themes = [];
    while ($theme_row = $themes_query->fetchArray()) {
        $themes[] = ['name' => $theme_row['name'], 'url' => $theme_row['url']];
    }
    $themes_json = json_encode($themes);
    return $themes_json;
}

function loadIbo(){
    $app_info_json = file_get_contents('./ibo.json');
    $app_info = json_decode($app_info_json, true);
    $android_version_code = $app_info['app_info']['android_version_code'];
    $apk_url = $app_info['app_info']['apk_url'];
    return array(
        'android_version_code' => $android_version_code,
        'apk_url' => $apk_url
    );
}

function lang(){
    $language_json = file_get_contents('./language.json');
    return $language_json;
}

function note($lists_expiring = []){
    $note_json = file_get_contents('./note.json');
    $note_data = json_decode($note_json, true);
    
    if (!empty($lists_expiring)) {
        $note_data['title'] = "⚠️ AVISO DE VENCIMENTO";
        $content = "Olá! Identificamos listas próximas do vencimento:\n\n";
        foreach ($lists_expiring as $list) {
            $content .= "• " . $list['name'] . ": vence em " . $list['days'] . " dias (" . $list['date'] . ")\n";
        }
        $content .= "\nEntre em contato para renovar e não perder o sinal!";
        $note_data['content'] = $content;
    }
    
    return json_encode($note_data);
}

function getUserData($mac_address){
    $db = new SQLite3('./.eggziedb.db');
    $mac_address = strtolower($mac_address);
    $ibo_query = $db->query('SELECT * FROM ibo WHERE LOWER(mac_address)="' . $mac_address . '"');
    $urls = [];
    $lists_expiring = [];
    $today = new DateTime();
    $app_expire_date = "2030-01-01"; // Default caso não tenha listas

    while ($ibo_row = $ibo_query->fetchArray()) {
        $urls[] = [
            'is_protected' => "1", 
            'id' => md5($ibo_row['password'] . $ibo_row['id']), 
            'url' => $ibo_row['url']."/get.php?username=".$ibo_row['username']."&password=".$ibo_row['password']."&type=m3u_plus&output=ts", 
            'name' => $ibo_row['title'], 
            'created_at' => '2023-04-15 00:06:09',
            'updated_at' => '2023-04-15 00:06:09'
        ];
        
        // Verificar vencimento desta lista específica
        if (!empty($ibo_row['expire_date'])) {
            $expiry = new DateTime($ibo_row['expire_date']);
            $interval = $today->diff($expiry);
            $days_left = (int)$interval->format('%r%a');
            
            if ($days_left <= 5 && $days_left >= 0) {
                $lists_expiring[] = [
                    'name' => $ibo_row['title'],
                    'days' => $days_left,
                    'date' => $ibo_row['expire_date']
                ];
            }
            
            // A data de expiração do APP será a da lista que vence mais longe (ou a padrão de 20 anos se você preferir)
            // Mas para garantir que o app não bloqueie, vamos manter uma data longa no retorno principal
            $app_expire_date = "2045-01-01"; 
        }
    }
    
    return [
        'urls' => json_encode($urls),
        'expire_date' => $app_expire_date,
        'expiring_info' => $lists_expiring
    ];
}

function escapeUrl($url) {
  return addcslashes($url, '/\\');
}

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $post_data = file_get_contents('php://input');
    $db = new SQLite3('./.eggziedb.db');
    
    $json_data = json_decode($post_data);
    $json_data = $json_data->data;
    $json_data = getDecodedString($json_data);
    $json_data = json_decode($json_data, true);
    $mac_address = getDecodedString($json_data['app_device_id']);
    $mac_address = chunk_split($mac_address, 2, ':');
    $mac_address = rtrim($mac_address, ':');
    
    $ibo_data = loadIbo();
    $result = getUserData($mac_address);
    $userData = $result['urls'];
    $expire_date = $result['expire_date'];
    
    // Gerar notificação baseada nas listas que estão vencendo
    $dynamic_note = note($result['expiring_info']);
    
    if($userData !== "[]"){
        $output_json = '{"android_version_code":"'.$ibo_data['android_version_code'].'","apk_url":"'.escapeUrl($ibo_data['app_url']).'","device_key":"136115","expire_date":"'.$expire_date.'","is_google_paid":true,"is_trial":0,"notification":'.$dynamic_note.',"urls":'.$userData.',"mac_registered":true,"themes":'.themes().',"trial_days":360,"plan_id":"03370629","mac_address":"'.$mac_address.'","pin":"0000","price":"0","app_version":"'.$ibo_data['android_version_code'].'","languages":[' . lang() . '],"apk_link":"'.escapeUrl($ibo_data['apk_url']).'"}'; 
    }else{
        $output_json = '{"android_version_code":"'.$ibo_data['android_version_code'].'","apk_url":"'.escapeUrl($ibo_data['app_url']).'","device_key":"136115","expire_date":"2030-01-01","is_google_paid":false,"is_trial":1,"notification":'.$dynamic_note.',"urls":[],"mac_registered":false,"themes":'.themes().',"trial_days":7,"plan_id":"","mac_address":"'.$mac_address.'","pin":"0000","price":"0","app_version":"'.$ibo_data['android_version_code'].'","languages":[' . lang() . '],"apk_link":"'.escapeUrl($ibo_data['apk_url']).'"}'; 
    }

    $output_json = '{"data":"'.getEncodedString($output_json).'"}';

    http_response_code(200);
    header('HTTP/1.1 200 OK');
    header('Content-Type: application/json');
    echo $output_json;
}
