ogp_api.php 53 KB

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