api_functions.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. <?php
  2. function get_function_args($main_request)
  3. {
  4. //______________ Token
  5. $functions["token/test"] = array("token" => true);
  6. $functions["token/create"] = array("user" => true, "password" => true);
  7. //______________ Remote Servers
  8. $functions["server/list"] = array("token" => true);
  9. $functions["server/status"] = array("token" => true, "remote_server_id" => true);
  10. $functions["server/restart"] = array("token" => true, "remote_server_id" => true);
  11. $functions["server/create"] = array("token" => true, "agent_name" => true, "agent_ip" => true, "agent_port" => true, "agent_user" => true, "encryption_key" => true, "ftp_ip" => true, "ftp_port" => true, "timeout" => true, "use_nat" => true, "display_public_ip" => true);
  12. $functions["server/remove"] = array("token" => true, "remote_server_id" => true);
  13. $functions["server/add_ip"] = array("token" => true, "remote_server_id" => true, "ip" => true);
  14. $functions["server/remove_ip"] = array("token" => true, "remote_server_id" => true, "ip" => true);
  15. $functions["server/list_ips"] = array("token" => true, "remote_server_id" => true);
  16. $functions["server/edit_ip"] = array("token" => true, "remote_server_id" => true, "old_ip" => true, "new_ip" => true);
  17. //______________ Game Servers
  18. $functions["user_games/list_games"] = array("token" => true,"system" => true,"architecture" => true);
  19. $functions["user_games/list_servers"] = array("token" => true);
  20. $functions["user_games/create"] = array("token" => true, "remote_server_id" => true, "server_name" => true, "home_cfg_id" => true, "mod_cfg_id" => true, "ip" => true, "port" => true, "control_password" => true, "enable_ftp" => true, "ftp_password" => true, "slots" => true, "affinity" => true, "nice" => true);
  21. $functions["user_games/clone"] = array("token" => true, "origin_home_id" => true, "new_server_name" => true, "new_ip" => true, "new_port" => true, "control_password" => true, "enable_ftp" => true, "ftp_password" => true, "slots" => true, "affinity" => true, "nice" => true);
  22. $functions["user_games/set_expiration"] = array("token" => true, "home_id" => true, "timestamp" => true);
  23. //______________ Users
  24. $functions["user_admin/list"] = array("token" => true);
  25. $functions["user_admin/get"] = array("token" => true, "email" => true);
  26. $functions["user_admin/create"] = array("token" => true, "name" => true, "password" => true, "email" => true);
  27. $functions["user_admin/remove"] = array("token" => true, "email" => true);
  28. $functions["user_admin/set_expiration"] = array("token" => true, "email" => true, "timestamp" => true);
  29. $functions["user_admin/list_assigned"] = array("token" => true, "email" => true);
  30. $functions["user_admin/assign"] = array("token" => true, "home_id" => true, "email" => true, "timestamp" => true);
  31. $functions["user_admin/remove_assign"] = array("token" => true, "home_id" => true, "email" => true);
  32. //______________ Game Manager
  33. $functions["gamemanager/start"] = array("token" => true, "ip" => true, "port" => true, "mod_key" => false);
  34. $functions["gamemanager/stop"] = array("token" => true, "ip" => true, "port" => true, "mod_key" => false);
  35. $functions["gamemanager/restart"] = array("token" => true, "ip" => true, "port" => true, "mod_key" => false);
  36. $functions["gamemanager/rcon"] = array("token" => true, "ip" => true, "port" => true, "mod_key" => false, "command" => true);
  37. $functions["gamemanager/update"] = array("token" => true, "ip" => true, "port" => true, "mod_key" => false, "type" => true, "manual_url" => false);
  38. //______________ Lite File Manager
  39. $functions["litefm/list"] = array("token" => true, "ip" => true, "port" => true, "relative_path" => true);
  40. $functions["litefm/get"] = array("token" => true, "ip" => true, "port" => true, "relative_path" => true);
  41. $functions["litefm/save"] = array("token" => true, "ip" => true, "port" => true, "relative_path" => true, "contents" => true);
  42. $functions["litefm/remove"] = array("token" => true, "ip" => true, "port" => true, "relative_path" => true);
  43. //______________ Addons Manager
  44. $functions["addonsmanager/list"] = array("token" => true);
  45. $functions["addonsmanager/install"] = array("token" => true, "ip" => true, "port" => true, "addon_id" => true);
  46. //______________ Steam Workshop
  47. $functions["steam_workshop/install"] = array("token" => true, "ip" => true, "port" => true, "mod_key" => false, "mods_list" => true);
  48. if($main_request == "all")
  49. return $functions;
  50. return isset($functions["$main_request"])?$functions["$main_request"]:false;
  51. }
  52. function get_query_port($server_xml, $server_port)
  53. {
  54. if ($server_xml->query_port)
  55. {
  56. if ($server_xml->query_port['type'] == 'add')
  57. return $server_port + $server_xml->query_port;
  58. if ($server_xml->query_port['type'] == 'subtract')
  59. return $server_port - $server_xml->query_port;
  60. }
  61. return $server_port;
  62. }
  63. function get_start_cmd($user_info,$remote,$server_xml,$home_info,$mod_id,$ip,$port,$db)
  64. {
  65. $last_param = json_decode($home_info['last_param'], True);
  66. $os = $remote->what_os();
  67. $isAdmin = false;
  68. if(hasValue($user_info) && hasValue($user_info['user_id'])){
  69. $isAdmin = $db->isAdmin($user_info['user_id']);
  70. }
  71. $cli_param_data['GAME_TYPE'] = $home_info['mods'][$mod_id]['mod_key'];
  72. $cli_param_data['IP'] = $ip;
  73. $cli_param_data['PORT'] = $port;
  74. $cli_param_data['HOSTNAME'] = $home_info['home_name'];
  75. $cli_param_data['PID_FILE'] = "ogp_game_startup.pid";
  76. // Linux
  77. if( preg_match("/Linux/", $os) )
  78. {
  79. if(preg_match("/_win(32|64)?$/", $home_info['game_key']))
  80. {
  81. $home_path_wine = $remote->exec("winepath -w ".$home_info['home_path']);
  82. $home_path_wine = str_replace("\\","\\\\", $home_path_wine);
  83. $home_path_wine = trim($home_path_wine);
  84. $cli_param_data['BASE_PATH'] = $home_path_wine;
  85. $cli_param_data['HOME_PATH'] = $home_path_wine;
  86. $cli_param_data['SAVE_PATH'] = $home_path_wine;
  87. $cli_param_data['OUTPUT_PATH'] = $home_path_wine;
  88. $cli_param_data['USER_PATH'] = $home_path_wine;
  89. }
  90. else
  91. {
  92. $cli_param_data['BASE_PATH'] = $home_info['home_path'];
  93. $cli_param_data['HOME_PATH'] = $home_info['home_path'];
  94. $cli_param_data['SAVE_PATH'] = $home_info['home_path'];
  95. $cli_param_data['OUTPUT_PATH'] = $home_info['home_path'];
  96. $cli_param_data['USER_PATH'] = $home_info['home_path'];
  97. }
  98. }
  99. // Windows
  100. elseif( preg_match("/CYGWIN/", $os) )
  101. {
  102. $home_path_win = $remote->exec("cygpath -w ".$home_info['home_path']);
  103. $home_path_win = str_replace("\\","\\\\", $home_path_win);
  104. $home_path_win = trim($home_path_win);
  105. $cli_param_data['BASE_PATH'] = $home_path_win;
  106. $cli_param_data['HOME_PATH'] = $home_path_win;
  107. $cli_param_data['SAVE_PATH'] = $home_path_win;
  108. $cli_param_data['OUTPUT_PATH'] = $home_path_win;
  109. $cli_param_data['USER_PATH'] = $home_path_win;
  110. }
  111. if ($server_xml->protocol == "gameq")
  112. {
  113. $cli_param_data['QUERY_PORT'] = get_query_port($server_xml, $port);
  114. }
  115. elseif ($server_xml->protocol == "lgsl")
  116. {
  117. require('protocol/lgsl/lgsl_protocol.php');
  118. $get_ports = lgsl_port_conversion((string)$server_xml->lgsl_query_name, $port, "", "");
  119. $cli_param_data['QUERY_PORT'] = $get_ports['1'];
  120. }
  121. elseif ($server_xml->protocol == "teamspeak3")
  122. {
  123. $cli_param_data['QUERY_PORT'] = $port + 24;
  124. }
  125. $cli_param_data['MAP'] = ($last_param === NULL or !isset($last_param['map'])) ? "" : $last_param['map'];
  126. $cli_param_data['PLAYERS'] = ($last_param === NULL or !isset($last_param['players'])) ?
  127. isset($home_info['mods'][$mod_id]['max_players']) ?
  128. $home_info['mods'][$mod_id]['max_players'] : "1" : $last_param['players'];
  129. $cli_param_data['CONTROL_PASSWORD'] = $home_info['control_password'];
  130. $start_cmd = "";
  131. // If the template is empty then these are not needed.
  132. if ( $server_xml->cli_template )
  133. {
  134. $start_cmd = $server_xml->cli_template;
  135. if ( $server_xml->cli_params )
  136. {
  137. foreach ( $server_xml->cli_params->cli_param as $cli )
  138. {
  139. // If s is found the param is seperated with space
  140. $add_space = preg_match( "/s/", $cli['options'] ) > 0 ? " " : "";
  141. $cli_value = $cli_param_data[(string) $cli['id'] ];
  142. // If q is found we add quotes around the value.
  143. if ( preg_match( "/q/", $cli['options'] ) > 0 )
  144. {
  145. $cli_value = "\"".$cli_value."\"";
  146. }
  147. $start_cmd = preg_replace( "/%".$cli['id']."%/",
  148. $cli['cli_string'].$add_space.$cli_value, $start_cmd );
  149. }
  150. }
  151. if ( $server_xml->reserve_ports )
  152. {
  153. foreach ( $server_xml->reserve_ports->port as $reserve_port )
  154. {
  155. // If s is found the param is seperated with space
  156. $add_space = preg_match( "/s/", $reserve_port['options'] ) > 0 ? " " : "";
  157. $cli_value = $reserve_port['type'] == "add" ? $port + (string) $reserve_port:
  158. $port - (string) $reserve_port;
  159. // If q is found we add quotes around the value.
  160. if ( preg_match( "/q/", $reserve_port['options'] ) > 0 )
  161. {
  162. $cli_value = "\"".$cli_value."\"";
  163. }
  164. $start_cmd = preg_replace( "/%".$reserve_port['id']."%/",
  165. $reserve_port['cli_string'].$add_space.$cli_value, $start_cmd );
  166. }
  167. }
  168. }
  169. if ( $isAdmin )
  170. {
  171. $home_info['access_rights'] = "ufpet";
  172. }
  173. $param_access_enabled = preg_match("/p/",$home_info['access_rights']) > 0 ? TRUE : FALSE;
  174. if ($param_access_enabled && $last_param !== NULL and isset($server_xml->server_params->param) )
  175. {
  176. foreach($server_xml->server_params->param as $param)
  177. {
  178. foreach ($last_param as $paramKey => $paramValue)
  179. {
  180. if (!isset($paramValue))
  181. $paramValue = (string)$param->default;
  182. if ($param['key'] == $paramKey)
  183. {
  184. if (0 == strlen($paramValue))
  185. continue;
  186. if ($param['key'] == $paramValue) // it's a checkbox
  187. $new_param = $paramKey;
  188. elseif($param->option == "ns" or $param->options == "ns")
  189. $new_param = $paramKey.clean_server_param_value($paramValue, $server_xml->cli_allow_chars);
  190. elseif($param->option == "q" or $param->options == "q")
  191. $new_param = $paramKey . '"' . clean_server_param_value($paramValue, $server_xml->cli_allow_chars) . '"';
  192. elseif($param->option == "s" or $param->options == "s")
  193. $new_param = $paramKey . ' ' . clean_server_param_value($paramValue, $server_xml->cli_allow_chars);
  194. else
  195. $new_param = $paramKey . ' "' . clean_server_param_value($paramValue, $server_xml->cli_allow_chars) . '"';
  196. if ($param['id'] == NULL || $param['id'] == "")
  197. $start_cmd .= ' '.$new_param;
  198. else
  199. $start_cmd = preg_replace( "/%".$param['id']."%/", $new_param, $start_cmd );
  200. }
  201. }
  202. if ($param['id'] != NULL && $param['id'] != ""){
  203. $start_cmd = preg_replace( "/%".$param['id']."%/", '', $start_cmd );
  204. }
  205. }
  206. }
  207. $extra_param_access_enabled = preg_match("/e/",$home_info['access_rights']) > 0 ? TRUE:FALSE;
  208. if ( array_key_exists('extra', $last_param) && $extra_param_access_enabled )
  209. $extra_default = $last_param['extra'];
  210. else
  211. $extra_default = $home_info['mods'][$mod_id]['extra_params'];
  212. $start_cmd .= " ".str_replace("\\\\", "\\", clean_server_param_value($extra_default, $server_xml->cli_allow_chars));
  213. return $start_cmd;
  214. }
  215. function send_rcon_command($command, $remote, $server_xml, $home_info, $home_id, $ip, $port)
  216. {
  217. if( $server_xml->gameq_query_name and $server_xml->gameq_query_name == "minecraft" )
  218. {
  219. require_once("modules/gamemanager/MinecraftRcon.class.php");
  220. $server_properties_file = clean_path($home_info['home_path']."/server.properties");
  221. $retval = $remote->remote_readfile($server_properties_file, $data);
  222. if($retval == 1 and strpos($data, 'rcon.port') !== FALSE)
  223. {
  224. $server_properties = parse_ini_string($data);
  225. $rcon_port = $server_properties['rcon.port'];
  226. }
  227. else
  228. {
  229. $rcon_port = $port+10;
  230. }
  231. $rcon = new MinecraftRcon;
  232. if( $rcon->Connect($ip, $rcon_port, $home_info['control_password']) )
  233. {
  234. $return = $rcon->Command($command);
  235. if($return)
  236. return $return;
  237. else
  238. return FALSE;
  239. $rcon->Disconnect();
  240. }
  241. else
  242. return FALSE;
  243. }
  244. elseif( $server_xml->lgsl_query_name and $server_xml->lgsl_query_name == "7dtd" )
  245. {
  246. $query_port = $port + 1;
  247. $return = $remote->exec('exec 3<>/dev/tcp/'.$ip.'/'. $query_port .' && echo -en "'.$command.'\\nexit\\n" >&3 && cat <&3');
  248. if(preg_match("/Connected with 7DTD server/",$return))
  249. return $return;
  250. else
  251. return FALSE;
  252. }
  253. else
  254. {
  255. $remote_retval = $remote->remote_send_rcon_command( $home_id, $ip, $port, $server_xml->control_protocol, $home_info['control_password'],$server_xml->control_protocol_type,$command,$return);
  256. if ( $remote_retval === 1 )
  257. return $return;
  258. elseif ( $remote_retval === -10 )
  259. return FALSE;
  260. else
  261. return FALSE;
  262. }
  263. }
  264. function test_rsync_response($address)
  265. {
  266. $starttime = microtime(true);
  267. $fp = fsockopen($address, 873, $errno, $errstr, 3);
  268. $stoptime = microtime(true);
  269. if (!$fp) {
  270. return FALSE;
  271. }
  272. else
  273. {
  274. $out = "Connection: Close\r\n\r\n";
  275. fwrite($fp, $out);
  276. $response = "";
  277. while (!feof($fp)) {
  278. $response .= fgets($fp, 128);
  279. }
  280. fclose($fp);
  281. if(strstr($response,"@RSYNCD"))
  282. {
  283. $response_time = ($stoptime - $starttime);
  284. return $response_time;
  285. }
  286. else
  287. return FALSE;
  288. }
  289. }
  290. function get_faster_rsync($rsync_sites)
  291. {
  292. $faster = "NONE";
  293. foreach($rsync_sites as $site)
  294. {
  295. list($url,$name) = explode('|', $site);
  296. $current_time = test_rsync_response($url);
  297. if($response_time !== FALSE)
  298. {
  299. if(!isset($previous_time))
  300. $faster = $url;
  301. if($previous_time > $current_time)
  302. $faster = $url;
  303. $previous_time = $current_time;
  304. }
  305. }
  306. if($faster == "NONE")
  307. $faster = "rsync.opengamepanel.org";
  308. return $faster;
  309. }
  310. function get_download_filename($url)
  311. {
  312. if(empty($url) or !filter_var($url, FILTER_VALIDATE_URL))
  313. return FALSE;
  314. $headers = get_headers($url, 1);
  315. if($headers['Server'] == 'cloudflare')
  316. return basename($url);
  317. if(isset($headers[0]) and preg_match('/200|302/', $headers[0]))
  318. {
  319. if(isset($headers['Content-Disposition']))
  320. {
  321. list($type, $filename) = explode('filename=',$headers['Content-Disposition']);
  322. }
  323. }
  324. else
  325. $filename = basename($url);
  326. return trim($filename);
  327. }
  328. ?>