Advertisement
FeRR4L

[PHP] WHMCS GEOLOCATION HOOK

Sep 26th, 2013
380
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.68 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * GEOLOCATION HOOK FOR WHMCS
  5.  * This hook will automatically setup currency and language
  6.  * for nonlogged users in your clientarea.
  7.  *
  8.  *
  9.  * For more information, read the whole article here:
  10.  * http://blog.whmcs.com
  11.  *
  12.  *
  13.  * @author  ModulesGarden
  14.  * @link    http://www.modulesgarden.com
  15.  */
  16.  
  17. $country_to_currency = array(
  18.     'default' => 'ARS',
  19.     'US'      => 'USD',
  20.     'UK'      => 'EUR',
  21.     'PL'      => 'EUR',
  22.     'DE'      => 'EUR',
  23.     'NL'      => 'EUR',
  24.     'FR'      => 'EUR',
  25.     'IT'      => 'EUR',
  26.     'CH'      => 'EUR',
  27.     // NOTE: You can add more below
  28. );
  29.  
  30. $country_to_language = array(
  31.     'default' => 'spanish',
  32.     'US'      => 'english',
  33.     'UK'      => 'english',
  34.     'DE'      => 'english',
  35.     'FR'      => 'english',
  36.     'IT'      => 'english',
  37.     // NOTE: You can add more below
  38. );
  39.  
  40. $language_to_template = array(
  41.     'english' => 'nhp2012theme',
  42.     'german'  => 'nhp2012theme',
  43.     'french'  => 'nhp2012theme',
  44.     'italian' => 'nhp2012theme',
  45.     // NOTE: You can add more below
  46. );
  47.  
  48.  
  49. $allowed_scripts = array(
  50.     'index.php',
  51.     'clientarea.php',
  52.     'cart.php',
  53.     'knowledgebase.php',
  54.     'announcements.php',
  55.     'serverstatus.php',
  56.     'affiliates.php',
  57.     'contact.php'
  58. );
  59.  
  60.  
  61. /**
  62. * FUNCTION geolocation_getCurrencyId
  63. * This function will return currency id for specific code.
  64. *
  65. * @param  string
  66. * @return int
  67. */
  68. function geolocation_getCurrencyId($currency) {
  69.     $q = mysql_query('SELECT id FROM tblcurrencies WHERE code = "'.mysql_escape_string($currency).'"'); // escape string just in case
  70.     $r = mysql_fetch_assoc($q);
  71.     mysql_free_result($q);
  72.    
  73.     if(isset($r['id'])) {
  74.         return $r['id'];
  75.     }
  76. }
  77.  
  78. $script_path        = substr($_SERVER['SCRIPT_NAME'], strrpos($_SERVER['SCRIPT_NAME'], DIRECTORY_SEPARATOR)+1);
  79. $current_template   = $CONFIG['Template'];
  80. $language           = $_SESSION['Language'];
  81.  
  82. /**
  83.  * Main Geolocation Script
  84.  *
  85.  * NOT run script
  86.  * - if we are in adminarea
  87.  * - if already setup for this session
  88.  * - if user is logged in
  89.  * - NEW: allowing to run the hook only for specific scripts (defined above)
  90.  */
  91. if(in_array($script_path, $allowed_scripts) && strpos($_SERVER['REQUEST_URI'], $customadminpath) === false && !isset($_SESSION['geolocation_setup']) && $_SESSION['uid'] == false) {
  92.     $_SESSION['geolocation_setup'] = true; // prevent from redirecting back again in this session
  93.        
  94.    
  95.     /**
  96.      * Get Country using external service - Hostip.info example
  97.      * NOTE: You can handle this part with any other method.
  98.      *
  99.     $current_country = '';
  100.     $ret = file_get_contents("http://api.hostip.info/get_json.php?ip=".$_SERVER['REMOTE_ADDR']); // this can be called also via cURL
  101.     $parsed_json = @json_decode($ret,true);
  102.     if(isset($parsed_json['country_code'])) {
  103.         $current_country = $parsed_json['country_code'];
  104.     }
  105.      *
  106.      */
  107.    
  108.     /**
  109.      * NEW: Get Country using external service - Maxmind GeoLite Example
  110.      * http://dev.maxmind.com/geoip/geolite
  111.      * NOTE: You can handle this part with any other method.
  112.      */
  113.     include "geoip.inc";
  114.     $gi = geoip_open(dirname(__FILE__).DIRECTORY_SEPARATOR."GeoIP.dat", GEOIP_STANDARD);
  115.     $current_country = geoip_country_code_by_addr($gi, $_SERVER['REMOTE_ADDR']);
  116.     geoip_close($gi);
  117.    
  118.     /**
  119.      * Get language, currency and currency ID in order to setup the right values in the system
  120.      */
  121.     $currency = $current_country != '' && isset($country_to_currency[$current_country])
  122.                 ? $country_to_currency[$current_country]
  123.                 : $country_to_currency['default'];    
  124.    
  125.     $currency_id = geolocation_getCurrencyId($currency);
  126.    
  127.     $language = $current_country != '' && isset($country_to_language[$current_country])
  128.                 ? $country_to_language[$current_country]
  129.                 : $country_to_language['default'];
  130.    
  131.    
  132.    
  133.     /**
  134.      * Setup Currency Session
  135.      * NOTE: You can remove/disable this part if not needed.
  136.      */
  137.     if($currency_id && (!isset($_SESSION['currency']) || $_SESSION['currency'] != $currency))
  138.     {        
  139.         $_SESSION['currency'] = $_SESSION['switched_currency'] = $currency_id;
  140.     }
  141.     /**
  142.      * Setup URL Redirection to Switch Language
  143.      * NOTE: You can remove/disable this part if not needed.
  144.      */
  145.     if(!isset($_SESSION['Language']) || $_SESSION['Language'] != $language) {
  146.         $location = '?language='.$language;    
  147.         if($_SERVER['QUERY_STRING'] != '')
  148.             $location .= '&'.$_SERVER['QUERY_STRING'];
  149.        
  150.         ob_clean();
  151.         header('location: '.$location);
  152.         die();
  153.     }
  154.    
  155. }
  156.  
  157. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement