clone_home.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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 exec_ogp_module()
  25. {
  26. global $db, $settings;
  27. $home_id = $_REQUEST['home_id'];
  28. $default_home_dir = $settings["default_game_server_home_path_prefix"];
  29. $server_row = $db->getGameHomeWithoutMods($home_id);
  30. $game_key = $server_row["game_key"];
  31. $readable_game_key = substr($game_key, 0, stripos($game_key, "_"));
  32. $readable_game_key = strtolower($readable_game_key);
  33. if ( empty($server_row) )
  34. {
  35. print_failure(get_lang('invalid_home_id'));
  36. return;
  37. }
  38. echo "<h2>".get_lang_f('cloning_home',htmlentities($server_row['home_name']))."</h2>";
  39. echo create_back_button('user_games');
  40. include_once('includes/lib_remote.php');
  41. $remote = new OGPRemoteLibrary($server_row['agent_ip'],$server_row['agent_port'],$server_row['encryption_key'],$server_row['timeout']);
  42. if(isset($_REQUEST['clone_home']))
  43. {
  44. $server_name = $_POST['new_home_name'];
  45. $user_group = $_POST['user_group'];
  46. $web_user = $db->getUserById($server_row['user_id_main']);
  47. $web_user = $web_user["users_login"];
  48. // Game path logic
  49. $game_path = "/home/".$server_row['ogp_user']."/OGP_User_Files/"; // Default
  50. $skipId = false;
  51. if(hasValue($default_home_dir)){
  52. $game_path = $default_home_dir;
  53. $game_path = str_replace("{USERNAME}", $web_user, $game_path); // Replace some user supported variables with actual value.
  54. if(stripos($game_path, "{SKIPID}") !== false){
  55. $skipId = true;
  56. }
  57. $game_path = str_replace("{SKIPID}", "", $game_path);
  58. $game_path = str_replace("{GAMEKEY}", $readable_game_key, $game_path);
  59. }
  60. if($game_path[strlen($game_path)-1] != "/"){ // Make sure the path ends with forward slash
  61. $game_path .= "/";
  62. }
  63. $game_path = clean_path($game_path); // Clean it
  64. // End game path logic
  65. $clone_home_id = $db->addGameHome($server_row['remote_server_id'], $server_row['user_id_main'],
  66. $server_row['home_cfg_id'], $game_path, $server_name, '', genRandomString(8), $skipId);
  67. $server_path = $game_path;
  68. if(!$skipId)
  69. $server_path .= $clone_home_id;
  70. // Create new home directory if it doesn't already exist
  71. $remote->exec("mkdir -p " . clean_path($server_path));
  72. if ( $clone_home_id === FALSE )
  73. {
  74. print_failure(get_lang_f('cloning_of_home_failed',$home_id));
  75. return;
  76. }
  77. if( isset($_REQUEST['clone_mods']) )
  78. {
  79. $enabled_mods = $db->getHomeMods($home_id);
  80. if( empty($enabled_mods) )
  81. {
  82. print_failure(get_lang('note').": ".get_lang('no_mods_to_clone'));
  83. }
  84. else
  85. {
  86. foreach ( $enabled_mods as $enabled_rows )
  87. {
  88. if ( $db->addModToGameHome($clone_home_id,
  89. $enabled_rows['mod_cfg_id']) === FALSE )
  90. {
  91. print_failure(get_lang_f('failed_to_add_mod',$enabled_rows['mod_cfg'],
  92. $clone_home_id));
  93. return;
  94. }
  95. if ( $db->updateGameModParams($enabled_rows['max_players'],
  96. $enabled_rows['extra_params'],$enabled_rows['cpu_affinity'],
  97. $enabled_rows['nice'],$clone_home_id,
  98. $enabled_rows['mod_cfg_id']) === FALSE )
  99. {
  100. print_failure(get_lang_f('failed_to_update_mod_settings',$clone_home_id));
  101. return;
  102. }
  103. }
  104. print_success(get_lang_f('successfully_cloned_mods',$clone_home_id));
  105. }
  106. }
  107. print_success(get_lang('successfully_copied_home_database'));
  108. # do the remote copy call here
  109. echo "<p>".get_lang_f('copying_home_remotely',$server_row['home_path'],$server_path)."</p>";
  110. $db->logger(get_lang_f('copying_home_remotely',$server_row['home_path'],$server_path));
  111. $clone_rc = $remote->clone_home($server_row['home_path'],$server_path,$user_group);
  112. if($clone_rc == -1)
  113. {
  114. print_success(get_lang('game_server_copy_is_running'));
  115. ?>
  116. <div class="html5-progress-bar">
  117. <progress id="progressbar" value="0" max="100"></progress>
  118. <span class="progress-value">0%</span>
  119. </div>
  120. <script type="text/javascript">
  121. $(document).ready(function()
  122. {
  123. $.get( "home.php?m=user_games&type=cleared&p=get_size&home_id=<?php echo $home_id;?>&bytes", function( orig_data ) {
  124. var orig_size = parseInt(orig_data,10);
  125. var refreshId = setInterval(function() {
  126. $.get( "home.php?m=user_games&type=cleared&p=get_size&home_id=<?php echo $clone_home_id;?>&bytes", function( dest_data ) {
  127. var dest_size = parseInt(dest_data,10);
  128. var progress = parseInt(dest_size * 100 / orig_size);
  129. $('#progressbar').val(progress);
  130. $('.progress-value').html(progress + '%');
  131. if(progress == 100)
  132. {
  133. clearInterval(refreshId);
  134. }
  135. });
  136. }, 2000);
  137. });
  138. });
  139. </script>
  140. <?php
  141. }
  142. elseif($clone_rc == 1)
  143. {
  144. print_success(get_lang('game_server_copy_was_successful'));
  145. }
  146. else
  147. {
  148. print_failure(get_lang_f('game_server_copy_failed_with_return_code', $clone_rc));
  149. }
  150. echo "<br><a href='home.php?m=user_games'>&lt;&lt; ".get_lang('back_to_game_servers')."</a>";
  151. return;
  152. }
  153. // Form to edit game path.
  154. $avail_mods = $db->getHomeMods($home_id);
  155. $read_status = $remote->remote_readfile('/etc/passwd', $passwd_array);
  156. if ( $read_status === -1 )
  157. {
  158. print_failure(get_lang('agent_offline'));
  159. return;
  160. }
  161. else if ( $read_status == 1 )
  162. {
  163. $passwd_array = preg_split("/\n/",$passwd_array);
  164. }
  165. require_once("includes/form_table_class.php");
  166. $ft = new FormTable();
  167. $ft->start_form('?m=user_games&amp;p=clone');
  168. $ft->add_field_hidden('home_id',$home_id);
  169. $ft->start_table();
  170. $ft->add_custom_field('agent_ip',$server_row['agent_ip']);
  171. $ft->add_custom_field('current_home_path',$server_row['home_path']);
  172. $ft->add_field('string','new_home_name',htmlentities($server_row['home_name']));
  173. echo "<tr><td class='right'>".get_lang('clone_mods').":</td>
  174. <td class='left'><input type='checkbox' name='clone_mods' value='y' /></td></tr>";
  175. echo "<input name='user_group' type='hidden' value='".get_user_uid_gid_from_passwd($passwd_array,$server_row['ogp_user'])."' /></tr>";
  176. echo "</table>";
  177. $ft->add_button('submit','clone_home',get_lang('clone_home'));
  178. echo "<p class='info'>".get_lang('the_name_of_the_server_to_help_users_to_identify_it')."</p>";
  179. echo "</form>";
  180. echo "<h3>".get_lang('ips_and_ports_used_in_this_home')."</h3>";
  181. echo "<p>".get_lang('note_ips_and_ports_are_not_cloned')."</p>";
  182. $assigned = $db->getHomeIpPorts($home_id);
  183. if( !empty($assigned) )
  184. {
  185. foreach ( $assigned as $assigned_rows )
  186. {
  187. echo "<p>".$assigned_rows['ip'].":".$assigned_rows['port']."</p>\n";
  188. }
  189. }
  190. $enabled_mods = $db->getHomeMods($home_id);
  191. echo "<h3>".get_lang('mods_and_settings_for_this_game_server')."</h3>";
  192. if( empty($enabled_mods) )
  193. {
  194. print_failure(get_lang('note').": ".get_lang('note_no_mods'));
  195. return;
  196. }
  197. echo "<table class='center'>\n";
  198. echo "<tr><td>".get_lang('mod_name')."</td><td>".
  199. get_lang('max_players')."</td><td>".
  200. get_lang('extra_cmd_line_args')."</td><td>".
  201. get_lang('cpu_affinity')."</td><td>".
  202. get_lang('nice_level')."</td></tr>\n";
  203. foreach ( $enabled_mods as $enabled_rows )
  204. {
  205. echo "<tr>";
  206. echo "<td>".$enabled_rows['mod_name']."</td>";
  207. echo "<td>".$enabled_rows['max_players']."</td>";
  208. echo "<td>".$enabled_rows['extra_params']."</td>";
  209. echo "<td>".$enabled_rows['cpu_affinity']."</td>";
  210. echo "<td>".$enabled_rows['nice']."</td></tr>\n";
  211. }
  212. echo "</table>\n";
  213. }
  214. ?>