Bomshteyn Consulting

Drupal switch domain per continent module

Drupal is known to have very good support for multi-site installations (with both shared content and / or just shared modules, themes and core codebase). Drupal is also known to have very good i18n (internalization) and l10n (localization). So in theory a website that needs to cater different content to different continents / regions plus translate it shouldn’t be a problem right? WRONG! 

Here is one of the problems encountered and of course our solution in form of “magic” custom module…

The problem: the client wanted to use geo-location to redirect users to the right domain – content specific sub-site we were using the great domain module for managing the multisite content.

Surprised as we were, we couldn’t find a module for this basic use case, (through we saw other geoip modules switching the language but not one that would switch the domain) so here is the module.

Nothing fancy in the module’s info file:


name = GEOIP to continent
description = GEOIP to continent
package = Other
core = 7.x

In our module we also utilize the install file:

<?php
/**
 * Implements hook_install().
 */
function geoip2continent_install() {
	_geoip2continent_default_insert();
}

/**
 * Implements hook_uninstall().
 */
function geoip2continent_uninstall() {
  // todo: add any uninstall code
}

/**
 * Implements hook_schema().
 */
function geoip2continent_schema() {

  $schema['geoip2continent'] = array(
    'description' => 'Stores country to continent mapping',
    'fields' => array(
      'country' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
        'description' => 'country code',
      ),
      'continent' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
        'description' => 'continent code',
      ),
    ),
    'indexes' => array(
      'name'    => array('country'),
      'surname' => array('continent'),
    ),
  );

  return $schema;
}

/**
 * Inserts default entry into geoip2continent table
 */ 
function _geoip2continent_default_insert() {
	$sql = "INSERT INTO {geoip2continent} (country, continent) VALUES
	('AD', 'EU'), 
	('AE', 'AS'), 
	('AF', 'AS'), 
	('AG', 'NA'), 
	('AI', 'NA'), 
	('AL', 'EU'), 
	('AM', 'AS'), 
	('AN', 'NA'), 
	('AO', 'AF'), 
	('AP', 'AS'), 
	('AQ', 'AN'), 
	('AR', 'SA'), 
	('AS', 'OC'), 
	('AT', 'EU'), 
	('AU', 'OC'), 
	('AW', 'NA'), 
	('AX', 'EU'), 
	('AZ', 'AS'), 
	('BA', 'EU'), 
	('BB', 'NA'), 
	('BD', 'AS'), 
	('BE', 'EU'), 
	('BF', 'AF'), 
	('BG', 'EU'), 
	('BH', 'AS'), 
	('BI', 'AF'), 
	('BJ', 'AF'), 
	('BL', 'NA'), 
	('BM', 'NA'), 
	('BN', 'AS'), 
	('BO', 'SA'), 
	('BR', 'SA'), 
	('BS', 'NA'), 
	('BT', 'AS'), 
	('BV', 'AN'), 
	('BW', 'AF'), 
	('BY', 'EU'), 
	('BZ', 'NA'), 
	('CA', 'NA'), 
	('CC', 'AS'), 
	('CD', 'AF'), 
	('CF', 'AF'), 
	('CG', 'AF'), 
	('CH', 'EU'), 
	('CI', 'AF'), 
	('CK', 'OC'), 
	('CL', 'SA'), 
	('CM', 'AF'), 
	('CN', 'AS'), 
	('CO', 'SA'), 
	('CR', 'NA'), 
	('CU', 'NA'), 
	('CV', 'AF'), 
	('CX', 'AS'), 
	('CY', 'AS'), 
	('CZ', 'EU'), 
	('DE', 'EU'), 
	('DJ', 'AF'), 
	('DK', 'EU'), 
	('DM', 'NA'), 
	('DO', 'NA'), 
	('DZ', 'AF'), 
	('EC', 'SA'), 
	('EE', 'EU'), 
	('EG', 'AF'), 
	('EH', 'AF'), 
	('ER', 'AF'), 
	('ES', 'EU'), 
	('ET', 'AF'), 
	('EU', 'EU'), 
	('FI', 'EU'), 
	('FJ', 'OC'), 
	('FK', 'SA'), 
	('FM', 'OC'), 
	('FO', 'EU'), 
	('FR', 'EU'), 
	('FX', 'EU'), 
	('GA', 'AF'), 
	('GB', 'EU'), 
	('GD', 'NA'), 
	('GE', 'AS'), 
	('GF', 'SA'), 
	('GG', 'EU'), 
	('GH', 'AF'), 
	('GI', 'EU'), 
	('GL', 'NA'), 
	('GM', 'AF'), 
	('GN', 'AF'), 
	('GP', 'NA'), 
	('GQ', 'AF'), 
	('GR', 'EU'), 
	('GS', 'AN'), 
	('GT', 'NA'), 
	('GU', 'OC'), 
	('GW', 'AF'), 
	('GY', 'SA'), 
	('HK', 'AS'), 
	('HM', 'AN'), 
	('HN', 'NA'), 
	('HR', 'EU'), 
	('HT', 'NA'), 
	('HU', 'EU'), 
	('ID', 'AS'), 
	('IE', 'EU'), 
	('IL', 'AS'), 
	('IM', 'EU'), 
	('IN', 'AS'), 
	('IO', 'AS'), 
	('IQ', 'AS'), 
	('IR', 'AS'), 
	('IS', 'EU'), 
	('IT', 'EU'), 
	('JE', 'EU'), 
	('JM', 'NA'), 
	('JO', 'AS'), 
	('JP', 'AS'), 
	('KE', 'AF'), 
	('KG', 'AS'), 
	('KH', 'AS'), 
	('KI', 'OC'), 
	('KM', 'AF'), 
	('KN', 'NA'), 
	('KP', 'AS'), 
	('KR', 'AS'), 
	('KW', 'AS'), 
	('KY', 'NA'), 
	('KZ', 'AS'), 
	('LA', 'AS'), 
	('LB', 'AS'), 
	('LC', 'NA'), 
	('LI', 'EU'), 
	('LK', 'AS'), 
	('LR', 'AF'), 
	('LS', 'AF'), 
	('LT', 'EU'), 
	('LU', 'EU'), 
	('LV', 'EU'), 
	('LY', 'AF'), 
	('MA', 'AF'), 
	('MC', 'EU'), 
	('MD', 'EU'), 
	('ME', 'EU'), 
	('MF', 'NA'), 
	('MG', 'AF'), 
	('MH', 'OC'), 
	('MK', 'EU'), 
	('ML', 'AF'), 
	('MM', 'AS'), 
	('MN', 'AS'), 
	('MO', 'AS'), 
	('MP', 'OC'), 
	('MQ', 'NA'), 
	('MR', 'AF'), 
	('MS', 'NA'), 
	('MT', 'EU'), 
	('MU', 'AF'), 
	('MV', 'AS'), 
	('MW', 'AF'), 
	('MX', 'NA'), 
	('MY', 'AS'), 
	('MZ', 'AF'), 
	('NA', 'AF'), 
	('NC', 'OC'), 
	('NE', 'AF'), 
	('NF', 'OC'), 
	('NG', 'AF'), 
	('NI', 'NA'), 
	('NL', 'EU'), 
	('NO', 'EU'), 
	('NP', 'AS'), 
	('NR', 'OC'), 
	('NU', 'OC'), 
	('NZ', 'OC'),  
	('OM', 'AS'), 
	('PA', 'NA'), 
	('PE', 'SA'), 
	('PF', 'OC'), 
	('PG', 'OC'), 
	('PH', 'AS'), 
	('PK', 'AS'), 
	('PL', 'EU'), 
	('PM', 'NA'), 
	('PN', 'OC'), 
	('PR', 'NA'), 
	('PS', 'AS'), 
	('PT', 'EU'), 
	('PW', 'OC'), 
	('PY', 'SA'), 
	('QA', 'AS'), 
	('RE', 'AF'), 
	('RO', 'EU'), 
	('RS', 'EU'), 
	('RU', 'EU'), 
	('RW', 'AF'), 
	('SA', 'AS'), 
	('SB', 'OC'), 
	('SC', 'AF'), 
	('SD', 'AF'), 
	('SE', 'EU'), 
	('SG', 'AS'), 
	('SH', 'AF'), 
	('SI', 'EU'), 
	('SJ', 'EU'), 
	('SK', 'EU'), 
	('SL', 'AF'), 
	('SM', 'EU'), 
	('SN', 'AF'), 
	('SO', 'AF'), 
	('SR', 'SA'), 
	('ST', 'AF'), 
	('SV', 'NA'), 
	('SY', 'AS'), 
	('SZ', 'AF'), 
	('TC', 'NA'), 
	('TD', 'AF'), 
	('TF', 'AN'), 
	('TG', 'AF'), 
	('TH', 'AS'), 
	('TJ', 'AS'), 
	('TK', 'OC'), 
	('TL', 'AS'), 
	('TM', 'AS'), 
	('TN', 'AF'), 
	('TO', 'OC'), 
	('TR', 'EU'), 
	('TT', 'NA'), 
	('TV', 'OC'), 
	('TW', 'AS'), 
	('TZ', 'AF'), 
	('UA', 'EU'), 
	('UG', 'AF'), 
	('UM', 'OC'), 
	('US', 'NA'), 
	('UY', 'SA'), 
	('UZ', 'AS'), 
	('VA', 'EU'), 
	('VC', 'NA'), 
	('VE', 'SA'), 
	('VG', 'NA'), 
	('VI', 'NA'), 
	('VN', 'AS'), 
	('VU', 'OC'), 
	('WF', 'OC'), 
	('WS', 'OC'), 
	('YE', 'AS'), 
	('YT', 'AF'), 
	('ZA', 'AF'), 
	('ZM', 'AF'), 
	('ZW', 'AF')";
	db_query($sql);
}

now to the main module file, in our case we are using a free geoip api from http://freegeoip.net/ but you could replace it with any other geoip api.

we are also not going strict to continents so we added our own routing, here are the subsites we use:

  • North America
  • Europe
  • Oceania / Australia
  • South America / Brazil
  • Hong Kong
  • China / Rest of Asia

so here is the module code:

<?php
	function geoip2continent_init(){
		// get current domain
		$domain = domain_get_domain();
		$current_domain_id = $domain['domain_id'];

		// check if is front page and if current domain is the main domain
		if ((drupal_is_front_page() == TRUE) && ($current_domain_id == 1)){

			$continent = getContinentCodeFromIP();

			switch($continent){
				case "NA": // user from North America
				$forward_to_domain = "2";
				break;
				case "EU": // user from Europe
				$forward_to_domain = "3";
				break;
				case "OC": // user from Oceania / Australia
				$forward_to_domain = "4";
				break;
				case "SA": // user from South America / Brazil
				$forward_to_domain = "5";
				break;
				case "HK": // user from Hong Kong
				$forward_to_domain = "6";
				break;
				case "AS": // user from China / Asia
				$forward_to_domain = "7";
				break;
			}
			// redirect
			domain_goto(domain_lookup($forward_to_domain));		
			} else {		
			// if not front page don't redirect
		}
	}

	function getContinentCodeFromIP(){
		$results = file_get_contents("http://freegeoip.net/csv/".$_SERVER['REMOTE_ADDR']);
		$results = explode(",",$results);
		$country = $results[1];
		$country_code = str_replace('"','',$country);

		if($country_code == "HK"){
			return $country_code;
		}

		$result = db_query('SELECT g.country, g.continent FROM {geoip2continent} g WHERE g.country = :country',
		array(':country' => $country_code))->fetchAll();

		return $result[0]->continent;
	}

As you see were are also redirecting only in case, that person comes to the home page.