ogp_api.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. /*
  3. *
  4. * OGP - Open Game Panel
  5. * Copyright (C) Copyright (C) 2008 - 2013 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 runSteamAutoUpdate(){
  35. global $settings, $db, $remote, $server_xml, $mod_xml, $server_home, $mod_key, $mod_id, $appId, $home_ip_ports;
  36. $installer_name = isset($mod_xml->installer_name) ? $mod_xml->installer_name : $mod_key;
  37. $modname = $installer_name == '90' ? $mod_key : '';
  38. $betaname = isset($mod_xml->betaname) ? $mod_xml->betaname : '';
  39. $betapwd = isset($mod_xml->betapwd) ? $mod_xml->betapwd : '';
  40. $login = $mod_xml->installer_login ? $mod_xml->installer_login : $settings['steam_user'];
  41. $pass = $mod_xml->installer_login ? '' : $settings['steam_pass'];
  42. $exec_folder_path = $server_xml->exe_location;
  43. $exec_path = $server_xml->server_exec_name;
  44. $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'];
  45. $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'];
  46. $cfg_os = preg_match('/win32|win64/', $server_xml->game_key) ? 'windows' : 'linux';
  47. $lockFiles = !empty($server_xml->lock_files) ? trim($server_xml->lock_files) : '';
  48. $preStart = !empty($server_xml->pre_start) ? trim($server_xml->pre_start) : '';
  49. $envVars = !empty($server_xml->environment_variables) ? trim($server_xml->environment_variables) : '';
  50. $startup_cmd = get_start_cmd($remote, $server_xml, $server_home, $mod_id, $home_ip_ports['ip'], $home_ip_ports['port'], $remote->what_os());
  51. // Make the update call
  52. $update = $remote->automatic_steam_update(
  53. //generic
  54. $homeId, $server_home['home_path'], $home_ip_ports['ip'], $home_ip_ports['port'], $exec_path, $exec_folder_path,
  55. //stop
  56. $server_xml->control_protocol, $server_home['control_password'], $server_xml->control_type,
  57. //update
  58. $appId, $modname, $betaname, $betapwd, $login, $pass, $settings['steam_guard'], $precmd, $postcmd, $cfg_os, $lockFiles,
  59. //start
  60. $startup_cmd, $server_home['mods'][$mod_id]['cpu_affinity'], $server_home['mods'][$mod_id]['nice'], $preStart, $envVars
  61. );
  62. if($update == 1){
  63. return true;
  64. }else if(hasValue($update, true)){
  65. return $update;
  66. }
  67. return false;
  68. }
  69. /****************** *
  70. * CODE APP START *
  71. * ******************/
  72. // Report all PHP errors
  73. error_reporting(E_ERROR);
  74. // Path definitions
  75. define("IMAGES", "images/");
  76. define("INCLUDES", "includes/");
  77. define("MODULES", "modules/");
  78. define("CONFIG_FILE","includes/config.inc.php");
  79. require_once 'includes/functions.php';
  80. require_once 'includes/helpers.php';
  81. require_once 'includes/html_functions.php';
  82. require_once 'modules/config_games/server_config_parser.php';
  83. require_once 'modules/gamemanager/home_handling_functions.php';
  84. require_once 'includes/lib_remote.php';
  85. // Start the session valid for opengamepanel_web only
  86. startSession();
  87. require_once CONFIG_FILE;
  88. // Connect to the database server and select database.
  89. $db = createDatabaseConnection($db_type, $db_host, $db_user, $db_pass, $db_name, $table_prefix);
  90. $settings = $db->getSettings();
  91. @$GLOBALS['panel_language'] = $settings['panel_language'];
  92. // Handle API Request
  93. if(isset($_REQUEST["action"])){
  94. $action = $_REQUEST["action"];
  95. switch($action){
  96. case "autoUpdateSteamHome":
  97. $homeId = $_REQUEST["homeid"];
  98. $controlPass = $_REQUEST["controlpass"];
  99. if(hasValue($homeId) && hasValue($controlPass) && is_numeric($homeId)){
  100. $server_home = $db->getGameHome($homeId);
  101. if(hasValue($server_home) && is_array($server_home) && count($server_home) > 0){
  102. if(trim($server_home["control_password"]) == trim(strip_tags($controlPass))){
  103. // Key matches what is stored in the database.
  104. $home_ip_ports = $db->getHomeIpPorts($homeId)[0];
  105. $server_xml = read_server_config(SERVER_CONFIG_LOCATION . '/' . $server_home['home_cfg_file']);
  106. if($server_xml->installer == 'steamcmd') {
  107. $remote = new OGPRemoteLibrary($server_home['agent_ip'], $server_home['agent_port'], $server_home['encryption_key'], $server_home['timeout']);
  108. $appId = (int)$server_xml->mods->mod->installer_name;
  109. $mod_id = key($server_home['mods']);
  110. $mod_key = $server_home['mods'][$mod_id]['mod_key'];
  111. $mod_xml = xml_get_mod($server_xml, $mod_key);
  112. if($remote->rfile_exists($server_home['home_path'] . '/steamapps/appmanifest_' . $appId . '.acf') === 1)
  113. {
  114. if($mod_xml !== false){
  115. $ourVersion = $remote->installed_steam_version($server_home['home_path'], $appId, 0);
  116. $steamVersion = $remote->fetch_steam_version($appId, 0);
  117. if($ourVersion != $steamVersion){
  118. $success = runSteamAutoUpdate($server_xml, $mod_xml, $server_home, $mod_key);
  119. if($success == 1){
  120. $resultOp["message"] = "Server \"" . $server_home["home_name"] . "\" has been successfully auto-updated via SteamCMD and restarted.";
  121. $resultOp["success"] = true;
  122. }else if($success == 2){
  123. $resultOp["message"] = "Server \"" . $server_home["home_name"] . "\" has been successfully auto-updated via SteamCMD.";
  124. $resultOp["success"] = true;
  125. }else{
  126. if(is_array($success)){
  127. $resultOp["message"] = "Server \"" . $server_home["home_name"] . "\" failed to auto-update. Agent returned: " . print_r($success, true);
  128. }else{
  129. $resultOp["message"] = "Server \"" . $server_home["home_name"] . "\" failed to auto-update. Agent returned error code: " . $success;
  130. }
  131. $resultOp["success"] = false;
  132. }
  133. }else{
  134. $resultOp["message"] = "Server is already up-to-date.";
  135. $resultOp["success"] = false;
  136. }
  137. }else{
  138. $resultOp["message"] = "Problem retrieving server mod XML.";
  139. $resultOp["success"] = false;
  140. }
  141. }else{
  142. $resultOp["message"] = "Unable to find appmanifest.";
  143. $resultOp["success"] = false;
  144. }
  145. }else{
  146. $resultOp["message"] = "Game server does NOT integrate directly with Steam.";
  147. $resultOp["success"] = false;
  148. }
  149. }else{
  150. $resultOp["message"] = "Server home key does not match stored information.";
  151. $resultOp["success"] = false;
  152. }
  153. }else{
  154. $resultOp["message"] = "Unable to find game server home.";
  155. $resultOp["success"] = false;
  156. }
  157. }else{
  158. $resultOp["message"] = "Invalid inputs.";
  159. $resultOp["success"] = false;
  160. }
  161. break;
  162. }
  163. // Output JSON
  164. if(hasValue($resultOp["message"]) && hasValue($resultOp["success"], true)){
  165. outPutJSON($resultOp);
  166. }
  167. }
  168. exit();
  169. ?>