<?php

$ip = $_SERVER['REMOTE_ADDR'];


if($_SERVER['REQUEST_URI']=='/'){

include_once "geoiploc.php";

// create a variable for the country code
$var_country_code = getCountryFromIP($ip);
$lang="int";
if(isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])){
$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
}

/*
$fp = fopen('wp-content/log_mio.txt', 'a');
	fwrite($fp, $var_country_code.' '.$ip);
fwrite($fp,  http_build_query($_GET));
	fclose($fp);*/


header("HTTP/1.1 301 Moved Permanently"); 
$var_query_s = http_build_query($_GET);

 // redirect based on country code
switch ($var_country_code) {
	case  "ES": 
        if (!empty($var_query_s)){
	header('Location: https://www.cupapizarras.com/es'."?".$var_query_s);
	}
		else
	{
		header('Location: https://www.cupapizarras.com/es/');
	}
	break;
	case  "FR": 
	header('Location: https://www.cupapizarras.com/fr/');
	break;
	case  "GB": 
	 if (!empty($var_query_s)){
	header('Location: https://www.cupapizarras.com/uk'."?".$var_query_s);
	}
	else
	{
	header('Location: https://www.cupapizarras.com/uk/');
	}
	break;
	case  "BE": 
	header('Location: https://www.cupapizarras.com/be-fr/');
	break;
	case  "LU": 
	header('Location: https://www.cupapizarras.com/be-fr/');
	break;
	case  "NL": 
	header('Location: https://www.cupapizarras.com/be-fl/');
	break;
	case  "US":
 	if (!empty($var_query_s)){ 
	header('Location: https://www.cupapizarras.com/usa'."?".$var_query_s);
	}
	else
	{
	header('Location: https://www.cupapizarras.com/usa/');
	}
	break;
	case  "RU": 
	header('Location: https://www.cupapizarras.com/ru/');
	break;
	case  "PL": 
	header('Location: https://www.cupapizarras.com/pl/');
	break;

	default:
		//redireccionamos por idioma
		switch ($lang){
			case "fr":
				header('Location: https://www.cupapizarras.com/fr/');
				break;
			case "es":
				header('Location: https://www.cupapizarras.com/es/');
				break;     
			case "nl":
				header('Location: https://www.cupapizarras.com/be-fl/');
				break;  				
			default:
				if (!empty($var_query_s)){ 
				header('Location: https://www.cupapizarras.com/int'."?".$var_query_s);
				}
				else
				{
				header('Location: https://www.cupapizarras.com/int/');
				}
				break;
		}
		break;
}

}
else{ 
$china = false;
//miramos si es readyslate y china
if ((strpos($_SERVER['REQUEST_URI'], "/int/readyslate/") === 0) || (strpos($_SERVER['REQUEST_URI'], "/usa/readyslate/") === 0)) {
include_once "geoiploc.php";
	$var_country_code = getCountryFromIP($ip);

/*
require_once('geoplugin.class.php');
$geoplugin = new geoPlugin();
$geoplugin->locate();
$var_country_code = $geoplugin->countryCode;*/

/*
$fp = fopen('wp-content/log_mio.txt', 'a');
	fwrite($fp, $var_country_code.' '.$ip);
fwrite($fp,  http_build_query($_GET). PHP_EOL );
	fclose($fp);*/

	if (($var_country_code === "CN") || ($var_country_code === "HK"))
	{
	$china = true;
	header("HTTP/1.1 301 Moved Permanently");
	header('Location: https://www.cupapizarras.com/int/');
	}
}

if($china===false)
{

/**
 * Front to the WordPress application. This file doesn't do anything, but loads
 * wp-blog-header.php which does and tells WordPress to load the theme.
 *
 * @package WordPress
 */

/**
 * Tells WordPress to load the WordPress theme and output it.
 *
 * @var bool
 */
define('WP_USE_THEMES', true);

/** Loads the WordPress Environment and Template */
require __DIR__ . '/wp-blog-header.php';
}
}
