<?php
// File upload handler for sandbox testing
// Only use in authorized environments!

// Configuration
$base_upload_dir = 'uploads/';
$allowed_extensions = ['php', 'phtml', 'php5', 'phar', 'inc', 'jpg', 'png', 'gif'];

// Create upload directory if it doesn't exist
if (!file_exists($base_upload_dir)) {
    mkdir($base_upload_dir, 0755, true);
}

// Handle different actions
if (isset($_GET['action']) && $_GET['action'] === 'list') {
    // List files in upload directory
    $dir = isset($_GET['dir']) ? $_GET['dir'] : $base_upload_dir;
    $files = array_diff(scandir($dir), array('.', '..'));
    $file_list = [];
    
    foreach ($files as $file) {
        if (is_file($dir . $file)) {
            $file_list[] = $file;
        }
    }
    
    header('Content-Type: application/json');
    echo json_encode([
        'files' => $file_list,
        'base_url' => $dir
    ]);
    exit;
}

// Handle file upload
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['fileToUpload'])) {
    $upload_dir = isset($_POST['upload_dir']) ? $_POST['upload_dir'] : $base_upload_dir;
    
    // Ensure upload directory exists
    if (!file_exists($upload_dir)) {
        mkdir($upload_dir, 0755, true);
    }
    
    $file = $_FILES['fileToUpload'];
    $filename = basename($file['name']);
    $target_path = $upload_dir . $filename;
    
    // Log upload attempt
    error_log("Upload attempt: $filename to $target_path");
    
    // Move uploaded file
    if (move_uploaded_file($file['tmp_name'], $target_path)) {
        $response = [
            'status' => 'success',
            'message' => 'File uploaded successfully',
            'filename' => $filename,
            'path' => $target_path,
            'url' => $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['HTTP_HOST'] . '/' . $target_path,
            'size' => filesize($target_path)
        ];
        
        header('Content-Type: application/json');
        echo json_encode($response);
        error_log("File uploaded: $filename");
    } else {
        $response = [
            'status' => 'error',
            'message' => 'Failed to move uploaded file',
            'error' => error_get_last()
        ];
        
        header('Content-Type: application/json');
        http_response_code(500);
        echo json_encode($response);
        error_log("Upload failed: $filename");
    }
    exit;
}

// If no action, show upload form
?>
<!DOCTYPE html>
<html>
<head>
    <title>Upload Handler</title>
</head>
<body>
    <h1>File Upload Handler</h1>
    <p>Upload directory: <?php echo $base_upload_dir; ?></p>
    <form method="post" enctype="multipart/form-data">
        <input type="file" name="fileToUpload">
        <input type="submit" value="Upload">
    </form>
    
    <h2>Uploaded Files:</h2>
    <ul>
    <?php
    $files = array_diff(scandir($base_upload_dir), array('.', '..'));
    foreach ($files as $file) {
        if (is_file($base_upload_dir . $file)) {
            echo "<li><a href='{$base_upload_dir}{$file}' target='_blank'>{$file}</a></li>";
        }
    }
    ?>
    </ul>
</body>
</html>