⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 index.php

📁 聊天程序
💻 PHP
字号:
<?

////////////////////////////////////////////////////////////////////////////////////////////////////////

class room
{
var $Name;
var $MsgFile;
var $OnlineFile;

function write_msg_to_file($msg)
{
$last=getnum($this->MsgFile);
$last++;
$this_file="files/".$this->Name."_msg_".($last%10).".php";
$fp1=@fopen($this->MsgFile,"w+");@flock($fp1,3);
$fp2=@fopen($this_file,"w+");@flock($fp2,3);
@fwrite($fp1,$last);
@fwrite($fp2,$msg);
@fclose($fp2);@fclose($fp1);
}

function init()
{
global $username;
global $chating;
global $inchat;
if($chating=="yes")
	{
	$this->up_time();
	echo "<head><meta http-equiv=\"Refresh\" content=\"0 ;url=index.php\"></head>";
	exit;
	}
setcookie("chating","yes",time()+864000);
$this->write_msg_to_file("<!--|| ||enter||system|| ||--><b><font color=blue>◇公告◇: ".$username." 进入了聊天室!</font></b><br>\n");
$this->up_time();
echo "<head><meta http-equiv=\"Refresh\" content=\"0 ;url=index.php\"></head>";
}

function escape()
{
global $username;
$row=file($this->OnlineFile);
for($i=0;$i<count($row);$i++)
	{
	$sub=explode("|",$row[$i]);
	if($sub[1]==$username)
		{	
		$this->write_msg_to_file("<!--|| ||exit||system|| ||--><b><font color=blue>◇公告◇: ".$username." 离开了聊天室!</font></b><br>\n");
		delrow($this->OnlineFile,$i);
		break;
		}
	}
setcookie("inchat");
setcookie("chating");
setcookie("admin_room");
display_head();
message("正在返回大厅!","return","index.php");
display_foot();
}

function del_offline()
{
$row=file($this->OnlineFile);
for($i=0;$i<count($row);$i++)
	{
	$sub=explode("|",$row[$i]);
	$timedif=time()-$sub[2];
	if($timedif<=600)
		$all.=chop($row[$i])."\n";
	}
writeF($this->OnlineFile,$all,"w");
}

function up_time()
{
global $username;
$row=file($this->OnlineFile);
for($i=0;$i<count($row);$i++)
	{
	$sub=explode("|",$row[$i]);
	if($username==$sub[1])
		{
		$rownum=$i;
		$newrow="|".$username."|".time()."|\n";
		replace($this->OnlineFile,$rownum,$newrow);
		return;
		}
	}
$newrow="|".$username."|".time()."|\n";
writeF($this->OnlineFile,$newrow,"a+");
}

function writemsg($message,$emot,$act,$color,$obj,$pri,$priname)
{
global $username;
if($color=="")
	$color="#000000";
if($obj=="")
	$obj="大家";
if($pri=="")
	$pri="open";
if($act!="")
	{
	$act=str_replace("[me]",$username,$act);
	$act=str_replace("[obj]",$obj,$act);
	}
if($message!="" || $emot!="emot/0.gif" || $act!="")
	{
	$message=safe($message);
	if($act!="" && chop($message)=="")
		{
		$act=str_replace("(","",$act);
		$act=str_replace(")","",$act);
		$all="<!--||".$username."||".$obj."||".$pri."||".$priname."||--><b><font color=".$color.">".$act."</font></b><img src=".$emot." ALIGN=absmiddle><br>";
		}
	else
		{
		if($obj=="大家")
			$all="<!--||".$username."||".$obj."||".$pri."||".$priname."||--><font color=blue>".$username."</font> 说: <font color=".$color.">".$message.$act."</font><img src=".$emot." ALIGN=absmiddle><br>";
		else
			$all="<!--||".$username."||".$obj."||".$pri."||".$priname."||--><font color=blue>".$username."</font> 对 <font color=green>".$obj."</font> 说:<font color=".$color.">".$message.$act."</font><img src=".$emot." ALIGN=absmiddle><br>";
		}
	$this->write_msg_to_file($all."\n");
	}
$this->up_time();
echo "<script>parent.init.msgsubmit();</script>\n";
echo "<script>parent.init.display_my_message('".$all."','".$pri."')</script>\n";
echo "<script>parent.statu('complete')</script>\n";
}

function msg_flush($last)
{
ini_set("max_execution_time","200");
global $username;
$delaytime="0";
$flush_delay=db("get","files/system.php",1,"flush_delay",2);
if($flush_delay=="")  $flush_delay=2;
$delaymax=intval((180/$flush_delay));
echo "<html><head><style>body{letter-spacing:-7px; COLOR: #085878;}</style></head>\n";
echo "<body leftmargin=0 topmargin=5 background=index.files/flushback.gif>\n";
echo "<!-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -->\n";
echo "<!-- -- -- -- -- -- -- -- -- -- flush -- messages -- begin -- -- -- -- -- -- -- -- -- -- -->\n";
echo "<!-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -->\n";
flush();
while(1)
	{
	$num=getnum($this->MsgFile);
	if($last=="")
		$last=$num;
	if($last<$num)
		{
		for(;$last<$num;)
			{
			$last++;
			$this_file="files/".$this->Name."_msg_".($last%10).".php";
			$row=file($this_file);
			$message=$row[0];
			$message=str_replace("\n","",$message);
			$message=str_replace("
","",$message);
			$sub=explode("||",$message);
			if($sub[3]=="system")
				echo "<script>parent.init.system_message('".$sub[2]."','".$message."','".$last."','".$sub[4]."')</script>\n";
			else
				echo "<script>parent.init.display_message('".$sub[1]."','".$sub[2]."','".$message."','".$last."','".$sub[3]."','".$sub[4]."');</script>\n";
			flush();
			}
		}
	echo "|\n";
	flush();
	$delaytime++;
	if($delaytime>$delaymax)
		break;
	sleep($flush_delay);	
	}
sleep(1);
echo "<script>self.location.href='index.php?action=flush&last=".$last."'</script>\n";
}

function show_online()
{
/*global $admin_room;
global $inchat;
if($admin_room==$inchat)
	$admin="yes";*/
global $username;
if(db("get","files/system.php",1,"admin_name",2)==$username)
	$admin="yes";
$this->del_offline();
?>
<html><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link rel="stylesheet" href="index.files/css.css" type="text/css">
<meta http-equiv="Refresh" content="60 ; url=index.php?action=show_online"></head>
<body bgcolor=#EFEFEF text=#000000 leftmargin=0 topmargin=0>
<center>
<table width=130 border=0 cellspacing=0 cellpadding=0>
<?

$row=file($this->OnlineFile);
for($i=0;$i<count($row);$i++)
	{
	$sub=explode("|",$row[$i]);
	$name=$sub[1];
	$timedif=time()-$sub[2];
	if($timedif<=200)
		$icon="index.files/active.gif";
	else
		$icon="index.files/sleep.gif";
	if($admin=="yes")
		$admin_sign=" &nbsp;&nbsp;<a href=index.php?action=kick&tokick=".$name." title=将该用户踢出房间 target=temp>踢</a>";
	echo "<tr><td width=20><img src=\"".$icon."\"></td><td height=25 align=center><a href=\"javascript:parent.init.cs('".$name."')\" title=".$name.">".limitstr($name,14).$admin_sign."</td></tr>\n";
	}
echo "</table></center></body>\n";
}

function display()
{
include("index.files/main.php");
}

function kick($tokick)
{
global $username;
if(db("get","files/system.php",1,"admin_name",2)!=$username)  die("1");
writeF("files/lock.php",$tokick,"a+");
$this->write_msg_to_file("<!--|| ||kick||system||".$tokick."||--><b><font color=blue>◇公告◇: ".$tokick." 被管理员踢出了聊天室!</font></b><br>\n");
}

}

///////////////////////////////////////////////////////////////////////////////////////////////////////

function check()
{
global $username;
$row=file("files/lock.php");
for($i=0;$i<count($row);$i++)
	if($username==chop($row[$i]))
		return "no";
return "yes";
}

function checkroom($roomname)
{
$row=file("files/roomlist.php");
for($i=1;$i<count($row);$i++)
	{
	$sub=explode("|",$row[$i]);
	if($roomname==$sub[1])
		return "yes";
	}
return "no";	
}

function enter($room)
{
global $username;
global $inchat;
if(checkroom($room)=="no")
	{
	display_head();
	message("指定的房间不存在");
	display_foot();
	exit;
	}
$row=file("files/roomlist.php");
for($i=1;$i<count($row);$i++)
	{
	$sub=explode("|",$row[$i]);
	if($room==$sub[1])
		{
		$admin=explode(",",$sub[3]);
		for($j=0;$j<count($admin);$j++)
			if($admin[$j]==$username)
				{
				setcookie("admin_room",$room,time()+86400);
				break;
				}
		break;
		}
	}
setcookie("inchat",$room,time()+86400);
echo "<head><meta http-equiv=\"Refresh\" content=\"0 ;url=index.php?action=init\"></head>";
}

function showroom()
{
global $username;
display_head();
echo "<table border=0 cellspacing=0 cellpadding=2 width=260><tr><td width=40></td><td colspan=2>欢迎,".$username."</td></tr>\n";
$row=file("files/roomlist.php");
for($i=1;$i<count($row);$i++)
	{	
	$sub=explode("|",$row[$i]);
	$name=$sub[1];
	$icon=$sub[2];
	$MsgFile=$name."_msg.php";
	$OnlineFile=$name."_online.php";
	$onlinenum=count(file("files/".$OnlineFile));
	if($onlinenum=="0")
		writeF("files/".$MsgFile,"1","w+");
	echo "<tr><td width=40 align=center><img src=\"files/".$icon."\" border=0 height=20 width=20></td><td width=100 valign=middle><a href=index.php?action=enter&room=".$name.">".limitstr($name,16)."</a></td>\n";
	echo "<td width=120 valign=middle>在线人数:".$onlinenum."人</td></tr>\n";
	}
echo "</table>\n";
display_foot();
}

function usercp_display()
{
//用户设置...
echo "<head><meta http-equiv=\"Refresh\" content=\"0 ;url=index.php\"></head>";
//programming...
}

function usercp_save()
{
//保存用户设置...
echo "<head><meta http-equiv=\"Refresh\" content=\"0 ;url=index.php\"></head>";
//programming...
}

///////////////////////////////////////////////////////////////////////////////////////////////////////

require("index.files/fun.php");

if($action=="display_login")
	{
	display_head();
	display_login();
	display_foot();
	}
if($action=="login")
	login($user,$pass,$longin_method);

if(checklogin()=="no")
	{
	display_head();
	display_login();
	display_foot();
	exit;
	}
if(check()=="no")
	{
	display_head();
	message("<font color=red>你被管理员禁止访问了,请联系管理员</font>");
	display_foot();
	exit;
	}
if($inchat=="")
	{
	if($action=="showroom" || $action=="")
		showroom();
	if($action=="enter")
		enter($room);
	if($action=="usercp")
		usercp_display();
	if($action=="usercp_save")
		usercp_save();
	}
else
	{
	$room=$inchat;
	$ROOM=new room();
	$ROOM->Name=$room;
	$ROOM->MsgFile="files/".$room."_msg.php";
	$ROOM->OnlineFile="files/".$room."_online.php";
	if($action=="display" || $action=="")
		$ROOM->display();
	if($action=="flush")
		$ROOM->msg_flush($last);
	if($action=="init")
		$ROOM->init();
	if($action=="escape")
		$ROOM->escape();
	if($action=="write")
		{
		if($message2!="")
			{
			$message2=str_replace("
","",$message2);
			$message2=str_replace("\n","[br]",$message2);
			}
		$message=$message.$message2;
		$ROOM->writemsg($message,$emot,$act,$color,$obj,$pri,$priname);
		}
	if($action=="kick")
		$ROOM->kick($tokick);
	if($action=="show_online")
		$ROOM->show_online();
	if($action=="showroom" || $action=="usercp" || $action=="usercp_save")
		echo "<head><meta http-equiv=\"Refresh\" content=\"0 ;url=index.php\"></head>";
	if($action=="enter")
		{
		display_head();
		message("您还没有退出房间(".$room."),正在返回","return","index.php");
		display_foot();
		}
	}

////////////////////////////////////////////////////////////////////////////////////////////////////////

?>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -