function exec_ogp_module()
{
    global $db;
    print "<h2>" . get_lang('modules') . "</h2>";
    print '<a href="?m=modulemanager&amp;p=update">' . get_lang('update_modules') . '</a>';
    $modules = $db->getInstalledModules();
    if ($modules === FALSE) {
        print_failure(get_lang('no_installed_modules'));
        return;
    }
    print "<p class='note'>" . get_lang("not_complete") . "</p>";
    print "<table class='center'><tr class='first_row'><td>" . get_lang('module_id') . "</td>\n        <td>" . get_lang('module_name') . "</td><td>" . get_lang('module_folder') . "</td>\n        <td>" . get_lang('module_version') . "</td><td>" . get_lang('db_version') . "</td>\n        <td></td></tr>";
    require_once 'modules/modulemanager/module_handling.php';
    $installed_modules = array();
    $i = 0;
    foreach ($modules as $row) {
        print "<tr class='tr" . $i++ % 2 . "'><td>" . $row['id'] . "</td>";
        print "<td>" . $row['title'] . "</td><td>" . $row['folder'] . "</td><td>" . $row['version'] . "</td><td>" . $row['db_version'] . "</td>";
        print "<td><a href='?m=modulemanager&amp;p=del&amp;id=" . $row['id'] . "&amp;module=" . $row['folder'] . "'>";
        print get_lang('delete') . "</a></td></tr>\n";
        array_push($installed_modules, $row['folder']);
    }
    print "</table>";
    $not_installed = array_diff(list_available_modules(), $installed_modules);
    if (empty($not_installed)) {
        return;
    }
    print "<h3>" . get_lang('modules_available_for_install') . "</h3>";
    echo "<table class='center'><tr class='first_row'><td>" . get_lang('module_folder') . "</td><td></td></tr>";
    foreach ($not_installed as $available_module) {
        echo "<tr><td>" . $available_module . "</td><td>";
        echo "<a href='?m=modulemanager&amp;p=add&amp;module=" . $available_module . "'>";
        echo get_lang('install') . "</a></td></tr>";
    }
    echo "</table>";
}
function install()
{
    $step = isset($_REQUEST['step']) ? $_REQUEST['step'] : "0";
    if ($step == "0") {
        $locale_files = makefilelist("lang/", ".|..|.svn", true, "folders");
        $counter = 0;
        $columns = 3;
        $width = round(100 / $columns);
        echo "<div id=\"install-title\" style=\"margin-top:-4px;\">" . get_lang('install_lang') . "</div>";
        echo "<table class='lang' style=\"margin-bottom:10px;\">\n<tr>";
        for ($i = 0; $i < count($locale_files); $i++) {
            if ($counter != 0 && $counter % $columns == 0) {
                echo "</tr>\n<tr>\n";
            }
            echo "<td style='width:" . $width . "%' >";
            if ($locale_files[$i] == $_SESSION['users_lang']) {
                echo "<li><b>" . $locale_files[$i] . "</b></li>";
            } else {
                echo "<li><a href='?localeset=" . $locale_files[$i] . "'>" . $locale_files[$i] . "</a></li>";
            }
            echo "</td>\n";
            $counter++;
        }
        echo "</tr>\n</table>\n";
        echo "<div id=\"install-title\">" . get_lang('install_welcome') . "</div>";
        echo "<h3>" . get_lang('file_permission_check') . ":</h3>";
        $failed = false;
        echo "<table class='install'>\n";
        // config.inc.php is checked seperately because we need to check first if the file
        // exists or not.
        $value = 'includes/config.inc.php';
        if (!is_file($value)) {
            @($control = fopen($value, "w+"));
            if ($control == false) {
                echo "<tr><td>" . $value . "</td><td><span class='failure'>" . get_lang('create_an_empty_file') . "</span></td></tr>";
                $failed = true;
            }
        } else {
            if (!is_writable($value)) {
                echo "<tr><td>" . $value . "</td><td><span class='failure'>" . get_lang('write_permission_required') . "</span></td></tr>";
                $failed = true;
            } else {
                echo "<tr><td>" . $value . "</td><td><span class='success'>" . get_lang('OK') . "</span></td></tr>";
            }
        }
        // Check if the folder "modules/TS3Admin/templates_c" is writable
        $value = 'modules/TS3Admin/templates_c';
        if (!is_writable($value)) {
            echo "<tr><td>" . $value . "</td><td><span class='failure'>" . get_lang('write_permission_required') . "</span></td></tr>";
            $failed = true;
        } else {
            echo "<tr><td>" . $value . "</td><td><span class='success'>" . get_lang('OK') . "</span></td></tr>";
        }
        echo "</table>";
        echo "<h3>" . get_lang('php_version_check') . "</h3>\n";
        echo "<table class='install'>";
        echo "<tr><td>PHP Version >= " . REQUIRED_PHP_VERSION . "</td><td>";
        if (version_compare(PHP_VERSION, REQUIRED_PHP_VERSION, ">=")) {
            echo "<span class='success'>" . PHP_VERSION . "</span>";
        } else {
            echo "<span class='failure'>" . PHP_VERSION . "</span>";
            $failed = true;
        }
        echo "</td></tr></table>";
        /* TODO: how to check if pear is enabled or not? */
        $properties_to_check = array(array("name" => "PHP XML-RPC module", "type" => "f", "value" => "xmlrpc_server_create"), array("name" => "PHP Curl module", "type" => "f", "value" => "curl_init"), array("name" => "PHP XML Reader", "type" => "c", "value" => "XMLReader"), array("name" => "PHP JSON Extension", "type" => "f", "value" => "json_decode"), array("name" => "PHP mbstring Extension", "type" => "x", "value" => "mbstring"));
        echo "<h3>" . get_lang('checking_required_modules') . "</h3>\n<table class='install'>";
        foreach ($properties_to_check as $propertie) {
            if ($propertie['type'] === "f" && function_exists($propertie['value']) || $propertie['type'] === "c" && class_exists($propertie['value']) || $propertie['type'] === "x" && extension_loaded($propertie['value'])) {
                echo "<tr><td>" . $propertie['name'] . "</td>\n                        <td><span class='success'>" . get_lang('found') . "</span></td></tr>";
            } else {
                echo "<tr><td>" . $propertie['name'] . "</td>\n                        <td><span class='failure'>" . get_lang('not_found') . "</span></td></tr>";
                $failed = true;
            }
        }
        echo "<tr><td>Pear XXTEA</td><td>";
        $xxtea_found = false;
        $pear_found = false;
        // Lets search for XXTEA pear module from include path.
        $include_paths = explode(PATH_SEPARATOR, get_include_path());
        foreach ($include_paths as $include_path) {
            if (file_exists($include_path . "/" . "Crypt/XXTEA.php")) {
                $xxtea_found = true;
            }
            // Pear always includes System.php file that should be found from the include path.
            if (file_exists($include_path . "/" . "System.php")) {
                $pear_found = true;
            }
        }
        if ($xxtea_found) {
            print_success(get_lang('found'));
        } else {
            print_failure(get_lang('not_found'));
            echo "<p class='info'>" . get_lang('pear_xxtea_info') . "</p>";
            $failed = true;
        }
        echo "</td></tr>";
        echo "<tr><td>Pear</td><td>";
        if ($pear_found) {
            print_success(get_lang('found'));
        } else {
            print_failure(get_lang('not_found'));
            $failed = true;
        }
        echo "</td></tr>";
        echo "<tr><td>file_get_contents()</td><td>";
        if (is_function_available('file_get_contents')) {
            print_success(get_lang('found'));
        } else {
            print_failure(get_lang('not_found'));
            $failed = true;
        }
        echo "</td></tr>";
        echo "<tr><td>allow_url_fopen=on</td><td>";
        if (is_function_available('allow_url_fopen')) {
            print_success(get_lang('found'));
        } else {
            print_failure(get_lang('not_found'));
            $failed = true;
        }
        echo "</td></tr>";
        echo "</table>\n";
        if ($failed) {
            echo "<p><a href='?'>" . get_lang('refresh') . "</a></p>\n";
        } else {
            echo "<p><a href='?step=1'>" . get_lang('next') . "</a></p>\n";
        }
        echo "</td></tr></table>\n";
    } else {
        if ($step == "1") {
            echo "<table class='install'><tr><td>\n";
            if (is_readable('includes/config.inc.php')) {
                require_once "includes/config.inc.php";
            }
            echo "<form name='setup' method='post' action='?step=2'>";
            echo "<table class='install'>\n";
            echo "<tr><td colspan='2'><div id=\"install-title\" style=\"margin-left:-21px; margin-top:-7px;\">" . get_lang('database_settings') . "</div></td></tr>\n            <tr><td>" . get_lang('database_type') . ":</td><td>MySQL</td></tr>\n            <tr><td>" . get_lang('database_hostname') . ":</td>\n            <td><input type='text' value='";
            echo isset($db_host) ? $db_host : "localhost";
            echo "' name='db_host' class='textbox' /></td></tr>\n            <tr><td>" . get_lang('database_username') . ":</td>\n            <td><input type='text' value='";
            echo isset($db_user) ? $db_user : "";
            echo "' name='db_user' class='textbox' /></td></tr>\n            <tr><td>" . get_lang('database_password') . ":</td>\n            <td><input type='password' value='";
            echo isset($db_pass) ? $db_pass : "";
            echo "' name='db_pass' class='textbox' /></td></tr>\n            <tr><td>" . get_lang('database_name') . ":</td>\n            <td><input type='text' value='";
            echo isset($db_name) ? $db_name : "";
            echo "' name='db_name' class='textbox' /></td></tr>";
            echo "<tr><td>" . get_lang('database_prefix') . ":</td>\n            <td><input type='text' value='";
            echo isset($table_prefix) ? $table_prefix : "ogp_";
            echo "' name='table_prefix' class='textbox' /></td></tr>";
            echo "</table>\n\n            <p><input type='submit' name='next' value='" . get_lang('next') . "' class='button' /></p></form>";
            echo "<p><a href='?step=0'>" . get_lang('back') . "</a></p>";
            echo "</td></tr></table>\n";
        } else {
            if ($step == "2") {
                echo "<table class='install'><tr><td>\n";
                if (isset($_POST['db_host'])) {
                    $db_host = stripinput($_POST['db_host']);
                    $db_user = stripinput($_POST['db_user']);
                    $db_pass = stripinput($_POST['db_pass']);
                    $db_name = stripinput($_POST['db_name']);
                    $table_prefix = stripinput($_POST['table_prefix']);
                    $db_type = "mysql";
                    $config = "<?php\n" . "###############################################\n" . "# Site configuration\n" . "###############################################\n" . "\$db_host=\"" . $db_host . "\";\n" . "\$db_user=\"" . $db_user . "\";\n" . "\$db_pass=\"" . $db_pass . "\";\n" . "\$db_name=\"" . $db_name . "\";\n" . "\$table_prefix=\"" . $table_prefix . "\";\n" . "\$db_type=\"" . $db_type . "\";\n" . "?>";
                    $temp = @fopen("includes/config.inc.php", "w");
                    if (!@fwrite($temp, $config)) {
                        print_failure(get_lang('unable_to_write_config'));
                        echo "<p><a href='?step=0'>" . get_lang('back') . "</a></p>";
                        fclose($temp);
                        return;
                    }
                    fclose($temp);
                }
                require_once "includes/config.inc.php";
                $db = createDatabaseConnection($db_type, $db_host, $db_user, $db_pass, $db_name, $table_prefix);
                $error_text = "";
                if (get_db_error_text($db, $error_text)) {
                    print_failure($error_text);
                    echo "<p><a href='?step=1'>" . get_lang('back') . "</a></p>";
                    return;
                }
                $fail = false;
                // These belong to module manager, but they need to be created before other modules can be "installed".
                $result = $db->query("DROP TABLE IF EXISTS " . $table_prefix . "modules");
                $result = $db->query("CREATE TABLE IF NOT EXISTS `" . $table_prefix . "modules` (\n            `id` smallint(5) unsigned NOT NULL auto_increment,\n            `title` varchar(100) NOT NULL default '',\n            `folder` varchar(100) NOT NULL default '',\n            `version` varchar(10) NOT NULL default '0',\n            `db_version` int(10) NOT NULL default '0',\n            PRIMARY KEY  (`id`),\n        UNIQUE KEY `folder` (`folder`)\n    ) ENGINE=MyISAM  DEFAULT CHARSET=latin1;");
                $result = $db->query("DROP TABLE IF EXISTS " . $table_prefix . "module_menus");
                $result = $db->query("CREATE TABLE IF NOT EXISTS `" . $table_prefix . "module_menus` (\n            `module_id` int(11) NOT NULL COMMENT 'This references to modules.id',\n            `subpage` varchar(64) NOT NULL default '',\n            `group` varchar(32) NOT NULL,\n            `menu_name` varchar(128) NOT NULL,\n\t\t\t`pos` INT UNSIGNED NOT NULL,\n            PRIMARY KEY  (`module_id`,`subpage`,`group`)\n        ) ENGINE=MyISAM DEFAULT CHARSET=latin1;");
                if (!$result) {
                    $fail = true;
                }
                // Install modules.
                require_once "modules/modulemanager/module_handling.php";
                @add_lang_module('modulemanager');
                $modules = list_available_modules();
                foreach ($modules as $module) {
                    $fail = $fail || install_module($db, $module, FALSE) < 0;
                }
                if ($fail) {
                    print_failure(get_lang('database_setup_failure'));
                    echo "<p><a href='?step=1'>" . get_lang('back') . "</a></p>";
                    echo "<p>" . get_lang('unable_to_resolve') . " <a href='http://www.opengamepanel.org/'>http://www.opengamepanel.org</a></p>";
                    return;
                }
                print_success(get_lang('config_written'));
                print_success(get_lang('database_created'));
                echo "<form name='setup' method='post' action='?'>\n\n            <input type='hidden' name='step' value='3' />";
                echo "<table class='install'>\n";
                echo "<tr><td colspan='2'><div id=\"install-title\" style=\"margin-left:-21px;\">" . get_lang('admin_login_details') . "</div>";
                echo "<p>" . get_lang('admin_login_details_info') . "</p></td></tr>";
                echo "<tr><td>" . get_lang('username') . ":</td><td><input type='text' name='username' maxlength='30' class='textbox' /></td></tr>";
                echo "<tr><td>" . get_lang('password') . ":</td><td><input type='password' name='password1' maxlength='20' class='textbox' /></td></tr>";
                echo "<tr><td>" . get_lang('repeat_password') . ":</td><td><input type='password' name='password2' maxlength='20' class='textbox' /></td></tr>";
                echo "<tr><td>" . get_lang('email') . ":</td><td><input type='text' name='email' maxlength='100' class='textbox' /></td></tr>";
                echo "</table>\n";
                echo "<p><input type='submit' name='next' value='" . get_lang('next') . "' class='button' /></p></form>\n";
                echo "<p><a href='?step=1'>" . get_lang('back') . "</a></p>";
                echo "</td></tr></table>\n";
            } else {
                if ($step == "3") {
                    echo "<table class='install'><tr><td>\n";
                    require_once "includes/config.inc.php";
                    $db = createDatabaseConnection($db_type, $db_host, $db_user, $db_pass, $db_name, $table_prefix);
                    $error = "";
                    $username = stripinput($_POST['username']);
                    $password1 = stripinput($_POST['password1']);
                    $password2 = stripinput($_POST['password2']);
                    $email = stripinput($_POST['email']);
                    if (!preg_match("/^[-0-9A-Z_@\\s]+\$/i", $username)) {
                        print_failure(get_lang('invalid_username'));
                        echo "<p><a href='?step=2'>" . get_lang('back') . "</a></p>";
                        return;
                    }
                    // TODO: replace with a constant
                    if (strlen($password1) < 6) {
                        print_failure(get_lang_f('password_too_short', 6));
                        echo "<p><a href='?step=2'>" . get_lang('back') . "</a></p>";
                        return;
                    }
                    if (!preg_match("/^[0-9A-Z@]{6,20}\$/i", $password1)) {
                        print_failure(get_lang('password_contains_invalid_characters'));
                        echo "<p><a href='?step=2'>" . get_lang('back') . "</a></p>";
                        return;
                    }
                    if ($password1 != $password2) {
                        print_failure(get_lang('password_mismatch'));
                        echo "<p><a href='?step=2'>" . get_lang('back') . "</a></p>";
                        return;
                    }
                    if (!preg_match("/^[-0-9A-Z_\\.]{1,50}@([-0-9A-Z_\\.]+\\.){1,50}([0-9A-Z]){2,4}\$/i", $email)) {
                        print_failure(get_lang('invalid_email_address'));
                        echo "<p><a href='?step=2'>" . get_lang('back') . "</a></p>";
                        return;
                    }
                    //detect nighly builds, if not its SVN
                    if (file_exists("version.txt")) {
                        $file = "version.txt";
                        $contents = file($file);
                        $nversion = implode($contents);
                        $nversion2 = substr($nversion, 60);
                        $nversion2 = trim($nversion2);
                        $site_settings = array("title" => "Open Game Panel", "slogan" => "" . get_lang('slogan') . "", "ogp_version" => "{$nversion2}", "version_type" => "SVN", "theme" => "Revolution", "welcome_title" => "1", "welcome_title_message" => "<h0>" . get_lang('welcome_title_message') . "</h0>", "page_auto_refresh" => "1");
                        unlink('version.txt');
                    } else {
                        $site_settings = array("title" => "Open Game Panel", "slogan" => "" . get_lang('slogan') . "", "ogp_version" => "0", "version_type" => "SVN", "theme" => "Revolution", "welcome_title" => "1", "welcome_title_message" => "<h0>" . get_lang('welcome_title_message') . "</h0>", "page_auto_refresh" => "1");
                    }
                    $result = $db->setSettings($site_settings);
                    $result = $db->addUser($username, $password1, "admin", $email);
                    print_success(get_lang('setup_complete'));
                    echo "<p class='note'>" . get_lang('remove_install_and_secure_config') . "</p>";
                    echo "<p class='note'><a href='index.php'>" . get_lang('go_to_panel') . "</a></p>";
                    echo "</td></tr></table>\n";
                    echo "</div>\n";
                }
            }
        }
    }
}