ogp_api.php 55 KB

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