api_functions.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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, "ip" => true, "port" => 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 and $server_xml->query_port['type'] == "add" )
  55. return $server_port + $server_xml->query_port;
  56. return $server_port;
  57. }
  58. function get_start_cmd($user_info,$remote,$server_xml,$home_info,$mod_id,$ip,$port,$db)
  59. {
  60. $last_param = json_decode($home_info['last_param'], True);
  61. $os = $remote->what_os();
  62. $isAdmin = false;
  63. if(hasValue($user_info) && hasValue($user_info['user_id'])){
  64. $isAdmin = $db->isAdmin($user_info['user_id']);
  65. }
  66. $cli_param_data['GAME_TYPE'] = $home_info['mods'][$mod_id]['mod_key'];
  67. $cli_param_data['IP'] = $ip;
  68. $cli_param_data['PORT'] = $port;
  69. $cli_param_data['HOSTNAME'] = $home_info['home_name'];
  70. $cli_param_data['PID_FILE'] = "ogp_game_startup.pid";
  71. // Linux
  72. if( preg_match("/Linux/", $os) )
  73. {
  74. if(preg_match("/_win(32|64)?$/", $home_info['game_key']))
  75. {
  76. $home_path_wine = $remote->exec("winepath -w ".$home_info['home_path']);
  77. $home_path_wine = str_replace("\\","\\\\", $home_path_wine);
  78. $home_path_wine = trim($home_path_wine);
  79. $cli_param_data['BASE_PATH'] = $home_path_wine;
  80. $cli_param_data['HOME_PATH'] = $home_path_wine;
  81. $cli_param_data['SAVE_PATH'] = $home_path_wine;
  82. $cli_param_data['OUTPUT_PATH'] = $home_path_wine;
  83. $cli_param_data['USER_PATH'] = $home_path_wine;
  84. }
  85. else
  86. {
  87. $cli_param_data['BASE_PATH'] = $home_info['home_path'];
  88. $cli_param_data['HOME_PATH'] = $home_info['home_path'];
  89. $cli_param_data['SAVE_PATH'] = $home_info['home_path'];
  90. $cli_param_data['OUTPUT_PATH'] = $home_info['home_path'];
  91. $cli_param_data['USER_PATH'] = $home_info['home_path'];
  92. }
  93. }
  94. // Windows
  95. elseif( preg_match("/CYGWIN/", $os) )
  96. {
  97. $home_path_win = $remote->exec("cygpath -w ".$home_info['home_path']);
  98. $home_path_win = str_replace("\\","\\\\", $home_path_win);
  99. $home_path_win = trim($home_path_win);
  100. $cli_param_data['BASE_PATH'] = $home_path_win;
  101. $cli_param_data['HOME_PATH'] = $home_path_win;
  102. $cli_param_data['SAVE_PATH'] = $home_path_win;
  103. $cli_param_data['OUTPUT_PATH'] = $home_path_win;
  104. $cli_param_data['USER_PATH'] = $home_path_win;
  105. }
  106. if ($server_xml->protocol == "gameq")
  107. {
  108. $cli_param_data['QUERY_PORT'] = get_query_port($server_xml, $port);
  109. }
  110. elseif ($server_xml->protocol == "lgsl")
  111. {
  112. require('protocol/lgsl/lgsl_protocol.php');
  113. $get_ports = lgsl_port_conversion((string)$server_xml->lgsl_query_name, $port, "", "");
  114. $cli_param_data['QUERY_PORT'] = $get_ports['1'];
  115. }
  116. elseif ($server_xml->protocol == "teamspeak3")
  117. {
  118. $cli_param_data['QUERY_PORT'] = $port + 24;
  119. }
  120. $cli_param_data['MAP'] = ($last_param === NULL or !isset($last_param['map'])) ? "" : $last_param['map'];
  121. $cli_param_data['PLAYERS'] = ($last_param === NULL or !isset($last_param['players'])) ?
  122. isset($home_info['mods'][$mod_id]['max_players']) ?
  123. $home_info['mods'][$mod_id]['max_players'] : "1" : $last_param['players'];
  124. $cli_param_data['CONTROL_PASSWORD'] = $home_info['control_password'];
  125. $start_cmd = "";
  126. // If the template is empty then these are not needed.
  127. if ( $server_xml->cli_template )
  128. {
  129. $start_cmd = $server_xml->cli_template;
  130. if ( $server_xml->cli_params )
  131. {
  132. foreach ( $server_xml->cli_params->cli_param as $cli )
  133. {
  134. // If s is found the param is seperated with space
  135. $add_space = preg_match( "/s/", $cli['options'] ) > 0 ? " " : "";
  136. $cli_value = $cli_param_data[(string) $cli['id'] ];
  137. // If q is found we add quotes around the value.
  138. if ( preg_match( "/q/", $cli['options'] ) > 0 )
  139. {
  140. $cli_value = "\"".$cli_value."\"";
  141. }
  142. $start_cmd = preg_replace( "/%".$cli['id']."%/",
  143. $cli['cli_string'].$add_space.$cli_value, $start_cmd );
  144. }
  145. }
  146. if ( $server_xml->reserve_ports )
  147. {
  148. foreach ( $server_xml->reserve_ports->port as $reserve_port )
  149. {
  150. // If s is found the param is seperated with space
  151. $add_space = preg_match( "/s/", $reserve_port['options'] ) > 0 ? " " : "";
  152. $cli_value = $reserve_port['type'] == "add" ? $port + (string) $reserve_port:
  153. $port - (string) $reserve_port;
  154. // If q is found we add quotes around the value.
  155. if ( preg_match( "/q/", $reserve_port['options'] ) > 0 )
  156. {
  157. $cli_value = "\"".$cli_value."\"";
  158. }
  159. $start_cmd = preg_replace( "/%".$reserve_port['id']."%/",
  160. $reserve_port['cli_string'].$add_space.$cli_value, $start_cmd );
  161. }
  162. }
  163. }
  164. if ( $isAdmin )
  165. {
  166. $home_info['access_rights'] = "ufpet";
  167. }
  168. $param_access_enabled = preg_match("/p/",$home_info['access_rights']) > 0 ? TRUE : FALSE;
  169. if ($param_access_enabled && $last_param !== NULL and isset($server_xml->server_params->param) )
  170. {
  171. foreach($server_xml->server_params->param as $param)
  172. {
  173. foreach ($last_param as $paramKey => $paramValue)
  174. {
  175. if (!isset($paramValue))
  176. $paramValue = (string)$param->default;
  177. if ($param['key'] == $paramKey)
  178. {
  179. if (0 == strlen($paramValue))
  180. continue;
  181. if ($param['key'] == $paramValue) // it's a checkbox
  182. $new_param = $paramKey;
  183. elseif($param->option == "ns" or $param->options == "ns")
  184. $new_param = $paramKey.clean_server_param_value($paramValue, $server_xml->cli_allow_chars);
  185. elseif($param->option == "q" or $param->options == "q")
  186. $new_param = $paramKey . '"' . clean_server_param_value($paramValue, $server_xml->cli_allow_chars) . '"';
  187. elseif($param->option == "s" or $param->options == "s")
  188. $new_param = $paramKey . ' ' . clean_server_param_value($paramValue, $server_xml->cli_allow_chars);
  189. else
  190. $new_param = $paramKey . ' "' . clean_server_param_value($paramValue, $server_xml->cli_allow_chars) . '"';
  191. if ($param['id'] == NULL || $param['id'] == "")
  192. $start_cmd .= ' '.$new_param;
  193. else
  194. $start_cmd = preg_replace( "/%".$param['id']."%/", $new_param, $start_cmd );
  195. }
  196. }
  197. if ($param['id'] != NULL && $param['id'] != ""){
  198. $start_cmd = preg_replace( "/%".$param['id']."%/", '', $start_cmd );
  199. }
  200. }
  201. }
  202. $extra_param_access_enabled = preg_match("/e/",$home_info['access_rights']) > 0 ? TRUE:FALSE;
  203. if ( array_key_exists('extra', $last_param) && $extra_param_access_enabled )
  204. $extra_default = $last_param['extra'];
  205. else
  206. $extra_default = $home_info['mods'][$mod_id]['extra_params'];
  207. $start_cmd .= " ".str_replace("\\\\", "\\", clean_server_param_value($extra_default, $server_xml->cli_allow_chars));
  208. return $start_cmd;
  209. }
  210. function send_rcon_command($command, $remote, $server_xml, $home_info, $home_id, $ip, $port)
  211. {
  212. if( $server_xml->gameq_query_name and $server_xml->gameq_query_name == "minecraft" )
  213. {
  214. require_once("modules/gamemanager/MinecraftRcon.class.php");
  215. $server_properties_file = clean_path($home_info['home_path']."/server.properties");
  216. $retval = $remote->remote_readfile($server_properties_file, $data);
  217. if($retval == 1 and strpos($data, 'rcon.port') !== FALSE)
  218. {
  219. $server_properties = parse_ini_string($data);
  220. $rcon_port = $server_properties['rcon.port'];
  221. }
  222. else
  223. {
  224. $rcon_port = $port+10;
  225. }
  226. $rcon = new MinecraftRcon;
  227. if( $rcon->Connect($ip, $rcon_port, $home_info['control_password']) )
  228. {
  229. $return = $rcon->Command($command);
  230. if($return)
  231. return $return;
  232. else
  233. return FALSE;
  234. $rcon->Disconnect();
  235. }
  236. else
  237. return FALSE;
  238. }
  239. elseif( $server_xml->lgsl_query_name and $server_xml->lgsl_query_name == "7dtd" )
  240. {
  241. $query_port = $port + 1;
  242. $return = $remote->exec('exec 3<>/dev/tcp/'.$ip.'/'. $query_port .' && echo -en "'.$command.'\\nexit\\n" >&3 && cat <&3');
  243. if(preg_match("/Connected with 7DTD server/",$return))
  244. return $return;
  245. else
  246. return FALSE;
  247. }
  248. else
  249. {
  250. $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);
  251. if ( $remote_retval === 1 )
  252. return $return;
  253. elseif ( $remote_retval === -10 )
  254. return FALSE;
  255. else
  256. return FALSE;
  257. }
  258. }
  259. function test_rsync_response($address)
  260. {
  261. $starttime = microtime(true);
  262. $fp = fsockopen($address, 873, $errno, $errstr, 3);
  263. $stoptime = microtime(true);
  264. if (!$fp) {
  265. return FALSE;
  266. }
  267. else
  268. {
  269. $out = "Connection: Close\r\n\r\n";
  270. fwrite($fp, $out);
  271. $response = "";
  272. while (!feof($fp)) {
  273. $response .= fgets($fp, 128);
  274. }
  275. fclose($fp);
  276. if(strstr($response,"@RSYNCD"))
  277. {
  278. $response_time = ($stoptime - $starttime);
  279. return $response_time;
  280. }
  281. else
  282. return FALSE;
  283. }
  284. }
  285. function get_faster_rsync($rsync_sites)
  286. {
  287. $faster = "NONE";
  288. foreach($rsync_sites as $site)
  289. {
  290. list($url,$name) = explode('|', $site);
  291. $current_time = test_rsync_response($url);
  292. if($response_time !== FALSE)
  293. {
  294. if(!isset($previous_time))
  295. $faster = $url;
  296. if($previous_time > $current_time)
  297. $faster = $url;
  298. $previous_time = $current_time;
  299. }
  300. }
  301. if($faster == "NONE")
  302. $faster = "rsync.opengamepanel.org";
  303. return $faster;
  304. }
  305. function get_download_filename($url)
  306. {
  307. if(empty($url) or !filter_var($url, FILTER_VALIDATE_URL))
  308. return FALSE;
  309. $headers = get_headers($url, 1);
  310. if($headers['Server'] == 'cloudflare')
  311. return basename($url);
  312. if(isset($headers[0]) and preg_match('/200|302/', $headers[0]))
  313. {
  314. if(isset($headers['Content-Disposition']))
  315. {
  316. list($type, $filename) = explode('filename=',$headers['Content-Disposition']);
  317. }
  318. }
  319. else
  320. $filename = basename($url);
  321. return trim($filename);
  322. }
  323. ?>