1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
class CloudFlareGeoip extends Geoip |
|
|
|
|
4
|
|
|
{ |
5
|
|
|
private static $debug_email = ''; |
|
|
|
|
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Find the country for an IP address. |
9
|
|
|
* |
10
|
|
|
* Always returns a string (parent method may return array) |
11
|
|
|
* |
12
|
|
|
* To return the code only, pass in true for the |
13
|
|
|
* $codeOnly parameter. |
14
|
|
|
* |
15
|
|
|
* @param string $address The IP address to get the country of |
16
|
|
|
* @param boolean $codeOnly Returns just the country code |
17
|
|
|
* |
18
|
|
|
* @return string |
19
|
|
|
*/ |
20
|
|
|
public static function ip2country($address, $codeOnly = false) |
|
|
|
|
21
|
|
|
{ |
22
|
|
|
$results1 = null; |
23
|
|
|
$results2 = null; |
24
|
|
|
if (isset($_SERVER["HTTP_CF_IPCOUNTRY"])) { |
25
|
|
|
$results2 = $_SERVER["HTTP_CF_IPCOUNTRY"]; |
26
|
|
|
} else { |
27
|
|
|
$results1 = parent::ip2country($address); |
28
|
|
|
} |
29
|
|
|
$returnValue = $results2 ? $results2 : $results1 ; |
30
|
|
|
if ($codeOnly) { |
31
|
|
|
if (is_array($returnValue)) { |
32
|
|
|
return $returnValue['code']; |
33
|
|
|
} else { |
34
|
|
|
return $returnValue; |
35
|
|
|
} |
36
|
|
|
} else { |
37
|
|
|
$name = $this->countryCode2name($returnValue); |
|
|
|
|
38
|
|
|
return array('code' => $returnValue, 'name' => $name); |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Returns the country code, for the current visitor |
44
|
|
|
* |
45
|
|
|
* @return string|bool |
46
|
|
|
*/ |
47
|
|
|
public static function visitor_country() |
|
|
|
|
48
|
|
|
{ |
49
|
|
|
$code = null; |
50
|
|
|
if (Director::isDev()) { |
51
|
|
|
if (isset($_GET['countryfortestingonly'])) { |
52
|
|
|
$code = $_GET['countryfortestingonly']; |
53
|
|
|
Session::set("countryfortestingonly", $code); |
54
|
|
|
} |
55
|
|
|
if ($code = Session::get("countryfortestingonly")) { |
56
|
|
|
Session::set("MyCloudFlareCountry", $code); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
if (!$code) { |
60
|
|
|
if (isset($_SERVER["HTTP_CF_IPCOUNTRY"]) && $_SERVER["HTTP_CF_IPCOUNTRY"]) { |
61
|
|
|
return $_SERVER["HTTP_CF_IPCOUNTRY"]; |
62
|
|
|
} else { |
63
|
|
|
$code = Session::get("MyCloudFlareCountry"); |
64
|
|
|
if (!$code) { |
65
|
|
|
if ($address = self::get_remote_address()) { |
66
|
|
|
$code = CloudFlareGeoip::ip2country($address, true); |
67
|
|
|
} |
68
|
|
|
if (!$code) { |
69
|
|
|
$code = self::get_default_country_code(); |
70
|
|
|
} |
71
|
|
|
if (!$code) { |
72
|
|
|
$code = Config::inst()->get("EcommerceCountry", "default_country_code"); |
73
|
|
|
} |
74
|
|
|
Session::set("MyCloudFlareCountry", $code); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $code; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @see: http://stackoverflow.com/questions/14985518/cloudflare-and-logging-visitor-ip-addresses-via-in-php |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
|
|
public static function get_remote_address() |
|
|
|
|
87
|
|
|
{ |
88
|
|
|
$ip = null; |
89
|
|
|
if (isset($_GET["ipfortestingonly"]) && Director::isDev()) { |
90
|
|
|
$ip = $_GET["ipfortestingonly"]; |
91
|
|
|
} elseif (isset($_SERVER['HTTP_CF_CONNECTING_IP'])) { |
92
|
|
|
$ip = $_SERVER['HTTP_CF_CONNECTING_IP']; |
93
|
|
|
} |
94
|
|
|
if ( |
95
|
|
|
!$ip || |
96
|
|
|
!filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) |
97
|
|
|
) { |
98
|
|
|
$ip = Controller::curr()->getRequest()->getIP(); |
99
|
|
|
} |
100
|
|
|
return $ip; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.