<?php
/*********************************************************
程序名称：站长助手 WEBMASTER HELPER
作者：feixin
email:kusite@etang.com
版权：feixin (http://www.zc18.com)
//////////////////////////////////////////////////////////

最后编辑日期：2003-6-12
*********************************************************/

if (!defined('IN_WH')) {
  die("不要黑我啊");
}

include("./include/mysql.php");

$db = new WHmysql(WH_DB_HOST, WH_DB_NAME, WH_DB_USER, WH_DB_PW, 0);
?>
<table align=center>
<tr>
	<td><a href="index.php?op=mysql">首页</a>&nbsp;&nbsp;<a href="index.php?op=mysql&action=viewAllTables">查看所有的表</a>&nbsp;&nbsp;<a href="index.php?op=mysql_backup">备份数据</a>&nbsp;&nbsp;<a href="index.php?op=mysql&action=query">运行MySQL语句</a>&nbsp;&nbsp;<a href="index.php?op=mysql&action=size">数据空间统计</a><br /><hr></td>
</tr>

<?php
if($action == "viewAllTables") {
	if(empty($page)){ $page = 1;}
	$tables = $db->list_tables(WH_DB_NAME);
	$n = $db->num_rows($tables);
	if($n == 0){ $WH->message("暂时没有数据表！", "", 1);}
	include("./include/page.php");
	$WHpage = new WHpage;
	$WHpage->count = $n;
	$WHpage->page = $page;
	$WHpage->pagesize = WH_PERPAGE;
	$WHpage->para = "op=mysql&action=viewAllTables";
	$WHpage->creat();
	?><tr><td><? echo $WHpage->show();?></td></tr>
	<tr><td><table><script language="JavaScript">
	<!--
	function setCheckboxes(the_form, do_check)
	{
		var elts      = (typeof(document.forms[the_form].elements['selected_db[]']) != 'undefined')
					  ? document.forms[the_form].elements['selected_db[]']
					  : document.forms[the_form].elements['selected_tbl[]'];
		var elts_cnt  = (typeof(elts.length) != 'undefined')
					  ? elts.length
					  : 0;

		if (elts_cnt) {
			for (var i = 0; i < elts_cnt; i++) {
				elts[i].checked = do_check;
			} // end for
		} else {
			elts.checked        = do_check;
		} // end if... else

		return true;
	} // end of the 'setCheckboxes()' function
	//-->
	</script><form name="myform" action="index.php?op=mysql" method=POST>
	<?php
	for ($i=$WHpage->begin;$i<$WHpage->end;$i++) {
		echo ("<tr><td width=60%><a href=\"index.php?op=mysql_table&table=".$db->tablename($tables,$i)."\" target=_blank title='点击编辑表'>".$db->tablename($tables,$i)."</a></td><td width=40%><input type=\"checkbox\" name=\"selected_tbl[]\" value=\"".$db->tablename($tables,$i)."\" id=\"checkbox_tbl_$i\" /></td></tr>");
	}
	?><tr><td><a href="#" onclick="setCheckboxes('myform', true); return false;">
            全选</a>
        &nbsp;/&nbsp;
        <a href="#" onclick="setCheckboxes('myform', false); return false;">
            全部取消</a></td><td><input type="hidden" name=action value=table><input type="hidden" name=page value=<?=$page?>><select name=table_op><option value=''>选择操作</option><option value='drop'>删除</option><option value='optimize'>优化</option><option value='check'>检查</option><option value='analyze'>分析</option><option value='empty'>清空</option></select></td></tr></table><tr><td><? echo $WHpage->show();?><br /><br />（<input type="checkbox" name="selected_all" value=1 />对所有表进行操作，共<?=$WHpage->count?>）<br /><input type="checkbox" name="submit_ok" value=1 />确认操作(请小心操作，不会再确认）<input type="submit" name=submit value="确认"></form></td></tr>
	<?php

}
else if($action == "query") {
	$query = trim($query);
	if(empty($query)){
		?>
		<tr><td><form action="index.php?op=mysql" method=POST>在数据库 mysql 执行以下指令:（只是运行<b>mysql_query</b>命令）<br /><input type="hidden" name=action value=query><textarea name="query" cols="60" rows="7" wrap="virtual" ></textarea><br /><input type="checkbox" name="submit_ok" value=1 />确认操作(请小心操作，不会再确认）<input type="submit" name=submit value="确认"></form></td></tr>
<?php
	}
	else
	{
		if(!$submit_ok){ 
			$WH->message("你没有确认操作！", "index.php?op=mysql&action=query", 1);
		}

		if (get_magic_quotes_gpc()) {
			$query = stripslashes($query);
		}

		$db->query($db->format($query));
		$WH->message("已经成功运行[ <b>{$query}</b> ]语句！", "index.php?op=mysql", 1, 0);
	}

}
else if($action == "size") {
	$query = $db->query("SHOW TABLE STATUS");
	$dbsize = $total_db = 0;
	$dbstatus = "";
	while($table = $db->fetch_array($query)) {
		$total_db ++;
		$tmp_size = $table[Data_length] + $table[Index_length];
		$dbstatus .= "<tr><td>表：<b><a href=\"index.php?op=mysql_table&table=$table[Name]\" title='点击编辑表' target=_blank>$table[Name]</a></b>&nbsp;有<b>{$table[Rows]}</b>条记录&nbsp;占用空间<b>".$WHuds->size($tmp_size)."</b>&nbsp;&nbsp;建立时间$table[Create_time]&nbsp;更新时间$table[Update_time]</td></tr>";
		$dbsize += $tmp_size;
		
	}
	if($total_db == 0){ $WH->message("暂时没有数据表！", "", 1);}
	$dbsize = $WHuds->size($dbsize);
	
	echo "<tr><td>共<b>{$total_db}</b>个表，数据占用空间：<b>$dbsize</b></td></tr>";
	echo $dbstatus;

}
else if($action == "table") {
	if(!$submit_ok){ 
		$WH->message("你没有确认操作！", "index.php?op=mysql&action=viewAllTables&page=$page", 1);
	}

	if(!$table_op){ 
		$WH->message("你没有选择操作！", "index.php?op=mysql&action=viewAllTables&page=$page", 1);
	}
	
	$tables = $db->list_tables(WH_DB_NAME);
	$n = $db->num_rows($tables);
	if($n == 0){ $WH->message("暂时没有数据表！", "", 1);}

	$tmp_tbl = $split_word = "";
	
	for($i=0;$i<$n;$i++)
	{
		table_op();
	}

	switch($table_op){
			case "drop":
				$sql = "DROP TABLE ".$tmp_tbl.";";
				break;
			case "optimize":
				$sql = "OPTIMIZE TABLE ".$tmp_tbl.";";
				break;
			case "check":
				$sql = "CHECK TABLE ".$tmp_tbl.";";
				break;
			case "analyze":
				$sql = "ANALYZE TABLE ".$tmp_tbl.";";
				break;
			case "empty":
				$sql = "DELETE FROM ".$tmp_tbl.";";
				break;
			default:
				$sql = "";
				break;
		}
		
		if(!empty($sql) && $tmp_tbl != ""){ 
			//s($sql);
			$db->query($db->format($sql));
			$WH->message("已经运行[ <b>{$sql}</b> ]");
		}

	$WH->message("已经完成所有操作，现在返回！", "index.php?op=mysql&action=viewAllTables&page=$page", 1, 0);

}
else
{
	
?>

<tr><td>数据服务器：<?=WH_DB_HOST?></td></tr>
<tr><td>数据名称：<?=WH_DB_NAME?></td></tr>
<tr><td>数据用户：<?=WH_DB_USER?></td></tr>


<tr><td><form action="index.php?op=mysql" method=POST>在数据库 mysql 执行以下指令（只是运行<b>mysql_query</b>命令）:<br /><input type="hidden" name=action value=query><textarea name="query" cols="60" rows="7" wrap="virtual" ></textarea><br /><input type="checkbox" name="submit_ok" value=1 />确认操作(请小心操作，不会再确认）<input type="submit" name=submit value="确认"></form></td></tr>
<?php
}

function table_op()
{
	global $selected_tbl, $i, $db, $selected_all, $tables, $tmp_tbl, $split_word;
	
	if($selected_all || $selected_tbl[$i] != ""){
		if($selected_all){ 
			$tmp_tbl = $tmp_tbl.$split_word.$db->tablename($tables,$i);
		}else{
			$tmp_tbl = $tmp_tbl.$split_word.$selected_tbl[$i];
		}
		$split_word = " ,";
	}
}



?>
</table>