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 bool $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 | |||||
42 | return $returnValue; |
||||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||||
43 | } |
||||
44 | $name = parent::countryCode2name($returnValue); |
||||
0 ignored issues
–
show
It seems like
$returnValue can also be of type boolean and null ; however, parameter $code of Sunnysideup\Geoip\Geoip::countryCode2name() does only seem to accept string , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
45 | |||||
46 | return [ |
||||
47 | 'code' => $returnValue, |
||||
48 | 'name' => $name, |
||||
49 | ]; |
||||
50 | } |
||||
51 | |||||
52 | /** |
||||
53 | * Returns the country code, for the current visitor. |
||||
54 | * |
||||
55 | * @return bool|string |
||||
56 | */ |
||||
57 | public static function visitor_country() |
||||
58 | { |
||||
59 | $code = null; |
||||
60 | if (Director::isDev()) { |
||||
61 | if (isset($_GET['countryfortestingonly'])) { |
||||
62 | $code = $_GET['countryfortestingonly']; |
||||
63 | Controller::curr()->getRequest()->getSession()->set('countryfortestingonly', $code); |
||||
64 | } |
||||
65 | if ($code = Controller::curr()->getRequest()->getSession()->get('countryfortestingonly')) { |
||||
66 | Controller::curr()->getRequest()->getSession()->set('MyCloudFlareCountry', $code); |
||||
67 | } |
||||
68 | } |
||||
69 | if (! $code) { |
||||
70 | if (isset($_SERVER['HTTP_CF_IPCOUNTRY']) && $_SERVER['HTTP_CF_IPCOUNTRY']) { |
||||
71 | return $_SERVER['HTTP_CF_IPCOUNTRY']; |
||||
72 | } |
||||
73 | $code = Controller::curr()->getRequest()->getSession()->get('MyCloudFlareCountry'); |
||||
74 | if (! $code) { |
||||
75 | $address = self::get_remote_address(); |
||||
76 | if (strlen($address) > 3) { |
||||
77 | $code = CloudFlareGeoIP::ip2country($address, true); |
||||
78 | } |
||||
79 | if (! $code) { |
||||
80 | $code = self::get_default_country_code(); |
||||
81 | } |
||||
82 | if ('' === $code) { |
||||
83 | $code = Config::inst()->get('CloudFlareGeoip', 'default_country_code'); |
||||
84 | } |
||||
85 | Controller::curr()->getRequest()->getSession()->set('MyCloudFlareCountry', $code); |
||||
86 | } |
||||
87 | } |
||||
88 | |||||
89 | return $code; |
||||
90 | } |
||||
91 | |||||
92 | /** |
||||
93 | * @see: http://stackoverflow.com/questions/14985518/cloudflare-and-logging-visitor-ip-addresses-via-in-php |
||||
94 | * |
||||
95 | * @return string |
||||
96 | */ |
||||
97 | public static function get_remote_address() |
||||
98 | { |
||||
99 | $ip = null; |
||||
100 | if (isset($_GET['ipfortestingonly']) && Director::isDev()) { |
||||
101 | $ip = $_GET['ipfortestingonly']; |
||||
102 | } elseif (isset($_SERVER['HTTP_CF_CONNECTING_IP'])) { |
||||
103 | $ip = $_SERVER['HTTP_CF_CONNECTING_IP']; |
||||
104 | } |
||||
105 | if (! $ip || |
||||
106 | ! filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) |
||||
107 | ) { |
||||
108 | $ip = Controller::curr()->getRequest()->getIP(); |
||||
109 | } |
||||
110 | |||||
111 | return $ip; |
||||
112 | } |
||||
113 | } |
||||
114 |