util.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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. function exec_ogp_module()
  25. {
  26. global $db;
  27. include 'util_config.php';
  28. $userInfo = $db->getUserById($_SESSION['user_id']);
  29. $userRole = $userInfo['users_role'];
  30. $commands = array();
  31. foreach($availableCommands as $command){
  32. if($userRole == 'admin' && $command['admin'] === true){
  33. $commands[] = '<option value="'.$command['title'].'">'.get_lang($command['title']).'</option>';
  34. }
  35. if($userRole == 'user' && $command['user'] === true){
  36. $commands[] = '<option value="'.$command['title'].'">'.get_lang($command['title']).'</option>';
  37. }
  38. if($userRole == 'subuser' && $command['subuser'] === true){
  39. $commands[] = '<option value="'.$command['title'].'">'.get_lang($command['title']).'</option>';
  40. }
  41. }
  42. ?>
  43. <h2><?php echo get_lang('module_name'); ?></h2>
  44. <div id="tabs">
  45. <ul>
  46. <li><a href="#tabs-1"><?php echo get_lang('network_tools'); ?></a></li>
  47. <li><a href="#tabs-2"><?php echo get_lang('sourcemod_admins'); ?></a></li>
  48. <li><a href="#tabs-3"><?php echo get_lang('steam_converter'); ?></a></li>
  49. </ul>
  50. <div id="tabs-1">
  51. <div>
  52. <span id="loading_agents" class="show"><?php echo get_lang('loading_agents'); ?></span>
  53. <span id="loading_failed" class="hide"><?php echo get_lang('loading_failed'); ?></span>
  54. <span id="no_commands" class="hide"><?php echo get_lang('no_commands'); ?></span>
  55. <span id="agents_offline" class="hide"><?php echo get_lang('agents_offline'); ?></span>
  56. </div>
  57. <div id="options" class="hide">
  58. <div><?php echo get_lang('your_ip'); ?> <span id="your-address"><?php echo getClientIPAddress(); ?></span></div>
  59. <form action="" method="POST" id="network_tools">
  60. <div id="select_agent"></div>
  61. <label for="command"><?php echo get_lang('command'); ?></label>
  62. <select id="command" name="command"><?php echo implode('', $commands); ?></select>
  63. <label for="remote_target"><?php echo get_lang('remote_target'); ?></label>
  64. <input type="text" id="remote_target" name="remote_target" required>
  65. <button type="submit"><?php echo get_lang('submit'); ?></button>
  66. </form>
  67. </div>
  68. <div id="loading" class="hide"></div>
  69. <div id="output" class="hide"></div>
  70. </div><!--/#tabs-1-->
  71. <div id="tabs-2">
  72. <div>
  73. <div>
  74. <span id="no_servers" class="hide"><?php echo get_lang('no_servers'); ?></span>
  75. </div>
  76. <div id="add_admin" class="hide">
  77. <form action="" method="POST" id="addadmin_form">
  78. <div id="games"></div>
  79. <label for="addSteamid"><?php echo get_lang('steamid'); ?></label>
  80. <input type="text" id="addSteamid" name="addSteamid" pattern="^STEAM_[01]:[01]:\d+$" required>
  81. <label for="immunity"><?php echo get_lang('immunity'); ?></label>
  82. <input type="number" id="immunity" name="immunity" min="1" max="99">
  83. <label for="sourcemod_perms"><?php echo get_lang('sourcemod_perms'); ?></label>
  84. <select id="sourcemod_perms" name="sourcemod_perms">
  85. <option value="root"><?php echo get_lang('sourcemod_perm_root'); ?></option>
  86. <option value="custom"><?php echo get_lang('sourcemod_perm_custom'); ?></option>
  87. </select>
  88. <div id="sourcemod_flagList" class="hide">
  89. <fieldset>
  90. <?php
  91. foreach(range('a', 't') as $flag){
  92. ?>
  93. <div class="item">
  94. <input type="checkbox" id="flag_<?php echo $flag; ?>" name="flags[]" value="<?php echo $flag; ?>">
  95. <label for="flag_<?php echo $flag; ?>"><?php echo get_lang('sourcemod_flag_'.$flag); ?></label>
  96. </div>
  97. <?php
  98. }
  99. ?>
  100. </fieldset>
  101. </div>
  102. <input type="hidden" name="remote_server_id" id="remote_server_id" value="">
  103. <input type="hidden" name="gameserver_name" id="gameserver_name" value="">
  104. <input type="hidden" name="gameserver_ip" id="gameserver_ip" value="">
  105. <input type="hidden" name="gameserver_port" id="gameserver_port" value="">
  106. <button type="submit"><?php echo get_lang('submit'); ?></button>
  107. </form>
  108. </div>
  109. <div id="invalid_server" class="hide"><?php echo get_lang('server_not_selected'); ?></div>
  110. <div id="invalid_steamid_admin" class="hide"><?php echo get_lang('invalid_steamid'); ?></div>
  111. <div id="invalid_response_admin" class="hide"><?php echo get_lang('post_failed'); ?></div>
  112. <div id="invalid_immunity" class="hide"><?php echo get_lang('invalid_immunity'); ?></div>
  113. <div id="all_servers_offline" class="hide"><?php echo get_lang('agents_offline'); ?></div>
  114. <div id="addadmin_response" class="hide"></div>
  115. </div>
  116. </div><!--/#tabs-2-->
  117. <div id="tabs-3">
  118. <div>
  119. <form action="" method="POST" id="steam_converter">
  120. <label for="steam_input">SteamID / SteamID3 / SteamID64 / CustomID:</label>
  121. <input type="text" name="steam_input" id="steam_input" required>
  122. <button type="submit"><?php echo get_lang('submit'); ?></button>
  123. </form>
  124. </div>
  125. <div id="invalid_steamid" class="hide"><?php echo get_lang('invalid_steamid'); ?></div>
  126. <div id="invalid_response" class="hide"><?php echo get_lang('post_failed'); ?></div>
  127. <div id="steam_info">
  128. <div id="steamLink"></div>
  129. <div id="steamId"></div>
  130. <div id="steamId3"></div>
  131. <div id="steamId64"></div>
  132. </div>
  133. </div><!--/#tabs-3-->
  134. </div> <!--/#tabs-->
  135. <script>
  136. $(function(){
  137. $("#tabs").tabs();
  138. $("#loading").removeClass('hide').addClass('show');
  139. // Load the agents via an external script so the user isn't waiting an eternity if they have several agents and multiple are offline
  140. $.getJSON("/modules/util/agents.php", function(data){
  141. var agents = "";
  142. var agentsOnline = 0;
  143. $("#loading_agents").removeClass('show').addClass('hide');
  144. $("#loading").removeClass('show').addClass('hide');
  145. $("#options").removeClass('hide').addClass('show');
  146. agents += "<label for=\"agent\"><?php echo get_lang('select_agent'); ?></label>\r\n"
  147. agents += "<select id=\"agent\" name=\"agent\">\r\n"
  148. for(var i = 0; i<data.length; ++i){
  149. agents += "\t<option value=\"" + data[i]['id'] + "\"" + (data[i]['status']===0?" disabled":"") + ">" + data[i]['name'] +"</option>\r\n";
  150. if(data[i]['status'] == 1){
  151. ++agentsOnline;
  152. }
  153. }
  154. agents += "</select>";
  155. if(agentsOnline == 0){
  156. $("#options").removeClass('show').addClass('hide');
  157. $("#agents_offline").removeClass('hide').addClass('show');
  158. // Hide the sourcemod add admin form if all agents are offline.
  159. $("#addadmin_form").addClass('hide');
  160. $("#all_servers_offline").removeClass('hide').addClass('show');
  161. }else if($("#command option").length == 0){
  162. $("#options").removeClass('show').addClass('hide');
  163. $("#no_commands").removeClass('hide').addClass('show');
  164. }else{
  165. $("#select_agent").html(agents);
  166. }
  167. }).fail(function(){
  168. $("#loading_agents").removeClass('show').addClass('hide');
  169. $("#loading").removeClass('show').addClass('hide');
  170. $("#loading_failed").removeClass('hide').addClass('show');
  171. });
  172. // Handle the network_tools form.
  173. $("#network_tools").on("submit", function(e){
  174. var target = $("#remote_target").val();
  175. var agent = $("#agent").val();
  176. var command = $("#command").val();
  177. // Some validation browser-side. Still need to do the same server-side.
  178. if(target.length === 0){
  179. // target input is empty.
  180. $("#output").removeClass('hide').addClass('show').html("<pre><?php echo get_lang('target_empty'); ?></pre>");
  181. }else if(agent.length === 0){
  182. // We'll only get to this point if there's no agents, or all agents are offline but the form was still submitted.
  183. $("#output").removeClass('hide').addClass('show').html("<pre><?php echo get_lang('agent_invalid'); ?></pre>");
  184. }else if(command.length === 0){
  185. // We'll only get to this point if there's no command specified.
  186. $("#output").removeClass('hide').addClass('show').html("<pre><?php echo get_lang('command_empty'); ?></pre>");
  187. }else{
  188. $("#loading").removeClass('hide').addClass('show');
  189. $("#output").removeClass('show').addClass('hide')
  190. $("#network_tools button").prop({disabled:true}); // Disable the submit button if we're about to post - preventing stuff being ran several times via spamming the button and/or enter.
  191. $.post("/modules/util/network_tools.php", $("#network_tools").serialize(), function(postCommand){
  192. $("#loading").removeClass('show').addClass('hide');
  193. $("#output").removeClass('hide').addClass('show').html("<pre>" + postCommand + "</pre>");
  194. $("#network_tools button").prop({disabled:false});
  195. }).fail(function(){
  196. $("#loading").removeClass('show').addClass('hide');
  197. $("#output").removeClass('hide').addClass('show').html("<pre><?php echo get_lang('post_failed'); ?></pre>");
  198. $("#network_tools button").prop({disabled:false});
  199. });
  200. }
  201. e.preventDefault();
  202. });// ./end network_tools form handling.
  203. // ----- Sourcemod Admins -----
  204. $.getJSON("/modules/util/addadmin_helper.php", function(gs){
  205. if(gs.length === 0){
  206. $("#no_servers").removeClass('hide').addClass('show');
  207. }else{
  208. var games = "";
  209. $("#add_admin").removeClass('hide').addClass('show');
  210. games += "<label for=\"gameserver_id\"><?php echo get_lang('select_server'); ?></label>\r\n"
  211. games += "<select id=\"gameserver_id\" name=\"gameserver_id\">\r\n"
  212. games += "<option value=\"0\" selected><?php echo get_lang('select_server_option'); ?></option>\r\n"
  213. for(var i = 0; i<gs.length; ++i){
  214. games += "\t<option value=\"" + gs[i]['home_id'] + "\">" + gs[i]['home_name'] +"</option>\r\n";
  215. }
  216. games += "</select>";
  217. $("#games").html(games);
  218. $("#gameserver_id").change(function(){
  219. var home = $("#gameserver_id").val();
  220. var gameserver_id = $("#gameserver_id").val();
  221. // although the disabled attribute is put on the option, set all the values to 0 if it's somehow chosen again via the form being edited
  222. if(gameserver_id == 0){
  223. $("#remote_server_id").val(0);
  224. $("#gameserver_name").val(0);
  225. $("#gameserver_ip").val(0);
  226. $("#gameserver_port").val(0);
  227. }else{
  228. for(var i = 0; i<gs.length; ++i){
  229. if(home === gs[i]['home_id']){
  230. $("option[value='0']").attr("disabled", "disabled");
  231. $("#remote_server_id").val(gs[i]['remote_server_id']);
  232. $("#gameserver_name").val(gs[i]['game_name']);
  233. $("#gameserver_ip").val(gs[i]['ip']);
  234. $("#gameserver_port").val(gs[i]['port']);
  235. break;
  236. }
  237. }
  238. }
  239. });
  240. }
  241. }); // ./end Sourcemod Admins
  242. $("#sourcemod_perms").change(function(){
  243. var sourcemod_perms = $("#sourcemod_perms").val();
  244. if(sourcemod_perms === 'root'){
  245. $("#sourcemod_flagList").removeClass('show').addClass('hide');
  246. $('.item input[type="checkbox"]').prop('checked', false);
  247. }else{
  248. $("#sourcemod_flagList").removeClass('hide').addClass('show');
  249. }
  250. });
  251. // Process the sourcemod admin form on submission.
  252. $("#addadmin_form").on("submit", function(e){
  253. $("#addadmin_form button").prop({disabled:true});
  254. var errors = 0;
  255. var remoteId = $("#remote_server_id").val();
  256. var gameName = $("#gameserver_name").val();
  257. var gameIp = $("#gameserver_ip").val();
  258. var gamePort = $("#gameserver_port").val();
  259. var addSteamid = $("#addSteamid").val();
  260. var immunity = $("#immunity").val();
  261. var permissionType = $("#sourcemod_perms").val();
  262. // Set the message divs back to defaults.
  263. $("#invalid_server").removeClass('show').addClass('hide');
  264. $("#invalid_steamid_admin").removeClass('show').addClass('hide');
  265. $("#invalid_response_admin").removeClass('show').addClass('hide');
  266. $("#invalid_immunity").removeClass('show').addClass('hide');
  267. $("#addadmin_response").removeClass('show').addClass('hide');
  268. if(remoteId.length === 0 || gameName.length === 0 || gameIp.length === 0 || gamePort.length === 0){
  269. $("#invalid_server").removeClass('hide').addClass('show');
  270. ++errors;
  271. }
  272. if(!(addSteamid.match(/^STEAM_[01]:[01]:\d+$/))){
  273. $("#invalid_steamid_admin").removeClass('hide').addClass('show');
  274. ++errors;
  275. }
  276. if(immunity.length > 2 || isNaN(immunity)){
  277. $("#invalid_immunity").removeClass('hide').addClass('show');
  278. ++errors;
  279. }
  280. if(errors === 0){
  281. $.post("/modules/util/addadmin_helper.php", $("#addadmin_form").serialize(), function(postCommand){
  282. $("#addadmin_response").removeClass('hide').addClass('show').html(postCommand);
  283. //$("#addadmin_form button").prop({disabled:false});
  284. }).fail(function(){
  285. $("#invalid_response_admin").removeClass('hide').addClass('show');
  286. $("#addadmin_form button").prop({disabled:false});
  287. });
  288. }else{
  289. $("#addadmin_form button").prop({disabled:false});
  290. }
  291. e.preventDefault();
  292. }); // add_admin form handling
  293. // ----- Steam Converter -----
  294. $("#steam_converter").on("submit", function(e){
  295. $("#steam_converter button").prop({disabled:true});
  296. $.post("/modules/util/steamid_converter.php", $("#steam_converter").serialize(), function(steam_data){
  297. var json = $.parseJSON(steam_data);
  298. if(json.length === 0){
  299. $("#steam_info").removeClass('show').addClass('hide');
  300. $("#invalid_steamid").removeClass('hide').addClass('show');
  301. $("#invalid_response").removeClass('show').addClass('hide');
  302. $("#steam_converter button").prop({disabled:false});
  303. }else{
  304. $("#steam_info").removeClass('hide').addClass('show');
  305. $("#invalid_steamid").removeClass('show').addClass('hide');
  306. $("#invalid_response").removeClass('show').addClass('hide');
  307. $("#steamLink").html('<b>Profile Link:</b> ' + json.steamProfile);
  308. $("#steamId").html('<b>Legacy ID:</b> ' + json.steamId);
  309. $("#steamId3").html('<b>SteamID3:</b> ' + json.steamId3);
  310. $("#steamId64").html('<b>SteamID64:</b> ' + json.communityId);
  311. $("#steam_converter button").prop({disabled:false});
  312. }
  313. }).fail(function(){
  314. $("#invalid_steamid").removeClass('show').addClass('hide');
  315. $("#steam_info").removeClass('show').addClass('hide');
  316. $("#invalid_response").removeClass('hide').addClass('show');
  317. });
  318. e.preventDefault();
  319. }); // ./end steam_converter form handling.
  320. $("#your-address").click(function(){
  321. $("#remote_target").val($("#your-address").text());
  322. });
  323. });
  324. </script>
  325. <?php
  326. }
  327. ?>