Advertisement
Guest User

source

a guest
May 29th, 2014
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.72 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' => 'USD',
  19.     'US'      => 'USD',
  20.     'id'      => 'IDR',
  21.     // NOTE: You can add more below
  22. );
  23.  
  24. $country_to_language = array(
  25.     'default' => 'english',
  26.     'US'    => 'english',
  27.     'id'    => 'indonesia'
  28.     // NOTE: You can add more below
  29. );
  30.  
  31. $language_to_template = array(
  32.     'english'  => 'default',
  33.     'indonesia'  => 'portal',
  34.     'default' => 'default'
  35.  
  36.     // NOTE: You can add more below
  37. );
  38.  
  39. $allowed_scripts = array(
  40.     'p1.php',
  41.     'index.php',
  42.     'clientarea.php',
  43.     'cart.php',
  44.     'knowledgebase.php',
  45.     'announcements.php',
  46.     'serverstatus.php',
  47.     'affiliates.php',
  48.     'contact.php'
  49.     // NOTE: You can add more below
  50. );
  51.  
  52.  
  53.  
  54. /**
  55. * FUNCTION geolocation_getCurrencyId
  56. * This function will return currency id for specific code.
  57. *
  58. * @param  string
  59. * @return int
  60. */
  61. function geolocation_getCurrencyId($currency) {
  62.     $q = mysql_query('SELECT id FROM tblcurrencies WHERE code = "'.mysql_escape_string($currency).'"'); // escape string just in case
  63.     $r = mysql_fetch_assoc($q);
  64.     mysql_free_result($q);
  65.    
  66.     if(isset($r['id'])) {
  67.         return $r['id'];
  68.     }
  69. }
  70.  
  71. $script_path        = substr($_SERVER['SCRIPT_NAME'], strrpos($_SERVER['SCRIPT_NAME'], DIRECTORY_SEPARATOR)+1);
  72. $current_template   = $_SESSION['Template'];
  73. $_language          = $_SESSION['Language'];
  74.  
  75.  
  76. /**
  77.  * Main Geolocation Script
  78.  *
  79.  * NOT run script
  80.  * - if we are in adminarea
  81.  * - if already setup for this session
  82.  * - if user is logged in
  83.  * - NEW: allowing to run the hook only for specific scripts (defined above)
  84.  */
  85.  
  86. if(in_array($script_path, $allowed_scripts) && strpos($_SERVER['REQUEST_URI'], $customadminpath) === false && $_SESSION['geolocation_setup'] !== true && $_SESSION['uid'] == false) {
  87.     $_SESSION['geolocation_setup'] = true; // prevent from redirecting back again in this session
  88.        
  89.    
  90.     /**
  91.      * Get Country using external service - Hostip.info example
  92.      * NOTE: You can handle this part with any other method.
  93.      *
  94.     $current_country = '';
  95.     $ret = file_get_contents("http://api.hostip.info/get_json.php?ip=".$_SERVER['REMOTE_ADDR']); // this can be called also via cURL
  96.     $parsed_json = @json_decode($ret,true);
  97.     if(isset($parsed_json['country_code'])) {
  98.         $current_country = $parsed_json['country_code'];
  99.     }
  100.      *
  101.      */
  102.    
  103.     /**
  104.      * NEW: Get Country using external service - Maxmind GeoLite Example
  105.      * http://dev.maxmind.com/geoip/geolite
  106.      * NOTE: You can handle this part with any other method.
  107.      */
  108.     include "geoip.inc";
  109.     $gi = geoip_open(dirname(__FILE__).DIRECTORY_SEPARATOR."GeoIP.dat", GEOIP_STANDARD);
  110.     $current_country = geoip_country_code_by_addr($gi, $_SERVER['REMOTE_ADDR']);
  111.     geoip_close($gi);
  112.    
  113.     /**
  114.      * Get language, currency and currency ID in order to setup the right values in the system
  115.      */
  116.     $currency = $current_country != '' && isset($country_to_currency[$current_country])
  117.                 ? $country_to_currency[$current_country]
  118.                 : $country_to_currency['default'];    
  119.    
  120.     $currency_id = geolocation_getCurrencyId($currency);
  121.    
  122.     $_language = $current_country != '' && isset($country_to_language[$current_country])
  123.                 ? $country_to_language[$current_country]
  124.                 : $country_to_language['default'];
  125.    
  126.    
  127.    
  128.     /**
  129.      * Setup Currency Session
  130.      * NOTE: You can remove/disable this part if not needed.
  131.      */
  132.     if($currency_id && (!isset($_SESSION['currency']) || $_SESSION['currency'] != $currency)) {        
  133.         $_SESSION['currency'] = $_SESSION['switched_currency'] = $currency_id;
  134.     }
  135.    
  136.    
  137.     /**
  138.      * Setting up template for redirect
  139.      * NOTE: You can remove/disable this part if not needed.
  140.      */
  141.     $systpl = isset($language_to_template[$_language])
  142.                 ? $language_to_template[$_language]
  143.                 : $language_to_template['default'];
  144.    
  145.     /**
  146.      * Setup URL Redirection to Switch Language
  147.      * NOTE: You can remove/disable this part if not needed.
  148.      */
  149.  
  150.     if(!isset($_SESSION['Language']) || $_SESSION['Language'] != $_language) {
  151.         if(!empty($_language) && !empty($systpl)){
  152.             $location = '?language='.$_language.'&systpl='.$systpl;  
  153.            
  154.             // below some protection against duplicating the same parameters in the URL of redirect
  155.             parse_str(html_entity_decode($_SERVER['QUERY_STRING']), $query_string);    
  156.        
  157.             if(isset($query_string['language'])) $query_string['language'] = null;
  158.             if(isset($query_string['systpl']))   $query_string['systpl'] = null;
  159.        
  160.             $new_query_string = !empty($query_string) ? http_build_query($query_string) : '';
  161.      
  162.             if($new_query_string != '')
  163.                 $location .= '&'.$new_query_string;
  164.  
  165.             ob_clean();
  166.             header('location: '.$location);
  167.             die();
  168.         }
  169.     }
  170. }
  171.  
  172. /**
  173.  * Change template if
  174.  * - current template is NOT correct for this language
  175.  * - current template is NOT default, if no language was chosen
  176.  *
  177.  * Special thanks to Shalom Burla from Star Network & Promotion LTD :)
  178.  * NOTE: You can remove/disable this part if not needed.
  179.  */
  180.  
  181. $systpl = isset($language_to_template[$_language])
  182.             ? $language_to_template[$_language]
  183.             : $language_to_template['default'];
  184.  
  185.  
  186.  
  187. if(in_array($script_path, $allowed_scripts) && strpos($_SERVER['REQUEST_URI'], $customadminpath) === false && $current_template != $systpl && !empty($systpl)) {
  188.     if($systpl != $_GET['systpl']){
  189.         $location = '?systpl='.$systpl;
  190.        
  191.         parse_str(html_entity_decode($_SERVER['QUERY_STRING']), $query_string);
  192.  
  193.         if(isset($query_string['systpl']))   $query_string['systpl'] = null;
  194.        
  195.         $new_query_string = !empty($query_string) ? http_build_query($query_string) : '';
  196.        
  197.         if($new_query_string != '')
  198.             $location .= '&'.$new_query_string;
  199.        
  200.         ob_clean();
  201.         header('location: '.$location);
  202.         die();
  203.     }
  204. }
  205. /**
  206.  * Preventing from switching currency by user
  207.  * NOTE: You can remove/disable this part if not needed.
  208.  */
  209. if(isset($_SESSION['switched_currency']) && $_SESSION['switched_currency'] != $_SESSION['currency'] ) {
  210.     $_SESSION['currency'] = $_SESSION['switched_currency'];
  211. }
  212.  
  213. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement