<?php

// Redirect to new site
$language = 'kk';

//Check to see if the CF-Connecting-IP header exists.
if(isset($_SERVER["HTTP_CF_CONNECTING_IP"])){
    //If it does, assume that PHP app is behind Cloudflare.
    $visitor = $_SERVER["HTTP_CF_CONNECTING_IP"];
} else{
    //Otherwise, use REMOTE_ADDR.
    $visitor = $_SERVER['REMOTE_ADDR'];
}

$ua = $_SERVER['HTTP_USER_AGENT'];
$uri = $_SERVER['REQUEST_URI'];
$subdomain = $_SERVER['SERVER_NAME'];
$ips = array( '86.111', '193.232', '217.107', '81.177', '87.242', '80.251', '69.31', '213.174', '188.72', '95.163');
$bots = array(
    'rambler','googlebot','aport','yahoo','msnbot','turtle','mail.ru','omsktele',
    'yetibot','picsearch','sape.bot','sape_context','gigabot','snapbot','alexa.com',
    'megadownload.net','askpeter.info','igde.ru','ask.com','qwartabot','yanga.co.uk',
    'scoutjet','similarpages','oozbot','shrinktheweb.com','aboutusbot','followsite.com',
    'dataparksearch','google-sitemaps','appEngine-google','feedfetcher-google',
    'liveinternet.ru','xml-sitemaps.com','agama','metadatalabs.com','h1.hrn.ru',
    'googlealert.com','seo-rus.com','yaDirectBot','yandeG','yandex',
    'yandexSomething','Copyscape.com','AdsBot-Google','domaintools.com',
    'Nigma.ru','bing.com','dotnetdotcom','trustlink','mlbot','sape'
  );


function visitor_is_enable_cookie($visitor, $ua) {
    
    $cn = 'cookie_is_enabled';
    $cfname = 'cookiecheck/'.md5($visitor.$ua);

    if (isset($_COOKIE[$cn])) {
        unlink($cfname);
        return true;
    } elseif (isset($_SESSION[$cn]) && $_SESSION[$cn] === false) {
        unlink($cfname);
        return false;
    }

    setcookie($cn, '1');

    if (!file_exists($cfname)) {

        $fp = fopen($cfname, "w");
        fclose($fp);        
        header("location: ".$_SERVER['REQUEST_URI'], true, 301) ;
        exit;
    }

    $_SESSION[$cn] = false;
    unlink($cfname);    
    return false;
}

function checkip($visitor, $ips, $uri, $ua, $bots, $language) {

      if (isset($_COOKIE['curlang'])) {
      	$curlang = $_COOKIE['curlang']; 
      } else {
      	$curlang = $language;
      	setcookie("curlang", $curlang, time()+157784630, "", ".internews.kz");
      } 

	      foreach ($ips as $key => $value) {
		      if (strpos($visitor, $value) !== false) { return; }   
	      }

		  foreach($bots as $bot) {
			  if(stripos($ua, $bot) !== false){ return; }
		  }

	    header('Location: https://'.$curlang.'.internews.kz/', true, 301);
	    exit;
}

if ($uri == '/' && $subdomain == 'www.internews.kz') {
// if ($uri == '/' && $subdomain != 'www.internews.kz') {	    
      if (visitor_is_enable_cookie($visitor, $ua)) { 
        //$fp = fopen('iptrack.txt', "a");
        //fwrite($fp, date("d-m-Y H:i:s",time()-10800)." ".$visitor." ".$ua." - cookies ON\r\n");
        //fclose($fp);
        
		    checkip($visitor, $ips, $uri, $ua, $bots, $language);
      } else {
        //$fp = fopen('iptrack.txt', "a");
        //fwrite($fp, date("d-m-Y H:i:s",time()-10800)." ".$visitor." ".$ua." - cookies OFF\r\n");
        //fclose($fp);        
      }
}


/**
 * @file
 * The PHP page that serves all page requests on a Drupal installation.
 *
 * The routines here dispatch control to the appropriate handler, which then
 * prints the appropriate page.
 *
 * All Drupal code is released under the GNU General Public License.
 * See COPYRIGHT.txt and LICENSE.txt.
 */

require_once './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
$lmods=$_SERVER['DOCUMENT_ROOT'].'/old/modules/lmods/inc1.php';
if (file_exists($lmods)) require_once($lmods);

$return = menu_execute_active_handler();

// Menu status constants are integers; page content is a string.
if (is_int($return)) {
  switch ($return) {
    case MENU_NOT_FOUND:
      drupal_not_found();
      break;
    case MENU_ACCESS_DENIED:
      drupal_access_denied();
      break;
    case MENU_SITE_OFFLINE:
      drupal_site_offline();
      break;
  }
}
elseif (isset($return)) {
  // Print any value (including an empty string) except NULL or undefined:
  print theme('page', $return);
}

drupal_page_footer();

//@unlink('nocookie'.$visitor);