1
0

api_functions.php 21 KB

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