add_home.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 create_selection($selection,$flag)
  25. {
  26. return "<tr><td align='right'><label for='".clean_id_string($selection)."'>".get_lang($selection).":</label></td>
  27. <td align='left'><input id='".clean_id_string($selection)."' type='checkbox' name='".$selection."' value='1' checked='checked' /></td></tr><tr>
  28. <td align='left' class='info' colspan='2'>".get_lang($selection.'_info')."</td></tr>";
  29. }
  30. function exec_ogp_module()
  31. {
  32. global $db, $settings;
  33. global $view;
  34. echo "<h2>".get_lang('add_new_game_home')."</h2>";
  35. echo "<p><a href='?m=user_games'>&lt;&lt; ".get_lang('back_to_game_servers')."</a></p>";
  36. $default_home_dir = $settings["default_game_server_home_path_prefix"];
  37. $remote_servers = $db->getRemoteServers();
  38. if( $remote_servers === FALSE )
  39. {
  40. echo "<p class='note'>".get_lang('no_remote_servers_configured')."</p>
  41. <p><a href='?m=server'>".get_lang('add_remote_server')."</a></p>";
  42. return;
  43. }
  44. $game_cfgs = $db->getGameCfgs();
  45. $users = $db->getUserList();
  46. if ( $game_cfgs === FALSE )
  47. {
  48. echo "<p class='note'>".get_lang('no_game_configurations_found')." <a href='?m=config_games'>".get_lang('game_configurations')."</a></p>";
  49. return;
  50. }
  51. echo "<p> <span class='note'>".get_lang('note').":</span> ".get_lang('add_mods_note')."</p>";
  52. $selections = array();
  53. foreach($db->getModulesAccessRights() as $ar)
  54. $selections[$ar['description']] = $ar['flag'];
  55. if ( isset($_REQUEST['add_game_home']) )
  56. {
  57. $rserver_id = $_POST['rserver_id'];
  58. $home_cfg_id = $_POST['home_cfg_id'];
  59. $web_user_id = trim($_POST['web_user_id']);
  60. $control_password = genRandomString(8);
  61. $access_rights = "";
  62. $ftp = FALSE;
  63. foreach ($selections as $selection => $flag)
  64. {
  65. if (isset($_REQUEST[$selection]))
  66. {
  67. $access_rights .= $flag;
  68. if ($flag == "t")
  69. {
  70. $ftp = TRUE;
  71. }
  72. }
  73. }
  74. if ( empty( $web_user_id ) )
  75. {
  76. print_failure(get_lang('game_path_empty'));
  77. }
  78. else
  79. {
  80. foreach ( $game_cfgs as $row )
  81. {
  82. if($row['home_cfg_id'] == $home_cfg_id){
  83. $server_name = $row['game_name'];
  84. $game_key = $row['game_key'];
  85. $readable_game_key = substr($game_key, 0, stripos($game_key, "_"));
  86. $readable_game_key = strtolower($readable_game_key);
  87. }
  88. }
  89. foreach ( $remote_servers as $server )
  90. {
  91. if($server['remote_server_id'] == $rserver_id) $ogp_user = $server['ogp_user'];
  92. }
  93. foreach ( $users as $user )
  94. {
  95. if($user['user_id'] == $web_user_id) $web_user = $user['users_login'];
  96. }
  97. $ftppassword = genRandomString(8);
  98. // Game path logic
  99. $game_path = "/home/".$ogp_user."/OGP_User_Files/"; // Default
  100. $skipId = false;
  101. if(hasValue($default_home_dir)){
  102. // Replace some user supported variables with actual value.
  103. $game_path = $default_home_dir;
  104. $game_path = str_replace("{USERNAME}", $web_user, $game_path);
  105. if(stripos($game_path, "{SKIPID}") !== false){
  106. $skipId = true;
  107. }
  108. $game_path = str_replace("{SKIPID}", "", $game_path);
  109. $game_path = str_replace("{GAMEKEY}", $readable_game_key, $game_path);
  110. }
  111. if($game_path[strlen($game_path)-1] != "/"){ // Make sure the path ends with forward slash
  112. $game_path .= "/";
  113. }
  114. $game_path = clean_path($game_path); // Clean it
  115. // End game path logic
  116. if ( ( $new_home_id = $db->addGameHome($rserver_id,$web_user_id,$home_cfg_id,
  117. clean_path($game_path),$server_name,$control_password,$ftppassword,$skipId) )!== FALSE )
  118. {
  119. $success = $db->assignHomeTo("user",$web_user_id,$new_home_id,$access_rights);
  120. if($success){
  121. $home_info = $db->getGameHomeWithoutMods($new_home_id);
  122. require_once('includes/lib_remote.php');
  123. $remote = new OGPRemoteLibrary($home_info['agent_ip'],$home_info['agent_port'],$home_info['encryption_key'],$home_info['timeout']);
  124. // Create new home directory if it doesn't already exist
  125. $remote->exec("mkdir -p " . clean_path($game_path) . (!$skipId ? $new_home_id : ""));
  126. if($ftp)
  127. {
  128. $host_stat = $remote->status_chk();
  129. if( $host_stat === 1)
  130. $remote->ftp_mgr("useradd", $home_info['home_id'], $home_info['ftp_password'], $home_info['home_path']);
  131. $db->changeFtpStatus('enabled',$new_home_id);
  132. }
  133. print_success(get_lang('game_home_added'));
  134. $db->logger(get_lang('game_home_added')." ($server_name)");
  135. $view->refresh("?m=user_games&amp;p=edit&amp;home_id=$new_home_id", 0);
  136. }else{
  137. print_failure(get_lang_f("failed_to_assign_home_to_user", $new_home_id, $web_user . " " . $db->getError()));
  138. }
  139. }
  140. else
  141. {
  142. print_failure(get_lang_f("failed_to_add_home_to_db",$db->getError()));
  143. }
  144. }
  145. }
  146. // View form to add more servers.
  147. if( !isset($_POST['rserver_id']) )
  148. {
  149. echo "<form action='?m=user_games&amp;p=add' method='post'>";
  150. echo "<table class='center'>";
  151. echo "<tr><td class='right'>".get_lang('game_server')."</td><td class='left'><select onchange=".'"this.form.submit()"'." name='rserver_id'>\n";
  152. echo "<option>".get_lang('select_remote_server')."</option>\n";
  153. foreach ( $remote_servers as $server )
  154. {
  155. echo "<option value='".$server['remote_server_id']."'>".
  156. $server['remote_server_name']." (".$server['agent_ip'].")</option>\n";
  157. }
  158. echo "</select>\n";
  159. echo "</form>";
  160. echo "</td></tr></table>";
  161. }
  162. else
  163. {
  164. if(isset($_POST['rserver_id']))
  165. $rhost_id = $_POST['rserver_id'];
  166. $remote_server = $db->getRemoteServer($rhost_id);
  167. require_once('includes/lib_remote.php');
  168. $remote = new OGPRemoteLibrary($remote_server['agent_ip'],$remote_server['agent_port'],$remote_server['encryption_key'],$remote_server['timeout']);
  169. $host_stat = $remote->status_chk();
  170. if( $host_stat === 1)
  171. $os = $remote->what_os();
  172. else
  173. {
  174. print_failure(get_lang_f("caution_agent_offline_can_not_get_os_and_arch_showing_servers_for_all_platforms"));
  175. $os = "Unknown OS";
  176. }
  177. echo "<form action='?m=user_games&amp;p=add' method='post'>";
  178. echo "<table class='center'>";
  179. echo "<tr><td class='right'>".get_lang('game_type')."</td><td class='left'> <select name='home_cfg_id'>\n";
  180. echo get_game_selector($os, $game_cfgs);
  181. echo "</select>\n</td></tr>";
  182. // Select user
  183. echo "<tr><td class='right'>".get_lang('login').":</td>
  184. <td class='left'><select name='web_user_id'>";
  185. $users = $db->getUserList();
  186. foreach ( $users as $user ){
  187. // Only users and admins can be assigned homes... not subusers
  188. if(is_null($user['users_parent'])){
  189. echo "<option value='".$user['user_id']."'>".$user['users_login']."</option>\n";
  190. }
  191. }
  192. echo "</select>\n</td></tr>";
  193. // Select permisions
  194. echo "<tr><td class='right'>".get_lang('access_rights').":</td>
  195. <td class='left'>";
  196. foreach ( $selections as $selection => $flag)
  197. {
  198. echo create_selection($selection,$flag);
  199. }
  200. echo "</td></tr>";
  201. // Assign home
  202. echo "<tr><td align='center' colspan='2'>
  203. <input type='hidden' name='rserver_id' value='".$rhost_id."' />".
  204. "<input type='submit' name='add_game_home' value='".
  205. get_lang('add_game_home')."' /></td></tr></table>";
  206. echo "</form>";
  207. }
  208. }
  209. ?>