ogp_api.php 54 KB

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