插件55:浏览聊天记录

<?php // Plug-in 55: View Chat
/*
 * 插件说明:
 * 插件接受一个聊天室数据文件,显示当前用户可以浏览的全部聊天记录,可以是密聊内容,也可以是公开的内容。它需要以下参数:
 * $datafile 字符串,表示聊天室数据文件保存的位置。
 * $username 浏览聊天记录的当前用户名。
 * $maxtime 与聊天服务器的最大连接时间,单位为秒。这个参数必须取相当大的值,
 * 防止频繁反复刷新聊天内容,通常取300秒比较合适。
 */
// This is an executable example with additional code supplied
// To obtain just the plug-ins please click on the Download link

if (!PIPHP_ViewChat('chatroom.txt', 'fredsmith', 300))
   echo "Error. Could not open the chat data file";
else die("<script>self.location='" . $_SERVER['PHP_SELF'] .
   "'</script>");

function PIPHP_ViewChat($datafile, $username, $maxtime)
{
   // Plug-in 55: View Chat
   //
   // This plug-in takes the name of a chatroom data file
   // and displays the posts in it. The arguments required
   // are:
   //
   //    $datafile: File containing chat data
   //    $username: The username of the chat viewer
   //    $maxtime:  Maximum number of seconds the script
   //               may run for before returning
   //
   // Upon failure this plug-in returns FALSE, otherwise it
   // will return TRUE.

   if (!file_exists($datafile)) return FALSE;

   set_time_limit($maxtime + 5);
   $tn      = time();
   $tstart  = "<table width='100%' border='0'><tr><td " .
              "width='15%' align='right'>";
   $tmiddle = "</td><td width='85%'>";
   $tend    = "</td></tr></table><script>scrollBy(0,1000);" .
              "</script>\n";
   $oldpnum = 0;
   
   while (1)
   {
      $lines = explode("\n",
         rtrim(file_get_contents($datafile)));

      foreach ($lines as $line)
      {
         $thisline = explode("|", $line);
         $postnum  = $thisline[0];
         $to       = $thisline[1];
         $from     = $thisline[2];
         $message  = $thisline[3];

         if ($postnum > $oldpnum)
         {
            if ($to == "")
            {
               echo $tstart . "$from:" . $tmiddle .
                  $message . $tend;
            }
            elseif ($to == $username || $from == $username)
            {
               echo $tstart . "$from:" . $tmiddle .
                  "(PM to $to) <i>$message</i>" . $tend;
            }

            $oldpnum = $postnum;
            ob_flush();
            flush();
         }
      }
      
      sleep(2);
      if ((time() - $tn) > $maxtime) return TRUE;
   }
}

?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值