archive-ccc8458/video/api/delete.php 1.6 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
<?php
declare(strict_types=1);
header('Content-Type: application/json');

require_once __DIR__ . '/../lib/auth.php';
require_once __DIR__ . '/../lib/hash.php';

require_https();

if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    http_response_code(405);
    echo json_encode(['error' => 'method not allowed']);
    exit;
}

if (!rate_limit_check()) {
    http_response_code(429);
    echo json_encode(['error' => 'too many failed attempts, try again later']);
    exit;
}

$pw = password_from_request();
if ($pw === null || $pw === '' || !verify_password($pw)) {
    rate_limit_fail();
    http_response_code(401);
    echo json_encode(['error' => 'invalid password']);
    exit;
}
rate_limit_reset();

// Accept hash from form field or JSON body.
$hash = (string)($_POST['hash'] ?? '');
if ($hash === '' && (str_starts_with($_SERVER['CONTENT_TYPE'] ?? '', 'application/json'))) {
    $body = json_decode((string) file_get_contents('php://input'), true);
    if (is_array($body)) $hash = (string)($body['hash'] ?? '');
}

if (!is_valid_hash($hash)) {
    http_response_code(400);
    echo json_encode(['error' => 'invalid or missing hash']);
    exit;
}

$path = find_video_file($hash);
if ($path === null) {
    http_response_code(404);
    echo json_encode(['error' => 'not found']);
    exit;
}

if (!@unlink($path)) {
    http_response_code(500);
    echo json_encode(['error' => 'could not delete file']);
    exit;
}
// Best-effort: also remove a sibling thumbnail and unlisted marker if present.
$thumb = find_thumb_file($hash);
if ($thumb !== null) @unlink($thumb);
set_unlisted($hash, false);

echo json_encode(['deleted' => $hash]);