PluginProbe
Depicter — Popup & Slider Builder / trunk
Depicter — Popup & Slider Builder vtrunk
4.8.1 trunk 1.0.0 1.1.0 1.1.2 1.1.4 1.1.6 1.1.7 1.1.8 1.1.9 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.5 1.3.8 1.5.0 1.5.1 1.5.2 1.5.5 1.6.0 1.6.1 1.6.2 1.7.0 All 76 releases
depicter / app / src / Services / GeoLocateService.php

GeoLocateService.php in Depicter — Popup & Slider Builder trunk, at app/src/Services/GeoLocateService.php

129 lines 3.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Depicter\Services;
4
5 use Averta\Core\Utility\JSON;
6 use Depicter\GuzzleHttp\Exception\GuzzleException;
7
8 class GeoLocateService
9 {
10
11 /**
12 * API endpoints for geolocating an IP address
13 *
14 * @var array
15 */
16 private $geoipApies = array(
17 // 'ipinfo.io' => 'https://ipinfo.io/%s/json',
18 'ip-api.com' => 'http://ip-api.com/json/%s',
19 'geoplugin.net' => 'http://www.geoplugin.net/json.gp?ip=%s',
20 );
21
22 public function getCountry(){
23 $ip = $this->getIP();
24 if ( ! $country_code = \Depicter::cache()->get( 'country_of_' . $ip ) ) {
25 $geoipServicesKeys = array_keys( $this->geoipApies );
26
27 shuffle( $geoipServicesKeys );
28
29 foreach ( $geoipServicesKeys as $serviceName ) {
30 $serviceEndpoint = $this->geoipApies[ $serviceName ];
31 try {
32 $response = \Depicter::remote()->get( sprintf( $serviceEndpoint, $ip ) );
33 $responseBody = $response->getBody()->getContents();
34 if ( JSON::isJson( $responseBody ) ) {
35 $data = JSON::decode( $responseBody );
36
37 switch ( $serviceName ) {
38 case 'ipinfo.io':
39 $country_code = $data->country ?? '';
40 break;
41 case 'ip-api.com':
42 $country_code = $data->countryCode ?? ''; // @codingStandardsIgnoreLine
43 break;
44 case 'geoplugin.net':
45 $country_code = $data->geoplugin_countryCode ?? ''; // @codingStandardsIgnoreLine
46 break;
47 default:
48 $country_code = '';
49 break;
50 }
51
52 $country_code = sanitize_text_field( strtoupper( $country_code ) );
53
54 if ( $country_code ) {
55 \Depicter::cache()->set( 'country_of_' . $ip, $country_code, DAY_IN_SECONDS );
56 break;
57 }
58 }
59 } catch( GuzzleException $e ){
60 }
61
62 }
63 }
64
65 return $country_code;
66 }
67
68 /**
69 * Get ip address
70 *
71 * @return mixed
72 */
73 public function getIP(){
74 if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
75 $ip = $_SERVER['HTTP_CLIENT_IP'];
76 } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
77 $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
78 } else {
79 $ip = $_SERVER['REMOTE_ADDR'];
80 }
81
82 return $ip;
83 }
84
85 public function getTimezone(){
86 $ip = $this->getIP();
87 if ( ! $timezone = \Depicter::cache()->get( 'timezone_of_' . $ip ) ) {
88 $geoipServicesKeys = array_keys( $this->geoipApies );
89
90 shuffle( $geoipServicesKeys );
91
92 foreach ( $geoipServicesKeys as $serviceName ) {
93 $serviceEndpoint = $this->geoipApies[ $serviceName ];
94 try {
95 $response = \Depicter::remote()->get( sprintf( $serviceEndpoint, $ip ) );
96 $responseBody = $response->getBody()->getContents();
97 if ( JSON::isJson( $responseBody ) ) {
98 $data = JSON::decode( $responseBody );
99
100 switch ( $serviceName ) {
101 case 'ipinfo.io':
102 $timezone = $data->timezone ?? '';
103 break;
104 case 'ip-api.com':
105 $timezone = $data->timezone ?? ''; // @codingStandardsIgnoreLine
106 break;
107 case 'geoplugin.net':
108 $timezone = $data->geoplugin_timezone ?? ''; // @codingStandardsIgnoreLine
109 break;
110 default:
111 $timezone = '';
112 break;
113 }
114
115 if ( $timezone ) {
116 \Depicter::cache()->set( 'timezone_of_' . $ip, $timezone, DAY_IN_SECONDS );
117 break;
118 }
119 }
120 } catch( GuzzleException $e ){
121 }
122
123 }
124 }
125
126 return $timezone;
127 }
128 }
129