From 8479c78becc0b0da1d55232e1f5544f0328c3a46 Mon Sep 17 00:00:00 2001 From: Aaron Dalton Date: Mon, 5 Sep 2016 04:11:44 +0000 Subject: [PATCH] Moved to built-in fetch and caching utilities, greatly reducing the number of API calls --- CHANGELOG.md | 8 +++++- README.md | 4 +-- blueprints.yaml | 2 +- classes/geoplugin.class.php | 55 ++++++++++++------------------------- geoplugin.php | 2 +- 5 files changed, 28 insertions(+), 43 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f7a10ab..a756782 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,10 @@ -# v0.1.0 +# v1.0.1 +## 09/04/2016 + +1. [](#improved) + * Moved to the built-in fetch and cache utilities, saving on API calls + +# v1.0.0 ## 09/03/2016 1. [](#new) diff --git a/README.md b/README.md index 7467c0b..04bf583 100644 --- a/README.md +++ b/README.md @@ -45,6 +45,4 @@ plugins.geoplugin.latitude ## Performance -I don't know enough about the Grav lifecycle to know how often this API call actually occurs. If you have lots of API calls happening, or if you have lots of traffic, this plugin may become problematic. - -If this is an area of expertise of yours, I welcome feedback and pull requests. \ No newline at end of file +This plugin uses Grav's built-in caching API, so only one external API call should ever be made for a given IP address unless you clear the cache. diff --git a/blueprints.yaml b/blueprints.yaml index bd072d0..f023332 100644 --- a/blueprints.yaml +++ b/blueprints.yaml @@ -1,5 +1,5 @@ name: Geoplugin -version: 1.0.0 +version: 1.0.1 description: Geo locates your visitor using GeoPlugin.com icon: globe author: diff --git a/classes/geoplugin.class.php b/classes/geoplugin.class.php index 18268d1..a05cb70 100644 --- a/classes/geoplugin.class.php +++ b/classes/geoplugin.class.php @@ -26,8 +26,15 @@ */ +/* +The licence text above has been untouched, but the code has been modified to use +built-in Grav utilities. +*/ + namespace Grav\Plugin; +use Grav\Common\GPM\Response; + class geoPlugin { //the geoPlugin server @@ -55,22 +62,23 @@ function geoPlugin() { } - function locate($ip = null) { + function locate($cache, $ip = null) { global $_SERVER; if ( is_null( $ip ) ) { $ip = $_SERVER['REMOTE_ADDR']; } - - $host = str_replace( '{IP}', $ip, $this->host ); - $host = str_replace( '{CURRENCY}', $this->currency, $host ); - - $data = array(); - - $response = $this->fetch($host); - - $data = unserialize($response); + + $data = $cache->fetch('geoplugin.'.$ip); + if (! $data) { + $host = str_replace( '{IP}', $ip, $this->host ); + $host = str_replace( '{CURRENCY}', $this->currency, $host ); + $response = Response::get($host); + $data = array(); + $data = unserialize($response); + $cache->save('geoplugin.'.$ip, $data); + } //set the geoPlugin vars $this->ip = $ip; @@ -89,33 +97,6 @@ function locate($ip = null) { } - function fetch($host) { - - if ( function_exists('curl_init') ) { - - //use cURL to fetch data - $ch = curl_init(); - curl_setopt($ch, CURLOPT_URL, $host); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); - curl_setopt($ch, CURLOPT_USERAGENT, 'geoPlugin PHP Class v1.0'); - $response = curl_exec($ch); - curl_close ($ch); - - } else if ( ini_get('allow_url_fopen') ) { - - //fall back to fopen() - $response = file_get_contents($host, 'r'); - - } else { - - trigger_error ('geoPlugin class Error: Cannot retrieve data. Either compile PHP with cURL support or enable allow_url_fopen in php.ini ', E_USER_ERROR); - return; - - } - - return $response; - } - function convert($amount, $float=2, $symbol=true) { //easily convert amounts to geolocated currency. diff --git a/geoplugin.php b/geoplugin.php index 8299fdb..d45ddeb 100644 --- a/geoplugin.php +++ b/geoplugin.php @@ -42,7 +42,7 @@ public function onPluginsInitialized() // Enable the main event we are interested in $geoplugin = new geoPlugin(); - $geoplugin->locate(); + $geoplugin->locate($this->grav['cache']); $this->config->set('plugins.geoplugin.city', $geoplugin->city); $this->config->set('plugins.geoplugin.region', $geoplugin->region); $this->config->set('plugins.geoplugin.areaCode', $geoplugin->areaCode);