ogp_api.php 55 KB

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