Location: PHPKode > scripts > Geo Redirect > geographical-redirect/geo-redirect.php
<?php
/*
Plugin Name:Geo Redirect
Plugin URI: http://wordpress.org/extend/plugins/geographical-redirect/
Description: A plugin that provides visitor redirect according to their geographical location
Author: Ladrower
Author URI: http://profiles.wordpress.org/users/Ladrower/
Author e-mail: hide@address.com
Version: 2.0
License: Free
*/
require_once "geoip/geoipcity.inc";
require_once "geo-redirect-admin.php";

add_action( 'check_client_location', 'geo_redirect_client_location' );

class Geo_Redirect{
	public 	$ip;
	private $gi;
	public 	$country_code;
	public 	$country_id;
	public 	$geo_redirect_data;
	public 	$site_lang;
	public 	$lang_slug;
	public 	$site_domain;
	private $no_redirect;
	private $referer;
	
	public function __construct()
	{
		$this->ip = $_SERVER['REMOTE_ADDR'];
		$this->gi = geoip_open( dirname(__FILE__) . "/geoip/ipdatabase/GeoIP.dat/GeoIP.dat", GEOIP_STANDARD);
		$this->site_domain = $_SERVER['SERVER_NAME'];
		$this->no_redirect = (isset($_GET['no_redirect']) || (isset($_POST['pwd']) && isset($_POST['log']))) ? true : false;
		$this->referer = $_SERVER['HTTP_REFERER'];
		$this->lang_slug = 'lang';
		$this->getGeoRedirectData();
		$this->getSiteLang();
	}
	
	public function getCountryCode()
	{
		$this->country_code = geoip_country_code_by_addr($this->gi,$this->ip);  
		return $this->country_code;
	}

	public function getCountryId()
	{
		$this->country_id = geoip_country_id_by_addr($this->gi,$this->ip);  
		return $this->country_id;
	}
	
	public function getGeoRedirectData()
	{
		if (function_exists('get_option'))
			$this->geo_redirect_data = get_option('geo_redirect_data');
		return $this->geo_redirect_data;
	}
	
	public function getSiteLang()
	{
		if (!is_array($this->geo_redirect_data))
			return '';
			
		$this->lang_slug = ($this->geo_redirect_data['lang_slug'] != '') ? $this->geo_redirect_data['lang_slug'] : 'lang';
		$this->site_lang = (isset($_GET[$this->lang_slug])) ? ( (string) htmlentities(strtolower($_GET[$this->lang_slug])) ) : '';	
		return $this->site_lang;
	}
	
	public function checkIfRedirectNeeded()
	{
		$this->getCountryId();
		
		if (empty($this->country_id))
			return false;
		
		if (!is_array($this->geo_redirect_data))
			return false;
		
		if (is_array($this->geo_redirect_data['redirect'])) {
			$country_found = false;
			foreach($this->geo_redirect_data['redirect'] as $data) {
				if ($data['country_id'] == $this->country_id) {
					$country_found = true;
					$lang_code = $data['lang_code'];
					$domain = $data['domain'];
					$url = $data['url'];
					if (!empty($url)) {
						$current_url = 'http://' . $this->site_domain . $_SERVER['REQUEST_URI'];
						if ($current_url != $url)
							$this->redirectByUrl($url);
						
					} elseif (!empty($domain)) {
						if ($this->site_domain != $domain)
							$this->redirectByDomain($domain);
							
					} elseif(!empty($lang_code)) {
						if ($this->site_lang != $lang_code)	
							$this->redirectByLang($lang_code);
					}
					break;
				}
				if ($data['country_id'] == -1)
					$default_data = $data;
		
			}
			if (!$country_found && !empty($default_data)) {
				$lang_code = $default_data['lang_code'];
				$domain = $default_data['domain'];
				$url = $default_data['url'];
				if (!empty($url)) {
					if ('http://' . $this->site_domain . $_SERVER['REQUEST_URI'] != $url)
						$this->redirectByUrl($url);
						
				} elseif (!empty($domain)) {
					if ($this->site_domain != $domain)
						$this->redirectByDomain($domain);
						
				} elseif(!empty($lang_code)) {
					if ($this->site_lang != $lang_code)	
						$this->redirectByLang($lang_code);
				}	
			}
			
		}
	
	}
	
	private function redirectByUrl($url)
	{
		if ($url != ''){
			$this->redirectTo($url);
		}
	}

	private function redirectByDomain($domain)
	{
		if ($domain != ''){
			$to = 'http://' . $domain . $_SERVER['REQUEST_URI'];
			$this->redirectTo($to);
		}
	}
	
	private function redirectByLang($lang_code)
	{
		if ($lang_code != ''){
			$lang_url = (strpos($_SERVER['REQUEST_URI'],'?') === false) ? '?' : '&';
			$to = get_bloginfo('url') . $_SERVER['REQUEST_URI'] . $lang_url . $this->lang_slug . '=' . $lang_code;
			$this->redirectTo($to);
		}
	}
	
	private function redirectTo($to)
	{
		if ($this->no_redirect)
			return false;
			
		if ($this->checkReferer())
			return false;
			
		header("Location: " . $to);
		exit;
	}
	
	private function checkReferer()
	{
		if (is_array($this->geo_redirect_data))
			$only_outsite = $this->geo_redirect_data['only_outsite'];
			
		if ($only_outsite == 1) {
			if (empty($this->referer))
				return true;
				
			$outsite = parse_url($this->referer);
			$domain = $outsite['host'];
			if ($this->site_domain != $domain)
				return false;
				
			return true;
		}
		return false;
	}
	
	public function __destruct()
	{
		geoip_close($this->gi);
	}	
	
}

function geo_redirect_client_location(){
	$geo = new Geo_Redirect();
	$geo->checkIfRedirectNeeded();
}

if (!is_admin())
	do_action('check_client_location'); 

?>
Return current item: Geo Redirect