<?php
switch($db_type)
{
	case 'mysql':install_mysql();break;
	case 'access':install_access();break;
}
function install_mysql()
{
	global $db_host,$db_name,$db_user,$db_pass,$db_prefix;
	$db_host = post('db_host');
	$db_name = post('db_name');
	$db_user = post('db_user');
	$db_pass = post('db_pass');
	$db_prefix = post('db_prefix');
	
	$help = '操作上若有不明之处，可加入RHSB2C用户QQ群寻求其他用户帮助，群号61628043';
	if($db_host == '' || $db_name == '' || $db_user == '')
	{
		echo '数据库信息不足，安装失败！<br />';
		echo $help; exit();
	}elseif(mysql_connect($db_host,$db_user,$db_pass)){
		if(mysql_select_db($db_name))
		{
			echo '连接数据库成功，正在创建数据表...<br />';
		}else{
			echo '数据库'.$db_name.'不存在，正在创建数据库...<br />';
			if(mysql_query('create database '.$db_name))
			{
				echo '创建数据库成功，重新连接数据库...<br />';
				if(!mysql_select_db($db_name))
				{
					echo '连接数据库失败，退出安装！<br />';
					echo $help; exit();
				}
			}else{
				echo '创建数据库失败，退出安装！<br />';
				echo $help; exit();
			}
		}
		if(mysql_query("select * from ".$db_prefix."varia"))
		{
			echo '检测到数据表已存在，安装失败！如果您之前已经安装过一次，重新安装之前请先清空数据库或更换数据表前缀。<br />';
			echo $help; exit();
		}
	}else{
		echo '连接数据库失败，请检查您所填写的数据库信息是否正确。<br />';
		echo $help; exit();
	}
	mysql_query('set names utf8');
	
	//创建数据表
	$sql = "create table if not exists ".$db_prefix."account(
		acc_id int(11) not null primary key auto_increment,
		acc_user_id int(11) default 0,
		acc_type varchar(50),
		acc_sn varchar(50),
		acc_name varchar(50),
		acc_increase int(11) default 0,
		acc_amount double default 0,
		acc_add_time int(11) default 0,
		acc_edit_time int(11) default 0,
		acc_status int(11) default 0
		) engine=myisam default charset=utf8";
	sql_query($sql);
	
	$sql = "create table if not exists ".$db_prefix."admin(
		adm_id int(11) not null primary key auto_increment,
		adm_username varchar(50),
		adm_password varchar(50),
		adm_grade int(11) default 0,
		adm_power text,
		adm_prev_login int(11) default 0,
		adm_last_login int(11) default 0
		) engine=myisam default charset=utf8";
	sql_query($sql);
	
	$sql = "create table if not exists ".$db_prefix."article(
		art_id int(11) not null primary key auto_increment,
		art_cat_id int(11) default 0,
		art_lang varchar(50) default 'none',
		art_type varchar(50),
		art_title varchar(250),
		art_text text,
		art_author varchar(50),
		art_add_time int(11) default 0,
		art_img varchar(250),
		art_keywords varchar(250),
		art_description varchar(250),
		art_hits int(11) default 0,
		art_url varchar(250),
		art_best int(11) default 0,
		art_top int(11) default 0,
		art_index int(11) default 0,
		art_show int(11) default 1		
		) engine=myisam default charset=utf8";
	sql_query($sql);
	
	$sql = "create table if not exists ".$db_prefix."attribute(
		att_id int(11) not null primary key auto_increment,
		att_cat_id int(11) default 0,
		att_lang varchar(50) default 'none',
		att_code varchar(50),
		att_name varchar(50),
		att_top int(11) default 0,
		att_index int(11) default 0,
		att_show int(11) default 1
		) engine=myisam default charset=utf8";
	sql_query($sql);
	
	$sql = "create table if not exists ".$db_prefix."booking(
		boo_id int(11) not null primary key auto_increment,
		boo_user_id int(11) default 0,
		boo_goods_id int(11) default 0,
		boo_lang varchar(50) default 'none',
		boo_number int(11) default 0,
		boo_text text,
		boo_consignee varchar(50),
		boo_email varchar(50),
		boo_tel varchar(50),
		boo_add_time int(11) default 0,
		boo_note varchar(250)
		) engine=myisam default charset=utf8";
	sql_query($sql);
	
	$sql = "create table if not exists ".$db_prefix."brand(
		bra_id int(11) not null primary key auto_increment,
		bra_lang varchar(50) default 'none',
		bra_name varchar(100),
		bra_img varchar(250),
		bra_text text,
		bra_url varchar(250),
		bra_top int(11) default 0,
		bra_index int(11) default 0,
		bra_show int(11) default 1
		) engine=myisam default charset=utf8";
	sql_query($sql);
	
	$sql = "create table if not exists ".$db_prefix."cat_art(
		cat_id int(11) not null primary key auto_increment,
		cat_parent_id int(11) default 0,
		cat_lang varchar(50) default 'none',
		cat_name varchar(100),
		cat_template varchar(50),
		cat_best int(11) default 0,
		cat_top int(11) default 0,
		cat_index int(11) default 0,
		cat_show int(11) default 1
		) engine=myisam default charset=utf8";
	sql_query($sql);
	
	$sql = "create table if not exists ".$db_prefix."cat_goo(
		cat_id int(11) not null primary key auto_increment,
		cat_parent_id int(11) default 0,
		cat_lang varchar(50) default 'none',
		cat_name varchar(100),
		cat_template varchar(50),
		cat_best int(11) default 0,
		cat_top int(11) default 0,
		cat_index int(11) default 0,
		cat_show int(11) default 1
		) engine=myisam default charset=utf8";
	sql_query($sql);
	
	$sql = "create table if not exists ".$db_prefix."collection(
		col_id int(11) not null primary key auto_increment,
		col_user_id int(11) default 0,
		col_goods_id int(11) default 0,
		col_lang varchar(50) default 'none',
		col_add_time int(11) default 0
		) engine=myisam default charset=utf8";
	sql_query($sql);
	
	$sql = "create table if not exists ".$db_prefix."comment(
		com_id int(11) not null primary key auto_increment,
		com_user_id int(11) default 0,
		com_lang varchar(50) default 'none',
		com_type varchar(50),
		com_page_id int(11) default 0,
		com_email varchar(50),
		com_text text,
		com_reply text,
		com_rank int(11) default 0,
		com_add_time int(11) default 0,
		com_show int(11) default 1
		) engine=myisam default charset=utf8";
	sql_query($sql);
	
	$sql = "create table if not exists ".$db_prefix."consignee(
		con_id int(11) not null primary key auto_increment,
		con_user_id int(11) default 0,
		con_lang varchar(50) default 'none',
		con_region varchar(50),
		con_consignee varchar(50),
		con_email varchar(50),
		con_address varchar(250),
		con_zipcode varchar(50),
		con_tel varchar(50),
		con_mobile varchar(50),
		con_building varchar(50),
		con_best_time varchar(50)
		) engine=myisam default charset=utf8";
	sql_query($sql);
	
	$sql = "create table if not exists ".$db_prefix."goods(
		goo_id int(11) not null primary key auto_increment,
		goo_cat_id int(11) default 0,
		goo_lang varchar(50) default 'none',
		goo_title varchar(250),
		goo_sn varchar(50),
		goo_img varchar(250),
		goo_x_img varchar(250),
		goo_more_img text,
		goo_text text,
		goo_market_price double default 0,
		goo_shop_price double default 0,
		goo_brand_id int(11) default 0,
		goo_number int(11) default 0,
		goo_sell int(11) default 0,
		goo_url varchar(250),
		goo_attribute text,
		goo_keywords varchar(250),
		goo_description varchar(250),
		goo_hits int(11) default 0,
		goo_add_time int(11) default 0,
		goo_promotion int(11) default 0,
		goo_new int(11) default 0,
		goo_best int(11) default 0,
		goo_hot int(11) default 0,
		goo_top int(11) default 0,
		goo_index int(11) default 0,
		goo_show int(11) default 1
		) engine=myisam default charset=utf8";
	sql_query($sql);
	
	$sql = "create table if not exists ".$db_prefix."link(
		lin_id int(11) not null primary key auto_increment,
		lin_lang varchar(50) default 'none',
		lin_word varchar(50),
		lin_url varchar(250),
		lin_img varchar(250),
		lin_title varchar(50),
		lin_top int(11) default 0,
		lin_index int(11) default 0,
		lin_show int(11) default 1
		) engine=myisam default charset=utf8";
	sql_query($sql);
	
	$sql = "create table if not exists ".$db_prefix."menu(
		men_id int(11) not null primary key auto_increment,
		men_lang varchar(50) default 'none',
		men_type varchar(50),
		men_name varchar(100),
		men_url varchar(250),
		men_top int(11) default 0,
		men_index int(11) default 0,
		men_show int(11) default 1
		) engine=myisam default charset=utf8";
	sql_query($sql);
	
	$sql = "create table if not exists ".$db_prefix."message(
		mes_id int(11) not null primary key auto_increment,
		mes_user_id int(11) default 0,
		mes_lang varchar(50) default 'none',
		mes_type varchar(50),
		mes_email varchar(50),
		mes_title varchar(100),
		mes_text text,
		mes_reply text,
		mes_add_time int(11) default 0,
		mes_show int(11) default 1
		) engine=myisam default charset=utf8";
	sql_query($sql);
	
	$sql = "create table if not exists ".$db_prefix."orders(
		ord_id int(11) not null primary key auto_increment,
		ord_user_id int(11) default 0,
		ord_lang varchar(50) default 'none',
		ord_sn varchar(50),
		ord_goods_json text,
		ord_region varchar(50),
		ord_consignee varchar(50),
		ord_email varchar(50),
		ord_address varchar(250),
		ord_zipcode varchar(50),
		ord_tel varchar(50),
		ord_mobile varchar(50),
		ord_building varchar(50),
		ord_best_time varchar(50),
		ord_message text,
		ord_note text,
		ord_shipping_id int(11) default 0,
		ord_shipping_name varchar(50),
		ord_payment_id int(11) default 0,
		ord_payment_name varchar(50),
		ord_price_json text,
		ord_price_total double default 0,
		ord_express_no varchar(50),
		ord_add_time int(11) default 0,
		ord_pay_time int(11) default 0,
		ord_shipping_time int(11) default 0,
		ord_status int(11) default 0
		) engine=myisam default charset=utf8";
	sql_query($sql);
	
	$sql = "create table if not exists ".$db_prefix."payment(
		pay_id int(11) not null primary key auto_increment,
		pay_lang varchar(50) default 'none',
		pay_code varchar(50),
		pay_name varchar(100),
		pay_price double default 0,
		pay_text text,
		pay_config text,
		pay_top int(11) default 0,
		pay_index int(11) default 0,
		pay_show int(11) default 1
		) engine=myisam default charset=utf8";
	sql_query($sql);
	
	$sql = "create table if not exists ".$db_prefix."picture(
		pic_id int(11) not null primary key auto_increment,
		pic_lang varchar(50) default 'none',
		pic_type varchar(50),
		pic_title varchar(50),
		pic_path varchar(250),
		pic_url varchar(250),
		pic_site varchar(50),
		pic_text text,
		pic_add_time int(11) default 0,
		pic_top int(11) default 0,
		pic_index int(11) default 0,
		pic_show int(11) default 1
		) engine=myisam default charset=utf8";
	sql_query($sql);
	
	$sql = "create table if not exists ".$db_prefix."region(
		reg_id int(11) not null primary key auto_increment,
		reg_parent_id int(11) default 0,
		reg_lang varchar(50) default 'none',
		reg_name varchar(50),
		reg_top int(11) default 0,
		reg_index int(11) default 0,
		reg_show int(11) default 1
		) engine=myisam default charset=utf8";
	sql_query($sql);
	
	$sql = "create table if not exists ".$db_prefix."users(
		use_id int(11) not null primary key auto_increment,
		use_username varchar(50),
		use_password varchar(50),
		use_real_name varchar(50),
		use_email varchar(50),
		use_qq varchar(50),
		use_tel varchar(50),
		use_phone varchar(50),
		use_address varchar(250),
		use_money double default 0,
		use_birthday int(11) default 0,
		use_sex int(11) default 0,
		use_reg_time int(11) default 0,
		use_prev_login int(11) default 0,
		use_last_login int(11) default 0,
		use_question varchar(50),
		use_answer varchar(50)
		) engine=myisam default charset=utf8";
	sql_query($sql);
	
	$sql = "create table if not exists ".$db_prefix."safe(
		saf_id int(11) not null primary key auto_increment,
		saf_ip varchar(50),
		saf_action varchar(50),
		saf_time int(11) default 0
		) engine=myisam default charset=utf8";
	sql_query($sql);
	
	$sql = "create table if not exists ".$db_prefix."shipping(
		shi_id int(11) not null primary key auto_increment,
		shi_lang varchar(50) default 'none',
		shi_name varchar(100),
		shi_price double default 0,
		shi_text text,
		shi_top int(11) default 0,
		shi_index int(11) default 0,
		shi_show int(11) default 1
		) engine=myisam default charset=utf8";
	sql_query($sql);
	
	$sql = "create table if not exists ".$db_prefix."varia(
		var_id int(11) not null primary key auto_increment,
		var_lang varchar(50) default 'none',
		var_name varchar(50),
		var_value varchar(250),
		var_text text
		) engine=myisam default charset=utf8";
	sql_query($sql);
}
function install_access()
{
	global $conn,$db_name,$db_prefix;
	$random = str_pad(mt_rand(1,pow(10,8) - 1),8,'0',STR_PAD_LEFT);
	$random .= str_pad(mt_rand(1,pow(10,8) - 1),8,'0',STR_PAD_LEFT);
	$random .= str_pad(mt_rand(1,pow(10,8) - 1),8,'0',STR_PAD_LEFT);
	$db_name = '#' . $random . '.mdb';
	$database = '../data/' . $db_name;
	if(!copy('#data.mdb',$database))
	{
		echo '复制文件失败，您的网站目录可能没有写入权限，或者没有启用父路径，<br />';
		echo '如果无法解决，请加入RHSB2C用户QQ群寻求帮助，群号61628043<br />';
		exit();
	}
	$connstr = 'provider=microsoft.jet.oledb.4.0;data source='.realpath($database);
	$conn = new com('adodb.connection',NULL,65001);
	$conn->open($connstr);
	
	//创建数据表
	$sql = "create table ".$db_prefix."account(
		acc_id long identity primary key,
		acc_user_id long default 0,
		acc_type varchar(50),
		acc_sn varchar(50),
		acc_name varchar(50),
		acc_increase long default 0,
		acc_amount double default 0,
		acc_add_time long default 0,
		acc_edit_time long default 0,
		acc_status long default 0
		)";
	sql_query($sql);
	
	$sql = "create table ".$db_prefix."admin(
		adm_id long identity primary key,
		adm_username varchar(50),
		adm_password varchar(50),
		adm_grade long default 0,
		adm_power text,
		adm_prev_login long default 0,
		adm_last_login long default 0
		)";
	sql_query($sql);
	
	$sql = "create table ".$db_prefix."article(
		art_id long identity primary key,
		art_cat_id long default 0,
		art_lang varchar(50) default 'none',
		art_type varchar(50),
		art_title varchar(250),
		art_text text,
		art_author varchar(50),
		art_add_time long default 0,
		art_img varchar(250),
		art_keywords varchar(250),
		art_description varchar(250),
		art_hits long default 0,
		art_url varchar(250),
		art_best long default 0,
		art_top long default 0,
		art_index long default 0,
		art_show long default 1		
		)";
	sql_query($sql);
	
	$sql = "create table ".$db_prefix."attribute(
		att_id long identity primary key,
		att_cat_id long default 0,
		att_lang varchar(50) default 'none',
		att_code varchar(50),
		att_name varchar(50),
		att_top long default 0,
		att_index long default 0,
		att_show long default 1
		)";
	sql_query($sql);
	
	$sql = "create table ".$db_prefix."booking(
		boo_id long identity primary key,
		boo_user_id long default 0,
		boo_goods_id long default 0,
		boo_lang varchar(50) default 'none',
		boo_number long default 0,
		boo_text text,
		boo_consignee varchar(50),
		boo_email varchar(50),
		boo_tel varchar(50),
		boo_add_time long default 0,
		boo_note varchar(250)
		)";
	sql_query($sql);
	
	$sql = "create table ".$db_prefix."brand(
		bra_id long identity primary key,
		bra_lang varchar(50) default 'none',
		bra_name varchar(100),
		bra_img varchar(250),
		bra_text text,
		bra_url varchar(250),
		bra_top long default 0,
		bra_index long default 0,
		bra_show long default 1
		)";
	sql_query($sql);
	
	$sql = "create table ".$db_prefix."cat_art(
		cat_id long identity primary key,
		cat_parent_id long default 0,
		cat_lang varchar(50) default 'none',
		cat_name varchar(100),
		cat_template varchar(50),
		cat_best long default 0,
		cat_top long default 0,
		cat_index long default 0,
		cat_show long default 1
		)";
	sql_query($sql);
	
	$sql = "create table ".$db_prefix."cat_goo(
		cat_id long identity primary key,
		cat_parent_id long default 0,
		cat_lang varchar(50) default 'none',
		cat_name varchar(100),
		cat_template varchar(50),
		cat_best long default 0,
		cat_top long default 0,
		cat_index long default 0,
		cat_show long default 1
		)";
	sql_query($sql);
	
	$sql = "create table ".$db_prefix."collection(
		col_id long identity primary key,
		col_user_id long default 0,
		col_goods_id long default 0,
		col_lang varchar(50) default 'none',
		col_add_time long default 0
		)";
	sql_query($sql);
	
	$sql = "create table ".$db_prefix."comment(
		com_id long identity primary key,
		com_user_id long default 0,
		com_lang varchar(50) default 'none',
		com_type varchar(50),
		com_page_id long default 0,
		com_email varchar(50),
		com_text text,
		com_reply text,
		com_rank long default 0,
		com_add_time long default 0,
		com_show long default 1
		)";
	sql_query($sql);
	
	$sql = "create table ".$db_prefix."consignee(
		con_id long identity primary key,
		con_user_id long default 0,
		con_lang varchar(50) default 'none',
		con_region varchar(50),
		con_consignee varchar(50),
		con_email varchar(50),
		con_address varchar(250),
		con_zipcode varchar(50),
		con_tel varchar(50),
		con_mobile varchar(50),
		con_building varchar(50),
		con_best_time varchar(50)
		)";
	sql_query($sql);
	
	$sql = "create table ".$db_prefix."goods(
		goo_id long identity primary key,
		goo_cat_id long default 0,
		goo_lang varchar(50) default 'none',
		goo_title varchar(250),
		goo_sn varchar(50),
		goo_img varchar(250),
		goo_x_img varchar(250),
		goo_more_img text,
		goo_text text,
		goo_market_price double default 0,
		goo_shop_price double default 0,
		goo_brand_id long default 0,
		goo_number long default 0,
		goo_sell long default 0,
		goo_url varchar(250),
		goo_attribute text,
		goo_keywords varchar(250),
		goo_description varchar(250),
		goo_hits long default 0,
		goo_add_time long default 0,
		goo_promotion long default 0,
		goo_new long default 0,
		goo_best long default 0,
		goo_hot long default 0,
		goo_top long default 0,
		goo_index long default 0,
		goo_show long default 1
		)";
	sql_query($sql);
	
	$sql = "create table ".$db_prefix."link(
		lin_id long identity primary key,
		lin_lang varchar(50) default 'none',
		lin_word varchar(50),
		lin_url varchar(250),
		lin_img varchar(250),
		lin_title varchar(50),
		lin_top long default 0,
		lin_index long default 0,
		lin_show long default 1
		)";
	sql_query($sql);
	
	$sql = "create table ".$db_prefix."menu(
		men_id long identity primary key,
		men_lang varchar(50) default 'none',
		men_type varchar(50),
		men_name varchar(100),
		men_url varchar(250),
		men_top long default 0,
		men_index long default 0,
		men_show long default 1
		)";
	sql_query($sql);
	
	$sql = "create table ".$db_prefix."message(
		mes_id long identity primary key,
		mes_user_id long default 0,
		mes_lang varchar(50) default 'none',
		mes_type varchar(50),
		mes_email varchar(50),
		mes_title varchar(100),
		mes_text text,
		mes_reply text,
		mes_add_time long default 0,
		mes_show long default 1
		)";
	sql_query($sql);
	
	$sql = "create table ".$db_prefix."orders(
		ord_id long identity primary key,
		ord_user_id long default 0,
		ord_lang varchar(50) default 'none',
		ord_sn varchar(50),
		ord_goods_json text,
		ord_region varchar(50),
		ord_consignee varchar(50),
		ord_email varchar(50),
		ord_address varchar(250),
		ord_zipcode varchar(50),
		ord_tel varchar(50),
		ord_mobile varchar(50),
		ord_building varchar(50),
		ord_best_time varchar(50),
		ord_message text,
		ord_note text,
		ord_shipping_id long default 0,
		ord_shipping_name varchar(50),
		ord_payment_id long default 0,
		ord_payment_name varchar(50),
		ord_price_json text,
		ord_price_total double default 0,
		ord_express_no varchar(50),
		ord_add_time long default 0,
		ord_pay_time long default 0,
		ord_shipping_time long default 0,
		ord_status long default 0
		)";
	sql_query($sql);
	
	$sql = "create table ".$db_prefix."payment(
		pay_id long identity primary key,
		pay_lang varchar(50) default 'none',
		pay_code varchar(50),
		pay_name varchar(100),
		pay_price double default 0,
		pay_text text,
		pay_config text,
		pay_top long default 0,
		pay_index long default 0,
		pay_show long default 1
		)";
	sql_query($sql);
	
	$sql = "create table ".$db_prefix."picture(
		pic_id long identity primary key,
		pic_lang varchar(50) default 'none',
		pic_type varchar(50),
		pic_title varchar(50),
		pic_path varchar(250),
		pic_url varchar(250),
		pic_site varchar(50),
		pic_text text,
		pic_add_time long default 0,
		pic_top long default 0,
		pic_index long default 0,
		pic_show long default 1
		)";
	sql_query($sql);
	
	$sql = "create table ".$db_prefix."region(
		reg_id long identity primary key,
		reg_parent_id long default 0,
		reg_lang varchar(50) default 'none',
		reg_name varchar(50),
		reg_top long default 0,
		reg_index long default 0,
		reg_show long default 1
		)";
	sql_query($sql);
	
	$sql = "create table ".$db_prefix."users(
		use_id long identity primary key,
		use_username varchar(50),
		use_password varchar(50),
		use_real_name varchar(50),
		use_email varchar(50),
		use_qq varchar(50),
		use_tel varchar(50),
		use_phone varchar(50),
		use_address varchar(250),
		use_money double default 0,
		use_birthday long default 0,
		use_sex long default 0,
		use_reg_time long default 0,
		use_prev_login long default 0,
		use_last_login long default 0,
		use_question varchar(50),
		use_answer varchar(50)
		)";
	sql_query($sql);
	
	$sql = "create table ".$db_prefix."safe(
		saf_id long identity primary key,
		saf_ip varchar(50),
		saf_action varchar(50),
		saf_time long default 0
		)";
	sql_query($sql);
	
	$sql = "create table ".$db_prefix."shipping(
		shi_id long identity primary key,
		shi_lang varchar(50) default 'none',
		shi_name varchar(100),
		shi_price double default 0,
		shi_text text,
		shi_top long default 0,
		shi_index long default 0,
		shi_show long default 1
		)";
	sql_query($sql);
	
	$sql = "create table ".$db_prefix."varia(
		var_id long identity primary key,
		var_lang varchar(50) default 'none',
		var_name varchar(50),
		var_value varchar(250),
		var_text text
		)";
	sql_query($sql);
}
//红头船
?>