<?
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';
	Initialize( $_GET, array( 'step'=>'intval', 'submit'=>'strval' ,'action'=>'strval','host'=>'strval', 'user'=>'strval' ,'password'=>'strval','dbname'=>'strval','dbcharset'=>'strval','tablepre'=>'strval','tplname'=>'strval','isregister'=>'strval','saveolddays'=>'strval','username'=>'strval','password'=>'strval','repassword'=>'strval','timezone'=>'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=big5" />
<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.0 </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.0 是建立 Web 站點統計分析程序的專業Web軟件。它採用PHP+Mysql+Apache+Zend構造運行環境，將功能強大的站點流量檢測系統部署在您的站點上，全面分析的報告讓您隨時掌握站點運行狀況。無論站點是採用動態還是靜態技術構建，
        PHPStat Counter 都能一一適應，無需另作處理。</p>
        <p>本安裝嚮導將引導您一步一步的進行系統安裝。</p>
        <p>我們提供了一些重要的資源，可幫助您找到有關 PHPStat Counter 1.0 的更多信息。</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] 	    = $_GET[host];
	$databasevalue[user] 	    = $_GET[user];
	$databasevalue[password]    = $_GET[password];
	$databasevalue[dbname] 	    = $_GET[dbname];
	$databasevalue[dbcharset]   = $_GET[dbcharset];
	$databasevalue[tablepre]    = $_GET[tablepre];
	$databasevalue[tplname]     = $_GET[tplname];
	$databasevalue[isregister]  = $_GET[isregister];
	$databasevalue[saveolddays] = $_GET[saveolddays];
	$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("install/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("\[SAVE_OLD_DAYS\]",$databasevalue[saveolddays],$content);
		$content = ereg_replace("\[TIMEZONE\]",$databasevalue[timezone],$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')
	{	
		$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;
		}

		@mysql_query( "TRUNCATE TABLE `" . DB_PREFIX . "users`" );
		@mysql_query( "TRUNCATE TABLE `" . DB_PREFIX . "user_site`" );
		$sql = "INSERT INTO `" . DB_PREFIX . "users` ( `manager`, `password`,`currentsitecode`,`groupname`) VALUES ( '$manager', '" . md5($password) . "','strongsuperadmin','strongsuperadmin');";
		@mysql_query( $sql );
			
		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.0</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>
