<?php
  header('Content-type: text/html; charset=utf-8');
    
  if (array_key_exists('info',$_GET) && $_GET['info']=='')  
     { phpinfo(); die(); } 
  
  if (!get_subdomain( 'eprasys.com', $sub_domain ))
  if (!get_subdomain( 'eprasys.com.ua', $sub_domain )) 
  if (!get_subdomain( 'cosmonova', $sub_domain )) // for local tests
       error("ДОМЕН $SERVER НЕ ПОДДЕРЖИВАЕТСЯ");

  //echo "SUBDOMAIN $sub_domain \n"; phpinfo(); die();
  
  $redirect = null;
  
  switch ($sub_domain)
  {
    case 'geos':           
            if (preg_match('%^service=%',$_SERVER['QUERY_STRING']))
            redirect( "/proxima/?$_SERVER[QUERY_STRING]" );
            break;
    case 'city':
            redir( 'city', 'gistest' );
            break;
    case 'public-parking': 
            redir( 'parking_pub', 'gistest' );
            break;
    case 'kiev-parking':   
            redir( 'parking' );
            break;
    default: error("НЕ НАЙДЕН ГЕО-СЕРВИС \nдля поддомена $sub_domain");
  }
    
  error("Неверный URL");
  
  
function get_subdomain( $base_domain, &$sub_domain )
{
  $sub_domain = null;
  $host = $_SERVER['HTTP_HOST'];
  $s = str_replace( '.', '\.', ".$base_domain" ); 
  if (!preg_match( "|^(.+?)$s|i", $host, $matches ))
       return false;
  $sub_domain = $matches[1];
  return true;
}

function redir( $service, $user = null )
{
  if ($_SERVER['QUERY_STRING']) return;
  if ($user)
       $url = "/proxima/?service=$service&profile=eprasys&silent=true&user=$user";  
  else $url = "/proxima/?service=$service&profile=eprasys";
  redirect( $url );
}
function redirect( $url = null )
{
  header("Location: $url");
  die();
}

function error($msg)
{
  die("\n<pre>\n$msg");
}

