pallets-eco / flask-admin

Simple and extensible administrative interface framework for Flask
https://flask-admin.readthedocs.io
BSD 3-Clause "New" or "Revised" License
5.79k stars 1.57k forks source link

Sorting subdirectories in fileadmin is broken #1594

Open hadleyrich opened 6 years ago

hadleyrich commented 6 years ago

When trying to sort in a subdirectory in the fileadmin you are redirected to the base path. Pull request #1496 fixes this.

ljluestc commented 4 months ago
<?php
// Example PHP script handling file sorting in TYPO3 or similar CMS/framework

// Assuming a function to get the current directory path from request parameters or session
$currentDirectory = isset($_GET['dir']) ? $_GET['dir'] : '';

// Constructing the base path dynamically based on configuration or environment
$basePath = '/path/to/fileadmin'; // Replace with actual base path of fileadmin

// Ensure $currentDirectory is properly sanitized and validated to prevent directory traversal attacks
$fullPath = realpath($basePath . '/' . $currentDirectory);

if ($fullPath !== false && strpos($fullPath, $basePath) === 0) {
    // Valid directory within fileadmin
    $files = scandir($fullPath);

    // Sort files based on sorting criteria (e.g., name, size, date)
    $sortField = isset($_GET['sort']) ? $_GET['sort'] : 'name';

    switch ($sortField) {
        case 'name':
            natcasesort($files); // Sort by name
            break;
        case 'size':
            usort($files, function($a, $b) use ($fullPath) {
                return filesize($fullPath . '/' . $a) - filesize($fullPath . '/' . $b);
            });
            break;
        case 'date':
            usort($files, function($a, $b) use ($fullPath) {
                return filemtime($fullPath . '/' . $a) - filemtime($fullPath . '/' . $b);
            });
            break;
        default:
            // Default sorting logic
            natcasesort($files); // Sort by name
            break;
    }

    // Output sorted files
    foreach ($files as $file) {
        echo '<li>' . $file . '</li>';
    }
} else {
    echo 'Invalid directory path or access denied.';
}
?>