api_functions.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  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. function getClientForwardedIP(){
  329. if(isset($_SERVER['HTTP_CF_CONNECTING_IP']) and !empty($_SERVER['HTTP_CF_CONNECTING_IP']))
  330. return $_SERVER['HTTP_CF_CONNECTING_IP'];
  331. if(isset($_SERVER['HTTP_X_FORWARDED_FOR']) and !empty($_SERVER['HTTP_X_FORWARDED_FOR']))
  332. return $_SERVER['HTTP_X_FORWARDED_FOR'];
  333. if(isset($_SERVER['HTTP_X_REAL_IP']) and !empty($_SERVER['HTTP_X_REAL_IP']))
  334. return $_SERVER['HTTP_X_REAL_IP'];
  335. return false;
  336. }
  337. function is_valid_ipv4($ip)
  338. {
  339. if(filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4))
  340. return true;
  341. return false;
  342. }
  343. function is_valid_ipv6($ip)
  344. {
  345. if(filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6))
  346. return true;
  347. return false;
  348. }
  349. // https://github.com/rmccue/Requests/blob/master/library/Requests/IPv6.php
  350. function ipv6_uncompress($ip)
  351. {
  352. if (substr_count($ip, '::') !== 1) {
  353. return $ip;
  354. }
  355. list($ip1, $ip2) = explode('::', $ip);
  356. $c1 = ($ip1 === '') ? -1 : substr_count($ip1, ':');
  357. $c2 = ($ip2 === '') ? -1 : substr_count($ip2, ':');
  358. if (strpos($ip2, '.') !== false) {
  359. $c2++;
  360. }
  361. // ::
  362. if ($c1 === -1 && $c2 === -1) {
  363. $ip = '0:0:0:0:0:0:0:0';
  364. }
  365. // ::xxx
  366. else if ($c1 === -1) {
  367. $fill = str_repeat('0:', 7 - $c2);
  368. $ip = str_replace('::', $fill, $ip);
  369. }
  370. // xxx::
  371. else if ($c2 === -1) {
  372. $fill = str_repeat(':0', 7 - $c1);
  373. $ip = str_replace('::', $fill, $ip);
  374. }
  375. // xxx::xxx
  376. else {
  377. $fill = ':' . str_repeat('0:', 6 - $c2 - $c1);
  378. $ip = str_replace('::', $fill, $ip);
  379. }
  380. return $ip;
  381. }
  382. function split_v6_v4($ip) {
  383. if (strpos($ip, '.') !== false) {
  384. $pos = strrpos($ip, ':');
  385. $ipv6_part = substr($ip, 0, $pos);
  386. $ipv4_part = substr($ip, $pos + 1);
  387. return array($ipv6_part, $ipv4_part);
  388. }
  389. else {
  390. return array($ip, '');
  391. }
  392. }
  393. function ipv6_compress($ip)
  394. {
  395. // Prepare the IP to be compressed
  396. $ip = ipv6_uncompress($ip);
  397. $ip_parts = split_v6_v4($ip);
  398. // Replace all leading zeros
  399. $ip_parts[0] = preg_replace('/(^|:)0+([0-9])/', '\1\2', $ip_parts[0]);
  400. // Find bunches of zeros
  401. if (preg_match_all('/(?:^|:)(?:0(?::|$))+/', $ip_parts[0], $matches, PREG_OFFSET_CAPTURE)) {
  402. $max = 0;
  403. $pos = null;
  404. foreach ($matches[0] as $match) {
  405. if (strlen($match[0]) > $max) {
  406. $max = strlen($match[0]);
  407. $pos = $match[1];
  408. }
  409. }
  410. $ip_parts[0] = substr_replace($ip_parts[0], '::', $pos, $max);
  411. }
  412. if ($ip_parts[1] !== '') {
  413. return implode(':', $ip_parts);
  414. }
  415. else {
  416. return $ip_parts[0];
  417. }
  418. }
  419. function is_authorized()
  420. {
  421. require_once 'includes/ip_in_range.php';
  422. $api_hosts_file = 'api_authorized.hosts';
  423. $api_fwd_hosts_file = 'api_authorized.fwd_hosts';
  424. global $db, $settings;
  425. if(!@$settings['use_authorized_hosts']){
  426. return true;
  427. }
  428. $authorized_hosts = array();
  429. $ip = getHostByName(getHostName());
  430. if(is_valid_ipv4($ip))
  431. $authorized_hosts['address']['ipv4'][] = $ip;
  432. elseif(is_valid_ipv6($ip))
  433. $authorized_hosts['address']['ipv6'][] = $ip;
  434. $remote_servers = $db->getRemoteServers();
  435. foreach($remote_servers as $remote_server)
  436. {
  437. $ip = getHostByName($remote_server['agent_ip']);
  438. if(is_valid_ipv4($ip) and !in_array($ip, $authorized_hosts['address']['ipv4']))
  439. $authorized_hosts['address']['ipv4'][] = $ip;
  440. elseif(is_valid_ipv6($ip) and !in_array($ip, $authorized_hosts['address']['ipv6']))
  441. $authorized_hosts['address']['ipv6'][] = $ip;
  442. unset($ip);
  443. }
  444. if(file_exists($api_hosts_file))
  445. {
  446. $hosts_list = file_get_contents($api_hosts_file);
  447. $hosts = preg_split("/[\r\n]+/", $hosts_list);
  448. foreach($hosts as $host)
  449. {
  450. $host = trim($host);
  451. if($host == '')
  452. continue;
  453. if(strstr($host, '/'))
  454. {
  455. list($ip, $range) = explode('/', $host, 2);
  456. if(is_valid_ipv4($ip) and !in_array($host, $authorized_hosts['cidr']['ipv4']))
  457. $authorized_hosts['cidr']['ipv4'][] = $host;
  458. elseif(is_valid_ipv6($ip) and !in_array(ipv6_compress($ip)."/".$range, $authorized_hosts['cidr']['ipv6']))
  459. $authorized_hosts['cidr']['ipv6'][] = ipv6_compress($ip)."/".$range;
  460. unset($ip, $range);
  461. }
  462. else
  463. {
  464. $ip = getHostByName($host);
  465. if(is_valid_ipv4($ip) and !in_array($ip, $authorized_hosts['address']['ipv4']))
  466. $authorized_hosts['address']['ipv4'][] = $ip;
  467. elseif(is_valid_ipv6($ip) and !in_array(ipv6_compress($ip), $authorized_hosts['address']['ipv6']))
  468. $authorized_hosts['address']['ipv6'][] = ipv6_compress($ip);
  469. unset($ip);
  470. }
  471. }
  472. }
  473. $client_forwarded_ip = getClientForwardedIP();
  474. $client_ip = $_SERVER['REMOTE_ADDR'];
  475. ## Check authorized_hosts
  476. $authorized_host = false;
  477. if(is_valid_ipv4($client_ip))
  478. {
  479. if(in_array($client_ip, $authorized_hosts['address']['ipv4']))
  480. $authorized_host = true;
  481. else
  482. {
  483. foreach($authorized_hosts['cidr']['ipv4'] as $ipv4_cidr)
  484. if(ipv4_in_range($client_ip, $ipv4_cidr))
  485. $authorized_host = true;
  486. }
  487. }
  488. elseif(is_valid_ipv6($client_ip))
  489. {
  490. if(in_array(ipv6_compress($client_ip), $authorized_hosts['address']['ipv6']))
  491. $authorized_host = true;
  492. else
  493. {
  494. foreach($authorized_hosts['cidr']['ipv6'] as $ipv6_cidr)
  495. if(ipv6_in_range(ipv6_compress($client_ip), $ipv6_cidr))
  496. $authorized_host = true;
  497. }
  498. }
  499. if($authorized_host)
  500. {
  501. if($client_forwarded_ip)
  502. {
  503. ## Check also authorized_fwd_hosts
  504. $authorized_fwd_hosts = array();
  505. if(file_exists($api_fwd_hosts_file))
  506. {
  507. $fwd_hosts_list = file_get_contents($api_fwd_hosts_file);
  508. $fwd_hosts = preg_split("/[\r\n]+/", $fwd_hosts_list);
  509. foreach($fwd_hosts as $fwd_host)
  510. {
  511. $fwd_host = trim($fwd_host);
  512. if($fwd_host == '')
  513. continue;
  514. if(strstr($fwd_host, '/'))
  515. {
  516. list($ip, $range) = explode('/', $fwd_host, 2);
  517. if(is_valid_ipv4($ip) and !in_array($fwd_host, $authorized_fwd_hosts['cidr']['ipv4']))
  518. $authorized_fwd_hosts['cidr']['ipv4'][] = $fwd_host;
  519. elseif(is_valid_ipv6($ip) and !in_array(ipv6_compress($ip)."/".$range, $authorized_fwd_hosts['cidr']['ipv6']))
  520. $authorized_fwd_hosts['cidr']['ipv6'][] = ipv6_compress($ip)."/".$range;
  521. unset($ip, $range);
  522. }
  523. else
  524. {
  525. $ip = getHostByName($fwd_host);
  526. if(is_valid_ipv4($ip) and !in_array($ip, $authorized_fwd_hosts['address']['ipv4']))
  527. $authorized_fwd_hosts['address']['ipv4'][] = $ip;
  528. elseif(is_valid_ipv6($ip) and !in_array(ipv6_compress($ip), $authorized_fwd_hosts['address']['ipv6']))
  529. $authorized_fwd_hosts['address']['ipv6'][] = ipv6_compress($ip);
  530. unset($ip);
  531. }
  532. }
  533. if(is_valid_ipv4($client_forwarded_ip))
  534. {
  535. if(in_array($client_forwarded_ip, $authorized_fwd_hosts['address']['ipv4']))
  536. return true;
  537. else
  538. {
  539. foreach($authorized_fwd_hosts['cidr']['ipv4'] as $ipv4_cidr)
  540. if(ipv4_in_range($client_forwarded_ip, $ipv4_cidr))
  541. return true;
  542. }
  543. }
  544. elseif(is_valid_ipv6($client_forwarded_ip))
  545. {
  546. if(in_array(ipv6_compress($client_forwarded_ip), $authorized_fwd_hosts['address']['ipv6']))
  547. return true;
  548. else
  549. {
  550. foreach($authorized_fwd_hosts['cidr']['ipv6'] as $ipv6_cidr)
  551. if(ipv6_in_range(ipv6_compress($client_forwarded_ip), $ipv6_cidr))
  552. return true;
  553. }
  554. }
  555. }
  556. }
  557. else
  558. return true;
  559. }
  560. return false;
  561. }
  562. ?>