From 45a95d25a34d0a9a9e2dd2975547bdee6da836d3 Mon Sep 17 00:00:00 2001
From: Nikita Petrov <nikita.petrov.drupal@gmail.com>
Date: Sat, 14 Mar 2015 19:56:23 +0300
Subject:  Nikita Petrov: Generated with Drush iq

---
 README.txt          |  43 ++++++++++++++-
 geoplugin.api.php   |  20 ++++++-
 geoplugin.class.php | 154 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 geoplugin.module    |  15 +++++
 4 files changed, 229 insertions(+), 3 deletions(-)
 create mode 100644 geoplugin.class.php

diff --git a/README.txt b/README.txt
index 17bd5d7..9a43b4d 100644
--- a/README.txt
+++ b/README.txt
@@ -3,7 +3,48 @@ geoPlugin

 geoPlugin is a free resource to provide geolocation technology to your Drupal site.

-This module integrates geoPlugin and Drupal via JavaScript.
+This module integrates geoPlugin and Drupal.
+
+
+Installation
+============
+
+Just install the module in your usual way. No additional setup is required.
+
+
+Usage
+=====
+
+You can use this module in two ways: via JavaScript call or via PHP call.
+
+1. JavaScript example:
+
+var geoplugin = jQuery.cookie('geoplugin');
+if (geoplugin && 'geoplugin_city' in geoplugin) {
+  console.log("You are in " + geoplugin.geoplugin_city);
+}
+
+2. PHP example:
+
+if (module_exists('geoplugin')) {
+  $geoplugin = geoplugin_locate();
+
+  echo "Geolocation results for {$geoplugin->ip}: <br />\n".
+    "City: {$geoplugin->city} <br />\n".
+    "Region: {$geoplugin->region} <br />\n".
+    "Area Code: {$geoplugin->areaCode} <br />\n".
+    "DMA Code: {$geoplugin->dmaCode} <br />\n".
+    "Country Name: {$geoplugin->countryName} <br />\n".
+    "Country Code: {$geoplugin->countryCode} <br />\n".
+    "Longitude: {$geoplugin->longitude} <br />\n".
+    "Latitude: {$geoplugin->latitude} <br />\n".
+    "Currency Code: {$geoplugin->currencyCode} <br />\n".
+    "Currency Symbol: {$geoplugin->currencySymbol} <br />\n".
+    "Exchange Rate: {$geoplugin->currencyConverter} <br />\n";
+}
+
+See more examples on http://www.geoplugin.com/webservices/php#php_class
+

 Author
 ======
diff --git a/geoplugin.api.php b/geoplugin.api.php
index d54aae3..73ce985 100644
--- a/geoplugin.api.php
+++ b/geoplugin.api.php
@@ -3,6 +3,22 @@
 /**
  * @file
  * Hooks and API functions provided by geoPlugin module.
- *
- * @TODO: Provide server-based Drupal API.
+ *
  */
+
+/**
+ * Example of hook_geoplugin_settings_alter().
+ */
+function hook_geoplugin_settings_alter(&$geoplugin) {
+  // The default currency is USD.
+  // If we wanted to change the base currency, we would use the following line
+  $geoplugin->currency = 'EUR';
+}
+
+/**
+ * Example of hook_geoplugin_info_alter().
+ */
+function hook_geoplugin_info_alter(&$geoplugin) {
+  // If we want somehow alter the resulted info of geoplugin, we can do it here.
+  if ($geoplugin->countryCode == 'RU') $geoplugin->country = 'Russia';
+}
diff --git a/geoplugin.class.php b/geoplugin.class.php
new file mode 100644
index 0000000..05391bd
--- /dev/null
+++ b/geoplugin.class.php
@@ -0,0 +1,154 @@
+<?php
+/*
+This PHP class is free software: you can redistribute it and/or modify
+the code under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+However, the license header, copyright and author credits
+must not be modified in any form and always be displayed.
+
+This class is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+@author geoPlugin (gp_support@geoplugin.com)
+@copyright Copyright geoPlugin (gp_support@geoplugin.com)
+$version 1.01
+
+
+This PHP class uses the PHP Webservice of http://www.geoplugin.com/ to geolocate IP addresses
+
+Geographical location of the IP address (visitor) and locate currency (symbol, code and exchange rate) are returned.
+
+See http://www.geoplugin.com/webservices/php for more specific details of this free service
+
+*/
+
+class geoPlugin {
+
+	//the geoPlugin server
+	var $host = 'http://www.geoplugin.net/php.gp?ip={IP}&base_currency={CURRENCY}';
+
+	//the default base currency
+	var $currency = 'USD';
+
+	//initiate the geoPlugin vars
+	var $ip = null;
+	var $city = null;
+	var $region = null;
+	var $areaCode = null;
+	var $dmaCode = null;
+	var $countryCode = null;
+	var $countryName = null;
+	var $continentCode = null;
+	var $latitude = null;
+	var $longitude = null;
+	var $currencyCode = null;
+	var $currencySymbol = null;
+	var $currencyConverter = null;
+
+	function geoPlugin() {
+
+	}
+
+	function locate($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);
+
+		//set the geoPlugin vars
+		$this->ip = $ip;
+		$this->city = $data['geoplugin_city'];
+		$this->region = $data['geoplugin_region'];
+		$this->areaCode = $data['geoplugin_areaCode'];
+		$this->dmaCode = $data['geoplugin_dmaCode'];
+		$this->countryCode = $data['geoplugin_countryCode'];
+		$this->countryName = $data['geoplugin_countryName'];
+		$this->continentCode = $data['geoplugin_continentCode'];
+		$this->latitude = $data['geoplugin_latitude'];
+		$this->longitude = $data['geoplugin_longitude'];
+		$this->currencyCode = $data['geoplugin_currencyCode'];
+		$this->currencySymbol = $data['geoplugin_currencySymbol'];
+		$this->currencyConverter = $data['geoplugin_currencyConverter'];
+
+	}
+
+	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.
+		if ( !is_numeric($this->currencyConverter) || $this->currencyConverter == 0 ) {
+			trigger_error('geoPlugin class Notice: currencyConverter has no value.', E_USER_NOTICE);
+			return $amount;
+		}
+		if ( !is_numeric($amount) ) {
+			trigger_error ('geoPlugin class Warning: The amount passed to geoPlugin::convert is not numeric.', E_USER_WARNING);
+			return $amount;
+		}
+		if ( $symbol === true ) {
+			return $this->currencySymbol . round( ($amount * $this->currencyConverter), $float );
+		} else {
+			return round( ($amount * $this->currencyConverter), $float );
+		}
+	}
+
+	function nearby($radius=10, $limit=null) {
+
+		if ( !is_numeric($this->latitude) || !is_numeric($this->longitude) ) {
+			trigger_error ('geoPlugin class Warning: Incorrect latitude or longitude values.', E_USER_NOTICE);
+			return array( array() );
+		}
+
+		$host = "http://www.geoplugin.net/extras/nearby.gp?lat=" . $this->latitude . "&long=" . $this->longitude . "&radius={$radius}";
+
+		if ( is_numeric($limit) )
+			$host .= "&limit={$limit}";
+
+		return unserialize( $this->fetch($host) );
+
+	}
+
+
+}
+
+?>
diff --git a/geoplugin.module b/geoplugin.module
index 83ea2a6..2da106a 100644
--- a/geoplugin.module
+++ b/geoplugin.module
@@ -20,3 +20,18 @@ function geoplugin_page_build() {
   drupal_add_js(drupal_get_path('module', 'geoplugin') . '/js/geoplugin.js');

 }
+
+/**
+ * Calls the geoplugin.net web service and returns a geolocation information based on ip of current user.
+ *
+ * @return geoPlugin object.
+ */
+function geoplugin_locate() {
+  // Include the original php class provided by geoplugin.com on http://www.geoplugin.com/webservices/php#php_class page.
+  require_once(drupal_get_path('module', 'geoplugin') . '/geoplugin.class.php');
+  $geoplugin = new geoPlugin();
+  module_invoke_all('geoplugin_settings_alter', $geoplugin);
+  $geoplugin->locate();
+  module_invoke_all('geoplugin_info_alter', $geoplugin);
+  return $geoplugin;
+}
--
1.9.1

