<?php
header("content-type:text/html;charset=gbk");
define('ROOT_PATH',str_replace("\\",'/',dirname(dirname(__FILE__))));
define('IN_PHPUP',1);
$step=!empty($_GET['step'])?intval($_GET['step']):1;
if(is_file(ROOT_PATH.'/data/install.lock'))
{
	include ROOT_PATH."/install/template/step8.php"; //安装过了
	exit;
}
switch($step)
{
	case 1:
		include ROOT_PATH."/install/template/step1.php"; //协议说明
	break;
	case 2:
		if($_GET['agree'])
		{
			$filelist=array('data','data/cache','data/upload','data/logo','data/upload');
		
			if(function_exists('gd_info'))
			{
				$v=gd_info();
				$gd_v=$v["GD Version"];
			}
			else
			{
				$gd_v=false;
			}
			$d=dirname(dirname(__FILE__));
			$disk_total=intval(disk_free_space($d)/1024/1024).'M';
			include ROOT_PATH."/install/template/step2.php"; //文件检测
		}
		else
		{
			echo "<script language='javascript'>alert('必须同意协议规定才能继续安装');window.location='index.php?step=1';</script>";
		}
	break;
	case 3:
		if(isset($_POST['error']))
		{
			echo "<script language='javascript'>alert('有未解决的错误');window.location='index.php?step=2&agree=1';</script>";
		}
		else
		{
			include ROOT_PATH."/install/template/step3.php"; //配置信息
		}
	break;
	case 4:
		if($_POST)
		{
			$dbhost=trim(strip_tags($_POST['dbhost']));
			$dbuser=trim(strip_tags($_POST['dbuser']));
			$dbpw=trim(strip_tags($_POST['dbpw']));
			
			//初始化数据连接
			$link=@mysql_connect($dbhost,$dbuser,$dbpw);
			if($link)
			{
			$query=mysql_query('show databases');
			$exists=0;
			while($databases=mysql_fetch_array($query))
			{
				$exi[]=$databases['Database'];
			}
			if(!in_array(trim(strip_tags($_POST['dbname'])),$exi))
			{
				echo "<script language='javascript'>alert('数据库不存在');window.location='index.php?step=3';</script>";
				exit;
			}
			elseif(is_file('install.sql') && is_readable('install.sql'))
			{
$sql=readf('install.sql');

$config=<<<EOF
<?php
/*
	[Phpup.Net!] (C)2009-2011 Phpup.net.
	This is NOT a freeware, use is subject to license terms

	\$Id: config.inc.php 2010-08-24 10:42 \$
*/

if(!defined('IN_PHPUP')) {
	exit('Access Denied');
}
//数据库服务器
\$dbhost="{dbhost}";\n
//数据库用户名
\$dbuser="{dbuser}";\n
//数据库密码
\$dbpw="{dbpw}";\n
//数据库名
\$dbname="{dbname}";\n
//数据库编码
\$dbcharset="gbk";\n
//数据表前缀
\$table_prefix="{table_prefix}";\n
//网站编码
\$charset="gbk";\n
//session前缀
\$session_prefix='phpup_';\n
EOF;
				$config=str_replace('{dbhost}',$dbhost,$config);
				$config=str_replace('{dbuser}',$dbuser,$config);
				$config=str_replace('{dbpw}',$dbpw,$config);
				$config=str_replace('{dbname}',trim(strip_tags($_POST['dbname'])),$config);
				$config=str_replace('{table_prefix}',trim(strip_tags($_POST['table_prefix'])),$config);
				if(function_exists('file_put_contents'))
				{
					file_put_contents(ROOT_PATH.'/data/config.inc.php',$config);
				}
				else
				{
					$fp=fopen(ROOT_PATH.'/data/config.inc.php','w');
					fwrite($fp,$config);
					fclose($fp);
				}
				$sql=str_replace('{tableprefix}',trim(strip_tags($_POST['table_prefix'])),str_replace('{dataname}',trim(strip_tags($_POST['dbname'])),$sql));
				$sql=explode(';',$sql);
				include ROOT_PATH."/install/template/step4.php"; //安装数据库
				$error='';
				foreach($sql as $k=>$v)
				{
					if(mysql_query($v))
					{
						$error.=!mysql_error()?'数据表创建成功\r\n':mysql_error();
					}
				}
				echo "<script language='javascript'>document.getElementById('user_login').value='".$error."';document.getElementById('wp-submit').disabled='';</script>";
			}
			}
			
			else
			{
				echo "<script language='javascript'>alert('数据库连接错误');window.location='index.php?step=3';</script>";
				exit;
			}
		}
		else
		{
			include ROOT_PATH."/install/template/step3.php"; //配置信息
		}
		echo "</body></html>";
	break;
	case 6:
		include ROOT_PATH."/install/template/step6.php"; //协议说明
	break;
	case 7:
		include ROOT_PATH."/data/config.inc.php"; //配置信息
		$link=mysql_connect($dbhost,$dbuser,$dbpw);
		mysql_query('use `'.$dbname.'`');
		mysql_query('set names '.$dbcharset);
		if(mysql_error())
		{

			echo "<SCRIPT LANGUAGE='JavaScript'>window.location='index.php';</SCRIPT>";
		}
		else
		{
			$sql[]="insert into {$table_prefix}setting(variable,content) values('site_title','".addslashes(trim(strip_tags($_POST['site_title'])))."'),('site_url','".addslashes(trim(strip_tags($_POST['site_url'])))."'),('site_imgurl','".$_POST['img_url']."'),('site_charset','gbk'),('adminpath','admin'),('site_logo','data/logo/logo.jpg'),('site_template_dir','default4'),('site_cache_dir', 'data/cache'),('site_upload_dir', 'data/upload'),('site_copyright','Copyright 团购窝窝.All Rights Reserved. '),('site_benai','京ICP备11021038号'),('site_group_count','18');";
			
			$sql[]="INSERT INTO `{$table_prefix}user` (`email`, `phone`, `sex`, `qq`, `address`, `zip`, `pwd`, `connect_user`, `connect_content`, `updatetime`, `usertype`, `logtime`, `username`, `allowed`, `money`) VALUES('server@lht.com', 'xxx', 1, '', NULL, NULL, '".md52($_POST['adminpassword'])."', '蓝海豚', '蓝海豚', ".time().", 'adminuser', 0, '".addslashes(trim(strip_tags($_POST['administr'])))."', 1, 0.00)";
			$sql[]="INSERT INTO `phpup_ad` (`id`, `title`, `content`, `starttime`, `endtime`, `adindex`) VALUES(1, '百度管家', '<script type=\"text/javascript\" src=\"http://cbjs.baidu.com/js/m.js\"></script>\r\n<script type=\"text/javascript\">\r\nBAIDU_CLB_preloadSlots(\"238965\");\r\n</script>\r\n<script type=\"text/javascript\">BAIDU_CLB_fillSlot(\"238965\");</script>', 0, 0, 'html-headbanner');";
			if(!empty($sql))
			{
				foreach($sql as $v)
				{
					mysql_query($v) or die(mysql_error());
				}
			}
			touch(ROOT_PATH.'/data/install.lock');
			include ROOT_PATH."/install/template/step7.php"; //协议说明
		}
	break;
	default:
		include ROOT_PATH."/install/template/step1.php"; //协议说明
	break;
}

//md5加密
function md52($str)
{
	$str=substr(md5($str),3,20);
	return $str;
}

//读取文件内容
function readf($file)
{
	if(function_exists('file_get_contents'))
	{
		$content=file_get_contents($file);
	}
	else
	{
		$fp=fopen($file,'r');
		while(!feof($fp))
		{
			$content=fgets($fp,1024);
		}
		fclose($fp);
	}
	return $content;
}
