-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwmGeolocation.php
127 lines (110 loc) · 3.13 KB
/
wmGeolocation.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
<?php
/**
* World Manager geolocation plugin for Joomla.
* This is required to use the wmCareers & wmLanguageSwitch plugins.
* @package wmGeolocation
* @version 1.1
* @author Grant McNally <[email protected]>
* @link https://github.com/grantism/wmGeolocation
*/
defined('_JEXEC') or die;
require_once JPATH_PLUGINS . '/system/wmGeolocation/geoip/vendor/autoload.php';
use Joomla\CMS\Factory as JFactory;
use Joomla\CMS\Plugin\CMSPlugin as JPlugin;
use GeoIp2\Database\Reader;
class PlgSystemWmGeolocation extends JPlugin {
private $countryIsoCode = 'US';
/**
* PlgSystemWmGeolocation constructor.
* @param object $subject
* @param array $config
*/
public function __construct(&$subject, $config) {
parent::__construct($subject, $config);
$reader = new Reader(JPATH_PLUGINS . '/system/wmGeolocation/geoip/GeoLite2-Country.mmdb');
$ip = $_SERVER['REMOTE_ADDR'];
try {
$geoIp = $reader->country($ip);
$this->countryIsoCode = $geoIp->country->isoCode;
}
catch (Exception $e) {
/**
* Exception is thrown if IP address isn't found in the database.
* This mostly happens on local networks during development & testing.
*/
}
// Set global var so users location can be accessed in the site.
JFactory::getApplication()->set('wmGeolocation', $this);
}
/**
* @return string
*/
public function getCountryIsoCode() {
return $this->countryIsoCode;
}
/**
* @return bool
*/
public function isSpanish() {
$spanishCountries = array(
'AR', //Argentina
'BO', //Bolivia
'CL', //Chile
'CO', //Colombia
'CR', //Costa Rica
'CU', //Cuba
'DO', //Dominican Republic
'EC', //Ecuador
'ES', //Spain
'GQ', //Equatorial Guinea
'GT', //Guatemala
'HN', //Honduras
'MX', //Mexico
'NI', //Nicaragua
'PA', //Panama
'PE', //Peru
'PR', //Puerto Rico
'PY', //Paraguay
'SV', //El Salvador
'UY', //Uruguay
'VE', //Venezuela
);
return in_array($this->getCountryIsoCode(), $spanishCountries);
}
/**
* @return bool
*/
public function isEnglish() {
$englishCountries = array(
'AU', //Australia
'GB', //UK
'NZ', //New Zealand
'US', //USA
);
return in_array($this->getCountryIsoCode(), $englishCountries);
}
/**
* @return bool
*/
public function isAustralia() {
return $this->getCountryIsoCode() == 'AU';
}
/**
* @return bool
*/
public function isUnitedStates() {
return $this->getCountryIsoCode() == 'US';
}
/**
* @return bool
*/
public function isCanada() {
return $this->getCountryIsoCode() == 'CA';
}
/**
* @return bool
*/
public function isIndonesia() {
return $this->getCountryIsoCode() == 'ID';
}
}