ogp_api.php 56 KB

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