<?php
/**
 * @var $data array
 * @var $systemLocalScripts array
 * @var $return boolean
 */
$query           = $_GET['q'] ?? null;
$scriptingBefore = microtime(true);
header('scriptingBefore: '.$scriptingBefore);
include_once __DIR__.'/defines.php';

if (HTTPS and !IS_HTTPS) {
	header('Location: https://'.HOST.PUBLIC_ROOT.REQUEST, true, 301);
	die;
} elseif (!HTTPS and IS_HTTPS) {
	header('Location: http://'.HOST.PUBLIC_ROOT.REQUEST, true, 301);
	die;
} elseif (WWW and !IS_WWW) {
	header('Location: '.PROTOCOL.'://www.'.HOST.PUBLIC_ROOT.REQUEST, true, 301);
	die;
} elseif (!WWW and IS_WWW) {
	header('Location: '.PROTOCOL.'://'.strtr(HOST, ['www.' => null]).PUBLIC_ROOT.REQUEST, true, 301);
	die;
}

$handle = fopen(__DIR__.'/lib/incs/rewrite.csv', 'r');
while ($item = fgetcsv($handle, 0, chr(9))) {
	if (REQUEST == $item[1]) {
		if ($item[0] == 1) {
			header('Location: '.(filter_var($item[2], FILTER_VALIDATE_URL) ? null : BASE_URL).$item[2], true, IS_DEVELOPER ? 307 : 301);
			
			die;
		}
	}
}

include_once PANEL_DIR.'_connect.php';
define('F_LANGUAGES_LIST', require BASE_DIR.'lib/incs/lng/_languages.php');
$langData = [];
foreach (F_LANGUAGES_LIST as $langName => $langVal) {
	if (!is_file(BASE_DIR.'lib/incs/lng/'.$langName.'.php')) {
		if (IS_DEVELOPER) {
			ob_clean();
			die('Olmaması Gerekir!<br>'.__FILE__.':'.__LINE__);
		}
		
		continue;
	}
	
	$langData[$langName] = require BASE_DIR.'lib/incs/lng/'.$langName.'.php';
}
define('F_LANGUAGES_DATA', $langData);

$siteCache = false;
if (SITE_CACHE and isset($_GET['q']) and !isset($_COOKIE['sistemUser'])) {
	$cacheFile = UPLOADS_DIR.'.sc/'.PROTOCOL.'/';
	if (preg_match('#/$#', $_GET['q']) or empty($_GET['q'])) {
		$cacheFile .= $_GET['q'].'index.html';
	} else {
		$cacheFile .= $_GET['q'];
	}
	
	$split = explode('?', REQUEST);
	if (count($split) > 1) {
		$cacheFile .= '#'.preg_replace('/[^A-Za-z0-9]/', '', urldecode($split[1]));
	}
	
	if (!IS_DEVELOPER AND !isset($_COOKIE['sistemUser'])) {
		if (is_file($cacheFile) and filemtime($cacheFile) < (time() + intval(SITE_CACHE_TIME))) {
			readfile($cacheFile);
			die;
		}
	}
	
	$siteCache = true;
}

$disableTheme = isset($_GET['disableTheme']);

include PANEL_DIR.'config.php';
if (IS_AMP AND !AMP_ACTIVATE) {
	t('AMP NOT ACTIVATED');
	page_not_found();
}

if (isset($_GET['q'])) {
	$split = explode('/', $_GET['q']);
	$lng   = strtr($split[0], ['-' => '_']);
	if ($lng == MAIN_LANGUAGE) {
		$q = preg_replace('#^[^/]+/#', '', $_GET['q']);
		unset($_GET['q']);
		moved(BASE_URL.$q.(count($_GET) ? '?'.http_build_query($_GET) : null));
	}
	if (is_file(BASE_DIR.'lib/incs/lng/'.$lng.'.php')) {
		define('LNG', $lng);
		$nqs = preg_replace('#^[^/]+/#', '', $_GET['q']);
		$ng  = $_GET;
		if (isset($ng['q']))
			unset($ng['q']);
		if (IS_URL_DIRECT_PHP) {
			// en/route.php?page=....
			define('QUERY', 'route.php?'.urldecode(http_build_query($ng)));
		} else {
			define('QUERY', $nqs);
		}
		unset($nqs);
		unset($ng);
		define('DESKTOP_BASE_LNG_URL', DESKTOP_BASE_URL.$split[0].'/');
		define('BASE_LNG_URL', BASE_URL.$split[0].'/');
	} else {
		define('LNG', MAIN_LANGUAGE);
		define('QUERY', $_GET['q']);
		define('DESKTOP_BASE_LNG_URL', DESKTOP_BASE_URL);
		define('BASE_LNG_URL', BASE_URL);
	}
	if (isset($_GET['q'])) {
		unset($_GET['q']);
	}
	define('QUERY_WITH_PARAMS', REQUEST);
	define('IS_REWRITE', true);
	include_once __DIR__.'/patterns.php';
} else {
	// route.php?page=....
	define('LNG', isset($_GET['lng']) ? strtr($_GET['lng'], ['-' => '_']) : MAIN_LANGUAGE);
	define('DESKTOP_BASE_LNG_URL', DESKTOP_BASE_URL.(LNG == MAIN_LANGUAGE ? null : strtr(LNG, ['_' => '-']).'/'));
	define('BASE_LNG_URL', BASE_URL.(LNG == MAIN_LANGUAGE ? null : strtr(LNG, ['_' => '-']).'/'));
	define('IS_REWRITE', false);
	$nq = $_GET;
	if (isset($nq['q']))
		unset($nq['q']);
	define('QUERY', 'route.php?'.urldecode(http_build_query($nq)));
	unset($nq);
}

define('PAGE', $_GET['page']);
define('PAGE_NAME', explode('/', PAGE)[0]);
$file = BASE_DIR.'themes/'.(!$disableTheme ? (IS_AMP ? '_amp' : THEME).'/' : null).PAGE.'.php';
if (!is_file($file)) {
	t($file);
	page_not_found();
}
include_once BASE_DIR.'lib/incs/functions.php';
include_once THEME_LIB_DIR.'incs/functions.php';
include PANEL_DIR.'lib/incs/class/class.settings.php';
include PANEL_DIR.'lib/incs/class/class.user.php';
include_once $file;

afterRouteFileInclude();