<?php
version_compare(PHP_VERSION, '5.2.0', '>') || die('require PHP > 5.2.0 !');
define('NZCMS_INST', dirname(__FILE__));
define('NZCMS_CORE', dirname(NZCMS_INST));
define('NZCMS_ROOT', dirname(NZCMS_CORE));
define('APP_NAME', basename(NZCMS_CORE));
error_reporting(0);
date_default_timezone_set('Asia/Shanghai');
header('Content-Type: text/html; charset=UTF-8');
if(is_file(NZCMS_CORE.'/config/config.php')) {
	header("HTTP/1.0 404 Not Found");
	header("Status: 404 Not Found");
	include NZCMS_INST.'/tpl/lock.php';
	exit;
}
include NZCMS_CORE.'/finephp/base/base.func.php';
include NZCMS_INST.'/function.php';
$do = isset($_GET['do']) && in_array($_GET['do'], array('license', 'check_env', 'check_db', 'complete')) ? $_GET['do'] : 'license';
if($do == 'license') {
	include NZCMS_INST.'/tpl/header.php';
	include NZCMS_INST.'/tpl/license.php';
	include NZCMS_INST.'/tpl/footer.php';
}elseif($do == 'check_env') {
	include NZCMS_INST.'/tpl/header.php';
	include NZCMS_INST.'/tpl/check_env.php';
	include NZCMS_INST.'/tpl/footer.php';
}elseif($do == 'check_db') {
	$mysql_support = function_exists('mysql_connect');
	$mysqli_support = function_exists('mysqli_connect');
	$isphp5 = is_php('5.5.0');
	
	include NZCMS_INST.'/tpl/header.php';
	include NZCMS_INST.'/tpl/check_db.php';
	include NZCMS_INST.'/tpl/footer.php';
}elseif($do == 'complete') {
	include NZCMS_INST.'/tpl/header.php';
	echo '<div id="cont" class="content"></div><div class="button"></div>';
	include NZCMS_INST.'/tpl/footer.php';
	if(!isset($_POST['dbhost'])) {
		js_back('<u>非法访问！</u>');
	}
	$dbtype = isset($_POST['dbtype']) ? trim($_POST['dbtype']) : '';
	$dbhost = isset($_POST['dbhost']) ? trim($_POST['dbhost']) : '';
	$dbport = isset($_POST['dbport']) ? trim($_POST['dbport']) : '';
	$dbuser = isset($_POST['dbuser']) ? trim($_POST['dbuser']) : '';
	$dbpw = isset($_POST['dbpw']) ? trim($_POST['dbpw']) : '';
	$dbname = isset($_POST['dbname']) ? trim($_POST['dbname']) : '';
	$charset = 'UTF8';
	$tablepre = isset($_POST['dbpre']) ? trim($_POST['dbpre']) : '';
	$adm_user = isset($_POST['adm_user']) ? trim($_POST['adm_user']) : '';
	$adm_pass = isset($_POST['adm_pass']) ? trim(str_replace(' ', '', $_POST['adm_pass'])) : '';
	if(empty($dbhost)) {
		js_back('<u>数据库主机不能为空！</u>');
	}elseif(empty($dbuser)) {
		js_back('<u>数据库用户名不能为空！</u>');
	}elseif(!preg_match('/^\w+$/', $dbname)) {
		js_back('<u>数据库名不正确！</u>');
	}elseif(empty($tablepre)) {
		js_back('<u>数据库表前辍不能为空！</u>');
	}elseif(!preg_match('/^\w+$/', $tablepre)) {
		js_back('<u>数据库表前辍不正确！</u>');
	}elseif(empty($adm_user)) {
		js_back('<u>创始人用户名不能为空！</u>');
	}elseif(strlen($adm_pass) < 8) {
		js_back('<u>密码不能小于8位数！</u>');
	}
	// 连接数据库
	if($dbtype == 'mysql'){
		if(!function_exists('mysql_connect')) {
			js_back('函数 mysql_connect() 不存在，请检查 php.ini 是否加载了 mysql 模块！');
		}
		$link = mysql_connect($dbhost.':'.$dbport, $dbuser, $dbpw);
		if(!$link) {
			js_back('MySQL 主机、账号或密码不正确！<br><u>'.mysql_error().'</u>');
		}
	}
	if($dbtype == 'mysqli'){
		
		if(!function_exists('mysqli_connect') && !extension_loaded('mysqli')) {
			js_back('函数 mysqli_connect() 不存在，缺少mysqli扩展！');
		}
		$link = mysqli_connect($dbhost, $dbuser, $dbpw ,NULL ,$dbport);
		$errno = mysqli_connect_errno();
		if($errno) {
			js_back('<u>'.$errno.'：</u>链接失败，请检查主机、端口、账号或密码是否正确！<br>');
		}
	}
	try{
		if($dbtype == 'mysql'){
			mysql_select_db($dbname, $link);
			if(mysql_errno() == 1049) {
				mysql_query("CREATE DATABASE $dbname DEFAULT CHARACTER SET UTF8");
				if(!mysql_select_db($dbname, $link)) {
					js_back('自动创建数据库失败！您的MySQL账号是否有权限创建数据库？<br><u>'.mysql_error().'</u>');
				}
			}
			// 为防止意外，让用户自己做选择
			if(empty($_POST['cover'])) {
				$query = mysql_query("SHOW TABLES FROM $dbname");
				while($row = mysql_fetch_row($query)) {
					if(preg_match("#^{$tablepre}#", $row[0])) {
						js_back('<u>发现有相同表前缀，请返回选择“覆盖安装”或“修改表前缀”。</u>');
					}
				}
			}
			// 设置编码
			mysql_query("SET names utf8, sql_mode=''");
		}elseif($dbtype == 'mysqli'){
			if(!mysqli_select_db($link,$dbname)) {
				if(!@mysqli_query($link,"CREATE DATABASE $dbname DEFAULT CHARACTER SET UTF8")){
					js_back('自动创建数据库失败！您的MySQL账号是否有权限创建数据库？<br><u>'.mysqli_error().'</u>');
				};
				mysqli_select_db($link , $dbname);
			}
			// 为防止意外，让用户自己做选择
			if(empty($_POST['cover'])) {
				$query = mysqli_query($link,"SHOW TABLES FROM $dbname");
				while($row = mysqli_fetch_row($query)) {
					if(preg_match("#^{$tablepre}#", $row[0])) {
						js_back('<u>发现有相同表前缀，请返回选择“覆盖安装”或“修改表前缀”。</u>');
					}
				}
			}
			// 设置编码
			mysqli_query($link,"SET names utf8, sql_mode=''");
		}
	}catch(Exception $e) {
		if($dbtype == 'mysql'){
			js_back('<u>未知错误！</u><br><u>'.mysql_error().'</u>');
		}else{
			js_back('<u>未知错误！</u><br><u>'.mysqli_error($link).'</u>');
		}
	}

	// 创建数据表
	$file = NZCMS_INST.'/data/mysql.sql';
	if(!is_file($file)) {
		js_back('mysql.sql 文件 <u>丢失</u>');
	}
	$s = file_get_contents($file);
	$sqls = split_sql($s, $tablepre);
	foreach($sqls as $sql) {
		$sql = str_replace("\n", '', trim($sql));
		if($dbtype == 'mysql'){
			$ret = mysql_query($sql);
		}else{
			$ret = mysqli_query($link,$sql);
		}
		if(substr($sql, 0, 6) == 'CREATE') {
			$name = preg_replace("/CREATE TABLE ([`a-z0-9_]+) .*/is", "\\1", $sql);
			if($ret) {
				js_show('创建数据表 '.$name.' ... <i>成功</i>');
			}else{
				js_back('创建数据表 '.$name.' ... <u>失败</u> (您的数据库没有写权限？)');
			}
		}
		if(!$ret) {
			js_back('创建数据表失败</u> (您的数据库没有权限？)<br><u>'.($dbtype == 'mysql') ? mysql_error() : mysqli_error($link).'</u>');
		}
	}

	// 创建基本数据
	$file = NZCMS_INST.'/data/mysql_data.sql';
	if(!is_file($file)) {
		js_back('mysql_data.sql 文件 <u>丢失</u>');
	}
	$s = file_get_contents($file);
	$sqls = split_sql($s, $tablepre);
	$ret = true;
	foreach($sqls as $sql) {
		$sql = str_replace("\n", '', trim($sql));
		$name = preg_replace("/INSERT INTO ([`a-z0-9_]+) .*/is", "\\1", $sql);
		if($dbtype == 'mysql'){
			$res = mysql_query($sql);
		}else{
			$res = mysqli_query($link,$sql);
		}
		if($res){
			js_show('创建基本数据'.$name.' ... <i>成功</i>');
		}else{
			js_back('创建基本数据'.$name.' ... 失败！<u>'.($dbtype == 'mysql') ? mysql_error() : mysqli_error($link).'</u>');
		}
		//mysql_query($sql) || $ret = false;
	}
	if(!$ret) exit;
	$salt = random(16, 3, '0123456789abcdefghijklmnopqrstuvwxyz~!@#$%^&*()_+<>,.');
	$password = md5(md5($adm_pass).$salt);
	$ip = ip2long(ip());
	$time = time();
	if($dbtype == 'mysql'){
		$ret = mysql_query("INSERT INTO `{$tablepre}user` (`uid`, `username`, `password`, `salt`, `groupid`, `email`, `mobile`, `regip`, `regdate`, `loginip`, `logindate`, `lastip`, `lastdate`, `contents`, `comments`, `logins`, `author`, `status`, `isadmin`, `avatar`, `recordtime`, `recordays`, `setoptime`, `money`, `dmoney`, `gold`, `alipay`, `alipay_name`, `qq`, `email_staus`, `mobile_staus`) VALUES(1, '{$adm_user}', '{$password}', '{$salt}', 1, '', '', {$ip}, {$time}, 0, 0, 0, 0, 0, 0, 0, 'admin', 1, 1, '', 0, 0, 0, '0.00', '0.00', 0, '', '', '', 0, 0);");
	}else{
		$ret = mysqli_query($link,"INSERT INTO `{$tablepre}user` (`uid`, `username`, `password`, `salt`, `groupid`, `email`, `mobile`, `regip`, `regdate`, `loginip`, `logindate`, `lastip`, `lastdate`, `contents`, `comments`, `logins`, `author`, `status`, `isadmin`, `avatar`, `recordtime`, `recordays`, `setoptime`, `money`, `dmoney`, `gold`, `alipay`, `alipay_name`, `qq`, `email_staus`, `mobile_staus`) VALUES(1, '{$adm_user}', '{$password}', '{$salt}', 1, '', '', {$ip}, {$time}, 0, 0, 0, 0, 0, 0, 0, 'admin', 1, 1, '', 0, 0, 0, '0.00', '0.00', 0, '', '', '', 0, 0);");
	}
	js_show('创建创始人 ... '.($ret ? '<i>成功</i>' : '<u>失败</u>'));
	if(!$ret) exit;
	// 初始网站设置
	$webdomain = empty($_SERVER['HTTP_HOST']) ? $_SERVER['SERVER_NAME'] : $_SERVER['HTTP_HOST'];
	$webdir = get_webdir();
	$weburl = 'http://'.$webdomain.$webdir;
	$cfg = array(
		'webname' => 'NZ数字化交易管理系统',
		'webdomain' => $webdomain,
		'webdir' => $webdir,
		'webmail' => 'admin@qq.com',
		'tongji' => '',
		'beian' => '京ICP备000001号',
		'seo_title' => 'NZ数字化交易管理系统',
		'seo_keywords' => '虚拟交易系统,付费下载系统,',
		'seo_description' => '数字化内容交易系统、源码/素材/模板交易系统、付费下载系统',

		'link_show' => '{alias}.html',
		'link_show_type' => 3,
		'link_show_end' => '.html',
		'link_cate_page_pre' => '/page_',
		'link_cate_page_end' => '.html',
		'link_cate_end' => '/',
		'link_tag_pre' => 'tag/',
		'link_tag_end' => '.html',
		'link_comment_pre' => 'comment/',
		'link_comment_end' => '.html',
		'link_index_end' => '.html',

		'up_img_ext' => 'jpg,jpeg,gif,png',
		'up_img_max_size' => '1024',
		'up_file_ext' => 'zip',
		'up_file_max_size' => '20480',
		'thumb_article_w' => 163,
		'thumb_article_h' => 124,
		'thumb_products_w' => 250,
		'thumb_products_h' => 250,
		'thumb_photo_w' => 250,
		'thumb_photo_h' => 250,
		'thumb_type' => 2,
		'thumb_quality' => 90,
		'watermark_pos' => 0,
		'watermark_pct' => 90,
		
		'theme' => 'default',
		'gzip' => 1,
		'dis_comment' => 0,
		'user_comment' => 1,
		'comment_status' => 1,
		'comment_filter' => '',
		
		'lifealipay' => array(
			'off' => 1 ,
			'id' => '' ,
			'notice' => '转帐成功后请联系站长，以便及时确认！' ,
		),
		'alipay' => array(
			'off' => 0 ,
			'id' => '' ,
			'partner' => '' ,
			'key' => '' ,
			'sign_type' => 'md5' ,
			'input_charset' => 'utf-8' ,
			'cacert' => '../cacert.pem' ,
			'transport' => 'http' ,
		),
		'weixin' => array(
			'off' => 0 ,
			'APPID' => '' ,
			'MCHID' => '' ,
			'KEY' => '' ,
			'APPSECRET' => '' ,
		),
		'teegon' => array(
			'off' => 0 ,
			'TEE_CLIENT_ID' => '' ,
			'TEE_CLIENT_SECRET' => '' ,
			'alipay_off' 	=> 0 ,
			'wxpay_off' 	=> 0 ,
		),
		'offbank' => array(
			'1' => array(
				'bank' => '中国银行' ,
				'name' => '张三' ,
				'card' => '1234531371324' ,
				'addr' => '中国银行北京市支行' ,
			),
		),
		'user' => array(
			'open_user_model' => 1 ,
			'open_user_reg' =>  1,
			'gift' => 0,
			'gift_mobi_off' => 0,
			'gift_email_off' => 0,
			'charge' => 1,
			'mintx' => 100,
		),
	);
	$vip = array(
		'key'	=> '',
		'_key'	=> '',
		'api_url'	=> 'http://www.newzhan.com/app/index.php111',
		'version'	=> '2.2.0',
		'release'	=> '20161227',
		'backup_theme'	=> 0,
	);
	$settings = addslashes(json_encode($cfg));
	if($dbtype == 'mysql'){
		$ret = mysql_query("INSERT INTO {$tablepre}kv SET k='cfg',v='{$settings}',expiry='0'");
	}else{
		$ret = mysqli_query($link,"INSERT INTO {$tablepre}kv SET k='cfg',v='{$settings}',expiry='0'");
	}
	js_show('初始网站设置 ... '.($ret ? '<i>成功</i>' : '<u>失败</u>'));
	if(!$ret) exit;
	$_vip = addslashes(json_encode($vip));
	if($dbtype == 'mysql'){
		$ret = mysql_query("INSERT INTO {$tablepre}kv SET k='vip',v='{$_vip}',expiry='0'");
	}else{
		$ret = mysqli_query($link,"INSERT INTO {$tablepre}kv SET k='vip',v='{$_vip}',expiry='0'");
	}
	js_show('初始网站设置 ... '.($ret ? '<i>成功</i>' : '<u>失败</u>'));
	$runtime = NZCMS_CORE.'/runtime/';
	$file = $runtime.'_runtime.php';
	if(is_file($file)) {
		$ret = unlink($runtime.'_runtime.php');
		js_show('清除 runtime/_runtime.php ... <i>完成</i>');
	}
	$tpmdir = array('_control', '_model', '_view');
	foreach($tpmdir as $dir) {
		$ret = _rmdir($runtime.'nzcms'.$dir);
		js_show('清除 runtime/nzcms'.$dir.' ... <i>完成</i>');
	}
	foreach($tpmdir as $dir) {
		if($dir == '_model') continue;
		$ret = _rmdir($runtime.'nzcms_admin'.$dir);
		js_show('清除 runtime/nzcms_admin'.$dir.' ... <i>完成</i>');
	}
	$file = NZCMS_INST.'/config.sample.php';
	if(!is_file($file)) {
		js_back('config.sample.php 文件 <u>丢失</u>');
	}
	$auth_key = random(32, 3);
	$cookie_pre = 'pp'.random(5, 3).'_';
	$s = file_get_contents($file);
	$s = preg_replace("#'auth_key' => '\w*',#", "'auth_key' => '".addslashes($auth_key)."',", $s);
	$s = preg_replace("#'cookie_pre' => '\w*',#", "'cookie_pre' => '".addslashes($cookie_pre)."',", $s);
	$s = preg_replace("#'type' => '\w*',#", "'type' => '".addslashes($dbtype)."',", $s);
	$s = preg_replace("#'host' => '\w*',#", "'host' => '".addslashes($dbhost)."',", $s);
	$s = preg_replace("#'port' => '\w*',#", "'port' => '".addslashes($dbport)."',", $s);
	$s = preg_replace("#'user' => '\w*',#", "'user' => '".addslashes($dbuser)."',", $s);
	$s = preg_replace("#'password' => '\w*',#", "'password' => '".addslashes($dbpw)."',", $s);
	$s = preg_replace("#'name' => '\w*',#", "'name' => '".addslashes($dbname)."',", $s);
	$s = preg_replace("#'tablepre' => '\w*',#", "'tablepre' => '".addslashes($tablepre)."',", $s);
	$s = preg_replace("#'pre' => '\w*',#", "'pre' => '".addslashes($tablepre)."',", $s);
	$ret = file_put_contents(NZCMS_CORE.'/config/config.php', $s);
	js_show('设置 config/config.php ... '.($ret ? '<i>成功</i>' : '<u>失败</u>'));
	if(!$ret) exit;
	// 安装结束提示
	$s = '<div class="end"><h3>恭喜！您的网站已安装完成啦！</h3><p>';
	$s .= '首页地址：<a href="'.$weburl.'" target="_blank">'.$weburl.'</a><br>';
	$s .= '后台地址：<a href="'.$weburl.'admin/" target="_blank">'.$weburl.'admin/</a><br>';
	$s .= '用户名：'.$adm_user.'　<br>密　码：'.$adm_pass.'<br>';
	$s .= '请牢记以上信息，现在可以登陆后台进行管理了</p></div>';
	js_show($s);
}
?>
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	











	














































































































































































































































































































	
<?	
	// 统计一下安装数
	echo '<script type="text/javascript" src="http://www.newzhan.com123/app/?u=index-index-webdomain-'.$webdomain.'-type-1"></script>';
?>
