<?php


/*  written  by:  oliver  weber  oliver.weber@intellidi.com  */
/*  Version  1.2  -  19.7.2018                                                          */


/*  the  function  gets  the  localization  data  */
function ip_info($ip = NULL, $purpose = "location", $deep_detect = TRUE) {
    $output = NULL;
    if (filter_var($ip, FILTER_VALIDATE_IP) === FALSE) {
        $ip = $_SERVER["REMOTE_ADDR"];
        if ($deep_detect) {
            if (filter_var(@$_SERVER['HTTP_X_FORWARDED_FOR'], FILTER_VALIDATE_IP))
                $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
            if (filter_var(@$_SERVER['HTTP_CLIENT_IP'], FILTER_VALIDATE_IP))
                $ip = $_SERVER['HTTP_CLIENT_IP'];
        }
    }
    $purpose    = str_replace(array("name", "\n", "\t", " ", "-", "_"), NULL, strtolower(trim($purpose)));
    $support    = array("country", "countrycode", "state", "region", "city", "location", "address");
    $continents = array(
        "AF" => "Africa",
        "AN" => "Antarctica",
        "AS" => "Asia",
        "EU" => "Europe",
        "OC" => "Australia (Oceania)",
        "NA" => "North America",
        "SA" => "South America"
    );
    if (filter_var($ip, FILTER_VALIDATE_IP) && in_array($purpose, $support)) {
        $ipdat = @json_decode(file_get_contents("http://www.geoplugin.net/json.gp?ip=" . $ip));
        if (@strlen(trim($ipdat->geoplugin_countryCode)) == 2) {
            switch ($purpose) {
                case "location":
                    $output = array(
                        "city"           => @$ipdat->geoplugin_city,
                        "state"          => @$ipdat->geoplugin_regionName,
                        "country"        => @$ipdat->geoplugin_countryName,
                        "country_code"   => @$ipdat->geoplugin_countryCode,
                        "continent"      => @$continents[strtoupper($ipdat->geoplugin_continentCode)],
                        "continent_code" => @$ipdat->geoplugin_continentCode
                    );
                    break;
                case "address":
                    $address = array($ipdat->geoplugin_countryName);
                    if (@strlen($ipdat->geoplugin_regionName) >= 1)
                        $address[] = $ipdat->geoplugin_regionName;
                    if (@strlen($ipdat->geoplugin_city) >= 1)
                        $address[] = $ipdat->geoplugin_city;
                    $output = implode(", ", array_reverse($address));
                    break;
                case "city":
                    $output = @$ipdat->geoplugin_city;
                    break;
                case "state":
                    $output = @$ipdat->geoplugin_regionName;
                    break;
                case "region":
                    $output = @$ipdat->geoplugin_regionName;
                    break;
                case "country":
                    $output = @$ipdat->geoplugin_countryName;
                    break;
                case "countrycode":
                    $output = @$ipdat->geoplugin_countryCode;
                    break;
            }
        }
    }
    return $output;
}

/* here the code starts */

/* location = $l_sub . $l_main . $l_host . $l_path */
/*default*/

$iPod    = stripos($_SERVER['HTTP_USER_AGENT'],"iPod");
$iPhone  = stripos($_SERVER['HTTP_USER_AGENT'],"iPhone");
$iPad    = stripos($_SERVER['HTTP_USER_AGENT'],"iPad");
$Android = stripos($_SERVER['HTTP_USER_AGENT'],"Android");


$SSLprotocol = "https://";
$protocol = "http://";
$web_main = "picooc";
$loc_array = ip_info();
$check_region = 0;
$use_wap = 0;
$get_browser_language = 0;

$location ="";

/* pre-define chinese location - based on mobile */
if( $iPod || $iPhone || $Android ) {
    $location_cn = $SSLprotocol."www.".$web_main.".com"."/wap";
    $use_wap = 1;
} else {
    $location_cn = $SSLprotocol."www.".$web_main.".com"."/index.html?lag=zh";
}


/* predefine all location aDRESSES */

$location_us = $SSLprotocol."www.".$web_main.".com"."/en/index.html?lag=en";
$location_de = $SSLprotocol."eu.".$web_main.".com"."/de";
$location_eu = $SSLprotocol.$location = "eu.".$web_main.".com"."/en";
/* 韩国代理商提前申请的域名，暂时不能用了，暂时修改域名 2019-05-14 peiian */
// $location_kr = $protocol."www.".$web_main.".co.kr"; /*no https*/
$location_kr = $protocol."www.".$web_main.".com"."/kr"; /*no https*/
$location_ph = $protocol."www.".$web_main."ph"."/ph/index.html?lag=en";
$location_ja = $protocol."www.".$web_main.".com"."/ja";
$location_ru = $SSLprotocol."www.".$web_main.".ru";


switch($loc_array['country_code'])
{
        /* switch to Chinese webcontent / decide between mobile (wap) and non mobile */
    case ("CN"):
        $location = $location_cn;
        echo($location);
        break;

        /*swtich to german webcontent for: Austria, Switzerland, Germany */
    case ("DE"):
    case ("AU"):
    case ("CH"):
        $location = $location_de;
        break;
    case ("JP"):
        $location = $location_ja;
        break;

        /*switch to US for US and Canada */
    case ("US"):
    case ("CA"):
        $location = $location_us;
        break;

        /* switch to Philipines */
    case ("PH"):
        $location = $location_ph;
        break;

        /* switch to Korea */
    case ("KR"):
        $location = $location_kr;
        break;

    case ('RU'):
        $location = $location_ru;
        break;

    default:
        /* no direct supported country detected */
        /* check now for region */
        $check_region = 1;
        break;

}

/* check the browser language settings and decide based on those */




/* check now the regions, based on continent codes
This is ONLY done, if a direct match above was not possible

EU Europe
NA North America
SA South America
AN Antarctica
OC Oceania

AF Africa
AS Asia
*/

if ($check_region == 1)
{
    switch($loc_array['continent_code'])
    {
        case ("EU"):
            $location = $location_eu;
            break;

        case ("NA"):
        case ("SA"):
        case ("AN"):
        case ("OC"):
            $location = $location_us;
            break;

        /* no region detected -> get browser language */
        case (""):
            $get_browser_language = 1;
            break;

        default:
            /* no direct supported region & country detected */
            /* go to chinese site */
            $location = $location_cn;
            break;
    }
}

if ($get_browser_language == 1)
{

    $lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
    switch ($lang){
        case "zh":
            $location = $location_cn;
            break;

        case "de":
            $location = $location_de;
            break;

        case "it":
        case "sp":
        case "fr":
        case "en":
            $location = $location_eu;
            break;

        case "ko":
            $location = $location_kr;
            break;

        default:
            //echo "PAGE ENGLISH - Setting Default";
            $location = $location_us;
        break;
    }
}


/*THIS BELOW COMMENTED CODE WAS FOR TEST ONLY */
/*print_r(array_values($loc_array));
echo "Country Code: ".$loc_array['country_code']."; ";
echo "Continent Code: ".$loc_array['continent_code']."; ";
echo "Location Link: ".$location."; ";
echo "Wap: ".$use_wap."; ";*/



/* check if stay here, or leave this site and file */
/* 为了SEO最重要离开这个网页，如果其它的网页要用          */
/* by Oliver :-)                                   */

header("Location: $location");
exit();


/* In the Chinese HTML Page. The only thing I added / changed was:
-Add the language code to the html tag
-Add the EU flag and link to eu english page */
?>
