<?php
header("Content-type:text/html;charset=utf-8");
define('__KIMS__',true);
error_reporting(E_ALL);
ini_set('display_errors','0');
ini_set('log_errors','1');
require __DIR__.'/_core/function/security.func.php';
kimsq_send_security_headers();

ini_set('session.use_only_cookies','1');
ini_set('session.use_strict_mode','1');
session_name('KIMSQSESSID');
session_set_cookie_params(array(
	'lifetime' => 0,
	'path' => '/',
	'secure' => kimsq_is_https(),
	'httponly' => true,
	'samesite' => 'Lax'
));
$sessionPath = __DIR__.'/_tmp/session';
if (is_dir($sessionPath) && is_writable($sessionPath)) session_save_path($sessionPath);
session_start();

/* KimsQ originally relied on register_globals and magic quotes.  Import only
 * valid request names, then escape values with the active mysqli connection. */
kimsq_import_request_variables();

$d = array();
$g = array(
	'path_root'   => './',
	'path_core'   => './_core/',
	'path_var'    => './_var/',
	'path_tmp'    => './_tmp/',
	'path_layout' => './layouts/',
	'path_module' => './modules/',
	'path_widget' => './widgets/',
	'path_switch' => './switchs/',
	'path_page'   => './pages/',
	'path_file'   => './files/',
	'sys_lang'    => 'korean'
);

$g['time_split'] = explode(' ',microtime());
$g['time_start'] = $g['time_split'][0]+$g['time_split'][1];

if (is_file($g['path_var'].'db.info.php'))
{
	require $g['path_module'].'admin/var/var.system.php';
	$g['url_file'] = str_replace('/index.php','',$_SERVER['SCRIPT_NAME']);
	$g['url_host'] = kimsq_site_origin();
	$g['url_http'] = $g['url_host'].($d['admin']['http_port']!=80?':'.$d['admin']['http_port']:'');
	$g['url_sslp'] = 'https://'.$_SERVER['HTTP_HOST'].($_SERVER['HTTPS']!='on'&&$d['admin']['ssl_port']?':'.$d['admin']['ssl_port']:'');
	$g['url_root'] = $g['url_http'].$g['url_file'];
	$g['ssl_root'] = $g['url_sslp'].$g['url_file'];
	
	require $g['path_var'].'db.info.php';
	require $g['path_var'].'table.info.php';
	require $g['path_var'].'switch.var.php';
	require $g['path_core'].'function/db.mysql.func.php';
	require $g['path_core'].'function/sys.func.php';
	foreach(getSwitchInc('start') as $_switch) include $_switch;
	require $g['path_core'].'engine/main.engine.php';
}
else $m = 'admin';

if (!empty($keyword))
{
	$keyword = trim($keyword);
	$_keyword= stripslashes(htmlspecialchars($keyword));
}
if (empty($p)) $p = 1;
if (empty($m) || !preg_match('/^[A-Za-z0-9_-]+$/',(string)$m) || !is_dir($g['path_module'].$m)) $m = $g['sys_module'];
if (!is_dir($g['path_module'].$m.'/lang.'.$_HS['lang'])) $_HS['lang']=$g['sys_lang'];
$g['dir_module'] = $g['path_module'].$m.'/';
$g['url_module'] = $g['s'].'/modules/'.$m;
$g['img_module'] = $g['url_module'].'/image';
$g['add_module'] = $g['dir_module'].'_main.php';

if (is_file($g['add_module'])) include $g['add_module'];
if ($a) require $g['path_core'].'engine/action.engine.php';
if ($_HS['open'] > 1) require $g['path_core'].'engine/siteopen.engine.php';
if (!$s && $m != 'admin') getLink($g['s'].'/?r='.$r.'&m=admin&module='.$g['sys_module'].'&nosite=Y','','','');

include $g['dir_module'].'main.php';

if ($m=='admin' || $iframe=='Y') $d['layout']['php'] = $_HM['layout'] = '_blank/main.php';
else {
	if (!$g['mobile']||$_SESSION['pcmode']=='Y') $d['layout']['php'] = $prelayout ? $prelayout.'.php' : ($_HM['layout'] ? $_HM['layout'] : $_HS['layout']);
	else $d['layout']['php'] = $prelayout ? $prelayout.'.php' : ($_HS['m_layout'] ? $_HS['m_layout'] : ($_HM['layout'] ? $_HM['layout'] : $_HS['layout']));
}

$d['layout']['dir'] = dirname($d['layout']['php']);
$d['layout']['str'] = str_replace('.php','',$d['layout']['php']);
$d['layout']['pwd'] = $g['path_layout'].$d['layout']['str'];
$d['layout']['var'] = $g['path_layout'].$d['layout']['dir'].'/_main.php';

$g['url_layout'] = $g['s'].'/layouts/'.$d['layout']['dir'];
$g['img_layout'] = $g['url_layout'].'/image';
if (is_file($d['layout']['var'])) include $d['layout']['var'];
define('__KIMS_CONTENT__',$g['path_core'].'engine/content.engine.php');
define('__KIMS_CONTAINER_HEAD__',$g['path_core'].'engine/container_head.engine.php');
define('__KIMS_CONTAINER_FOOT__',$g['path_core'].'engine/container_foot.engine.php');
foreach($g['switch_1'] as $_switch) include $_switch;
if ($m!='admin'){include $g['path_var'].'sitephp/'.$_HS['uid'].'.php';if($_HS['buffer']){$g['buffer']=true;ob_start('ob_gzhandler');}}
?>
<?php if($_HS['dtd']=='xhtml_1'):?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html id="kimsQStart" lang="<?php echo $lang['sys']['lang']?>" xml:lang="<?php echo $lang['sys']['lang']?>" xmlns="http://www.w3.org/1999/xhtml">
<?php else:?>
<!DOCTYPE html>
<html lang="<?php echo $lang['sys']['lang']?>">
<?php endif?>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<?php if($g['mobile']&&$_SESSION['pcmode']!='Y'&&$_HS['m_layout']):?>
<meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,minimum-scale=1.0,user-scalable=no,target-densitydpi=medium-dpi" />
<meta name="apple-mobile-web-app-capable" content="no" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<?php endif?>
<title><?php echo $g['browtitle']?></title>
<?php require $g['path_core'].'engine/cssjs.engine.php'?>
</head>
<body>
<?php if(!$d['admin']['hidepannel']&&$my['admin']&&!$iframe&&(!$g['mobile']||$_SESSION['pcmode']=='Y')):?>
<?php include $g['path_var'].'language/'.$g['sys_selectlang'].'/_top.lang.php'?>
<?php include $g['path_core'].'engine/adminbar.engine.php'?>
<?php endif?>
<?php include $g['path_layout'].$d['layout']['php']?>
<?php if($g['mobile']&&$_SESSION['pcmode']=='Y'&&$iframe!='Y'):?>
<div id="pctomobile"><a href="<?php echo $g['s']?>/?r=<?php echo $r?>&amp;a=mobilemode"><?php echo sprintf($lang['sys']['viewpcmode'],$m=='admin'?$lang['top']['adminmode']:$lang['top']['homepage'])?></a></div>
<?php endif?>

<div id="_box_layer_"></div>
<div id="_action_layer_"></div>
<div id="_hidden_layer_"></div>
<div id="_overLayer_" class="hide"></div>
<iframe name="_action_frame_<?php echo $m?>" width="0" height="0" frameborder="0" scrolling="no"></iframe>
<script type="text/javascript">
//<![CDATA[
document.body.onclick = closeMemberLayer;
document.onkeydown = closeImgLayer;
//]]>
</script>
<script type="text/javascript">
//<![CDATA[
(function () {
	var token = '<?php echo kimsq_csrf_token()?>';
	function addToken(form) {
		if (!form || !form.elements || form.elements._csrf) return;
		var input = document.createElement('input');
		input.type = 'hidden'; input.name = '_csrf'; input.value = token;
		form.appendChild(input);
	}
	document.addEventListener('submit', function (event) { addToken(event.target); }, true);
	var nativeSubmit = HTMLFormElement.prototype.submit;
	HTMLFormElement.prototype.submit = function () { addToken(this); return nativeSubmit.call(this); };
	document.addEventListener('click', function (event) {
		var link = event.target;
		while (link && link.tagName !== 'A') link = link.parentNode;
		if (!link) return;
		var href = link.getAttribute('href');
		if (!href || href.indexOf('a=') === -1 || /(?:[?&])_csrf=/.test(href)) return;
		if (/^[a-z][a-z0-9+.-]*:/i.test(href) && link.host !== window.location.host) return;
		link.href = href + (href.indexOf('?') === -1 ? '?' : '&') + '_csrf=' + encodeURIComponent(token);
	}, true);
}());
//]]>
</script>
<?php foreach($g['switch_3'] as $_switch) include $_switch?>
<?php echo $_HS['footercode']?>
</body>
</html>
<?php foreach($g['switch_4'] as $_switch) include $_switch?>
<?php if($g['widget_cssjs']) include $g['path_core'].'engine/widget.cssjs.php'?>
<?php if($g['buffer']) ob_end_flush()?>
