home_handling_functions.php 14 KB

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