-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathipgeo.class.php
1025 lines (912 loc) · 32 KB
/
ipgeo.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* IP Geolocation Class
*
* @package WordPress
* @sub-package ipgeo
* @since 1.2
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly.
if(!class_exists('IP_Geo_Location'))
{
class IP_Geo_Location
{
protected static $_instance = null;
/**
* Get the singleton instance of the class.
*
* @return IP_Geo_Location The singleton instance.
*/
public static function instance() {
if ( is_null( self::$_instance ) ) {
self::$_instance = new self();
}
return self::$_instance;
}
/**
* Construct the plugin object
*
* @return void
*/
public function __construct()
{
// Initialize Settings
add_action('wp_enqueue_scripts', array(&$this, 'ipgeo_enqueue_scripts'));
// ipgeo shortcode
add_shortcode('ipgeo', array(&$this, 'ipgeo_shortcode'));
// WPbakery Functionality
require_once('includes/shortcode-generator/wpbakery/wpbakery.class.php');
add_action( 'vc_before_init' , array( 'IPGeoWpbakeryShortcode' , 'create_shortcode' ) ); // VC functionality
// Gutenberg Functionality
require_once( 'includes/shortcode-generator/gutenberg/gutenberg-block.php' );
// Elementor Functionality
require_once( 'includes/shortcode-generator/elementor/elementor.class.php' );
add_action( 'init' , array( 'IPGeoElementor' , 'init' ) );
// Divi Functionality
require_once( 'includes/shortcode-generator/divi/ipgeo-divi.php' );
do_action('ipgeo_plugin_hooks');
} // END public function __construct()
/**
* Enqueues IP Geo styles and map-related scripts.
*
* @return void
*/
public function ipgeo_enqueue_scripts()
{
wp_enqueue_style('ipgeo', plugins_url( '/assets/css/ipgeo.css', __FILE__ ), array(), IP_GEOLOCATION_VERSION );
// load map styles & scripts
$enable_map_token = get_option('ipgeo_enable_map');
if( $enable_map_token )
{
$map_api_token = get_option('ipgeo_map_api_token');
if($map_api_token)
{
$map_service = get_option('ipgeo_map_service');
switch($map_service)
{
case "cedarmaps":
wp_enqueue_style( 'cedarmaps', 'https://api.cedarmaps.com/cedarmaps.js/v1.8.1/cedarmaps.css' , array(), '1.8.1');
break;
case "google":
wp_enqueue_script( 'google-map-script', 'https://maps.googleapis.com/maps/api/js?key='. esc_attr( $map_api_token ).'&callback=initMap', array(), '3.57', true );
break;
case "leaflet":
wp_enqueue_style('leaflet-map', 'https://unpkg.com/[email protected]/dist/leaflet.css', array(), '1.9.4');
wp_enqueue_script('leaflet-map', 'https://unpkg.com/[email protected]/dist/leaflet.js', array(), '1.9.4', false);
break;
case "mapbox":
wp_enqueue_script( 'mapbox', 'https://api.mapbox.com/mapbox-gl-js/v2.15.0/mapbox-gl.js', array(), '2.15.0', false);
wp_enqueue_style( 'mapbox', 'https://api.mapbox.com/mapbox-gl-js/v2.15.0/mapbox-gl.css', array(), '2.15.0');
break;
case "mapir":
wp_enqueue_style( 'mapir-sdk', 'https://cdn.map.ir/web-sdk/1.4.2/css/mapp.min.css', array(), '1.4.2');
wp_enqueue_style( 'mapir-style', 'https://cdn.map.ir/web-sdk/1.4.2/css/fa/style.css', array(), '1.4.2');
wp_enqueue_script( 'mapir-env' , 'https://cdn.map.ir/web-sdk/1.4.2/js/mapp.env.js', array('jquery'), '1.4.2', true);
wp_add_inline_script( 'mapir-env', 'var $ = jQuery.noConflict();', 'before' );
wp_enqueue_script( 'mapir-min' , 'https://cdn.map.ir/web-sdk/1.4.2/js/mapp.min.js', array('jquery', 'mapir-env'), '1.4.2', true );
wp_add_inline_script( 'mapir-min', 'var $ = jQuery.noConflict();', 'before' );
wp_add_inline_script( 'mapir-min', 'jQuery(document).ready(function() {
var app = new Mapp({
element: "#ipgeo_map",
presets: {
latlng: {
lat: 36.844718933105,
lng: 54.438331604004
},
zoom: 16
},
apiKey: "'. esc_attr( $map_api_token ) .'"
});
app.addVectorLayers();
app.addMarker({
name: "basic-marker",
latlng: {
lat: 36.844718933105,
lng: 54.438331604004
},
popup: false
});
});', 'after' );
break;
case "parsimap":
wp_enqueue_script('parsi-map', 'https://cdn.parsimap.ir/third-party/mapbox-gl-js/v1.13.0/mapbox-gl.js', array(), '1.13.0', false);
wp_enqueue_style('parsi-map', 'https://cdn.parsimap.ir/third-party/mapbox-gl-js/v1.13.0/mapbox-gl.css', array(), '1.13.0');
break;
}
}
}
}
/**
* Get the Mapir environment settings.
*
* @return array The Mapir environment settings.
*/
public function get_mapir_env() {
$output = array(
'mode' => 'production',
'domain' => site_url(),
'href' => site_url()
);
return $output;
}
/**
* Get client IP
*
* @return string client_ip
*/
public function get_client_ip()
{
$client_ip = '';
if(isset($_SERVER['HTTP_CLIENT_IP']) && !empty($_SERVER['HTTP_CLIENT_IP']))
$ip = $_SERVER['HTTP_CLIENT_IP'];
elseif(isset($_SERVER['HTTP_X_FORWARDED_FOR']) && !empty($_SERVER['HTTP_X_FORWARDED_FOR']))
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
elseif(isset($_SERVER['REMOTE_ADDR']) && !empty($_SERVER['REMOTE_ADDR']))
$ip = $_SERVER['REMOTE_ADDR'];
return explode(",", $ip)[0];
}
/**
* IP Geo Shortcode: show IP Geo Location form and result.
*
* @return string The HTML output of the IP Geo shortcode.
*/
public function ipgeo_shortcode() {
ob_start();
$ipgeo_input_class = get_option('ipgeo_input_class');
$ipgeo_button_class = get_option('ipgeo_button_class');
?>
<div class="ipgeo-container">
<form class="ipgeo-form" method="post" action="">
<?php wp_nonce_field('ipgeo_location_nonce_action', 'ipgeo_location_nonce'); ?>
<input type="text" <?php if(!empty($ipgeo_input_class)) echo 'class="'.esc_attr( $ipgeo_input_class ).'"'; ?> name="ip" value="<?php if(isset($_POST['ip']) && isset($_POST['ipgeo_location_nonce']) && wp_verify_nonce($_POST['ipgeo_location_nonce'], 'ipgeo_location_nonce_action')) echo esc_attr( $_POST['ip'] ); ?>" placeholder="<?php esc_attr_e('Enter IP Address here', 'ip-geolocation'); ?>" />
<input type="submit" <?php if(!empty($ipgeo_button_class)) echo 'class="'.esc_attr( $ipgeo_button_class ).'"'; ?> name="check" value="<?php esc_attr_e('Search', 'ip-geolocation'); ?>" />
</form>
<?php
$ip = '';
if(isset($_POST['check']) && isset($_POST['ipgeo_location_nonce']) && wp_verify_nonce($_POST['ipgeo_location_nonce'], 'ipgeo_location_nonce_action')) {
$ip = sanitize_text_field( $_POST['ip'] );
$this->get_ip_info($ip);
} else {
$default_ip_type = get_option('ipgeo_default_ip_type');
if(!empty($default_ip_type)) {
if($default_ip_type == 'client') {
$ip = $this->get_client_ip();
if(!empty($ip))
$this->get_ip_info($ip);
else
$this->get_ip_info();
} else {
$this->get_ip_info();
}
} else {
$ip = $this->get_client_ip();
$this->get_ip_info($ip);
}
}
?>
</div>
<?php
$output = ob_get_contents();
ob_end_clean();
return apply_filters('ipgeo_shortcode_filter', $output);
}
/**
* Get IP information and display the result.
*
* @param string|null $inp_ip (optional) The IP address to retrieve information for.
* @return void
*/
public function get_ip_info( $inp_ip = null )
{
$ip = sanitize_text_field( $inp_ip );
$error = "";
// get api service
if(!empty($ip) && !WP_Http::is_ip_address($ip))
{
$result['error'] = __('IP Address is invalid.', 'ip-geolocation');
}
else
{
$api_service = get_option('ipgeo_api_service');
if(!empty($api_service))
{
if($api_service =="ip-api" || $api_service == 'ipapi')
{
$result = $this->get_result_data_api( $ip, $api_service );
}
else
{
$api_token = get_option('ipgeo_api_token');
// check api token is empty or no
if(!empty($api_token))
{
$result = $this->get_result_data_api( $ip, $api_service, $api_token );
}
}
}
}
// return error
if(isset($result['error']))
{
if(is_array($result['error']))
$error = $result['error']['title'];
else
$error = $result['error'];
}
// show data
if(empty($error))
{
$sanitized_result = $this->get_sanitize_result( $api_service, $result );
if(!empty($sanitized_result))
{
echo '<div id="ipw_main_area" class="home-ip-details">';
// show info
foreach($sanitized_result as $index => $res_value)
{
if($index=="status") continue; // skip 'status' item
if(!empty($res_value) && !is_array($res_value))
{
echo '<div class="json-widget-entry">';
echo '<div class="indent-0 String">';
echo '<i></i> ';
$final_index = ( $index == "ip" || $index == "isp" ) ? esc_attr( strtoupper( $index ) ) : esc_attr( ucwords( $index ) );
echo '<span class="key">'.esc_attr( $final_index ).':</span> ';
if( wp_http_validate_url ( $res_value ) )
echo '<span class="value"><img src="'.esc_url( $res_value ).'" /></span>';
else
echo '<span class="value">'.esc_attr( $res_value ).'</span>';
echo '</div>';
echo '</div>';
}
}
// show latitude
echo '<div class="json-widget-entry">';
echo '<div class="indent-0 String">';
echo '<i></i> ';
echo '<span class="key">Latitude:</span> ';
echo '<span class="value">'.esc_attr( $sanitized_result['location']['lat'] ).'</span>';
echo '</div>';
echo '</div>';
// show longtitude
echo '<div class="json-widget-entry">';
echo '<div class="indent-0 String">';
echo '<i></i> ';
echo '<span class="key">Longitude:</span> ';
echo '<span class="value">'.esc_attr( $sanitized_result['location']['lng'] ).'</span>';
echo '</div>';
echo '</div>';
echo '</div>';
// embed maps
$location = $this->get_api_location( $sanitized_result );
if(!empty($location))
$this->load_maps($location[0], $location[1]);
}
}
else
{
echo '<p class="alert alert-danger">'.esc_attr( $error ).'</p>';
}
}
/**
* Retrieve result data from the API.
*
* @param string $ip The IP address to retrieve information for.
* @param string $api_service The API service to use.
* @param string $api_key (optional) The API key for the service.
* @return array The result data from the API.
*/
protected function get_result_data_api( $ip, $api_service, $api_key = '' )
{
$result = [];
$error = '';
$api_key = esc_attr( $api_key );
// check ans sanitize ip address
if(!empty($ip) && !WP_Http::is_ip_address($ip))
$result['error'] = __('IP Address is invalid.', 'ip-geolocation');
// api service condition
if(empty($error))
{
switch($api_service)
{
case "abstractapi":
if(!empty($ip))
$api_url = 'https://ipgeolocation.abstractapi.com/v1/?api_key='.esc_attr($api_key).'&ip_address='.$ip;
else
$api_url = 'https://ipgeolocation.abstractapi.com/v1/?api_key='.esc_attr($api_key);
break;
case "apiip":
if(!empty($ip))
$api_url = 'http://apiip.net/api/check?accessKey='.esc_attr($api_key).'&ip='.$ip;
else
$api_url = 'http://apiip.net/api/check?accessKey='.esc_attr($api_key);
break;
case "freeipapi":
if(!empty($ip))
$api_url = 'https://freeipapi.com/api/json/'.$ip;
else
$api_url = 'https://freeipapi.com/api/json';
break;
case "ip-api":
if(!empty($ip))
$api_url = 'http://ip-api.com/json/'.$ip;
else
$api_url = 'http://ip-api.com/json/';
break;
case "ipapi":
if(!empty($ip))
$api_url = 'https://ipapi.co/json/'.$ip;
else
$api_url = 'https://ipapi.co/json/';
break;
case "ipdata":
if(!empty($ip))
$api_url = 'https://api.ipdata.co/'.$ip.'?api-key='.esc_attr($api_key);
else
$api_url = 'https://api.ipdata.co/?api-key='.esc_attr($api_key);
break;
case "ip2location":
if(!empty($ip))
$api_url = 'https://api.ip2location.io/?format=json&key='.esc_attr($api_key).'&ip='.$ip;
else
$api_url = 'https://api.ip2location.io/?format=json&key='.esc_attr($api_key);
break;
case "ipbase":
if(!empty($ip))
$api_url = 'https://api.ipbase.com/v2/info?apikey='.esc_attr($api_key).'&ip='.$ip;
else
$api_url = 'https://api.ipbase.com/v2/info?apikey='.esc_attr($api_key);
break;
case "ipgeolocation":
if(!empty($ip))
$api_url = 'https://api.ipgeolocation.io/ipgeo?apikey='.esc_attr($api_key).'&ip='.$ip;
else
$api_url = 'https://api.ipgeolocation.io/ipgeo?apikey='.esc_attr($api_key);
break;
case "ipify":
if(!empty($ip))
$api_url = 'https://geo.ipify.org/api/v2/country,city,vpn?apiKey='.esc_attr($api_key).'&ipAddress='.$ip;
else
$api_url = 'https://geo.ipify.org/api/v2/country,city,vpn?apiKey='.esc_attr($api_key);
break;
case "ipinfo":
if(!empty($ip))
$api_url = 'https://ipinfo.io/'.$ip.'?token='.esc_attr($api_key);
else
$api_url = 'https://ipinfo.io/?token='.esc_attr($api_key);
break;
case "ipstack":
if(!empty($ip))
$api_url = 'http://api.ipstack.com/'.$ip.'?access_key='.esc_attr($api_key);
else
$api_url = 'http://api.ipstack.com/check?access_key='.esc_attr($api_key);
break;
case "ipwhois":
if(!empty($ip))
$api_url = 'http://ipwho.is/'.$ip;
else
$api_url = 'http://ipwho.is/';
break;
}
$response = wp_remote_get( wp_http_validate_url( $api_url ) );
if( ! is_wp_error( $response ) )
$result = json_decode($response['body'], true);
}
if(isset($result['status']) && $result['status']=="fail")
$result['error'] = __('API doesn\'t get a valid data.', 'ip-geolocation');
return apply_filters('result_data_api_filter', $result);
}
/**
* Get the sanitized result from the raw API result.
*
* @param string $api_service The API service used.
* @param array $raw_api_result The raw API result.
* @return array The sanitized result.
*/
protected function get_sanitize_result( $api_service, $raw_api_result )
{
$sanitized_result = [];
if(!empty($raw_api_result))
{
switch($api_service)
{
case "abstractapi":
if(is_array($raw_api_result))
{
foreach($raw_api_result as $key => $val)
{
if(!is_array($val) && !is_null($val))
{
if($key!="latitude" && $key!="longitude" && $key!="success")
{
$sanitized_result[$key] = $val;
}
}
else
{
if($key=="timezone")
{
$sanitized_result['timezone'] = $val['name'];
}
}
}
if( !empty( $raw_api_result['latitude'] ) && !empty( $raw_api_result['longitude'] ) )
$sanitized_result['location'] = [ 'lat' => $raw_api_result['latitude'] , 'lng' => $raw_api_result['longitude'] ];
}
break;
case "apiip":
if(is_array($raw_api_result))
{
foreach($raw_api_result as $key => $val)
{
if( $key == 'borders' || $key == 'topLevelDomains' || is_array($val) )
continue;
if($key!="latitude" && $key!="longitude")
{
$sanitized_result[$key] = $val;
}
}
if(!empty($raw_api_result['currency']))
$sanitized_result['currency'] = $raw_api_result['currency']['name'];
if( !empty( $raw_api_result['latitude'] ) && !empty( $raw_api_result['longitude'] ) )
$sanitized_result['location'] = [ 'lat' => $raw_api_result['latitude'] , 'lng' => $raw_api_result['longitude'] ];
}
break;
case "freeipapi":
if(is_array($raw_api_result))
{
foreach($raw_api_result as $key => $val)
{
if($key!="Latitude" && $key!="Longitude")
{
$sanitized_result[$key] = $val;
}
}
if( !empty( $raw_api_result['Latitude'] ) && !empty( $raw_api_result['Longitude'] ) )
$sanitized_result['location'] = [ 'lat' => $raw_api_result['Latitude'] , 'lng' => $raw_api_result['Longitude'] ];
}
break;
case "ip-api":
if(is_array($raw_api_result))
{
foreach($raw_api_result as $key => $val)
{
if(!is_array($val) && !is_null($val))
{
if($key!="lat" && $key!="lon" && $key!="success")
{
$sanitized_result[$key] = $val;
}
}
}
if( !empty( $raw_api_result['lat'] ) && !empty( $raw_api_result['lon'] ) )
$sanitized_result['location'] = [ 'lat' => $raw_api_result['lat'] , 'lng' => $raw_api_result['lon'] ];
}
break;
case "ipapi":
if(is_array($raw_api_result))
{
foreach($raw_api_result as $key => $val)
{
if(!is_array($val) && !is_null($val))
{
if($key!="latitude" && $key!="longitude" && $key!="success")
{
$sanitized_result[$key] = $val;
}
}
}
if( !empty( $raw_api_result['latitude'] ) && !empty( $raw_api_result['longitude'] ) )
$sanitized_result['location'] = [ 'lat' => $raw_api_result['latitude'] , 'lng' => $raw_api_result['longitude'] ];
}
break;
case "ipdata":
if(is_array($raw_api_result))
{
foreach($raw_api_result as $key => $val)
{
if(!is_array($val) && !is_null($val))
{
if($key!="latitude" && $key!="longitude" && $key!="flag" && $key != 'emoji_unicode')
{
$sanitized_result[$key] = $val;
}
}
}
if( !empty( $raw_api_result['currency']['name'] ) )
$sanitized_result['currency'] = $raw_api_result['currency']['name'];
if( !empty( $raw_api_result['time_zone']['name'] ) )
$sanitized_result['Time Zone'] = $raw_api_result['time_zone']['name'];
if( !empty( $raw_api_result['languages'] ) )
$sanitized_result['languages'] = implode(",", array_column( $raw_api_result['languages'], 'name' ) );
if( !empty( $raw_api_result['latitude'] ) && !empty( $raw_api_result['longitude'] ) )
$sanitized_result['location'] = [ 'lat' => $raw_api_result['latitude'] , 'lng' => $raw_api_result['longitude'] ];
}
break;
case "ip2location":
if(is_array($raw_api_result))
{
foreach($raw_api_result as $key => $val)
{
if($key!="latitude" && $key!="longitude")
{
$sanitized_result[$key] = $val;
}
}
if( !empty( $raw_api_result['latitude'] ) && !empty( $raw_api_result['longitude'] ) )
$sanitized_result['location'] = [ 'lat' => $raw_api_result['latitude'] , 'lng' => $raw_api_result['longitude'] ];
}
break;
case "ipbase":
if(is_array($raw_api_result['data']))
{
foreach($raw_api_result['data'] as $key => $val)
{
if(is_array($val))
{
if(!is_null($val))
{
// get timezone
if($key=="timezone")
$sanitized_result['timezone'] = $val['id'];
// get isp info
if($key=="connection")
{
$sanitized_result['ASN'] = $val['asn'];
$sanitized_result['organization'] = $val['organization'];
$sanitized_result['ISP'] = $val['isp'];
}
// get location info
if($key=="location")
{
$sanitized_result['continent'] = $val['continent'];
$sanitized_result['country'] = $val['country']['alpha2'];
$sanitized_result['city'] = $val['city']['name'];
if( !empty( $val['latitude'] ) && !empty( $val['longitude'] ) )
$sanitized_result['location'] = [ 'lat' => $val['latitude'] , 'lng' => $val['longitude'] ];
}
}
}
else
{
$sanitized_result[$key] = $val;
}
}
}
break;
case "ipgeolocation":
if(is_array($raw_api_result))
{
foreach($raw_api_result as $key => $val)
{
if(!is_array($val) && !is_null($val))
{
if($key!="latitude" && $key!="longitude")
{
$sanitized_result[$key] = $val;
}
}
else
{
if($key == 'time_zone')
{
$sanitized_result[$key] = $val['name'];
}
if($key == 'currency')
{
$sanitized_result[$key] = "{$val['code']} / {$val['name']} / {$val['symbol']}";
}
}
}
if( !empty( $raw_api_result['latitude'] ) && !empty( $raw_api_result['longitude'] ) )
$sanitized_result['location'] = [ 'lat' => $raw_api_result['latitude'] , 'lng' => $raw_api_result['longitude'] ];
}
break;
case "ipify":
if(is_array($raw_api_result))
{
foreach($raw_api_result as $key => $val)
{
if(is_array($val) && $key=="location")
{
if(!is_null($val) && !is_null($val))
{
foreach($val as $index => $child_val)
{
if($index=="country" || $index=="city" || $index=="region" || $index=="timezone")
$sanitized_result[$index] = $child_val;
}
if( !empty( $val['lat'] ) && !empty( $val['lng'] ) )
$sanitized_result['location'] = [ 'lat' => $val['lat'] , 'lng' => $val['lng'] ];
}
}
else
{
$sanitized_result[$key] = $val;
}
}
}
break;
case "ipinfo":
if(is_array($raw_api_result))
{
foreach($raw_api_result as $key => $val)
{
if(!is_array($val) && !is_null($val))
{
if($key=="loc")
{
$location = explode(",", $val);
if( !empty( $location[0] ) && !empty( $location[1] ) )
$sanitized_result['location'] = [ 'lat' => $location[0], 'lng' => $location[1] ];
}
else
{
$sanitized_result[$key] = $val;
}
}
}
}
break;
case "ipstack":
if(is_array($raw_api_result))
{
foreach($raw_api_result as $key => $val)
{
if( is_array($val) )
continue;
if($key!="latitude" && $key!="longitude")
{
$sanitized_result[$key] = $val;
}
}
if(!empty($raw_api_result['location']))
{
$sanitized_result['captial'] = $raw_api_result['location']['capital'];
$sanitized_result['languages'] = implode(", ", array_column( $raw_api_result['location']['languages'], 'name') );
}
if(!empty($raw_api_result['time_zone']))
{
$sanitized_result['timezone'] = $raw_api_result['time_zone']['id'];
}
if( !empty( $raw_api_result['latitude'] ) && !empty( $raw_api_result['longitude'] ) )
$sanitized_result['location'] = [ 'lat' => $raw_api_result['latitude'] , 'lng' => $raw_api_result['longitude'] ];
}
break;
case "ipwhois":
if(is_array($raw_api_result))
{
foreach($raw_api_result as $key => $val)
{
if(!is_array($val) && !is_null($val))
{
if($key!="latitude" && $key!="longitude" && $key!="success")
{
$sanitized_result[$key] = $val;
}
}
else
{
if($key == 'timezone')
{
$sanitized_result['timezone'] = $val['id'];
}
}
}
if( !empty( $raw_api_result['latitude'] ) && !empty( $raw_api_result['longitude'] ) )
$sanitized_result['location'] = [ 'lat' => $raw_api_result['latitude'] , 'lng' => $raw_api_result['longitude'] ];
}
break;
}
}
return $sanitized_result;
}
/**
* Get the location for the API result.
*
* @param array $api_result The API result.
* @return array The location coordinates [lat, lng].
*/
protected function get_api_location( $api_result )
{
$loc = [];
if(!is_null($api_result))
{
if(!is_null($api_result['location']))
{
return [
$api_result['location']['lat'],
$api_result['location']['lng']
];
}
}
return $loc;
}
/**
* Load Maps on the site.
*
* @param float $lat The latitude.
* @param float $lng The longitude.
* @return void
*/
protected function load_maps($lat, $lng)
{
$latitude = esc_attr( $lat );
$longitude = esc_attr( $lng );
$enable_map_token = get_option('ipgeo_enable_map');
if( $enable_map_token )
{
$map_api_token = get_option('ipgeo_map_api_token');
$map_width_section = get_option('ipgeo_map_width_section');
$map_height_section = get_option('ipgeo_map_height_section');
if($map_api_token)
{
?>
<!-- IPGeo Map Section -->
<div id="ipgeo_map" <?php if( !empty( $map_width_section ) || !empty( $map_height_section ) ) : ?>style="<?php if( !empty( $map_width_section ) ) echo 'width:'.esc_attr( $map_width_section ).';'; ?><?php if( !empty( $map_height_section ) ) echo 'height:'.esc_attr( $map_height_section ).';'; ?>"<?php endif; ?>></div>
<?php
$map_service = get_option('ipgeo_map_service');
switch($map_service)
{
case "cedarmaps":
?>
<script>
function initMap()
{
// Map options
var cm_options = {
"center":{
"lat": <?php echo esc_attr( $latitude ); ?>,
"lng": <?php echo esc_attr( $longitude ); ?>
},
"maptype": "light",
"scrollWheelZoom": true,
"zoomControl": true,
"zoom": 15,
"minZoom": 6,
"maxZoom":17,
"legendControl": false,
"attributionControl": false
}
// Initialized CedarMap
var map = window.L.cedarmaps.map("ipgeo_map", "https://api.cedarmaps.com/v1/tiles/cedarmaps.streets.json?access_token=<?php echo esc_attr( $map_api_token ); ?>", cm_options);
// Markers options
var markers = [{"center":{"lat":<?php echo esc_attr( $latitude ); ?>,"lng":<?php echo esc_attr( $longitude ); ?>},"iconOpts":{"iconUrl":"https://api.cedarmaps.com/v1/markers/marker-default.png","iconRetinaUrl":"https://api.cedarmaps.com/v1/markers/[email protected]","iconSize":[82,98]}}];
var markersLeaflet = [];
var _marker = null;
map.setView(cm_options.center, cm_options.zoom);
// Add Markers on Map
if (markers.length === 0) return;
markers.map(function(marker) {
var iconOpts = {
iconUrl: marker.iconOpts.iconUrl,
iconRetinaUrl: marker.iconOpts.iconRetinaUrl,
iconSize: marker.iconOpts.iconSize,
popupAnchor: [0, -49]
};
const markerIcon = {
icon: window.L.icon(iconOpts)
};
_marker = new window.L.marker(marker.center, markerIcon);
markersLeaflet.push(_marker);
if (marker.popupContent) {
_marker.bindPopup(marker.popupContent);
}
_marker.addTo(map);
});
// Bounding Map to Markers
if (markers.length > 1) {
var group = new window.L.featureGroup(markersLeaflet);
map.fitBounds(group.getBounds(), { padding: [30, 30] });
}
};
(function(c,e,d,a){
var p = c.createElement(e);
p.async = 1; p.src = d;
p.onload = a;
c.body.appendChild(p);
})(document, 'script', 'https://api.cedarmaps.com/cedarmaps.js/v1.8.1/cedarmaps.js', initMap);
</script>
<?php
break;
case "google":
?>
<script>
// Initialize and add the map
function initMap() {
// The location of Uluru
var uluru = {lat: <?php echo esc_attr( $latitude ); ?>, lng: <?php echo esc_attr( $longitude ); ?>};
// The map, centered at Uluru
var map = new google.maps.Map(
document.getElementById('ipgeo_map'), {zoom: 18, center: uluru}
);
// The marker, positioned at Uluru
var marker = new google.maps.Marker({position: uluru, map: map});
}
</script>
<script defer src="https://maps.googleapis.com/maps/api/js?key=<?php echo esc_attr( $map_api_token ); ?>&callback=initMap"></script>
<?php
break;
case "leaflet":
?>
<script>
// Initialize the map
const map = L.map('ipgeo_map')
// Get the tile layer from OpenStreetMaps
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
// Specify the maximum zoom of the map
maxZoom: 18,
// Set the attribution for OpenStreetMaps
attribution: 'IP Geo Location | © <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
// Set the view of the map
// with the latitude, longitude and the zoom value
map.setView([<?php echo esc_attr( $latitude ); ?>, <?php echo esc_attr( $longitude ); ?>], 16);
// Show a market at the position of the Eiffel Tower
L.marker([<?php echo esc_attr( $latitude ); ?>, <?php echo esc_attr( $longitude ); ?>]).addTo(map);
</script>
<?php
break;
case "mapbox":
?>
<script>
mapboxgl.accessToken = '<?php echo esc_attr( $map_api_token ); ?>';
const map = new mapboxgl.Map({
container: 'ipgeo_map',
// Choose from Mapbox's core styles, or make your own style with Mapbox Studio
style: 'mapbox://styles/mapbox/streets-v12',
center: [<?php echo esc_attr( $longitude ); ?>, <?php echo esc_attr( $latitude ); ?>],
attributionControl: false, // disable the default attribution control
zoom: 15
});
// Create a default Marker and add it to the map.
const marker1 = new mapboxgl.Marker()
.setLngLat([<?php echo esc_attr( $longitude ); ?>, <?php echo esc_attr( $latitude ); ?>])
.addTo(map);
</script>
<?php
break;
case "mapir":
// all scripts loaded in header of page
break;
case "parsimap":
?>
<script>
mapboxgl.setRTLTextPlugin(
'https://cdn.parsimap.ir/third-party/mapbox-gl-js/plugins/mapbox-gl-rtl-text/v0.2.3/mapbox-gl-rtl-text.js',
null,
)
const map = new mapboxgl.Map({
container: 'ipgeo_map',
style: 'https://api.parsimap.ir/styles/parsimap-streets-v11?key=<?php echo esc_attr( $map_api_token ); ?>',
center: [<?php echo esc_attr( $latitude ); ?>, <?php echo esc_attr( $longitude ); ?>],
zoom: 8,
})
</script>
<?php
break;
}
}
}
}