|
|
<?php |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function sanitizePath($path) { |
|
|
|
|
|
$path = preg_replace('@/+@' , DIRECTORY_SEPARATOR, preg_replace('@\?.*$@' , '', preg_replace('@\.{2,}@' , '', preg_replace('@[^\/\\a-zA-Z0-9\-\._]@', '', $path)))); |
|
|
return $path; |
|
|
} |
|
|
|
|
|
if (isset($_POST['mediaPath']) && ($path = sanitizePath(substr($_POST['mediaPath'], 0, 256)))) { |
|
|
define('UPLOAD_PATH', $path); |
|
|
} else { |
|
|
define('UPLOAD_PATH', 'media'); |
|
|
} |
|
|
|
|
|
$scandir = __DIR__ . DIRECTORY_SEPARATOR. UPLOAD_PATH; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$scan = function ($dir) use ($scandir, &$scan) { |
|
|
$files = []; |
|
|
|
|
|
|
|
|
|
|
|
if (file_exists($dir)) { |
|
|
foreach (scandir($dir) as $f) { |
|
|
if (! $f || $f[0] == '.') { |
|
|
continue; |
|
|
} |
|
|
|
|
|
if (is_dir($dir . '/' . $f)) { |
|
|
|
|
|
|
|
|
$files[] = [ |
|
|
'name' => $f, |
|
|
'type' => 'folder', |
|
|
'path' => str_replace($scandir, '', $dir) . '/' . $f, |
|
|
'items' => $scan($dir . '/' . $f), |
|
|
]; |
|
|
} else { |
|
|
|
|
|
|
|
|
$files[] = [ |
|
|
'name' => $f, |
|
|
'type' => 'file', |
|
|
'path' => str_replace($scandir, '', $dir) . '/' . $f, |
|
|
'size' => filesize($dir . '/' . $f), |
|
|
]; |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
return $files; |
|
|
}; |
|
|
|
|
|
$response = $scan($scandir); |
|
|
|
|
|
|
|
|
|
|
|
header('Content-type: application/json'); |
|
|
|
|
|
echo json_encode([ |
|
|
'name' => '', |
|
|
'type' => 'folder', |
|
|
'path' => '', |
|
|
'items' => $response, |
|
|
]); |
|
|
|