<?php
if(!defined('__KIMS__')) exit;

$iswpiinstall = false;
$wpivfile  = './wpi.var.php';
if (file_exists($wpivfile))
{
	$iswpiinstall = true;
	include_once $wpivfile;
}
$moduledir = array();
$_oldtable = array();
$_tmptable = array();
$_tmpdfile = $g['path_var'].'db.info.php';
$_tmptfile = $g['path_var'].'table.info.php';
include_once $g['path_core'].'function/sys.func.php';
include_once $g['path_core'].'function/db.mysql.func.php';
$date = getVDate(0);

if (is_file($_tmpdfile)) getLink('./','','','');

$dbhost = trim((string)$dbhost);
$dbname = trim((string)$dbname);
$dbuser = trim((string)$dbuser);
$dbhead = trim((string)$dbhead);
$dbport = (int)$dbport;

// Validate the administrator password before creating files or database tables.
if (!kimsq_password_is_valid($pw1) || $pw0 !== $pw1)
{
	echo '<script type="text/javascript">parent.isSubmit=false;parent.goStep(3);parent.procForm.pw0.focus();</script>';
	getLink('','',!kimsq_password_is_valid($pw1)?'비밀번호는 12자 이상 입력해 주세요.':'입력한 두 비밀번호가 일치하지 않습니다.','');
}
if (!preg_match('/^[A-Za-z0-9._:-]{1,255}$/', $dbhost) || !preg_match('/^[A-Za-z0-9_$-]{1,64}$/', $dbname)
	|| !preg_match('/^[A-Za-z0-9_$.-]{1,64}$/', $dbuser) || !preg_match('/^[A-Za-z0-9_]{1,32}$/', $dbhead)
	|| $dbport < 1 || $dbport > 65535) getLink('','','Invalid database configuration.','');
$dbtype = 'InnoDB';
$DB = array('host' => $dbhost, 'name' => $dbname, 'user' => $dbuser, 'pass' => (string)$dbpass, 'head' => $dbhead, 'port' => $dbport, 'type' => $dbtype);
$DB_CONNECT = isConnectDb($DB);
if (!$DB_CONNECT)
{
	echo '<script type="text/javascript">parent.isSubmit=false;parent.goStep(2);</script>';
	getLink('','','DB접속 유저네임이나 패스워드 혹은 포트가 정확하지 않습니다.','');
}
$DB_SELECT = true;
if (!$DB_SELECT)
{
	echo '<script type="text/javascript">parent.isSubmit=false;parent.goStep(2);parent.procForm.dbname.focus();</script>';
	getLink('','','DB네임이 정확하지 않습니다.','');
}

$ISRBDB = db_fetch_array(db_query('select count(*) from '.$dbhead.'_s_module',$DB_CONNECT));
if ($ISRBDB[0])
{
	echo '<script type="text/javascript">parent.isSubmit=false;parent.goStep(2);parent.procForm.dbhead.focus();</script>';
	getLink('','','이미 동일한 테이블식별자로 주스큐용 DB가 생성되어 있습니다.    \n다른 테이블식별자를 사용해 주세요.','');
}

// Check every location that the installer must modify before creating DB files or tables.
$installWritable = array(
	$g['path_var'],
	$g['path_page'],
	$g['path_module'].'admin/var/users/'
);
foreach (array('main','join','login','mypage','search','agreement','private','postrule','main_mobile') as $_pageid)
{
	$installWritable[] = $g['path_page'].$_pageid.'.php';
	$installWritable[] = $g['path_page'].$_pageid.'.widget.php';
}
$dirh = opendir($g['path_module']);
while (false !== ($_file = readdir($dirh)))
{
	if ($_file == '.' || $_file == '..') continue;
	$_settingDir = $g['path_module'].$_file.'/_setting/';
	if (is_file($_settingDir.'db.table.php')) $installWritable[] = $_settingDir;
}
closedir($dirh);
foreach ($installWritable as $_installPath)
{
	if ((is_dir($_installPath) && !is_writable($_installPath)) || (is_file($_installPath) && !is_writable($_installPath)))
	{
		echo '<script type="text/javascript">parent.isSubmit=false;parent.hideInstallProgress&&parent.hideInstallProgress();</script>';
		getLink('','','설치에 필요한 파일 쓰기 권한이 없습니다. 서버 관리자에게 확인해 주세요.\n'.$_installPath,'');
	}
}

$fp = fopen($_tmpdfile,'w');
fwrite($fp, "<?php\n");
foreach ($DB as $_dbkey => $_dbvalue) fwrite($fp, '$DB['.var_export($_dbkey,true).'] = '.var_export($_dbvalue,true).";\n");
fwrite($fp, "?>");
fclose($fp);
@chmod($_tmpdfile,0640);
$DB['type'] = $dbtype;
$DB['head'] = $dbhead;

if (is_file($_tmptfile)) 
{
	include_once $_tmptfile;
	$_oldtable = $table;
}

$dirh = opendir($g['path_module']); 
while(false !== ($_file = readdir($dirh))) 
{ 
	if($_file == '.' || $_file == '..') continue;

	if(is_file($g['path_module'].$_file.'/_setting/db.table.php')) 
	{	
		$table = array();
		$module= $_file;
		include_once $g['path_module'].$_file.'/_setting/db.table.php';
		include_once $g['path_module'].$_file.'/_setting/db.schema.php';

		foreach($table as $key => $val) $_tmptable[$key] = $val;
		rename($g['path_module'].$_file.'/_setting/db.table.php',$g['path_module'].$_file.'/_setting/db.table.php.done');

		$moduledir[$_file] = array($_file,count($table));
	}
	else {
		$moduledir[$_file] = array($_file,0);
	}
} 
closedir($dirh);

$fp = fopen($_tmptfile,'w');
fwrite($fp, "<?php\n");
foreach($_oldtable as $key => $val)
{
	if (!$_tmptable[$key])
	{
		fwrite($fp, "\$table['$key'] = \"$val\";\n");
	}
}
foreach($_tmptable as $key => $val)
{
	fwrite($fp, "\$table['$key'] = \"$val\";\n");
}
fwrite($fp, "?>");
fclose($fp);
@chmod($_tmptfile,0707);

include $_tmptfile;

$gid = 0;

$mdlarray = array('home','layout','module','market','admin','member','bbs','comment','upload');
foreach($mdlarray as $_val)
{
	$QUE = "insert into ".$table['s_module']." 
	(gid,`system`,hidden,mobile,name,id,tblnum,d_regis) 
	values 
	('".$gid."','1','0','1','".getFolderName($g['path_module'].$moduledir[$_val][0])."','".$moduledir[$_val][0]."','".$moduledir[$_val][1]."','".$date['totime']."')";
	db_query($QUE,$DB_CONNECT);
	$gid++;
}

$mdlarray = array('popup','filemanager','dbmanager','mobile','domain','counter','search','widget','tag','trackback','editor','rewrite','zipsearch');

foreach($mdlarray as $_val)
{
	$QUE = "insert into ".$table['s_module']." 
	(gid,`system`,hidden,mobile,name,id,tblnum,d_regis) 
	values 
	('".$gid."','".(strstr('[counter][widget][tag][rewrite]','['.$_val.']')?0:1)."','".(strstr('[rewrite][zipsearch]','['.$_val.']')?1:0)."','".(strstr('[rewrite][zipsearch]','['.$_val.']')?0:1)."','".getFolderName($g['path_module'].$moduledir[$_val][0])."','".$moduledir[$_val][0]."','".$moduledir[$_val][1]."','".$date['totime']."')";
	db_query($QUE,$DB_CONNECT);
	$gid++;
}

$siteid = 'home';
$QKEY = "gid,id,name,title,titlefix,icon,layout,startpage,m_layout,m_startpage,lang,open,dtd,nametype,timecal,rewrite,buffer,usescode,headercode,footercode";
$QVAL = "'0','".$siteid."','$sitename','$sitename','0','1.png','$layout','1','mobile/main.php','9','$sitelang','1','xhtml_1','nic','0','0','0','0','',''";
getDbInsert($table['s_site'],$QKEY,$QVAL);
db_query("OPTIMIZE TABLE ".$table['s_site'],$DB_CONNECT); 


$pagesarray = array
(
	'main'=>array('메인화면','3',''),
	'join'=>array('회원가입','1','./?m=member&front=join'),
	'login'=>array('로그인','1','./?m=member&front=login'),
	'mypage'=>array('마이페이지','1','./?m=member&front=mypage'),
	'search'=>array('통합검색','1','./?m=search'),
	'agreement'=>array('홈페이지 이용약관','3',''),
	'private'=>array('개인정보 취급방침','3',''),
	'postrule'=>array('게시물 게재원칙','3','')
);

$maincontent = '<iframe src="http://docs.kimsq.com/guide/rb/defaultPage_kr.php'.($iswpiinstall?'?wpi=Y':'').'" width="100%" height="650" frameborder="0" scrolling="no" allowTransparency="true"></iframe>';

foreach($pagesarray as $_key => $_val)
{
	$QUE = "insert into ".$table['s_page']." 
	(pagetype,ismain,mobile,id,category,name,perm_g,perm_l,layout,joint,hit,d_regis,d_update)
	values
	('$_val[1]','".($_key=='main'?1:0)."','".($_key=='main'?1:0)."','$_key','기본페이지','$_val[0]','','0','','$_val[2]','0','".$date['totime']."','')";
	db_query($QUE,$DB_CONNECT);

	$mfile = $g['path_page'].$_key.'.php';
	$fp = fopen($mfile,'w');
	fwrite($fp,($_key=='main'?$maincontent:$_val[0]));
	fclose($fp);
	@chmod($mfile,0707);
	$mfile = $g['path_page'].$_key.'.widget.php';
	$fp = fopen($mfile,'w');
	fwrite($fp,'');
	fclose($fp);
	@chmod($mfile,0707);
}


$QUE = "insert into ".$table['s_page']." 
(pagetype,ismain,mobile,id,category,name,perm_g,perm_l,layout,joint,hit,sosokmenu,d_regis,d_update)
values
('3','0','1','main_mobile','모바일페이지','메인화면','','0','','','0','','".$date['totime']."','')";
db_query($QUE,$DB_CONNECT);

$mobilemaincontent = '<h3 style="border-bottom:#dfdfdf dotted 2px;padding:0 0 10px 0;font-size:13px;">'."\n";
$mobilemaincontent.= '<img src="<?php echo $g[\'img_core\']?>/_public/ico_notice.gif" alt="" style="position:relative;top:2px;left:-2px;" />모바일 웹사이트가 개설되었습니다'."\n";
$mobilemaincontent.= '</h3>'."\n\n";
$mobilemaincontent.= '<p style="color:#666666;line-height:150%;padding:0 0 0 5px;">'."\n";
$mobilemaincontent.= '손님께서는 모바일기기(<?php echo $g[\'mobile\']?>)로 접속하셨습니다.<br />'."\n";
$mobilemaincontent.= '이 페이지는 모바일 메인페이지의 전시내용을 변경하거나 편집을 통해 변경할 수 있습니다.<br />'."\n";
$mobilemaincontent.= '자세한 것은 주스큐 모바일 안내서를 참고하세요.'."\n";
$mobilemaincontent.= '</p>'."\n\n";
$mobilemaincontent.= '<p style="text-align:right;padding:20px 10px 0 0;font-size:11px;color:#c0c0c0;">'."\n";
$mobilemaincontent.= 'Powered by kimsQ<span style="color:#ff0000;font-weight:bold;">Rb</span>'."\n";
$mobilemaincontent.= '</p>'."\n";

$mfile = $g['path_page'].'main_mobile.php';
$fp = fopen($mfile,'w');
fwrite($fp,$mobilemaincontent);
fclose($fp);
@chmod($mfile,0707);
$mfile = $g['path_page'].'main_mobile.widget.php';
$fp = fopen($mfile,'w');
fwrite($fp,'');
fclose($fp);
@chmod($mfile,0707);

$passwordHash = kimsq_hash_password($pw1);
db_query("insert into ".$table['s_mbrid']." (site,id,pw)values('1','$id','$passwordHash')",$DB_CONNECT);
$QUE = "insert into ".$table['s_mbrdata']." 
(memberuid,site,auth,sosok,level,comp,admin,adm_view,
email,name,nic,grade,photo,home,sex,birth1,birth2,birthtype,tel1,tel2,zip,
addr0,addr1,addr2,job,marr1,marr2,sms,mailing,smail,point,usepoint,money,cash,num_login,pw_q,pw_a,now_log,last_log,last_pw,is_paper,d_regis,tmpcode,sns,addfield)
values
('1','1','1','1','1','0','1','',
'$email','$name','관리자','','','','0','0','0','0','','','',
'','','','','0','0','1','1','0','0','0','0','0','1','주스큐 설치시에 입력한 회원비밀번호는?','$pw1','1','".$date['totime']."','".$date['today']."','0','".$date['totime']."','','','')";
db_query($QUE,$DB_CONNECT);

$sosokset = array('A그룹','B그룹','C그룹','D그룹','E그룹','F그룹','G그룹','H그룹');
$i = 0;
foreach ($sosokset as $_val)
{
	getDbInsert($table['s_mbrgroup'],'gid,name,num',"'".$i."','".$_val."','".(!$i?1:0)."'");
	$i++;
}
for ($i = 1; $i < 101; $i++)
{
	getDbInsert($table['s_mbrlevel'],'gid,name,num,login,post,comment',"'".($i==20?1:0)."','레벨".$i."','".($i==1?1:0)."','0','0','0'");
}

setcookie('svshop', $id.'|', array('expires' => time()+60*60*24*30, 'path' => '/', 'secure' => kimsq_is_https(), 'httponly' => true, 'samesite' => 'Lax'));

session_regenerate_id(true);
$_SESSION['mbr_uid'] = 1;
$_SESSION['mbr_pw']  = $passwordHash;

$fp = fopen($g['path_module'].'admin/var/users/'.$id.'.widget.php','w');
fwrite($fp,'');
fclose($fp);
@chmod($g['path_module'].'admin/var/users/'.$id.'.widget.php',0707);

include_once $g['path_module'].'admin/lib/telemetry.func.php';
if (jusqTelemetryCreate(true)) jusqTelemetryReport('install',true);


if ($iswpiinstall)
{
	unlink($wpivfile);
	getLink('./','','설치가 완료되었습니다. 홈페이지로 이동합니다.','');
}

$installPath = str_replace('/index.php','',(string)($_SERVER['SCRIPT_NAME'] ?? '/index.php'));
$installHome = rtrim(kimsq_site_origin().$installPath,'/').'/';
echo '<!DOCTYPE html><html lang="ko"><head><meta charset="utf-8" /><meta name="robots" content="noindex,nofollow" /></head><body><script type="text/javascript">(function(){var home='.
	json_encode($installHome, JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT).
	';var targetWindow=window.top&&window.top!==window?window.top:window;targetWindow.location.replace(home);})();</script></body></html>';
exit;
?>
