<?
/*
 
 本软件版权归作者所有,在投入使用之前注意获取许可
 作者：程威（wiwiboy）
 项目：PHPStat流量统计 1.0
 电话：13553166317
 邮箱：wiwiboy@126.com
 QQ号：158839753
 网址：http://www.phpstat.net
 phpstat.net保留全部权力，受相关法律和国际		  		
		公约保护，请勿非法修改、转载、散播，或用于其他赢利行为，		
		并请勿删除版权声明。
*/
/*	
	verson:v1.2 2007-3-23
	author:wiwiboy
	last update:2007-3-23
	describe:编辑用户信息 
*/
	session_start();	
	include_once './include.inc/config.inc.php';
	include_once './include.inc/global.inc.php';
	include_once './session.php';
	include_once './include.inc/conn.db.inc.php';
	include_once './include.inc/tpl.inc.php';
	include_once './include.inc/function.php';
	include_once './parse_site.php';
	$manager	  = $_SESSION[SESSION_MANAGER];
	$managergroup = $_SESSION[SESSION_USERGROUP];
	$managercode  = $_SESSION[SESSION_USERGROUP_CODE];	
	Initialize( $_GET, array( 'action'=>'strval' ,'managerid'=>'strval','website'=>'strval'));
	Initialize( $_POST, array( 'managerid'=>'intval' ,'password'=>'strval','sitename'=>'strval','website'=>'strval','sitedes'=>'strval','action'=>'strval','websiteid'=>'intval','site'=>'strval','sitetype'=>'strval','siteshow'=>'strval','qq'=>'strval','email'=>'strval','msn'=>'strval','group'=>'strval','exclusioninter'=>'strval','exclusionip'=>'strval') );
	

	
	unset($Res);
	unset($Tmp);

	$website = $_GET[website];	
	if($managergroup != 'admin' and $managergroup != 'superadmin' and $managergroup != 'strongsuperadmin')
	{
		$msg ="<div align=center>你的此次操作被限制。如果您是管理员，请点击`<a href=login.php>[这里]</a>`登录<BR>";
		$goback = "<a href=# onclick=\"history.go(-1)\">请您返回</a></div>";
		showMsg ( $msg , $goback );
	}
	
	if($_GET[action] == "") $_GET[action] = "common";
	//编辑用户资料
	if($_GET[action] == "edituser")
	{
		$Res = $Conn->Execute( " select * from  $TableList[users] where manager = '$manager' ");
		$Tmp = $Res->FetchRow();
		$Tpl->assign( 'UserData', $Tmp );
		$Tpl->assign( 'selectMode', 'user' );
		$Tpl->assign( 'Main', $Tpl->fetch( 'user_info.html' ) . $ScriptCode );
		$Tpl->assign( 'Title', '修改帐户资料 - '.$SoftWareName .$SoftWareVersion  );
		$Tpl->assign( 'NowView', '修改帐户资料' );
	}
	//更改用户资料
	if($_POST[action] == "updateuser")
	{	
		
		if(trim($_POST[password]) != "") 
		{	
			$_POST[password] = md5($_POST[password]);
			$password = "password = '$_POST[password]',";
		}
		else
			$password = "";
		$Conn->Execute( " update $TableList[users] set $password qq = '$_POST[qq]', msn = '$_POST[msn]',email = '$_POST[email]' where id = '$_POST[managerid]'");
		header("location:user_info.php?website=true&action=edituser&managerid=$_GET[managerid]");
	}
	//用户站点列表
	if($_GET[action] == "sitelist")
	{
		$Res = $Conn->Execute( " select site,sitename,website,all_count,all_count_ip from  $TableList[site] where managerid = '$manager'");
		while($Tmp = $Res->FetchRow())
		{
			$Datas[] = $Tmp;
		}
		$Tpl->assign( 'UserData', $Datas );
		$Tpl->assign( 'selectMode', 'sitelist' );
		$Tpl->assign( 'Main', $Tpl->fetch( 'user_info.html' ) . $ScriptCode );	
		$Tpl->assign( 'Title', '您的站点列表 - '.$SoftWareName .$SoftWareVersion  );
		$Tpl->assign( 'NowView', '您的站点列表' );
	}
	//添加用户站点
	if($_GET[action] == "addsite")
	{	
		$ResSite = $Conn->Execute( " select websiteid from $TableList[user_site] where managerid = '$manager'");
		while($TmpSite = $ResSite->FetchRow())
		$WebsiteID[] = $TmpSite[websiteid];
		$Where = @implode("|",$WebsiteID);
		if( count($Where) > 0)
		{
			$WhereStr = " website REGEXP '".$Where."' or ";
		}
		$Res = $Conn->Execute(" select sitename,website from $TableList[site] where ($WhereStr managerid = '$manager') and sitetype = '0'");
		if( $managergroup == 'superadmin' )
		{
			$Res = $Conn->Execute(" select sitename,website from $TableList[site] where  msn = '$managercode' and sitetype = '0'");
		}
		if( $managergroup == 'strongsuperadmin')
		{
			$Res = $Conn->Execute(" select sitename,website from $TableList[site] where  sitetype = '0'");
		}
		while($Tmp = $Res->FetchRow())
		{
			$SiteType[]	= $Tmp;		
		}
		$Tpl->assign( 'sitetype', $SiteType );
		$Tpl->assign( 'selectMode', 'site' );
		$Tpl->assign( 'action', 'insertsite' );
		$Tpl->assign( 'submit', '添加' );
		$Tpl->assign( 'Main', $Tpl->fetch( 'user_info.html' ) . $ScriptCode );
		$Tpl->assign( 'Title', '添加下属站点 - '.$SoftWareName .$SoftWareVersion  );
		$Tpl->assign( 'NowView', '添加下属站点' );
	}
	//添加用户站点
	if($_POST[action] == "insertsite")
	{	
		$Res = $Conn->Execute(" select * from $TableList[site] where website = '$_POST[website]'");
		if($Res->RecordCount() > 0)
		{
			$msg ="<div align=center>站点代码相同<BR>";
			$goback = "<a href=# onclick=\"history.go(-1)\">请您返回修改</a></div>";
			showMsg ( $msg , $goback );
		}
		else
		{	
			$IpArray = @explode("|",$_POST[exclusionip]);
			$IpArray = @array_unique ($IpArray);
			if( count($IpArray) > 1 )
			foreach( $IpArray as $Key => $Value )
			{	
				//unset($ValueStart);unset($ValueEnd);
				$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( '.', $_POST[exclusionip] );
				if( $ValueArray[3] == "" )
				{
					$ValueStart = $_POST[exclusionip].'0';
					$ValueEnd	= $_POST[exclusionip].'255';
				}
				else
				{
					$ValueStart = $_POST[exclusionip];	
					$ValueEnd	= $_POST[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 = \"$_POST[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("|",$_POST[exclusioninter]);
			$DomainArray = @array_unique ($DomainArray);
			if( count($DomainArray) > 1 )
			foreach( $DomainArray as $Key => $Value )
			{				
				$exclusioninterArray[] = $Value; 							
			}
			else
			{	
				$exclusioninterArray[]	= $_POST[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( $_POST[sitetype] == '0' or $_POST[sitetype] == '' )
			{					
				$fileStr .= "\$mainsitecode = \"".$_POST[website]."\";\n";
				$_POST[sitetype] = '0';
			}
			else
			{
				$fileStr .= "\$mainsitecode = \"".$_POST[sitetype]."\";\n";
			}
			$fileStr .= "?>";
			$fileStr1 = $fileStr;
			
			
			if( $_POST[sitetype] != '0' )
			{
				$ResList = $Conn->Execute( "select website from $TableList[site] where sitetype = '$_POST[sitetype]'");
					while( $TmpList = $ResList->FetchRow() )
					{
						$SiteListArray[] = $TmpList[website];
					}
					$SiteListArray[] = $_POST[sitetype];
					$SiteListArray[] = $_POST[website];
					$SiteListStr = @implode('|',$SiteListArray);
					$fileStr = "<?\nif( !defined('WEB_HOME') ) exit('Access Denied');\n";		
					$fileStr .= "\n//程序主站所包含子站列表\n";	
					$fileStr .= "\$mainsiteregexp = \"$SiteListStr\";\n";	
					$fileStr .= "?>";
					$fileStr2 = $fileStr;
					
			}
			$_POST[website] = trim( strtolower( $_POST[website] ) );
			$r = $Conn->Execute( " insert into $TableList[site](managerid,site,sitename,website,sitedes,sitetype,siteshow,msn,exclusionip,exclusioninter) Values('$manager','$_POST[site]','$_POST[sitename]','$_POST[website]','$_POST[sitedes]','$_POST[sitetype]','$_POST[siteshow]','$managercode','$_POST[exclusionip]','$_POST[exclusioninter]')");
			$websiteid = $Conn->Insert_ID();
			if( !file_exists( COUNT_DIRNAME."/exclusion/website_".$websiteid.".php" ) )
			{
				Error( '无法写入站点文件 website_'.$websiteid.'.php , Error code :<A HREF="http://www.phpstat.net/Errorcode.php#005" target=_blank>005</A> line:'.__LINE__,"count/" );
			}
			write_to_file(COUNT_DIRNAME."/exclusion/website_".$websiteid.".php",$fileStr1,'w+');

			if( !file_exists( COUNT_DIRNAME."/exclusion/website_".$websiteid."_regexp.php" ) )
			{
				Error( '无法写入站点文件 website_'.$websiteid.'_regexp.php , Error code :<A HREF="http://www.phpstat.net/Errorcode.php#005" target=_blank>005</A> line:'.__LINE__,"count/" );
			}					write_to_file(COUNT_DIRNAME."/exclusion/website_".$websiteid."_regexp.php",$fileStr2,'w+');

			$r1 = $r2 = true;
			if( $r and $_POST[sitetype] == '0')
			{	
				$Res = $Conn->Execute("SELECT id FROM ".DB_PREFIX.$Tmp[website]."_ip_hits  LIMIT 1");				
				if( !$Res )	
				{
					$content = str_replace( 'PREFIX_' , DB_PREFIX.$_POST[website].'_', file_get_contents( './count/js/structure_ip_hits.sql' ) );
					if( $Versions > 40 )
					$content = ereg_replace(";"," DEFAULT CHARACTER SET ".DB_CHARSET." ;",$content);
					$r1 = $Conn->Execute( $content );
				}
				$Res = $Conn->Execute("SELECT id FROM ".DB_PREFIX.$Tmp[website]."_ip_limit  LIMIT 1");				
				if( !$Res )
				{
					$content = str_replace( 'PREFIX_' , DB_PREFIX.$_POST[website].'_', file_get_contents( './count/js/structure_ip_limit.sql' ) );
					if( $Versions > 40 )
					$content = ereg_replace(";"," DEFAULT CHARACTER SET ".DB_CHARSET." ;",$content);
					$r2 = $Conn->Execute( $content );
				}
				if( !$r1 or !$r2 )
				{
					$msg ="<div align=center>站点相关数据库配置失败，请重新编辑<BR>";
					$goback = "<a href=# onclick=\"history.go(-1)\">请您返回</a></div>";
					showMsg ( $msg , $goback );
				}
				if( $_POST[sitetype] != '0' and  $_POST[sitetype] != '')
				{
					
				}

			}
			
			$sql = "SELECT * FROM `" . DB_PREFIX . "site";
			$res = $Conn->Execute( $sql );
			while( $row = $res->FetchRow() )
			{
				$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);

			echo "<script language=\"javascript\" type=\"text/javascript\" src=\"http://www.phpstat.net/Check/CheckProductID.php?p=".$PHPStatProductKey."&u=".$LOCALDOMAIN."\" ></script>";	
			echo "<meta http-equiv=\"refresh\" content=\"0;URL=site_list.php?website=true&action=sitelist\">";
		
		}
	}
	//编辑用户的站点
	if($_GET[action] == "editsite")
	{	
		if($_SESSION[SESSION_USERGROUP] == 'superadmin' or $_SESSION[SESSION_USERGROUP] == 'strongsuperadmin')
		{
			$Res = $Conn->Execute( " select id,managerid,site,sitename,website,sitedes,sitetype,siteshow,msn,exclusionip,exclusioninter from  $TableList[site] where website = '$_GET[website]'");
		}
		elseif($_SESSION[SESSION_USERGROUP] == 'admin')
		{	
			
			$Res = $Conn->Execute( " select id,managerid,site,sitename,website,sitedes,sitetype,siteshow,msn,exclusionip,exclusioninter from  $TableList[site] where website = '$_GET[website]'");			
			
		}
		$Tmp = $Res->FetchRow();
		if($Tmp[siteshow] == "no") $Tmp[siteshowtext] = "需要密码";
		elseif($Tmp[siteshow] == "yes") $Tmp[siteshowtext] = "公开结果";
		if($Tmp[sitetype] == "0") 
		{
			$Tmp[sitetypetext] = "作为主站";
			$Tmp[sitetypevalue] = "0";
		}
		else
		{	
			$Res2 = $Conn->Execute(" select sitename from $TableList[site] where website = '$Tmp[sitetype]'");
			$Tmp2 = $Res2->FetchRow();
			$Tmp[sitetypetext] = "作为 ".$Tmp2[sitename]." 的频道或者子站";
			$Tmp[sitetypevalue] = $Tmp[sitetype];
		}

		$ResSite = $Conn->Execute( " select websiteid from $TableList[user_site] where managerid = '$manager'");
		while($TmpSite = $ResSite->FetchRow())
		$WebsiteID[] = $TmpSite[websiteid];
		$Where = @implode("|",$WebsiteID);
		if( count($Where) > 0)
		{
			$WhereStr = " website REGEXP '".$Where."' or ";
		}
		$Res = $Conn->Execute(" select sitename,website from $TableList[site] where $WhereStr managerid = '$manager' and sitetype = '0'");
		if( $managergroup == 'superadmin' or $managergroup == 'strongsuperadmin')
		{
			$Res = $Conn->Execute(" select sitename,website from $TableList[site] where  msn = '$managercode' and website != '$_GET[website]' and sitetype = '0' ");
		}
		while($Tmp3 = $Res->FetchRow())
		{
			$SiteType[]	= $Tmp3;		
		}
		$Tpl->assign( 'sitetype', $SiteType );
		if($Tmp[siteshow] == "yes") $Tmp[siteshowtext] = "公开结果";
		$Tpl->assign( 'UserData', $Tmp );
		$Tpl->assign( 'selectMode', 'site' );
		$Tpl->assign( 'NowView', '编辑用户的站点' );
		$Tpl->assign( 'Title', '编辑用户的站点 - '.$SoftWareName .$SoftWareVersion );
		$Tpl->assign( 'Main', $Tpl->fetch( 'user_info.html' ) . $ScriptCode );
	}
	//更改用户的站点
	if($_POST[action] == "updatesite")
	{	
		
		$Res = $Conn->Execute(" select * from $TableList[site] where website = '$_POST[website]'  and id != '$_POST[websiteid]'");
		if($Res->RecordCount() > 0)
		{
			$msg ="<div align=center>站点代码相同<BR>";
			$goback = "<a href=# onclick=\"history.go(-1)\">请您返回修改</a></div>";
			showMsg ( $msg , $goback );
		}
		else
		{	
			$Res = $Conn->Execute( " select id,website,sitetype from $TableList[site] where id = '$_POST[websiteid]' ");
			$Tmp = $Res->FetchRow();
			$websiteid = $Tmp[id];
			
			{
				$POSTTypeRes = $Conn->Execute( " select id,website,sitetype from $TableList[site] where website = '$_POST[sitetype]' ");
				$POSTTypeTmp = $POSTTypeRes->FetchRow();
				$POSTwebsiteid = $POSTTypeTmp[id];

				$TmpTypeRes = $Conn->Execute( " select id,website,sitetype from $TableList[site] where website = '$Tmp[sitetype]' ");
				$TmpTypeTmp = $TmpTypeRes->FetchRow();
				$Tmpwebsiteid = $TmpTypeTmp[id];
			}

			$IsRes = $Conn->Execute( " select count(*) as count from $TableList[site] where sitetype = '$Tmp[website]' ");
			$IsTmp = $IsRes->FetchRow();
			if( $Tmp[website] != $_POST[website] )
			{				
				if( $IsTmp[count] > 0 )
				{
					$msg ="<div align=center>主站包含子站，暂时不能修改<B>站点代码*</B>，您必须先删除所包含的子站<BR>";
					$goback = "<a href=# onclick=\"history.go(-2)\">请您返回</a></div>";	
					showMsg ( $msg , $goback );
				}
			}
			//主站修改站点统计代码
			if( $_POST[sitetype] == '0' and $Tmp[sitetype] == '0' and $Tmp[website] != $_POST[website])
			{
				$Res = $Conn->Execute("SELECT id FROM ".DB_PREFIX.$Tmp[website]."_ip_hits  LIMIT 1");
					
				if( $Res )
				{
					$r1 = $Conn->Execute( "RENAME TABLE ".DB_PREFIX.$Tmp[website]."_ip_hits TO ".DB_PREFIX.$_POST[website]."_ip_hits");
					$r2 = $Conn->Execute( $content );
				}

				$Res = $Conn->Execute("SELECT id FROM ".DB_PREFIX.$Tmp[website]."_ip_limit LIMIT 1");
				if( $Res )
				{
					$r1 = $Conn->Execute( "RENAME TABLE ".DB_PREFIX.$Tmp[website]."_ip_limit TO ".DB_PREFIX.$_POST[website]."_ip_limit");
					$r2 = $Conn->Execute( $content );
				}				
			}
			
			if( $_POST[sitetype] != '0' )
			{				
				if( $IsTmp[count] > 0 )
				{
					$msg ="<div align=center>主站包含子站，暂时不能修改<B>统计类型*</B>，您必须先删除所包含的子站<BR>";
					$goback = "<a href=# onclick=\"history.go(-2)\">请您返回</a></div>";	
					showMsg ( $msg , $goback );
				}
			}
			
			$IpArray = @explode("|",$_POST[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( '.', $_POST[exclusionip] );
				if( $ValueArray[3] == "" )
				{
					$ValueStart = $_POST[exclusionip].'0';
					$ValueEnd	= $_POST[exclusionip].'255';
				}
				else
				{
					$ValueStart = $_POST[exclusionip];	
					$ValueEnd	= $_POST[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//统计网站地址\n";	
			$fileStr .= "\$siteurl = \"$_POST[site]\";\n";	
			$fileStr .= "\n//程序排除IP地址列表\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("|",$_POST[exclusioninter]);
			$DomainArray = @array_unique ($DomainArray);
			if( count($DomainArray) > 1 )
			foreach( $DomainArray as $Key => $Value )
			{				
				$exclusioninterArray[] = $Value; 
			}
			else
			{	
				$exclusioninterArray[]	= $_POST[exclusioninter];
			}		
			
			$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( $_POST[sitetype] == '0' )
			{					
				$fileStr .= "\$mainsitecode = \"".$_POST[website]."\";\n";
			}
			else
			{
				$fileStr .= "\$mainsitecode = \"".$_POST[sitetype]."\";\n";
			}
			$fileStr .= "?>";
			if( !file_exists( COUNT_DIRNAME."/exclusion/website_".$websiteid.".php" ) )
			{
				Error( '无法写入站点文件 website_'.$websiteid.'.php , Error code :<A HREF="http://www.phpstat.net/Errorcode.php#005" target=_blank>005</A> line:'.__LINE__,"count/" );
			}
			write_to_file(COUNT_DIRNAME."/exclusion/website_".$websiteid.".php",$fileStr,'w+');

			

			$r = $Conn->Execute( " update $TableList[user_site] set websiteid = '$_POST[website]' where managerid = '$manager' and websiteid = '$Tmp[website]'");
			if( $r )$r = $Conn->Execute( " update $TableList[site] set site='$_POST[site]' ,sitename = '$_POST[sitename]', website = '$_POST[website]',sitedes = '$_POST[sitedes]',sitetype = '$_POST[sitetype]',exclusionip = '$_POST[exclusionip]',exclusioninter = '$_POST[exclusioninter]' where id = '$_POST[websiteid]'");
			
			//如果修改成主站类型，生成相关配置文件
			if( $r and ( $_POST[sitetype] == '0' or $_POST[sitetype] == '' ) and $Tmp[sitetype] != "0" )
			{
	
				$ResList = $Conn->Execute( "select website from $TableList[site] where sitetype = '$_POST[website]'");
				while( $TmpList = $ResList->FetchRow() )
				{
					$SiteListArray[] = $TmpList[website];
				}
				$SiteListArray[] = $_POST[website];
				$SiteListStr = @implode('|',$SiteListArray);
				$fileStr = "<?\nif( !defined('WEB_HOME') ) exit('Access Denied');\n";		
				$fileStr .= "\n//程序主站所包含子站列表\n";	
				$fileStr .= "\$mainsiteregexp = \"$SiteListStr\";\n";	
				$fileStr .= "?>";
				if( !file_exists( COUNT_DIRNAME."/exclusion/website_".$websiteid."_regexp.php" ) )
				{
					Error( '无法写入站点文件 website_'.$websiteid.'_regexp.php , Error code :<A HREF="http://www.phpstat.net/Errorcode.php#005" target=_blank>005</A> line:'.__LINE__,"count/" );
				}				write_to_file(COUNT_DIRNAME."/exclusion/website_".$websiteid."_regexp.php",$fileStr,'w+');

				if( $Tmp[sitetype] != '0' )
				{
					unset($SiteListArray);
					$ResList = $Conn->Execute( "select website from $TableList[site] where sitetype = '$Tmp[sitetype]'");
					while( $TmpList = $ResList->FetchRow() )
					{
						$SiteListArray[] = $TmpList[website];
					}
					$SiteListArray[] = $Tmp[sitetype];
					$SiteListStr = @implode('|',$SiteListArray);
					$fileStr = "<?\nif( !defined('WEB_HOME') ) exit('Access Denied');\n";
					$fileStr .= "\n//程序主站所包含子站列表\n";	
					$fileStr .= "\$mainsiteregexp = \"$SiteListStr\";\n";	
					$fileStr .= "?>";
					if( !file_exists( COUNT_DIRNAME."/exclusion/website_".$Tmpwebsiteid."_regexp.php" ) )
					{
						Error( '无法写入站点文件 website_'.$Tmpwebsiteid.'_regexp.php , Error code :<A HREF="http://www.phpstat.net/Errorcode.php#005" target=_blank>005</A> line:'.__LINE__ ,"count/");
					}	write_to_file(COUNT_DIRNAME."/exclusion/website_".$Tmpwebsiteid."_regexp.php",$fileStr,'w+');
				}
			}			
			elseif( $_POST[sitetype] != '0' )
			{

				$ResList = $Conn->Execute( "select website from $TableList[site] where sitetype = '$_POST[sitetype]'");
				while( $TmpList = $ResList->FetchRow() )
				{
					$SiteListArray[] = $TmpList[website];
				}
				$SiteListArray[] = $_POST[sitetype];
				$SiteListStr = @implode('|',$SiteListArray);
				$fileStr = "<?\nif( !defined('WEB_HOME') ) exit('Access Denied');\n";
				$fileStr .= "\n//程序主站所包含子站列表\n";	
				$fileStr .= "\$mainsiteregexp = \"$SiteListStr\";\n";	
				$fileStr .= "?>";
				if( !file_exists( COUNT_DIRNAME."/exclusion/website_".$POSTwebsiteid."_regexp.php" ) )
				{
					Error( '无法写入站点文件 website_'.$POSTwebsiteid.'_regexp.php , Error code :<A HREF="http://www.phpstat.net/Errorcode.php#005" target=_blank>005</A> line:'.__LINE__ ,"count/");
				}
				write_to_file(COUNT_DIRNAME."/exclusion/website_".$POSTwebsiteid."_regexp.php",$fileStr,'w+');
				
				if( $_POST[sitetype] != $Tmp[sitetype] and $Tmp[sitetype] != '0' )
				{
					unset($SiteListArray);
					$ResList = $Conn->Execute( "select website from $TableList[site] where sitetype = '$Tmp[sitetype]'");
					while( $TmpList = $ResList->FetchRow() )
					{
						$SiteListArray[] = $TmpList[website];
					}
					$SiteListArray[] = $Tmp[sitetype];
					$SiteListStr = @implode('|',$SiteListArray);
					$fileStr = "<?\nif( !defined('WEB_HOME') ) exit('Access Denied');\n";
					$fileStr .= "\n//程序主站所包含子站列表\n";	
					$fileStr .= "\$mainsiteregexp = \"$SiteListStr\";\n";	
					$fileStr .= "?>";
					if( !file_exists( COUNT_DIRNAME."/exclusion/website_".$Tmpwebsiteid."_regexp.php" ) )
					{
						Error( '无法写入站点文件 website_'.$Tmpwebsiteid.'_regexp.php , Error code :<A HREF="http://www.phpstat.net/Errorcode.php#005" target=_blank>005</A> line:'.__LINE__ ,"count/");
					}	
					write_to_file(COUNT_DIRNAME."/exclusion/".$Tmpwebsiteid."_regexp.php",$fileStr,'w+');
				}
			}
			
			

			//子站转变主站,相关数据库操作
			if( $r and $_POST[sitetype] == '0' and $Tmp[sitetype] != "0" )
			{	
				$r1 = $r2 = true;
				if( $r )
				{	
					$Res = $Conn->Execute("SELECT id FROM ".DB_PREFIX.$Tmp[website]."_ip_hits  LIMIT 1");
				
					if( $Res and ( $_POST[website] != $Tmp[website] ))
					{
						$r1 = $Conn->Execute( "RENAME TABLE ".DB_PREFIX.$Tmp[website]."_ip_hits TO ".DB_PREFIX.$_POST[website]."_ip_hits");
					}
					elseif( !$Res )
					{
						$content = str_replace( 'PREFIX_' , DB_PREFIX.$_POST[website].'_', file_get_contents( './count/js/structure_ip_hits.sql' ) );
						if( $Versions > 40 )
						$content = ereg_replace(";"," DEFAULT CHARACTER SET ".DB_CHARSET." ;",$content);
						$r1 = $Conn->Execute( $content );
					}

					$Res = $Conn->Execute("SELECT id FROM ".DB_PREFIX.$Tmp[website]."_ip_limit LIMIT 1");
					if( $Res and ( $_POST[website] != $Tmp[website] ))
					{
						$r2 = $Conn->Execute( "RENAME TABLE ".DB_PREFIX.$Tmp[website]."_ip_limit TO ".DB_PREFIX.$_POST[website]."_ip_limit");						
					}
					elseif( !$Res )
					{
						$content = str_replace( 'PREFIX_' , DB_PREFIX.$_POST[website].'_', file_get_contents( './count/js/structure_ip_limit.sql' ) );
						if( $Versions > 40 )
						$content = ereg_replace(";"," DEFAULT CHARACTER SET ".DB_CHARSET." ;",$content);
						$r2 = $Conn->Execute( $content );
					}
					//从以前所属主站转移数据
					$Conn->Execute(" INSERT INTO `".DB_PREFIX.$_POST[website].'_'."ip_hits`(`ip`, `time`, `pageurl`, `pagetitle`, `pagefrom`, `pagefromsite`, `keyword`,	`todayfirst`, `website`) SELECT `ip`, `time`, `pageurl`, `pagetitle`, `pagefrom`, `pagefromsite`, `keyword`,	`todayfirst`, `website` FROM `".DB_PREFIX.$Tmp[sitetype].'_'."ip_hits` WHERE `website` = '$_POST[website]'");
					$Conn->Execute(" INSERT INTO `".DB_PREFIX.$_POST[website].'_'."ip_limit`(`ip`, `time`, `pageurl`, `pagetitle`, `pagefrom`, `language`, `color`, `screensize`,  `pagefromsite`, `country`, `address`, `province`, `city`, `alexatool`, `timezone`, `firsttime`,`lasttime`, `system`, `browser`, `bot`, `java`, `flash`, `returner`, `returnday`, `pagecount`, `timeslong`,`website`) SELECT `ip`, `time`, `pageurl`, `pagetitle`, `pagefrom`, `language`, `color`, `screensize`,  `pagefromsite`, `country`, `address`, `province`, `city`, `alexatool`, `timezone`, `firsttime`, `lasttime`, `system`, `browser`, `bot`, `java`, `flash`, `returner`, `returnday`, `pagecount`, `timeslong`,`website` FROM `".DB_PREFIX.$Tmp[sitetype].'_'."ip_limit` WHERE `website` = '$_POST[website]'");					

					$Conn->Execute("DELETE FROM  ".DB_PREFIX.$Tmp[sitetype].'_'."ip_hits WHERE `website` = '$_POST[website]'");
					$Conn->Execute("DELETE FROM  ".DB_PREFIX.$Tmp[sitetype].'_'."ip_limit WHERE `website` = '$_POST[website]'");
				}
				$_POST[website] = trim( strtolower( $_POST[website] ) );
				if( !$r1 or !$r2 )
				{
					$msg ="<div align=center>站点代码修改失败，系统已自动恢复到修改以前状态<BR>";
					$goback = "<a href=# onclick=\"history.go(-2)\">请您返回</a></div>";
					$r = $Conn->Execute( " update $TableList[user_site] set websiteid = '$Tmp[website]' where managerid = '$manager' and websiteid = '$_POST[website]'");
					$r = $Conn->Execute( " update $TableList[site] set site='$_POST[site]' ,sitename = '$_POST[sitename]', website = '$Tmp[website]',sitedes = '$_POST[sitedes]',sitetype = '$_POST[sitetype]',exclusionip = '$_POST[exclusionip]',exclusioninter = '$_POST[exclusioninter]' where id = '$_POST[websiteid]'");
					showMsg ( $msg , $goback );
				}
				
			}
			//主站转变子站,相关数据库操作
			if( $r and $_POST[sitetype] != '0' and $_POST[sitetype] != '' and $Tmp[sitetype] == '0')
			{	
				$r1 = $r2 = true;
				if( $r )
				{	
					//从主站转移数据到所属主站
					$Conn->Execute(" INSERT INTO `".DB_PREFIX.$_POST[sitetype].'_'."ip_hits`(`ip`, `time`, `pageurl`, `pagetitle`, `pagefrom`, `pagefromsite`, `keyword`, `website`) SELECT `ip`, `time`, `pageurl`, `pagetitle`, `pagefrom`, `pagefromsite`, `keyword`, `website` FROM `".DB_PREFIX.$_POST[website].'_'."ip_hits` WHERE `website` = '$_POST[website]'");
					
					$Conn->Execute(" INSERT INTO `".DB_PREFIX.$_POST[sitetype].'_'."ip_limit`(`ip`, `time`, `pageurl`, `pagetitle`, `pagefrom`, `language`, `color`, `screensize`,  `pagefromsite`, `country`, `address`, `province`, `city`, `alexatool`, `timezone`, `firsttime`, `lasttime`, `system`, `browser`, `bot`, `java`, `flash`, `returner`, `returnday`, `pagecount`, `timeslong`,`website`) SELECT `ip`, `time`, `pageurl`, `pagetitle`, `pagefrom`, `language`, `color`, `screensize`,  `pagefromsite`, `country`, `address`, `province`, `city`, `alexatool`, `timezone`, `firsttime`, `lasttime`, `system`, `browser`, `bot`, `java`, `flash`, `returner`, `returnday`, `pagecount`, `timeslong`, `website` FROM `".DB_PREFIX.$_POST[website].'_'."ip_limit` WHERE `website` = '$_POST[website]'");
					$r1 = $Conn->Execute("drop table ".DB_PREFIX.$_POST[website].'_'."ip_hits");
					$r2 = $Conn->Execute("drop table ".DB_PREFIX.$_POST[website].'_'."ip_limit");
				}
				
				if( !$r1 or !$r2 )
				{
					$msg ="<div align=center>站点代码修改失败，系统已自动恢复到修改以前状态<BR>";
					$goback = "<a href=# onclick=\"history.go(-2)\">请您返回</a></div>";
					$r = $Conn->Execute( " update $TableList[user_site] set websiteid = '$Tmp[website]' where managerid = '$manager' and websiteid = '$_POST[website]'");
					$r = $Conn->Execute( " update $TableList[site] set site='$_POST[site]' ,sitename = '$_POST[sitename]', website = '$Tmp[website]',sitedes = '$_POST[sitedes]',sitetype = '$Tmp[sitetype]',exclusionip = '$_POST[exclusionip]',exclusioninter = '$_POST[exclusioninter]' where id = '$_POST[websiteid]'");
					showMsg ( $msg , $goback );
				}
				
			}

			
		}
		
		if( $_POST[sitetype] == '0' and $Tmp[sitetype] == '0' and $Tmp[website] == $_POST[website])
		{
			$Res = $Conn->Execute("SELECT id FROM ".DB_PREFIX.$Tmp[website]."_ip_hits  LIMIT 1");
			$mark = false;
			if( !$Res )
			{
				$mark = true;
				$content = str_replace( 'PREFIX_' , DB_PREFIX.$_POST[website].'_', file_get_contents( './count/js/structure_ip_hits.sql' ) );
				if( $Versions > 40 )
				$content = ereg_replace(";"," DEFAULT CHARACTER SET ".DB_CHARSET." ;",$content);
				$r1 = $Conn->Execute( $content );
			}

			$Res = $Conn->Execute("SELECT id FROM ".DB_PREFIX.$Tmp[website]."_ip_limit LIMIT 1");
			if( !$Res )
			{
				$mark = true;
				$content = str_replace( 'PREFIX_' , DB_PREFIX.$_POST[website].'_', file_get_contents( './count/js/structure_ip_limit.sql' ) );
				if( $Versions > 40 )
				$content = ereg_replace(";"," DEFAULT CHARACTER SET ".DB_CHARSET." ;",$content);
				$r2 = $Conn->Execute( $content );
			}

			if( $mark and (!$r1 or !$r2) )
			{
				$msg ="<div align=center>站点相关数据库配置失败，请重新编辑。<BR>仔细检查是否拥有权限创建数据表".DB_PREFIX.$Tmp[website]."_limit，".DB_PREFIX.$Tmp[website]."_hits<BR>";
				$goback = "<a href=# onclick=\"history.go(-1)\">请您返回</a></div>";
				showMsg ( $msg , $goback );
			}
		}
		$sql = "SELECT * FROM `" . DB_PREFIX . "site";
		$res = $Conn->Execute( $sql );
		while( $row = $res->FetchRow() )
		{
			$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);
		header("location:site_list.php?website=true&action=sitelist");
	}
	
	########################START###########################
	

	if( file_exists(COUNT_DIRNAME."/system.php") )
	include_once COUNT_DIRNAME."/system.php";
	if( $CHECKSYSTEMDATE != date("Ymd") )
	{	
		$URes = $Conn->Execute( " select count(*) as counts from $TableList[users] " );
		$UTmp = $URes->FetchRow();
		$Ucounts = $UTmp[counts];

		$SRes = $Conn->Execute( " select count(*) as counts from $TableList[site] " );
		$STmp = $SRes->FetchRow();
		$Scounts = $STmp[counts];
		echo "<script language=\"javascript\" type=\"text/javascript\" src=\"http://%6D%79%2E%70%68%70%73%74%61%74%2E%6E%65%74/CheckSystem.php?p=".$PHPStatProductKey."&d=".$LOCALDOMAIN."&u=".$Ucounts."&s=".$Scounts."&c=".$SYSTEMCHECKDCODE."\"></script>";	
	}

	if( function_exists( CheckSystem ) )
	{
		CheckSystem( $SYSTEMCHECKDCODE );
	}
	else
	{
		echo "<SCRIPT LANGUAGE=\"JavaScript\">
			
			alert( '系统文件丢失!' );
			
			</SCRIPT>";
			exit;
	}
	########################END###########################

	$Tpl->assign( 'username', $_SESSION[SESSION_MANAGER] );
	if($_SESSION[SESSION_USERGROUP] == "strongsuperadmin")
	{
		$Tpl->assign( 'QuickLink', "<A HREF=\"site_list.php?website=true&action=sitelist\">网站配置信息</A> <a href=\"manage_db.php\">数据库管理</a> <a href=\"manage_all_site.php?website=true&ac=common\">站点列表管理</a> <a href=\"manage_user.php\">用户列表管理</a> <a href=\"syscfg.php\">软件配置信息</a> <a href=\"error_log.php\">错误日志信息</a> <a href=\"_php_info.php\" target=_blank>PHP信息</a>" );
	}
	elseif($_SESSION[SESSION_USERGROUP] == "superadmin" )
	{
		$Tpl->assign( 'QuickLink', " <A HREF=\"site_list.php?website=true&action=sitelist\">网站配置信息</A> <a href=\"manage_all_site.php?website=true&ac=common\">站点列表管理</a> <a href=\"manage_user.php\">用户列表管理</a>" );
	}
	elseif( $_SESSION[SESSION_USERGROUP] == "admin")
	{
		$Tpl->assign( 'QuickLink', " <A HREF=\"site_list.php?website=true&action=sitelist\">网站配置信息</A> <a href=\"manage_all_site.php?website=true&ac=common\">站点列表管理</a>" );
	}
	else
	{
		$Tpl->assign( 'QuickLink', " <A HREF=\"site_list.php?website=true&action=sitelist\">网站配置信息</A> <a href=\"manage_all_site.php?website=true&ac=common\">站点列表管理</a>" );
	}
	$Tpl->assign( 'CopyRight', $PHPStatBottomRight );
	_out($Tpl->fetch( 'main_index.html' ));

?>