create_servers.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <?php
  2. require_once("includes/lib_remote.php");
  3. require_once("modules/config_games/server_config_parser.php");
  4. function exec_ogp_module()
  5. {
  6. global $db,$view,$settings;
  7. $user_id = $_SESSION['user_id'];
  8. $cart_id = $_POST['cart_id'];
  9. $cart_paid = $db->resultQuery( "SELECT paid FROM OGP_DB_PREFIXbilling_carts WHERE paid=1 AND cart_id=".$db->realEscapeSingle($cart_id) );
  10. $isAdmin = $db->isAdmin( $_SESSION['user_id'] );
  11. if ( $isAdmin )
  12. $orders = $db->resultQuery( "SELECT * FROM OGP_DB_PREFIXbilling_orders WHERE cart_id=".$db->realEscapeSingle($cart_id) );
  13. else
  14. $orders = $db->resultQuery( "SELECT * FROM OGP_DB_PREFIXbilling_orders WHERE cart_id=".$db->realEscapeSingle($cart_id)." AND user_id=".$db->realEscapeSingle($user_id) );
  15. if( !empty($orders) and !empty($cart_paid) )
  16. {
  17. foreach($orders as $order)
  18. {
  19. $order_id = $order['order_id'];
  20. $service_id = $order['service_id'];
  21. $home_name = $order['home_name'];
  22. $remote_control_password = $order['remote_control_password'];
  23. $ftp_password = $order['ftp_password'];
  24. $ip = $order['ip'];
  25. $max_players = $order['max_players'];
  26. $user_id = $order['user_id'];
  27. $extended = $order['extended'] == "1" ? TRUE : FALSE;
  28. //Query service info
  29. $service = $db->resultQuery( "SELECT *
  30. FROM OGP_DB_PREFIXbilling_services
  31. WHERE service_id=".$db->realEscapeSingle($service_id) );
  32. if( !empty( $service[0] ) )
  33. {
  34. $home_cfg_id = $service[0]['home_cfg_id'];
  35. $mod_cfg_id = $service[0]['mod_cfg_id'];
  36. $remote_server_id = $service[0]['remote_server_id'];
  37. $ftp = $service[0]['ftp'];
  38. $install_method = $service[0]['install_method'];
  39. $manual_url = $service[0]['manual_url'];
  40. $access_rights = $service[0]['access_rights'];
  41. }
  42. else
  43. return;
  44. if($extended)
  45. {
  46. $home_id = $order['home_id'];
  47. //Get The home info without mods in 1 array (Necesary for remote connection).
  48. $home_info = $db->getGameHomeWithoutMods($home_id);
  49. //Create the remote connection
  50. $remote = new OGPRemoteLibrary($home_info['agent_ip'],$home_info['agent_port'],$home_info['encryption_key'],$home_info['timeout']);
  51. //Reassign the server
  52. $db->assignHomeTo( "user", $user_id, $home_id, $access_rights );
  53. //Reenable the FTP account
  54. if ($ftp == "enabled")
  55. {
  56. $remote->ftp_mgr("useradd", $home_info['home_id'], $home_info['ftp_password'], $home_info['home_path']);
  57. $db->changeFtpStatus('enabled',$home_info['home_id']);
  58. }
  59. echo "<h4>".get_lang('success')."</h4><br><p>".get_lang('redirecting_to_game_monitor')."</p><br>";
  60. }
  61. else
  62. {
  63. //OPTIONS, change it at your choice;
  64. $extra_params = "";//no extra params defined by default
  65. $cpu_affinity = "NA";//Affinity to one core/thread of the cpu by number, use NA to disable it
  66. $nice = "0";//Min priority=19 Max Priority=-19
  67. //Add Game home to database
  68. $rserver = $db->getRemoteServer($remote_server_id);
  69. $game_path = "/home/".$rserver['ogp_user']."/OGP_User_Files/simple-billing/";
  70. $home_id = $db->addGameHome( $remote_server_id, $user_id, $home_cfg_id, $game_path, $home_name, $remote_control_password, $ftp_password);
  71. //Add IP:Port Pair to the Game Home
  72. $add_port = $db->addGameIpPort( $home_id, $ip, $db->getNextAvailablePort($ip,$home_cfg_id) );
  73. //Assign the Game Mod to the Game Home
  74. $mod_id = $db->addModToGameHome( $home_id, $mod_cfg_id );
  75. $db->updateGameModParams( $max_players, $extra_params, $cpu_affinity, $nice, $home_id, $mod_cfg_id );
  76. $db->assignHomeTo( "user", $user_id, $home_id, $access_rights );
  77. //Get The home info without mods in 1 array (Necesary for remote connection).
  78. $home_info = $db->getGameHomeWithoutMods($home_id);
  79. //Create the remote connection
  80. $remote = new OGPRemoteLibrary($home_info['agent_ip'],$home_info['agent_port'],$home_info['encryption_key'],$home_info['timeout']);
  81. //Get Full home info in 1 array
  82. $home_info = $db->getGameHome($home_id);
  83. //Read the Game Config from the XML file
  84. $server_xml = read_server_config(SERVER_CONFIG_LOCATION."/".$home_info['home_cfg_file']);
  85. //Get Values from XML
  86. $modkey = $home_info['mods'][$mod_id]['mod_key'];
  87. $mod_xml = xml_get_mod($server_xml, $modkey);
  88. $installer_name = $mod_xml->installer_name;
  89. $mod_cfg_id = $home_info['mods'][$mod_id]['mod_cfg_id'];
  90. //Get Preinstall commands from db
  91. $game_mod_precmd = $db->resultQuery("SELECT DISTINCT precmd FROM OGP_DB_PREFIXgame_mods WHERE mod_id='" . $db->realEscapeSingle($mod_id) . "'");
  92. if ($game_mod_precmd[0]['precmd'] === NULL OR empty($game_mod_precmd[0]['precmd']))
  93. {
  94. $config_mod_precmd = $db->resultQuery("SELECT DISTINCT def_precmd FROM OGP_DB_PREFIXconfig_mods WHERE mod_cfg_id='" . $db->realEscapeSingle($mod_cfg_id) . "'");
  95. if ($config_mod_precmd[0]['def_precmd'] === NULL OR empty($config_mod_precmd[0]['def_precmd']))
  96. $precmd = "";
  97. else
  98. $precmd = $config_mod_precmd[0]['def_precmd'];
  99. }
  100. else
  101. $precmd = $game_mod_precmd[0]['precmd'];
  102. //Get Postinstall commands from db
  103. $game_mod_postcmd = $db->resultQuery("SELECT DISTINCT postcmd FROM OGP_DB_PREFIXgame_mods WHERE mod_id='" . $db->realEscapeSingle($mod_id) . "'");
  104. if ($game_mod_postcmd[0]['postcmd'] === NULL OR empty($game_mod_postcmd[0]['postcmd']))
  105. {
  106. $config_mod_postcmd = $db->resultQuery("SELECT DISTINCT def_postcmd FROM OGP_DB_PREFIXconfig_mods WHERE mod_cfg_id='" . $db->realEscapeSingle($mod_cfg_id) . "'");
  107. if ($config_mod_postcmd[0]['def_postcmd'] === NULL OR empty($config_mod_postcmd[0]['def_postcmd']))
  108. $postcmd = "";
  109. else
  110. $postcmd = $config_mod_postcmd[0]['def_postcmd'];
  111. }
  112. else
  113. $postcmd = $game_mod_postcmd[0]['postcmd'];
  114. //Enable FTP account in remote server
  115. if ($ftp == "enabled")
  116. {
  117. $remote->ftp_mgr("useradd", $home_info['home_id'], $home_info['ftp_password'], $home_info['home_path']);
  118. $db->changeFtpStatus('enabled',$home_info['home_id']);
  119. }
  120. //Install files for this service in the remote server
  121. // -Steam
  122. $exec_folder_path = clean_path($home_info['home_path'] . "/" . $server_xml->exe_location );
  123. $exec_path = clean_path($exec_folder_path . "/" . $server_xml->server_exec_name );
  124. if ($install_method == "steam")
  125. {
  126. if ( $server_xml->installer == "steamcmd" )
  127. {
  128. if( preg_match("/win32/", $server_xml->game_key) OR preg_match("/win64/", $server_xml->game_key) )
  129. $cfg_os = "windows";
  130. elseif( preg_match("/linux/", $server_xml->game_key) )
  131. $cfg_os = "linux";
  132. // Some games like L4D2 require anonymous login
  133. if($mod_xml->installer_login){
  134. $login = $mod_xml->installer_login;
  135. $pass = '';
  136. }else{
  137. $login = $settings['steam_user'];
  138. $pass = $settings['steam_pass'];
  139. }
  140. $modname = ( $installer_name == '90' and !preg_match("/(cstrike|valve)/", $modkey) ) ? $modkey : '';
  141. $betaname = isset($mod_xml->betaname) ? $mod_xml->betaname : '';
  142. $betapwd = isset($mod_xml->betapwd) ? $mod_xml->betapwd : '';
  143. $arch = isset($mod_xml->steam_bitness) ? $mod_xml->steam_bitness : '';
  144. $remote->steam_cmd( $home_id,$home_info['home_path'],$installer_name,$modname,
  145. $betaname,$betapwd,$login,$pass,$settings['steam_guard'],
  146. $exec_folder_path,$exec_path,$precmd,$postcmd,$cfg_os,'',$arch);
  147. }
  148. }
  149. // -Rsync
  150. elseif ($install_method == "rsync")
  151. {
  152. //Rsync Server
  153. $url = "rsync.opengamepanel.org";
  154. //OS
  155. if( preg_match("/win32/", $server_xml->game_key) OR preg_match("/win64/", $server_xml->game_key) )
  156. $os = "windows";
  157. elseif( preg_match("/linux/", $server_xml->game_key) )
  158. $os = "linux";
  159. //Rsync Game Name
  160. if( isset($server_xml->lgsl_query_name) )
  161. {
  162. $rs_gname = $server_xml->lgsl_query_name;
  163. if($rs_gname == "quake3")
  164. {
  165. if($server_xml->game_name == "Quake 3")
  166. $rs_gname = "q3";
  167. }
  168. }
  169. elseif( isset($server_xml->gameq_query_name) )
  170. {
  171. $rs_gname = $server_xml->gameq_query_name;
  172. if($rs_gname == "minecraft")
  173. {
  174. if($server_xml->game_name == "Minecraft Tekkit")
  175. $rs_gname = "tekkit";
  176. elseif($server_xml->game_name == "Minecraft Bukkit")
  177. $rs_gname = "bukkit";
  178. }
  179. }
  180. elseif( isset($server_xml->protocol) )
  181. $rs_gname = $server_xml->protocol;
  182. else
  183. $rs_gname = $server_xml->mods->mod['key'];
  184. //Starting Sync
  185. $full_url = "$url/ogp_game_installer/$rs_gname/$os/";
  186. $remote->start_rsync_install($home_id,$home_info['home_path'],"$full_url",$exec_folder_path,$exec_path,$precmd,$postcmd);
  187. }
  188. // -Manual
  189. elseif ($install_method == "manual")
  190. {
  191. // Start File Download and uncompress
  192. $filename = !empty($manual_url) ? substr($manual_url, -9) : "";
  193. $remote->start_file_download($manual_url,$home_info['home_path'],$filename,"uncompress");
  194. }
  195. echo "<h4>".get_lang('success')."</h4><br><p>".get_lang('starting_installations')."</p><br>";
  196. }
  197. // Set expiration date in ogp database
  198. if ($order['invoice_duration'] == "hour")
  199. {
  200. $add_time = time() + ($order['qty'] * 60 * 60);
  201. $end_date = date('YmdHi',$add_time);
  202. $period_to_extend = time() + ( ( $order['qty'] * 60 * 60 ) + 900 ); // Fifteen minutes to extend or finish the server.
  203. $finish_date = date('YmdHi',$period_to_extend);
  204. }
  205. elseif ($order['invoice_duration'] == "month")
  206. {
  207. $end_date = date('YmdHi', strtotime('+'.$order['qty'].' month'));
  208. $finish_date = date('YmdHi', strtotime('+'.$order['qty'].' month 5 day')); // 5 days to extend or finish the server.
  209. }
  210. elseif ($order['invoice_duration'] == "year")
  211. {
  212. $end_date = date('YmdHi', strtotime('+'.$order['qty'].' year'));
  213. $finish_date = date('YmdHi', strtotime('+'.$order['qty'].' year 15 day')); // Fifteen days to extend or finish the server.
  214. }
  215. // set order expire date
  216. $db->query("UPDATE OGP_DB_PREFIXbilling_orders
  217. SET end_date='" . $db->realEscapeSingle($end_date) . "'
  218. WHERE order_id=".$db->realEscapeSingle($order_id));
  219. $db->query("UPDATE OGP_DB_PREFIXbilling_orders
  220. SET finish_date='" . $db->realEscapeSingle($finish_date) . "'
  221. WHERE order_id=".$db->realEscapeSingle($order_id));
  222. // Save home id created by this order
  223. $db->query("UPDATE OGP_DB_PREFIXbilling_orders
  224. SET home_id='" . $db->realEscapeSingle($home_id) . "' WHERE order_id=".$db->realEscapeSingle($order_id));
  225. }
  226. //Update Cart Payment Status as 3(paid and installed)
  227. $db->query("UPDATE OGP_DB_PREFIXbilling_carts
  228. SET paid=3
  229. WHERE cart_id=".$db->realEscapeSingle($cart_id));
  230. // Set payment/creation date
  231. $date = date('d/m/Y H:i');
  232. $db->query("UPDATE OGP_DB_PREFIXbilling_carts
  233. SET date='" . $db->realEscapeSingle($date) . "'
  234. WHERE cart_id=".$db->realEscapeSingle($cart_id));
  235. //Refresh to Game Monitor.
  236. $view->refresh("home.php?m=gamemanager&p=game_monitor");
  237. }
  238. }
  239. ?>