restart_server.php 15 KB

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