function exec_ogp_module()
{
    require_once MODULES . "/litefm/litefm.php";
    $home_id = $_REQUEST['home_id'];
    if (empty($home_id)) {
        print_failure(get_lang('home_id_missing'));
        return;
    }
    if (litefm_check($home_id) === FALSE) {
        return;
    }
    global $db;
    $isAdmin = $db->isAdmin($_SESSION['user_id']);
    if ($isAdmin) {
        $home_cfg = $db->getGameHome($home_id);
    } else {
        $home_cfg = $db->getUserGameHome($_SESSION['user_id'], $home_id);
    }
    if ($home_cfg === FALSE) {
        print_failure(get_lang('no_access_to_home'));
        return;
    }
    echo "<table class='center' style='width:100%;'>" . show_back($home_id) . "</table>";
    //Logic to open the file we're editing
    $remote = new OGPRemoteLibrary($home_cfg['agent_ip'], $home_cfg['agent_port'], $home_cfg['encryption_key']);
    $data = "";
    $file_info = $remote->remote_readfile($home_cfg['home_path'] . "/" . $_SESSION['fm_cwd_' . $home_id], $data);
    if ($file_info === 0) {
        print_failure(get_lang("not_found"));
        return;
    } else {
        if ($file_info === -1) {
            print_failure(get_lang("agent_offline"));
            return;
        } else {
            if ($file_info === -2) {
                print_failure(get_lang("failed_read"));
                return;
            }
        }
    }
    echo "<form action='?m=litefm&amp;p=write_file' method='post'>";
    echo "<input type='hidden' name='home_id' value='{$home_id}' />";
    echo "<textarea name='file_content' style='width:98%;' rows='40'>{$data}</textarea>";
    echo "<p><input type='submit' name='save_file' value='" . get_lang('save') . "' /></p>";
    echo "</form>";
    show_back($home_id);
}
function exec_ogp_module()
{
    global $view;
    global $db;
    echo "<h2>" . get_lang('view_log') . "</h2>";
    $rhost_id = @$_REQUEST['rhost_id'];
    $remote_server = $db->getRemoteServer($rhost_id);
    $remote = new OGPRemoteLibrary($remote_server['agent_ip'], $remote_server['agent_port'], $remote_server['encryption_key']);
    if (isset($_POST['save_file'])) {
        $file_info = $remote->remote_writefile('./ogp_agent.log', $_REQUEST['file_content']);
        if ($file_info === 1) {
            print_success(get_lang('wrote_changes'));
        } else {
            if ($file_info === 0) {
                print_failure(get_lang('failed_write'));
            } else {
                print_failure(get_lang("agent_offline"));
            }
        }
    }
    $data = "";
    $file_info = $remote->remote_readfile('./ogp_agent.log', $data);
    if ($file_info === 0) {
        print_failure(get_lang("not_found"));
        return;
    } else {
        if ($file_info === -1) {
            print_failure(get_lang("agent_offline"));
            return;
        } else {
            if ($file_info === -2) {
                print_failure(get_lang("failed_read"));
                return;
            }
        }
    }
    echo "<form action='?m=server&amp;p=log&amp;rhost_id=" . $rhost_id . "' method='post'>";
    echo "<textarea name='file_content' style='width:98%;' rows='40'>{$data}</textarea>";
    echo "<p><input type='submit' name='save_file' value='Save' /></p>";
    echo "</form>";
}
function exec_ogp_module()
{
    global $db;
    global $view;
    $pieces = explode("-", $_GET['home_id-mod_id-ip-port']);
    $home_id = $pieces[0];
    $mod_id = $pieces[1];
    $ip = $pieces[2];
    $port = $pieces[3];
    $user_id = $_SESSION['user_id'];
    $isAdmin = $db->isAdmin($user_id);
    if ($isAdmin) {
        $home_info = $db->getGameHome($home_id);
    } else {
        $home_info = $db->getUserGameHome($user_id, $home_id);
    }
    if ($home_info === FALSE) {
        print_failure(get_lang('no_access_to_home'));
        return;
    }
    $server_xml = read_server_config(SERVER_CONFIG_LOCATION . "/" . $home_info['home_cfg_file']);
    if (!$server_xml) {
        echo create_back_button($_GET['m'], 'game_monitor&home_id-mod_id-ip-port=' . $_GET['home_id-mod_id-ip-port']);
        return;
    }
    require_once 'includes/lib_remote.php';
    $remote = new OGPRemoteLibrary($home_info['agent_ip'], $home_info['agent_port'], $home_info['encryption_key']);
    $home_log = "";
    if (isset($server_xml->console_log)) {
        $log_retval = $remote->remote_readfile($home_info['home_path'] . '/' . $server_xml->console_log, $home_log);
    } else {
        $log_retval = $remote->get_log(OGP_SCREEN_TYPE_HOME, $home_info['home_id'], clean_path($home_info['home_path'] . "/" . $server_xml->exe_location), $home_log);
    }
    if ($log_retval == 0) {
        print_failure(get_lang('agent_offline'));
        echo create_back_button($_GET['m'], 'game_monitor&home_id-mod_id-ip-port=' . $_GET['home_id-mod_id-ip-port']);
    } elseif ($log_retval == 1 || $log_retval == 2) {
        // Using the refreshed class
        if (isset($_GET['refreshed'])) {
            echo "<pre class='log'>" . $home_log . "</pre>";
        } else {
            echo "<h2>" . $home_info['home_name'] . "</h2>";
            require_once "includes/refreshed.php";
            $control = '<form method="POST" >
						<input type="submit" name="';
            if (isset($_POST['full'])) {
                $height = "100%";
                $control .= 'default" value="-';
            } else {
                $height = "500px";
                $control .= 'full" value="+';
            }
            $control .= '" /></form><br />';
            $intervals = array("4s" => "4000", "8s" => "8000", "30s" => "30000", "2m" => "120000", "5m" => "300000");
            $intSel = '<form action="" method="GET" >
					   <input type="hidden" name="m" value="gamemanager" />
					   <input type="hidden" name="p" value="log" />
					   <input type="hidden" name="home_id-mod_id-ip-port" value="' . $_GET['home_id-mod_id-ip-port'] . '" />' . get_lang('refresh_interval') . ':<select name="setInterval" onchange="this.form.submit();">';
            foreach ($intervals as $interval => $value) {
                $selected = "";
                if (isset($_GET['setInterval']) and $_GET['setInterval'] == $value) {
                    $selected = 'selected="selected"';
                }
                $intSel .= '<option value="' . $value . '" ' . $selected . ' >' . $interval . '</option>';
            }
            $intSel .= "</select></form>";
            $setInterval = $_GET['setInterval'] ? $_GET['setInterval'] : 4000;
            $refresh = new refreshed();
            $pos = $refresh->add("home.php?m=gamemanager&p=log&type=cleared&refreshed&home_id-mod_id-ip-port=" . $_GET['home_id-mod_id-ip-port']);
            echo $refresh->getdiv($pos, "height:" . $height . ";overflow:auto;max-width:1600px;");
            ?>
<script type="text/javascript">$(document).ready(function(){ <?php 
            echo $refresh->build("{$setInterval}");
            ?>
} ); </script><?php 
            echo "<table class='center' ><tr><td>{$intSel}</td><td>{$control}</td></tr></table>";
            if ($remote->is_screen_running(OGP_SCREEN_TYPE_HOME, $home_info['home_id']) != 1) {
                print_failure(get_lang('server_not_running'));
            } else {
                if ($server_xml->control_protocol == 'rcon' or $server_xml->control_protocol == 'rcon2' or @$server_xml->gameq_query_name == "minecraft" or $server_xml->control_protocol == 'lcon' or isset($server_xml->lgsl_query_name) and $server_xml->lgsl_query_name == "7dtd") {
                    require 'modules/gamemanager/rcon.php';
                }
            }
            echo create_back_button($_GET['m'], 'game_monitor&home_id-mod_id-ip-port=' . $_GET['home_id-mod_id-ip-port']);
        }
    } else {
        print_failure(get_lang_f('unable_to_get_log', $log_retval));
        echo create_back_button($_GET['m'], 'game_monitor&home_id-mod_id-ip-port=' . $_GET['home_id-mod_id-ip-port']);
    }
}
function exec_ogp_module()
{
    global $db;
    global $view;
    $home_id = $_REQUEST['home_id'];
    $isAdmin = $db->isAdmin($_SESSION['user_id']);
    if ($isAdmin) {
        $home_info = $db->getGameHome($home_id);
    } else {
        $home_info = $db->getUserGameHome($_SESSION['user_id'], $home_id);
    }
    if ($home_info == FALSE) {
        print_failure(get_lang('no_rights_to_start_server'));
        echo "<table class='center'><tr><td><a href='?m=gamemanager&amp;p=game_monitor&amp;home_id-mod_id-ip-port=" . $home_info['home_id'] . "-" . $home_info['mod_id'] . "-" . $_REQUEST['ip'] . "-" . $_REQUEST['port'] . "'><< " . get_lang('back') . "</a></td></tr></table>";
        return;
    }
    $mod_id = $_REQUEST['mod_id'];
    if (!array_key_exists($mod_id, $home_info['mods'])) {
        print_failure("Unable to retrieve mod information from database.");
        return;
    }
    echo "<h2>";
    echo empty($home_info['home_name']) ? get_lang('not_available') : $home_info['home_name'];
    echo "</h2>";
    require_once 'includes/lib_remote.php';
    $remote = new OGPRemoteLibrary($home_info['agent_ip'], $home_info['agent_port'], $home_info['encryption_key']);
    $server_xml = read_server_config(SERVER_CONFIG_LOCATION . "/" . $home_info['home_cfg_file']);
    if (!$server_xml) {
        echo "<table class='center'><tr><td><a href='?m=gamemanager&amp;p=game_monitor&amp;home_id-mod_id-ip-port=" . $home_info['home_id'] . "-" . $home_info['mod_id'] . "-" . $_REQUEST['ip'] . "-" . $_REQUEST['port'] . "'><< " . get_lang('back') . "</a></td></tr></table>";
        return;
    }
    // It compares ip and port on POST with the pair on DB for security reasons (URL HACKING)
    $home_id = $home_info['home_id'];
    $ip_info = $db->getHomeIpPorts($home_id);
    foreach ($ip_info as $ip_ports_row) {
        if ($ip_ports_row['ip'] == $_REQUEST['ip'] && $ip_ports_row['port'] == $_REQUEST['port']) {
            $ip = $ip_ports_row['ip'];
            $port = $ip_ports_row['port'];
        }
    }
    if (!isset($ip) or !isset($port)) {
        echo "<h2>" . get_lang_f('ip_port_pair_not_owned') . "</h2>";
        echo "<table class='center'><tr><td><a href='?m=gamemanager&amp;p=game_monitor&amp;home_id-mod_id-ip-port={$home_id}-{$mod_id}-{$ip}-{$port}'><< " . get_lang('back') . "</a></td></tr></table>";
        return;
    }
    if (isset($server_xml->console_log)) {
        $log_retval = $remote->remote_readfile($home_info['home_path'] . '/' . $server_xml->console_log, $home_log);
    } else {
        $log_retval = $remote->get_log(OGP_SCREEN_TYPE_HOME, $home_info['home_id'], clean_path($home_info['home_path'] . "/" . $server_xml->exe_location), $home_log);
    }
    function getLastLines($string, $n = 1)
    {
        $lines = explode("\n", $string);
        $lines = array_slice($lines, -$n);
        return implode("\n", $lines);
    }
    $home_log = getLastLines($home_log, 40);
    if ($log_retval > 0) {
        if ($log_retval == 2) {
            print_failure(get_lang('server_not_running_log_found'));
        }
        echo "<pre style='background:black;color:white;'>" . $home_log . "</pre>";
        if ($log_retval == 2) {
            return;
        }
    } else {
        print_failure(get_lang_f('unable_to_get_log', $log_retval));
    }
    // If game is not supported by lgsl we skip the lgsl checks and
    // assume successfull start.
    if ($home_info['use_nat'] == 1) {
        $query_ip = $home_info['agent_ip'];
    } else {
        $query_ip = $ip;
    }
    $running = $remote->is_screen_running(OGP_SCREEN_TYPE_HOME, $home_info['home_id']);
    if ($server_xml->lgsl_query_name) {
        require 'protocol/lgsl/lgsl_protocol.php';
        $get_q_and_s = lgsl_port_conversion((string) $server_xml->lgsl_query_name, $port, "", "");
        //Connection port
        $c_port = $get_q_and_s['0'];
        //query port
        $q_port = $get_q_and_s['1'];
        //software port
        $s_port = $get_q_and_s['2'];
        $data = lgsl_query_live((string) $server_xml->lgsl_query_name, $query_ip, $c_port, $q_port, $s_port, "sa");
        if ($data['b']['status'] == "0") {
            $running = FALSE;
        }
    } elseif ($server_xml->gameq_query_name) {
        require 'protocol/GameQ/GameQ.php';
        $query_port = get_query_port($server_xml, $port);
        $servers = array(array('id' => 'server', 'type' => (string) $server_xml->gameq_query_name, 'host' => $query_ip . ":" . $query_port));
        $gq = new GameQ();
        $gq->addServers($servers);
        $gq->setOption('timeout', 4);
        $gq->setOption('debug', FALSE);
        $gq->setFilter('normalise');
        $game = $gq->requestData();
        if (!$game['server']['gq_online']) {
            $running = FALSE;
        }
    }
    if (!$running) {
        if (!isset($_GET['retry'])) {
            $retry = 0;
        } else {
            $retry = $_GET['retry'];
        }
        if ($retry >= 5) {
            echo "<p>" . get_lang('server_running_not_responding') . "\n\t\t\t<a href=?m=gamemanager&amp;p=stop&amp;home_id=" . $home_info['home_id'] . "&amp;ip=" . $ip . "&amp;port=" . $port . ">" . get_lang('already_running_stop_server') . ".</a></p>";
            echo "<table class='center'><tr><td><a href='?m=gamemanager&amp;p=game_monitor&amp;home_id-mod_id-ip-port={$home_id}-{$mod_id}-{$ip}-{$port}'><< " . get_lang('back') . "</a></td></tr></table>";
        }
        echo "</b>Retry #" . $retry . ".</b>";
        $retry++;
        print "<p class='note'>" . get_lang('starting_server') . "</p>";
        $view->refresh("?m=gamemanager&amp;p=start&amp;refresh&amp;ip={$ip}&amp;port={$port}&amp;home_id={$home_id}&amp;mod_id={$mod_id}&amp;retry=" . $retry, 3);
        return;
    }
    print_success(get_lang('server_started'));
    $ip_id = $db->getIpIdByIp($ip);
    $db->delServerStatusCache($ip_id, $port);
    $view->refresh("?m=gamemanager&amp;p=game_monitor&amp;home_id-mod_id-ip-port={$home_id}-{$mod_id}-{$ip}-{$port}");
    return;
}
            $replacements[$replace_id]['options'] = (string) $value;
        }
    }
    $replace_id++;
}
foreach ($replacements as $key => $replacement) {
    $filepath = $replacement['filepath'];
    $file_replacements[$filepath][$key] = $replacement;
}
/* echo "<xmp>";
print_r($file_replacements);
echo "</xmp>"; */
require_once 'includes/lib_remote.php';
$remote = new OGPRemoteLibrary($server_home['agent_ip'], $server_home['agent_port'], $server_home['encryption_key']);
foreach ($file_replacements as $filepath => $replacements) {
    $file_info = $remote->remote_readfile($server_home['home_path'] . "/{$filepath}", $file_content);
    if ($file_info === 0) {
        $remote->exec("touch " . $server_home['home_path'] . "/{$filepath}");
        $file_info = "";
    }
    foreach ($replacements as $replacement) {
        $info_param = $replacement['info_param'];
        $default = $replacement['default'];
        $var = $replacement['var'];
        $options = $replacement['options'];
        $param_enc = mb_detect_encoding($info_param);
        $file_enc = mb_detect_encoding($file_content);
        if ($param_enc != $file_enc) {
            $file_content = mb_convert_encoding($file_content, $param_enc, $file_enc);
        }
        if (!in_array($options, array("tags", "tagValueByName", "sc", "sqc"))) {
function exec_ogp_module()
{
    global $db;
    $home_id = $_REQUEST['home_id'];
    $server_row = $db->getGameHomeWithoutMods($home_id);
    if (empty($server_row)) {
        print_failure(get_lang('invalid_home_id'));
        return;
    }
    echo "<h2>" . get_lang_f('cloning_home', $server_row['home_name']) . "</h2>";
    echo create_back_button('user_games');
    include 'includes/lib_remote.php';
    $remote = new OGPRemoteLibrary($server_row['agent_ip'], $server_row['agent_port'], $server_row['encryption_key']);
    if (isset($_REQUEST['clone_home'])) {
        $server_name = $_POST['new_home_name'];
        $user_group = $_POST['user_group'];
        $clone_home_id = $db->addGameHome($server_row['remote_server_id'], $server_row['user_id_main'], $server_row['home_cfg_id'], "/home/" . $server_row['ogp_user'] . "/OGP_User_Files/", $server_name, '', genRandomString(8));
        $server_path = "/home/" . $server_row['ogp_user'] . "/OGP_User_Files/" . $clone_home_id;
        if ($clone_home_id === FALSE) {
            print_failure(get_lang_f('cloning_of_home_failed', $home_id));
            return;
        }
        if (isset($_REQUEST['clone_mods'])) {
            $enabled_mods = $db->getHomeMods($home_id);
            if (empty($enabled_mods)) {
                print_failure(get_lang('note') . ": " . get_lang('no_mods_to_clone'));
            } else {
                foreach ($enabled_mods as $enabled_rows) {
                    if ($db->addModToGameHome($clone_home_id, $enabled_rows['mod_cfg_id']) === FALSE) {
                        print_failure(get_lang_f('failed_to_add_mod', $enabled_rows['mod_cfg'], $clone_home_id));
                        return;
                    }
                    if ($db->updateGameModParams($enabled_rows['max_players'], $enabled_rows['extra_params'], $enabled_rows['cpu_affinity'], $enabled_rows['nice'], $clone_home_id, $enabled_rows['mod_cfg_id']) === FALSE) {
                        print_failure(get_lang_f('failed_to_update_mod_settings', $clone_home_id));
                        return;
                    }
                }
                print_success(get_lang_f('successfully_cloned_mods', $clone_home_id));
            }
        }
        print_success(get_lang('successfully_copied_home_database'));
        # do the remote copy call here
        echo "<p>" . get_lang_f('copying_home_remotely', $server_row['home_path'], $server_path) . "</p>";
        $db->logger(get_lang_f('copying_home_remotely', $server_row['home_path'], $server_path));
        $clone_rc = $remote->clone_home($server_row['home_path'], $server_path, $user_group);
        if ($clone_rc == -1) {
            print_success(get_lang('game_server_copy_is_running'));
            ?>
			<div class="html5-progress-bar">
			<progress id="progressbar" value="0" max="100"></progress>
			<span class="progress-value">0%</span>
			</div>
			<script type="text/javascript" src="js/jquery-1.7.1.min.js"></script>
			<script type="text/javascript">
			$(document).ready(function() 
			{
				$.get( "home.php?m=user_games&type=cleared&p=get_size&home_id=<?php 
            echo $home_id;
            ?>
&bytes", function( orig_data ) {
					var orig_size = parseInt(orig_data,10);
					var refreshId = setInterval(function() {
						$.get( "home.php?m=user_games&type=cleared&p=get_size&home_id=<?php 
            echo $clone_home_id;
            ?>
&bytes", function( dest_data ) {
							var dest_size = parseInt(dest_data,10);
							var progress = parseInt(dest_size * 100 / orig_size);
							$('#progressbar').val(progress);
							$('.progress-value').html(progress + '%');
							if(progress == 100)
							{
								clearInterval(refreshId);
							}
						});
					}, 2000);
				});
			});
			</script>
			<?php 
        } elseif ($clone_rc == 1) {
            print_success(get_lang('game_server_copy_was_successful'));
        } else {
            print_failure(get_lang_f('game_server_copy_failed_with_return_code', $clone_rc));
        }
        echo "<br><a href='home.php?m=user_games'>&lt;&lt; " . get_lang('back_to_game_servers') . "</a>";
        return;
    }
    // Form to edit game path.
    $avail_mods = $db->getHomeMods($home_id);
    $read_status = $remote->remote_readfile('/etc/passwd', $passwd_array);
    if ($read_status === -1) {
        print_failure(get_lang('agent_offline'));
        return;
    } else {
        if ($read_status == 1) {
            $passwd_array = preg_split("/\n/", $passwd_array);
        }
    }
    require_once "includes/form_table_class.php";
    $ft = new FormTable();
    $ft->start_form('?m=user_games&amp;p=clone');
    $ft->add_field_hidden('home_id', $home_id);
    $ft->start_table();
    $ft->add_custom_field('agent_ip', $server_row['agent_ip']);
    $ft->add_custom_field('current_home_path', $server_row['home_path']);
    $ft->add_field('string', 'new_home_name', $server_row['home_name']);
    echo "<tr><td class='right'>" . get_lang('clone_mods') . ":</td>\n\t\t<td class='left'><input type='checkbox' name='clone_mods' value='y' /></td></tr>";
    echo "<input name='user_group' type='hidden' value='" . get_user_uid_gid_from_passwd($passwd_array, $server_row['ogp_user']) . "' /></tr>";
    echo "</table>";
    $ft->add_button('submit', 'clone_home', get_lang('clone_home'));
    echo "<p class='info'>" . get_lang('the_name_of_the_server_to_help_users_to_identify_it') . "</p>";
    echo "</form>";
    echo "<h3>" . get_lang('ips_and_ports_used_in_this_home') . "</h3>";
    echo "<p>" . get_lang('note_ips_and_ports_are_not_cloned') . "</p>";
    $assigned = $db->getHomeIpPorts($home_id);
    if (!empty($assigned)) {
        foreach ($assigned as $assigned_rows) {
            echo "<p>" . $assigned_rows['ip'] . ":" . $assigned_rows['port'] . "</p>\n";
        }
    }
    $enabled_mods = $db->getHomeMods($home_id);
    echo "<h3>" . get_lang('mods_and_settings_for_this_game_server') . "</h3>";
    if (empty($enabled_mods)) {
        print_failure(get_lang('note') . ": " . get_lang('note_no_mods'));
        return;
    }
    echo "<table class='center'>\n";
    echo "<tr><td>" . get_lang('mod_name') . "</td><td>" . get_lang('max_players') . "</td><td>" . get_lang('extra_cmd_line_args') . "</td><td>" . get_lang('cpu_affinity') . "</td><td>" . get_lang('nice_level') . "</td></tr>\n";
    foreach ($enabled_mods as $enabled_rows) {
        echo "<tr>";
        echo "<td>" . $enabled_rows['mod_name'] . "</td>";
        echo "<td>" . $enabled_rows['max_players'] . "</td>";
        echo "<td>" . $enabled_rows['extra_params'] . "</td>";
        echo "<td>" . $enabled_rows['cpu_affinity'] . "</td>";
        echo "<td>" . $enabled_rows['nice'] . "</td></tr>\n";
    }
    echo "</table>\n";
}
function exec_ogp_module()
{
    global $view, $db;
    $ip = $_REQUEST['ip'];
    $port = $_REQUEST['port'];
    $home_id = $_REQUEST['home_id'];
    $user_id = $_SESSION['user_id'];
    $home_id = $_REQUEST['home_id'];
    $isAdmin = $db->isAdmin($_SESSION['user_id']);
    if ($isAdmin) {
        $home_info = $db->getGameHome($home_id);
    } else {
        $home_info = $db->getUserGameHome($_SESSION['user_id'], $home_id);
    }
    require_once 'includes/lib_remote.php';
    $remote = new OGPRemoteLibrary($home_info['agent_ip'], $home_info['agent_port'], $home_info['encryption_key']);
    $mod_id = $_REQUEST['mod_id'];
    if ($home_info === FALSE) {
        print_failure(get_lang('no_rights_to_stop_server'));
        return;
    }
    echo "<h2>";
    echo empty($home_info['home_name']) ? get_lang('not_available') : $home_info['home_name'];
    echo "</h2>";
    $server_xml = read_server_config(SERVER_CONFIG_LOCATION . "/" . $home_info['home_cfg_file']);
    if (!$server_xml) {
        echo create_back_button("gamemanager", "game_monitor");
        return;
    }
    $rserver = $db->getRemoteServerById($home_info['remote_server_id']);
    if (empty($rserver)) {
        print_failure("" . get_lang('not_found_server') . " " . $home_info['remote_server_id'] . ".");
    } else {
        if (isset($_REQUEST['refresh'])) {
            if (isset($server_xml->console_log)) {
                $log_retval = $remote->remote_readfile($home_info['home_path'] . '/' . $server_xml->console_log, $home_log);
            } else {
                $log_retval = $remote->get_log(OGP_SCREEN_TYPE_HOME, $home_info['home_id'], clean_path($home_info['home_path'] . "/" . $server_xml->exe_location), $home_log);
            }
            function getLastLines($string, $n = 1)
            {
                $lines = explode("\n", $string);
                $lines = array_slice($lines, -$n);
                return implode("\n", $lines);
            }
            $home_log = getLastLines($home_log, 40);
            if ($log_retval > 0) {
                if ($log_retval == 2) {
                    print_failure(get_lang('server_not_running_log_found'));
                }
                echo "<pre style='background:black;color:white;'>" . $home_log . "</pre>";
                if ($log_retval == 2) {
                    return;
                }
            } else {
                print_failure(get_lang_f('unable_to_get_log', $log_retval));
            }
            // If game is not supported by lgsl we skip the lgsl checks and
            // assume successfull start.
            if ($home_info['use_nat'] == 1) {
                $query_ip = $home_info['agent_ip'];
            } else {
                $query_ip = $ip;
            }
            $running = TRUE;
            if ($server_xml->lgsl_query_name) {
                require 'protocol/lgsl/lgsl_protocol.php';
                $get_q_and_s = lgsl_port_conversion((string) $server_xml->lgsl_query_name, $port, "", "");
                //Connection port
                $c_port = $get_q_and_s['0'];
                //query port
                $q_port = $get_q_and_s['1'];
                //software port
                $s_port = $get_q_and_s['2'];
                $data = lgsl_query_live((string) $server_xml->lgsl_query_name, $query_ip, $c_port, $q_port, $s_port, "sa");
                if ($data['b']['status'] == "0") {
                    $running = FALSE;
                }
            } elseif ($server_xml->gameq_query_name) {
                require 'protocol/GameQ/GameQ.php';
                $query_port = get_query_port($server_xml, $port);
                $servers = array(array('id' => 'server', 'type' => (string) $server_xml->gameq_query_name, 'host' => $query_ip . ":" . $query_port));
                $gq = new GameQ();
                $gq->addServers($servers);
                $gq->setOption('timeout', 4);
                $gq->setOption('debug', FALSE);
                $gq->setFilter('normalise');
                $game = $gq->requestData();
                if (!$game['server']['gq_online']) {
                    $running = FALSE;
                }
            }
            if (!$running) {
                if (!isset($_GET['retry'])) {
                    $retry = 0;
                } else {
                    $retry = $_GET['retry'];
                }
                if ($retry >= 5) {
                    echo "<p>" . get_lang('server_running_not_responding') . "\n\t\t\t\t\t<a href=?m=gamemanager&amp;p=stop&amp;home_id=" . $home_info['home_id'] . "&amp;ip=" . $ip . "&amp;port=" . $port . ">" . get_lang('already_running_stop_server') . ".</a></p>";
                    echo "<table class='center'><tr><td><a href='?m=gamemanager&amp;p=game_monitor&amp;home_id-mod_id-ip-port={$home_id}-{$mod_id}-{$ip}-{$port}'><< " . get_lang('back') . "</a></td></tr></table>";
                }
                echo "</b>Retry #" . $retry . ".</b>";
                $retry++;
                print "<p class='note'>" . get_lang('starting_server') . "</p>";
                $view->refresh("?m=gamemanager&amp;p=start&amp;refresh&amp;ip={$ip}&amp;port={$port}&amp;home_id={$home_id}&amp;mod_id={$mod_id}&amp;retry=" . $retry, 3);
                return;
            }
            print_success(get_lang_f('server_restarted', $home_info['home_name']));
            $ip_id = $db->getIpIdByIp($ip);
            $db->delServerStatusCache($ip_id, $port);
            $view->refresh("?m=gamemanager&amp;p=game_monitor&amp;home_id-mod_id-ip-port=" . $home_info['home_id'] . "-" . $home_info['mod_id'] . "-" . $_REQUEST['ip'] . "-" . $_REQUEST['port']);
            echo "<p>" . get_lang('follow_server_status') . " <a href='?m=gamemanager&amp;p=game_monitor&amp;home_id-mod_id-ip-port=" . $home_info['home_id'] . "-" . $home_info['mod_id'] . "-" . $_REQUEST['ip'] . "-" . $_REQUEST['port'] . "'>" . get_lang('game_monitor') . "</a> " . get_lang('page') . ".</p>";
            return;
        } else {
            if ($server_xml->replace_texts) {
                $server_home = $home_info;
                if (isset($server_xml->lgsl_query_name)) {
                    require_once 'protocol/lgsl/lgsl_protocol.php';
                }
                require_once "modules/gamemanager/cfg_text_replace.php";
            }
            $control_type = isset($server_xml->control_protocol_type) ? $server_xml->control_protocol_type : "";
            $run_dir = isset($server_xml->exe_location) ? $server_xml->exe_location : "";
            $last_param = json_decode($home_info['last_param'], True);
            $cli_param_data['GAME_TYPE'] = $home_info['mods'][$mod_id]['mod_key'];
            $cli_param_data['IP'] = $ip;
            $cli_param_data['PORT'] = $port;
            $cli_param_data['HOSTNAME'] = $home_info['home_name'];
            $cli_param_data['PID_FILE'] = "ogp_game_startup.pid";
            $os = $remote->what_os();
            // Linux
            if (preg_match("/Linux/", $os)) {
                $cli_param_data['BASE_PATH'] = $home_info['home_path'];
                $cli_param_data['HOME_PATH'] = $home_info['home_path'];
                $cli_param_data['SAVE_PATH'] = $home_info['home_path'];
                $cli_param_data['OUTPUT_PATH'] = $home_info['home_path'];
                $cli_param_data['USER_PATH'] = $home_info['home_path'];
            } elseif (preg_match("/CYGWIN/", $os)) {
                $home_path_win = $remote->exec("cygpath -w " . $home_info['home_path']);
                $home_path_win = str_replace("\\", "\\\\", $home_path_win);
                $home_path_win = trim($home_path_win);
                $cli_param_data['BASE_PATH'] = $home_path_win;
                $cli_param_data['HOME_PATH'] = $home_path_win;
                $cli_param_data['SAVE_PATH'] = $home_path_win;
                $cli_param_data['OUTPUT_PATH'] = $home_path_win;
                $cli_param_data['USER_PATH'] = $home_path_win;
            }
            if ($server_xml->protocol == "gameq") {
                $cli_param_data['QUERY_PORT'] = get_query_port($server_xml, $port);
            } elseif ($server_xml->protocol == "lgsl") {
                require 'protocol/lgsl/lgsl_protocol.php';
                $get_ports = lgsl_port_conversion((string) $server_xml->lgsl_query_name, $port, "", "");
                $cli_param_data['QUERY_PORT'] = $get_ports['1'];
            } elseif ($server_xml->protocol == "teamspeak3") {
                $cli_param_data['QUERY_PORT'] = "10011";
            }
            $cli_param_data['MAP'] = empty($last_param['map']) ? "" : $last_param['map'];
            $cli_param_data['PLAYERS'] = empty($last_param['players']) ? $home_info['mods'][$mod_id]['max_players'] : $last_param['players'];
            $start_cmd = "";
            // If the template is empty then these are not needed.
            if ($server_xml->cli_template) {
                $start_cmd = $server_xml->cli_template;
                if ($server_xml->cli_params) {
                    foreach ($server_xml->cli_params->cli_param as $cli) {
                        // If s is found the param is seperated with space
                        $add_space = preg_match("/s/", $cli['options']) > 0 ? " " : "";
                        $cli_value = $cli_param_data[(string) $cli['id']];
                        // If q is found we add quotes around the value.
                        if (preg_match("/q/", $cli['options']) > 0) {
                            $cli_value = "\"" . $cli_value . "\"";
                        }
                        $start_cmd = preg_replace("/%" . $cli['id'] . "%/", $cli['cli_string'] . $add_space . $cli_value, $start_cmd);
                    }
                }
            }
            if ($isAdmin) {
                $home_info['access_rights'] = "ufpet";
            }
            $param_access_enabled = preg_match("/p/", $home_info['access_rights']) > 0 ? TRUE : FALSE;
            if ($param_access_enabled && isset($last_param)) {
                foreach ($server_xml->server_params->param as $param) {
                    foreach ($last_param as $paramKey => $paramValue) {
                        if (!isset($paramValue)) {
                            $paramValue = (string) $param->default;
                        }
                        if ($param['key'] == $paramKey) {
                            if (0 == strlen($paramValue)) {
                                continue;
                            }
                            if ($param['key'] == $paramValue) {
                                // it's a checkbox
                                $new_param = $paramKey;
                            } elseif ($param->option == "ns" or $param->options == "ns") {
                                $new_param = $paramKey . $paramValue;
                            } else {
                                $new_param = $paramKey . ' "' . $paramValue . '"';
                            }
                            if ($param['id'] == NULL || $param['id'] == "") {
                                $start_cmd .= ' ' . $new_param;
                            } else {
                                $start_cmd = preg_replace("/%" . $param['id'] . "%/", $new_param, $start_cmd);
                            }
                        }
                    }
                    $start_cmd = preg_replace("/%" . $param['id'] . "%/", '', $start_cmd);
                }
            }
            $extra_param_access_enabled = preg_match("/e/", $home_info['access_rights']) > 0 ? TRUE : FALSE;
            if (array_key_exists('extra', $last_param) && $extra_param_access_enabled) {
                $extra_default = $last_param['extra'];
            } else {
                $extra_default = $home_info['mods'][$mod_id]['extra_params'];
            }
            $start_cmd .= " " . $extra_default;
            $remote_retval = $remote->remote_restart_server($home_info['home_id'], $ip, $port, $server_xml->control_protocol, $home_info['control_password'], $control_type, $home_info['home_path'], $server_xml->server_exec_name, $run_dir, $start_cmd, $home_info['cpu_affinity'], $home_info['nice']);
            $db->logger(get_lang_f('server_restarted', $home_info['home_name']) . "({$ip}:{$port})");
            if ($remote_retval === 1) {
                print "<p class='note'>" . get_lang('restarting_server') . "</p>";
                $view->refresh("?m=gamemanager&amp;p=restart&amp;refresh&amp;ip={$ip}&amp;port={$port}&amp;home_id={$home_id}&amp;mod_id={$mod_id}", 3);
                return;
            } else {
                if ($remote_retval === -1) {
                    print_failure(get_lang('server_cant_start'));
                    $view->refresh("?m=gamemanager&amp;p=game_monitor&amp;home_id-mod_id-ip-port=" . $home_info['home_id'] . "-" . $home_info['mod_id'] . "-" . $ip . "-" . $port, 3);
                } else {
                    if ($remote_retval === -2) {
                        print_failure(get_lang('server_cant_stop'));
                        $view->refresh("?m=gamemanager&amp;p=game_monitor&amp;home_id-mod_id-ip-port=" . $home_info['home_id'] . "-" . $home_info['mod_id'] . "-" . $ip . "-" . $port, 3);
                    } else {
                        $screen_running = $remote->is_screen_running(OGP_SCREEN_TYPE_HOME, $home_info['home_id']);
                        if ($screen_running == 1) {
                            print "<p class='note'>" . get_lang('restarting_server') . "</p>";
                            $view->refresh("?m=gamemanager&amp;p=restart&amp;refresh&amp;ip={$ip}&amp;port={$port}&amp;home_id={$home_id}&amp;mod_id={$mod_id}", 3);
                            return;
                        } else {
                            print_failure("" . get_lang('error_occured_remote_host') . ".{$remote_retval}");
                            $view->refresh("?m=gamemanager&amp;p=game_monitor&amp;home_id-mod_id-ip-port=" . $home_info['home_id'] . "-" . $home_info['mod_id'] . "-" . $ip . "-" . $port, 3);
                        }
                    }
                }
            }
        }
    }
}
 if ($max_players > 0) {
     echo "<tr><td class='right'>" . get_lang('max_players') . ":</td><td class='left'>";
     for ($i = $max_players; $i > 0; $i--) {
         $players["{$i}"] = "{$i}";
     }
     echo create_drop_box_from_array($players, 'max_players', $last_param['players']);
     echo "<span class='info'>(" . get_lang('max') . ": " . $max_players . ")</span></td></tr>\n";
 }
 echo "<tr><td class='right'>" . get_lang('ip_and_port') . ":</td><td class='left'>" . $server_home['ip'] . ":" . $server_home['port'] . "<input name='ip_port' type='hidden' value='" . $server_home['ip'] . ":" . $server_home['port'] . "'/></td></tr>";
 list($list_type, ) = explode(":", $server_xml->map_list);
 $map_array = -5;
 $maps_found = FALSE;
 // use maplist file as primary map location.
 if ($server_xml->map_list) {
     $mapfile = $home_info['home_path'] . "/" . preg_replace("/mod/", $home_info['mods'][$mod_id]['mod_key'], $server_xml->map_list);
     $read_status = $remote->remote_readfile($mapfile, $map_array);
     // If file was found and read successfully then lets seperate the maps.
     if ($read_status == 1) {
         $map_array = preg_split("/\n/", $map_array);
         $maps_found = "FILE";
     } else {
         $map_array = $read_status;
     }
 }
 // If we could not find maps from the maps file lets try directory listing
 // if possible.
 if ($maps_found === FALSE && $server_xml->maps_location) {
     // replace mod with the modname if nessessary.
     $map_path = $home_info['home_path'] . "/" . preg_replace("/mod/", $home_info['mods'][$mod_id]['mod_key'], $server_xml->maps_location);
     // Then we use the directory.
     $map_array = $remote->remote_dirlist($map_path);