WebOrbiton
v1.0.1

PriviMetrics 2

104 lines · 3.7 KB
  1. <?php
  2. ​
  3. if (!function_exists('pm_fetch_location')) {
  4. ​
  5. function pm_fetch_location(string $ip): array
  6. {
  7. $service = env('GEO_EXTERNAL_SERVICE', 'ip-api');
  8. $apiKey = env('GEO_API_KEY', '');
  9. ​
  10. switch ($service) {
  11. case 'ip-api':
  12. return pm_geo_ip_api($ip);
  13. case 'ipinfo':
  14. return pm_geo_ipinfo($ip, $apiKey);
  15. case 'ipstack':
  16. return pm_geo_ipstack($ip, $apiKey);
  17. case 'ip2location':
  18. return pm_geo_ip2location($ip, $apiKey);
  19. case 'dbip':
  20. return pm_geo_dbip($ip);
  21. default:
  22. return ['country' => 'Unknown', 'code' => 'XX'];
  23. }
  24. }
  25. ​
  26. function pm_geo_ip_api(string $ip): array
  27. {
  28. $url = "http://ip-api.com/json/{$ip}?fields=status,country,countryCode";
  29. $ch = curl_init($url);
  30. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  31. curl_setopt($ch, CURLOPT_TIMEOUT, 2);
  32. $response = curl_exec($ch);
  33. if (PHP_VERSION_ID < 80000) {
  34. curl_close($ch);
  35. }
  36. ​
  37. $data = $response ? json_decode($response, true) : null;
  38. if ($data && ($data['status'] ?? '') === 'success') {
  39. return ['country' => $data['country'], 'code' => $data['countryCode']];
  40. }
  41. return ['country' => 'Unknown', 'code' => 'XX'];
  42. }
  43. ​
  44. function pm_geo_ipinfo(string $ip, string $token): array
  45. {
  46. if ($token === '') return ['country' => 'Unknown', 'code' => 'XX'];
  47. $url = "https://ipinfo.io/{$ip}/json?token=" . urlencode($token);
  48. $response = @file_get_contents($url);
  49. if ($response) {
  50. $data = json_decode($response, true);
  51. if ($data && isset($data['country'])) {
  52. return ['country' => $data['country'], 'code' => $data['country']];
  53. }
  54. }
  55. return ['country' => 'Unknown', 'code' => 'XX'];
  56. }
  57. ​
  58. function pm_geo_ipstack(string $ip, string $apiKey): array
  59. {
  60. if ($apiKey === '') return ['country' => 'Unknown', 'code' => 'XX'];
  61. $url = "http://api.ipstack.com/{$ip}?access_key=" . urlencode($apiKey);
  62. $response = @file_get_contents($url);
  63. if ($response) {
  64. $data = json_decode($response, true);
  65. if ($data && isset($data['country_name'], $data['country_code'])) {
  66. return ['country' => $data['country_name'], 'code' => $data['country_code']];
  67. }
  68. }
  69. return ['country' => 'Unknown', 'code' => 'XX'];
  70. }
  71. ​
  72. function pm_geo_ip2location(string $ip, string $apiKey): array
  73. {
  74. if ($apiKey === '') return ['country' => 'Unknown', 'code' => 'XX'];
  75. $url = "https://api.ip2location.io/?key=" . urlencode($apiKey) . "&ip={$ip}";
  76. $ch = curl_init($url);
  77. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  78. curl_setopt($ch, CURLOPT_TIMEOUT, 3);
  79. $response = curl_exec($ch);
  80. if (PHP_VERSION_ID < 80000) {
  81. curl_close($ch);
  82. }
  83. ​
  84. $data = $response ? json_decode($response, true) : null;
  85. if ($data && isset($data['country_code'])) {
  86. return ['country' => $data['country_name'], 'code' => $data['country_code']];
  87. }
  88. return ['country' => 'Unknown', 'code' => 'XX'];
  89. }
  90. ​
  91. function pm_geo_dbip(string $ip): array
  92. {
  93. $url = "https://api.db-ip.com/v2/free/{$ip}";
  94. $response = @file_get_contents($url);
  95. if ($response) {
  96. $data = json_decode($response, true);
  97. if ($data && isset($data['countryName'], $data['countryCode'])) {
  98. return ['country' => $data['countryName'], 'code' => $data['countryCode']];
  99. }
  100. }
  101. return ['country' => 'Unknown', 'code' => 'XX'];
  102. }
  103. }
  104. ​