<?
ob_start(); 
if(function_exists(session_cache_limiter))
{
	session_cache_limiter("private, must-revalidate"); 
}
?>
<?
	ini_set('max_execution_time',5000);
	include_once './include.inc/config.inc.php';
	include_once './include.inc/global.inc.php';
	include_once './include.inc/tpl.inc.php';
	include_once './include.inc/function.php';
	$user = trim($_GET[user]);
	$password = trim($_GET[password]);
	$repassword = trim($_GET[repassword]);
	Initialize( $_GET, array( 'step'=>'intval', 'submit'=>'strval' ,'action'=>'strval','host'=>'strval', 'user'=>'strval' ,'dbname'=>'strval','dbcharset'=>'strval','tablepre'=>'strval','tplname'=>'strval','isregister'=>'strval','pnumber'=>'strval','checkcode'=>'strval','username'=>'strval','password'=>'strval','repassword'=>'strval','timezone'=>'strval','installtype'=>'strval') );
	//错误文件
	$fp = fopen("错误日志.log","a+");
	
	if( file_exists( './' . COUNT_DIRNAME . '/count.js' ) || file_exists( './' . COUNT_DIRNAME . '/count.php' ) )
	{
		echo '<div class=heading>./' . COUNT_DIRNAME . '/count.js' . '和' . './' . COUNT_DIRNAME . '/count.php' . '已经存在,安装不能继续.如果需要重新安装,请先删除这两个文件!</div>';
		exit;
	}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<STYLE type=text/css>
BODY {
	FONT-SIZE: 12px; FONT-FAMILY: Geneva, Arial, Helvetica, san-serif
}
TD {
	FONT-SIZE: 12px; FONT-FAMILY: Geneva, Arial, Helvetica, san-serif
}
.welcome {
	FONT-WEIGHT: normal; FONT-SIZE: 28px; FONT-FAMILY: Verdana, Arial, Helvetica, sans-serif
; color: #333399}
.heading { FONT-WEIGHT: bold; FONT-SIZE: 12px; FONT-FAMILY: Geneva, Arial, Helvetica, san-serif; color: #333399 }
.button {
	background: #006699;
	border-top: 1px solid #FF9B9D;
	border-right: 1px solid #4D0002;
	border-bottom: 1px solid #4D0002;
	border-left: 1px solid #FF9B9D;
	color: #FFF;

}
input {
	font-family: Geneva, Arial, Helvetica, san-serif;
	font-size:12px;
	background: #ffffff;
	border-top: 1px solid #cccccc;
	border-right: 1px solid #cccccc;
	border-bottom: 1px solid #cccccc;
	border-left: 1px solid #cccccc;
	color: #000;

}
</STYLE>
<title>PHPStat <?=$SoftWareVersion?> 安装程序 </title>
</head>

<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td align="center" class="welcome">欢迎使用<span class="leftfont_1"> PHPStat Counter 1.2 </span></td>
  </tr>
</table>
<br />
<?
if( $_GET[step] == '' )
{
?>
<table cellspacing="2" cellpadding="1" width="670" align="center" border="0">
  <tbody>
    <tr>
      <td height="74"><p>PHPStat Counter 1.2 是建立 Web 站点统计分析程序的专业Web软件。它采用PHP+Mysql+Apache+Zend构造运行环境，将功能强大的站点流量检测系统部署在您的站点上，全面分析的报告让您随时掌握站点运行状况。无论站点是采用动态还是静态技术构建，
        PHPStat Counter 都能一一适应，无需另作处理。</p>
        <p>本安装向导将引导您一步一步的进行系统安装。</p>
        <p>我们提供了一些重要的资源，可帮助您找到有关 PHPStat Counter 1.2 的更多信息。</p>
        <p>安装步骤：<hr align="left" size="1" width=100%/></p></td>
    </tr>
    <tr>
      <td height="18"><blockquote>
        <p> <span class="heading">检查参数设置</span><br />
          请务必设置您拥有的Mysql帐号信息，准确填写有助您安装的顺利进行。</p>
        <p><span class="heading">检查目录属性</span><br />
          PHPStat Counter需要进行文件的读写操作，必须相应拥有目录的777属性。</p>
        <p><span class="heading">安装数据库</span><br />
          PHPStat Counter进行数据初始化安装，主要是IP地址库、Unicode编码库以及设置创建人帐号。</p>
        <p><span class="heading">安装完成</span><br />
PHPStat Counter删除安装文件，确保系统安全性。</p>
        
      </blockquote>
        <p>开始安装：
        <hr align="left" size="1" width="100%"/>
        <table width="100%" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td align="center"><input name="Submit" type="button" class="button" onclick="window.location='install.php?step=1'" value="开　始　安　装"></td>
          </tr>
        </table>
        <hr align="left" size="1" width="100%"/>
      </p></td>
    </tr>
  </tbody>
</table>
<?
}

if( $_GET[step] == '1' )
{
	include_once 'install/step_1.php';
}

elseif( $_GET[step] == '2' )
{	
	include_once 'install/step_2.php';
}

elseif( $_GET[step] == '3' and $_GET[submit] == '')
{	
	$databasevalue = array();
	$databasevalue[host] 	    = strtolower($_GET[host]);
	$databasevalue[user] 	    = ($user);
	$databasevalue[password]    = ($password);
	$databasevalue[dbname] 	    = strtolower($_GET[dbname]);
	$databasevalue[dbcharset]   = $_GET[dbcharset];
	$databasevalue[tablepre]    = strtolower($_GET[tablepre]);
	$databasevalue[tplname]     = $_GET[tplname];
	$databasevalue[isregister]  = $_GET[isregister];
	$databasevalue[checkcode]   = strtoupper($_GET[checkcode]);
	$databasevalue[pnumber]		= strtoupper($_GET[pnumber]);
	$databasevalue[timezone]    = $_GET[timezone];
	$iserror = false;
	
	$r		 = @mysql_connect($databasevalue[host],$databasevalue[user],$databasevalue[password]);
	$vrs	 = @mysql_query("select version() as version");
	$vrsrow  = @mysql_fetch_array($vrs);
	$Version = explode('.',$vrsrow[version]);
	$Versionint = $Version[0];
	$Versionint .= $Version[1];
	if( $Versionint >= 41 )
	{
		@mysql_query("CREATE DATABASE `".$databasevalue[dbname]."` DEFAULT CHARACTER SET ".$databasevalue[dbcharset]." COLLATE ".$databasevalue[dbcharset]."_chinese_ci;");
	}
	else
	{
		@mysql_query("CREATE DATABASE `".$databasevalue[dbname]."`");	
	}	
		@mysql_select_db($databasevalue[dbname],$r);
	if( ! $r )
	{
		$errors = "<div class=heading>连接数据库失败,请正确配置数据库信息!</div>";
		$errors .= "<br><input name=\"Submit\" type=\"button\" class=\"button\" onclick=\"window.history.go(-1)\" value=\"返回修改\" />";
		$iserror = true;		
	}
	$r2 = @mysql_select_db($databasevalue[dbname],$r);
	if( ! $r2 )
	{
		$errors2 = "<div class=heading>选择数据库失败,请正确填写数据库名称信息!</div>";
		$errors2 .= "<br><input name=\"Submit\" type=\"button\" class=\"button\" onclick=\"window.history.go(-1)\" value=\"返回修改\" />";
		$iserror = true;		
	}
	if($iserror == false)
	{		
		$content = @file_get_contents(COUNT_DIRNAME."/js/config.inc.php");
		$content = ereg_replace("\[DB_HOSTNAME\]",$databasevalue[host],$content);
		$content = ereg_replace("\[DB_USERNAME\]",$databasevalue[user],$content);
		$content = ereg_replace("\[DB_PASSWORD\]",$databasevalue[password],$content);
		$content = ereg_replace("\[DB_DATABASE\]",$databasevalue[dbname],$content);
		$content = ereg_replace("\[DB_CHARSET\]",$databasevalue[dbcharset],$content);
		$content = ereg_replace("\[DB_PREFIX\]",$databasevalue[tablepre],$content);
		$content = ereg_replace("\[TPL_NAME\]",$databasevalue[tplname],$content);
		$content = ereg_replace("\[IS_REGISTER\]",$databasevalue[isregister],$content);
		$content = ereg_replace("\[CHECK_CODE\]",$databasevalue[checkcode],$content);
		$content = ereg_replace("\[PRODUCT_NUMBER\]",$databasevalue[pnumber],$content);
		$content = ereg_replace("\[TIMEZONE\]",$databasevalue[timezone],$content);
		$content = ereg_replace("\[SHOW_TEXT1\]",'PHPSTAT 统计',$content);
		$content = ereg_replace("\[SHOW_TEXT2\]",'PHPSTAT.NET',$content);
		$content = ereg_replace("\[TITLE_SHOW\]",'PHPSTAT 统计',$content);
		$localdomain = 'http://' . $_SERVER["HTTP_HOST"] . dirname( $_SERVER["PHP_SELF"] );
		if(substr($localdomain,-1) == '\\')
		{
			$localdomain = substr($localdomain,0,-1);
		}
		$content = ereg_replace("\[COUNT_URL\]",$localdomain,$content);
		
		$fp = fopen("include.inc/config.inc.php", "w+");
		if (flock($fp, LOCK_EX)) { 
			fwrite($fp, $content);
			flock($fp, LOCK_UN); 
		} 
		fclose($fp);
		$insert = "<div class=heading>数据库信息配置完整，进行数据库安装</div>";
		$insert .= "<br><br><input name=\"Submit\" type=\"button\" class=\"button\" onclick=\"window.location='install.php?step=3&submit=yes'\" value=\"安　装　数　据　库\">";
	}
	include_once 'install/step_3.php';
}

elseif( $_GET[step] == '3' and $_GET[submit] == 'yes')
	{	
		$r = @mysql_connect(DB_HOSTNAME,DB_USERNAME,DB_PASSWORD);
		@mysql_select_db(DB_DATABASE,$r);
		@mysql_query("SET charset '".DB_CHARSET."'");
		include_once 'install/step_3_2.php';
	}
elseif( $_GET[step] == '4' and $_GET[submit] == '')
	{
		include_once 'install/step_4.php';
	}
elseif( $_GET[step] == '4' and $_GET[submit] == 'yes')
	{	
		include_once 'tools/mkfiles.php';	

		$r = @mysql_connect(DB_HOSTNAME,DB_USERNAME,DB_PASSWORD);		
		@mysql_select_db(DB_DATABASE,$r);
		@mysql_query("SET charset '".DB_CHARSET."'");
		$manager = trim( strval( $_GET[username] ) );
		$password = trim( strval( $_GET[password] ) );
		$repassword = trim( strval( $_GET[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;
		}
		
		if( $_GET[installtype] == "new" )
		{
			@mysql_query( "TRUNCATE TABLE `" . DB_PREFIX . "users`" );
			@mysql_query( "TRUNCATE TABLE `" . DB_PREFIX . "user_site`" );
		}
		else
		{
			
			@mysql_query( "DELETE FROM `" . DB_PREFIX . "users` where groupname = 'superstrongadmin'" );
		}

		$sql = "INSERT INTO `" . DB_PREFIX . "users` ( `manager`, `password`,`currentsitecode`,`groupname`) VALUES ( '$manager', '" . md5($password) . "','strongsuperadmin','strongsuperadmin');";
		@mysql_query( $sql );

		$sql = "SELECT * FROM `" . DB_PREFIX . "site";
		$res = mysql_query( $sql );
		while( $row = mysql_fetch_array($res) )
		{
			$IpArray = @explode("|",$row[exclusionip]);
			$IpArray = @array_unique ($IpArray);
			if( count($IpArray) > 1 )
			foreach( $IpArray as $Key => $Value )
			{	
				
				$ValueArray = @explode( '.', $Value );
				if( $ValueArray[3] == "" )
				{
					$ValueStart = $Value.'0';
					$ValueEnd	= $Value.'255';
				}
				else
				{
					$ValueStart = $Value;	
					$ValueEnd	= $Value;
				}
				$exclusionip[startip]  = ip2num($ValueStart);
				$exclusionip[endip]    = ip2num($ValueEnd);
				$exclusionipArray[]    = $exclusionip;
			}
			else
			{	
				$ValueArray = @explode( '.', $row[exclusionip] );
				if( $ValueArray[3] == "" )
				{
					$ValueStart = $row[exclusionip].'0';
					$ValueEnd	= $row[exclusionip].'255';
				}
				else
				{
					$ValueStart = $row[exclusionip];	
					$ValueEnd	= $row[exclusionip];
				}
				$exclusionip[startip]  = ip2num($ValueStart);
				$exclusionip[endip]    = ip2num($ValueEnd);
				$exclusionipArray[]	   = $exclusionip;
			}

			$fileStr = "<?\nif( !defined('WEB_HOME') ) exit('Access Denied');\n";
			$fileStr .= "if( file_exists( \"../count/exclusion/website_\".\$websiteid.\"_regexp.php\")  )\n";
			$fileStr .= "include_once \"../count/exclusion/website_\".\$websiteid.\"_regexp.php\";\n";
			$fileStr .= "\n//程序排除IP地址列表\n";	
			$fileStr .= "\n//统计网站地址\n";	
			$fileStr .= "\$siteurl = \"$row[site]\";\n";	
			for($i=0;$i < count($exclusionipArray);$i++)
			{	
				if($exclusionipArray[$i]['startip'] != "0" and $exclusionipArray[$i]['endip'] != "0")
				{
					$fileStr .= "\$exclusionip[$i][startip] = \"".$exclusionipArray[$i]['startip']."\";\n";
					$fileStr .= "\$exclusionip[$i][endip]   = \"".$exclusionipArray[$i]['endip']."\";\n";
				}
			}
			
			$DomainArray = @explode("|",$row[exclusioninter]);
			$DomainArray = @array_unique ($DomainArray);
			if( count($DomainArray) > 1 )
			foreach( $DomainArray as $Key => $Value )
			{				
				$exclusioninterArray[] = $Value;
			}
			else
			{	
				$exclusioninterArray[]	= $row[exclusioninter];
			}		
			$exclusioninterArray = @array_unique ($exclusioninterArray);

			$fileStr .= "\n//程序包含目录地址列表\n";		
			for($i=0;$i < count($exclusioninterArray);$i++)
			{
				if($exclusioninterArray[$i] != "" and $exclusioninterArray[$i] != "0")
				$fileStr .= "\$exclusioninter[$i] = \"".$exclusioninterArray[$i]."\";\n";
			}
			$fileStr .= "\n//程序所属主站点代码\n";
			if( $row[sitetype] == '0' or $row[sitetype] == '' )
			{					
				$fileStr .= "\$mainsitecode = \"".$row[website]."\";\n";
				$row[sitetype] = '0';
			}
			else
			{
				$fileStr .= "\$mainsitecode = \"".$row[sitetype]."\";\n";
			}
			$fileStr .= "?>";
			$fileStr1 = $fileStr;

			if( $row[sitetype] == '0' )
			{
				unset($SiteListArray);
				$ResList = mysql_query( "select website from $TableList[site] where sitetype = '$row[website]'");
					while( $TmpList = mysql_fetch_array($ResList) )
					{
						$SiteListArray[] = $TmpList[website];
					}
					$SiteListArray[] = $row[website];
					//$SiteListArray[] = $row[website];
					$SiteListStr = @implode('|',$SiteListArray);
					$fileStr = "<?\nif( !defined('WEB_HOME') ) exit('Access Denied');\n";		
					$fileStr .= "\n//程序主站所包含子站列表\n";	
					$fileStr .= "\$mainsiteregexp = \"$SiteListStr\";\n";	
					$fileStr .= "?>";
					$fileStr2 = $fileStr;
					
			}			
			write_to_file(COUNT_DIRNAME."/exclusion/website_".$row[id].".php",$fileStr1,'w+');
			write_to_file(COUNT_DIRNAME."/exclusion/website_".$row[id]."_regexp.php",$fileStr2,'w+');
			$website = $row[website];
			$replace[$website] = $row[id];
			

		}
		if( count($replace) > 0 )
		foreach( $replace as $key => $value )
		{
			$replacearray .= "\$WebSiteId['$key'] = \"$value\";\r\n";	
		}		
		$content = @read_from_file(COUNT_DIRNAME."/js/websiteid.php");
		$content = ereg_replace("\[WEBSITEID\]",$replacearray,$content);
		write_to_file(COUNT_DIRNAME."/websiteid.php",$content);
		
			
		include_once 'install/step_5.php';
		

		@rename('index.html','indexhtml.lock');	
		@rename('default.html','index.html');	
}


?>
<table width="670" border="0" align="center" cellpadding="1" cellspacing="2">
  <tr>
    <td><table cellpadding="1" cellspacing="2">
      <tr>
        <td width="223" class="heading"><div align="justify">PHPStat Counter 1.2</div></td>
        <td width="223" class="heading"><div align="justify">技术支持</div></td>
        <td width="224" class="heading"><div align="justify">在线资源</div></td>
      </tr>
    </table></td>
  </tr>
  <tr>
    <td><hr align="left" size="1" /></td>
  </tr>
  <tr>
    <td><?include_once 'install/footer.php';?></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td><p>&nbsp;</p></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
  </tr>
</table>
</body>
</html>
