1
0

amx_addadmin_helper.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 'includes/lib_remote.php';
  25. function exec_ogp_module()
  26. {
  27. global $db;
  28. include 'modules/util/functions.php';
  29. include 'modules/util/util_config.php';
  30. $servers = getUserServers($db->getIpPortsForUser($_SESSION['user_id']), $subuserAdminManagement, $amx_supportedGames);
  31. // If it's a post request and the user is signed in - process it.
  32. // 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.
  33. if($_SERVER['REQUEST_METHOD'] === 'POST'){
  34. $serverInfo = array();
  35. $flags = '';
  36. $amx_modFlags = range('a', 'u');
  37. // 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.
  38. if(!empty($_POST['amx_gameserver_id']) && !empty($_POST['remote_server_id']) && !empty($_POST['gameserver_name'])
  39. && !empty($_POST['gameserver_ip']) && !empty($_POST['gameserver_port']) && !empty($_POST['amx_mod_perms'])
  40. && !empty($_POST['amx_login_type']) && (!empty($_POST['amx_Steamid']) || !empty($_POST['amx_Nickname']) && !empty($_POST['amx_Password']))){
  41. foreach($servers as $server){
  42. // Try to check if hidden form values have been manually edited. If not, process.
  43. if($server['remote_server_id'] == $_POST['remote_server_id'] && $server['home_id'] == $_POST['amx_gameserver_id']
  44. && $server['game_name'] == $_POST['gameserver_name'] && $server['ip'] == $_POST['gameserver_ip'] && $server['port'] == $_POST['gameserver_port']){
  45. $serverInfo = $server;
  46. break;
  47. }
  48. }
  49. if(!empty($serverInfo)){
  50. $remote = new OGPRemoteLibrary($serverInfo['agent_ip'], $serverInfo['agent_port'], $serverInfo['encryption_key'], $serverInfo['timeout']);
  51. if($remote->status_chk() === 1){
  52. if(($_POST['amx_login_type'] == 'amx_login_steamid' && preg_match('/^STEAM_[01]:[01]:\d+$/', $_POST['amx_Steamid']))
  53. || ($_POST['amx_login_type'] == 'amx_login_nick_pass'
  54. && preg_match('/^[^\s][A-zÀ-ÿ0-9 !@)(,}\/|\.:?;{#$%&*+=-]{1,28}[^\s]$/', $_POST['amx_Nickname'])
  55. && preg_match('/^[^\s][A-zÀ-ÿ0-9 !@)(,}\/|\.:?;{#$%&*+=-]{1,28}[^\s]$/', $_POST['amx_Password']))){
  56. if($_POST['amx_mod_perms'] == 'root'){
  57. $flags = 'abcdefghijklmnopqrstu';
  58. }elseif($_POST['amx_mod_perms'] == 'custom'){
  59. if(!empty($_POST['amx_flags']) && is_array($_POST['amx_flags'])){
  60. $x = array_intersect(array_values($_POST['amx_flags']), $amx_modFlags);
  61. $flags = implode('', ($x));
  62. }
  63. }
  64. if(!empty($flags)){
  65. $adminFile = $serverInfo['home_path'].'/'.$serverInfo['mod_key'].'/'.$adminFiles['amx_mod'];
  66. // Build up what the new line will be.
  67. if($_POST['amx_login_type'] == 'amx_login_steamid')
  68. {
  69. $newLine = "\"{$_POST['amx_Steamid']}\" \"\" \"${flags}\" \"ce\"";
  70. }
  71. elseif($_POST['amx_login_type'] == 'amx_login_nick_pass')
  72. {
  73. $newLine = "\"{$_POST['amx_Nickname']}\" \"{$_POST['amx_Password']}\" \"${flags}\" \"a\"";
  74. }
  75. // Only process if the $adminFile exists;
  76. if($remote->rfile_exists($adminFile) === 1){
  77. $remote->remote_readfile($adminFile, $file_content);
  78. // Decide if it's replacing an existing line or is a new line.
  79. if($_POST['amx_login_type'] == 'amx_login_steamid')
  80. {
  81. if(preg_match('/'.preg_quote($_POST['amx_Steamid']).'/i', $file_content)){
  82. $file_content = preg_replace('/.*'.preg_quote($_POST['amx_Steamid']).'.*/i', $newLine, $file_content);
  83. }else{
  84. $file_content .= $newLine."\r\n";
  85. }
  86. }
  87. elseif($_POST['amx_login_type'] == 'amx_login_nick_pass')
  88. {
  89. if(preg_match('/'.preg_quote($_POST['amx_Nickname']).'/i', $file_content)){
  90. $file_content = preg_replace('/.*'.preg_quote($_POST['amx_Nickname']).'.*/i', $newLine, $file_content);
  91. }else{
  92. $file_content .= $newLine."\r\n";
  93. }
  94. }
  95. if($remote->remote_writefile($adminFile, $file_content) === 1){
  96. if(!empty($serverInfo['control_password'])){
  97. $reloadAdmins = $remote->remote_send_rcon_command($serverInfo['home_id'], $serverInfo['ip'], $serverInfo['port'], 'rcon', $serverInfo['control_password'], '', 'amx_reloadadmins', $return);
  98. if($reloadAdmins === -1){
  99. echo get_lang('rcon_reload_admins_failed');
  100. }elseif($reloadAdmins === 1){
  101. echo $return;
  102. }
  103. }else{
  104. // No rcon password stored - can't reload admins
  105. echo get_lang_f('add_success_no_rcon', $_POST['amx_Steamid']);
  106. }
  107. }else{
  108. // There was a problem writing to the admin file.
  109. echo get_lang_f('writefile_error', $adminFile);
  110. }
  111. }else{
  112. // The remote admin file doesn't exist.
  113. echo get_lang_f('remotefile_nonexistent', $adminFiles['amx_mod']);
  114. }
  115. }else{
  116. // There wasn't any flags specified.
  117. echo get_lang('empty_flag_list');
  118. }
  119. }else{
  120. // invalid steam_id format given.
  121. echo get_lang('invalid_steam_format');
  122. }
  123. }else{
  124. // Agent is offline. We can't add any admins here.
  125. echo get_lang('selected_server_offline');
  126. }
  127. }else{
  128. // the hidden input values don't exist in our servers array. however, they should exist.
  129. // 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.
  130. echo get_lang('malformed_form');
  131. }
  132. }else{
  133. // An empty form was submitted.
  134. echo get_lang('empty_form_data');
  135. }
  136. }else{
  137. $return = array();
  138. for($x = 0; $x < count($servers); ++$x){
  139. $return[] = array(
  140. 'remote_server_id' => $servers[$x]['remote_server_id'],
  141. 'ip' => $servers[$x]['ip'],
  142. 'port' => $servers[$x]['port'],
  143. 'home_id' => $servers[$x]['home_id'],
  144. 'home_name' => $servers[$x]['home_name'],
  145. 'game_name' => $servers[$x]['game_name'],
  146. );
  147. }
  148. echo json_encode($return);
  149. }
  150. }
  151. ?>