function exec_ogp_module()
{
    global $db;
    $isAdmin = $db->isAdmin($_SESSION['user_id']);
    if (isset($_REQUEST['user_id']) && !$isAdmin) {
        echo "<p class='note'>" . get_lang('not_available') . "</p>";
        return;
    }
    if (isset($_REQUEST['group_id']) && !$isAdmin) {
        $result = $db->getUserGroupList($_SESSION['user_id']);
        foreach ($result as $row) {
            if ($row['group_id'] == $_REQUEST['group_id']) {
                $own_group = TRUE;
            }
        }
    }
    if (!$isAdmin && !isset($own_group)) {
        echo "<p class='note'>" . get_lang('not_available') . "</p>";
        return;
    }
    if (!isset($_REQUEST['user_id']) && !isset($_REQUEST['group_id'])) {
        print_failure(get_lang("invalid_url"));
        return;
    }
    /// \todo We might want to save this information to XML file?
    $selections = array("allow_updates" => "u", "allow_file_management" => "f", "allow_parameter_usage" => "p", "allow_extra_params" => "e", "allow_ftp" => "t", "allow_custom_fields" => "c");
    if (isset($_REQUEST['user_id'])) {
        $assign_id = $_REQUEST['user_id'];
        $id_type = "user";
        $user = $db->getUserById($assign_id);
        $assign_name = $user['users_login'];
    } else {
        if (isset($_REQUEST['group_id'])) {
            $assign_id = $_REQUEST['group_id'];
            $id_type = "group";
            $group = $db->getGroupById($assign_id);
            $assign_name = $group['group_name'];
        }
    }
    $submit = isset($_POST['submit']) ? $_POST['submit'] : "";
    if (isset($_REQUEST['assign'])) {
        $access_rights = "";
        foreach ($selections as $selection => $flag) {
            if (isset($_REQUEST[$selection])) {
                $access_rights .= $flag;
            }
        }
        $hacker = FALSE;
        if (!$isAdmin) {
            $home_info = $db->getUserGameHome($_SESSION['user_id'], $_REQUEST['home_id']);
            if (!$home_info) {
                print_failure(get_lang_f("failed_to_assign_game_for", $id_type, "(Hacking attempt)"));
                $hacker = TRUE;
            } else {
                foreach ($selections as $selection => $flag) {
                    if (isset($_REQUEST[$selection])) {
                        if (!preg_match("/{$flag}/", $home_info['access_rights'])) {
                            print_failure(get_lang_f("failed_to_assign_game_for", $id_type, "(Hacking attempt)"));
                            $hacker = TRUE;
                        }
                    }
                }
            }
        }
        if (!$hacker) {
            if ($db->assignHomeTo($id_type, $assign_id, $_REQUEST['home_id'], $access_rights) === TRUE) {
                print_success(get_lang_f("assigned_home_to_" . $id_type, $_REQUEST['home_id'], $assign_name));
                $db->logger(get_lang_f("assigned_home_to_" . $id_type, $_REQUEST['home_id'], $assign_name));
            } else {
                print_failure(get_lang_f("failed_to_assign_game_for_", $id_type, $db->getError()));
            }
        }
        unset($_POST['home_id']);
    } else {
        if (isset($_REQUEST['unassign'])) {
            if ($db->unassignHomeFrom($id_type, $assign_id, $_REQUEST['home_id']) === TRUE) {
                print_success(get_lang_f("unassigned_home_from_" . $id_type, $_REQUEST['home_id'], $assign_name));
                $db->logger(get_lang_f("unassigned_home_from_" . $id_type, $_REQUEST['home_id'], $assign_name));
            } else {
                print_failure(get_lang_f("failed_to_assign_game_from_", $id_type));
            }
        }
    }
    $remote_servers = $db->getRemoteServers();
    if (empty($remote_servers)) {
        print_failure(get_lang("no_remote_servers_available_please_add_at_least_one"));
        echo "<p><a href='?m=server'>" . get_lang('add_remote_server') . "</a></p>";
        return;
    }
    if ($isAdmin) {
        $available_homes = $db->getAvailableHomesFor($id_type, $assign_id);
    } else {
        $available_homes = $db->getAvailableUserHomesFor($id_type, $assign_id, $_SESSION['user_id']);
    }
    if (!empty($available_homes)) {
        echo "<h2>" . get_lang_f('assign_new_home_to_' . $id_type, $assign_name) . "</h2>";
        echo "<form action='?m=user_games&amp;p=assign' method='post'>";
        echo "<input name='" . $id_type . "_id' value='" . $assign_id . "' type='hidden' />\n";
        echo "<table class='center'><tr><td align='right'><label for='home_id'>" . get_lang('select_home') . ":</label></td>";
        echo '<td align="left"><select id="home_id" name="home_id" onchange="this.form.submit();">';
        echo "<option></option>\n";
        foreach ($available_homes as $home) {
            if (isset($_POST['home_id']) && $_POST['home_id'] == $home['home_id']) {
                $selected = "selected='selected'";
            } else {
                $selected = "";
            }
            echo "<option value='" . $home['home_id'] . "' {$selected} >" . $home['home_name'] . "</option>\n";
        }
        echo "</select></td>\n";
        if (isset($_POST['home_id']) and !empty($_POST['home_id'])) {
            if ($isAdmin) {
                $access_rights = "ufpetc";
            } else {
                $home_info = $db->getUserGameHome($_SESSION['user_id'], $_POST['home_id']);
                $access_rights = $home_info['access_rights'];
            }
            foreach ($selections as $selection => $flag) {
                echo create_selection($selection, $flag, $access_rights);
            }
            echo "<tr><td colspan='2'><input type='submit' name='assign' value='" . get_lang('assign') . "' /></td></tr>";
        }
        echo "</table></form>";
    } else {
        echo "<h2>" . get_lang("no_more_homes_available_that_can_be_assigned_for_this_{$id_type}") . "</h2>";
        //print_lang('you_can_add_a_new_game_server_from');
        //echo "<a href='?m=user_games'>".get_lang('game_servers')."</a>.</p>";
        if ($isAdmin) {
            echo get_lang_f("you_can_add_a_new_game_server_from", "<a href='?m=user_games'>" . get_lang('game_servers') . "</a>") . "</p>";
        }
    }
    // View servers for use if there are any.
    $game_homes = $db->getHomesFor($id_type, $assign_id);
    if (empty($game_homes)) {
        echo "<h3>" . get_lang_f("no_homes_assigned_to_" . $id_type, $assign_name) . "</h3>";
    } else {
        echo "<h2>" . get_lang('assigned_homes') . "</h2>";
        echo '<table class="center">';
        echo "<tr><th>" . get_lang('home_id') . "</th><th>" . get_lang('game_server') . "</th>\n            <th>" . get_lang('game_type') . "</th>\n            <th align='center'>" . get_lang('game_home') . "</th>\n            <th>" . get_lang('game_home_name') . "</th><th>" . get_lang('access_rights') . "</th>\n            <th>" . get_lang('actions') . "</th></tr>";
        foreach ($game_homes as $row) {
            echo "<tr><td>{$row['home_id']}</td>\n                <td>" . $row['agent_ip'] . " (Agent)</td>\n                <td>{$row['game_name']}</td>\n                <td>{$row['home_path']}</td>\n                <td>{$row['home_name']}</td>\n                <td>";
            echo empty($row['access_rights']) ? "-" : $row['access_rights'];
            echo "</td>\n                <td>\n                <form action='?m=user_games&amp;p=assign' method='post'>\n                <input name='" . $id_type . "_id' value='{$assign_id}' type='hidden' />\n                <input name='home_id' value='" . $row['home_id'] . "' type='hidden' />\n                <input type='submit' name='unassign' value='" . get_lang('unassign') . "' /></form></td>\n                </tr>";
        }
        echo "</table>";
    }
    if ($id_type === "group") {
        echo create_back_button('user_admin', 'show_groups');
    } else {
        echo create_back_button('user_admin');
    }
}
function exec_ogp_module()
{
    global $db;
    global $view;
    echo "<h2>" . get_lang('add_new_game_home') . "</h2>";
    echo "<p><a href='?m=user_games'>&lt;&lt; " . get_lang('back_to_game_servers') . "</a></p>";
    $remote_servers = $db->getRemoteServers();
    if ($remote_servers === FALSE) {
        echo "<p class='note'>" . get_lang('no_remote_servers_configured') . "</p>\n\t\t\t  <p><a href='?m=server'>" . get_lang('add_remote_server') . "</a></p>";
        return;
    }
    $game_cfgs = $db->getGameCfgs();
    $users = $db->getUserList();
    if ($game_cfgs === FALSE) {
        echo "<p class='note'>" . get_lang('no_game_configurations_found') . " <a href='?m=config_games'>" . get_lang('game_configurations') . "</a></p>";
        return;
    }
    echo "<p> <span class='note'>" . get_lang('note') . ":</span> " . get_lang('add_mods_note') . "</p>";
    $selections = array("allow_updates" => "u", "allow_file_management" => "f", "allow_parameter_usage" => "p", "allow_extra_params" => "e", "allow_ftp" => "t", "allow_custom_fields" => "c");
    if (isset($_REQUEST['add_game_home'])) {
        $rserver_id = $_POST['rserver_id'];
        $home_cfg_id = $_POST['home_cfg_id'];
        $web_user_id = trim($_POST['web_user_id']);
        $control_password = genRandomString(8);
        $access_rights = "";
        $ftp = FALSE;
        foreach ($selections as $selection => $flag) {
            if (isset($_REQUEST[$selection])) {
                $access_rights .= $flag;
                if ($flag == "t") {
                    $ftp = TRUE;
                }
            }
        }
        if (empty($web_user_id)) {
            print_failure(get_lang('game_path_empty'));
        } else {
            foreach ($game_cfgs as $row) {
                if ($row['home_cfg_id'] == $home_cfg_id) {
                    $server_name = $row['game_name'];
                }
            }
            foreach ($remote_servers as $server) {
                if ($server['remote_server_id'] == $rserver_id) {
                    $ogp_user = $server['ogp_user'];
                }
            }
            foreach ($users as $user) {
                if ($user['user_id'] == $web_user_id) {
                    $web_user = $user['users_login'];
                }
            }
            $ftppassword = genRandomString(8);
            $game_path = "/home/" . $ogp_user . "/OGP_User_Files/";
            if (($new_home_id = $db->addGameHome($rserver_id, $web_user_id, $home_cfg_id, clean_path($game_path), $server_name, $control_password, $ftppassword)) !== FALSE) {
                $db->assignHomeTo("user", $web_user_id, $new_home_id, $access_rights);
                if ($ftp) {
                    $home_info = $db->getGameHomeWithoutMods($new_home_id);
                    require_once 'includes/lib_remote.php';
                    $remote = new OGPRemoteLibrary($home_info['agent_ip'], $home_info['agent_port'], $home_info['encryption_key']);
                    $host_stat = $remote->status_chk();
                    if ($host_stat === 1) {
                        $remote->ftp_mgr("useradd", $home_info['home_id'], $home_info['ftp_password'], $home_info['home_path']);
                    }
                    $db->changeFtpStatus('enabled', $new_home_id);
                }
                print_success(get_lang('game_home_added'));
                $db->logger(get_lang('game_home_added') . " ({$server_name})");
                $view->refresh("?m=user_games&amp;p=edit&amp;home_id={$new_home_id}");
            } else {
                print_failure(get_lang_f("failed_to_add_home_to_db", $db->getError()));
            }
        }
    }
    // View form to add more servers.
    if (!isset($_POST['rserver_id'])) {
        echo "<form action='?m=user_games&amp;p=add' method='post'>";
        echo "<table class='center'>";
        echo "<tr><td  class='right'>" . get_lang('game_server') . "</td><td class='left'><select onchange=" . '"this.form.submit()"' . " name='rserver_id'>\n";
        echo "<option>" . get_lang('select_remote_server') . "</option>\n";
        foreach ($remote_servers as $server) {
            echo "<option value='" . $server['remote_server_id'] . "'>" . $server['remote_server_name'] . " (" . $server['agent_ip'] . ")</option>\n";
        }
        echo "</select>\n";
        echo "</form>";
        echo "</td></tr></table>";
    } else {
        if (isset($_POST['rserver_id'])) {
            $rhost_id = $_POST['rserver_id'];
        }
        $remote_server = $db->getRemoteServer($rhost_id);
        require_once 'includes/lib_remote.php';
        $remote = new OGPRemoteLibrary($remote_server['agent_ip'], $remote_server['agent_port'], $remote_server['encryption_key']);
        $host_stat = $remote->status_chk();
        if ($host_stat === 1) {
            $os = $remote->what_os();
        } else {
            print_failure(get_lang_f("caution_agent_offline_can_not_get_os_and_arch_showing_servers_for_all_platforms"));
            $os = "Unknown OS";
        }
        echo "<form action='?m=user_games&amp;p=add' method='post'>";
        echo "<table class='center'>";
        echo "<tr><td class='right'>" . get_lang('game_type') . "</td><td class='left'> <select name='home_cfg_id'>\n";
        // Linux 64 bits + wine
        if (preg_match("/Linux/", $os) and preg_match("/64/", $os) and preg_match("/wine/", $os)) {
            foreach ($game_cfgs as $row) {
                if (preg_match("/linux/", $row['game_key'])) {
                    echo "<option value='" . $row['home_cfg_id'] . "'>" . $row['game_name'];
                }
                if (preg_match("/64/", $row['game_key'])) {
                    echo " (64 bit)";
                }
                echo "</option>\n";
            }
            echo "<option style='background:black;color:white;' value=''>" . get_lang('wine_games') . ":</option>\n";
            foreach ($game_cfgs as $row) {
                if (preg_match("/win/", $row['game_key'])) {
                    echo "<option value='" . $row['home_cfg_id'] . "'>" . $row['game_name'];
                }
                if (preg_match("/64/", $row['game_key'])) {
                    echo " (64 bit)";
                }
                echo "</option>\n";
            }
        } elseif (preg_match("/Linux/", $os) and preg_match("/64/", $os)) {
            foreach ($game_cfgs as $row) {
                if (preg_match("/linux/", $row['game_key'])) {
                    echo "<option value='" . $row['home_cfg_id'] . "'>" . $row['game_name'];
                }
                if (preg_match("/64/", $row['game_key'])) {
                    echo " (64 bit)";
                }
                echo "</option>\n";
            }
        } elseif (preg_match("/Linux/", $os) and preg_match("/wine/", $os)) {
            foreach ($game_cfgs as $row) {
                if (preg_match("/linux32/", $row['game_key'])) {
                    echo "<option value='" . $row['home_cfg_id'] . "'>" . $row['game_name'] . "</option>\n";
                }
            }
            echo "<option style='background:black;color:white;' value=''>" . get_lang('wine_games') . "</option>\n";
            foreach ($game_cfgs as $row) {
                if (preg_match("/win32/", $row['game_key'])) {
                    echo "<option value='" . $row['home_cfg_id'] . "'>" . $row['game_name'] . "</option>\n";
                }
            }
        } elseif (preg_match("/Linux/", $os)) {
            foreach ($game_cfgs as $row) {
                if (preg_match("/linux32/", $row['game_key'])) {
                    echo "<option value='" . $row['home_cfg_id'] . "'>" . $row['game_name'] . "</option>\n";
                }
            }
        } elseif (preg_match("/CYGWIN/", $os) and preg_match("/64/", $os)) {
            foreach ($game_cfgs as $row) {
                if (preg_match("/win/", $row['game_key'])) {
                    echo "<option value='" . $row['home_cfg_id'] . "'>" . $row['game_name'];
                }
                if (preg_match("/64/", $row['game_key'])) {
                    echo " (64 bit)";
                }
                echo "</option>\n";
            }
        } elseif (preg_match("/CYGWIN/", $os)) {
            foreach ($game_cfgs as $row) {
                if (preg_match("/win32/", $row['game_key'])) {
                    echo "<option value='" . $row['home_cfg_id'] . "'>" . $row['game_name'] . "</option>\n";
                }
            }
        } elseif ($os == "Unknown OS") {
            foreach ($game_cfgs as $row) {
                echo "<option value='" . $row['home_cfg_id'] . "'>" . $row['game_name'];
                if (preg_match("/64/", $row['game_key'])) {
                    echo " (64 bit)";
                }
                echo "</option>\n";
            }
        }
        echo "</select>\n</td></tr>";
        // Select user
        echo "<tr><td class='right'>" . get_lang('login') . ":</td>\n\t\t\t<td class='left'><select name='web_user_id'>";
        $users = $db->getUserList();
        foreach ($users as $user) {
            // Only users and admins can be assigned homes... not subusers
            if (is_null($user['users_parent'])) {
                echo "<option value='" . $user['user_id'] . "'>" . $user['users_login'] . "</option>\n";
            }
        }
        echo "</select>\n</td></tr>";
        // Select permisions
        echo "<tr><td class='right'>" . get_lang('access_rights') . ":</td>\n\t\t\t<td class='left'>";
        foreach ($selections as $selection => $flag) {
            echo create_selection($selection, $flag);
        }
        echo "</td></tr>";
        // Assign home
        echo "<tr><td align='center' colspan='2'>\n\t\t\t  <input type='hidden' name='rserver_id' value='" . $rhost_id . "' />" . "<input type='submit' name='add_game_home' value='" . get_lang('add_game_home') . "' /></td></tr></table>";
        echo "</form>";
    }
}