<?php
//****************参数设置****************
//矫正服务器时间
date_default_timezone_set('Etc/GMT-8');
//显示在线用户
$disonline = false;
//新登陆时显示最近内容的条数(默认为30条)
$leastnum = 10;
//默认的房间名(默认是每天换一个文件),如果去掉d,则是每月换一个文件
$room = date("Y").'-'.date("m").'-'.date("d");
//房间保存路径,必须以/结尾
$roomdir = "rooms/";
//编码方式,注意:这里请不要更改!
$charset = "UTF-8";
//客户端最大显示内容条数(建议不要太大)
$maxdisplay = 100;//语言
$lang = array(
//聊天室描述
"description"=>"",
//聊天室标题
"title"=>"决策主力财经股票网 主力决策在线聊天室(盘中聊天室交流,盘后论坛交流)推荐股票奖励超赢软件-专业股票证券交流",
//第一个到聊天室的欢迎
"firstone"=>"<span style='color:#16a5e9;'>聊天记录每天0点自动清空,恭喜你今天第一哦!决策主力大盘,盘中10点40分-下午14点10分准时播报.中午12点30分和下午16点30分在论坛发贴播报.经常到聊天室推荐股票,收益不错的奖励超赢软件.盘中聊天室交流,盘后论坛交流</span>",
//当信息有禁止内容时显示
"ban"=>"I am a pig!",
//关键字
"keywords"=>"聊天室,www.jue-ce.com",
//发言提示
"hereyourwords" => "在这里发言!"
);
//过滤文字
$badwords = array('fuck','你妈','共产党');error_reporting(0);
header("content-type:text/html; charset=utf-8");$get_past_sec = 3; //如果发现丢话,可以适当调大这个值
$touchs = 30; //检查在线人数的时间间隔if (!function_exists("file_get_contents"))
{
function file_get_contents($path)
{ if (!file_exists($path)) return false;
$fp=@fopen($path,"r");
$all=fread($fp,filesize($path));
fclose($fp);
return $all;
}
}if (!function_exists("file_put_contents"))
{
function file_put_contents($path,$val)
{
$fp=@fopen($path,"w");
fputs($fp,$val);
fclose($fp);
return true;
}
}$title = $lang["title"];
$earlier = 10;
$description = $lang["description"];
$origroom = $room;
$least = (isset($_GET["dis"]))?intval($_GET["dis"]):$leastnum;
$touchme = isset($_POST['touchme'])?$_POST['touchme']:'';
if (!is_dir($roomdir)) @mkdir($roomdir) or die("error when creating folder $roomdir");
$room = isset($_GET['room'])?$_GET['room']:'';
if (!$room) $room = isset($_POST["room"])?$_POST["room"]:'';
$room = checkfilename($room);
if (!$room) $room = $origroom;
$filename = $roomdir.$room.".dat.php";
$datafile = $roomdir.$room.".php";
if (!file_exists($filename)) file_put_contents($filename,'<?php die();?>'."\n".time()."|".$lang["firstone"]."\n");
if (!file_exists($datafile)) file_put_contents($datafile,'<?php die();?>'."\n");
$action = isset($_POST["action"])?$_POST["action"]:'';
$windid = isset($_POST["name"])?$_POST["name"]:'';function checkfilename($file)
{
/*
if (!$file) return "";
$file = trim($file);
$a = substr($file,-1);
$file = preg_replace("^[.\\\/]*","",$file);
$file = preg_replace("[.\\\/]*$","",$file);
$arr = array("../","./","/","\\","..\\",".\\");
$file = str_replace($arr,"",$file);
return $file;
*/
return $file;
}function get_ip()
{
global $_SERVER;
if ($_SERVER)
{
if ( isset($_SERVER['HTTP_X_FORWARDED_FOR']) )
$realip = isset($_SERVER["HTTP_X_FORWARDED_FOR"])?$_SERVER["HTTP_X_FORWARDED_FOR"]:'';
else if ( isset($_SERVER["HTTP_CLIENT_IP"] ))
$realip = $_SERVER["HTTP_CLIENT_IP"];
else
$realip = $_SERVER["REMOTE_ADDR"];
}
else
{
if ( getenv( 'HTTP_X_FORWARDED_FOR' ) )
$realip = getenv( 'HTTP_X_FORWARDED_FOR' );
else if ( getenv( 'HTTP_CLIENT_ip' ) )
$realip = getenv( 'HTTP_CLIENT_ip' );
else
$realip = getenv( 'REMOTE_ADDR' );
}
return $realip;
}function array2json($arr)
{
$keys = array_keys($arr);
$isarr = true;
$json = "";
for($i=0;$i<count($keys);$i++)
{
if ($keys[$i] !== $i)
{
$isarr = false;
break;
}
}
// $json = $space;
$json.= ($isarr)?"[":"{";
for($i=0;$i<count($keys);$i++)
{
if ($i!=0) $json.= ",";
$item = $arr[$keys[$i]];
$json.=($isarr)?"":$keys[$i].':';
if (is_array($item))
$json.=array2json($item);
else if (is_string($item))
$json.='"'.badwordsfilter(str_replace(array("\r","\n"),"",$item)).'"';
else $json.=$item;
}
$json.= ($isarr)?"]":"}";
return $json;
}function keeponline()
{
global $disonline,$datafile;
if (!$disonline) return;
$name = $_POST['name'];
$ip = get_ip();
$onlines = @file_get_contents($datafile);
$s1 = "|{$name}|{$ip}|";
if (strpos($onlines,$s1) === false)
{
if (strpos($onlines,"|".$name."|") === false)
{
$fp = @fopen($datafile,"a+");
if ($fp)
{
if (@flock($fp, LOCK_EX))
{
@fputs($fp,time()."|".time().$s1."\n");
@flock($fp, LOCK_UN);
}
@fclose($fp);
}
}
else
{
echo "NAME";
die();
}
}
}//过滤函数
function badwordsfilter($str) {
global $badwords;
return str_replace($badwords,'**',$str);
}if ($action == "write")
{
$color = $_POST["color"];
if (!eregi("[0-9a-fA-F]{6}",$color) || $color == "#000000") $color = "";
$color = "#".$color;
$size = intval($_POST["size"]);
$name = htmlspecialchars(str_replace(array("\n","\r"),"",$_POST['name']));
if (!$name) die("No Name!!");
$ip = get_ip();
keeponline(); $s = "";
$style = "";
$font = $_POST["font"];
if ($font == "songti") $font = "宋体";
else if ($font == "heiti") $font = "黑体";
else if ($font == "kaiti") $font = "楷体_GB2312";
else $font = "";
$style .= (!$font)?"":"font-family:".$font.";";
$style .= (!$_POST["bold"])?"":"font-weight:bold;";
$style .= (!$color || $color == "#")?"":"color:{$color};";
$style .= (!$size || $size == "16")?"":"font-size:{$size}px;";
$t = time();
$arr = explode("\n",$_POST['content']);
if (count($arr) > 20) die('error');
for($i = 0;$i<count($arr);$i++)
{
$content = $arr[$i];
$content = trim($content);
$content = str_replace(array("\n","\r"),"",$content);
if (!$content) continue;
$content = htmlspecialchars($content);
$content = preg_replace("~\[img\](http:\/\/[a-zA-Z0-9\.-_\+%\?]*)\[\/img\]~i", "<img src='$1' />", $content);
$content = ($style)?"<span style='{$style}'>{$content}</span>":$content;
$s.= $t."|".$name.":".$content."\n";
} if (!$s) die("No Content!!");
$fp = @fopen($filename,"a+");
if (!$fp) die("repeat");
$re_time = 0;
while(!@flock($fp, LOCK_EX))
{
sleep(1);
$re_time++;
if ($re_time >=4) break;
}
if ($re_time <4)
{
@fputs($fp,$s);
@flock($fp, LOCK_UN);
}
else die("repeat");
@fclose($fp);
echo "OK";
}
else if ($action == "read")
{
$first = isset($_POST["first"])?$_POST["first"]:'';
$lastmod = intval($_POST["lastmod"]) - $get_past_sec; //得到两秒以内的所有发言,
$alastmod = @filemtime($filename);
if ($lastmod - $alastmod > 360*48) die;
$name = $_POST['name'];
$name = str_replace("\n","",$name);
$ip = get_ip();
$json = array();
$json["lastmod"] = time();
$item = array();
$newonline = array();
$offline = array(); $fp = @fopen($filename,'r');
flock($fp,LOCK_EX);
$s = fread($fp,filesize($filename));
flock($fp,LOCK_UN);
fclose($fp);
$lines = explode("\n",$s); if ($alastmod >= $lastmod && !$first)
{
foreach($lines as $l)
{
$item2 = array();
$l = str_replace(array("\n","\r"),"",$l);
if (strpos($l,"|") === false) continue;
$arr = explode("|",$l);
$t = intval($arr[0]);
if ($t >= $lastmod)
{
$item2["time"] = date("H:i:s",$t);
// $item2["word"] = badwordsfilter(addslashes($arr[1]));
$item2["word"] = addslashes($arr[1]);
$item[] = $item2;
}
}
}
else if ($first)
{
$item = array();
$total = count($lines);
for($i=$total-1;$i>=$total-$least;$i--)
{
if ($i<=0) break;
$item2 = array();
$l = str_replace(array("\n","\r"),"",$lines[$i]);
if (strpos($l,"|") === false) continue;
$arr = explode("|",$l);
$t = intval($arr[0]);
$item2["time"] = (date("m-d",time()) == date("m-d",$t))?date("H:i:s",$t):date("m-d H:i",$t);
$item2["word"] = addslashes($arr[1]);
$item[] = $item2;
}
$item = array_reverse($item);
} $s = "";
$nt = time();
$onlines = array();
if($disonline && $touchme)
{
$users = @file($datafile);
foreach($users as $l)
{
$l = str_replace(array("\r","\n"),"",$l);
if (strpos($l,"|") === false)
{
$s.=$l."\n";
continue;
}
$arr = explode("|",$l);
if ($nt - intval($arr[1]) < $touchs*3)
{
if (trim($name) == trim($arr[2]))
{
$s.= $arr[0]."|".time()."|".$name."|".get_ip()."|\n";
}
else $s.=$l."\n";
$onlines [] = htmlspecialchars($arr[2]);
}
}
@file_put_contents($datafile,$s);
$json["onlines"] = $onlines;
}
$json["lines"] = $item;
echo array2json($json);
}
else if ($action == "keep" )
{
keeponline();
echo "keep ok";
}
else if ($action == "quit")
{
$name = $_POST['name'];
if($disonline)
{
$users = @file($datafile);
foreach($users as $l)
{
$l = str_replace(array("\r","\n"),"",$l);
if (strpos($l,"|") === false)
{
$s.=$l."\n";
continue;
}
$arr = explode("|",$l);
if (trim($name) == trim($arr[2])) continue;
else $s.=$l."\n";
}
@file_put_contents($datafile,$s);
echo "OK";
}
die();
}
else
{
?>