addadmin_helper.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. /*
  3. *
  4. * OGP - Open Game Panel
  5. * Copyright (C) Copyright (C) 2008 - 2012 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. include 'util_config.php';
  25. session_name($sessionName);
  26. session_start();
  27. if(!empty($_SESSION['user_id']) === true){
  28. // This entire section is nothing but a big, messy, workaround.
  29. // Make ogpLang happy.
  30. $_REQUEST['m'] = 'util';
  31. // We need to change directory to be able to include lib_remote and make a database connection without any errors
  32. // This is becasue the following files include other files via their relative path rather than absolute path... could be fixed by editing them... but until then, this is just a hacky workaround.
  33. $cwd = getcwd();
  34. if(chdir('../../') === true){
  35. require_once('includes/config.inc.php');
  36. require_once('includes/helpers.php');
  37. require_once('includes/lib_remote.php');
  38. include_once("includes/lang.php");
  39. ogpLang();
  40. $db = createDatabaseConnection($db_type, $db_host, $db_user, $db_pass, $db_name, $table_prefix);
  41. }else{
  42. die(get_lang('chdir_failed'));
  43. }
  44. if(chdir($cwd) === false){
  45. die(get_lang('chdir_failed'));
  46. }
  47. // Get gameservers belonging to each user with matching permissions if they're not an admin.
  48. function getUserServers($servers, $flags){
  49. global $db, $supportedGames;
  50. $info = array();
  51. $userInfo = $db->getUserById($_SESSION['user_id']);
  52. $userRole = $userInfo['users_role'];
  53. if(!empty($servers)){
  54. foreach($servers as $server){
  55. $gamehome = $db->getUserGameHome($_SESSION['user_id'], $server['home_id']);
  56. if(in_array($gamehome['game_name'], $supportedGames) === true){
  57. if($userRole !== 'admin'){
  58. if(strpbrk($gamehome['access_rights'], $flags) !== false){
  59. $info[] = $server;
  60. }
  61. }else{
  62. $info[] = $server;
  63. }
  64. }
  65. }
  66. }
  67. return $info;
  68. }
  69. $servers = getUserServers($db->getIpPortsForUser($_SESSION['user_id']), $subuserAdminManagement);
  70. // If it's a post request and the user is signed in - process it.
  71. // Otherwise, remove some sensitive info (such as encryption_key) from $servers - only keeping what the user needs to see and json_encode it for JS to process.
  72. if($_SERVER['REQUEST_METHOD'] === 'POST'){
  73. $serverInfo = array();
  74. $flags = '';
  75. $sourcemodFlags = range('a', 't');
  76. $immunityRange = range(1, 99);
  77. // Don't use isset here because they're always going to be set if the form is submitted - we only want to process the data if the following isn't empty.
  78. if(!empty($_POST['gameserver_id']) && !empty($_POST['remote_server_id']) && !empty($_POST['gameserver_name'])
  79. && !empty($_POST['gameserver_ip']) && !empty($_POST['gameserver_port']) && !empty($_POST['addSteamid']) && !empty($_POST['sourcemod_perms'])){
  80. foreach($servers as $server){
  81. // Try to check if hidden form values have been manually edited. If not, process.
  82. if($server['remote_server_id'] == $_POST['remote_server_id'] && $server['home_id'] == $_POST['gameserver_id']
  83. && $server['game_name'] == $_POST['gameserver_name'] && $server['ip'] == $_POST['gameserver_ip'] && $server['port'] == $_POST['gameserver_port']){
  84. $serverInfo = $server;
  85. }
  86. }
  87. if(!empty($serverInfo)){
  88. $remote = new OGPRemoteLibrary($serverInfo['agent_ip'], $serverInfo['agent_port'], $serverInfo['encryption_key'], $serverInfo['timeout']);
  89. if($remote->status_chk() === 1){
  90. if(preg_match('/^STEAM_[01]:[01]:\d+$/', $_POST['addSteamid'])){
  91. $immunity = (!empty($_POST['immunity']) && in_array($_POST['immunity'], $immunityRange)) ? $_POST['immunity'] : '';
  92. if($_POST['sourcemod_perms'] == 'root'){
  93. $flags = 'z';
  94. }elseif($_POST['sourcemod_perms'] == 'custom'){
  95. if(!empty($_POST['flags']) && is_array($_POST['flags'])){
  96. $x = array_intersect(array_values($_POST['flags']), $sourcemodFlags);
  97. $flags = implode('', ($x));
  98. }
  99. }
  100. if(!empty($flags)){
  101. $adminFile = $serverInfo['home_path'].'/'.$serverInfo['mod_key'].'/'.$adminFiles['sourcemod'];
  102. // Build up what the new line will be.
  103. $newLine = "\"{$_POST['addSteamid']}\"\t\"".(!empty($immunity) ? $immunity.':' : '').$flags."\"";
  104. // Only process if the $adminFile exists;
  105. if($remote->rfile_exists($adminFile) === 1){
  106. $remote->remote_readfile($adminFile, $file_content);
  107. // Decide if it's replacing an existing line or is a new line.
  108. if(preg_match('/'.$_POST['addSteamid'].'/i', $file_content)){
  109. $file_content = preg_replace('/.*'.$_POST['addSteamid'].'.*/i', $newLine, $file_content);
  110. }else{
  111. $file_content .= $newLine."\r\n";
  112. }
  113. if($remote->remote_writefile($adminFile, $file_content) === 1){
  114. if(!empty($serverInfo['control_password'])){
  115. $reloadAdmins = $remote->remote_send_rcon_command($serverInfo['home_id'], $serverInfo['ip'], $serverInfo['port'], 'rcon2', $serverInfo['control_password'], '', 'sm_reloadadmins', $return);
  116. if($reloadAdmins === -1){
  117. echo get_lang('rcon_reload_admins_failed');
  118. }elseif($reloadAdmins === 1){
  119. if(preg_match('/Admin cache has been refreshed/i', $return)){
  120. echo get_lang_f('reload_admins_success', $_POST['addSteamid']);
  121. }else{
  122. echo get_lang('reload_admins_failed');
  123. }
  124. }
  125. }else{
  126. // No rcon password stored - can't reload admins
  127. echo get_lang_f('add_success_no_rcon', $_POST['addSteamid']);
  128. }
  129. }else{
  130. // There was a problem writing to the admin file.
  131. echo get_lang_f('writefile_error', $adminFile);
  132. }
  133. }else{
  134. // The remote admin file doesn't exist.
  135. echo get_lang_f('remotefile_nonexistent', $adminFiles['sourcemod']);
  136. }
  137. }else{
  138. // There wasn't any flags specified.
  139. echo get_lang('empty_flag_list');
  140. }
  141. }else{
  142. // invalid steam_id format given.
  143. echo get_lang('invalid_steam_format');
  144. }
  145. }else{
  146. // Agent is offline. We can't add any admins here.
  147. echo get_lang('selected_server_offline');
  148. }
  149. }else{
  150. // the hidden input values don't exist in our servers array. however, they should exist.
  151. // if we're here: 1) the hidden variables have either been manually changed, or 2) the user was removed from accessing the selected server while still on the page.
  152. echo get_lang('malformed_form');
  153. }
  154. }else{
  155. // An empty form was submitted.
  156. echo get_lang('empty_form_data');
  157. }
  158. }else{
  159. $return = array();
  160. for($x = 0; $x < count($servers); ++$x){
  161. $return[] = array(
  162. 'remote_server_id' => $servers[$x]['remote_server_id'],
  163. 'ip' => $servers[$x]['ip'],
  164. 'port' => $servers[$x]['port'],
  165. 'home_id' => $servers[$x]['home_id'],
  166. 'home_name' => $servers[$x]['home_name'],
  167. 'game_name' => $servers[$x]['game_name'],
  168. );
  169. }
  170. echo json_encode($return);
  171. }
  172. }else{
  173. header('HTTP/1.0 403 Forbidden');
  174. exit;
  175. }
  176. ?>