# This patch file was generated by NetBeans IDE
# Following Index: paths are relative to: C:\wamp\www\gautier\sites\all\modules\smart_ip
# This patch can be applied using context Tools: Patch action on respective folder.
# It uses platform neutral UTF-8 encoding and \n newlines.
# Above lines and this line are ignored by the patching process.
Index: includes/smart_ip.admin.inc
--- includes/smart_ip.admin.inc Base (BASE)
+++ includes/smart_ip.admin.inc Locally Modified (Based On LOCAL)
@@ -15,7 +15,7 @@
   // Define submit handler function
   $form['#submit'][] = 'smart_ip_admin_settings_submit';
   $form['#validate'][] = 'smart_ip_admin_settings_validate';
-  if (!variable_get('smart_ip_use_ipinfodb_service', TRUE)) {
+  if (!variable_get('smart_ip_use_ipinfodb_service', TRUE) && !variable_get('smart_ip_use_maxmindgeoip_service', FALSE)) {
     $recover = !variable_get('smart_ip_db_update_busy', FALSE) & (variable_get('smart_ip_get_zip_done', FALSE) | variable_get('smart_ip_extract_zip_done', FALSE) | variable_get('smart_ip_store_location_csv_done', FALSE));
     // Container for database update preference forms 
     $form['smart_ip_database_update'] = array(
@@ -114,13 +114,46 @@
     '#default_value' => variable_get('smart_ip_ipinfodb_key'),
   );
   
+  
+  // Form to enable the use of Maxmind GeoIP service
+  $use_maxmindgeoip = variable_get('smart_ip_use_maxmindgeoip_service', TRUE);
+  $form['smart_ip_preferences']['smart_ip_use_maxmindgeoip_service'] = array(
+    '#type'          => 'checkbox',
+    '#title'         => t('Use Maxmind service'),
+    '#description'   => t('geoip.maxmind.com service will be used to query Geo IP information instead of Smart IP database. For using this, you need to buy a developer key.'),
+    '#default_value' => $use_maxmindgeoip,
+  );
+  
+  // Form for choosing Maxmind GeoIP service
+  $form['smart_ip_preferences']['smart_ip_maxmind_service'] = array(
+    '#type'          => 'radios',
+    '#title'         => t('Maxmind GeoIP service'),
+    '#description'   => t('Choose service. Only Country is available at the moment.'),
+    '#default_value' => variable_get('smart_ip_maxmind_service'),
+    '#options' => array(
+        'country' => 'Country',
+        //@todo Use other services
+        /*'city' => 'City',
+        'city_isp_org' => 'City/ISP/Org',
+        'omni' => 'Omni'*/
+    )
+  );
+  
+  // Form for entering Maxmind GeoIP key
+  $form['smart_ip_preferences']['smart_ip_maxmind_key'] = array(
+    '#type'          => 'textfield',
+    '#title'         => t('Maxmind GeoIP developer key'),
+    '#description'   => t('The use of geoip.maxmind.com service requires developer key.'),
+    '#default_value' => variable_get('smart_ip_maxmind_key'),
+  );
+  
   // Form to enable automatic Smart IP database update every one month
   $form['smart_ip_preferences']['smart_ip_auto_update'] = array(
     '#type'          => 'checkbox',
     '#title'         => t('Automatic Smart IP database update'),
     '#description'   => t('Database will be automatically updated via cron.php every one month (Maxmind updates every first day of a month). !cron must be enabled for this to work. Note: this option is very expensive to run and it requires about 450MB database and 600MB additional database space for temporary working table.', array('!cron' => l(t('Cron'), 'admin/config/system/cron'))),
-    '#default_value' => $use_ipinfodb & variable_get('smart_ip_auto_update', TRUE),
-    '#disabled'      => $use_ipinfodb,
+    '#default_value' => $use_ipinfodb & !$use_maxmindgeoip & variable_get('smart_ip_auto_update', TRUE),
+    '#disabled'      => $use_ipinfodb || $use_maxmindgeoip,
     '#states'        => array(
       'unchecked' => array(
         ':input[name="smart_ip_use_ipinfodb_service"]' => array('checked' => TRUE),
@@ -130,9 +163,11 @@
       ),
       'checked' => array(
         ':input[name="smart_ip_use_ipinfodb_service"]' => array('checked' => FALSE),
+        ':input[name="smart_ip_use_maxmindgeoip_service"]' => array('checked' => FALSE),
       ),
       'enabled' => array(
         ':input[name="smart_ip_use_ipinfodb_service"]' => array('checked' => FALSE),
+        ':input[name="smart_ip_use_maxmindgeoip_service"]' => array('checked' => FALSE),
       ),
     ),
   );
@@ -211,6 +246,8 @@
   $location = smart_ip_get_location($lookup);
   if ($location) {
     $message = '<p>' . t('IP Address @ip is assigned to the following location details:', array('@ip' => $lookup)) . '</p><dl>' .
+      '<dt>' . t('Country code:') . '</dt>' .
+      '<dd>' . t('%country_code', array('%country_code' => $location['country_code'])) . '</dd>' .
\ No newline at end of file
       '<dt>' . t('Country:') . '</dt>' .
       '<dd>' . t('%country', array('%country' => $location['country'])) . '</dd>' .
       '<dt>' . t('Region:') . '</dt>' .
Index: smart_ip.module
--- smart_ip.module Base (BASE)
+++ smart_ip.module Locally Modified (Based On LOCAL)
@@ -13,7 +13,7 @@
  * Additionaly, users accessing a server on a local network may be using
  * an IP that is not assigned to any country (e.g. 192.168.x.x).
  *
- * @author Roland Michael dela PeÃ±a.
+ * @author Roland Michael dela Peña.
  */
 define('SMART_IP_LOCATION_CSV', 'GeoLiteCity-Location.csv');
 define('SMART_IP_BLOCKS_CSV', 'GeoLiteCity-Blocks.csv');
@@ -351,7 +351,8 @@
  */
 function smart_ip_get_location($ip_address) {
   $use_ipinfodb_service = variable_get('smart_ip_use_ipinfodb_service', TRUE);
-  if (!$use_ipinfodb_service) {
+  $use_maxmind_service = variable_get('smart_ip_use_maxmindgeoip_service', TRUE);
+  if (!$use_ipinfodb_service && !$use_maxmind_service) {
     $ip = explode('.', $ip_address);
     $result = FALSE;
     if (count($ip) == 4) {
@@ -369,6 +370,21 @@
       }
     }
   }
+  
+  //Use Maxmind GeoIP service if needed : only for Maxmind GeoIp Country
+  if($use_maxmind_service){
+      $request = drupal_http_request(smart_ip_get_maxmindgeoip_url($ip_address));
+      $result['country_code'] = strtolower($request->data);
+      $result['country']      = '';
+      $result['region']       = '';
+      $result['region_code']  = '';
+      $result['city']         = '';
+      $result['zip']          = '';
+      $result['latitude']     = '';
+      $result['longitude']    = '';
+  }
+  
+  //Else use IpInfoDb
   if ($use_ipinfodb_service || empty($result)) {
     $ipinfodb_key = variable_get('smart_ip_ipinfodb_key', 0);
     $version = variable_get('smart_ip_use_ipinfodb_api_version', 3);
@@ -470,6 +486,18 @@
   }
 }
 
+/*
+ * Get Maxmind GeoIP URL
+ * @param String $ip_adresse IP address to geolocate
+ */
+function smart_ip_get_maxmindgeoip_url($ip_address) {
+    switch(variable_get('smart_ip_maxmind_service')){
+        case 'country':
+            return "https://geoip.maxmind.com/a?l=".variable_get('smart_ip_maxmind_key')."&i=$ip_address";
+    }
+    return '';
+}
+
\ No newline at end of file
 /**
  * Helper function for grabbing Maxmind's CSV archive filename.
  */
