home_handling_functions.php 16 KB

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