1
0

ogp_api.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. <?php
  2. /*
  3. *
  4. * OGP - Open Game Panel
  5. * Copyright (C) 2008 - 2017 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. /******************
  25. * Functions *
  26. * ***************/
  27. function outPutJSON($result){
  28. global $action, $db;
  29. // Send JSON ouput
  30. header('Content-Type: application/json');
  31. echo json_encode($result);
  32. exit();
  33. }
  34. function runRemoteAction($action){
  35. global $settings, $db, $remote, $server_xml, $mod_xml, $server_home, $mod_key, $mod_id, $appId, $home_ip_ports, $homeId, $resultOp;
  36. // Load XML and server options
  37. $installer_name = isset($mod_xml->installer_name) ? $mod_xml->installer_name : $mod_key;
  38. $modname = $installer_name == '90' ? $mod_key : '';
  39. $betaname = isset($mod_xml->betaname) ? $mod_xml->betaname : '';
  40. $betapwd = isset($mod_xml->betapwd) ? $mod_xml->betapwd : '';
  41. $login = $mod_xml->installer_login ? $mod_xml->installer_login : $settings['steam_user'];
  42. $pass = $mod_xml->installer_login ? '' : $settings['steam_pass'];
  43. $exec_folder_path = $server_xml->exe_location;
  44. $exec_path = $server_xml->server_exec_name;
  45. $precmd = $server_home['mods'][$mod_id]['precmd'] == "" ? ($server_home['mods'][$mod_id]['def_precmd'] == "" ? $server_xml->pre_install : $server_home['mods'][$mod_id]['def_precmd'] ) : $server_home['mods'][$mod_id]['precmd'];
  46. $postcmd = $server_home['mods'][$mod_id]['postcmd'] == "" ? ($server_home['mods'][$mod_id]['def_postcmd'] == "" ? $server_xml->post_install : $server_home['mods'][$mod_id]['def_precmd'] ) : $server_home['mods'][$mod_id]['postcmd'];
  47. $cfg_os = preg_match('/win32|win64/', $server_xml->game_key) ? 'windows' : 'linux';
  48. $lockFiles = !empty($server_xml->lock_files) ? trim($server_xml->lock_files) : '';
  49. $preStart = !empty($server_xml->pre_start) ? trim($server_xml->pre_start) : '';
  50. $envVars = !empty($server_xml->environment_variables) ? trim($server_xml->environment_variables) : '';
  51. $startup_cmd = get_start_cmd($remote, $server_xml, $server_home, $mod_id, $home_ip_ports['ip'], $home_ip_ports['port'], $db);
  52. switch($action){
  53. case "steam_auto_update":
  54. // Make the update call
  55. $remoteResult = $remote->automatic_steam_update(
  56. //generic
  57. $homeId, $server_home['home_path'], $home_ip_ports['ip'], $home_ip_ports['port'], $exec_path, $exec_folder_path,
  58. //stop
  59. $server_xml->control_protocol, $server_home['control_password'], $server_xml->control_type,
  60. //update
  61. $appId, $modname, $betaname, $betapwd, $login, $pass, $settings['steam_guard'], $precmd, $postcmd, $cfg_os, $lockFiles,
  62. //start
  63. $startup_cmd, $server_home['mods'][$mod_id]['cpu_affinity'], $server_home['mods'][$mod_id]['nice'], $preStart, $envVars, $server_xml->game_key
  64. );
  65. break;
  66. case "restart_server":
  67. $remoteResult = $remote->remote_restart_server($server_home['home_id'],
  68. $home_ip_ports['ip'],
  69. $home_ip_ports['port'],
  70. $server_xml->control_protocol,
  71. $server_home['control_password'],
  72. $server_xml->control_protocol_type,
  73. $server_home['home_path'],
  74. $server_xml->server_exec_name,
  75. $server_xml->exe_location,
  76. $startup_cmd,
  77. $server_home['mods'][$mod_id]['cpu_affinity'],
  78. $server_home['mods'][$mod_id]['nice'],
  79. $preStart,
  80. $envVars,
  81. $server_xml->game_key
  82. );
  83. break;
  84. case "stop_server":
  85. $remoteResult = $remote->remote_stop_server($server_home['home_id'],
  86. $home_ip_ports['ip'],
  87. $home_ip_ports['port'],
  88. $server_xml->control_protocol,
  89. $server_home['control_password'],
  90. $server_xml->control_protocol_type,
  91. $server_home['home_path']
  92. );
  93. break;
  94. case "start_server":
  95. $remoteResult = $remote->universal_start($server_home['home_id'],
  96. $server_home['home_path'],
  97. $server_xml->server_exec_name,
  98. $server_xml->exe_location,
  99. $startup_cmd,
  100. $home_ip_ports['port'],
  101. $home_ip_ports['ip'],
  102. $server_home['mods'][$mod_id]['cpu_affinity'],
  103. $server_home['mods'][$mod_id]['nice'],
  104. $preStart,
  105. $envVars,
  106. $server_xml->game_key);
  107. break;
  108. }
  109. if($remoteResult == 1){
  110. return true;
  111. }else if(hasValue($remoteResult, true)){
  112. return $remoteResult;
  113. }
  114. return false;
  115. }
  116. function callSteamAutoUpdate(){
  117. global $settings, $db, $remote, $server_xml, $mod_xml, $server_home, $mod_key, $mod_id, $appId, $home_ip_ports, $resultOp;
  118. if($server_xml->installer == 'steamcmd') {
  119. if($remote->rfile_exists($server_home['home_path'] . '/steamapps/appmanifest_' . $appId . '.acf') === 1){
  120. $ourVersion = $remote->installed_steam_version($server_home['home_path'], $appId, 0);
  121. $steamVersion = $remote->fetch_steam_version($appId, 0);
  122. if($ourVersion != $steamVersion){
  123. $success = runRemoteAction("steam_auto_update");
  124. if($success == 1){
  125. $resultOp["message"] = "Server \"" . $server_home["home_name"] . "\" has been successfully auto-updated via SteamCMD and restarted.";
  126. $resultOp["success"] = true;
  127. }else if($success == 2){
  128. $resultOp["message"] = "Server \"" . $server_home["home_name"] . "\" has been successfully auto-updated via SteamCMD.";
  129. $resultOp["success"] = true;
  130. }else{
  131. if(is_array($success)){
  132. $resultOp["message"] = "Server \"" . $server_home["home_name"] . "\" failed to auto-update. Agent returned: " . print_r($success, true);
  133. }else{
  134. $resultOp["message"] = "Server \"" . $server_home["home_name"] . "\" failed to auto-update. Agent returned error code: " . $success;
  135. }
  136. $resultOp["success"] = false;
  137. }
  138. }else{
  139. $resultOp["message"] = "Server is already up-to-date.";
  140. $resultOp["success"] = false;
  141. }
  142. }else{
  143. $resultOp["message"] = "Unable to find appmanifest.";
  144. $resultOp["success"] = false;
  145. }
  146. }else{
  147. $resultOp["message"] = "Game server does NOT integrate directly with Steam.";
  148. $resultOp["success"] = false;
  149. }
  150. }
  151. function callRestartServer(){
  152. global $settings, $db, $remote, $server_xml, $mod_xml, $server_home, $mod_key, $mod_id, $appId, $home_ip_ports, $resultOp;
  153. $success = runRemoteAction("restart_server");
  154. if($success == 1){
  155. $resultOp["message"] = "Server \"" . $server_home["home_name"] . "\" has been successfully restarted.";
  156. $resultOp["success"] = true;
  157. }else{
  158. $resultOp["message"] = "Server \"" . $server_home["home_name"] . "\" failed to restart.";
  159. $resultOp["success"] = false;
  160. }
  161. }
  162. function callStartServer(){
  163. global $settings, $db, $remote, $server_xml, $mod_xml, $server_home, $mod_key, $mod_id, $appId, $home_ip_ports, $resultOp;
  164. $success = runRemoteAction("start_server");
  165. if($success == 1){
  166. $resultOp["message"] = "Server \"" . $server_home["home_name"] . "\" has been successfully started.";
  167. $resultOp["success"] = true;
  168. }else{
  169. $resultOp["message"] = "Server \"" . $server_home["home_name"] . "\" failed to start.";
  170. $resultOp["success"] = false;
  171. }
  172. }
  173. function callStopServer(){
  174. global $settings, $db, $remote, $server_xml, $mod_xml, $server_home, $mod_key, $mod_id, $appId, $home_ip_ports, $resultOp;
  175. $success = runRemoteAction("stop_server");
  176. if($success == 1){
  177. $resultOp["message"] = "Server \"" . $server_home["home_name"] . "\" has stopped successfully.";
  178. $resultOp["success"] = true;
  179. }else{
  180. $resultOp["message"] = "Server \"" . $server_home["home_name"] . "\" failed to stop.";
  181. $resultOp["success"] = false;
  182. }
  183. }
  184. /****************** *
  185. * CODE APP START *
  186. * ******************/
  187. // Report all PHP errors
  188. error_reporting(E_ERROR);
  189. // Path definitions
  190. define("IMAGES", "images/");
  191. define("INCLUDES", "includes/");
  192. define("MODULES", "modules/");
  193. define("CONFIG_FILE","includes/config.inc.php");
  194. require_once 'includes/functions.php';
  195. require_once 'includes/helpers.php';
  196. require_once 'includes/html_functions.php';
  197. require_once 'modules/config_games/server_config_parser.php';
  198. require_once 'modules/gamemanager/home_handling_functions.php';
  199. require_once 'includes/lib_remote.php';
  200. // Start the session valid for opengamepanel_web only
  201. startSession();
  202. require_once CONFIG_FILE;
  203. // Connect to the database server and select database.
  204. $db = createDatabaseConnection($db_type, $db_host, $db_user, $db_pass, $db_name, $table_prefix);
  205. $settings = $db->getSettings();
  206. @$GLOBALS['panel_language'] = $settings['panel_language'];
  207. // Handle API Request
  208. if(hasValue($_REQUEST["action"]) && hasValue($_REQUEST["homeid"]) && hasValue($_REQUEST["controlpass"]) && is_numeric($_REQUEST["homeid"])){
  209. // Get the variables we need
  210. $action = $_REQUEST["action"];
  211. $homeId = $_REQUEST["homeid"];
  212. $controlPass = $_REQUEST["controlpass"];
  213. // Get home information
  214. $server_home = $db->getGameHome($homeId);
  215. if(hasValue($server_home) && is_array($server_home) && count($server_home) > 0){
  216. if(trim($server_home["control_password"]) == trim(strip_tags($controlPass))){
  217. // Set command server variables (home server XML, IPs, etc)
  218. $server_xml = read_server_config(SERVER_CONFIG_LOCATION . '/' . $server_home['home_cfg_file']);
  219. if($server_xml){
  220. $getIpPorts = $db->getHomeIpPorts($homeId);
  221. $home_ip_ports = $getIpPorts[0];
  222. $remote = new OGPRemoteLibrary($server_home['agent_ip'], $server_home['agent_port'], $server_home['encryption_key'], $server_home['timeout']);
  223. $appId = (int)$server_xml->mods->mod->installer_name;
  224. $mod_id = key($server_home['mods']);
  225. $mod_key = $server_home['mods'][$mod_id]['mod_key'];
  226. $mod_xml = xml_get_mod($server_xml, $mod_key);
  227. if($mod_xml !== false){
  228. /****************************************/
  229. // Actual API Logic :) /
  230. /****************************************/
  231. // Handle API Action
  232. switch($action){
  233. case "autoUpdateSteamHome":
  234. callSteamAutoUpdate();
  235. break;
  236. case "restartServer":
  237. callRestartServer();
  238. break;
  239. case "startServer":
  240. callStartServer();
  241. break;
  242. case "stopServer":
  243. callStopServer();
  244. break;
  245. default:
  246. $resultOp["message"] = "Invalid action specified.";
  247. $resultOp["success"] = false;
  248. }
  249. /****************************************/
  250. // End Actual API Logic :) /
  251. /****************************************/
  252. }else{
  253. $resultOp["message"] = "Problem retrieving server mod XML.";
  254. $resultOp["success"] = false;
  255. }
  256. }else{
  257. $resultOp["message"] = "Failed to read server XML.";
  258. $resultOp["success"] = false;
  259. }
  260. }else{
  261. $resultOp["message"] = "Server home key does not match stored information.";
  262. $resultOp["success"] = false;
  263. }
  264. }else{
  265. $resultOp["message"] = "Unable to find game server home.";
  266. $resultOp["success"] = false;
  267. }
  268. }else{
  269. $resultOp["message"] = "Invalid inputs.";
  270. $resultOp["success"] = false;
  271. }
  272. // Output JSON
  273. if(hasValue($resultOp["message"]) && hasValue($resultOp["success"], true)){
  274. outPutJSON($resultOp);
  275. }
  276. exit();
  277. ?>