start_server.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. require_once('home_handling_functions.php');
  25. require_once("modules/config_games/server_config_parser.php");
  26. function exec_ogp_module()
  27. {
  28. global $db;
  29. global $view;
  30. $home_id = $_REQUEST['home_id'];
  31. $isAdmin = $db->isAdmin( $_SESSION['user_id'] );
  32. if($isAdmin)
  33. $home_info = $db->getGameHome($home_id);
  34. else
  35. $home_info = $db->getUserGameHome($_SESSION['user_id'],$home_id);
  36. if ( $home_info == FALSE )
  37. {
  38. print_failure(get_lang('no_rights_to_start_server'));
  39. echo "<table class='center'><tr><td><a href='?m=gamemanager&amp;p=game_monitor&amp;home_id-mod_id-ip-port=". $home_info['home_id'] . "-". $home_info['mod_id'] . "-" . $_REQUEST['ip'] . "-" . $_REQUEST['port'] . "'><< ".get_lang('back')."</a></td></tr></table>";
  40. return;
  41. }
  42. $mod_id = $_REQUEST['mod_id'];
  43. if ( !array_key_exists($mod_id,$home_info['mods']) )
  44. {
  45. print_failure("Unable to retrieve mod information from database.");
  46. return;
  47. }
  48. echo "<h2>";
  49. echo empty($home_info['home_name']) ? get_lang('not_available') : htmlentities($home_info['home_name']);
  50. echo "</h2>";
  51. require_once('includes/lib_remote.php');
  52. $remote = new OGPRemoteLibrary($home_info['agent_ip'],$home_info['agent_port'],$home_info['encryption_key'],$home_info['timeout']);
  53. $server_xml = read_server_config(SERVER_CONFIG_LOCATION."/".$home_info['home_cfg_file']);
  54. if ( !$server_xml )
  55. {
  56. echo "<table class='center'><tr><td><a href='?m=gamemanager&amp;p=game_monitor&amp;home_id-mod_id-ip-port=". $home_info['home_id'] . "-". $home_info['mod_id'] . "-" . $_REQUEST['ip'] . "-" . $_REQUEST['port'] . "'><< ".get_lang('back')."</a></td></tr></table>";
  57. return;
  58. }
  59. // It compares ip and port on POST with the pair on DB for security reasons (URL HACKING)
  60. $home_id = $home_info['home_id'];
  61. $ip_info = $db->getHomeIpPorts($home_id);
  62. foreach ( $ip_info as $ip_ports_row )
  63. {
  64. if($ip_ports_row['ip'] == $_REQUEST['ip'] && $ip_ports_row['port'] == $_REQUEST['port'])
  65. {
  66. $ip = $_REQUEST['ip'];
  67. $port = $ip_ports_row['port'];
  68. }
  69. }
  70. If (!isset($ip) OR !isset($port))
  71. {
  72. echo "<h2>" . get_lang_f('ip_port_pair_not_owned') . "</h2>";
  73. 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>";
  74. return;
  75. }
  76. if( isset( $server_xml->console_log ) )
  77. {
  78. $log_path = preg_replace("/%mod%/i", $home_info['mods'][$mod_id]['mod_key'], $server_xml->console_log);
  79. $log_retval = $remote->remote_readfile( $home_info['home_path'].'/'.$log_path, $home_log );
  80. }
  81. else
  82. {
  83. $log_retval = $remote->get_log(OGP_SCREEN_TYPE_HOME,
  84. $home_info['home_id'],
  85. clean_path($home_info['home_path']."/".$server_xml->exe_location),
  86. $home_log);
  87. }
  88. function getLastLines($string, $n = 1) {
  89. $lines = explode("\n", (string)$string);
  90. $lines = array_slice($lines, -$n);
  91. return implode("\n", $lines);
  92. }
  93. $home_log = getLastLines($home_log, 40);
  94. if ($log_retval > 0)
  95. {
  96. if ( $log_retval == 2 )
  97. print_failure(get_lang('server_not_running_log_found'));
  98. echo "<pre style='background:black;color:white;'>".$home_log."</pre>";
  99. if ($log_retval == 2)
  100. return;
  101. }
  102. else
  103. {
  104. print_failure(get_lang_f('unable_to_get_log',$log_retval));
  105. }
  106. // If game is not supported by lgsl we skip the lgsl checks and
  107. // assume successfull start.
  108. if ( $home_info['use_nat'] == 1 )
  109. $query_ip = $home_info['agent_ip'];
  110. else
  111. $query_ip = $ip;
  112. $running = $remote->is_screen_running(OGP_SCREEN_TYPE_HOME,$home_info['home_id']);
  113. if ( $server_xml->lgsl_query_name )
  114. {
  115. require('protocol/lgsl/lgsl_protocol.php');
  116. $get_q_and_s = lgsl_port_conversion((string)$server_xml->lgsl_query_name, $port, "", "");
  117. //Connection port
  118. $c_port = $get_q_and_s['0'];
  119. //query port
  120. $q_port = $get_q_and_s['1'];
  121. //software port
  122. $s_port = $get_q_and_s['2'];
  123. $data = lgsl_query_live((string)$server_xml->lgsl_query_name, $query_ip, $c_port, $q_port, $s_port, "sa");
  124. if ( $data['b']['status'] == "0" )
  125. {
  126. $running = FALSE;
  127. }
  128. }
  129. elseif ( $server_xml->gameq_query_name )
  130. {
  131. require_once 'protocol/GameQ/Autoloader.php';
  132. $query_port = get_query_port($server_xml, $port);
  133. $servers = array(
  134. array(
  135. 'id' => 'server',
  136. 'type' => (string)$server_xml->gameq_query_name,
  137. 'host' => $query_ip . ":" . $query_port,
  138. )
  139. );
  140. $gq = new \GameQ\GameQ();
  141. $gq->addServers($servers);
  142. $gq->setOption('timeout', 4);
  143. $gq->setOption('debug', FALSE);
  144. $gq->addFilter('normalise');
  145. $game = $gq->process();
  146. if ( ! $game['server']['gq_online'] )
  147. {
  148. $running = FALSE;
  149. }
  150. }
  151. if( ! $running )
  152. {
  153. if (!isset($_GET['retry']))
  154. $retry = 0;
  155. else
  156. $retry = $_GET['retry'];
  157. if ($retry >= 5)
  158. {
  159. echo "<p>".get_lang('server_running_not_responding').
  160. "<a href='?m=gamemanager&amp;p=stop&amp;home_id=$home_id&amp;mod_id=$mod_id&amp;ip=$ip&amp;port=$port' >".
  161. get_lang('already_running_stop_server').".</a></p>".
  162. "<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'><< ".
  163. get_lang('back')."</a></td></tr></table>";
  164. }
  165. echo "</b>Retry #".$retry.".</b>";
  166. $retry++;
  167. print("<p class='note'>".get_lang('starting_server')."</p>");
  168. $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);
  169. return;
  170. }
  171. print_success(get_lang('server_started'));
  172. $ip_id = $db->getIpIdByIp($ip);
  173. $db->delServerStatusCache($ip_id,$port);
  174. $view->refresh("?m=gamemanager&amp;p=game_monitor&amp;home_id-mod_id-ip-port=$home_id-$mod_id-$ip-$port" );
  175. return;
  176. }
  177. ?>