1
0

api_functions.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  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. //______________ Game Manager Admin
  39. $functions["gamemanager_admin/reorder"] = array("token" => true);
  40. //______________ Lite File Manager
  41. $functions["litefm/list"] = array("token" => true, "ip" => true, "port" => true, "relative_path" => true);
  42. $functions["litefm/get"] = array("token" => true, "ip" => true, "port" => true, "relative_path" => true);
  43. $functions["litefm/save"] = array("token" => true, "ip" => true, "port" => true, "relative_path" => true, "contents" => true);
  44. $functions["litefm/remove"] = array("token" => true, "ip" => true, "port" => true, "relative_path" => true);
  45. //______________ Addons Manager
  46. $functions["addonsmanager/list"] = array("token" => true);
  47. $functions["addonsmanager/install"] = array("token" => true, "ip" => true, "port" => true, "addon_id" => true);
  48. //______________ Steam Workshop
  49. $functions["steam_workshop/install"] = array("token" => true, "ip" => true, "port" => true, "mod_key" => false, "mods_list" => true);
  50. //______________ Settings
  51. $functions["setting/get"] = array("token" => true, "setting_name" => true);
  52. if($main_request == "all")
  53. return $functions;
  54. return isset($functions["$main_request"])?$functions["$main_request"]:false;
  55. }
  56. function get_query_port($server_xml, $server_port)
  57. {
  58. if ($server_xml->query_port)
  59. {
  60. if ($server_xml->query_port['type'] == 'add')
  61. return $server_port + $server_xml->query_port;
  62. if ($server_xml->query_port['type'] == 'subtract')
  63. return $server_port - $server_xml->query_port;
  64. }
  65. return $server_port;
  66. }
  67. function get_start_cmd($user_info,$remote,$server_xml,$home_info,$mod_id,$ip,$port,$db)
  68. {
  69. $last_param = json_decode($home_info['last_param'], True);
  70. $os = $remote->what_os();
  71. $isAdmin = false;
  72. if(hasValue($user_info) && hasValue($user_info['user_id'])){
  73. $isAdmin = $db->isAdmin($user_info['user_id']);
  74. }
  75. $cli_param_data['GAME_TYPE'] = $home_info['mods'][$mod_id]['mod_key'];
  76. $cli_param_data['IP'] = $ip;
  77. $cli_param_data['PORT'] = $port;
  78. $cli_param_data['HOSTNAME'] = $home_info['home_name'];
  79. $cli_param_data['PID_FILE'] = "ogp_game_startup.pid";
  80. // Linux
  81. if( preg_match("/Linux/", $os) )
  82. {
  83. if(preg_match("/_win(32|64)?$/", $home_info['game_key']))
  84. {
  85. $home_path_wine = $remote->exec("winepath -w ".$home_info['home_path']);
  86. $home_path_wine = str_replace("\\","\\\\", $home_path_wine);
  87. $home_path_wine = trim($home_path_wine);
  88. $cli_param_data['BASE_PATH'] = $home_path_wine;
  89. $cli_param_data['HOME_PATH'] = $home_path_wine;
  90. $cli_param_data['SAVE_PATH'] = $home_path_wine;
  91. $cli_param_data['OUTPUT_PATH'] = $home_path_wine;
  92. $cli_param_data['USER_PATH'] = $home_path_wine;
  93. }
  94. else
  95. {
  96. $cli_param_data['BASE_PATH'] = $home_info['home_path'];
  97. $cli_param_data['HOME_PATH'] = $home_info['home_path'];
  98. $cli_param_data['SAVE_PATH'] = $home_info['home_path'];
  99. $cli_param_data['OUTPUT_PATH'] = $home_info['home_path'];
  100. $cli_param_data['USER_PATH'] = $home_info['home_path'];
  101. }
  102. }
  103. // Windows
  104. elseif( preg_match("/CYGWIN/", $os) )
  105. {
  106. $home_path_win = $remote->exec("cygpath -w ".$home_info['home_path']);
  107. $home_path_win = str_replace("\\","\\\\", $home_path_win);
  108. $home_path_win = trim($home_path_win);
  109. $cli_param_data['BASE_PATH'] = $home_path_win;
  110. $cli_param_data['HOME_PATH'] = $home_path_win;
  111. $cli_param_data['SAVE_PATH'] = $home_path_win;
  112. $cli_param_data['OUTPUT_PATH'] = $home_path_win;
  113. $cli_param_data['USER_PATH'] = $home_path_win;
  114. }
  115. if ($server_xml->protocol == "gameq")
  116. {
  117. $cli_param_data['QUERY_PORT'] = get_query_port($server_xml, $port);
  118. }
  119. elseif ($server_xml->protocol == "lgsl")
  120. {
  121. require('protocol/lgsl/lgsl_protocol.php');
  122. $get_ports = lgsl_port_conversion((string)$server_xml->lgsl_query_name, $port, "", "");
  123. $cli_param_data['QUERY_PORT'] = $get_ports['1'];
  124. }
  125. elseif ($server_xml->protocol == "teamspeak3")
  126. {
  127. $cli_param_data['QUERY_PORT'] = $port + 24;
  128. }
  129. $cli_param_data['MAP'] = clean_server_param_value(($last_param === NULL or !isset($last_param['map'])) ? "" : $last_param['map'], $server_xml->cli_allow_chars);
  130. $cli_param_data['PLAYERS'] = ($last_param === NULL or !isset($last_param['players'])) ?
  131. isset($home_info['mods'][$mod_id]['max_players']) ?
  132. $home_info['mods'][$mod_id]['max_players'] : "1" : $last_param['players'];
  133. $cli_param_data['CONTROL_PASSWORD'] = $home_info['control_password'];
  134. $start_cmd = "";
  135. // If the template is empty then these are not needed.
  136. if ( $server_xml->cli_template )
  137. {
  138. $start_cmd = $server_xml->cli_template;
  139. if ( $server_xml->cli_params )
  140. {
  141. foreach ( $server_xml->cli_params->cli_param as $cli )
  142. {
  143. // If s is found the param is seperated with space
  144. $add_space = preg_match( "/s/", $cli['options'] ) > 0 ? " " : "";
  145. $cli_value = $cli_param_data[(string) $cli['id'] ];
  146. // If q is found we add quotes around the value.
  147. if ( preg_match( "/q/", $cli['options'] ) > 0 )
  148. {
  149. $cli_value = "\"".$cli_value."\"";
  150. }
  151. $start_cmd = preg_replace( "/%".$cli['id']."%/",
  152. $cli['cli_string'].$add_space.$cli_value, $start_cmd );
  153. }
  154. }
  155. if ( $server_xml->reserve_ports )
  156. {
  157. foreach ( $server_xml->reserve_ports->port as $reserve_port )
  158. {
  159. // If s is found the param is seperated with space
  160. $add_space = preg_match( "/s/", $reserve_port['options'] ) > 0 ? " " : "";
  161. $cli_value = $reserve_port['type'] == "add" ? $port + (string) $reserve_port:
  162. $port - (string) $reserve_port;
  163. // If q is found we add quotes around the value.
  164. if ( preg_match( "/q/", $reserve_port['options'] ) > 0 )
  165. {
  166. $cli_value = "\"".$cli_value."\"";
  167. }
  168. $start_cmd = preg_replace( "/%".$reserve_port['id']."%/",
  169. $reserve_port['cli_string'].$add_space.$cli_value, $start_cmd );
  170. }
  171. }
  172. }
  173. if ( $isAdmin )
  174. {
  175. $home_info['access_rights'] = "ufpet";
  176. }
  177. $param_access_enabled = preg_match("/p/",$home_info['access_rights']) > 0 ? TRUE : FALSE;
  178. if ($param_access_enabled && $last_param !== NULL and isset($server_xml->server_params->param) )
  179. {
  180. foreach($server_xml->server_params->param as $param)
  181. {
  182. foreach ($last_param as $paramKey => $paramValue)
  183. {
  184. if (!isset($paramValue))
  185. $paramValue = (string)$param->default;
  186. if ($param['key'] == $paramKey)
  187. {
  188. if (0 == strlen($paramValue))
  189. continue;
  190. if ($param['key'] == $paramValue) // it's a checkbox
  191. $new_param = $paramKey;
  192. elseif($param->option == "ns" or $param->options == "ns")
  193. $new_param = $paramKey.clean_server_param_value($paramValue, $server_xml->cli_allow_chars);
  194. elseif($param->option == "q" or $param->options == "q")
  195. $new_param = $paramKey . '"' . clean_server_param_value($paramValue, $server_xml->cli_allow_chars) . '"';
  196. elseif($param->option == "s" or $param->options == "s")
  197. $new_param = $paramKey . ' ' . clean_server_param_value($paramValue, $server_xml->cli_allow_chars);
  198. else
  199. $new_param = $paramKey . ' "' . clean_server_param_value($paramValue, $server_xml->cli_allow_chars) . '"';
  200. if ($param['id'] == NULL || $param['id'] == "")
  201. $start_cmd .= ' '.$new_param;
  202. else
  203. $start_cmd = preg_replace( "/%".$param['id']."%/", $new_param, $start_cmd );
  204. }
  205. }
  206. if ($param['id'] != NULL && $param['id'] != ""){
  207. $start_cmd = preg_replace( "/%".$param['id']."%/", '', $start_cmd );
  208. }
  209. }
  210. }
  211. $extra_param_access_enabled = preg_match("/e/",$home_info['access_rights']) > 0 ? TRUE:FALSE;
  212. if ( array_key_exists('extra', $last_param) && $extra_param_access_enabled )
  213. $extra_default = $last_param['extra'];
  214. else
  215. $extra_default = $home_info['mods'][$mod_id]['extra_params'];
  216. $start_cmd .= " ".str_replace("\\\\", "\\", clean_server_param_value($extra_default, $server_xml->cli_allow_chars));
  217. return $start_cmd;
  218. }
  219. function send_rcon_command($command, $remote, $server_xml, $home_info, $home_id, $ip, $port)
  220. {
  221. if( $server_xml->gameq_query_name and $server_xml->gameq_query_name == "minecraft" )
  222. {
  223. require_once("modules/gamemanager/MinecraftRcon.class.php");
  224. $server_properties_file = clean_path($home_info['home_path']."/server.properties");
  225. $retval = $remote->remote_readfile($server_properties_file, $data);
  226. if($retval == 1 and strpos($data, 'rcon.port') !== FALSE)
  227. {
  228. $server_properties = parse_ini_string($data);
  229. $rcon_port = $server_properties['rcon.port'];
  230. }
  231. else
  232. {
  233. $rcon_port = $port+10;
  234. }
  235. $rcon = new MinecraftRcon;
  236. if( $rcon->Connect($ip, $rcon_port, $home_info['control_password']) )
  237. {
  238. $return = $rcon->Command($command);
  239. if($return)
  240. return $return;
  241. else
  242. return FALSE;
  243. $rcon->Disconnect();
  244. }
  245. else
  246. return FALSE;
  247. }
  248. elseif( $server_xml->lgsl_query_name and $server_xml->lgsl_query_name == "7dtd" )
  249. {
  250. $query_port = $port + 1;
  251. $return = $remote->exec('exec 3<>/dev/tcp/'.$ip.'/'. $query_port .' && echo -en "'.$command.'\\nexit\\n" >&3 && cat <&3');
  252. if(preg_match("/Connected with 7DTD server/",$return))
  253. return $return;
  254. else
  255. return FALSE;
  256. }
  257. else
  258. {
  259. $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);
  260. if ( $remote_retval === 1 )
  261. return $return;
  262. elseif ( $remote_retval === -10 )
  263. return FALSE;
  264. else
  265. return FALSE;
  266. }
  267. }
  268. function test_rsync_response($address)
  269. {
  270. $starttime = microtime(true);
  271. $fp = fsockopen($address, 873, $errno, $errstr, 3);
  272. $stoptime = microtime(true);
  273. if (!$fp) {
  274. return FALSE;
  275. }
  276. else
  277. {
  278. $out = "Connection: Close\r\n\r\n";
  279. fwrite($fp, $out);
  280. $response = "";
  281. while (!feof($fp)) {
  282. $response .= fgets($fp, 128);
  283. }
  284. fclose($fp);
  285. if(strstr($response,"@RSYNCD"))
  286. {
  287. $response_time = ($stoptime - $starttime);
  288. return $response_time;
  289. }
  290. else
  291. return FALSE;
  292. }
  293. }
  294. function get_faster_rsync($rsync_sites)
  295. {
  296. $faster = "NONE";
  297. foreach($rsync_sites as $site)
  298. {
  299. list($url,$name) = explode('|', $site);
  300. $current_time = test_rsync_response($url);
  301. if($response_time !== FALSE)
  302. {
  303. if(!isset($previous_time))
  304. $faster = $url;
  305. if($previous_time > $current_time)
  306. $faster = $url;
  307. $previous_time = $current_time;
  308. }
  309. }
  310. if($faster == "NONE")
  311. $faster = "rsync.opengamepanel.org";
  312. return $faster;
  313. }
  314. function get_download_filename($url)
  315. {
  316. if(empty($url) or !filter_var($url, FILTER_VALIDATE_URL))
  317. return FALSE;
  318. $headers = get_headers($url, 1);
  319. if($headers['Server'] == 'cloudflare')
  320. return basename($url);
  321. if(isset($headers[0]) and preg_match('/200|302/', $headers[0]))
  322. {
  323. if(isset($headers['Content-Disposition']))
  324. {
  325. list($type, $filename) = explode('filename=',$headers['Content-Disposition']);
  326. }
  327. }
  328. else
  329. $filename = basename($url);
  330. return trim($filename);
  331. }
  332. function getClientForwardedIP(){
  333. if(isset($_SERVER['HTTP_CF_CONNECTING_IP']) and !empty($_SERVER['HTTP_CF_CONNECTING_IP']))
  334. return $_SERVER['HTTP_CF_CONNECTING_IP'];
  335. if(isset($_SERVER['HTTP_X_FORWARDED_FOR']) and !empty($_SERVER['HTTP_X_FORWARDED_FOR']))
  336. return $_SERVER['HTTP_X_FORWARDED_FOR'];
  337. if(isset($_SERVER['HTTP_X_REAL_IP']) and !empty($_SERVER['HTTP_X_REAL_IP']))
  338. return $_SERVER['HTTP_X_REAL_IP'];
  339. return false;
  340. }
  341. function is_valid_ipv4($ip)
  342. {
  343. if(filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4))
  344. return true;
  345. return false;
  346. }
  347. function is_valid_ipv6($ip)
  348. {
  349. if(filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6))
  350. return true;
  351. return false;
  352. }
  353. // https://github.com/rmccue/Requests/blob/master/library/Requests/IPv6.php
  354. function ipv6_uncompress($ip)
  355. {
  356. if (substr_count($ip, '::') !== 1) {
  357. return $ip;
  358. }
  359. list($ip1, $ip2) = explode('::', $ip);
  360. $c1 = ($ip1 === '') ? -1 : substr_count($ip1, ':');
  361. $c2 = ($ip2 === '') ? -1 : substr_count($ip2, ':');
  362. if (strpos($ip2, '.') !== false) {
  363. $c2++;
  364. }
  365. // ::
  366. if ($c1 === -1 && $c2 === -1) {
  367. $ip = '0:0:0:0:0:0:0:0';
  368. }
  369. // ::xxx
  370. else if ($c1 === -1) {
  371. $fill = str_repeat('0:', 7 - $c2);
  372. $ip = str_replace('::', $fill, $ip);
  373. }
  374. // xxx::
  375. else if ($c2 === -1) {
  376. $fill = str_repeat(':0', 7 - $c1);
  377. $ip = str_replace('::', $fill, $ip);
  378. }
  379. // xxx::xxx
  380. else {
  381. $fill = ':' . str_repeat('0:', 6 - $c2 - $c1);
  382. $ip = str_replace('::', $fill, $ip);
  383. }
  384. return $ip;
  385. }
  386. function split_v6_v4($ip) {
  387. if (strpos($ip, '.') !== false) {
  388. $pos = strrpos($ip, ':');
  389. $ipv6_part = substr($ip, 0, $pos);
  390. $ipv4_part = substr($ip, $pos + 1);
  391. return array($ipv6_part, $ipv4_part);
  392. }
  393. else {
  394. return array($ip, '');
  395. }
  396. }
  397. function ipv6_compress($ip)
  398. {
  399. // Prepare the IP to be compressed
  400. $ip = ipv6_uncompress($ip);
  401. $ip_parts = split_v6_v4($ip);
  402. // Replace all leading zeros
  403. $ip_parts[0] = preg_replace('/(^|:)0+([0-9])/', '\1\2', $ip_parts[0]);
  404. // Find bunches of zeros
  405. if (preg_match_all('/(?:^|:)(?:0(?::|$))+/', $ip_parts[0], $matches, PREG_OFFSET_CAPTURE)) {
  406. $max = 0;
  407. $pos = null;
  408. foreach ($matches[0] as $match) {
  409. if (strlen($match[0]) > $max) {
  410. $max = strlen($match[0]);
  411. $pos = $match[1];
  412. }
  413. }
  414. $ip_parts[0] = substr_replace($ip_parts[0], '::', $pos, $max);
  415. }
  416. if ($ip_parts[1] !== '') {
  417. return implode(':', $ip_parts);
  418. }
  419. else {
  420. return $ip_parts[0];
  421. }
  422. }
  423. function is_authorized()
  424. {
  425. require_once 'includes/ip_in_range.php';
  426. $api_hosts_file = 'api_authorized.hosts';
  427. $api_fwd_hosts_file = 'api_authorized.fwd_hosts';
  428. global $db, $settings;
  429. if(!@$settings['use_authorized_hosts']){
  430. return true;
  431. }
  432. $authorized_hosts = array();
  433. $ip = getHostByName(getHostName());
  434. if(is_valid_ipv4($ip))
  435. $authorized_hosts['address']['ipv4'][] = $ip;
  436. elseif(is_valid_ipv6($ip))
  437. $authorized_hosts['address']['ipv6'][] = $ip;
  438. $remote_servers = $db->getRemoteServers();
  439. foreach($remote_servers as $remote_server)
  440. {
  441. $ip = getHostByName($remote_server['agent_ip']);
  442. if(is_valid_ipv4($ip) and !in_array($ip, $authorized_hosts['address']['ipv4']))
  443. $authorized_hosts['address']['ipv4'][] = $ip;
  444. elseif(is_valid_ipv6($ip) and !in_array($ip, $authorized_hosts['address']['ipv6']))
  445. $authorized_hosts['address']['ipv6'][] = $ip;
  446. unset($ip);
  447. }
  448. if(file_exists($api_hosts_file))
  449. {
  450. $hosts_list = file_get_contents($api_hosts_file);
  451. $hosts = preg_split("/[\r\n]+/", $hosts_list);
  452. foreach($hosts as $host)
  453. {
  454. $host = trim($host);
  455. if($host == '')
  456. continue;
  457. if(strstr($host, '/'))
  458. {
  459. list($ip, $range) = explode('/', $host, 2);
  460. if(is_valid_ipv4($ip) and !in_array($host, $authorized_hosts['cidr']['ipv4']))
  461. $authorized_hosts['cidr']['ipv4'][] = $host;
  462. elseif(is_valid_ipv6($ip) and !in_array(ipv6_compress($ip)."/".$range, $authorized_hosts['cidr']['ipv6']))
  463. $authorized_hosts['cidr']['ipv6'][] = ipv6_compress($ip)."/".$range;
  464. unset($ip, $range);
  465. }
  466. else
  467. {
  468. $ip = getHostByName($host);
  469. if(is_valid_ipv4($ip) and !in_array($ip, $authorized_hosts['address']['ipv4']))
  470. $authorized_hosts['address']['ipv4'][] = $ip;
  471. elseif(is_valid_ipv6($ip) and !in_array(ipv6_compress($ip), $authorized_hosts['address']['ipv6']))
  472. $authorized_hosts['address']['ipv6'][] = ipv6_compress($ip);
  473. unset($ip);
  474. }
  475. }
  476. }
  477. $client_forwarded_ip = getClientForwardedIP();
  478. $client_ip = $_SERVER['REMOTE_ADDR'];
  479. ## Check authorized_hosts
  480. $authorized_host = false;
  481. if(is_valid_ipv4($client_ip))
  482. {
  483. if(in_array($client_ip, $authorized_hosts['address']['ipv4']))
  484. $authorized_host = true;
  485. else
  486. {
  487. foreach($authorized_hosts['cidr']['ipv4'] as $ipv4_cidr)
  488. if(ipv4_in_range($client_ip, $ipv4_cidr))
  489. $authorized_host = true;
  490. }
  491. }
  492. elseif(is_valid_ipv6($client_ip))
  493. {
  494. if(in_array(ipv6_compress($client_ip), $authorized_hosts['address']['ipv6']))
  495. $authorized_host = true;
  496. else
  497. {
  498. foreach($authorized_hosts['cidr']['ipv6'] as $ipv6_cidr)
  499. if(ipv6_in_range(ipv6_compress($client_ip), $ipv6_cidr))
  500. $authorized_host = true;
  501. }
  502. }
  503. if($authorized_host)
  504. {
  505. if($client_forwarded_ip)
  506. {
  507. ## Check also authorized_fwd_hosts
  508. $authorized_fwd_hosts = array();
  509. if(file_exists($api_fwd_hosts_file))
  510. {
  511. $fwd_hosts_list = file_get_contents($api_fwd_hosts_file);
  512. $fwd_hosts = preg_split("/[\r\n]+/", $fwd_hosts_list);
  513. foreach($fwd_hosts as $fwd_host)
  514. {
  515. $fwd_host = trim($fwd_host);
  516. if($fwd_host == '')
  517. continue;
  518. if(strstr($fwd_host, '/'))
  519. {
  520. list($ip, $range) = explode('/', $fwd_host, 2);
  521. if(is_valid_ipv4($ip) and !in_array($fwd_host, $authorized_fwd_hosts['cidr']['ipv4']))
  522. $authorized_fwd_hosts['cidr']['ipv4'][] = $fwd_host;
  523. elseif(is_valid_ipv6($ip) and !in_array(ipv6_compress($ip)."/".$range, $authorized_fwd_hosts['cidr']['ipv6']))
  524. $authorized_fwd_hosts['cidr']['ipv6'][] = ipv6_compress($ip)."/".$range;
  525. unset($ip, $range);
  526. }
  527. else
  528. {
  529. $ip = getHostByName($fwd_host);
  530. if(is_valid_ipv4($ip) and !in_array($ip, $authorized_fwd_hosts['address']['ipv4']))
  531. $authorized_fwd_hosts['address']['ipv4'][] = $ip;
  532. elseif(is_valid_ipv6($ip) and !in_array(ipv6_compress($ip), $authorized_fwd_hosts['address']['ipv6']))
  533. $authorized_fwd_hosts['address']['ipv6'][] = ipv6_compress($ip);
  534. unset($ip);
  535. }
  536. }
  537. if(is_valid_ipv4($client_forwarded_ip))
  538. {
  539. if(in_array($client_forwarded_ip, $authorized_fwd_hosts['address']['ipv4']))
  540. return true;
  541. else
  542. {
  543. foreach($authorized_fwd_hosts['cidr']['ipv4'] as $ipv4_cidr)
  544. if(ipv4_in_range($client_forwarded_ip, $ipv4_cidr))
  545. return true;
  546. }
  547. }
  548. elseif(is_valid_ipv6($client_forwarded_ip))
  549. {
  550. if(in_array(ipv6_compress($client_forwarded_ip), $authorized_fwd_hosts['address']['ipv6']))
  551. return true;
  552. else
  553. {
  554. foreach($authorized_fwd_hosts['cidr']['ipv6'] as $ipv6_cidr)
  555. if(ipv6_in_range(ipv6_compress($client_forwarded_ip), $ipv6_cidr))
  556. return true;
  557. }
  558. }
  559. }
  560. }
  561. else
  562. return true;
  563. }
  564. return false;
  565. }
  566. ?>