<?php
session_start();
require_once('Banco_de_dados.php');
require_once('f_rp.php');

// Obtém o parâmetro "link" da URL
$codeFilePath = isset($_GET["link"]) ? htmlspecialchars($_GET["link"]) : null;

// Verifica se o parâmetro "link" está presente
if (empty($codeFilePath)) {
    header("HTTP/1.1 400 Bad Request");
    echo "Parâmetro 'link' ausente.";
    exit;
}

// Define o caminho do diretório onde os arquivos APK estão armazenados
$apkDirectory = 'admin/file/';

// Adiciona a extensão ".apk" se não estiver presente
if (pathinfo($codeFilePath, PATHINFO_EXTENSION) !== 'apk') {
    $codeFilePath .= '.apk';
}

$PIN = str_replace('.apk', '', $codeFilePath);

// Caminho completo do arquivo APK
$apkFilePath = $apkDirectory . $codeFilePath;

// Verifica se o arquivo APK existe
if (file_exists($apkFilePath)) {
    // Atualiza o banco de dados
    MaisUmDolwd($PIN, $conn);

    // Define o cabeçalho para forçar o download do arquivo
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename="' . basename($apkFilePath) . '"');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($apkFilePath));

    // Lê e envia o arquivo
    readfile($apkFilePath);
    exit;
} else {
    // O arquivo APK não existe
    header("HTTP/1.1 404 Not Found");
    echo "O arquivo APK não existe.";
    exit;
}
?>
