<?php
 /**
 *      [XCECP] (C) COLNET Inc.
 *      This is NOT a freeware, use is subject to license terms
 */
define("IN_QKWEB",true);
define("INSTALL",true);
require("../includes/init.php");
if(file_exists(ROOT_PATH."data/install.lock")){Msg::show("已经执行过安装程序");}
$smarty->setTemplateDir(ROOT_PATH."install/template/");

$step = getgpc("step");
if(!$step){$step=1;}
if(!in_array($step,array(1,2))){Msg::show("未知步骤");}

if($step==2){
	$inajax = getgpc("inajax","P");
	$act=getgpc("act","P");
	if($inajax=="1"){
		$ret = array("success"=>false,"message"=>"参数错误");
		if($act=="dbchk"){
			$dbhost = getgpc("dbhost");
			$dbuser = getgpc("dbuser");
			$dbpwd = getgpc("dbpwd");
			$dbname = getgpc("dbname");
			$createdb = getgpc("createdb");
			if(!$dbhost || !$dbuser || !$dbname){
				$ret['message'] = '数据库信息填写不完整';
			}else{
				$db = new Mysql($dbhost,$dbuser,$dbpwd,$dbname,"utf-8");
				if(!$db->checkConnect()){
					$ret['message'] = '数据库连接失败，请修改后重试';
				}else if(!$db->checkDatabase() && ($createdb!='1' || ($createdb=='1'  && !$db->createDatabase()))){
					$ret['message'] = '选择数据库失败，请修改后重试';
				}else if(!$db->check(array('dbhost'=>$dbhost,'dbuser'=>$dbuser,'dbpwd'=>$dbpwd,'dbname'=>$dbname))){
					$ret['message'] = '数据库访问失败，请修改后重试';
				}else{
					$ret['message'] = '数据库连接成功';
					$ret['success']=true;
					$_SESSION['dbready'] = true;
					$_SESSION['dbhost'] = $dbhost;
					$_SESSION['dbuser'] = $dbuser;
					$_SESSION['dbpwd'] = $dbpwd;
					$_SESSION['dbname'] = $dbname;
					$db->close();
				}
			}
		}else if($act=="install"){
			if(!$_SESSION['dbready']){ $ret['message'] = '数据库连接失败'; }else{
					$db = new Mysql($_SESSION['dbhost'],$_SESSION['dbuser'],$_SESSION['dbpwd'],$_SESSION['dbname'],"utf8");
					if(!$db->check()){ $ret['message'] = '数据库连接失败'; }else{
					$conf = ROOT_PATH."configs/config.php";
					$config = file_get_contents($conf); //echo $config;
					$config = preg_replace("/[$]dbhost = \"(.*)\";\n/","\$dbhost = \"".$_SESSION['dbhost']."\";\n",$config);
					$config = preg_replace("/[$]dbuser = \"(.*)\";\n/","\$dbuser = \"".$_SESSION['dbuser']."\";\n",$config);
					$config = preg_replace("/[$]dbpwd = \"(.*)\";\n/","\$dbpwd = \"".$_SESSION['dbpwd']."\";\n",$config);
					$config = preg_replace("/[$]dbname = \"(.*)\";\n/","\$dbname = \"".$_SESSION['dbname']."\";\n",$config);
					
					if(!file_put_contents($conf,$config)){
						$ret['message'] = '写入配置文件失败';
					}else{
						$sql = file_get_contents(ROOT_PATH."install/data.sql");
						if(!$sql){$ret['message'] = '数据库文件不存在';}else if(!$db->clearDatabase()){
							$ret['message'] = '清空数据库失败：'.$db->msg;
						}else{
							
							$sql = preg_replace("/^--(.*)\n/m","",$sql);
							//echo $sql;exit;
							$sqlArr = explode(";\n",$sql);
							foreach($sqlArr as $item){
								$sql = $item.";";
								$sql = preg_replace("/\),\n/","),",$sql);
								$sql = preg_replace("/\n/","",$sql);
								if($sql && strlen($sql)>3 && !$db->query($sql,true)){ $error = true; break;}
							}
							if(!$error){
								file_put_contents(ROOT_PATH."data/install.lock","xcecp_".VERSION."\nwww.ueidc.com");
								$ret['success'] = true;
								$ret['message'] = '恭喜，炫彩电子商务平台安装成功';
							}else{
								$ret['message'] = '执行数据库操作失败，请通过phpMyAdmin手工处理或联系炫彩客服查看。';
							}
						}
					}
					}
			
			}
		}
		echo json_encode($ret);
		exit;
	}
	$df = ROOT_PATH."data/";
	if(!file_exists($df)){
		Msg::show("数据目录不存在");	
	}
	if(file_exists($df."install.lock")){Msg::show("安装已被锁定。请通过");}

	if(strpos($_SERVER['SERVER_SOFTWARE'],"Apache")===false && strpos($_SERVER['SERVER_SOFTWARE'],"nginx")===false){Msg::show("请在Apache服务器下安装。IIS等其它WEB服务器软件暂不受本系统支持。");}
	if(strpos($_SERVER['SERVER_SOFTWARE'],"PHP/5")===false && substr(PHP_VERSION,0,1)<5){Msg::show("系统所需PHP版本为PHP5以上，请先升级PHP版本，当前版本为 ".PHP_VERSION);}
	//if(strpos($_SERVER['Load']))
	if(function_exists("apache_get_modules") && !in_array("mod_rewrite",apache_get_modules())){
		Msg::show('Apache没有加载mod_rewrite模块，不可使用本系统，请先开启mod_rewrite');
	}

	
}



display("step".$step);

?>