Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * GEOLOCATION HOOK FOR WHMCS
- * This hook will automatically setup currency and language
- * for nonlogged users in your clientarea.
- *
- *
- * For more information, read the whole article here:
- * http://blog.whmcs.com
- *
- *
- * @author ModulesGarden
- * @link http://www.modulesgarden.com
- */
- $country_to_currency = array(
- 'default' => 'ARS',
- 'US' => 'USD',
- 'UK' => 'EUR',
- 'PL' => 'EUR',
- 'DE' => 'EUR',
- 'NL' => 'EUR',
- 'FR' => 'EUR',
- 'IT' => 'EUR',
- 'CH' => 'EUR',
- // NOTE: You can add more below
- );
- $country_to_language = array(
- 'default' => 'spanish',
- 'US' => 'english',
- 'UK' => 'english',
- 'DE' => 'english',
- 'FR' => 'english',
- 'IT' => 'english',
- // NOTE: You can add more below
- );
- $language_to_template = array(
- 'english' => 'nhp2012theme',
- 'german' => 'nhp2012theme',
- 'french' => 'nhp2012theme',
- 'italian' => 'nhp2012theme',
- // NOTE: You can add more below
- );
- $allowed_scripts = array(
- 'index.php',
- 'clientarea.php',
- 'cart.php',
- 'knowledgebase.php',
- 'announcements.php',
- 'serverstatus.php',
- 'affiliates.php',
- 'contact.php'
- );
- /**
- * FUNCTION geolocation_getCurrencyId
- * This function will return currency id for specific code.
- *
- * @param string
- * @return int
- */
- function geolocation_getCurrencyId($currency) {
- $q = mysql_query('SELECT id FROM tblcurrencies WHERE code = "'.mysql_escape_string($currency).'"'); // escape string just in case
- $r = mysql_fetch_assoc($q);
- mysql_free_result($q);
- if(isset($r['id'])) {
- return $r['id'];
- }
- }
- $script_path = substr($_SERVER['SCRIPT_NAME'], strrpos($_SERVER['SCRIPT_NAME'], DIRECTORY_SEPARATOR)+1);
- $current_template = $CONFIG['Template'];
- $language = $_SESSION['Language'];
- /**
- * Main Geolocation Script
- *
- * NOT run script
- * - if we are in adminarea
- * - if already setup for this session
- * - if user is logged in
- * - NEW: allowing to run the hook only for specific scripts (defined above)
- */
- if(in_array($script_path, $allowed_scripts) && strpos($_SERVER['REQUEST_URI'], $customadminpath) === false && !isset($_SESSION['geolocation_setup']) && $_SESSION['uid'] == false) {
- $_SESSION['geolocation_setup'] = true; // prevent from redirecting back again in this session
- /**
- * Get Country using external service - Hostip.info example
- * NOTE: You can handle this part with any other method.
- *
- $current_country = '';
- $ret = file_get_contents("http://api.hostip.info/get_json.php?ip=".$_SERVER['REMOTE_ADDR']); // this can be called also via cURL
- $parsed_json = @json_decode($ret,true);
- if(isset($parsed_json['country_code'])) {
- $current_country = $parsed_json['country_code'];
- }
- *
- */
- /**
- * NEW: Get Country using external service - Maxmind GeoLite Example
- * http://dev.maxmind.com/geoip/geolite
- * NOTE: You can handle this part with any other method.
- */
- include "geoip.inc";
- $gi = geoip_open(dirname(__FILE__).DIRECTORY_SEPARATOR."GeoIP.dat", GEOIP_STANDARD);
- $current_country = geoip_country_code_by_addr($gi, $_SERVER['REMOTE_ADDR']);
- geoip_close($gi);
- /**
- * Get language, currency and currency ID in order to setup the right values in the system
- */
- $currency = $current_country != '' && isset($country_to_currency[$current_country])
- ? $country_to_currency[$current_country]
- : $country_to_currency['default'];
- $currency_id = geolocation_getCurrencyId($currency);
- $language = $current_country != '' && isset($country_to_language[$current_country])
- ? $country_to_language[$current_country]
- : $country_to_language['default'];
- /**
- * Setup Currency Session
- * NOTE: You can remove/disable this part if not needed.
- */
- if($currency_id && (!isset($_SESSION['currency']) || $_SESSION['currency'] != $currency))
- {
- $_SESSION['currency'] = $_SESSION['switched_currency'] = $currency_id;
- }
- /**
- * Setup URL Redirection to Switch Language
- * NOTE: You can remove/disable this part if not needed.
- */
- if(!isset($_SESSION['Language']) || $_SESSION['Language'] != $language) {
- $location = '?language='.$language;
- if($_SERVER['QUERY_STRING'] != '')
- $location .= '&'.$_SERVER['QUERY_STRING'];
- ob_clean();
- header('location: '.$location);
- die();
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement