diff --git a/CHANGELOG.md b/CHANGELOG.md index 27199f4..d05a9ba 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,15 @@ # Changelog Craft CMS Geo Plugin +Version 1.4 ([@marzepani](https://github.com/marzepani)) +* Added IP anonymizer +* Removed deprecated service (telize.com) + +Version 1.3 +* _todo_ + Version 1.2 -Added cache option in template tag. -Added configuration using standard environment aware craft config files. +* Added cache option in template tag. +* Added configuration using standard environment aware craft config files. Version 1.0 -Initial Release \ No newline at end of file +Initial release \ No newline at end of file diff --git a/geo/GeoPlugin.php b/geo/GeoPlugin.php index 4597163..4a0b454 100644 --- a/geo/GeoPlugin.php +++ b/geo/GeoPlugin.php @@ -10,7 +10,7 @@ public function getName() public function getVersion() { - return '1.3'; + return '1.4'; } public function getDeveloper() diff --git a/geo/services/Geo_LocationService.php b/geo/services/Geo_LocationService.php index 42a059a..44f99be 100644 --- a/geo/services/Geo_LocationService.php +++ b/geo/services/Geo_LocationService.php @@ -20,17 +20,21 @@ public function getInfo($doCache) "timezone"=>"", "cached"=>false ); - + $devMode = craft()->config->get('devMode'); + $ip = craft()->request->getIpAddress(); $localIps = array("127.0.0.1","::1"); if(in_array($ip,$localIps) or $devMode) { - $ip = craft()->config->get('defaultIp', 'geo'); + $ip = craft()->config->get('defaultIp', 'geo'); } + // Anonymized IP + $ip = $this->anonymizeIp($ip); + $cachedData = craft()->cache->get("craft.geo.".$ip); if ($cachedData){ @@ -38,27 +42,22 @@ public function getInfo($doCache) $cached['cached']= true; return $cached; } - + $apiOne = $this->getNekudoData($ip); if (!empty($apiOne)){ - $data = array_merge($data,$apiOne); - }else{ - - $apiTwo = $this->getTelizeData($ip); - - if (!empty($apiTwo)){ - $data = array_merge($data,$apiTwo); - } - + $data = array_merge($data, $apiOne); + // } else { + // $apiTwo = $this->getTelizeData($ip); + // if (!empty($apiTwo)){ + // $data = array_merge($data, $apiTwo); + // } } - if($doCache){ $seconds = craft()->config->get('cacheTime', 'geo'); - craft()->cache->add("craft.geo.".$ip,json_encode($data),$seconds); + craft()->cache->add("craft.geo.".$ip,json_encode($data),$seconds); } - return $data; } @@ -106,27 +105,61 @@ private function getNekudoData($ip){ return $data; } - private function getTelizeData($ip){ - - $url = "/geip/".$ip; - $telizeClient = new \Guzzle\Http\Client("http://www.telize.com"); - $response = $telizeClient->get($url, array(), array("exceptions" => false))->send(); + // private function getTelizeData($ip){ + + // $url = "/geip/".$ip; + // $telizeClient = new \Guzzle\Http\Client("http://www.telize.com"); + // $response = $telizeClient->get($url, array(), array("exceptions" => false))->send(); + + // if (!$response->isSuccessful()) { + // return array(); + // } + + // $data = json_decode($response->getBody(),true); + + // $data = array( + // "ip"=>$data['ip'], + // "country_code"=>$data['country_code'], + // "country_name"=>$data['country'], + // "latitude"=>$data['latitude'], + // "longitude"=>$data['longitude'], + // "cached"=>false + // ); + + // return $data; + // } + + private function anonymizeIp($ip) { + if (filter_var($ip, FILTER_VALIDATE_IP) !== false) { + $segments = explode('.', $ip); + + switch (strlen($segments[3])) { + case 1: + case 2: + // Set last segment to zero + $segments[3] = 0; + break; + + case 3: + // Keep the first digit and set the last two to zero + $ending = substr($segments[3], 0, 1); + if (substr($segments[3], 1) == '00') { + // Set two random digits if IP already ended with two zeros + $ending .= rand(0, 9); + $ending .= rand(0, 9); + } else { + $ending .= '00'; + } + $segments[3] = $ending; + break; + } - if (!$response->isSuccessful()) { - return array(); + $anonymizedIp = implode('.', $segments); + return $anonymizedIp; + } else { + // Not a valid IP + return $ip; } - - $data = json_decode($response->getBody(),true); - - $data = array( - "ip"=>$data['ip'], - "country_code"=>$data['country_code'], - "country_name"=>$data['country'], - "latitude"=>$data['latitude'], - "longitude"=>$data['longitude'], - "cached"=>false - ); - - return $data; } + } diff --git a/readme.md b/readme.md index 0ce7ebd..36e2ca5 100644 --- a/readme.md +++ b/readme.md @@ -4,22 +4,26 @@ A simple plugin to get information about your users location. Put the geo folder in your craft plugins folder. -``` -The following will cache the users location from their ip, so subsequent -api calls are not made, but just looked up in the cache: +```twig +{# + # The following will cache the users location from their IP, so subsequent + # api calls are not made, but just looked up in the cache: + #} {% set data = craft.geo.info(true) %} -which is the same as: + +{# which is the same as: #} {% set data = craft.geo.info() %} -An Api call is made on every page view: +{# An Api call is made on every page view: #} {% set data = craft.geo.info(false) %} -You can then access the data like this: +{# You can then access the data like this: #} {{ data.country_code }} ``` + Variables available in craft twig templates: -``` +```twig location: {{ craft.geo.info.country_name }} ip: {{ craft.geo.info.ip }} country_code: {{ craft.geo.info.country_code }} @@ -36,18 +40,18 @@ cached: {{ craft.geo.info.cached }} ``` You are limited to 10,000 requests an hour for this plugin. It caches a single IP -address for 12 hours by default. You can config this with a config file as exaplained below. +address for 12 hours by default. You can config this with a config file as explained below. -If you are in Crafts devMode or visiting the site from the server itself then a default ip adress of 190.93.246.7 will be used. +If you are in Crafts devMode or visiting the site from the server itself then a default IP adress will be used. This setting is configurable by creating a geo.php file in your craft/config folder. An example of this file is found in the geo-examples folder. -# TODO +## TODO -Add additional API endpoints for API redundancy. +* Add additional API endpoints for API redundancy. +* Modularize enpoints so you can add your own endpoint plugins. -Modularize enpoints so you can add your own endpoint plugins. +## Licence -# Licence MIT. Pull requests welcome. \ No newline at end of file