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' => 'USD',
- 'US' => 'USD',
- 'id' => 'IDR',
- // NOTE: You can add more below
- );
- $country_to_language = array(
- 'default' => 'english',
- 'US' => 'english',
- 'id' => 'indonesia'
- // NOTE: You can add more below
- );
- $language_to_template = array(
- 'english' => 'default',
- 'indonesia' => 'portal',
- 'default' => 'default'
- // NOTE: You can add more below
- );
- $allowed_scripts = array(
- 'p1.php',
- 'index.php',
- 'clientarea.php',
- 'cart.php',
- 'knowledgebase.php',
- 'announcements.php',
- 'serverstatus.php',
- 'affiliates.php',
- 'contact.php'
- // NOTE: You can add more below
- );
- /**
- * 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 = $_SESSION['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 && $_SESSION['geolocation_setup'] !== true && $_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;
- }
- /**
- * Setting up template for redirect
- * NOTE: You can remove/disable this part if not needed.
- */
- $systpl = isset($language_to_template[$_language])
- ? $language_to_template[$_language]
- : $language_to_template['default'];
- /**
- * Setup URL Redirection to Switch Language
- * NOTE: You can remove/disable this part if not needed.
- */
- if(!isset($_SESSION['Language']) || $_SESSION['Language'] != $_language) {
- if(!empty($_language) && !empty($systpl)){
- $location = '?language='.$_language.'&systpl='.$systpl;
- // below some protection against duplicating the same parameters in the URL of redirect
- parse_str(html_entity_decode($_SERVER['QUERY_STRING']), $query_string);
- if(isset($query_string['language'])) $query_string['language'] = null;
- if(isset($query_string['systpl'])) $query_string['systpl'] = null;
- $new_query_string = !empty($query_string) ? http_build_query($query_string) : '';
- if($new_query_string != '')
- $location .= '&'.$new_query_string;
- ob_clean();
- header('location: '.$location);
- die();
- }
- }
- }
- /**
- * Change template if
- * - current template is NOT correct for this language
- * - current template is NOT default, if no language was chosen
- *
- * Special thanks to Shalom Burla from Star Network & Promotion LTD :)
- * NOTE: You can remove/disable this part if not needed.
- */
- $systpl = isset($language_to_template[$_language])
- ? $language_to_template[$_language]
- : $language_to_template['default'];
- if(in_array($script_path, $allowed_scripts) && strpos($_SERVER['REQUEST_URI'], $customadminpath) === false && $current_template != $systpl && !empty($systpl)) {
- if($systpl != $_GET['systpl']){
- $location = '?systpl='.$systpl;
- parse_str(html_entity_decode($_SERVER['QUERY_STRING']), $query_string);
- if(isset($query_string['systpl'])) $query_string['systpl'] = null;
- $new_query_string = !empty($query_string) ? http_build_query($query_string) : '';
- if($new_query_string != '')
- $location .= '&'.$new_query_string;
- ob_clean();
- header('location: '.$location);
- die();
- }
- }
- /**
- * Preventing from switching currency by user
- * NOTE: You can remove/disable this part if not needed.
- */
- if(isset($_SESSION['switched_currency']) && $_SESSION['switched_currency'] != $_SESSION['currency'] ) {
- $_SESSION['currency'] = $_SESSION['switched_currency'];
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement