<?php
require_once 'functions.php';
checkAuth();

$folderKey = urldecode($_POST['folder'] ?? '');
$subPath = urldecode($_POST['path'] ?? '');
$fileName = urldecode($_POST['file'] ?? '');

$hash = bin2hex(random_bytes(6)); // Unikalny identyfikator np. a1b2c3d4e5f6
$db = getDb();
$db['shares'][$hash] = [
    'folder' => $folderKey,
    'path' => $subPath,
    'file' => $fileName,
    'expires' => time() + (24 * 3600) // Wygasa po 24h
];
saveDb($db);

$scheme = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on') ? "https" : "http";
$link = $scheme . "://" . $_SERVER['HTTP_HOST'] . rtrim(dirname($_SERVER['PHP_SELF']), '/') . "/share.php?h=" . $hash;

header('Content-Type: application/json');
echo json_encode(['link' => $link]);
