function exec_ogp_module()
{
    $modDb = new MySQLModuleDatabase();
    require "includes/config.inc.php";
    $modDb->connect($db_host, $db_user, $db_pass, $db_name, $table_prefix);
    global $db;
    $isAdmin = $db->isAdmin($_SESSION['user_id']);
    if ($isAdmin) {
        $game_home = $db->getGameHome($_REQUEST['home_id']);
    } else {
        $game_home = $db->getUserGameHome($_SESSION['user_id'], $_GET['home_id']);
    }
    if (!$game_home and !$isAdmin) {
        return;
    }
    $home_dbs = $modDb->getMysqlDBsbyHomeId($game_home['home_id']);
    if (empty($home_dbs)) {
        return;
    }
    $db_id = $_REQUEST['db_id'];
    $mysql_db = $modDb->getMysqlHomeDBbyId($game_home['home_id'], $db_id);
    if (!$mysql_db) {
        return;
    }
    $command = 'mysqldump --host=' . $mysql_db['mysql_ip'] . ' --port=' . $mysql_db['mysql_port'] . ' --user='******'db_user'] . ' --password='******'db_passwd'] . ' ' . $mysql_db['db_name'];
    if ($mysql_db['remote_server_id'] != "0") {
        $remote_server = $db->getRemoteServer($mysql_db['remote_server_id']);
        $remote = new OGPRemoteLibrary($remote_server['agent_ip'], $remote_server['agent_port'], $remote_server['encryption_key']);
        $host_stat = $remote->status_chk();
        if ($host_stat === 1) {
            $dump = $remote->exec($command, $output);
        }
    } else {
        $dump = system($command);
    }
    header("Content-Type: text/plain");
    header('Content-Disposition: attachment; filename="backup-' . $mysql_db['db_name'] . '-' . date("d-m-Y") . '.sql"');
    echo $dump;
}
function exec_ogp_module()
{
    $modDb = new MySQLModuleDatabase();
    require "includes/config.inc.php";
    $modDb->connect($db_host, $db_user, $db_pass, $db_name, $table_prefix);
    global $view, $db;
    echo "<h2>" . get_lang('add_new_mysql_host') . "</h2>";
    if (isset($_GET['add_mysql_server'])) {
        foreach ($_GET as $name => $value) {
            $get[$name] = trim($value);
        }
        if (empty($get['mysql_ip'])) {
            print_failure(get_lang('enter_mysql_ip'));
        }
        if (!isPortValid($get['mysql_port'])) {
            print_failure(get_lang('enter_valid_port'));
        }
        if (empty($get['mysql_root_passwd'])) {
            print_failure(get_lang('enter_mysql_root_password'));
        }
        if (empty($get['mysql_name'])) {
            print_failure(get_lang('enter_mysql_name'));
        }
        if ($get['privilegies'] == "custom") {
            $priv = $get;
            $privilegies_str = "";
            unset($priv['m'], $priv['p'], $priv['remote_server_id'], $priv['mysql_ip'], $priv['mysql_port'], $priv['mysql_root_passwd'], $priv['mysql_name'], $priv['privilegies'], $priv['add_mysql_server']);
            foreach ($priv as $name => $value) {
                $privilegies_str .= str_replace("_", " ", $name) . ", ";
            }
        } else {
            $privilegies_str = "ALL";
        }
        $privilegies_str = rtrim($privilegies_str, ', ');
        $mysql_server_id = $modDb->addMysqlServer($get['remote_server_id'], $get['mysql_name'], $get['mysql_ip'], $get['mysql_port'], $get['mysql_root_passwd'], $privilegies_str);
        if (!$mysql_server_id) {
            print_failure(get_lang('could_not_add_mysql_server'));
            $view->refresh("?m=mysql&p=mysql_admin");
            return;
        }
        print_success(get_lang('server_added'));
        $view->refresh("?m=mysql&p=mysql_admin");
        return;
    }
    echo "<p>" . get_lang('note_mysql_host') . "</p>";
    $servers = $db->getRemoteServers();
    $conn_method[0] = get_lang('direct_connection');
    foreach ($servers as $server_row) {
        $id = $server_row['remote_server_id'];
        $name = get_lang_f('connection_through_remote_server_named', $server_row['remote_server_name']);
        $conn_method[$id] = $name;
    }
    require_once "includes/form_table_class.php";
    $ft = new FormTable();
    $ft->start_form("", "GET");
    $ft->start_table();
    $ft->add_field_hidden('m', 'mysql');
    $ft->add_field_hidden('p', 'mysql_admin');
    $ft->add_custom_field('connection_method', create_drop_box_from_array($conn_method, "remote_server_id", "0", false));
    $ft->add_field('string', 'mysql_name', isset($_GET['mysql_name']) ? $_GET['mysql_name'] : "");
    $ft->add_field('string', 'mysql_ip', isset($_GET['mysql_ip']) ? $_GET['mysql_ip'] : "localhost");
    $ft->add_field('string', 'mysql_port', isset($_GET['mysql_port']) ? $_GET['mysql_port'] : "3306");
    $ft->add_field('string', 'mysql_root_passwd', isset($_GET['mysql_root_passwd']) ? $_GET['mysql_root_passwd'] : "");
    $ft->add_custom_field('privilegies', create_drop_box_from_array(array('all' => get_lang('all'), 'custom' => get_lang('custom')), "privilegies", isset($_GET['privilegies']) ? $_GET['privilegies'] : "all", false));
    if (isset($_GET['privilegies']) and $_GET['privilegies'] == "custom") {
        $ft->add_custom_field('alter', '<input type="checkbox" name="ALTER" checked="checked" >');
        $ft->add_custom_field('create', '<input type="checkbox" name="CREATE" checked="checked" >');
        $ft->add_custom_field('create_temporary_tables', '<input type="checkbox" name="CREATE TEMPORARY TABLES" checked="checked" >');
        $ft->add_custom_field('delete', '<input type="checkbox" name="DELETE" checked="checked" >');
        $ft->add_custom_field('drop', '<input type="checkbox" name="DROP" checked="checked" >');
        $ft->add_custom_field('index', '<input type="checkbox" name="INDEX" checked="checked" >');
        $ft->add_custom_field('insert', '<input type="checkbox" name="INSERT" checked="checked" >');
        $ft->add_custom_field('lock_tables', '<input type="checkbox" name="LOCK TABLES" checked="checked" >');
        $ft->add_custom_field('select', '<input type="checkbox" name="SELECT" checked="checked" >');
        $ft->add_custom_field('update', '<input type="checkbox" name="UPDATE" checked="checked" >');
        $ft->add_custom_field('grant_option', '<input type="checkbox" name="GRANT OPTION" checked="checked" >');
    }
    $ft->end_table();
    $ft->add_button("submit", "add_mysql_server", get_lang('add_mysql_server'));
    $ft->end_form();
    $mysql_servers = $modDb->getMysqlServers();
    if ($mysql_servers === FALSE) {
        return;
    }
    $tr = 0;
    ?>
<table id="servermonitor" class="tablesorter remote">
		<thead> 
		<tr> 
			<th colspan="4" ><?php 
    print_lang('configured_mysql_hosts');
    ?>
</th> 
		</tr> 
		</thead> 
		<tbody> <?php 
    foreach ($mysql_servers as $mysql_server) {
        if ($mysql_server['remote_server_id'] != 0) {
            $remote_server = $db->getRemoteServer($mysql_server['remote_server_id']);
            $remote = new OGPRemoteLibrary($remote_server['agent_ip'], $remote_server['agent_port'], $remote_server['encryption_key']);
            $host_stat = $remote->status_chk();
            if ($host_stat === 0) {
                $server_status = "<span class='failure'>" . get_lang('offline') . "</span> ";
            } elseif ($host_stat === 1) {
                $server_status = "<span class='success'>" . get_lang('online') . "</span>";
                $command = "mysql -h " . $mysql_server['mysql_ip'] . " -P " . $mysql_server['mysql_port'] . " -u root -p" . $mysql_server['mysql_root_passwd'] . ' -e exit; echo $?';
                $test_mysql_conn = $remote->exec($command);
                if ($test_mysql_conn == 0) {
                    $server_status .= " / <span class='success'>" . get_lang('mysql_online') . "</span>";
                } else {
                    $server_status .= "/<span class='failure'>" . get_lang('mysql_offline') . "</span>";
                }
            } elseif ($host_stat === -1) {
                $server_status = "<span class='failure'>" . get_lang('encryption_key_mismatch') . "</span>\n";
            } else {
                $server_status = "<span class='failure'>" . get_lang('unknown_error') . ": {$host_stat}</span>\n";
            }
        } else {
            @($link = mysql_connect($mysql_server['mysql_ip'] . ':' . $mysql_server['mysql_port'], 'root', $mysql_server['mysql_root_passwd']));
            if ($link === FALSE) {
                $server_status = "<span class='failure'>" . get_lang('mysql_offline') . "</span>";
            } else {
                $server_status = "<span class='success'>" . get_lang('mysql_online') . "</span>";
                mysql_close($link);
            }
        }
        $databases = "";
        $mysql_server_dbs = $modDb->getMysqlServerDBs($mysql_server['mysql_server_id']);
        if (!empty($mysql_server_dbs)) {
            foreach ($mysql_server_dbs as $mysql_db) {
                $databases .= $mysql_db['db_name'] . "<a href='?m=mysql&p=edit&mysql_server_id=" . $mysql_server['mysql_server_id'] . "&assign=true&db_id=" . $mysql_db['db_id'] . "&edit_db_settings'>[" . get_lang('edit') . "]</a>\n" . "<a href='?m=mysql&p=edit&mysql_server_id=" . $mysql_server['mysql_server_id'] . "&assign=true&db_id=" . $mysql_db['db_id'] . "&remove_db'>[" . get_lang('remove') . "]</a>\n" . "<br>";
            }
        }
        $conection_type = $mysql_server['remote_server_id'] == 0 ? get_lang('direct_connection') : get_lang_f('connection_through_remote_server_named', $remote_server['remote_server_name']);
        $buttons = "<a href='?m=mysql&amp;p=edit&amp;mysql_server_id=" . $mysql_server['mysql_server_id'] . "&amp;delete'>[" . get_lang('remove') . "]</a>\n" . "<a href='?m=mysql&amp;p=edit&amp;mysql_server_id=" . $mysql_server['mysql_server_id'] . "&amp;edit'>[" . get_lang('edit') . "]</a>\n" . "<a href='?m=mysql&amp;p=edit&amp;mysql_server_id=" . $mysql_server['mysql_server_id'] . "&amp;assign'>[" . get_lang('assign_db') . "]</a>\n";
        $tittle = "<b>ID#:</b>  <b style='color:red;'>" . $mysql_server['mysql_server_id'] . "</b></td>\n\t\t\t\t\t<td class='collapsible' ><b>" . get_lang('mysql_server_name') . ":</b> " . $mysql_server['mysql_name'] . "</td>\n\t\t\t\t\t<td class='collapsible' ><b>" . get_lang('server_status') . ":</b>{$server_status}</td>";
        $data = "<tr class='expand-child' >\n\t\t\t\t   <td>\n\t\t\t\t\t<b>" . get_lang('mysql_ip_port') . ":</b> " . $mysql_server['mysql_ip'] . ":" . $mysql_server['mysql_port'] . "<br />\n\t\t\t\t\t<b>" . get_lang('mysql_root_passwd') . ":</b> " . $mysql_server['mysql_root_passwd'] . "<br />\n\t\t\t\t\t<b>" . get_lang('connection_method') . ":</b> " . $conection_type . "<br />\n\t\t\t\t\t<b>" . get_lang('user_privilegies') . ":</b> " . $mysql_server['privilegies_str'] . "<br />\n\t\t\t\t   </td>\n\t\t\t\t   <td>\n\t\t\t\t    <b>" . get_lang('current_dbs') . ":</b><br />" . $databases . "</td>\n\t\t\t\t   <td>\n\t\t\t\t    {$buttons}\n\t\t\t\t   </td>\n\t\t\t\t </tr>";
        // Template
        $first = "<tr class='maintr'><td class='collapsible' >{$tittle}</td></tr>";
        $second = $data;
        //Echo them all
        echo "{$first}{$second}";
    }
    echo "</tbody>";
    echo "</table>\n";
    ?>
<script type="text/javascript">
$(document).ready(function(){ 
	$("#servermonitor")
		.collapsible("td.collapsible", {collapse: true})
		.tablesorter({sortList: [[0,0], [1,0]] , widgets: ['zebra','repeatHeaders']});
});
</script>
<?php 
    unset($modDb);
}
function exec_ogp_module()
{
    $modDb = new MySQLModuleDatabase();
    require "includes/config.inc.php";
    $modDb->connect($db_host, $db_user, $db_pass, $db_name, $table_prefix);
    global $view, $db;
    $mysql_server_id = @$_REQUEST['mysql_server_id'];
    $mysql_server = $modDb->getMysqlServer($mysql_server_id);
    if ($mysql_server['remote_server_id'] == "0") {
        $server_homes = $db->getGameHomes();
    } else {
        $server_homes = $modDb->getGameHomesByRemoteServerId($mysql_server['remote_server_id']);
    }
    $homes_array[0] = get_lang('select_game_server');
    foreach ($server_homes as $server_home) {
        $homes_array["{$server_home['home_id']}"] = "(ID " . $server_home['home_id'] . ") " . $server_home['home_name'];
    }
    if ($mysql_server === FALSE) {
        print_failure(get_lang_f('invalid_mysql_server_id', $mysql_server_id));
        $view->refresh("?m=mysql&p=mysql_admin");
        return;
    }
    if (isset($_REQUEST['add_db'])) {
        $home_id = $_POST['home_id'];
        $db_user = trim($_POST['db_user']);
        $db_passwd = trim($_POST['db_passwd']);
        $db_name = trim($_POST['db_name']);
        $enabled = $_POST['enabled'];
        if (empty($db_user)) {
            print_failure(get_lang('enter_db_user'));
        } elseif (empty($db_passwd)) {
            print_failure(get_lang('enter_db_password'));
        } elseif (empty($db_name)) {
            print_failure(get_lang('enter_db_name'));
        } elseif ($home_id == 0) {
            print_failure(get_lang('select_game_server'));
        } else {
            $db_id = $modDb->addMysqlServerDB($mysql_server_id, $home_id, $db_user, $db_passwd, $db_name, $enabled);
            if (!$db_id) {
                print_failure(get_lang_f('there_is_another_db_named_or_user_named', $db_name, $db_user));
            } else {
                $mysql_db = $modDb->getMysqlDBbyId($db_id);
                if (!$mysql_db) {
                    return;
                }
                if ($mysql_db['remote_server_id'] != "0") {
                    $remote_server = $db->getRemoteServer($mysql_db['remote_server_id']);
                    $remote = new OGPRemoteLibrary($remote_server['agent_ip'], $remote_server['agent_port'], $remote_server['encryption_key']);
                    $host_stat = $remote->status_chk();
                    if ($host_stat === 1) {
                        $command = "mysql -h localhost -P " . $mysql_db['mysql_port'] . " -u root -p" . $mysql_db['mysql_root_passwd'] . ' -e exit; echo $?';
                        $test_mysql_conn = $remote->exec($command);
                        if ($test_mysql_conn == 0) {
                            $SQL = "CREATE DATABASE IF NOT EXISTS \\`" . $mysql_db['db_name'] . "\\`;" . "GRANT " . $mysql_db['privilegies_str'] . " ON \\`" . $mysql_db['db_name'] . "\\`.* TO '" . $mysql_db['db_user'] . "'@'localhost' IDENTIFIED BY '" . $mysql_db['db_passwd'] . "';" . "FLUSH PRIVILEGES;";
                            $command = "mysql --host=localhost --port=" . $mysql_db['mysql_port'] . " -uroot -p" . $mysql_db['mysql_root_passwd'] . " -e \"" . $SQL . "\"";
                            $result = $remote->exec($command);
                        }
                    }
                } else {
                    @($link = mysql_connect($mysql_db['mysql_ip'] . ':' . $mysql_db['mysql_port'], $mysql_db['db_user'], $mysql_db['db_passwd']));
                    if ($link === FALSE) {
                        @($link = mysql_connect($mysql_db['mysql_ip'] . ':' . $mysql_db['mysql_port'], 'root', $mysql_db['mysql_root_passwd']));
                        if ($link !== FALSE) {
                            $queries = array("CREATE DATABASE IF NOT EXISTS `" . $mysql_db['db_name'] . "`;", "GRANT " . $mysql_db['privilegies_str'] . " ON `" . $mysql_db['db_name'] . "`.* TO '" . $mysql_db['db_user'] . "'@'" . $mysql_db['mysql_ip'] . "' IDENTIFIED BY '" . $mysql_db['db_passwd'] . "';", "FLUSH PRIVILEGES;");
                            foreach ($queries as $query) {
                                @($return = mysql_query($query));
                                if (!$return) {
                                    break;
                                }
                            }
                            mysql_close($link);
                        }
                    }
                }
                print_success(get_lang_f('db_added_for_home_id', $_POST['home_id']));
            }
        }
        $view->refresh("?m=mysql&p=mysql_admin&amp;p=edit&amp;mysql_server_id=" . $mysql_server_id . "&amp;assign", 5);
    } else {
        if (isset($_REQUEST['remove_db'])) {
            $db_id = $_REQUEST['db_id'];
            $mysql_db = $modDb->getMysqlDBbyId($db_id);
            if ($mysql_db['remote_server_id'] != "0") {
                $remote_server = $db->getRemoteServer($mysql_db['remote_server_id']);
                $remote = new OGPRemoteLibrary($remote_server['agent_ip'], $remote_server['agent_port'], $remote_server['encryption_key']);
                $host_stat = $remote->status_chk();
                if ($host_stat === 1) {
                    $remote->exec('mysql --host=localhost --port=' . $mysql_db['mysql_port'] . ' -uroot -p' . $mysql_db['mysql_root_passwd'] . ' -e "DROP DATABASE ' . $mysql_db['db_name'] . ";DROP USER '" . $mysql_db['db_user'] . "'@'" . $mysql_db['mysql_ip'] . "';\"");
                }
            } else {
                @($link = mysql_connect($mysql_db['mysql_ip'] . ':' . $mysql_db['mysql_port'], 'root', $mysql_db['mysql_root_passwd']));
                if ($link !== FALSE) {
                    $queries = array("DROP DATABASE " . $mysql_db['db_name'] . ";", "DROP USER '" . $mysql_db['db_user'] . "'@'" . $mysql_db['mysql_ip'] . "';");
                    foreach ($queries as $query) {
                        @($return = mysql_query($query));
                        if (!$return) {
                            break;
                        }
                    }
                    mysql_close($link);
                    $modDb->connect($db_host, $db_user, $db_pass, $db_name, $table_prefix);
                }
            }
            if ($modDb->removeMysqlServerDB($db_id) === FALSE) {
                print_failure(get_lang('could_not_remove_db'));
            } else {
                print_success(get_lang_f('db_removed_successfully_from_mysql_server_named', $mysql_db['mysql_name']));
            }
            $view->refresh("?m=mysql&p=mysql_admin&amp;p=edit&amp;mysql_server_id=" . $mysql_server_id . "&amp;assign");
        } else {
            if (isset($_POST['save_db_changes'])) {
                $db_id = $_POST['db_id'];
                $home_id = $_POST['home_id'];
                $post_db_user = trim($_POST['db_user']);
                $post_db_passwd = trim($_POST['db_passwd']);
                $post_db_name = trim($_POST['db_name']);
                $enabled = $_POST['enabled'];
                if (empty($post_db_passwd)) {
                    print_failure(get_lang('enter_db_password'));
                } elseif ($home_id == 0) {
                    print_failure(get_lang('select_game_server'));
                } else {
                    $mysql_db = $modDb->getMysqlDBbyId($db_id);
                    if ($post_db_passwd != $mysql_db['db_passwd']) {
                        if ($mysql_db['remote_server_id'] != "0") {
                            $remote_server = $db->getRemoteServer($mysql_db['remote_server_id']);
                            $remote = new OGPRemoteLibrary($remote_server['agent_ip'], $remote_server['agent_port'], $remote_server['encryption_key']);
                            $host_stat = $remote->status_chk();
                            if ($host_stat === 1) {
                                $SQL = "DROP USER '" . $mysql_db['db_user'] . "'@'" . $mysql_server['mysql_ip'] . "';" . "GRANT " . $mysql_db['privilegies_str'] . " ON \\`" . $mysql_db['db_name'] . "\\`.* TO '" . $mysql_db['db_user'] . "'@'localhost' IDENTIFIED BY '" . $post_db_passwd . "';" . "FLUSH PRIVILEGES;";
                                $command = "mysql --host=localhost --port=" . $mysql_db['mysql_port'] . " -uroot -p" . $mysql_db['mysql_root_passwd'] . " -e \"" . $SQL . "\"";
                                $remote->exec($command);
                            }
                        } else {
                            @($link = mysql_connect($mysql_db['mysql_ip'] . ':' . $mysql_db['mysql_port'], 'root', $mysql_db['mysql_root_passwd']));
                            if ($link !== FALSE) {
                                $queries = array("DROP USER '" . $mysql_db['db_user'] . "'@'" . $mysql_server['mysql_ip'] . "';", "GRANT " . $mysql_db['privilegies_str'] . " ON `" . $mysql_db['db_name'] . "`.* TO '" . $mysql_db['db_user'] . "'@'" . $mysql_db['mysql_ip'] . "' IDENTIFIED BY '" . $post_db_passwd . "';", "FLUSH PRIVILEGES;");
                                foreach ($queries as $query) {
                                    @($return = mysql_query($query));
                                    if (!$return) {
                                        break;
                                    }
                                }
                                mysql_close($link);
                                $modDb->connect($db_host, $db_user, $db_pass, $db_name, $table_prefix);
                            }
                        }
                    }
                    if ($enabled != $mysql_db['enabled']) {
                        if ($mysql_db['remote_server_id'] != "0") {
                            $remote_server = $db->getRemoteServer($mysql_db['remote_server_id']);
                            $remote = new OGPRemoteLibrary($remote_server['agent_ip'], $remote_server['agent_port'], $remote_server['encryption_key']);
                            $host_stat = $remote->status_chk();
                            if ($host_stat === 1) {
                                if ($enabled == "0") {
                                    $SQL = "DROP USER '" . $mysql_db['db_user'] . "'@'" . $mysql_server['mysql_ip'] . "';" . "FLUSH PRIVILEGES;";
                                } else {
                                    $SQL = "GRANT " . $mysql_db['privilegies_str'] . " ON \\`" . $mysql_db['db_name'] . "\\`.* TO '" . $mysql_db['db_user'] . "'@'localhost' IDENTIFIED BY '" . $post_db_passwd . "';" . "FLUSH PRIVILEGES;";
                                }
                                $command = "mysql --host=localhost --port=" . $mysql_db['mysql_port'] . " -uroot -p" . $mysql_db['mysql_root_passwd'] . " -e \"" . $SQL . "\"";
                                $remote->exec($command);
                            }
                        } else {
                            @($link = mysql_connect($mysql_db['mysql_ip'] . ':' . $mysql_db['mysql_port'], 'root', $mysql_db['mysql_root_passwd']));
                            if ($link !== FALSE) {
                                if ($enabled == "0") {
                                    $queries = array("DROP USER '" . $mysql_db['db_user'] . "'@'" . $mysql_server['mysql_ip'] . "';", "FLUSH PRIVILEGES;");
                                } else {
                                    $queries = array("GRANT " . $mysql_db['privilegies_str'] . " ON `" . $mysql_db['db_name'] . "`.* TO '" . $mysql_db['db_user'] . "'@'" . $mysql_db['mysql_ip'] . "' IDENTIFIED BY '" . $post_db_passwd . "';", "FLUSH PRIVILEGES;");
                                }
                                foreach ($queries as $query) {
                                    @($return = mysql_query($query));
                                    if (!$return) {
                                        break;
                                    }
                                }
                                mysql_close($link);
                                $modDb->connect($db_host, $db_user, $db_pass, $db_name, $table_prefix);
                            }
                        }
                    }
                    if ($modDb->editMysqlServerDB($db_id, $home_id, $post_db_user, $post_db_passwd, $post_db_name, $enabled) === FALSE) {
                        print_failure(get_lang('could_not_be_changed'));
                    } else {
                        print_success(get_lang_f('db_changed_successfully', $post_db_name));
                    }
                }
                $view->refresh("?m=mysql&p=mysql_admin&amp;p=edit&amp;mysql_server_id=" . $mysql_server_id . "&amp;assign");
            } else {
                if (isset($_REQUEST['delete'])) {
                    if (!isset($_REQUEST['y'])) {
                        echo "<p>" . get_lang_f('areyousure_remove_mysql_server', $mysql_server['mysql_name']) . "</p>\n                <p><a href='?m=mysql&p=mysql_admin&amp;p=edit&amp;mysql_server_id=" . $mysql_server_id . "&amp;delete&amp;y=y'>" . get_lang('yes') . "</a> <a href='?m=mysql&p=mysql_admin'>" . get_lang('no') . "</a></p>";
                        return;
                    } else {
                        $mysql_dbs = $modDb->getMysqlServerDBs($mysql_server_id);
                        if (!empty($mysql_dbs)) {
                            foreach ($mysql_dbs as $mysql_db) {
                                if ($mysql_server['remote_server_id'] != "0") {
                                    $remote_server = $db->getRemoteServer($mysql_server['remote_server_id']);
                                    $remote = new OGPRemoteLibrary($remote_server['agent_ip'], $remote_server['agent_port'], $remote_server['encryption_key']);
                                    $host_stat = $remote->status_chk();
                                    if ($host_stat === 1) {
                                        $remote->exec('mysql --host=localhost --port=' . $mysql_server['mysql_port'] . ' -uroot -p' . $mysql_server['mysql_root_passwd'] . ' -e "DROP DATABASE ' . $mysql_db['db_name'] . ";DROP USER '" . $mysql_db['db_user'] . "'@'" . $mysql_server['mysql_ip'] . "';\"");
                                    }
                                } else {
                                    @($link = mysql_connect($mysql_server['mysql_ip'] . ':' . $mysql_server['mysql_port'], 'root', $mysql_server['mysql_root_passwd']));
                                    if ($link !== FALSE) {
                                        $queries = array("DROP DATABASE " . $mysql_db['db_name'] . ";", "DROP USER '" . $mysql_db['db_user'] . "'@'" . $mysql_server['mysql_ip'] . "';");
                                        foreach ($queries as $query) {
                                            @($return = mysql_query($query));
                                            if (!$return) {
                                                break;
                                            }
                                        }
                                        mysql_close($link);
                                    }
                                }
                            }
                        }
                        if ($modDb->removeMysqlServer($mysql_server_id) === FALSE) {
                            print_failure(get_lang('error_while_remove'));
                        } else {
                            print_success(get_lang_f('mysql_server_removed', $mysql_server['mysql_name']));
                        }
                    }
                    $view->refresh("?m=mysql&p=mysql_admin");
                    return;
                } else {
                    if (isset($_POST['save_settings'])) {
                        foreach ($_POST as $name => $value) {
                            $get[$name] = trim($value);
                        }
                        if (empty($get['mysql_ip'])) {
                            print_failure(get_lang('enter_mysql_ip'));
                        } elseif (!isPortValid($get['mysql_port'])) {
                            print_failure(get_lang('enter_valid_port'));
                        } elseif (empty($get['mysql_root_passwd'])) {
                            print_failure(get_lang('enter_mysql_root_password'));
                        } elseif (empty($get['mysql_name'])) {
                            print_failure(get_lang('enter_mysql_name'));
                        } elseif (!$modDb->editMysqlServer($mysql_server_id, $get['remote_server_id'], $get['mysql_name'], $get['mysql_ip'], $get['mysql_port'], $get['mysql_root_passwd'], $mysql_server['privilegies_str'])) {
                            print_failure(get_lang_f('unable_to_set_changes_to', $mysql_server['mysql_name']));
                        } else {
                            print_success(get_lang_f('mysql_server_settings_changed', $mysql_server['mysql_name']));
                        }
                        $view->refresh("?m=mysql&p=mysql_admin&amp;p=edit&amp;mysql_server_id=" . $mysql_server_id . "&amp;edit", 5);
                    } elseif (isset($_GET['edit'])) {
                        echo "<h2>" . get_lang_f('editing_mysql_server', $mysql_server['mysql_name']) . "</h2>";
                        $mysql_server = $modDb->getMysqlServer($mysql_server_id);
                        $servers = $db->getRemoteServers();
                        $conn_method[0] = get_lang('direct_connection');
                        foreach ($servers as $server_row) {
                            $id = $server_row['remote_server_id'];
                            $name = get_lang_f('connection_through_remote_server_named', $server_row['remote_server_name']);
                            $conn_method[$id] = $name;
                        }
                        $ft = new FormTable();
                        $ft->start_form('?m=mysql&amp;p=edit&amp;mysql_server_id=' . $mysql_server_id . '&amp;edit');
                        $ft->start_table();
                        $ft->add_custom_field('connection_method', create_drop_box_from_array($conn_method, "remote_server_id", $mysql_server['remote_server_id'], false));
                        $ft->add_field('string', 'mysql_name', $mysql_server['mysql_name']);
                        $ft->add_field('string', 'mysql_ip', $mysql_server['mysql_ip']);
                        $ft->add_field('string', 'mysql_port', $mysql_server['mysql_port']);
                        $ft->add_field('string', 'mysql_root_passwd', $mysql_server['mysql_root_passwd']);
                        $ft->end_table();
                        $ft->add_button("submit", "save_settings", get_lang('save_settings'));
                        $ft->end_form();
                        echo create_back_button('mysql', 'mysql_admin');
                    } elseif (isset($_GET['assign'])) {
                        echo "<h2>" . get_lang_f('mysql_dbs_for', $mysql_server['mysql_name']) . "</h2>";
                        $mysql_server_dbs = $modDb->getMysqlServerDBs($mysql_server['mysql_server_id']);
                        if (!empty($mysql_server_dbs)) {
                            echo "<h4>" . get_lang('edit_dbs') . "</h4>";
                            foreach ($mysql_server_dbs as $mysql_db) {
                                $home_info = $db->getGameHomeWithoutMods($mysql_db['home_id']);
                                $db_array["{$mysql_db['db_id']}"] = $mysql_db['db_name'] . " (" . $home_info['home_name'] . ")";
                            }
                            $ft = new FormTable();
                            $ft->start_form('', 'GET');
                            $ft->add_field_hidden('m', 'mysql');
                            $ft->add_field_hidden('p', 'edit');
                            $ft->add_field_hidden('mysql_server_id', $mysql_server_id);
                            $ft->add_field_hidden('assign', 'true');
                            $ft->start_table();
                            $ft->add_custom_field('select_db', create_drop_box_from_array($db_array, "db_id", isset($_GET['db_id']) ? $_GET['db_id'] : "", false));
                            $ft->end_table();
                            $ft->add_button('submit', 'edit_db_settings', get_lang('edit_db_settings'));
                            $ft->add_button('submit', 'remove_db', get_lang('remove_db'));
                            $ft->end_form();
                            if (isset($_GET['edit_db_settings'])) {
                                $mysql_db = $modDb->getMysqlDBbyId($_GET['db_id']);
                                $ft = new FormTable();
                                $ft->start_form('');
                                $ft->add_field_hidden('db_id', $mysql_db['db_id']);
                                $ft->start_table();
                                $ft->add_custom_field('game_server', create_drop_box_from_array($homes_array, "home_id", $mysql_db['home_id'], false));
                                $ft->add_field('string', 'db_user', $mysql_db['db_user'], "50", "readonly");
                                $ft->add_field('string', 'db_passwd', $mysql_db['db_passwd']);
                                $ft->add_field('string', 'db_name', $mysql_db['db_name'], "50", "readonly");
                                $ft->add_field('on_off', 'enabled', $mysql_db['enabled']);
                                $ft->end_table();
                                $ft->add_button('submit', 'save_db_changes', get_lang('save_db_changes'));
                                $ft->end_form();
                            }
                        }
                        echo "<h4>" . get_lang('add_db') . "</h4>";
                        $ft = new FormTable();
                        $ft->start_form('');
                        $ft->start_table();
                        $ft->add_custom_field('game_server', create_drop_box_from_array($homes_array, "home_id", "0", false));
                        $ft->add_field('string', 'db_user', '');
                        $ft->add_field('string', 'db_passwd', genRandomString('10'));
                        $ft->add_field('string', 'db_name', '');
                        $ft->add_field('on_off', 'enabled', '1');
                        $ft->end_table();
                        $ft->add_button('submit', 'add_db', get_lang('add_db'));
                        $ft->end_form();
                        echo create_back_button('mysql', 'mysql_admin');
                    } else {
                        print_failure("Invalid url.");
                        $view->refresh("?m=mysql&p=mysql_admin");
                    }
                }
            }
        }
    }
}
function exec_ogp_module()
{
    $modDb = new MySQLModuleDatabase();
    require "includes/config.inc.php";
    $modDb->connect($db_host, $db_user, $db_pass, $db_name, $table_prefix);
    global $view, $db;
    $isAdmin = $db->isAdmin($_SESSION['user_id']);
    if ($isAdmin) {
        $game_home = $db->getGameHome($_GET['home_id']);
    } else {
        $game_home = $db->getUserGameHome($_SESSION['user_id'], $_GET['home_id']);
    }
    if (!$game_home and !$isAdmin) {
        return;
    }
    echo "<h2>" . get_lang_f('mysql_dbs_for', $game_home['home_name']) . "</h2>";
    $home_dbs = $modDb->getMysqlDBsbyHomeId($game_home['home_id']);
    if (empty($home_dbs)) {
        print_failure(get_lang_f('there_are_no_databases_assigned_for', $game_home['home_name']));
        return;
    }
    $db_array["0"] = get_lang('select_db');
    foreach ($home_dbs as $home_db) {
        $db_array["{$home_db['db_id']}"] = $home_db['db_name'];
    }
    $ft = new FormTable();
    $ft->start_form('');
    $ft->start_table();
    $ft->add_custom_field('select_db', create_drop_box_from_array($db_array, "db_id", isset($_REQUEST['db_id']) ? $_REQUEST['db_id'] : "0", false));
    $ft->end_table();
    $ft->end_form();
    $database_exists = FALSE;
    $server_online = FALSE;
    if (isset($_REQUEST['db_id']) and $_REQUEST['db_id'] != "0") {
        $db_id = $_REQUEST['db_id'];
        $mysql_db = $modDb->getMysqlHomeDBbyId($game_home['home_id'], $db_id);
        if (!$mysql_db) {
            return;
        }
        if ($mysql_db['remote_server_id'] != "0") {
            $remote_server = $db->getRemoteServer($mysql_db['remote_server_id']);
            $remote = new OGPRemoteLibrary($remote_server['agent_ip'], $remote_server['agent_port'], $remote_server['encryption_key']);
            $host_stat = $remote->status_chk();
            if ($host_stat === 1) {
                $command = "mysql -h localhost -P " . $mysql_db['mysql_port'] . " -u root -p" . $mysql_db['mysql_root_passwd'] . ' -e exit; echo $?';
                $test_mysql_conn = $remote->exec($command);
                if ($test_mysql_conn == 0) {
                    $user_db = $remote->exec('mysqlshow --user='******'db_user'] . ' --password='******'db_passwd'] . ' ' . $mysql_db['db_name']);
                    if ($user_db != "") {
                        $database_exists = TRUE;
                    }
                    $server_online = TRUE;
                }
            }
        } else {
            @($link = mysql_connect($mysql_db['mysql_ip'] . ':' . $mysql_db['mysql_port'], $mysql_db['db_user'], $mysql_db['db_passwd']));
            if ($link !== FALSE) {
                $server_online = TRUE;
                if (mysql_select_db($mysql_db['db_name'], $link) !== FALSE) {
                    $databases = mysql_query("SHOW TABLES;");
                    $user_db = "Database: " . $mysql_db['db_name'] . "\nTables:\n";
                    while ($table = mysql_fetch_array($databases)) {
                        $user_db .= $table[0] . "\n";
                    }
                    $database_exists = TRUE;
                }
            }
        }
        if (isset($_POST['restore'])) {
            $command = 'mysql --host=' . $mysql_db['mysql_ip'] . ' --port=' . $mysql_db['mysql_port'] . ' --user='******'db_user'] . ' --password='******'db_passwd'] . ' ' . $mysql_db['db_name'] . ' < ' . $_FILES["file"]["tmp_name"];
            if ($mysql_db['remote_server_id'] != "0") {
                $remote->exec($command, $output);
            } else {
                system($command);
            }
            $view->refresh('?m=mysql&p=user_db&home_id=' . $game_home['home_id'] . '&db_id=' . $db_id, 0);
        }
        if ($server_online and $database_exists) {
            echo "<table class='database' ><tr><td>\n<div class='dragbox bloc rounded' ><h4>" . get_lang('db_info') . "</h4>\n" . "<table class='database_info' ><tr>" . "<td><b>" . get_lang('mysql_ip') . " :</b></td><td>" . $mysql_db['mysql_ip'] . "</td></tr>\n" . "<td><b>" . get_lang('mysql_port') . " :</b></td><td>" . $mysql_db['mysql_port'] . "</td></tr>\n" . "<td><b>" . get_lang('db_name') . " :</b></td><td>" . $mysql_db['db_name'] . "</td></tr>\n" . "<td><b>" . get_lang('db_user') . " :</b></td><td>" . $mysql_db['db_user'] . "</td></tr>\n" . "<td><b>" . get_lang('db_passwd') . " :</b></td><td>" . $mysql_db['db_passwd'] . "</td></tr>\n" . "<td><b>" . get_lang('privilegies') . " :</b></td><td>" . $mysql_db['privilegies_str'] . "</td></tr></table></div>\n" . "<td><div class='dragbox bloc rounded' style='background:black;' ><h4>" . get_lang('db_tables') . "</h4>" . "<pre><xmp>" . $user_db . "</xmp></pre></div></td></tr></table>";
            echo "<h2>" . get_lang('db_backup') . "</h2>";
            ?>
			<table class='administration-table'>
			 <tr>
			  <td>
			  <form method="POST" action="?m=mysql&p=get_dump&home_id=<?php 
            echo $game_home['home_id'];
            ?>
&db_id=<?php 
            echo $db_id;
            ?>
&type=cleared" >
			   <button name="download"><?php 
            print_lang('download_db_backup');
            ?>
</button>
			  </form>
			  <br>
			  <form method="POST" action="?m=mysql&p=user_db&home_id=<?php 
            echo $game_home['home_id'];
            ?>
&db_id=<?php 
            echo $db_id;
            ?>
" enctype="multipart/form-data">
			   <label for="file"><?php 
            print_lang('sql_file');
            ?>
:</label>
			   <input type="file" name="file" id="file" />
			   <button name="restore"><?php 
            print_lang('restore_db_backup');
            ?>
</button>
			  </form>
			  </td>
			 </tr>
			</table>
			<?php 
        }
    }
}