ogp_api.php 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830
  1. <?php
  2. /*
  3. ______________ Token Management
  4. ogp_api.php?token/create/{panel_user}/{panel_password}
  5. ogp_api.php?token/test/{token}
  6. ______________ Remote Servers
  7. ogp_api.php?server/list (POST/GET {token})
  8. ogp_api.php?server/status (POST/GET {token}{remote_server_id})
  9. ogp_api.php?server/restart (POST/GET {token}{remote_server_id})
  10. ogp_api.php?server/create (POST/GET {token}{agent_name}{agent_ip}{agent_port}{agent_user}{encryption_key}{ftp_ip}{ftp_port}{timeout}{use_nat}{display_public_ip})
  11. ogp_api.php?server/remove (POST/GET {token}{remote_server_id})
  12. ogp_api.php?server/add_ip (POST/GET {token}{remote_server_id}{ip})
  13. ogp_api.php?server/remove_ip (POST/GET {token}{remote_server_id}{ip})
  14. ogp_api.php?server/list_ips (POST/GET {token}{remote_server_id})
  15. ogp_api.php?server/edit_ip (POST/GET {token}{remote_server_id}{old_ip}{new_ip})
  16. ______________ Game Servers
  17. ogp_api.php?user_games/list_games (POST/GET {token}{system(windows|linux)}{architecture(32|64)})
  18. ogp_api.php?user_games/list_servers (POST/GET {token})
  19. ogp_api.php?user_games/create (POST/GET {token}{remote_server_id}{server_name}{home_cfg_id}{mod_cfg_id}{ip}{port}{control_password}{enable_ftp}{ftp_password}{slots}{affinity}{nice}{assign_to_username}{custom_user_login_homepath}) // {custom_user_login_homepath} is only used when a default_game_server_home_path_prefix setting is defined and used
  20. ogp_api.php?user_games/clone (POST/GET {token}{origin_home_id}{new_server_name}{new_ip}{new_port}{control_password}{enable_ftp}{ftp_password}{slots}{affinity}{nice}{assign_to_username}{custom_user_login_homepath}) // {custom_user_login_homepath} is only used when a default_game_server_home_path_prefix setting is defined and used
  21. ogp_api.php?user_games/set_expiration (POST/GET {token}{home_id}{timestamp})
  22. ______________ Users
  23. ogp_api.php?user_admin/list (POST/GET {token})
  24. ogp_api.php?user_admin/get (POST/GET {token}{email})
  25. ogp_api.php?user_admin/create (POST/GET {token}{email}{name}{password})
  26. ogp_api.php?user_admin/remove (POST/GET {token}{email})
  27. ogp_api.php?user_admin/set_expiration (POST/GET {token}{email}{timestamp})
  28. ogp_api.php?user_admin/list_assigned (POST/GET {token}{email})
  29. ogp_api.php?user_admin/assign (POST/GET {token}{email}{home_id}{timestamp})
  30. ogp_api.php?user_admin/remove_assign (POST/GET {token}{email}{home_id})
  31. ______________ Game Manager
  32. ogp_api.php?gamemanager/start (POST/GET {token}{ip}{port}{mod_key})
  33. ogp_api.php?gamemanager/stop (POST/GET {token}{ip}{port}{mod_key})
  34. ogp_api.php?gamemanager/restart (POST/GET {token}{ip}{port}{mod_key})
  35. ogp_api.php?gamemanager/rcon (POST/GET {token}{ip}{port}{mod_key}{command})
  36. ogp_api.php?gamemanager/update (POST/GET {token}{ip}{port}{mod_key}{type}{manual_url})
  37. ______________ Lite File Manager
  38. ogp_api.php?litefm/list (POST/GET {token}{ip}{port}{relative_path})
  39. ogp_api.php?litefm/get (POST/GET {token}{ip}{port}{relative_path})
  40. ogp_api.php?litefm/save (POST/GET {token}{ip}{port}{relative_path}{contents})
  41. ogp_api.php?litefm/remove (POST/GET {token}{ip}{port}{relative_path})
  42. ______________ Addons Manager
  43. ogp_api.php?addonsmanager/list (POST/GET {token})
  44. ogp_api.php?addonsmanager/install (POST/GET {token}{ip}{port}{mod_key}{addon_id})
  45. ______________ Steam Workshop
  46. ogp_api.php?steam_workshop/install (POST/GET {token}{ip}{port}{mods_list})
  47. *
  48. ______________ Panel Setting
  49. ogp_api.php?setting/get (POST/GET {token}{setting_name})
  50. */
  51. $main_request = key($_GET);
  52. $request = explode('/', $main_request);
  53. unset($_GET["$main_request"]);
  54. if(!empty($_GET))
  55. $_POST = array_merge($_POST,$_GET);
  56. // Allow json requests to the API as well
  57. $jsonData = file_get_contents('php://input');
  58. if(!empty($jsonData)){
  59. $jsonArr = json_decode($jsonData, true);
  60. }
  61. if(isset($jsonArr) && is_array($jsonArr) && !empty($jsonArr)){
  62. $_POST = array_merge($_POST, $jsonArr);
  63. }
  64. //Retirieve the function name
  65. $function = 'api_'.$request[0];
  66. //Remove the main function from the request
  67. array_splice($request, 0, 1);
  68. if(function_exists($function))
  69. {
  70. // Report only critical PHP errors
  71. error_reporting(E_ERROR);
  72. // Path definitions
  73. define("INCLUDES", "includes/");
  74. define("MODULES", "modules/");
  75. // require needed functions
  76. require_once INCLUDES.'functions.php';
  77. require_once INCLUDES.'helpers.php';
  78. require_once INCLUDES.'html_functions.php';
  79. require_once INCLUDES.'lib_remote.php';
  80. require_once INCLUDES.'config.inc.php';
  81. require_once MODULES.'config_games/server_config_parser.php';
  82. require_once INCLUDES.'api_functions.php';
  83. // API tokens table
  84. define("API_TABLE", $table_prefix."api_tokens");
  85. // Connect to the database server and select database.
  86. $db = createDatabaseConnection($db_type, $db_host, $db_user, $db_pass, $db_name, $table_prefix);
  87. $settings = $db->getSettings();
  88. if(!is_authorized())
  89. output(array("status" => '401', "message" => 'Unauthorized host'), $function);
  90. $db->checkApiTable();
  91. $logged_in = false;
  92. if($function != 'api_token')
  93. {
  94. if(isset($_POST['token']))
  95. {
  96. $token = $_POST['token'];
  97. $query = "SELECT user_id FROM ".API_TABLE." WHERE `token` = '".$db->real_escape_string($token)."';";
  98. $result = $db->resultQuery($query);
  99. if($result && isset($result[0]['user_id']))
  100. {
  101. $user_info = $db->getUserById($result[0]['user_id']);
  102. if(isset($user_info['users_login']))
  103. $logged_in = true;
  104. }
  105. }
  106. else
  107. {
  108. output(array("status" => "300", "message" => "No token supplied"), $function);
  109. }
  110. }
  111. if($logged_in or $function == 'api_token')
  112. {
  113. //call the function and output the returned data as json
  114. $func_req = str_replace('api_','',$function)."/".$request[0];
  115. if($main_request == "all")
  116. output(array("status" => "400", "message" => "BAD REQUEST"), $function);
  117. else
  118. $function_args = get_function_args("$func_req");
  119. if(!$function_args)
  120. output(array("status" => "400", "message" => "BAD REQUEST - CANT FIND FUNCTION ARGS"), $function);
  121. elseif(!(($func_req == "token/test" and isset($request[1])) OR ($func_req == "token/create" and isset($request[1]) and isset($request[2]))))
  122. {
  123. foreach($function_args as $arg => $mandatory)
  124. {
  125. if($mandatory and !isset($_POST["$arg"]))
  126. {
  127. output(array("status" => "400", "message" => "BAD REQUEST - MISSING REQUIRED ARGS", "fields_supplied" => $_POST, "fields_required" => $function_args), $function);
  128. break;
  129. }
  130. }
  131. }
  132. output($function(), $function);
  133. }
  134. else
  135. {
  136. output(array("status" => "301", "message" => "Invalid Token"), $function);
  137. }
  138. }
  139. else
  140. {
  141. output(array("status" => "400", "message" => "BAD REQUEST"), $function);
  142. }
  143. function output($result, $function){
  144. if($function == "api_setting"){
  145. if(is_array($result) && array_key_exists("status", $result) && $result["status"] != 200){
  146. outputPlainText("-1");
  147. }else{
  148. outputPlainText($result);
  149. }
  150. }else{
  151. outputJSON($result);
  152. }
  153. }
  154. function outputJSON($result){
  155. // Send JSON output
  156. header('Content-Type: application/json');
  157. echo json_encode($result);
  158. exit();
  159. }
  160. function outputPlainText($result){
  161. header("Content-Type: text/plain");
  162. echo $result;
  163. exit();
  164. }
  165. function isValidTimeStamp($timestamp)//https://stackoverflow.com/questions/2524680/check-whether-the-string-is-a-unix-timestamp
  166. {
  167. if(is_numeric($timestamp) and strtotime(date('d-m-Y H:i:s',$timestamp)) === (int)$timestamp)
  168. return true;
  169. return false;
  170. }
  171. function api_token()
  172. {
  173. global $request, $db;
  174. if($request[0] == "test")
  175. {
  176. $token = isset($request[1])?$request[1]:$_POST['token'];
  177. $query = "SELECT user_id FROM ".API_TABLE." WHERE `token` = '".$db->real_escape_string($token)."';";
  178. $result = $db->resultQuery($query);
  179. if($result && isset($result[0]['user_id']))
  180. {
  181. $user_info = $db->getUserById($result[0]['user_id']);
  182. if(isset($user_info['users_login']))
  183. {
  184. $status = "200";
  185. $message = $user_info['users_role'];
  186. }
  187. else
  188. {
  189. $status = "400";
  190. $message = "Invalid Token";
  191. }
  192. }
  193. else
  194. {
  195. $status = "400";
  196. $message = "Invalid Token";
  197. }
  198. }
  199. if($request[0] == "create")
  200. {
  201. $user = isset($request[1])?urldecode($request[1]):$_POST['user'];
  202. $password = isset($request[2])?urldecode($request[2]):$_POST['password'];
  203. $userInfo = $db->getUser($user);
  204. if(isset($userInfo['users_passwd']) && md5($password) === $userInfo['users_passwd'])
  205. {
  206. $token = bin2hex(openssl_random_pseudo_bytes(32));
  207. $old_token = $db->currentApiToken($userInfo['user_id']);
  208. // Update cronjob passwords in the URLs
  209. if($old_token and file_exists('modules/cron/shared_cron_functions.php')){
  210. require_once('modules/cron/shared_cron_functions.php');
  211. updateCronJobTokens($old_token, $token);
  212. }
  213. $query ="INSERT INTO ".API_TABLE.
  214. " (user_id, token)".
  215. " VALUES".
  216. " ('".$userInfo['user_id']."', '".$db->realEscapeSingle($token)."')".
  217. " ON DUPLICATE KEY UPDATE".
  218. " user_id = VALUES(user_id),".
  219. " token = VALUES(token);";
  220. if($db->query($query))
  221. {
  222. $status = "200";
  223. $message = $token;
  224. }
  225. else
  226. {
  227. $status = "500";
  228. $message = "database failure";
  229. }
  230. }
  231. else
  232. {
  233. $status = "400";
  234. $message = "Invalid login information";
  235. }
  236. }
  237. return array("status" => $status, "message" => $message);
  238. }
  239. function api_server()
  240. {
  241. global $request, $db, $user_info, $settings;
  242. if($user_info['users_role'] != "admin")
  243. return array("status" => '350', "message" => "This function is restricted to administrator accounts.");
  244. if($request[0] == "list")
  245. {
  246. $status = "200";
  247. $message = $db->getRemoteServers();
  248. }
  249. if($request[0] == "status")
  250. {
  251. $remote_server_id = $_POST['remote_server_id'];
  252. $remote_server = $db->getRemoteServer($remote_server_id);
  253. $remote = new OGPRemoteLibrary($remote_server['agent_ip'],$remote_server['agent_port'],$remote_server['encryption_key'],$remote_server['timeout']);
  254. $status = "200";
  255. $message = $remote->status_chk() == 1?'online':'offline';
  256. }
  257. if($request[0] == "restart")
  258. {
  259. $remote_server_id = $_POST['remote_server_id'];
  260. $remote_server = $db->getRemoteServer($remote_server_id);
  261. $remote = new OGPRemoteLibrary($remote_server['agent_ip'],$remote_server['agent_port'],$remote_server['encryption_key'],$remote_server['timeout']);
  262. $remote->agent_restart();
  263. $status = "200";
  264. $message = "success";
  265. }
  266. if($request[0] == "create")
  267. {
  268. $agent_name = $_POST['agent_name'];
  269. $agent_ip = $_POST['agent_ip'];
  270. $agent_port = $_POST['agent_port'];
  271. $agent_user = $_POST['agent_user'];
  272. $encryption_key = $_POST['encryption_key'];
  273. $ftp_ip = $_POST['ftp_ip'];
  274. $ftp_port = $_POST['ftp_port'];
  275. $timeout = $_POST['timeout'];
  276. $use_nat = $_POST['use_nat'];
  277. $display_public_ip = $_POST['display_public_ip'];
  278. $remote_server_id = $db->addRemoteServer($agent_ip,$agent_name,$agent_user,$agent_port,$ftp_ip,$ftp_port,$encryption_key,$timeout,$use_nat,$display_public_ip);
  279. $status = "200";
  280. $message = $remote_server_id;
  281. }
  282. if($request[0] == "remove")
  283. {
  284. $remote_server_id = $_POST['remote_server_id'];
  285. $status = "200";
  286. $message = $db->removeRemoteServer($remote_server_id);
  287. }
  288. if($request[0] == "add_ip")
  289. {
  290. $remote_server_id = $_POST['remote_server_id'];
  291. $ip = $_POST['ip'];
  292. $status = "200";
  293. $message = $db->addRemoteServerIP($remote_server_id, $ip);
  294. }
  295. if($request[0] == "remove_ip")
  296. {
  297. $remote_server_id = $_POST['remote_server_id'];
  298. $ip = $_POST['ip'];
  299. $ip_infos = $db->getRemoteServerIPs($remote_server_id);
  300. foreach($ip_infos as $ip_info)
  301. {
  302. if($ip_info['ip'] == $ip)
  303. {
  304. $message = $db->removeRemoteServerIPs($ip_info['ip_id']);
  305. break;
  306. }
  307. }
  308. $status = "200";
  309. }
  310. if($request[0] == "list_ips")
  311. {
  312. $remote_server_id = $_POST['remote_server_id'];
  313. $message = $db->getRemoteServerIPs($remote_server_id);
  314. $status = "200";
  315. }
  316. if($request[0] == "edit_ip")
  317. {
  318. $remote_server_id = $_POST['remote_server_id'];
  319. $old_ip = $_POST['old_ip'];
  320. $new_ip = $_POST['new_ip'];
  321. $ip_infos = $db->getRemoteServerIPs($remote_server_id);
  322. foreach($ip_infos as $ip_info)
  323. {
  324. if($ip_info['ip'] == $old_ip)
  325. {
  326. $message = $db->editRemoteServerIPs($ip_info['ip_id'], $new_ip);
  327. break;
  328. }
  329. }
  330. $status = "200";
  331. }
  332. return array("status" => $status, "message" => $message);
  333. }
  334. function api_user_games()
  335. {
  336. global $request, $db, $user_info, $settings;
  337. if($user_info['users_role'] != "admin")
  338. return array("status" => '350', "message" => "This function is restricted to administrator accounts.");
  339. if($request[0] == "list_games")
  340. {
  341. $system = strtolower($_POST['system']);
  342. if(!preg_match('/^(linux|windows)$/', $system))
  343. {
  344. $status = "302";
  345. $message = "list games: Incorrect system, valid options: windows, linux";
  346. return array("status" => $status, "message" => $message);
  347. }
  348. $architecture = strtolower($_POST['architecture']);
  349. if(!preg_match('/^(32|64)$/', $architecture))
  350. {
  351. $status = "303";
  352. $message = "list games: Incorrect architecture, valid options: 32, 64";
  353. return array("status" => $status, "message" => $message);
  354. }
  355. $games = $db->getGameCfgs();
  356. foreach($games as $key => $game)
  357. {
  358. $games[$key]['mods'] = $db->getCfgMods($game['home_cfg_id']);
  359. preg_match("/^([a-z0-9_-]+)_(linux|win)(32|64)?$/i",$game['game_key'],$matches);
  360. if(count($matches) == 4)
  361. list($game_key, $game_clean, $os, $arch) = $matches;
  362. else
  363. {
  364. list($game_key, $game_clean, $os) = $matches;
  365. $arch = "32";
  366. }
  367. if(strtolower($os) == 'linux')
  368. $sorted_games['linux'][$arch][] = $games[$key];
  369. elseif(strtolower($os) == 'win')
  370. $sorted_games['windows'][$arch][] = $games[$key];
  371. }
  372. $status = "200";
  373. $message = $sorted_games[$system][$architecture];
  374. }
  375. if($request[0] == "list_servers")
  376. {
  377. $status = "200";
  378. $message = $db->getGameHomes();
  379. }
  380. if($request[0] == "create")
  381. {
  382. $remote_server_id = $_POST['remote_server_id'];
  383. $server_name = $_POST['server_name'];
  384. $home_cfg_id = $_POST['home_cfg_id'];
  385. $mod_cfg_id = $_POST['mod_cfg_id'];
  386. $ip = $_POST['ip'];
  387. $port = $_POST['port'];
  388. $control_password = $_POST['control_password'];
  389. $enable_ftp = $_POST['enable_ftp'];
  390. $ftp_password = $_POST['ftp_password'];
  391. $slots = $_POST['slots'];
  392. $affinity = $_POST['affinity'];
  393. $nice = $_POST['nice'];
  394. $homeForUser = $user_info['users_login'];
  395. if(array_key_exists('custom_user_login_homepath', $_POST) && !empty($_POST['custom_user_login_homepath'])){
  396. $homeForUser = $_POST['custom_user_login_homepath'];
  397. }
  398. $assignGameServerToUserId = $user_info['user_id'];
  399. if(array_key_exists('assign_to_username', $_POST) && !empty($_POST['assign_to_username'])){
  400. $assignToAccountUser = $_POST['assign_to_username'];
  401. $userInfo = $db->getUser($assignToAccountUser);
  402. if($userInfo){
  403. $assignGameServerToUserId = $userInfo['user_id'];
  404. }
  405. }
  406. $remote_server = $db->getRemoteServer($remote_server_id);
  407. if($remote_server === FALSE)
  408. return array("status" => '304', "message" => "Remote Server ID#$remote_server_id does not exists");
  409. $game_cfg = $db->getGameCfg($home_cfg_id);
  410. if($game_cfg === FALSE)
  411. return array("status" => '305', "message" => "No game configuration found for home_cfg_id #" . $home_cfg_id . ".");
  412. $cfg_mods = $db->getCfgMods($home_cfg_id);
  413. $mod_key = FALSE;
  414. if($cfg_mods === FALSE)
  415. return array("status" => '306', "message" => "No game mods found for home_cfg_id #" . $home_cfg_id . ".");
  416. else
  417. {
  418. foreach($cfg_mods as $cfg_mod)
  419. {
  420. if($cfg_mod['mod_cfg_id'] == $mod_cfg_id)
  421. {
  422. $mod_key = $cfg_mod['mod_key'];
  423. break;
  424. }
  425. }
  426. }
  427. if($mod_key === FALSE)
  428. return array("status" => '307', "message" => "The mod_cfg_id #" . $mod_cfg_id . " does not belong to the game configuration for home_cfg_id #" . $home_cfg_id . ".");
  429. $ip_info = $db->resultQuery( "SELECT ip,ip_id FROM OGP_DB_PREFIXremote_server_ips WHERE ip='".$db->real_escape_string($ip)."' AND remote_server_id=".$db->real_escape_string($remote_server_id));
  430. if($ip_info === FALSE)
  431. return array("status" => '308', "message" => "The given IP address does not belongs to the given remote server.");
  432. $port = (int)(trim($port));
  433. if(!isPortValid($port))
  434. return array("status" => '309', "message" => "The given port is not a valid port.");
  435. $remote = new OGPRemoteLibrary($remote_server['agent_ip'],$remote_server['agent_port'],$remote_server['encryption_key'],$remote_server['timeout']);
  436. $host_stat = $remote->status_chk();
  437. if($host_stat !== 1)
  438. return array("status" => '310', "message" => "The remote server is offline.");
  439. // Game path logic
  440. $skipId = false;
  441. if(hasValue($settings["default_game_server_home_path_prefix"]))
  442. {
  443. // Replace some user supported variables with actual value.
  444. $game_path = str_replace("{USERNAME}", $homeForUser, $settings["default_game_server_home_path_prefix"]);
  445. if(stripos($game_path, "{SKIPID}") !== false){
  446. $game_path = str_replace("{SKIPID}", "", $game_path);
  447. $skipId = true;
  448. }
  449. $game_path = str_replace("{GAMEKEY}", strtolower(substr($game_cfg['game_key'], 0, stripos($game_cfg['game_key'], "_"))), $game_path);
  450. // Make sure the path ends with forward slash
  451. if($game_path[strlen($game_path)-1] != "/"){
  452. $game_path .= "/";
  453. }
  454. }
  455. else
  456. $game_path = "/home/".$remote_server['ogp_user']."/OGP_User_Files/"; // Default
  457. $game_path = clean_path($game_path); // Clean it
  458. $home_id = $db->addGameHome($remote_server_id, $assignGameServerToUserId, $home_cfg_id, $game_path, $server_name, $control_password, $ftp_password, $skipId);
  459. if($home_id === FALSE)
  460. return array("status" => '311', "message" => "Server could not be added to the database.");
  461. if($db->addGameIpPort($home_id, $ip_info[0]['ip_id'], $port) === FALSE)
  462. {
  463. $db->deleteGameHome($home_id);
  464. return array("status" => '312', "message" => "The given IP:Port is already in use.");
  465. }
  466. if($db->addModToGameHome($home_id, $mod_cfg_id) === FALSE )
  467. {
  468. $db->deleteGameHome($home_id);
  469. return array("status" => '313', "message" => "Failed to assing mod.");
  470. }
  471. if($db->updateGameModParams($slots, '', $affinity, $nice, $home_id, $mod_cfg_id) === FALSE)
  472. {
  473. $db->deleteGameHome($home_id);
  474. return array("status" => '314', "message" => "Maxplayers, affinity or nice could not be configured.");
  475. }
  476. // Create new home directory if it doesn't already exist
  477. $game_path = $game_path . (!$skipId ? $home_id : "");
  478. $remote->exec("mkdir -p " . $game_path);
  479. if($enable_ftp == "1")
  480. {
  481. $remote->ftp_mgr("useradd", $home_id, $ftp_password, $game_path);
  482. $db->changeFtpStatus('enabled',$home_id);
  483. }
  484. $status = "200";
  485. $message = $home_id;
  486. }
  487. if($request[0] == "clone")
  488. {
  489. $home_id = $_POST['origin_home_id'];
  490. $server_name = $_POST['new_server_name'];
  491. $ip = $_POST['new_ip'];
  492. $port = $_POST['new_port'];
  493. $control_password = $_POST['control_password'];
  494. $enable_ftp = $_POST['enable_ftp'];
  495. $ftp_password = $_POST['ftp_password'];
  496. $slots = $_POST['slots'];
  497. $affinity = $_POST['affinity'];
  498. $nice = $_POST['nice'];
  499. $homeForUser = $user_info['users_login'];
  500. if(array_key_exists('custom_user_login_homepath', $_POST) && !empty($_POST['custom_user_login_homepath'])){
  501. $homeForUser = $_POST['custom_user_login_homepath'];
  502. }
  503. $game_home = $db->getGameHome($home_id);
  504. if($game_home === FALSE)
  505. return array("status" => '315', "message" => "There is no game home with home_id #" . $home_id . ".");
  506. $assignGameServerToUserId = $game_home['user_id_main'];
  507. if(array_key_exists('assign_to_username', $_POST) && !empty($_POST['assign_to_username'])){
  508. $assignToAccountUser = $_POST['assign_to_username'];
  509. $userInfo = $db->getUser($assignToAccountUser);
  510. if($userInfo){
  511. $assignGameServerToUserId = $userInfo['user_id'];
  512. }
  513. }
  514. $remote = new OGPRemoteLibrary($game_home['agent_ip'],$game_home['agent_port'],$game_home['encryption_key'],$game_home['timeout']);
  515. $host_stat = $remote->status_chk();
  516. if($host_stat !== 1)
  517. return array("status" => '310', "message" => "The remote server is offline.");
  518. $ip_info = $db->resultQuery("SELECT ip,ip_id FROM OGP_DB_PREFIXremote_server_ips WHERE ip='".$db->real_escape_string($ip)."' AND remote_server_id=".$db->real_escape_string($game_home['remote_server_id']));
  519. if($ip_info === FALSE)
  520. return array("status" => '308', "message" => "The given IP address does not belongs to the given remote server.");
  521. $port = (int)(trim($port));
  522. if(!isPortValid($port))
  523. return array("status" => '309', "message" => "The given port is not a valid port.");
  524. // Game path logic
  525. $skipId = false;
  526. if(hasValue($settings["default_game_server_home_path_prefix"]))
  527. {
  528. // Replace some user supported variables with actual value.
  529. $game_path = str_replace("{USERNAME}", $homeForUser, $settings["default_game_server_home_path_prefix"]);
  530. if(stripos($game_path, "{SKIPID}") !== false){
  531. $game_path = str_replace("{SKIPID}", "", $game_path);
  532. $skipId = true;
  533. }
  534. $game_path = str_replace("{GAMEKEY}", strtolower(substr($game_home['game_key'], 0, stripos($game_home['game_key'], "_"))), $game_path);
  535. // Make sure the path ends with forward slash
  536. if($game_path[strlen($game_path)-1] != "/"){
  537. $game_path .= "/";
  538. }
  539. }
  540. else
  541. $game_path = "/home/".$game_home['ogp_user']."/OGP_User_Files/"; // Default
  542. $game_path = clean_path($game_path); // Clean it
  543. $clone_home_id = $db->addGameHome($game_home['remote_server_id'], $assignGameServerToUserId,
  544. $game_home['home_cfg_id'], $game_path, $server_name, $control_password, $ftp_password, $skipId);
  545. if ($clone_home_id === FALSE)
  546. return array("status" => '311', "message" => "Server could not be added to the database.");
  547. if($db->addGameIpPort($clone_home_id, $ip_info[0]['ip_id'], $port) === FALSE)
  548. {
  549. $db->deleteGameHome($clone_home_id);
  550. return array("status" => '312', "message" => "The given IP:Port is already in use.");
  551. }
  552. foreach ($game_home['mods'] as $mod_info)
  553. if($db->addModToGameHome($clone_home_id, $mod_info['mod_cfg_id']) !== FALSE)
  554. $db->updateGameModParams($slots, $mod_info['extra_params'], $affinity, $nice, $clone_home_id, $mod_info['mod_cfg_id']);
  555. // Create new home directory if it doesn't already exist
  556. $game_path = $game_path . (!$skipId ? $clone_home_id : "");
  557. $remote->exec("mkdir -p " . $game_path);
  558. if($enable_ftp == "1")
  559. {
  560. $remote->ftp_mgr("useradd", $clone_home_id, $ftp_password, $game_path);
  561. $db->changeFtpStatus('enabled', $clone_home_id);
  562. }
  563. $user_group = get_user_uid_gid_from_passwd(explode("\n", $remote->sudo_exec('cat /etc/passwd')), $game_home['ogp_user']);
  564. $status = "200";
  565. $message = array("clone_home_id" => $clone_home_id, "cloning_status" => $remote->clone_home($game_home['home_path'], $game_path, $user_group));
  566. }
  567. if($request[0] == "set_expiration")
  568. {
  569. $home_id = $_POST['home_id'];
  570. $date = date('d/m/Y H:i:s', $_POST['timestamp']);
  571. if($db->updateExpirationDate($home_id, $date, 'server') === TRUE)
  572. {
  573. $status = "200";
  574. $message = "Expiration date changed";
  575. }
  576. else
  577. {
  578. $status = "316";
  579. $message = "Expiration date could not be changed";
  580. }
  581. }
  582. return array("status" => $status, "message" => $message);
  583. }
  584. function api_user_admin()
  585. {
  586. global $request, $db, $user_info, $settings;
  587. if($user_info['users_role'] != "admin")
  588. return array("status" => '350', "message" => "This function is restricted to administrator accounts.");
  589. if($request[0] == "list")
  590. {
  591. $status = "200";
  592. $message = $db->getUserList();
  593. }
  594. if($request[0] == "get")
  595. {
  596. $email = $_POST['email'];
  597. $account = $db->getUserByEmail($email);
  598. if($account === FALSE)
  599. {
  600. $status = "317";
  601. $message = "There is no account with the given email address.";
  602. }
  603. else
  604. {
  605. $status = "200";
  606. $message = $account;
  607. }
  608. }
  609. if($request[0] == "create")
  610. {
  611. $name = $_POST['name'];
  612. $email = $_POST['email'];
  613. $password = $_POST['password'];
  614. if($db->addUser($name,$password,'user',$email) === FALSE)
  615. {
  616. $status = "318";
  617. $message = "Failed to create account, name or email already in use.";
  618. }
  619. else
  620. {
  621. $status = "200";
  622. $message = "Account created";
  623. }
  624. }
  625. if($request[0] == "remove")
  626. {
  627. $email = $_POST['email'];
  628. $account = $db->getUserByEmail($email);
  629. if($account === FALSE)
  630. {
  631. $status = "319";
  632. $message = "Failed to remove account, there is no account with the given email address ($email).";
  633. }
  634. else
  635. {
  636. $db->delUser($account['user_id']);
  637. $status = "200";
  638. $message = "Account removed successfully";
  639. }
  640. }
  641. if($request[0] == "set_expiration")
  642. {
  643. $email = $_POST['email'];
  644. $account = $db->getUserByEmail($email);
  645. if($account === FALSE)
  646. {
  647. $status = "319";
  648. $message = "There is no account with the given email address ($email).";
  649. }
  650. else
  651. {
  652. $date = (strtoupper($_POST['timestamp']) == 'X' or $_POST['timestamp'] == '') ? 'X': $date;
  653. if($date != 'X' and !isValidTimeStamp($_POST['timestamp']))
  654. {
  655. $status = "321";
  656. $message = "The given timestamp is not valid";
  657. }
  658. else
  659. {
  660. if($date != 'X')
  661. $date = $_POST['timestamp'];
  662. $account['user_expires'] = $date;
  663. if ($db->editUser($account, $account['user_id']) == FALSE)
  664. {
  665. $status = "316";
  666. $message = "Expiration date could not be changed";
  667. }
  668. else
  669. {
  670. $status = "200";
  671. $message = "Expiration date changed";
  672. }
  673. }
  674. }
  675. }
  676. if($request[0] == "list_assigned")
  677. {
  678. $email = $_POST['email'];
  679. $account = $db->getUserByEmail($email);
  680. if($account === FALSE)
  681. {
  682. $status = "319";
  683. $message = "There is no account with the given email address ($email).";
  684. }
  685. else
  686. {
  687. $game_homes = $db->getHomesFor('user',$account['user_id']);
  688. if(empty($game_homes))
  689. {
  690. $status = "323";
  691. $message = "There are no game homes assigned for the given user.";
  692. }
  693. else
  694. {
  695. $status = "200";
  696. $message = $game_homes;
  697. }
  698. }
  699. }
  700. if($request[0] == "assign")
  701. {
  702. $email = $_POST['email'];
  703. $home_id = $_POST['home_id'];
  704. $access_rights = strtolower($_POST['access_rights']);
  705. $game_home = $db->getGameHome($home_id);
  706. if($game_home === FALSE)
  707. return array("status" => '315', "message" => "There is no game home with home_id #" . $home_id . ".");
  708. if(!preg_match("/^u?f?p?e?t?c?$/", $access_rights))
  709. return array("status" => "324", "message" => "Ivalid string for access rights");
  710. $account = $db->getUserByEmail($email);
  711. if($account === FALSE)
  712. {
  713. $status = "319";
  714. $message = "There is no account with the given email address ($email).";
  715. }
  716. else
  717. {
  718. $date = (strtoupper($_POST['timestamp']) == 'X' or $_POST['timestamp'] == '') ? 'X': $date;
  719. if($date != 'X' and !isValidTimeStamp($_POST['timestamp']))
  720. {
  721. $status = "321";
  722. $message = "The given timestamp is not valid";
  723. }
  724. else
  725. {
  726. if($date != 'X')
  727. $date = date('d/m/Y H:i:s', $_POST['timestamp']);
  728. if ( $db->assignHomeTo('user', $account['user_id'], $home_id, $access_rights) === TRUE )
  729. {
  730. $db->updateExpirationDate($game_home['home_id'], $date, 'user', $account['user_id']);
  731. $status = "200";
  732. $message = "Home assigned successfully";
  733. }
  734. else
  735. {
  736. $status = "325";
  737. $message = "Home id#$home_id could not be assigned to $email.";
  738. }
  739. }
  740. }
  741. }
  742. if($request[0] == "remove_assign")
  743. {
  744. $email = $_POST['email'];
  745. $home_id = $_POST['home_id'];
  746. $game_home = $db->getGameHome($home_id);
  747. if($game_home === FALSE)
  748. return array("status" => '315', "message" => "There is no game home with home_id #" . $home_id . ".");
  749. $account = $db->getUserByEmail($email);
  750. if($account === FALSE)
  751. {
  752. $status = "319";
  753. $message = "There is no account with the given email address ($email).";
  754. }
  755. else
  756. {
  757. if ($db->unassignHomeFrom("user",$account['user_id'],$game_home['home_id']) === TRUE)
  758. {
  759. $status = "200";
  760. $message = "Home id#" . $game_home['home_id'] . " has been unnassigned from $email successfully.";
  761. }
  762. else
  763. {
  764. $status = "326";
  765. $message = "Home id#$home_id was not assigned to $email.";
  766. }
  767. }
  768. }
  769. return array("status" => $status, "message" => $message);
  770. }
  771. function api_gamemanager_admin()
  772. {
  773. global $request, $db, $user_info, $settings;
  774. $isAdmin = $db->isAdmin($user_info['user_id']);
  775. if($request[0] == "reorder")
  776. {
  777. if($isAdmin){
  778. $data = json_decode(file_get_contents('php://input'), true);
  779. if(array_key_exists("order", $data) && is_array($data["order"])){
  780. $updatedOrder = $db->saveGameServerOrder($data["order"]);
  781. if($updatedOrder){
  782. $status = "200";
  783. $message = "Game server order was successfully saved.";
  784. }else{
  785. $status = "335";
  786. $message = "Game server order was NOT saved.";
  787. }
  788. }else{
  789. $status = "335";
  790. $message = "Invalid inputs.";
  791. }
  792. }else{
  793. $status = "335";
  794. $message = "Only admin users can save the game server display order.";
  795. }
  796. }
  797. return array("status" => $status, "message" => $message);
  798. }
  799. function api_gamemanager()
  800. {
  801. global $request, $db, $user_info, $settings;
  802. $ip = trim($_POST['ip']);
  803. $port = (int) trim($_POST['port']);
  804. $mod_key = isset($_POST['mod_key'])?trim($_POST['mod_key']):'';
  805. if(!isPortValid($port))
  806. return array("status" => '309', "message" => "The given port is not a valid port.");
  807. if(!preg_match("/[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/",$ip))
  808. return array("status" => '327', "message" => "The given IP ($ip) is not a valid IP address.");
  809. $home_info = $db->getGameHomeByIP($ip, $port);
  810. if($home_info === FALSE)
  811. return array("status" => '328', "message" => "There is no game server with the given address ($ip:$port).");
  812. $mod_id = key($home_info['mods']);
  813. if($mod_key != '')
  814. {
  815. foreach($home_info['mods'] as $home_mod)
  816. {
  817. if($mod_key == $home_mod['mod_key'])
  818. {
  819. $mod_id = $home_mod['mod_id'];
  820. break;
  821. }
  822. }
  823. }
  824. $isAdmin = $db->isAdmin($user_info['user_id']);
  825. if($isAdmin)
  826. {
  827. $access_rights = 'ufpetc';
  828. }
  829. else
  830. {
  831. $game_home = $db->getUserGameHome($user_info['user_id'], $home_info['home_id']);
  832. if($game_home === FALSE)
  833. return array("status" => '329', "message" => "The given address ($ip:$port) does not belong to your account.");
  834. $access_rights = $game_home['access_rights'];
  835. }
  836. $server_xml = read_server_config(SERVER_CONFIG_LOCATION."/".$home_info['home_cfg_file']);
  837. if($server_xml === FALSE)
  838. return array("status" => '305', "message" => "No game configuration found for home_cfg_id #" . $home_cfg_id . ".");
  839. $remote = new OGPRemoteLibrary($home_info['agent_ip'],$home_info['agent_port'],$home_info['encryption_key'],$home_info['timeout']);
  840. $host_stat = $remote->status_chk();
  841. if($host_stat !== 1)
  842. return array("status" => '310', "message" => "The remote server is offline.");
  843. $os = $remote->what_os();
  844. if($request[0] == "start")
  845. {
  846. $start_cmd = get_start_cmd($user_info,$remote,$server_xml,$home_info,$mod_id,$ip,$port,$db);
  847. if(isset($server_xml->lgsl_query_name))
  848. require_once('protocol/lgsl/lgsl_protocol.php');
  849. // Do text replacements in cfg file
  850. if($server_xml->replace_texts)
  851. {
  852. foreach($home_info['mods'][$mod_id] as $key => $value)
  853. {
  854. $home_info[$key] = $value;
  855. }
  856. $server_home = $home_info;
  857. require_once(MODULES."gamemanager/cfg_text_replace.php");
  858. }
  859. // Run pre-start commands
  860. if(isset($server_xml->pre_start) && !empty($server_xml->pre_start)){
  861. $preStart = trim($server_xml->pre_start);
  862. }else{
  863. $preStart = "";
  864. }
  865. // Environment variables
  866. if(isset($server_xml->environment_variables) && !empty($server_xml->environment_variables)){
  867. $envVars = trim($server_xml->environment_variables);
  868. }else{
  869. $envVars = "";
  870. }
  871. // Additional files to lock
  872. if(isset($server_xml->lock_files) && !empty($server_xml->lock_files)){
  873. $lockFiles = trim($server_xml->lock_files);
  874. }else{
  875. $lockFiles = "";
  876. }
  877. if(!empty($lockFiles)){
  878. // Linux only call
  879. if(preg_match("/Linux/", $os)){
  880. $lockedFilesStatus = $remote->lock_additional_home_files($home_info['home_path'], $lockFiles, "lock");
  881. }
  882. }
  883. $start_retval = $remote->universal_start($home_info['home_id'],
  884. $home_info['home_path'],
  885. $server_xml->server_exec_name,
  886. $server_xml->exe_location,
  887. $start_cmd, $port, $ip,
  888. $home_info['mods'][$mod_id]['cpu_affinity'],
  889. $home_info['mods'][$mod_id]['nice'],
  890. $preStart,
  891. $envVars,
  892. $server_xml->game_key,
  893. (isset( $server_xml->console_log ) ? $server_xml->console_log : "")
  894. );
  895. if( $start_retval == AGENT_ERROR_NOT_EXECUTABLE)
  896. return array("status" => '330', "message" => "The server executable does not have execution permission.");
  897. elseif($start_retval <= 0)
  898. return array("status" => '331', "message" => "The server could not be started, already running.");
  899. else
  900. {
  901. $firewall_settings = $db->getFirewallSettings($home_info['remote_server_id']);
  902. if ($firewall_settings['status'] == "enable")
  903. {
  904. if ($server_xml->protocol == "gameq")
  905. {
  906. $query_port = get_query_port($server_xml, $port);
  907. }
  908. elseif ($server_xml->protocol == "lgsl")
  909. {
  910. $get_ports = lgsl_port_conversion((string)$server_xml->lgsl_query_name, $port, "", "");
  911. $query_port = $get_ports['1'];
  912. }
  913. elseif ($server_xml->protocol == "teamspeak3")
  914. {
  915. $query_port = $port + 24;
  916. }
  917. set_firewall($remote, $firewall_settings, 'allow', $port, $ip);
  918. if(isset($query_port) and $query_port != "" and $query_port != $port)
  919. set_firewall($remote, $firewall_settings, 'allow', $query_port, $ip);
  920. }
  921. $db->delServerStatusCache($home_info['ip_id'],$port);
  922. $status = "200";
  923. $message = "The server has been started.";
  924. }
  925. }
  926. if($request[0] == "stop")
  927. {
  928. $remote_retval = $remote->remote_stop_server($home_info['home_id'],
  929. $ip, $port, $server_xml->control_protocol,
  930. $home_info['control_password'],$server_xml->control_protocol_type, $home_info['home_path']);
  931. if($remote_retval === -1)
  932. return array("status" => '332', "message" => "The server could not be stopped.");
  933. elseif($remote_retval === -2)
  934. return array("status" => '332', "message" => "The server could not be stopped.");
  935. else
  936. {
  937. $firewall_settings = $db->getFirewallSettings($home_info['remote_server_id']);
  938. if ($firewall_settings['status'] == "enable")
  939. {
  940. if ($server_xml->protocol == "gameq")
  941. {
  942. $query_port = get_query_port($server_xml, $port);
  943. }
  944. elseif ($server_xml->protocol == "lgsl")
  945. {
  946. require('protocol/lgsl/lgsl_protocol.php');
  947. $get_ports = lgsl_port_conversion((string)$server_xml->lgsl_query_name, $port, "", "");
  948. $query_port = $get_ports['1'];
  949. }
  950. elseif ($server_xml->protocol == "teamspeak3")
  951. {
  952. $query_port = $port + 24;
  953. }
  954. set_firewall($remote, $firewall_settings, 'deny', $port, $ip);
  955. if(isset($query_port) and $query_port != "" and $query_port != $port)
  956. set_firewall($remote, $firewall_settings, 'deny', $query_port, $ip);
  957. }
  958. $status = "200";
  959. $message = "The server has been stopped.";
  960. }
  961. }
  962. if($request[0] == "restart")
  963. {
  964. $start_cmd = get_start_cmd($user_info,$remote,$server_xml,$home_info,$mod_id,$ip,$port,$db);
  965. // Do text replacements in cfg file
  966. if( $server_xml->replace_texts )
  967. {
  968. foreach($home_info['mods'][$mod_id] as $key => $value)
  969. {
  970. $home_info[$key] = $value;
  971. }
  972. $server_home = $home_info;
  973. if(isset($server_xml->lgsl_query_name))
  974. require_once('protocol/lgsl/lgsl_protocol.php');
  975. require_once(MODULES."gamemanager/cfg_text_replace.php");
  976. }
  977. // Run pre-start commands
  978. if(isset($server_xml->pre_start) && !empty($server_xml->pre_start)){
  979. $preStart = trim($server_xml->pre_start);
  980. }else{
  981. $preStart = "";
  982. }
  983. // Environment variables
  984. if(isset($server_xml->environment_variables) && !empty($server_xml->environment_variables)){
  985. $envVars = trim($server_xml->environment_variables);
  986. }else{
  987. $envVars = "";
  988. }
  989. // Additional files to lock
  990. if(isset($server_xml->lock_files) && !empty($server_xml->lock_files)){
  991. $lockFiles = trim($server_xml->lock_files);
  992. }else{
  993. $lockFiles = "";
  994. }
  995. if(!empty($lockFiles)){
  996. // Linux only call
  997. if(preg_match("/Linux/", $os)){
  998. $lockedFilesStatus = $remote->lock_additional_home_files($home_info['home_path'], $lockFiles, "lock");
  999. }
  1000. }
  1001. $remote_retval = $remote->remote_restart_server($home_info['home_id'],$ip,$port,$server_xml->control_protocol,
  1002. $home_info['control_password'],$server_xml->control_protocol_type,$home_info['home_path'],
  1003. $server_xml->server_exec_name,$server_xml->exe_location,$start_cmd,
  1004. $home_info['mods'][$mod_id]['cpu_affinity'],
  1005. $home_info['mods'][$mod_id]['nice'],
  1006. $preStart,
  1007. $envVars,
  1008. $server_xml->game_key,
  1009. (isset( $server_xml->console_log ) ? $server_xml->console_log : ""));
  1010. if($remote_retval === -1)
  1011. return array("status" => '333', "message" => "The server could not be restarted.");
  1012. elseif($remote_retval === -2)
  1013. return array("status" => '333', "message" => "The server could not be restarted.");
  1014. else
  1015. {
  1016. $ip_id = $db->getIpIdByIp($ip);
  1017. $db->delServerStatusCache($home_info['ip_id'],$port);
  1018. $status = "200";
  1019. $message = "The server has been restarted.";
  1020. }
  1021. }
  1022. if($request[0] == "rcon")
  1023. {
  1024. $command = $_POST['command'];
  1025. $response = send_rcon_command($command, $remote, $server_xml, $home_info, $home_info['home_id'], $ip, $port);
  1026. if($response === FALSE)
  1027. {
  1028. $status = "334";
  1029. $message = "The command could not be sent";
  1030. }
  1031. else
  1032. {
  1033. $status = "200";
  1034. $message = $response;
  1035. }
  1036. }
  1037. if($request[0] == "update")
  1038. {
  1039. if(!strstr($access_rights,'u'))
  1040. return array("status" => '352', "message" => "You don't have access right to update the server at $ip:$port.");
  1041. if($_POST['type'] == "steam")
  1042. {
  1043. if($server_xml->installer == "steamcmd")
  1044. {
  1045. $exec_folder_path = clean_path($home_info['home_path'] . "/" . $server_xml->exe_location);
  1046. $exec_path = clean_path($exec_folder_path . "/" . $server_xml->server_exec_name);
  1047. $mod_xml = xml_get_mod($server_xml, $home_info['mods'][$mod_id]['mod_key']);
  1048. $installer_name = $mod_xml->installer_name;
  1049. $modkey = $home_info['mods'][$mod_id]['mod_key'];
  1050. // Some games like L4D2 require anonymous login
  1051. if($mod_xml->installer_login){
  1052. $login = $mod_xml->installer_login;
  1053. $pass = '';
  1054. }else{
  1055. $login = $settings['steam_user'];
  1056. $pass = $settings['steam_pass'];
  1057. }
  1058. $modname = ( $installer_name == '90' and !preg_match("/(cstrike|valve)/", $modkey) ) ? $modkey : '';
  1059. $betaname = isset($mod_xml->betaname) ? $mod_xml->betaname : '';
  1060. $betapwd = isset($mod_xml->betapwd) ? $mod_xml->betapwd : '';
  1061. preg_match("/(win|linux)(32|64)?$/", $server_xml->game_key, $matches);
  1062. $os = strtolower($matches[1]) == 'linux'? 'linux':'windows';
  1063. $arch = isset($matches[2])?$matches[2]:'32';
  1064. $preInstallCMD = "";
  1065. if(isset($server_xml->post_install))
  1066. $preInstallCMD .= $server_xml->pre_install;
  1067. $postInstallCMD = "";
  1068. if(isset($server_xml->post_install))
  1069. $postInstallCMD .= $server_xml->post_install;
  1070. if($os == 'linux'){
  1071. $postInstallCMD .= "\n{OGP_LOCK_FILE} " . $home_info['home_path'] . "/" . ($server_xml->exe_location ? $server_xml->exe_location . "/" : "") . $server_xml->server_exec_name;
  1072. }
  1073. $remote->steam_cmd($home_info['home_id'],$home_info['home_path'],$installer_name,$modname,
  1074. $betaname,$betapwd,$login,$pass,$settings['steam_guard'],
  1075. $exec_folder_path,$exec_path,$preInstallCMD,$postInstallCMD,$os,'',$arch);
  1076. $status = "200";
  1077. $message = "Steam installation started";
  1078. }
  1079. else
  1080. {
  1081. $status = "335";
  1082. $message = 'This game is not supported by Steam installation.';
  1083. }
  1084. }
  1085. if($_POST['type'] == "rsync")
  1086. {
  1087. if(isset($server_xml->lgsl_query_name))
  1088. {
  1089. $rs_name = $server_xml->lgsl_query_name;
  1090. if($rs_name == "quake3" and $server_xml->game_name == "Quake 3")
  1091. $rs_name = "q3";
  1092. }
  1093. elseif(isset($server_xml->gameq_query_name))
  1094. {
  1095. $rs_name = $server_xml->gameq_query_name;
  1096. if($rs_name == "minecraft")
  1097. {
  1098. if($server_xml->game_name == "Minecraft Tekkit")
  1099. $rs_name = "tekkit";
  1100. elseif($server_xml->game_name == "Minecraft Bukkit")
  1101. $rs_name = "bukkit";
  1102. }
  1103. }
  1104. elseif(isset($server_xml->protocol))
  1105. $rs_name = $server_xml->protocol;
  1106. else
  1107. $rs_name = $server_xml->mods->mod['key'];
  1108. $rsync_available = isset($settings['rsync_available']) ? $settings['rsync_available'] : "1";
  1109. $remote_sites = MODULES."gamemanager/rsync_sites.list";
  1110. $local_sites = MODULES."gamemanager/rsync_sites_local.list";
  1111. $rsync_sites = array();
  1112. switch ($rsync_available) {
  1113. case "0":
  1114. if(file_exists($remote_sites))
  1115. {
  1116. $sites = file($remote_sites);
  1117. if($sites !== FALSE)
  1118. $rsync_sites = array_merge($rsync_sites, $sites);
  1119. }
  1120. if(file_exists($local_sites))
  1121. {
  1122. $sites = file($local_sites);
  1123. if($sites !== FALSE)
  1124. $rsync_sites = array_merge($rsync_sites, $sites);
  1125. }
  1126. break;
  1127. case "1":
  1128. if(file_exists($remote_sites))
  1129. {
  1130. $sites = file($remote_sites);
  1131. if($sites !== FALSE)
  1132. $rsync_sites = array_merge($rsync_sites, $sites);
  1133. }
  1134. break;
  1135. case "2":
  1136. if(file_exists($local_sites))
  1137. {
  1138. $sites = file($local_sites);
  1139. if($sites !== FALSE)
  1140. $rsync_sites = array_merge($rsync_sites, $sites);
  1141. }
  1142. break;
  1143. }
  1144. if(empty($rsync_sites))
  1145. return array("status" => '336', "message" => "No sync sites found, check the panel settings (Available rsync sites).");
  1146. $url = get_faster_rsync($rsync_sites);
  1147. $sync_list_file = MODULES."gamemanager/rsync.list";
  1148. if(!file_exists($sync_list_file))
  1149. return array("status" => '336', "message" => "The sync list file doesn't exists ($sync_list_file).");
  1150. $sync_list = file($sync_list_file, FILE_IGNORE_NEW_LINES);
  1151. if(!$sync_list or empty($sync_list))
  1152. return array("status" => '337', "message" => "Failed to read sync list file ($sync_list_file).");
  1153. if(in_array($rs_name, $sync_list))
  1154. {
  1155. $exec_folder_path = clean_path($home_info['home_path'] . "/" . $server_xml->exe_location);
  1156. $exec_path = clean_path($exec_folder_path . "/" . $server_xml->server_exec_name);
  1157. preg_match("/(win|linux)(32|64)?$/", $server_xml->game_key, $matches);
  1158. $os = strtolower($matches[1]) == 'linux'? 'linux':'windows';
  1159. $full_url = "$url/ogp_game_installer/$rs_name/$os/";
  1160. $preInstallCMD = "";
  1161. if(isset($server_xml->post_install))
  1162. $preInstallCMD .= $server_xml->pre_install;
  1163. $postInstallCMD = "";
  1164. if(isset($server_xml->post_install))
  1165. $postInstallCMD .= $server_xml->post_install;
  1166. if($os == 'linux'){
  1167. $postInstallCMD .= "\n{OGP_LOCK_FILE} " . $home_info['home_path'] . "/" . ($server_xml->exe_location ? $server_xml->exe_location . "/" : "") . $server_xml->server_exec_name;
  1168. }
  1169. $remote->start_rsync_install($home_id,$home_info['home_path'],"$full_url",$exec_folder_path,$exec_path,$preInstallCMD,$postInstallCMD);
  1170. $status = "200";
  1171. $message = "Rsync installation started";
  1172. }
  1173. else
  1174. {
  1175. $status = "335";
  1176. $message = 'This game is not supported by Rsync installation.';
  1177. }
  1178. }
  1179. if($_POST['type'] == "manual")
  1180. {
  1181. $manual_url = trim($_POST['manual_url']);
  1182. $filename = get_download_filename($manual_url);
  1183. if($filename)
  1184. {
  1185. $postInstallCMD = "";
  1186. if(isset($server_xml->post_install))
  1187. $postInstallCMD .= $server_xml->post_install;
  1188. preg_match("/(win|linux)(32|64)?$/", $server_xml->game_key, $matches);
  1189. $os = strtolower($matches[1]) == 'linux'? 'linux':'windows';
  1190. if($os == 'linux'){
  1191. $postInstallCMD .= "\n{OGP_LOCK_FILE} " . $home_info['home_path'] . "/" . ($server_xml->exe_location ? $server_xml->exe_location . "/" : "") . $server_xml->server_exec_name;
  1192. }
  1193. $remote->start_file_download($manual_url,$home_info['home_path'],$filename,"uncompress",$postInstallCMD);
  1194. $status = "200";
  1195. $message = "Manual installation started";
  1196. }
  1197. else
  1198. {
  1199. $status = "335";
  1200. $message = 'The URL for manual installation is empty or invalid.';
  1201. }
  1202. }
  1203. if($_POST['type'] == "master")
  1204. {
  1205. $ms_home_id = $db->getMasterServer($home_info['remote_server_id'], $home_info['home_cfg_id']);
  1206. if($ms_home_id !== FALSE)
  1207. {
  1208. $exec_folder_path = clean_path($home_info['home_path'] . "/" . $server_xml->exe_location );
  1209. $exec_path = clean_path($exec_folder_path . "/" . $server_xml->server_exec_name );
  1210. $ms_info = $db->getGameHome($ms_home_id);
  1211. $preInstallCMD = "";
  1212. if(isset($server_xml->post_install))
  1213. $preInstallCMD .= $server_xml->pre_install;
  1214. $postInstallCMD = "";
  1215. if(isset($server_xml->post_install))
  1216. $postInstallCMD .= $server_xml->post_install;
  1217. preg_match("/(win|linux)(32|64)?$/", $server_xml->game_key, $matches);
  1218. $os = strtolower($matches[1]) == 'linux'? 'linux':'windows';
  1219. if($os == 'linux'){
  1220. $postInstallCMD .= "\n{OGP_LOCK_FILE} " . $home_info['home_path'] . "/" . ($server_xml->exe_location ? $server_xml->exe_location . "/" : "") . $server_xml->server_exec_name;
  1221. }
  1222. $remote->masterServerUpdate($home_id,$home_info['home_path'],$ms_home_id,$ms_info['home_path'],$exec_folder_path,$exec_path,$preInstallCMD,$postInstallCMD);
  1223. $status = "200";
  1224. $message = "Installation from master server (" . $home_info['home_name'] . ") started";
  1225. }
  1226. else
  1227. {
  1228. $status = "335";
  1229. $message = 'There is no master server assigned for this game.';
  1230. }
  1231. }
  1232. }
  1233. return array("status" => $status, "message" => $message);
  1234. }
  1235. function api_litefm()
  1236. {
  1237. global $request, $db, $user_info, $settings;
  1238. $ip = $_POST['ip'];
  1239. $port = $_POST['port'];
  1240. $relative_path = $_POST['relative_path'];
  1241. $home_info = $db->getGameHomeByIP($ip, $port);
  1242. if($home_info === FALSE)
  1243. return array("status" => '328', "message" => "There is no game server with the given address ($ip:$port).");
  1244. $isAdmin = $db->isAdmin($user_info['user_id']);
  1245. if($isAdmin)
  1246. {
  1247. $access_rights = 'ufpetc';
  1248. }
  1249. else
  1250. {
  1251. $game_home = $db->getUserGameHome($user_info['user_id'], $home_info['home_id']);
  1252. if($game_home === FALSE)
  1253. return array("status" => '329', "message" => "The given address ($ip:$port) does not belong to your account.");
  1254. $access_rights = $game_home['access_rights'];
  1255. }
  1256. if(!strstr($access_rights,'f'))
  1257. return array("status" => '351', "message" => "You don't have access right for file management in server at $ip:$port.");
  1258. $server_xml = read_server_config(SERVER_CONFIG_LOCATION."/".$home_info['home_cfg_file']);
  1259. if($server_xml === FALSE)
  1260. return array("status" => '305', "message" => "No game configuration found for home_cfg_id #" . $home_cfg_id . ".");
  1261. $remote = new OGPRemoteLibrary($home_info['agent_ip'],$home_info['agent_port'],$home_info['encryption_key'],$home_info['timeout']);
  1262. $host_stat = $remote->status_chk();
  1263. if($host_stat !== 1)
  1264. return array("status" => '310', "message" => "The remote server is offline.");
  1265. $relative_path = preg_replace("/(\.\.)(\\\|\/)+/", '/', $relative_path);
  1266. $path = clean_path($home_info['home_path'].'/'.$relative_path);
  1267. if($request[0] != "save" and $remote->rfile_exists($path) === 0)
  1268. return array("status" => '338', "message" => "$path does not exists.");
  1269. if($request[0] == "list")
  1270. {
  1271. $status = "200";
  1272. $message = $remote->remote_dirlistfm($path);
  1273. }
  1274. if($request[0] == "get")
  1275. {
  1276. $remote->remote_readfile($path, $data);
  1277. $status = "200";
  1278. $message = $data;
  1279. }
  1280. if($request[0] == "save")
  1281. {
  1282. $contents = $_POST['contents'];
  1283. if($remote->remote_writefile($path, $contents) === 1)
  1284. {
  1285. $status = "200";
  1286. $message = "File $path written successfully";
  1287. }
  1288. else
  1289. {
  1290. $status = "339";
  1291. $message = "Could not write to the file.";
  1292. }
  1293. }
  1294. if($request[0] == "remove")
  1295. {
  1296. $remote->shell_action('remove_recursive', $path);
  1297. if($remote->rfile_exists($path) === 0)
  1298. {
  1299. $status = '200';
  1300. $message = "$path removed successfully.";
  1301. }
  1302. else
  1303. {
  1304. $status = '340';
  1305. $message = "$path could not be removed.";
  1306. }
  1307. }
  1308. return array("status" => $status, "message" => $message);
  1309. }
  1310. function api_addonsmanager()
  1311. {
  1312. global $request, $db, $user_info;
  1313. if($db->isModuleInstalled('addonsmanager') === FALSE)
  1314. return array("status" => '349', "message" => "This function is not available because the module is not installed.");
  1315. if($request[0] == "list")
  1316. {
  1317. $addons_rows = $db->resultQuery("SELECT addon_id, name, url, path, addon_type FROM OGP_DB_PREFIXaddons");
  1318. $status = "200";
  1319. $message = $addons_rows;
  1320. }
  1321. if($request[0] == "install")
  1322. {
  1323. $ip = $_POST['ip'];
  1324. $port = (int)$_POST['port'];
  1325. $mod_key = isset($_POST['mod_key'])?trim($_POST['mod_key']):'';
  1326. $addon_id = (int)$_POST['addon_id'];
  1327. $home_info = $db->getGameHomeByIP($ip, $port);
  1328. if($home_info === FALSE)
  1329. return array("status" => '328', "message" => "There is no game server with the given address ($ip:$port).");
  1330. $isAdmin = $db->isAdmin($user_info['user_id']);
  1331. if(!$isAdmin and $db->getUserGameHome($user_info['user_id'], $home_info['home_id']) === FALSE)
  1332. return array("status" => '329', "message" => "The given address ($ip:$port) does not belong to your account.");
  1333. $server_xml = read_server_config(SERVER_CONFIG_LOCATION."/".$home_info['home_cfg_file']);
  1334. if($server_xml === FALSE)
  1335. return array("status" => '305', "message" => "No game configuration found for home_cfg_id #" . $home_cfg_id . ".");
  1336. $remote = new OGPRemoteLibrary($home_info['agent_ip'],$home_info['agent_port'],$home_info['encryption_key'],$home_info['timeout']);
  1337. $host_stat = $remote->status_chk();
  1338. if($host_stat !== 1)
  1339. return array("status" => '310', "message" => "The remote server is offline.");
  1340. $mod_id = key($home_info['mods']);
  1341. if($mod_key != '')
  1342. {
  1343. foreach($home_info['mods'] as $home_mod)
  1344. {
  1345. if($mod_key == $home_mod['mod_key'])
  1346. {
  1347. $mod_id = $home_mod['mod_id'];
  1348. break;
  1349. }
  1350. }
  1351. }
  1352. else
  1353. $mod_key = $home_info['mods'][$mod_id]['mod_key'];
  1354. $query_groups = "";
  1355. if(!$isAdmin)
  1356. {
  1357. $groups = $db->getUsersGroups($user_info['user_id']);
  1358. $query_groups .= " AND (";
  1359. foreach($groups as $group)
  1360. $query_groups .= "group_id=".$group['group_id']." OR ";
  1361. $query_groups .= "group_id=0 OR group_id IS NULL)";
  1362. }
  1363. $addons_rows = $db->resultQuery("SELECT * FROM OGP_DB_PREFIXaddons WHERE home_cfg_id=".$home_info['home_cfg_id']." AND addon_id=".$addon_id.$query_groups);
  1364. if($addons_rows === FALSE)
  1365. return array("status" => '341', "message" => "Invalid addon id #" . $addon_id . ".");
  1366. $addon_info = $addons_rows[0];
  1367. $url = $addon_info['url'];
  1368. $filename = basename($url);
  1369. if($addon_info['post_script'] != "")
  1370. {
  1371. $addon_info['post_script'] = strip_real_escape_string($addon_info['post_script']);
  1372. if( isset($server_xml->gameq_query_name) )
  1373. {
  1374. $home_info['query_port'] = get_query_port($server_xml, $home_info['port']);
  1375. }
  1376. elseif( isset($server_xml->lgsl_query_name) )
  1377. {
  1378. require_once('protocol/lgsl/lgsl_protocol.php');
  1379. $get_q_and_s = lgsl_port_conversion((string)$server_xml->lgsl_query_name, $home_info['port'], "", "");
  1380. $home_info['query_port'] = $get_q_and_s['1'];
  1381. }
  1382. elseif ($server_xml->protocol == "teamspeak3")
  1383. {
  1384. $query_port = $port + 24;
  1385. }
  1386. $home_info["incremental"] = $db->incrementalNumByHomeId($home_info['home_id'], $home_info['mods'][$mod_id]['mod_cfg_id'], $home_info['remote_server_id']);
  1387. $post_script = preg_replace( "/\%home_path\%/i", $home_info['home_path'], $addon_info['post_script']);
  1388. $post_script = preg_replace( "/\%home_name\%/i", $home_info['home_name'], $post_script);
  1389. $post_script = preg_replace( "/\%control_password\%/i", $home_info['control_password'], $post_script);
  1390. $post_script = preg_replace( "/\%max_players\%/i", $home_info['mods'][$mod_id]['max_players'], $post_script);
  1391. $post_script = preg_replace( "/\%ip\%/i", $home_info['ip'], $post_script);
  1392. $post_script = preg_replace( "/\%port\%/i", $home_info['port'], $post_script);
  1393. $post_script = preg_replace( "/\%query_port\%/i", $home_info['query_port'], $post_script);
  1394. $post_script = preg_replace( "/\%incremental\%/i", $home_info['incremental'], $post_script);
  1395. }
  1396. $pid = $remote->start_file_download($addon_info['url'], $home_info['home_path']."/".$addon_info['path'], $filename, "uncompress", $post_script);
  1397. if($pid > 0)
  1398. {
  1399. $status = "200";
  1400. $message = "Addon installation started with process id #".$pid;
  1401. }
  1402. else
  1403. {
  1404. $status = "342";
  1405. $message = "Addon installation failed, file download could not be started.($retval)";
  1406. }
  1407. }
  1408. return array("status" => $status, "message" => $message);
  1409. }
  1410. function api_steam_workshop()
  1411. {
  1412. global $request, $db, $user_info, $settings;
  1413. if($db->isModuleInstalled('steam_workshop') === FALSE)
  1414. return array("status" => '349', "message" => "This function is not available because the module is not installed.");
  1415. define('CONFIGS', "modules/steam_workshop/game_configs/");
  1416. if($request[0] == "install")
  1417. {
  1418. $ip = $_POST['ip'];
  1419. $port = (int)$_POST['port'];
  1420. $mod_key = isset($_POST['mod_key'])?trim($_POST['mod_key']):'';
  1421. $mods_list = $_POST['mods_list'];
  1422. $home_info = $db->getGameHomeByIP($ip, $port);
  1423. if($home_info === FALSE)
  1424. return array("status" => '328', "message" => "There is no game server with the given address ($ip:$port).");
  1425. $isAdmin = $db->isAdmin($user_info['user_id']);
  1426. if(!$isAdmin and $db->getUserGameHome($user_info['user_id'], $home_info['home_id']) === FALSE)
  1427. return array("status" => '329', "message" => "The given address ($ip:$port) does not belong to your account.");
  1428. $server_xml = read_server_config(SERVER_CONFIG_LOCATION."/".$home_info['home_cfg_file']);
  1429. if($server_xml === FALSE)
  1430. return array("status" => '305', "message" => "No game configuration found for home_cfg_id #" . $home_cfg_id . ".");
  1431. $remote = new OGPRemoteLibrary($home_info['agent_ip'],$home_info['agent_port'],$home_info['encryption_key'],$home_info['timeout']);
  1432. $host_stat = $remote->status_chk();
  1433. if($host_stat !== 1)
  1434. return array("status" => '310', "message" => "The remote server is offline.");
  1435. require_once(MODULES.'steam_workshop/functions.php');
  1436. if(preg_match('/^([0-9]+,?)+$/', $mods_list))
  1437. {
  1438. $mod_id = key($home_info['mods']);
  1439. if($mod_key != '')
  1440. {
  1441. foreach($home_info['mods'] as $home_mod)
  1442. {
  1443. if($mod_key == $home_mod['mod_key'])
  1444. {
  1445. $mod_id = $home_mod['mod_id'];
  1446. break;
  1447. }
  1448. }
  1449. }
  1450. $mod_xml = xml_get_mod($server_xml, $home_info['mods'][$mod_id]['mod_key']);
  1451. if($mod_xml == FALSE)
  1452. return array("status" => '344', "message" => "mod_key not found from game xml.");
  1453. preg_match('/(linux|win)(32|64)?/i', $home_info['game_key'], $matches);
  1454. if(strtolower($matches[1]) == 'linux')
  1455. $os = "Linux";
  1456. elseif(strtolower($matches[1]) == 'win')
  1457. $os = "Windows";
  1458. $xml_file = CONFIGS.$mod_xml->installer_name."_".$os.".xml";
  1459. if(!file_exists($xml_file))
  1460. return array("status" => '344', "message" => "No Steam workshop xml file could be found for the game installed in the given ip:port.");
  1461. $dom = new DOMDocument();
  1462. if ( @$dom->load($xml_file) === FALSE )
  1463. return array("status" => '345', "message" => "The Steam workshop xml file for this game has bad format.");
  1464. $xml = simplexml_load_file($xml_file);
  1465. $mod_id_array = explode(',', $mods_list);
  1466. foreach($mod_id_array as $workshop_mod_id)
  1467. {
  1468. $exist = false;
  1469. foreach($xml->mods->mod as $mod)
  1470. {
  1471. if($mod['id'] == $workshop_mod_id)
  1472. {
  1473. $exist = true;
  1474. break;
  1475. }
  1476. }
  1477. if(belongs_to_workshop($workshop_mod_id, $xml->workshop_id))
  1478. {
  1479. if(!$exist)
  1480. {
  1481. list($mod_title, $mod_description, $mod_image_url, $download_url, $filename, $file_size) = get_mod_info($workshop_mod_id);
  1482. //add mods to the xml
  1483. $mod = new SimpleXMLElement('<mod/>');
  1484. $mod->addAttribute('id', $workshop_mod_id);
  1485. $mod->addChild('name', $mod_title);
  1486. $mod->addChild('description', base64_encode($mod_description));
  1487. $mod->addChild('image_url', $mod_image_url);
  1488. $mod->addChild('download_url', $download_url);
  1489. $mod->addChild('filename', $filename);
  1490. $mod->addChild('file_size', $file_size);
  1491. $moddom = dom_import_simplexml($mod)->ownerDocument;
  1492. $moddom->formatOutput = true;
  1493. $mod_string = $moddom->saveXML($moddom->documentElement);
  1494. $dom = dom_import_simplexml($xml)->ownerDocument;
  1495. $dom->formatOutput = true;
  1496. $mods = $dom->getElementsByTagName('mods')->item(0);
  1497. $f = $dom->createDocumentFragment();
  1498. $f->appendXML($mod_string."\n");
  1499. $mods->appendChild($f);
  1500. file_put_contents($xml_file, $dom->saveXML());
  1501. $xml = simplexml_load_file($xml_file);
  1502. }
  1503. }
  1504. else
  1505. {
  1506. break;
  1507. return array("status" => '346', "message" => "Mod $workshop_mod_id does not belong to workshop ".$xml->workshop_id.".");
  1508. }
  1509. }
  1510. $config = $xml->config;
  1511. $anonymous_login = $xml->anonymous_login;
  1512. $download_method = $xml->download_method;
  1513. $user = $settings['steam_user'];
  1514. $pass = $settings['steam_pass'];
  1515. $regex = $config->regex;
  1516. $mods_backreference_index = (int)$config->mods_backreference_index;
  1517. $variable = $config->variable;
  1518. $place_after = $config->place_after;
  1519. $mod_string = $config->mod_string;
  1520. $string_separator = $config->string_separator;
  1521. $config_file_path = clean_path($home_info['home_path']."/".$config->filepath);
  1522. $post_install = $xml->post_install;
  1523. $mod_names_list = get_mod_names_list($mods_list, $xml->mods->mod);
  1524. $mods_full_path = clean_path($home_info['home_path'].'/'.$xml->mods_path);
  1525. $workshop_id = $xml->workshop_id;
  1526. $url_list = "";
  1527. $filename_list = "";
  1528. if($download_method == "steamapi")
  1529. {
  1530. foreach($mod_id_array as $workshop_mod_id)
  1531. {
  1532. foreach($xml->mods->mod as $mod)
  1533. {
  1534. if($mod['id'] == $workshop_mod_id)
  1535. {
  1536. $separator = $url_list == ""?"":",";
  1537. $url_list .= $separator.$mod->download_url;
  1538. $filename_list .= $separator.$mod->filename;
  1539. }
  1540. }
  1541. }
  1542. }
  1543. if($remote->steam_workshop( $home_info['home_id'],$mods_full_path,$workshop_id,$mods_list,$regex,$mods_backreference_index,
  1544. $variable,$place_after,$mod_string,$string_separator,$config_file_path,$post_install,
  1545. $mod_names_list,$anonymous_login,$user,$pass,$download_method,$url_list,$filename_list ) == 1)
  1546. {
  1547. $status = "200";
  1548. $message = "Mods installation started successfully";
  1549. }
  1550. else
  1551. {
  1552. $status = '347';
  1553. $message = "The installation could not be started on the remote server.";
  1554. }
  1555. }
  1556. else
  1557. {
  1558. $status = '348';
  1559. $message = "The mods list has bad format ($mods_list), must be a list of mod ids separated by coma with no spaces, or only one mod id.";
  1560. }
  1561. }
  1562. return array("status" => $status, "message" => $message);
  1563. }
  1564. function api_setting()
  1565. {
  1566. global $request, $db, $user_info, $settings;
  1567. if($user_info['users_role'] != "admin"){
  1568. outputPlainText("-1");
  1569. }
  1570. if($request[0] == "get")
  1571. {
  1572. $setting = $_POST['setting_name'];
  1573. if(array_key_exists($setting, $settings)){
  1574. $status = "200";
  1575. $message = $settings[$setting];
  1576. outputPlainText($message);
  1577. }
  1578. }
  1579. outputPlainText("-1");
  1580. }
  1581. ?>