ogp_api.php 54 KB

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