<?php
session_start();
header("content-Type: text/html; charset=utf-8");
define('ZIBANMA_SYS_LINK', 'zibanma_com');
include '../config.php';
include '../zibanma/ZibanmaCommon.php';
include '../zibanma/ZibanmaMysql.php';
include '../zibanma/ZibanmaDatabase.php';
$comm = new ZibanmaCommon();
if($_POST){
	$post = $_POST;
	$info = array();
	$conn = mysql_connect($_POST['host'].':3306', $_POST['user'], $_POST['password']) or die("数据库链接失败:数据库帐号或密码错误");
	$selectdb = mysql_select_db($_POST['selectdb'], $conn) or die("数据库错误：数据库名错误或数据库不存在");
	mysql_query("set names utf8");
	if($selectdb){
		$config = file_get_contents('../config-demo.php');
		$config = str_replace('{ZIBANMA_DB_HOST}', $post['host'], $config);
		$config = str_replace('{ZIBANMA_DB_USER}', $post['user'], $config);
		$config = str_replace('{ZIBANMA_DB_PASSWORD}', $post['password'], $config);
		$config = str_replace('{ZIBANMA_DB_SELECTDB}', $post['selectdb'], $config);
		$config = str_replace('{ZIBANMA_DB_PREFIX}', $post['prefix'], $config);
		$status = file_put_contents('../config.php', $config);
		if($status){
			$sql = array();
			$sql[] = "DROP TABLE IF EXISTS `".$post['prefix']."member_user`;";
			$sql[] = "CREATE TABLE `".$post['prefix']."member_user` (
  `id` int(11) NOT NULL auto_increment,
  `user` varchar(255) NOT NULL,
  `password` varchar(255) NOT NULL,
  `level` int(4) default '1',
  `name` varchar(255) default NULL,
  `email` varchar(255) default NULL,
  `tel` varchar(255) default NULL,
  `address` varchar(255) default NULL,
  `count_login_error` int(11) default '0',
  `last_login_time` int(11) default NULL,
  `login_number` int(11) default '0',
  `key` varchar(255) default 'hansangcheng',
  `status` int(1) default '1' COMMENT '状态0删除1正常',
  `manage` int(1) default '0' COMMENT '管理权限0无1有',
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;";
			$sql[] = "DROP TABLE IF EXISTS `".$post['prefix']."news_article`;";
			$sql[] = "CREATE TABLE `".$post['prefix']."news_article` (
  `id` int(11) NOT NULL auto_increment,
  `title` varchar(255) default NULL,
  `keywords` varchar(255) default NULL,
  `description` text,
  `author` varchar(255) default NULL,
  `content` text,
  `category` int(11) default NULL,
  `click` int(11) default NULL,
  `create_user` varchar(255) default NULL,
  `create_time` int(11) default NULL,
  `status` tinyint(4) default '1' COMMENT '状态0删除1正常',
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=29 DEFAULT CHARSET=utf8;";
			$sql[] = "DROP TABLE IF EXISTS `".$post['prefix']."news_category`;";
			$sql[] = "CREATE TABLE `".$post['prefix']."news_category` (
  `id` int(11) NOT NULL auto_increment,
  `fid` int(11) default '0',
  `category_name` varchar(255) default NULL,
  `category_keywords` varchar(255) default NULL,
  `category_description` text,
  `sort` int(11) default '0',
  `status` tinyint(4) default '1' COMMENT '状态0删除1正常',
  `member_submit` tinyint(4) default '0' COMMENT '会员投稿分类0不允许1允许',
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;";
			$sql[] = "DROP TABLE IF EXISTS `".$post['prefix']."pic_album`;";
			$sql[] = "CREATE TABLE `".$post['prefix']."pic_album` (
  `id` int(11) NOT NULL auto_increment,
  `title` varchar(255) default NULL,
  `keywords` varchar(255) default NULL,
  `description` text,
  `content` text,
  `cover` varchar(255) default NULL,
  `click` int(11) default '0',
  `create_user` varchar(255) default NULL,
  `create_time` int(11) default NULL,
  `category` int(11) default NULL,
  `status` int(1) default '1' COMMENT '状态0删除1正常',
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;";
			$sql[] = "DROP TABLE IF EXISTS `".$post['prefix']."pic_category`;";
			$sql[] = "CREATE TABLE `".$post['prefix']."pic_category` (
  `id` int(11) NOT NULL auto_increment,
  `fid` int(11) default '0',
  `category_name` varchar(255) default NULL,
  `category_keywords` varchar(255) default NULL,
  `category_description` text,
  `sort` int(11) default '0',
  `status` tinyint(4) default '1' COMMENT '状态0删除1正常',
  `member_submit` tinyint(4) default '0' COMMENT '会员投稿分类0不允许1允许',
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=utf8;";
			$sql[] = "DROP TABLE IF EXISTS `".$post['prefix']."pic_graph`;";
			$sql[] = "CREATE TABLE `".$post['prefix']."pic_graph` (
  `id` int(11) NOT NULL auto_increment,
  `content` text,
  `keywords` varchar(255) default NULL,
  `description` text,
  `pic_thumb_small` varchar(255) default NULL,
  `pic_thumb_big` varchar(255) default NULL,
  `pic_original` varchar(255) default NULL,
  `create_user` varchar(255) default NULL,
  `create_time` int(11) default NULL,
  `album` int(11) default '0' COMMENT '所属相册',
  `click` int(11) default '0',
  `status` int(1) default '1' COMMENT '状态0删除1正常',
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;";
			$sql[] = "DROP TABLE IF EXISTS `".$post['prefix']."site_cache`;";
			$sql[] = "CREATE TABLE `".$post['prefix']."site_cache` (
  `id` char(255) NOT NULL default '',
  `content` text,
  `create_time` datetime default NULL,
  `create_minute` int(11) default '0' COMMENT '有效时间，单位：分钟',
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;";
			$sql[] = "DROP TABLE IF EXISTS `".$post['prefix']."site_config`;";
			$sql[] = "CREATE TABLE `".$post['prefix']."site_config` (
  `config_id` char(255) NOT NULL default '',
  `config_content` text,
  `describe` text COMMENT '描述',
  PRIMARY KEY  (`config_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;";
			$sql[] = "DROP TABLE IF EXISTS `".$post['prefix']."site_navigation`;";
			$sql[] = "CREATE TABLE `".$post['prefix']."site_navigation` (
  `id` int(11) NOT NULL auto_increment,
  `title` varchar(255) NOT NULL COMMENT '导航名称',
  `target` enum('_self','_blank','_praent') default '_self' COMMENT '打开方式',
  `url` varchar(255) default NULL,
  `sort` int(11) default NULL,
  `attribute` varchar(255) default NULL COMMENT '属性',
  `status` int(1) default NULL COMMENT '状态0删除1正常',
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;";
			$sql[] = "DROP TABLE IF EXISTS `".$post['prefix']."focus`;";
			$sql[] = "CREATE TABLE `".$post['prefix']."focus` (
  `id` int(11) NOT NULL auto_increment,
  `title` varchar(255) default NULL,
  `url` varchar(255) default NULL,
  `create_time` datetime default NULL,
  `sort` int(11) default NULL,
  `target` enum('_self','_blank') default '_self',
  `image` varchar(255) default NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8;";
			$sql[] = "DROP TABLE IF EXISTS `".$post['prefix']."link`;";
			$sql[] = "CREATE TABLE `".$post['prefix']."link` (
  `id` int(11) NOT NULL auto_increment,
  `title` varchar(255) default NULL,
  `url` varchar(255) default NULL,
  `logo` varchar(255) default NULL,
  `index` int(1) default '1' COMMENT '是否首页，1首页0内页',
  `name` varchar(255) default NULL,
  `qq` varchar(255) default NULL,
  `tel` varchar(255) default NULL,
  `status` int(1) default '0' COMMENT '状态1正常0删除',
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;";
			$sql[] = "insert into ".$post['prefix']."member_user (`user`,`password`,`key`,`status`,`manage`) values ('".$post['admin_user']."', '".md5($post['admin_password'])."', 'zibanma', 1, 1)";
			$sql[] = "INSERT INTO `".$post['prefix']."focus` VALUES (NULL, '焦点图1', '/', '2013-01-01 00:00:00', '0', '_blank', '/images/focus_1.jpg')";
			$sql[] = "INSERT INTO `".$post['prefix']."focus` VALUES (NULL, '焦点图2', '/', '2013-01-01 00:00:00', '0', '_blank', '/images/focus_2.jpg')";
			$sql[] = "INSERT INTO `".$post['prefix']."link` VALUES (NULL, '紫斑马软件', 'http://www.zibanma.com', '', '1', '紫斑马软件', '0', '0', '1')";
			foreach ($sql as $v){
				mysql_query($v);
			}
		}else{
			$info[] = '配置文件写入失败，请检查是否具有写入权限。';
		}
	}else{
		$info[] = '数据库配置失败，请检查数据库填写是否正确。';
	}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>紫斑马网站管理系统安装向导</title>
<style type="text/css">
body { margin:0px; padding:0px; width:100%; color:#fff; background-color:#1D517F; font-family: Arial,Helvetica,sans-serif,宋体; font-size: 14px;}
table { width:100%; background-color:#1D517F; }
td { background-color:#006699; text-indent:5px; height:30px;}
input { width:220px; height:22px; line-height:22px;}
#install { width:860px; margin:0px auto;}
#install .body { width:100%; margin-top:50px; box-shadow:#666 0px 0px 10px; float:left;}
</style>
</head>
<body>
<?php if($_POST): sleep(3);?>
<div id="install">
	<div class="body">
		<p style="text-align:center;">安装成功！请牢记您的后台管理帐号及密码 帐号：<?php echo $post['admin_user'];?> 密码：<?php echo $post['admin_password'];?></p>
		<p style="text-align:center;">后台管理地址：<a href="http://<?php echo $_SERVER['HTTP_HOST'];?>/admin_zibanma/" target="_blank" style="color:#ffffff;">http://<?php echo $_SERVER['HTTP_HOST'];?>/admin_zibanma/</a></p>
		<p style="text-align:center;">【特别提示：请立即删除install文件夹，避免留下安全隐患。】</p>
	</div>
</div>
<?php else:?>
<div id="install">
	<div class="body">
		<form action="" method="post">
		<table>
			<tr>
				<td colspan="2" style="text-align:center; font-size:16px; line-height:30px;">紫斑马网站管理系统安装向导</td>
			</tr>
			<tr>
				<td colspan="2" style="text-indent:100px;">数据库设置</td>
			</tr>
			<tr>
				<td style="text-align:right; width:30%;">数据库主机名：</td>
				<td><input type="text" name="host" value="localhost" /></td>
			</tr>
			<tr>
				<td style="text-align:right;">数据库帐号：</td>
				<td><input type="text" name="user" /></td>
			</tr>
			<tr>
				<td style="text-align:right;">数据库密码：</td>
				<td><input type="text" name="password" /></td>
			</tr>
			<tr>
				<td style="text-align:right;">数据库名：</td>
				<td><input type="text" name="selectdb" /></td>
			</tr>
			<tr>
				<td style="text-align:right;">数据表前缀：</td>
				<td><input type="text" name="prefix" value="zibanma_" /></td>
			</tr>
			<tr>
				<td colspan="2" style="text-indent:100px;">管理设置</td>
			</tr>
			<tr>
				<td style="text-align:right;">管理帐号：</td>
				<td><input type="text" name="admin_user" value="admin" /></td>
			</tr>
			<tr>
				<td style="text-align:right;">管理密码：</td>
				<td><input type="text" name="admin_password" value="admin" /></td>
			</tr>
			
			<tr>
				<td>&nbsp;</td>
				<td><input type="submit" value="开始安装" style="width:150px; height:30px;" /></td>
			</tr>
		</table>
		</form>
	</div>
</div>
<?php endif;?>
</body>
</html>