Skip to content
This repository has been archived by the owner on Jul 5, 2019. It is now read-only.

Commit

Permalink
Added IP anonymizer and removed deprecated service (telize.com)
Browse files Browse the repository at this point in the history
  • Loading branch information
craftedsystems committed Aug 30, 2017
1 parent 98f6cb3 commit 7adfa96
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 52 deletions.
13 changes: 10 additions & 3 deletions 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
Initial release
2 changes: 1 addition & 1 deletion geo/GeoPlugin.php
Expand Up @@ -10,7 +10,7 @@ public function getName()

public function getVersion()
{
return '1.3';
return '1.4';
}

public function getDeveloper()
Expand Down
103 changes: 68 additions & 35 deletions geo/services/Geo_LocationService.php
Expand Up @@ -20,45 +20,44 @@ 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){
$cached = json_decode($cachedData,true);
$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;
}
Expand Down Expand Up @@ -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;
}

}
30 changes: 17 additions & 13 deletions readme.md
Expand Up @@ -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 }}
Expand All @@ -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.

0 comments on commit 7adfa96

Please sign in to comment.