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