<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);

use Symfony\Component\HttpFoundation\Request;

header("X-UA-Compatible: IE=Edge");
require_once 'includes/class.system.php';
define('IS_CMS', false);

// no inaczej się nie da bo jest bajzel :/
//^ szkoda gadać....
// to jest nowotwór :/

if(strcmp($_SERVER['REMOTE_ADDR'],'213.241.28.18')===0)
{
	// if($_SERVER['REQUEST_URI'] !== '/')
	// {
		// echo $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
		// die;
	// }
}

if (strpos($_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], 'grandsal.pl/index.php') !== false) {
	header("HTTP/1.1 301 Moved Permanently");
	header('Location: https://www.grandsal.pl');
	die();
}
if (strpos($_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], 'grandsal.pl/index.html') !== false) {
	header('Location: https://www.grandsal.pl');
	die();
}
if (strpos($_SERVER['HTTP_HOST'], 'www.') === false) {
	// header("HTTP/1.1 301 Moved Permanently");
	// header('Location: https://www.'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
	// die();
}

function http_authenticate($user, $pass) {
	if (isset($_SERVER['PHP_AUTH_USER']) && (@$_SERVER['PHP_AUTH_USER']==$user) && (@$_SERVER['PHP_AUTH_PW']==$pass)) {
		return true;
	}
	else {
		header('WWW-Authenticate: Basic realm="Podaj haslo"');
		header('HTTP/1.0 401 Unauthorized');
		echo 'You must log in to watch this';
		exit;
	}
}
//http_authenticate('golden','Bangla1');

/*if (strpos($_SERVER['REQUEST_URI'], '/pl') !== false) {
	$explode = explode('/', $_SERVER['REQUEST_URI']);
	
	if (in_array($explode[1], array('pl','de','ru','en'))) {
		unset($explode[1]);
		$url = join('/', $explode);
	}
	
	$url = (strpos($_SERVER['HTTP_HOST'], 'www.') !== false) ? $_SERVER['HTTP_HOST'] . $url : 'www.' . $_SERVER['HTTP_HOST'] . $url;
	
	header("HTTP/1.1 301 Moved Permanently");
	header('Location: https://'.$url);
	die();
}	*/

try {

	$system = system::getInstance();
    $request = Request::createFromGlobals();
    $system->set('request', $request);

	$system->start();

	include_once('redirects.php');
	
    if(strpos($_SERVER['HTTP_HOST'],'gsub.pl') !== false){
        $exploded_domain = explode('.',$_SERVER['HTTP_HOST']);
        if(count($exploded_domain) == 3){
            header("Location: http://pl.grandsal.gsub.pl");
        }
    }

	if (!isset($_SERVER['HTTPS'])) {
		header("HTTP/1.1 301 Moved Permanently");
		header("Location: https://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
		die();
	}
	
	
	$pages = $system->loadModule('pages');
	$menu = $system->loadModule('menu');

    $lcas = $system->loadModule('language_change_and_search');
    $mb = $system->loadModule('menu_box');
    $ms = $system->loadModule('menu_social');
    $cm = $system->loadModule('cookie_msg');

    $pages->main();
    $menu->main();
    $lcas->main();
    $mb->main();
    $ms->main();
    $cm->main();

	
	$system->show();
	$system->stop();
}
catch (Exception $e) {
	if (getConfig('debug')){
		throw $e;
	} else {
        hLogger::getInstance()->getLogger('db')->addError("Blad bazy danych => ".$e->getMessage());

		$tpl = new Template(TPL_PATH . 'layout/error.php');
		echo $tpl->fetch();
	}
}
