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

header('Content-Type: application/json; charset=utf-8');

if (!validateCsrfToken()) {
    die(json_encode(['status'=>'error', 'msg'=>'Nieprawidłowy token bezpieczeństwa.']));
}

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

$hash = createShare($folderKey, $subPath, $fileName); // Unikalny identyfikator
if ($hash === false) {
    die(json_encode(['status'=>'error', 'msg'=>'Plik nie istnieje lub znajduje się poza dozwolonym folderem.']));
}

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

echo json_encode(['status'=>'ok', 'link' => $link]);
