Advertisement
Guest User

Untitled

a guest
May 25th, 2016
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 13.95 KB | None | 0 0
  1. <?php
  2. /*
  3.  *
  4.  * OGP - Open Game Panel
  5.  * Copyright (C) Copyright (C) 2008 - 2013 The OGP Development Team
  6.  *
  7.  * http://www.opengamepanel.org/
  8.  *
  9.  * This program is free software; you can redistribute it and/or
  10.  * modify it under the terms of the GNU General Public License
  11.  * as published by the Free Software Foundation; either version 2
  12.  * of the License, or any later version.
  13.  *
  14.  * This program is distributed in the hope that it will be useful,
  15.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.  * GNU General Public License for more details.
  18.  *
  19.  * You should have received a copy of the GNU General Public License
  20.  * along with this program; if not, write to the Free Software
  21.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  22.  *
  23.  */
  24.  
  25. require_once('home_handling_functions.php');
  26. require_once("modules/config_games/server_config_parser.php");
  27.  
  28. function exec_ogp_module() {
  29.     global $view,$db;
  30.  
  31.     $ip = $_REQUEST['ip'];
  32.     $port = $_REQUEST['port'];
  33.     $home_id = $_REQUEST['home_id'];
  34.     $mod_id = $_REQUEST['mod_id'];
  35.     $user_id = $_SESSION['user_id'];
  36.  
  37.     $isAdmin = $db->isAdmin($user_id);
  38.     if($isAdmin)
  39.         $home_info = $db->getGameHome($home_id);
  40.     else
  41.         $home_info = $db->getUserGameHome($user_id,$home_id);
  42.    
  43.     foreach($home_info['mods'][$mod_id] as $key => $value)
  44.     {
  45.         $home_info[$key] = $value;
  46.     }
  47.  
  48.     require_once('includes/lib_remote.php');
  49.     $remote = new OGPRemoteLibrary($home_info['agent_ip'],$home_info['agent_port'],$home_info['encryption_key'],$home_info['timeout']);
  50.  
  51.     if ( $home_info === FALSE )
  52.     {
  53.         print_failure(get_lang('no_rights_to_stop_server'));
  54.         return;
  55.     }
  56.  
  57.     echo "<h2>";
  58.     echo empty($home_info['home_name']) ? get_lang('not_available') : $home_info['home_name'];
  59.     echo "</h2>";
  60.  
  61.     $server_xml = read_server_config(SERVER_CONFIG_LOCATION."/".$home_info['home_cfg_file']);
  62.  
  63.     if ( !$server_xml )
  64.     {
  65.         echo create_back_button("gamemanager","game_monitor");
  66.         return;
  67.     }
  68.  
  69.     $rserver = $db->getRemoteServerById($home_info['remote_server_id']);
  70.  
  71.     if ( empty($rserver) )
  72.     {
  73.         print_failure("".get_lang('not_found_server')." ".$home_info['remote_server_id'].".");
  74.     }
  75.     else
  76.     {
  77.         if(isset($_REQUEST['refresh']))
  78.         {          
  79.             if( isset( $server_xml->console_log ) )
  80.             {
  81.                 $log_path = preg_replace("/mod/", $home_info['mods'][$mod_id]['mod_key'], $server_xml->console_log);
  82.                 $log_retval = $remote->remote_readfile( $home_info['home_path'].'/'.$log_path, $home_log );
  83.             }
  84.             else
  85.             {
  86.                 $log_retval = $remote->get_log(OGP_SCREEN_TYPE_HOME,
  87.                     $home_id,
  88.                     clean_path($home_info['home_path']."/".$server_xml->exe_location),
  89.                     $home_log);
  90.             }
  91.            
  92.             function getLastLines($string, $n = 1) {
  93.                 $lines = explode("\n", $string);
  94.                 $lines = array_slice($lines, -$n);
  95.                 return implode("\n", $lines);
  96.             }
  97.            
  98.             $home_log = getLastLines($home_log, 40);
  99.  
  100.             if ($log_retval > 0)
  101.             {
  102.                 if ( $log_retval == 2 )
  103.                     print_failure(get_lang('server_not_running_log_found'));   
  104.                 echo "<pre style='background:black;color:white;'>".$home_log."</pre>";
  105.                 if ($log_retval == 2)
  106.                     return;
  107.             }
  108.             else
  109.             {
  110.                 print_failure(get_lang_f('unable_to_get_log',$log_retval));
  111.             }
  112.            
  113.             // If game is not supported by lgsl we skip the lgsl checks and
  114.             // assume successfull start.
  115.             if ( $home_info['use_nat'] == 1 )
  116.                 $query_ip = $home_info['agent_ip'];
  117.             else
  118.                 $query_ip = $ip;
  119.  
  120.             $running = TRUE;
  121.            
  122.             if ( $server_xml->lgsl_query_name )
  123.             {
  124.                 require('protocol/lgsl/lgsl_protocol.php');
  125.                 $get_q_and_s = lgsl_port_conversion((string)$server_xml->lgsl_query_name, $port, "", "");
  126.  
  127.                 //Connection port
  128.                 $c_port = $get_q_and_s['0'];
  129.                 //query port
  130.                 $q_port = $get_q_and_s['1'];
  131.                 //software port
  132.                 $s_port = $get_q_and_s['2'];
  133.                        
  134.                 $data = lgsl_query_live((string)$server_xml->lgsl_query_name, $query_ip, $c_port, $q_port, $s_port, "sa");
  135.  
  136.                 if ( $data['b']['status'] == "0"  )
  137.                 {
  138.                     $running = FALSE;
  139.                 }
  140.             }
  141.             elseif ( $server_xml->gameq_query_name )
  142.             {
  143.                 require('protocol/GameQ/GameQ.php');
  144.                
  145.                 $query_port = get_query_port($server_xml, $port);
  146.                
  147.                 $servers = array(
  148.                     array(
  149.                         'id' => 'server',
  150.                         'type' => (string)$server_xml->gameq_query_name,
  151.                         'host' => $query_ip . ":" . $query_port,
  152.                     )
  153.                 );
  154.                 $gq = new GameQ();
  155.                 $gq->addServers($servers);
  156.                 $gq->setOption('timeout', 4);
  157.                 $gq->setOption('debug', FALSE);
  158.                 $gq->setFilter('normalise');
  159.                 $game = $gq->requestData();
  160.                        
  161.                 if ( ! $game['server']['gq_online'] )
  162.                 {
  163.                     $running = FALSE;
  164.                 }
  165.             }
  166.            
  167.             if( ! $running )
  168.             {
  169.                 if (!isset($_GET['retry']))
  170.                     $retry = 0;
  171.                 else
  172.                     $retry = $_GET['retry'];
  173.                    
  174.                 if ($retry >= 5)
  175.                 {
  176.                     echo "<p>".get_lang('server_running_not_responding')."
  177.                     <a href=?m=gamemanager&amp;p=stop&amp;home_id=".$home_id.
  178.                     "&amp;ip=".$ip."&amp;port=".
  179.                     $port.">".get_lang('already_running_stop_server').".</a></p>";
  180.                     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>";
  181.                 }
  182.                
  183.                 echo "</b>Retry #".$retry.".</b>";
  184.                 $retry++;
  185.                 print("<p class='note'>".get_lang('starting_server')."</p>");
  186.                 $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);
  187.                 return;
  188.             }
  189.             print_success(get_lang_f('server_restarted',$home_info['home_name']));
  190.             $ip_id = $db->getIpIdByIp($ip);
  191.             $db->delServerStatusCache($ip_id,$port);
  192.             $view->refresh("?m=gamemanager&amp;p=game_monitor&amp;home_id-mod_id-ip-port=". $home_id . "-". $mod_id . "-" . $ip . "-" . $port);
  193.             echo "<p>".get_lang('follow_server_status')." <a href='?m=gamemanager&amp;p=game_monitor&amp;home_id-mod_id-ip-port=".
  194.             $home_id . "-". $mod_id . "-" . $ip . "-" . $port . "'>".get_lang('game_monitor')."</a> ".get_lang('page').".</p>";
  195.  
  196.             return;
  197.         }
  198.         else
  199.         {      
  200.             if( $server_xml->replace_texts )
  201.             {
  202.                 $server_home = $home_info;
  203.                 if( isset($server_xml->lgsl_query_name) )
  204.                     require_once('protocol/lgsl/lgsl_protocol.php');
  205.                 require_once("modules/gamemanager/cfg_text_replace.php");
  206.             }
  207.            
  208.             $control_type = isset($server_xml->control_protocol_type) ? $server_xml->control_protocol_type : "";
  209.             $run_dir = isset($server_xml->exe_location) ? $server_xml->exe_location : "";
  210.            
  211.             $last_param = json_decode($home_info['last_param'], True);
  212.            
  213.             $cli_param_data['GAME_TYPE'] = $home_info['mods'][$mod_id]['mod_key'];
  214.             $cli_param_data['IP'] = $ip;
  215.             $cli_param_data['PORT'] = $port;
  216.             $cli_param_data['HOSTNAME'] = $home_info['home_name'];
  217.             $cli_param_data['PID_FILE'] = "ogp_game_startup.pid";
  218.            
  219.             $os = $remote->what_os();
  220.             // Linux
  221.             if( preg_match("/Linux/", $os) )
  222.             {
  223.                 if(preg_match("/_win(32|64)?$/", $home_info['game_key']))
  224.                 {
  225.                     $home_path_wine = $remote->exec("winepath -w ".$home_info['home_path']);
  226.                     $home_path_wine = str_replace("\\","\\\\", $home_path_wine);
  227.                     $home_path_wine = trim($home_path_wine);
  228.                     $cli_param_data['BASE_PATH'] = $home_path_wine;
  229.                     $cli_param_data['HOME_PATH'] = $home_path_wine;
  230.                     $cli_param_data['SAVE_PATH'] = $home_path_wine;
  231.                     $cli_param_data['OUTPUT_PATH'] = $home_path_wine;
  232.                     $cli_param_data['USER_PATH'] = $home_path_wine;
  233.                 }
  234.                 else
  235.                 {
  236.                     $cli_param_data['BASE_PATH'] = $home_info['home_path'];
  237.                     $cli_param_data['HOME_PATH'] = $home_info['home_path'];
  238.                     $cli_param_data['SAVE_PATH'] = $home_info['home_path'];
  239.                     $cli_param_data['OUTPUT_PATH'] = $home_info['home_path'];
  240.                     $cli_param_data['USER_PATH'] = $home_info['home_path'];
  241.                 }
  242.             }
  243.             // Windows
  244.             elseif( preg_match("/CYGWIN/", $os) )
  245.             {
  246.                 $home_path_win = $remote->exec("cygpath -w ".$home_info['home_path']);
  247.                 $home_path_win = str_replace("\\","\\\\", $home_path_win);
  248.                 $home_path_win = trim($home_path_win);
  249.                 $cli_param_data['BASE_PATH'] = $home_path_win;
  250.                 $cli_param_data['HOME_PATH'] = $home_path_win;
  251.                 $cli_param_data['SAVE_PATH'] = $home_path_win;
  252.                 $cli_param_data['OUTPUT_PATH'] = $home_path_win;
  253.                 $cli_param_data['USER_PATH'] = $home_path_win;
  254.             }
  255.            
  256.             if ($server_xml->protocol == "gameq")
  257.             {
  258.                 $cli_param_data['QUERY_PORT'] = get_query_port ($server_xml, $port);
  259.             }
  260.             elseif ($server_xml->protocol == "lgsl")
  261.             {
  262.                 require('protocol/lgsl/lgsl_protocol.php');
  263.                 $get_ports = lgsl_port_conversion((string)$server_xml->lgsl_query_name, $port, "", "");
  264.                 $cli_param_data['QUERY_PORT'] = $get_ports['1'];
  265.             }
  266.             elseif ($server_xml->protocol == "teamspeak3")
  267.             {
  268.                 $cli_param_data['QUERY_PORT'] = "10011";
  269.             }
  270.            
  271.             $cli_param_data['MAP'] = empty($last_param['map']) ?  "" : $last_param['map'];
  272.             $cli_param_data['PLAYERS'] = empty($last_param['players']) ? $home_info['mods'][$mod_id]['max_players'] : $last_param['players'];
  273.             $cli_param_data['CONTROL_PASSWORD'] = $home_info['control_password'];
  274.            
  275.             $start_cmd = "";
  276.             // If the template is empty then these are not needed.
  277.             if ( $server_xml->cli_template )
  278.             {
  279.                 $start_cmd = $server_xml->cli_template;
  280.                 if ( $server_xml->cli_params )
  281.                 {
  282.                     foreach ( $server_xml->cli_params->cli_param as $cli )
  283.                     {
  284.                         // If s is found the param is seperated with space
  285.                         $add_space = preg_match( "/s/", $cli['options'] ) > 0 ? " " : "";
  286.                         $cli_value = $cli_param_data[(string) $cli['id'] ];
  287.                         // If q is found we add quotes around the value.
  288.                         if ( preg_match( "/q/", $cli['options'] ) > 0 )
  289.                         {
  290.                             $cli_value = "\"".$cli_value."\"";
  291.                         }
  292.                         $start_cmd = preg_replace( "/%".$cli['id']."%/",
  293.                             $cli['cli_string'].$add_space.$cli_value, $start_cmd );
  294.                     }
  295.                 }
  296.                 if ( $server_xml->reserve_ports )
  297.                 {
  298.                     foreach ( $server_xml->reserve_ports->port as $reserve_port )
  299.                     {
  300.                         // If s is found the param is seperated with space
  301.                         $add_space = preg_match( "/s/", $reserve_port['options'] ) > 0 ? " " : "";
  302.                         $cli_value = $reserve_port['type'] == "add" ? $server_home['port'] + (string) $reserve_port:
  303.                                                                       $server_home['port'] - (string) $reserve_port;
  304.                         // If q is found we add quotes around the value.
  305.                         if ( preg_match( "/q/", $reserve_port['options'] ) > 0 )
  306.                         {
  307.                             $cli_value = "\"".$cli_value."\"";
  308.                         }
  309.                         $start_cmd = preg_replace( "/%".$reserve_port['id']."%/",
  310.                             $reserve_port['cli_string'].$add_space.$cli_value, $start_cmd );
  311.                     }
  312.                 }
  313.             }
  314.            
  315.             if ( $isAdmin )
  316.             {
  317.                 $home_info['access_rights'] = "ufpet";
  318.             }
  319.                        
  320.             $param_access_enabled = preg_match("/p/",$home_info['access_rights']) > 0 ? TRUE : FALSE;
  321.                        
  322.             if ( $param_access_enabled && isset($last_param) )
  323.             {
  324.                 foreach($server_xml->server_params->param as $param)
  325.                 {                      
  326.                     foreach ($last_param as $paramKey => $paramValue)
  327.                     {
  328.                         if (!isset($paramValue))
  329.                             $paramValue = (string)$param->default;
  330.                        
  331.                         if ($param['key'] == $paramKey)
  332.                         {  
  333.                             if (0 == strlen($paramValue))
  334.                                 continue;
  335.                             if ($param['key'] == $paramValue) // it's a checkbox
  336.                                 $new_param = $paramKey;
  337.                             elseif($param->option == "ns" or $param->options == "ns")
  338.                                 $new_param = $paramKey.$paramValue;
  339.                             else
  340.                                 $new_param = $paramKey.' "'.$paramValue.'"';
  341.                          
  342.                             if ($param['id'] == NULL || $param['id'] == "")
  343.                                 $start_cmd .= ' '.$new_param;
  344.                             else
  345.                                 $start_cmd = preg_replace( "/%".$param['id']."%/", $new_param, $start_cmd );
  346.                         }            
  347.                     }
  348.                     $start_cmd = preg_replace( "/%".$param['id']."%/", '', $start_cmd );
  349.                 }
  350.             }
  351.  
  352.             $extra_param_access_enabled = preg_match("/e/",$home_info['access_rights']) > 0 ? TRUE:FALSE;
  353.            
  354.             if ( array_key_exists('extra', $last_param) && $extra_param_access_enabled )
  355.                 $extra_default = $last_param['extra'];
  356.             else
  357.                 $extra_default = $home_info['mods'][$mod_id]['extra_params'];
  358.  
  359.             $start_cmd .= " ".$extra_default;
  360.                        
  361.             $remote_retval = $remote->remote_restart_server($home_id,$ip,$port,$server_xml->control_protocol,
  362.                                                             $home_info['control_password'],$control_type,$home_info['home_path'],
  363.                                                             $server_xml->server_exec_name,$run_dir,$start_cmd,
  364.                                                             $home_info['cpu_affinity'],$home_info['nice']);
  365.            
  366.             $db->logger(get_lang_f('server_restarted', $home_info['home_name']) . "($ip:$port)");
  367.                
  368.             if ( $remote_retval === 1 )
  369.             {
  370.                 print("<p class='note'>".get_lang('restarting_server')." ".$start_cmd."</p>");
  371.                 $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);
  372.                 return;
  373.             }
  374.             else if ( $remote_retval === -1 )
  375.             {
  376.                 print_failure(get_lang('server_cant_start'));
  377.                 $view->refresh("?m=gamemanager&amp;p=game_monitor&amp;home_id-mod_id-ip-port=". $home_id . "-". $mod_id . "-" . $ip . "-" . $port,3);
  378.             }
  379.             else if ( $remote_retval === -2 )
  380.             {
  381.                 print_failure(get_lang('server_cant_stop'));
  382.                 $view->refresh("?m=gamemanager&amp;p=game_monitor&amp;home_id-mod_id-ip-port=". $home_id . "-". $mod_id . "-" . $ip . "-" . $port,3);
  383.             }
  384.             else
  385.             {
  386.                 $screen_running = $remote->is_screen_running(OGP_SCREEN_TYPE_HOME,$home_id);
  387.                 if ( $screen_running == 1 )
  388.                 {
  389.                     print("<p class='note'>".get_lang('restarting_server')." ".$start_cmd."</p>");
  390.                     $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);
  391.                     return;
  392.                 }
  393.                 else
  394.                 {
  395.                     print_failure("".get_lang('error_occured_remote_host').".$remote_retval");
  396.                     $view->refresh("?m=gamemanager&amp;p=game_monitor&amp;home_id-mod_id-ip-port=". $home_id . "-". $mod_id . "-" . $ip . "-" . $port,3);
  397.                 }
  398.             }
  399.         }
  400.     }
  401. }
  402. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement