<?php

/**
 *      【橙创家校通v4.0】 (C)2010-2012 Tuntron Inc.
 *      
 *    	 http://www.tuntron.com   QQ：1121251729
 *
 *      &系统安装文件&
 */
set_time_limit(0);
$dbcharset = 'utf8';
$step = intval($_GET['step']);
$step = $step ? $step : '1' ;
require TUNTRON_DAT.'/version.php';
$installinfo= "欢迎来到 <font class=\"softname\">".TJ_SOFTNAME."</font> <font class=\"version\">v".TJ_VERSION."</font> 安装向导，安装前请仔细阅读安装说明后才开始安装。安装文件夹里同样提供了有关软件安装的说明，请您仔细阅读。";

if($step == '1'){
	
	chk_install();
	$info = "阅读安装协议";
	include(install_tpl("inc_head"));
	$licence = openfile("licence.txt");
	include(install_tpl("step1"));
}elseif($step == '2'){
	
	chk_install();
	$info = "阅读使用说明";
	include(install_tpl("inc_head"));
	$licence = openfile("readme.txt");
	include(install_tpl("step2"));
	
}elseif($step == '3'){
	
	chk_install();
	$info = "检测系统环境";
	$phpv = @phpversion();
	$sp_server = $_SERVER["SERVER_SOFTWARE"];
	$sp_host = (empty($_SERVER["REMOTE_ADDR"]) ? $_SERVER["REMOTE_HOST"] : $_SERVER["REMOTE_ADDR"]);
	$sp_name = $_SERVER["SERVER_NAME"];
	$short_open_tag = ini_get('short_open_tag')?'<font color=green>支持</font>':'<font color=red>请修改php.ini，short_open_tag=On，否则无法安装</font>';
	$disabled = ini_get('short_open_tag')?'':'disabled="disabled"';
	require_once(TUNTRON_DAT."/sp_testdirs.php");
	include(install_tpl("inc_head"));
	include(install_tpl("step3"));
	
}elseif ($step == '4'){
	
	chk_install();
	$info = "填写数据库及系统信息";
	include(install_tpl("inc_head"));
	include(install_tpl("step4"));
	
}elseif($step == '5'){
	chk_install();
	$db_host  	= trim($_POST['db_host']);
	$db_user  	= trim($_POST['db_user']);
	$db_pass  	= trim($_POST['db_pass']);
	$db_name  	= trim($_POST['db_name']);
	$db_tuntron = trim($_POST['db_tuntron']);
	$sitename   = trim($_POST['sitename']);
	$admin      = trim($_POST['manager']);
	$password   = trim($_POST['manager_pass']);
	$repassword = trim($_POST['manager_chkpass']);
	$email      = trim($_POST['email']);
	$in_type	= trim($_POST['install_type']);
	
	!$db_host && Note("未填写数据库服务器地址。");
	
	!$db_user && Note("未填写数据库服务器用户名。");
	
	!$db_name && Note("未填写数据库名称。");
	
	(!$db_tuntron || strstr($db_tuntron, '.')) && Note("您指定的数据表前缀包含点字符，请返回修改。");
	
	!$admin && Note("未填写创始人登录用户名。");
	
	!$password && Note("未填写创始人管理密码。");
	
	!$repassword && Note("未填写重复密码。");
	
	($password != $repassword) && Note("两次输入的密码不一致。");
	
	(!$email || !is_email($email)) && Note("电子邮件格式不正确。");

	$conn = @mysql_connect($db_host, $db_user, $db_pass);
	
	($conn === false) && Note("安装失败！请检查数据库用户名以及数据库密码是否正确。"); 
	
	mysql_connect($db_host, $db_user, $db_pass);
	
	$cur_os = PHP_OS;
	
	$cur_phpversion = PHP_VERSION;
	
	($cur_phpversion < '4.3.0') && Note("您的PHP版本低于4.3.0, 无法安装使用 ".MPS_SOFTNAME."<br />");

	$cur_mysqlversion = mysql_get_server_info();
	
	($cur_mysqlversion < '3.23') && Note("您的MySQL版本低于3.23, 由于程序没有经过此平台的测试, 建议您换 MySQL4 的数据库服务器.<br />");
	
	$yes = mysql_select_db($db_name);
	
	if($yes === false){
		$sql = $mysql_version >= '4.1' ? "CREATE DATABASE $db_name DEFAULT CHARACTER SET $db_charset" : "CREATE DATABASE $db_name";
		(mysql_query($sql, $conn) === false) && Note("无法创建数据库,请检查相关参数是否正确。");
	}
	
	@mysql_close($conn);
	
	$files = "<?php\n\n";
	$files .= "if (!defined('IS_RUN')) die('Forbidden');\n\n";
	$files .= "\$_G['dbinfo']    = array();\n\n";
	$files .= "\$_G['dbinfo']['dbcharset'] = \"$dbcharset\";\n\n";
	$files .= "//系统字符集编码\n\n";
	$files .= "\$_G['dbinfo']['dbhost']    = \"$db_host\";\n\n";
	$files .= "//数据库服务器地址，一般为localhost\n\n";
	$files .= "\$_G['dbinfo']['dbname']    = \"$db_name\";\n\n";
	$files .= "//使用的数据库名称\n\n";
	$files .= "\$_G['dbinfo']['dbuser']    = \"$db_user\";\n\n";
	$files .= "//数据库帐号\n\n";
	$files .= "\$_G['dbinfo']['dbpw']    = \"$db_pass\";\n\n";
	$files .= "//数据库密码\n\n";
	$files .= "\$_G['dbinfo']['dbpre']   = \"$db_tuntron\";\n\n";
	$files .= "//数据库前缀\n\n";
	$files .= "\$_G['dbinfo']['pconnect']    = \"0\";\n\n";
	$files .= "//数据库持久连接 0=关闭, 1=打开\n\n";
	$files .= "\$_G['dbinfo']['ping']    = \"FALSE\";\n\n";
	$files .= "//查询数据库前进行Ping操作，解决数据库连接丢失问题\n\n";
	$files .= "\$_G['cookiepre'] = \"".random("11")."_\";\n\n";
	$files .= "//cookies加密前缀\n\n";
	$files .= "\$_G['cookiepath'] = \"/\";\n\n";
	$files .= "//Cookie有效路径\n\n";
	$files .= "\$_G['cookiedomain'] = \"\";\n\n";
	$files .= "//Cookie作用域\n\n";
	$files .= "\$_G['charset'] = \"utf-8\";\n\n";
	$files .= "//网页编码\n\n";
	$files .= "\$_G['attackevasive'] = \"0\";\n\n";
	$files .= "//可防止大量的非正常请求造成的拒绝服务攻击\n\n";
	$files .= "\$_G['timezone'] = \"8\";\n\n";
	$files .= "//与格林时间的相差时间，北京时间为8，仅 PHP5 有效\n\n";
	$files .= "\$_G['admincp'] =  array();\n\n";
	$files .= "//0=关闭, 1=cookie 刷新限制, 2=限制代理访问, 3=cookie+代理限制\n\n";
	$files .= "\$_G['admincp']['checkip'] = \"1\";\n\n";
	$files .= "//后台管理操作是否验证管理员的IP,1=是[安全],0=否仅在管理员无法登陆后台时设置0。\n\n";
	$files .= "\$_G['admincp']['dir'] = \"admin\";\n\n";
	$files .= "//后台核心文件所在文件夹\n\n";
	$files .= "\$_G['lang_directory'] = \"CH\";\n\n";
	$files .= "//默认实用语言包\n\n";
	$files .= "\$_G['template'] = \"default\";\n\n";
	$files .= "//默认实用语言包\n\n";
	$files .= "\$_G['sl_domain'] = \"TRUE\";\n\n";
	$files .= "//默认实用语言包\n\n";

	$files .= "?>";
	
	$file = @fopen(TUNTRON_DAT. '/config.db.php', 'wb+');
	
	!$file && Note('无法打开数据库配置文件 /config.db.php');
	if(!@fwrite($file, trim($files))){
		Note('无法写入配置文件 config.db.php');
		exit;
	}
	@fclose($file);
	require_once(TUNTRON_DAT."/config.db.php");
	include_once(TUNTRON_INC.'/class/base.class.php'); //加载基础类文件
	include_once(TUNTRON_INC.'/class/mod.class.php');  //加载模块类文件
	require_once(TUNTRON_INC."/class/install.class.php");
	
	if(file_exists($import_sql = TUNTRON_PATH."/install/jxt_".TJ_VERSION.".sql")){;
	$install 	= G_class('install')->import($import_sql,$db_tuntron,$dbcharset);
	}else{Note('数据文件“/install/jxt_'.TJ_VERSION.'.sql”不存在');}
	if($install){
		$now_domain = get_inurl();
		G_class('install')->create_admin($admin,$password,$repassword);//创建管理员
		G_class('install')->create_siteinfo($sitename,$now_domain);    //更新网站信息
		write_lock();
		$step = "!";
		$info = "完成安装";
		$tuntron_install_success_info = "恭喜，您的".TJ_SOFTNAME."v".TJ_VERSION."已经安装成功！";
		include(install_tpl("inc_head"));
		include(install_tpl("step5"));
	} else {
		Note('您的'.TJ_SOFTNAME.'安装失败！');
	}
}

include(install_tpl("inc_foot"));

?>