<?
/*
* ===========================================*
* 项目: Ourplus网站流量统计分析系统*
* 版本: Bate1*
* 开发: 看海*
* ===========================================*
* Copyright (c) 2004 - 2005*
* 主页: http://stat.ourplus.com*
* 信箱: fantasysisky@hotmail.com*
* 版权信息: 本软体为共享软体(shareware)提供个人网站免费使用，
                作者看海 (stat.ourplus.com) 保留全部权力，受相关法律和国际
				公约保护，请勿非法修改、转载、散播，或用于其他赢利行为，
				并请勿删除版权声明。*
* ===========================================*
*/
	include_once './include.inc/config.inc.php';
	include_once './include.inc/conn.db.inc.php';
	include_once './include.inc/global.inc.php';
	include_once './include.inc/tpl.inc.php';
	include_once './include.inc/function.php';

	if( is_file( './' . COUNT_DIRNAME . '/count.js' ) || is_file( './' . COUNT_DIRNAME . '/count.php' ) )
	{
		echo './' . COUNT_DIRNAME . '/count.js' . '和' . './' . COUNT_DIRNAME . '/count.php' . '已经存在,安装不能继续.如果需要重新安装,请先删除这两个文件!';
		exit;
	}

	$setup = isset( $_GET[setup] ) ? intval( $_GET[setup] ) : 1;
	$error = false;

	echo "<U><B>欢迎使用 Ourplus Counter !</B></U><br>\n";

	if( $setup == 0 || $setup == 1 )
	{
		echo "现在开始将一步一步帮你安装本统计系统.<br>\n";
		echo "<input type=\"button\" name=\"next\" value=\"下一步\" onclick=\"location='?setup=2';\">\n";
	}
	else if( $setup == 2 )
	{
		echo "<U>现在开始检查目录可写性.</U><br>\n";
		echo WEB_HOME . '/' . COUNT_DIRNAME . "以及当前使用的模板目录下的templates_c目录必须为可写<br>\n";

		echo "<U>开始检查" . WEB_HOME . '/' . COUNT_DIRNAME . "目录</U><br>\n";
		if( is_writable( WEB_HOME . '/' . COUNT_DIRNAME ) )
		{
			echo "&nbsp;&nbsp;&nbsp;&nbsp;确认目录可写!<br>\n";
		}
		else
		{
			$error = true;
			echo "&nbsp;&nbsp;&nbsp;&nbsp;<FONT COLOR=\"#FF0000\">该目录不可写,请将目录权限设置为可写!</FONT><br>\n";
		}

		echo "<U>开始检查" . $Tpl->compile_dir . "目录</U><br>\n";
		if( is_writable( $Tpl->compile_dir ) )
		{
			echo "&nbsp;&nbsp;&nbsp;&nbsp;确认目录可写!<br>\n";
		}
		else
		{
			$error = true;
			echo "&nbsp;&nbsp;&nbsp;&nbsp;<FONT COLOR=\"#FF0000\">该目录不可写,请将目录权限设置为可写!</FONT><br>\n";
		}

		if( $error )
		{
			echo "<FONT COLOR=\"#FF0000\">请更正以上错误后重新安装!</FONT>";
		}
		else
		{
			echo "<input type=\"button\" name=\"next\" value=\"下一步\" onclick=\"location='?setup=3';\">\n";
		}
	}
	else if( $setup == 3 )
	{
		echo "<U>现在开始安装数据库.</U><br>\n";

		$d = dir("./install");
		while (false !== ($entry = $d->read()))
		{
			if ( ereg ("^structure_(.*)\.sql", $entry, $ret ) ) 
			{
				$structure[$ret[1]] = $entry;
			}
			else if( ereg ("^data_(.*)\.sql", $entry, $ret ) )
			{
				$data[$ret[1]] = $entry;
			}

		}
		$d->close();

		foreach( $structure as $key => $val )
		{
			echo '建立数据库' . $key . "<br>\n";
			$content = str_replace( 'PREFIX_' , DB_PREFIX, file_get_contents( './install/' . $val ) );
			mysql_query( $content );
			echo mysql_error();
		}
		
		echo "<br>\n";
		foreach( $data as $key => $val )
		{
			echo '插入数据' . $key . "<br>\n";
			$content = file( './install/' . $val );
			foreach( $content as $sql )
			{
				$sql = str_replace( 'PREFIX_' , DB_PREFIX, $sql );
				mysql_query( $sql );
			}
		}

		echo "<br>\n";
		echo "安装数据库的时候可能会出现一些错误提示,比如如果你是第二次安装,会提示表已经存在的错误信息.如果你确定错误不影响安装,那么请点下一步!<br>\n";
		echo "<input type=\"button\" name=\"next\" value=\"下一步\" onclick=\"location='?setup=4';\"><br>\n";

	}
	else if( $setup == 4 )
	{
		echo "<U>输入管理员账号信息.</U><br>\n";
		echo "<form name=\"form1\" method=\"post\" action=\"?setup=5\">\n";
		echo "管理员:<input name=\"manager\" type=\"text\" id=\"manager\" size=\"16\" maxlength=\"16\"><br>\n";
		echo "密&nbsp;&nbsp;码:<input name=\"password\" type=\"password\" id=\"password\" size=\"16\" maxlength=\"32\"><br>\n";
		echo "重&nbsp;&nbsp;复:<input name=\"repassword\" type=\"password\" id=\"repassword\" size=\"16\" maxlength=\"32\"><br>\n";
		echo "<input type=\"submit\" name=\"next\" value=\"下一步\"><br>\n";
		echo "</form>\n";
	}
	else if( $setup == 5 )
	{
		$manager = trim( strval( $_POST[manager] ) );
		$password = trim( strval( $_POST[password] ) );
		$repassword = trim( strval( $_POST[repassword] ) );

		if( $manager == '' || $password == '' )
		{
			echo "<SCRIPT LANGUAGE=\"JavaScript\">
			<!--
				alert('管理员账号或者密码不能为空!');
				history.back();
			//-->
			</SCRIPT>";
			exit;
		}
		
		if( $repassword != $password )
		{
			echo "<SCRIPT LANGUAGE=\"JavaScript\">
			<!--
				alert('您输入的两次密码不一致!');
				history.back();
			//-->
			</SCRIPT>";
			exit;
		}

		$sql = "TRUNCATE TABLE `" . DB_PREFIX . "users`";
		mysql_query( $sql );
		$sql = "INSERT DELAYED INTO `" . DB_PREFIX . "users` ( `manager`, `password`) VALUES ( '$manager', '" . md5($password) . "');";
		mysql_query( $sql );

		$content = read_from_file( './install/count.js' );
		$content = str_replace( 'COUNTER_INSTALL_URL' , DOMAIN_NAME, $content );

		write_to_file( './' . COUNT_DIRNAME . '/count.js', $content );
		copy( './install/count.php', './' . COUNT_DIRNAME . '/count.php' );

		echo "<U>完成安装</U><br>\n";
		echo "请务必删除 install.php 再使用本统计<br>\n";
		echo "<a href='./'>现在登录</a><br>\n";
	}
?>