<?php
session_start();
header("Content-Type: text/html;charset=GB2312");
set_magic_quotes_runtime(0);
include "Include/config.php";
include "Include/db.php";
function RoomList($RoomId=0,$tpl)
{
	global $db;
	if($RoomId!=0)
	$sql="select * from chat_room where Id='$RoomId'";
	else
	$sql="select * from chat_room";
	$Tmp=$db->query($sql);
	while($row=$db->fetch($Tmp))
	{
		$t=$tpl;
		$t=str_replace("{id}",$row['Id'],$t);
		$t=str_replace("{name}",$row['Name'],$t);
		$t=str_replace("{image}",$row['Image'],$t);
		$t=str_replace("{Ad}",$row['Ad'],$t);
		$tpls.=$t;
	}
	return $tpls;
}
function UserList($tpl)
{
	global $db;
	$Tmp=$db->query("select * from chat_user_online");
	while($row=$db->fetch($Tmp))
	{
		$t=$tpl;
		$t=str_replace("{id}",$row['Id'],$t);
		$t=str_replace("{chatid}",$row['ChatId'],$t);
		$t=str_replace("{roomid}",$row['RoomId'],$t);
		$t=str_replace("{roomname}",RoomList($row['RoomId'],'{name}'),$t);
		$t=str_replace("{name}",$row['Name'],$t);
		$t=str_replace("{sex}",$row['Sex'],$t);
		$t=str_replace("{ip}",$row['IP'],$t);
		$t=str_replace("{Lastdate}",date("Y-m-d H:i:s",$row['LastTime']),$t);
		$tpls.=$t;
	}
	return $tpls;
}
function MusicList($tpl)
{	
	global $db;
	$Tmp=$db->query("select * from chat_music order by id desc");
	while($row=$db->fetch($Tmp))
	{
		$t=$tpl;
		$t=str_replace("{title}",$row['title'],$t);
		$t=str_replace("{singer}",$row['singer'],$t);
		$t=str_replace("{id}",$row['id'],$t);
		$tpls.=$t;
	}
	return $tpls;
}
function AdList($tpl)
{	
	global $db;
	$Tmp=$db->query("select * from chat_ad order by id desc");
	while($row=$db->fetch($Tmp))
	{
		$t=$tpl;
		$t=str_replace("{title}",$row['title'],$t);
		$t=str_replace("{content}",$row['content'],$t);
		$t=str_replace("{id}",$row['id'],$t);
		$t=str_replace("{sleeptime}",$row['sleeptime']/60,$t);
		$tpls.=$t;
	}
	return $tpls;
}
if(isset($_SESSION['chatadmin']))
{
	if(isset($_GET['Act'])){
	$Act=$_GET['Act'];
	switch($Act){
		case "useradd":
			$username=$db->fhtml($_POST['username']);
			$usersex=$db->fhtml($_POST['sex']);
			$lasttime=strtotime($_POST['lasttime']);
			$RoomId=$db->fhtml($_POST['RoomId']);
			$Time=time();
			$Cid=md5($Time);
			$Sql="insert into chat_user_online(Id,ChatId,RoomId,Name,Sex,Color,Role,Ip,EnterTime,LastTime)values('0','$Cid','$RoomId','$username','$usersex','#000000','0','0.0.0.0','$Time','$lasttime')";
			$db->query($Sql);
		break;
		case "userdel":
			$cid=$db->fhtml($_GET['userdel']);
			$Time=time();
			$DelUser=$db->fetch($db->query("select * from chat_user_online where ChatId='$cid'"));
			$db->query("delete from chat_user_online where ChatId='$cid'");
		break;
		case "edituser":
			$cid=$db->fhtml($_GET['edituser']);
			$username=$db->fhtml($_POST['username']);
			$lasttime=strtotime($_POST['lasttime']);
			$db->query("update chat_user_online set Name='$username',LastTime='$lasttime' where ChatId='$cid'");
		break;
		case "musicadd":
			$title=$db->fhtml($_POST['title']);
			$singer=$db->fhtml($_POST['singer']);
			$url=$db->fhtml($_POST['url']);
			$sql="insert into chat_music(title,singer,url)values('$title','$singer','$url')";
			$db->query($sql);
		break;
		case "musicdel":
			$id=$db->fhtml($_GET['id']);
			$db->query("delete from chat_music where id='$id'");
		break;
		case "adadd":
			$title=$db->fhtml($_POST['title']);
			$content=$db->ghtml($_POST['content']);
			$sleeptime=$_POST['sleeptime']*60;
			$sql="insert into chat_ad(title,content,sleeptime,starttime)values('$title','$content','$sleeptime',now())";
			$db->query($sql);
		break;
		case "addel":
			$id=$db->fhtml($_GET['id']);
			$db->query("delete from chat_ad where id='$id'");
		break;
		case "roomadd":
			$Name=$db->fhtml($_POST['roomname']);
			$Image=$db->fhtml($_POST['imgurl']);
			$Ad=$db->fhtml($_POST['ad']);
			$db->query("insert into chat_room(Name,Image,Ad)values('$Name','$Image','$Ad')");
		break;
		case "roomdel":
			$id=$db->fhtml($_GET['id']);
			$db->query("delete from chat_room where Id='$id'");
		break;
		case "roomadminadd":
			$User=$db->fhtml($_POST['User']);
			$Id=$db->fhtml($_POST['roomid']);
			$db->query("insert into chat_admin(Name,RoomId)values('$User','$Id')");
		break;
		case "roomadminedit":
			$User=$db->fhtml($_POST['User']);
			$Id=$db->fhtml($_POST['roomid']);
			$db->query("update chat_admin set RoomId='$Id' where Name='$User'");
		break;
		case "roomadmindel":
			$Id=$db->fhtml($_GET['id']);
			$db->query("delete from chat_admin where id='$Id'");
		break;
		case "logout":
			unset($_SESSION['chatadmin']);
		break;
	}
	header('Location:Admin.php');
	exit;
	}
}
else
{
	if($_POST['Passwd']==$Admin_PWD)
	{
	$_SESSION['chatadmin']='true'; 
	exit("<script>location.href='Admin.php'</script>");
	}
	exit('<form action="?login" method="post" enctype="multipart/form-data">PassWord:<input type="password" name="Passwd" id="Passwd" /></form>');
	
}
?>
<!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>
<title>聊天室管理</title>
<style>
body { font-size:9pt; margin:2px; }
td { font-size:9pt; }
.title { background:#3366cc; color:#FFFFFF; font-size:12px; font-weight:bold; padding:4px; text-align:center; cursor:hand }
</style>
</head><script>
function Small(id)
{
var display=document.getElementById(id).style.display;
if(display=="none")
	{
	document.getElementById(id).style.display="block";
	setCookie(id,"block");
	}
	else 
	{
	document.getElementById(id).style.display="none";
	setCookie(id,"none");
	}
}
function setCookie(name,value)
{
    var Days = 30;
    var exp  = new Date();    //new Date("December 31, 9998");
        exp.setTime(exp.getTime() + Days*24*60*60*1000);
        document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
}
function getCookie(name)
{
    var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
        if(arr=document.cookie.match(reg)) return unescape(arr[2]);
        else 
		return "none";
}
function cinit()
{
document.getElementById("t1").style.display=getCookie("t1");
document.getElementById("t2").style.display=getCookie("t2");
document.getElementById("t3").style.display=getCookie("t3");
document.getElementById("t4").style.display=getCookie("t4");
document.getElementById("t5").style.display=getCookie("t5");
}

</script>
<body onload="cinit()">

<table width="100%" cellpadding="2" cellspacing="0" border="1" bordercolor=#000066 style="border-collapse:collapse;">
  <tr>
    <td class="title" onclick="Small('t1')">在线人员管理</td>
  </tr>
  <tr>
    <td style="padding:4px;"><div id="t1" style="height:550px; overflow:auto; ">
        <table width="98%" border="1" align="center" cellpadding="2" cellspacing="0" bordercolor="#F0F0F0">
          <tr>
            <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
                <form action='?Act=useradd' method='post' enctype='application/x-www-form-urlencoded' name='form4' id='form4'>
                  <tr>
                    <td>名字：
                      <input name="username" type="text" id="username" size="20" title="$row[IP]"/>
                      离线时间：
                      <input name="lasttime" type="text" id="lasttime" value="<?=date("Y-m-d H:i:s",time());?>"/>
                      性别：
                      <select name="sex" id="sex">
                        <option value="0">男</option>
                        <option value="1">女</option>
                      </select>
                      房间：
                      <select name="RoomId" id="RoomId">
                        <?=RoomList(0,'<option value="{id}">{name}</option>')?>
                      </select>
                      <input type="submit" value="添加僵尸" style="width:65px" />
                    </td>
                  </tr>
                </form>
              </table></td>
          </tr>
          <?=UserList('
		  <tr><form action="?Act=edituser&edituser={chatid}" method="post" enctype="multipart/form-data">
              <td> 名字：
                <input name="username" type="text" id="username" value="{name}"/>
                最后在线：
                <input name="lasttime" type="text" id="lasttime" value="{Lastdate}"/>
                IP：<input type="text" value="{ip}"/>
                房间：<input type="text" value="{roomname}"/>
                <input type="button"  value="踢出" onclick="location.href=\'?Act=userdel&userdel={chatid}\'"/>
                <input type="submit" value="修改"/>
              </td></form>
            </tr>
          ')?>
        </table>
      </div></td>
  </tr>
  <tr>
    <td class="title" onclick="Small('t2')"><strong>聊天室音乐管理</strong></td>
  </tr>
  <tr>
    <td style="padding:4px;"><div id="t2" style="height:550px; overflow:auto">
        <table width="98%" border="1" align="center" cellpadding="2" cellspacing="0" bordercolor="#F0F0F0">
          <tr>
            <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
                <form action="?Act=musicadd" method="post" enctype="application/x-www-form-urlencoded" name="form1" id="form1">
                  <tr>
                    <td>名称：
                      <input type="text" name="title" id="title" />
                      歌手 ：
                      <input type="text" name="singer" id="singer" />
                      地址：
                      <input name="url" type="text" id="url" value="" size="40" />
                      <input type="submit" name="button" id="button" value="添加" /></td>
                  </tr>
                </form>
              </table></td>
          </tr>
          <?=MusicList('
		  <tr>
            <td>
		  名称： 
		  <input type="text" value="{title}"/>
              歌手 ：
              <input type="text" value="{singer}"/>
              <input type="button"  value="删除" onclick="location.href=\'?Act=musicdel&id={id}\'"/>
		  </td>
          </tr>')?>
        </table>
      </div></td>
  </tr>
  <tr>
    <td class="title" onclick="Small('t3')">广告列表管理</td>
  </tr>
  <tr>
    <td style="padding:4px;"><div id="t3" style="height:550px; overflow:auto">
        <table width="98%" border="1" align="center" cellpadding="2" cellspacing="0" bordercolor="#F0F0F0">
          <tr>
            <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
                <form action="?Act=adadd" method="post" enctype="multipart/form-data" name="form2" id="form2">
                  <tr>
                    <td> 标题：
                      <input type="text" name="title" id="title" />
                      间隔：
                      <input name="sleeptime" type="text" id="sleeptime" value="30" />
                      分钟<br />
                      内容：
                      <textarea name="content" cols="100" rows="3" id="content">支持HTML</textarea>
                      <input type="submit" name="button2" id="button2" value="提交" />
                      <br /></td>
                  </tr>
                </form>
            </table></td>
          </tr>
          <?=AdList('
		  <tr>
            <td> 名称：
              <input type="text" value="{title}"/>
              间隔：
              <input type="text" value="{sleeptime}"/>
              <br />
              内容：
              <textarea cols="100" rows="3">{content}</textarea>
              <input type="button"  value="删除" onclick="location.href=\'?Act=addel&id={id}\'"/>
            </td>
          </tr>')?>
          
        </table>
      </div></td>
  </tr>
  <tr>
    <td class="title" onclick="Small('t4')"><strong>房间管理</strong></td>
  </tr>
  <tr>
    <td style="padding:4px;"><div id="t4" style="height:550px; overflow:auto">
        <table width="98%" border="1" align="center" cellpadding="2" cellspacing="0" bordercolor="#F0F0F0">
          <tr>
            <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
                <form action="?Act=roomadd" method="post" enctype="multipart/form-data" name="form2" id="form2">
                  <tr>
                    <td>房间名：
                      <input type="text" name="roomname" id="roomname" /> 
                      图片地址：
                      <input type="text" name="imgurl" id="imgurl" />
                      介绍：
                      <input type="text" name="ad" id="ad" />
                      <input type="submit" name="button2" id="button2" value="添加" />
                    <br /></td>
                  </tr>
                </form>
              </table></td>
          </tr>
          <?=RoomList(0,'<tr>
            <td>房间名：
              <input type="text" value="{name}" />
              图片地址：
  <input type="text" id="imgurl2" value="{image}" />
              介绍：
  <input type="text" id="ad2" value="{Ad}" /> 
  <input type="button"  value="删除" onclick="location.href=\'?Act=roomdel&id={id}\'"/>
            房间ID：{id}
			</td></tr>
		  ')?>
          
        </table>
      </div>
    </td>
  </tr>
  <tr>
    <td class="title" onclick="Small('t5')"><strong>房间管理人员</strong></td>
  </tr>
  <tr>
    <td style="padding:4px;"><div id="t5" style="height:550px; overflow:auto">
        <table width="98%" border="1" align="center" cellpadding="2" cellspacing="0" bordercolor="#F0F0F0">
          <tr>
            <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
                <form action="?Act=roomadminadd" method="post" enctype="multipart/form-data" name="form2" id="form2">
                  <tr>
                    <td>用户名：
                      <input type="text" name="User" id="User" /> 
                      房间ID列表：
                      <input name="roomid" type="text" id="roomid" value="0" />
                      （用&quot;|&quot;隔开 0为不管理任何房间）
                      
                      <input type="submit" name="button2" id="button2" value="添加" />
                    <br /></td>
                  </tr>
                </form>
              </table></td>
          </tr>
        <?php
        $re=$db->query("select * from chat_admin");
		while($row=$db->fetch($re))
		{
			echo "<tr><form action='?Act=roomadminedit' method='post' enctype='multipart/form-data'><td>用户名：
                      <input type='text' name='User' id='User' value='$row[Name]'/> 
                      房间ID列表：
                      <input type='text' name='roomid' id='roomid' value='$row[RoomId]' />
					  <input type='button' name='button2' id='button2' value='删除' onclick=\"location.href='?Act=roomadmindel&id={$row[id]}'\"/>
					  <input type='submit' name='button2' id='button2' value='修改' />
                      </td></form></tr>";
		}
		?>
          
        </table>
      </div>
    </td>
  </tr>
</table>
</body>
</html>
