Advertisement
Guest User

Untitled

a guest
Feb 3rd, 2017
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 12.73 KB | None | 0 0
  1. <?php
  2. /*
  3.  *
  4.  * OGP - Open Game Panel
  5.  * Copyright (C) 2008 - 2014 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. function get_query_port($server_xml, $server_port)
  25. {
  26.     if( $server_xml->query_port and $server_xml->query_port['type'] == "add" )
  27.         return $server_port + $server_xml->query_port;
  28.     return $server_port;
  29. }
  30. function get_start_cmd($remote,$server_xml,$home_info,$mod_id,$ip,$port)
  31. {  
  32.     $last_param = json_decode($home_info['last_param'], True);
  33.    
  34.     $cli_param_data['GAME_TYPE'] = $home_info['mods'][$mod_id]['mod_key'];
  35.     $cli_param_data['IP'] = $ip;
  36.     $cli_param_data['PORT'] = $port;
  37.     $cli_param_data['HOSTNAME'] = $home_info['home_name'];
  38.     $cli_param_data['PID_FILE'] = "ogp_game_startup.pid";
  39.    
  40.     $os = $remote->what_os();
  41.     // Linux
  42.     if( preg_match("/Linux/", $os) )
  43.     {
  44.         if(preg_match("/_win(32|64)?$/", $home_info['game_key']))
  45.         {
  46.             $home_path_wine = $remote->exec("winepath -w ".$home_info['home_path']);
  47.             $home_path_wine = str_replace("\\","\\\\", $home_path_wine);
  48.             $home_path_wine = trim($home_path_wine);
  49.             $cli_param_data['BASE_PATH'] = $home_path_wine;
  50.             $cli_param_data['HOME_PATH'] = $home_path_wine;
  51.             $cli_param_data['SAVE_PATH'] = $home_path_wine;
  52.             $cli_param_data['OUTPUT_PATH'] = $home_path_wine;
  53.             $cli_param_data['USER_PATH'] = $home_path_wine;
  54.         }
  55.         else
  56.         {
  57.             $cli_param_data['BASE_PATH'] = $home_info['home_path'];
  58.             $cli_param_data['HOME_PATH'] = $home_info['home_path'];
  59.             $cli_param_data['SAVE_PATH'] = $home_info['home_path'];
  60.             $cli_param_data['OUTPUT_PATH'] = $home_info['home_path'];
  61.             $cli_param_data['USER_PATH'] = $home_info['home_path'];
  62.         }
  63.     }
  64.     // Windows
  65.     elseif( preg_match("/CYGWIN/", $os) )
  66.     {
  67.         $home_path_win = $remote->exec("cygpath -w ".$home_info['home_path']);
  68.         $home_path_win = str_replace("\\","\\\\", $home_path_win);
  69.         $home_path_win = trim($home_path_win);
  70.         $cli_param_data['BASE_PATH'] = $home_path_win;
  71.         $cli_param_data['HOME_PATH'] = $home_path_win;
  72.         $cli_param_data['SAVE_PATH'] = $home_path_win;
  73.         $cli_param_data['OUTPUT_PATH'] = $home_path_win;
  74.         $cli_param_data['USER_PATH'] = $home_path_win;
  75.     }
  76.    
  77.     if ($server_xml->protocol == "gameq")
  78.     {
  79.         $cli_param_data['QUERY_PORT'] = get_query_port ($server_xml, $port);
  80.     }
  81.     elseif ($server_xml->protocol == "lgsl")
  82.     {
  83.         require('protocol/lgsl/lgsl_protocol.php');
  84.         $get_ports = lgsl_port_conversion((string)$server_xml->lgsl_query_name, $port, "", "");
  85.         $cli_param_data['QUERY_PORT'] = $get_ports['1'];
  86.     }
  87.     elseif ($server_xml->protocol == "teamspeak3")
  88.     {
  89.         $cli_param_data['QUERY_PORT'] = get_query_port($server_xml, $port);
  90.     }
  91.    
  92.     $cli_param_data['MAP'] = ($last_param === NULL or !isset($last_param['map'])) ?  "" : $last_param['map'];
  93.     $cli_param_data['PLAYERS'] = ($last_param === NULL or !isset($last_param['players'])) ?
  94.                                  isset($home_info['mods'][$mod_id]['max_players']) ?
  95.                                  $home_info['mods'][$mod_id]['max_players'] : "1" : $last_param['players'];
  96.     $cli_param_data['CONTROL_PASSWORD'] = $home_info['control_password'];
  97.    
  98.     $start_cmd = "";
  99.     // If the template is empty then these are not needed.
  100.     if ( $server_xml->cli_template )
  101.     {
  102.         $start_cmd = $server_xml->cli_template;
  103.         if ( $server_xml->cli_params )
  104.         {
  105.             foreach ( $server_xml->cli_params->cli_param as $cli )
  106.             {
  107.                 // If s is found the param is seperated with space
  108.                 $add_space = preg_match( "/s/", $cli['options'] ) > 0 ? " " : "";
  109.                 $cli_value = $cli_param_data[(string) $cli['id'] ];
  110.                 // If q is found we add quotes around the value.
  111.                 if ( preg_match( "/q/", $cli['options'] ) > 0 )
  112.                 {
  113.                     $cli_value = "\"".$cli_value."\"";
  114.                 }
  115.                 $start_cmd = preg_replace( "/%".$cli['id']."%/",
  116.                     $cli['cli_string'].$add_space.$cli_value, $start_cmd );
  117.             }
  118.         }
  119.        
  120.         if ( $server_xml->reserve_ports )
  121.         {
  122.             foreach ( $server_xml->reserve_ports->port as $reserve_port )
  123.             {
  124.                 // If s is found the param is seperated with space
  125.                 $add_space = preg_match( "/s/", $reserve_port['options'] ) > 0 ? " " : "";
  126.                 $cli_value = $reserve_port['type'] == "add" ? $port + (string) $reserve_port:
  127.                                                               $port - (string) $reserve_port;
  128.                 // If q is found we add quotes around the value.
  129.                 if ( preg_match( "/q/", $reserve_port['options'] ) > 0 )
  130.                 {
  131.                     $cli_value = "\"".$cli_value."\"";
  132.                 }
  133.                 $start_cmd = preg_replace( "/%".$reserve_port['id']."%/",
  134.                     $reserve_port['cli_string'].$add_space.$cli_value, $start_cmd );
  135.             }
  136.         }
  137.     }
  138.     // No need to check the access_rights for params and extra params
  139.     // because, even if the user have no access rights, last_param could be
  140.     // set by admin or just empty ($last_param === NULL).
  141.     if ( $last_param !== NULL and isset($server_xml->server_params->param) )
  142.     {
  143.         foreach($server_xml->server_params->param as $param)
  144.         {                      
  145.             foreach ($last_param as $paramKey => $paramValue)
  146.             {
  147.                 if (!isset($paramValue))
  148.                     $paramValue = (string)$param->default;
  149.                
  150.                 if ($param['key'] == $paramKey)
  151.                 {  
  152.                     if (0 == strlen($paramValue))
  153.                         continue;
  154.                     if ($param['key'] == $paramValue) // it's a checkbox
  155.                         $new_param = $paramKey;
  156.                     elseif($param->option == "ns" or $param->options == "ns")
  157.                         $new_param = $paramKey.clean_server_param_value($paramValue, $server_xml->cli_allow_chars);
  158.                     else
  159.                         $new_param = $paramKey.' "'.clean_server_param_value($paramValue, $server_xml->cli_allow_chars).'"';
  160.                  
  161.                     if ($param['id'] == NULL || $param['id'] == "")
  162.                         $start_cmd .= ' '.$new_param;
  163.                     else
  164.                         $start_cmd = preg_replace( "/%".$param['id']."%/", $new_param, $start_cmd );
  165.                 }            
  166.             }
  167.             $start_cmd = preg_replace( "/%".$param['id']."%/", '', $start_cmd );
  168.         }
  169.     }
  170.    
  171.     $extra = ($last_param !== NULL and array_key_exists('extra', $last_param) and $last_param['extra'] != "") ?
  172.               $last_param['extra'] : $home_info['mods'][$mod_id]['extra_params'];
  173.        
  174.     $start_cmd .= " ".str_replace("\\\\", "\\", clean_server_param_value($extra, $server_xml->cli_allow_chars));
  175.     return $start_cmd;
  176. }
  177. // This function is used to batch stop/restart servers in background.
  178. function exec_operation( $action, $home_id, $mod_id, $ip, $port )
  179. {
  180.     if(!is_numeric($port))
  181.         return FALSE;
  182.     if(!preg_match("/[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/",$ip))
  183.         return FALSE;
  184.    
  185.     global $db;
  186.     $isAdmin = $db->isAdmin( $_SESSION['user_id'] );
  187.     if($isAdmin)
  188.         $home_info = $db->getGameHome($home_id);
  189.     else
  190.         $home_info = $db->getUserGameHome($_SESSION['user_id'],$home_id);
  191.    
  192.     if( $home_info === FALSE )
  193.         return FALSE;
  194.    
  195.     $server_xml = read_server_config(SERVER_CONFIG_LOCATION."/".$home_info['home_cfg_file']);
  196.     if ( !$server_xml )
  197.         return FALSE;
  198.    
  199.     require_once('includes/lib_remote.php');
  200.     $remote = new OGPRemoteLibrary($home_info['agent_ip'],$home_info['agent_port'],$home_info['encryption_key'],$home_info['timeout']);
  201.        
  202.     if ( $action != "stop" )
  203.     {
  204.         if( $server_xml->replace_texts )
  205.         {
  206.             $server_home = $home_info;
  207.             if( isset($server_xml->lgsl_query_name) )
  208.                 require_once('protocol/lgsl/lgsl_protocol.php');
  209.             require_once("modules/gamemanager/cfg_text_replace.php");
  210.         }
  211.     }
  212.     $screen_running = $remote->is_screen_running(OGP_SCREEN_TYPE_HOME,$home_info['home_id']) === 1;
  213.    
  214.     if ( $action == "stop" AND $screen_running )
  215.     {
  216.         $remote_retval = $remote->remote_stop_server($home_info['home_id'],
  217.             $ip, $port, $server_xml->control_protocol,
  218.             $home_info['control_password'],$server_xml->control_protocol_type, $home_info['home_path']);
  219.         $db->logger(get_lang_f('server_stopped', $home_info['home_name'] ) . "($ip:$port)");
  220.         if ( $remote_retval === -1 )
  221.             return FALSE;
  222.         elseif( $remote_retval === -2 )
  223.             return FALSE;
  224.         else
  225.         {
  226.             $firewall_settings = $db->getFirewallSettings($home_info['remote_server_id']);
  227.             if ($firewall_settings['status'] == "enable")
  228.             {
  229.                 $ip_ports = $db->getHomeIpPorts($home_info['home_id']);
  230.                 foreach ($ip_ports as $ip_port)
  231.                 {
  232.                     if ($server_xml->protocol == "gameq")
  233.                     {
  234.                         $query_port = get_query_port($server_xml, $ip_port['port']);
  235.                     }
  236.                     elseif ($server_xml->protocol == "lgsl")
  237.                     {
  238.                         require('protocol/lgsl/lgsl_protocol.php');
  239.                         $get_ports = lgsl_port_conversion((string)$server_xml->lgsl_query_name, $ip_port['port'], "", "");
  240.                         $query_port = $get_ports['1'];
  241.                     }
  242.                     elseif ($server_xml->protocol == "teamspeak3")
  243.                     {
  244.                         $query_port = get_query_port($server_xml, $ip_port['port']);
  245.                     }
  246.                     set_firewall($remote, $firewall_settings, 'deny', $ip_port['port'], $ip_port['ip']);
  247.                     if(isset($query_port) and $query_port != "" and $query_port != $ip_port['port'])
  248.                         set_firewall($remote, $firewall_settings, 'deny', $query_port, $ip_port['ip']);
  249.                 }
  250.             }
  251.             return TRUE;
  252.         }
  253.     }
  254.     elseif ( $action == "restart" AND $screen_running )
  255.     {
  256.         $start_cmd = get_start_cmd($remote,$server_xml,$home_info,$mod_id,$ip,$port);
  257.         // Do text replacements in cfg file
  258.         if( $server_xml->replace_texts )
  259.         {
  260.             foreach($home_info['mods'][$mod_id] as $key => $value)
  261.             {
  262.                 $home_info[$key] = $value;
  263.             }
  264.             $server_home = $home_info;
  265.             if( isset($server_xml->lgsl_query_name) )
  266.                 require_once('protocol/lgsl/lgsl_protocol.php');
  267.             require_once("modules/gamemanager/cfg_text_replace.php");
  268.         }
  269.         $remote_retval = $remote->remote_restart_server($home_info['home_id'],$ip,$port,$server_xml->control_protocol,
  270.                                                         $home_info['control_password'],$server_xml->control_protocol_type,$home_info['home_path'],
  271.                                                         $server_xml->server_exec_name,$server_xml->exe_location,$start_cmd,
  272.                                                         $home_info['mods'][$mod_id]['cpu_affinity'],
  273.                                                         $home_info['mods'][$mod_id]['nice']);
  274.         $db->logger(get_lang_f('server_restarted', $home_info['home_name']) . "($ip:$port)");
  275.         if ( $remote_retval === -1 )
  276.             return FALSE;
  277.         else if ( $remote_retval === -2 )
  278.             return FALSE;
  279.         else
  280.         {
  281.             $ip_id = $db->getIpIdByIp($ip);
  282.             $db->delServerStatusCache($ip_id,$port);
  283.         }
  284.     }
  285.     elseif ( $action == "start" AND ! $screen_running )
  286.     {
  287.         $start_cmd = get_start_cmd($remote,$server_xml,$home_info,$mod_id,$ip,$port);
  288.         // Do text replacements in cfg file
  289.         if( $server_xml->replace_texts )
  290.         {
  291.             foreach($home_info['mods'][$mod_id] as $key => $value)
  292.             {
  293.                 $home_info[$key] = $value;
  294.             }
  295.             $server_home = $home_info;
  296.             if( isset($server_xml->lgsl_query_name) )
  297.                 require_once('protocol/lgsl/lgsl_protocol.php');
  298.             require_once("modules/gamemanager/cfg_text_replace.php");
  299.         }
  300.         $start_retval = $remote->universal_start($home_info['home_id'],
  301.                                                  $home_info['home_path'],
  302.                                                  $server_xml->server_exec_name, $server_xml->exe_location,
  303.                                                  $start_cmd, $port, $ip,
  304.                                                  $home_info['mods'][$mod_id]['cpu_affinity'],
  305.                                                  $home_info['mods'][$mod_id]['nice']);
  306.         $db->logger(get_lang('server_started') . " (".$home_info['home_name']." $ip:$port)");
  307.         if( $start_retval == AGENT_ERROR_NOT_EXECUTABLE or $start_retval <= 0)
  308.             return FALSE;
  309.         else
  310.         {
  311.             $firewall_settings = $db->getFirewallSettings($home_info['remote_server_id']);
  312.             if ($firewall_settings['status'] == "enable")
  313.             {
  314.                 $ip_ports = $db->getHomeIpPorts($home_info['home_id']);
  315.                 foreach ($ip_ports as $ip_port)
  316.                 {
  317.                     if ($server_xml->protocol == "gameq")
  318.                     {
  319.                         $query_port = get_query_port($server_xml, $ip_port['port']);
  320.                     }
  321.                     elseif ($server_xml->protocol == "lgsl")
  322.                     {
  323.                         require('protocol/lgsl/lgsl_protocol.php');
  324.                         $get_ports = lgsl_port_conversion((string)$server_xml->lgsl_query_name, $ip_port['port'], "", "");
  325.                         $query_port = $get_ports['1'];
  326.                     }
  327.                     elseif ($server_xml->protocol == "teamspeak3")
  328.                     {
  329.                         $query_port = get_query_port($server_xml, $ip_port['port']);
  330.                     }
  331.                     set_firewall($remote, $firewall_settings, 'allow', $ip_port['port'], $ip_port['ip']);
  332.                     if(isset($query_port) and $query_port != "" and $query_port != $ip_port['port'])
  333.                         set_firewall($remote, $firewall_settings, 'allow', $query_port, $ip_port['ip']);
  334.                 }
  335.             }
  336.             $ip_id = $db->getIpIdByIp($ip);
  337.             $db->delServerStatusCache($ip_id,$port);
  338.             return TRUE;
  339.         }
  340.     }
  341. }
  342. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement