<?
/*
 
 本软件版权归作者所有,在投入使用之前注意获取许可
 作者：程威（wiwiboy）
 项目：PHPStat流量统计 1.0
 电话：13553166317
 邮箱：wiwiboy@126.com
 QQ号：158839753
 网址：http://www.phpstat.net
 phpstat.net保留全部权力，受相关法律和国际		  		
		公约保护，请勿非法修改、转载、散播，或用于其他赢利行为，		
		并请勿删除版权声明。
*/
/*	
	verson:v1.0 20060618
	author:wiwiboy
	last update:2006/06/10	
*/
	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 './initmenu.php';
	
	if($_SESSION[SESSION_USERGROUP] != 'strongsuperadmin')
	{
		$msg ="<div align=center>此次操作被限制，如果您是管理员，请点击`<a href=login.php>这里</a>`登录<BR>";
		$goback = "<a href=# onclick=\"history.go(-1)\">请您返回</a></div>";
		showMsg ( $msg , $goback );
	}

	Initialize( $_GET, array( 'table'=>'strval', 'action'=>'strval', 'table'=>'strval') );
	Initialize( $_POST, array( 'table'=>'strval', 'action'=>'strval', 'table'=>'strval', 'date'=>'strval') );	
	if($_GET[action] == 'optimize')
	{
		if( $_GET[table] != '' )
		{
			$Conn->Execute( " OPTIMIZE TABLE `$_GET[table]` " );
		}
		else
		{
			foreach( $TableList as $Val )
			{
				$Conn->Execute( " OPTIMIZE TABLE `$Val` " );
			}
		}
		echo "<SCRIPT LANGUAGE=\"JavaScript\">
		<!--
			alert('数据库优化完成!');			
		//-->
		</SCRIPT>";
		echo "<meta http-equiv=\"refresh\" content=\"0;URL=manage_db.php\">";
	}
	if($_GET[action] == 'repair')
	{
		if( $_GET[table] != '' )
		{
			$Conn->Execute( " repair TABLE `$_GET[table]` " );
		}
		else
		{
			foreach( $TableList as $Val )
			{
				$Conn->Execute( " repair TABLE `$Val` " );
			}
		}
		echo "<SCRIPT LANGUAGE=\"JavaScript\">
		<!--
			alert('数据库修复完成!');			
		//-->
		</SCRIPT>";
		echo "<meta http-equiv=\"refresh\" content=\"0;URL=manage_db.php\">";
	}
	if($_GET[action] == 'optimizeall')
	{
		
		foreach( $TableList as $Val )
		{
			$Conn->Execute( " OPTIMIZE TABLE `$Val` " );
		}		
		echo "<SCRIPT LANGUAGE=\"JavaScript\">
		<!--
			alert('数据库优化完成!');
			//history.back();
		//-->
		</SCRIPT>";
		echo "<meta http-equiv=\"refresh\" content=\"0;URL=manage_db.php\">";
	}
	if($_GET[action] == 'repairall')
	{
		
		foreach( $TableList as $Val )
		{
			$Conn->Execute( " repair TABLE `$Val` " );
		}		
		echo "<SCRIPT LANGUAGE=\"JavaScript\">
		<!--
			alert('数据库修复完成!');
			//history.back();
		//-->
		</SCRIPT>";
		echo "<meta http-equiv=\"refresh\" content=\"0;URL=manage_db.php\">";
	}

	if($_POST[action] == 'deloldinfo')
	{
		$DateArr = explode('-',$_POST[date]);
		$StartTime = mktime(0,0,0,$DateArr[1],$DateArr[2],$DateArr[0]);
		foreach( $TableList as $Val )
		{	
			if( strstr($Val,'ip_data') or strstr($Val,'unicode2gb') or strstr($Val,'users') or strstr($Val,'holiday') or strstr($Val,'site') or strstr($Val,'adv'))
			{}
			else
			{
			if($Val == 'ip' or $Val == 'ip_history')
				$Conn->Execute( " delete from  $Val  where time < $StartTime" );
			else
				$Conn->Execute( " delete from  $Val  where times < $StartTime" );
			}
		}
		echo "<SCRIPT LANGUAGE=\"JavaScript\">
		<!--
			alert('数据库清空完成!');
			//history.back();
		//-->
		</SCRIPT>";
		echo "<meta http-equiv=\"refresh\" content=\"0;URL=manage_db.php\">";
	}

	if($_POST[action] == 'delallinfo')
	{
		$DateArr = explode('-',$_POST[date]);
		//$StartTime = mktime(0,0,0,$DateArr[1],$DateArr[2],$DateArr[0]);
		foreach( $TableList as $Val )
		{	
			if( strstr($Val,'ip_data') or strstr($Val,'unicode2gb') or strstr($Val,'users') or strstr($Val,'holiday') or strstr($Val,'site') or strstr($Val,'adv'))
			{}
			else
			{	
				//echo  $Val.'<BR>';
				$Conn->Execute( " truncate table $Val" );
			}
		}
		$handle=opendir("count/count");
		while ($file = readdir($handle)) 
		unlink("count/count/".$file);
		closedir($handle); 

		echo "<SCRIPT LANGUAGE=\"JavaScript\">
		<!--
			alert('数据库清空完成!');
			//history.back();
		-->
		</SCRIPT>";
		echo "<meta http-equiv=\"refresh\" content=\"0;URL=manage_db.php\">";
	}
	
	if($_POST[action] == 'delalltable')
	{
		
		foreach( $TableList as $Val )
		{
			$Conn->Execute( " drop table $Val" );
		}
		

		echo "<SCRIPT LANGUAGE=\"JavaScript\">
		<!--
			alert('智能一键删除数据库，成功完成!');
			//history.back();
		-->
		</SCRIPT>";
		echo "<meta http-equiv=\"refresh\" content=\"0;URL=index.htm\">";
	}
	
	$TableName[phpstat_adv] = "广告数据表";	
	$TableName[phpstat_alexa] = "ALEXA数据表";
	$TableName[phpstat_bot] = "网络蜘蛛数据表";
	$TableName[phpstat_browser] = "浏览器数据表";
	$TableName[phpstat_city] = "城市数据表";
	$TableName[phpstat_color] = "屏幕颜色数据表";
	$TableName[phpstat_country] = "广告数据表";
	$TableName[phpstat_day_data] = "24小时数据表";
	$TableName[phpstat_ip] = "当日明细数据表";
	$TableName[phpstat_ip_history] = "历史明细数据表";
	$TableName[phpstat_ip_limit] = "来访IP数据表";
	$TableName[phpstat_keyword] = "关键字数据表";
	$TableName[phpstat_language] = "语言数据表";
	$TableName[phpstat_month_data] = "每日数据表";
	$TableName[phpstat_page] = "受访页面数据表";
	$TableName[phpstat_plug] = "浏览器插件数据表";
	$TableName[phpstat_province] = "省份数据表";
	$TableName[phpstat_referer] = "访问来源数据表";
	$TableName[phpstat_returner] = "回头者数据表";
	$TableName[phpstat_screen] = "屏幕尺寸数据表";
	$TableName[phpstat_system] = "操作系统数据表";
	$TableName[phpstat_timezone] = "时区数据表";
	$TableName[phpstat_year_data] = "每月数据表";


	
	$Res = $Conn->Execute( " SHOW TABLE STATUS FROM ".DB_DATABASE );
	while( $Tmp = @$Res->FetchRow() )
	{	
		if( strstr($Tmp[Name],'ip_data') or strstr($Tmp[Name],'unicode2gb') or strstr($Tmp[Name],'users') or strstr($Tmp[Name],'site') or strstr($Tmp[Name],'holiday'))
		{
			$Tmp[st] = '无法进行删除操作';	
		}
		else
		{	
			if(strstr($Tmp[Name],DB_PREFIX))
			{
			$size = $Tmp[Data_length] + $Tmp[Index_length] + $Tmp[Data_free];
			if($size > 1024 * 1024) 
					$Size = sprintf("%01.2f",$size/(1024*1204)).' MB';
			elseif($size > 1024) 
					$Size = sprintf("%01.2f",$size/1024,0,3)." KB";
			else				 
					$Size = sprintf("%01.2f",$size,0,3)." B";

			$Tmp[Size] = $Size;
			$Tmp[CName] = '&nbsp;&nbsp;('.$TableName[$Tmp[Name]].')';	
			$TotalSize += $size;
			$TotalRows += $Tmp[Rows];			
			$TableInfo[] = $Tmp;
			}
		}
	}
	if($TotalSize > 1024 * 1024) 
			$TotalSize = sprintf("%01.2f",$TotalSize/(1024*1204),0,3).' MB';
	elseif($TotalSize > 1024) 
			$TotalSize = sprintf("%01.2f",$TotalSize/1024,0,3)." KB";
	else				 
			$TotalSize = sprintf("%01.2f",$TotalSize,0,3)." B";
	

	$Tpl->assign( 'TableInfo', $TableInfo );
	$Tpl->assign( 'TotalSize', $TotalSize );
	$Tpl->assign( 'TotalRows', $TotalRows );
	$Tpl->assign( 'Main', $Tpl->fetch( 'manage_db.html' )  );
	$Tpl->assign( 'Title', '数据库管理 '.$SoftWareName .$SoftWareVersion );
	$Tpl->assign( 'NowView', '数据库管理' );
	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_user.php\">用户列表管理</a> 
		<a href=\"syscfg.php\">软件配置信息</a> 
		<a href=\"_php_info.php\" target=_blank>PHP信息</a>" );
	}
	else
	{
		$Tpl->assign( 'QuickLink', "
		<A HREF=\"site_list.php?website=true&action=sitelist\">网站配置信息</A>" );
	}
	$Tpl->assign( 'CopyRight', $PHPStatBottomRight );
	_out( $Tpl->fetch( 'main_index.html' )  );
?>