<?

if ($bbs_is_open==0) {
	if (!$vibadminid || $vibadminid!=$admin_name || $vibadminpwd!=$admin_password)
	die($bbs_close_msg);
}

ipbanned();

//$begin_time = micro_time();

session_name('s');
session_start();

$ip=getenv('REMOTE_ADDR');
$ip1 = getenv('HTTP_X_FORWARDED_FOR');
if (($ip1 != "") && ($ip1 != "unknown")) $ip=$ip1;

//--refresh check --
$timestamp = time();
$cookietime = $timestamp+3153600;
if($refresh_allowed) {
	if ($REQUEST_URI == $HTTP_COOKIE_VARS['lastpath'] && ($timestamp-$HTTP_COOKIE_VARS['lastvisit_fr']<$refresh_allowed)) {
		die('本次显示禁止，原因：访问同一URL的刷新时间小于'.$refresh_allowed.'秒');
	}
	setCookie('lastpath', $REQUEST_URI, $cookietime);
	setCookie('lastvisit_fr', $timestamp, $cookietime);
}

//if (empty($HTTP_SESSION_VARS['vibid']) || empty($HTTP_SESSION_VARS['vibpwd'])|| !checkpass($HTTP_SESSION_VARS['vibid'],$HTTP_SESSION_VARS['vibpwd']))
//	$login_status=0; else $login_status=1;

if ($HTTP_SESSION_VARS['vibid']) {
	$login_status=1;
	getUserInfo();
	add_online();
	setcookie('lastvisit',$timestamp,$cookietime);
}else {
	$login_status=0;
	add_guest();
}
//----------------------------------------------------------------------------
/*--------this function comes from Y10K 's goto Search--------------
function micro_time() {
	preg_match('/^0(\\.\d+) (\d+)$/', microtime(), $tmp);
	return (real)$tmp[2].$tmp[1];
}
*/
//+------------------------------------------------------------------------------------------------------
//---------Display the Message Table-------------------
function msg_box($title,$content) {
global $titlecolor,$bordercolor,$subcolor,$tablewidth;
print <<<EOT
<table width=$tablewidth border=0 cellspacing=0 cellpadding=0 align=center bgcolor=$bordercolor>
 <tr>
 <td>
  <table width=100% border=0 cellspacing=1 cellpadding=3>
   <tr bgcolor=$titlecolor> 
    <td background=images/style/catbg.gif>$title</td>
   </tr>
   <tr bgcolor=FFFFFF> 
    <td><br>$content</td>
   </tr>
   <tr align=left bgcolor=$subcolor> 
    <td>&nbsp;</td>
   </tr>
  </table>
 </td>
 </tr>
<TABLE style=BORDER-COLLAPSE: collapse borderColor=#111111 cellSpacing=0 cellPadding=0 width=95% align=center border=0><TBODY><tr>
  <td background=images/style/bottombg.gif><P align=left><IMG height=18 src=images/style/bottomleft.gif width=2 border=0></P></td>
  <td background=images/style/bottombg.gif><P align=right><IMG height=18 src=images/style/bottomright.gif width=2 border=0></P></td>
</TBODY></tr>
</TABLE>
</table>
EOT;
}
//+------------------------------------------------------------------------------------------------------
//---------Display the Navigation Bar-------------------
function navi_bar($des,$l2,$l3="",$echorefresh="yes") {
global $bbs_title,$tablewidth;
  $closepic = "<img src='images/system/closedfold.gif'>";
  $openpic  = "<img src='images/system/openfold.gif'>";
  $barpic   = "<img src='images/system/bar.gif'>";
  $space = "&nbsp; ";
  if ($l3) $l2pic=$closepic; else $l2pic=$openpic;

print 
"
<!----Navigation Bar Begin---->
<table width=$tablewidth border=0 cellspacing=0 cellpadding=0 align=center>
<tr>
<td width=70% align=left valign=middle>你的位置：<br>"
.$space.$closepic."&nbsp;<a href='viboard.php'>$bbs_title</a><br>"
.$space.$barpic.$l2pic."&nbsp;$l2";

if ($l3) echo "<br>".$space.$space.$barpic.$openpic."&nbsp;".$l3;

print "</td><td align=right valign=bottom width=30%>".$des;

if ($echorefresh=="yes") echo '<br><a href="javascript:this.location.reload()"><img src="images/system/refresh.gif" height="12" border=0></a>';

echo "</td></tr></table>
<!----Navigation Bar End----->
";
}

//+------------------------------------------------------------------------------------------------------
//----find out the next available post file name------
function get_next_filename($list,$id) {

  $list=explode("\n",$list,11);
  $count=min(count($list),11);
  $filecount=0;
  for ($i=0; $i<$count; $i++) {
    $temp=explode("|",$list[$i]);
    list($nouse,$thiscount)=explode("_",$temp[5]);
    if ($thiscount>$filecount) $filecount=$thiscount;
  }
  $filecount++;
  while (file_exists("forum$id/f_$filecount")) $filecount++;
  return("f_$filecount");
}

//+------------------------------------------------------------------------------------------------------
//----push the Held Topics to the beginning of the list------

function push_heldtopics($list) {
	$list=explode("\n",$list,12);
	$count=count($list);
	for ($i=0; $i<$count-1; $i++) {
		$detail = explode("|",$list[$i]);
		if ($detail[10] > 2) {
			$held_topics[]=$list[$i];
			unset($list[$i]);
		}
	}
	if ($held_topics) $list= implode("\n",$held_topics)."\n".implode("\n",$list);
		else $list=implode("\n",$list);
	return $list;
}

//+------------------------------------------------------------------------------------------------------
//+-----------read & write------------

function readfromfile($file_name) {
	$filenum=fopen($file_name,"r");
	flock($filenum,LOCK_SH);
	$file_data=fread($filenum,filesize($file_name));
	fclose($filenum);
	return $file_data;
}
function writetofile($file_name,$data,$method="w") {
	$filenum=fopen($file_name,$method);
	flock($filenum,LOCK_EX);
	$file_data=fwrite($filenum,$data);
	fclose($filenum);
	return $file_data;
}

//+------------------------------------------------------------------------------------------------------

function check_permission($user,$type) {
global $admin_name,$forum_admin;
	if ($type=='forum') return 1;
	if ($user==$admin_name) return 1;
	if ($forum_admin && in_array($user,$forum_admin)) return 1;
return 0;
}

//+------------------------------------------------------------------------------------------------------

function get_forum_info() {
global $forum_name,$forum_icon,$forum_type,$forumid,$forumlist;
$forumlist=file("datafile/forumdata.php");
	$count=count($forumlist);
	for ($i=0; $i<$count; $i++) {
		$detail=explode("|", trim($forumlist[$i]));
		if ($detail[3]==$forumid) {
			$forum_name=$detail[1];
			$forum_icon=$detail[4]; 
			$forum_type=$detail[0];
			break;
		}
	}
}

//+------------------------------------------------------------------------------------------------------
//+------------Display the Jump-page---------------

function jump_page($URL,$title,$content) {
global $bordercolor,$forumcolortwo,$backgroundcolor;

$id=session_id();
if (strpos($URL,'?')===false) $URL.="?s=$id";
else $URL.="&s=$id";

print <<<EOT
	<html>
	<head>
	<style type="text/css">
	<!--
	a {color:#333333;font-size: 9pt;text-decoration:none; }
	a:hover {color:#000066;text-decoration:underline overline;}
	td{FONT-SIZE: 9pt; color:#333333; font-family:宋体}
	-->
	</style>
	<title></title>
	<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
	<meta HTTP-EQUIV=REFRESH CONTENT='4; URL=$URL'>
	<script language="Javascript">
		function Show(divid) {
		divid.filters.revealTrans.apply(); 
		divid.style.visibility = "visible"; 
		divid.filters.revealTrans.play(); 
	}
	function Hide(divid) {
		divid.filters.revealTrans.apply();
		divid.style.visibility = "hidden";
		divid.filters.revealTrans.play();
	}
	</script>
	</head>

	<body bgcolor=$backgroundcolor Onload="Show(msgboard)">

	<div id="msgboard" style="FILTER: revealTrans(transition=23,duration=0.5) blendTrans(duration=0.5); position:absolute; left:210px; top:150px; width:350px; height:100px; z-index:1; visibility: hidden">
	    <table width="100%" border=0 cellspacing=0 cellpadding=6 bgcolor=$forumcolortwo>
	     <tr>
	      <td>$content</td>
	    </tr>
	  </table>
	</div>
	<script>
	setTimeout('Hide(msgboard)',3000);
	</script>
	</body>
	</html>
EOT;
exit;
}

//+------------------------------------------------------------------------------------------------------
//+---------Ban some IPs---------------

function ipbanned() {
global $ip;
if (!file_exists("datafile/ipbans.php")) return;
	$term_bannedmembers = file("datafile/ipbans.php");
	if (empty($term_bannedmembers)) return;
	$count = count($term_bannedmembers);
	for ($i=0; $i<$count; $i++) {
		$bannedip = trim($term_bannedmembers[$i]);

		if (!$bannedip) continue;
		if ( strpos($ip , $bannedip)===0 ) {
			print (
'<html><head><title>IP 被禁止</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body bgcolor=FFFFFF><br>
<font color=000000 size=2>
对不起，你的 IP 被禁止，可能你没有遵守本站的规定<br><br>
如有疑问，请联系管理员。<br><br>
[<a href="javascript:history.go(-1)">点击这里返回前一个页面</a>]
</body>
</html>');
			exit;		
		}
	}
}

//+------------------------------------------------------------------------------------------------------
//+-----------User functions---------------------

function get_user_portait($usericon) {
	$portait=explode('%',$usericon);
	if (empty($usericon)) return '';
	if ($portait[1] && $portait[2] && $portait[3])
	return "<img src='$portait[1]' width=$portait[2] height=$portait[3] border=0>";
	else return "<img src='images/face/$portait[0]' border=0>";
}

function getUserInfo() {
global $vibid,$username,$regdate,$postamount,$level,$gotNewMessage,$id_unique;
	$username=$vibid;
	if (file_exists($id_unique."/".$username))
	$usertemp=readfromfile($id_unique."/".$username);
	$userdetail=explode("|",$usertemp);
	$regdate=$userdetail[5];
	$postamount=$userdetail[12];
	//$level=getUserLevel($postamount,$username);
	$gotNewMessage=0;
	if (file_exists($id_unique."/".$username.".rec")) {
		$messages=file($id_unique."/".$username.".rec");
		$msgcount=count($messages);
		for ($i=0; $i<$msgcount; $i++) {
			$msgdetail=explode("|",$messages[$i]);
			if ($msgdetail[4]==0) $gotNewMessage++;
		}
	}
}

function getUserLevel($amount,$username) {
global $forum_admin,$admin_name,$mtitle,$mpostmark,$admintitle,$motitle;

 $user_level=$mtitle[0];
 $count=count($mpostmark);

 for ($i=1; $i<$count; $i++)
   if ($mpostmark[$i] && $amount>=$mpostmark[$i]) $user_level=$mtitle[$i];

  if ($motitle && $forum_admin && in_array($username,$forum_admin)) $user_level=$motitle;
  if ($admintitle && $username==$admin_name) $user_level=$admintitle;

  return $user_level;
}

function getNextLevel($amount,$username) {
global $forum_admin,$admin_name,$mtitle,$mpostmark,$admintitle,$motitle;

 $next_level=$mpostmark[1];
 
 $count=count($mpostmark);

 for ($i=1; $i<$count; $i++)
   if ($mpostmark[$i] && $amount>=$mpostmark[$i]) $next_level=$mpostmark[$i+1];
return $next_level;
}

function getUserIcon($amount,$username) {
global $forum_admin,$admin_name,$mgraphic,$mpostmark,$admingraphic,$modgraphic;

  $usericon=$mgraphic[0];
  $count=count($mpostmark);

  for ($i=1; $i<$count; $i++)
    if ($mpostmark[$i] && $amount>=$mpostmark[$i]) $usericon=$mgraphic[$i];

  if ($modgraphic && $forum_admin && in_array($username,$forum_admin)) $usericon=$modgraphic;
  if ($admingraphic && $username==$admin_name) $usericon=$admingraphic;

  return $usericon;
}

function get_user_info($user) {
global $id_unique;
	if(!file_exists("$id_unique/$user")) return 0;
	$useri=explode("|",readfromfile("$id_unique/$user"));
	return $useri;
}

function checkpass($username,$password){
global $id_unique;
	if (!file_exists("$id_unique/$username") || strpos($username,"..")!==false) return 0;
	$useri=explode("|",readfromfile("$id_unique/$username"));
	$userpwd=$useri[1];
	if ($password==$userpwd) return 1; else return 0;
}

//+------------------------------------------------------------------------------------------------------
//+-------------Online Functions------------------

//-----Add members to online file--------
function add_online() {
global $privacy,$vibid,$timestamp,$ip,$online_limit,$forumid;
$onlinefile="datafile/online.php";
if ($privacy) return;
	$username=$vibid;

	if (!$forumid) $forumid=0;
	if (file_exists($onlinefile)) {
	  $online_user=trim(readfromfile($onlinefile));
	  $online_user=explode("\n",$online_user);
	  $new_online_user='';

	  $online_user_count=count($online_user);
	  for($i=0; $i<$online_user_count; $i++) {
		$online_user_detail=explode("|",$online_user[$i]);
		if ($timestamp-$online_user_detail[1]<=$online_limit
		     && $online_user_detail[0]!=$username
		     && $online_user_detail[2]!=$ip)
		      $new_online_user.=$online_user[$i]."\n";
	  }
	  $new_online_user.="$username|$timestamp|$ip|$forumid";
	} else $new_online_user="$username|$timestamp|$ip|$forumid";

	writetofile($onlinefile,$new_online_user);
}

//+-----------add guests to online file-----------------
function add_guest() {
global $timestamp,$ip,$online_limit,$forumid;
	$onlinefile="datafile/online.php";

	if (!$forumid) $forumid=0;
	if (file_exists($onlinefile)) {
	  $online_user=trim(readfromfile($onlinefile));
	  $online_user=explode("\n",$online_user);
	  $new_online_user='';
	  $online_user_count=count($online_user);
	  for($i=0; $i<$online_user_count; $i++) {
		$online_user_info=explode("|",$online_user[$i]);
		if (($timestamp-$online_user_info[1])<=$online_limit
		&& $online_user_info[2]!=$ip) {
			$new_online_user.=$online_user[$i]."\n";
		}
	  }
  	  $new_online_user.="guest|$timestamp|$ip|$forumid";
	}else $new_online_user="guest|$timestamp|$ip|$forumid";

	writetofile($onlinefile,$new_online_user);
}

//+------------------------------------------------------------------------------------------------------
//+----------kick out the dangerous Char---------
function safe_convert($d) {
  $d = str_replace("\t","",$d);
  $d = str_replace("<","&lt;",$d);
  $d = str_replace(">","&gt;",$d);
  $d = str_replace("\r","<br>",$d);
  $d = str_replace("\n","",$d);
  $d = str_replace("|","│",$d);
  $d = str_replace("  "," &nbsp;",$d);
  return $d;
}

//+------------------------------------------------------------------------------------------------------
//+-------To see where the IP comes from------------------
function convert_ip($ip) {
	$ip_detail=explode(".",$ip);
	if (file_exists("datafile/$ip_detail[0].txt")) $ip_data=file("datafile/$ip_detail[0].txt");
	else $ip_data=file("datafile/0.txt");
	for ($i=0; $i<=3; $i++) {
		$ip_detail[$i]     = sprintf("%03d", $ip_detail[$i]);
	}
	$ip=join(".",$ip_detail);
	$count=count($ip_data);
	$count=$count-1;
	for ($i=0; $i<=$count; $i++) {
		$ip_data_detail=explode("|",$ip_data[$i]);
		if ($ip>=$ip_data_detail[0] && $ip<=$ip_data_detail[1]) return $ip_data_detail[2].$ip_data_detail[3];
	}
	if ($ip_detail[0]=="127") return "本地机器";
	return "未知地区";
}
//+--------------To tell the user's Browser--------------------------
function browseinfo() {
$browser="";$browserver="";
$Browsers =array("Lynx","MOSAIC","AOL","Opera","JAVA","MacWeb","WebExplorer","OmniWeb");
$Agent = $GLOBALS["HTTP_USER_AGENT"];
for ($i=0; $i<=7; $i++) {
if (strpos($Agent,$Browsers[$i])) {
$browser = $Browsers[$i];
$browserver ="";
}
}
   if (ereg("Mozilla",$Agent) && !ereg("MSIE",$Agent)) {
      $temp =explode("(", $Agent); $Part=$temp[0];
      $temp =explode("/", $Part); $browserver=$temp[1];
      $temp =explode(" ",$browserver); $browserver=$temp[0];
      $browserver =preg_replace("/([\d\.]+)/","\\1",$browserver);
      $browserver = " $browserver";
      $browser = "Netscape Navigator";
   }
   if (ereg("Mozilla",$Agent) && ereg("Opera",$Agent)) {
      $temp =explode("(", $Agent); $Part=$temp[1];
      $temp =explode(")", $Part); $browserver=$temp[1];
      $temp =explode(" ",$browserver);$browserver=$temp[2];
      $browserver =preg_replace("/([\d\.]+)/","\\1",$browserver);
      $browserver = " $browserver";
      $browser = "Opera";
   }
if (ereg("Mozilla",$Agent) && ereg("MSIE",$Agent)) {
$temp = explode("(", $Agent); $Part=$temp[1];
$temp = explode(";",$Part); $Part=$temp[1];
$temp = explode(" ",$Part);$browserver=$temp[2];
$browserver =preg_replace("/([\d\.]+)/","\\1",$browserver);
$browserver = " $browserver";
$browser = "微软探索者(IE)";
}
if ($browser!="") {
$browseinfo = "$browser$browserver";
}
else {
$browseinfo = "未知的浏览器";
}
return $browseinfo;
}
//-----------------To tell the User's OS------------------------
function osinfo() {
$os="";
$Agent = $GLOBALS["HTTP_USER_AGENT"];
if (eregi('win',$Agent) && strpos($Agent, '95')) {
$os="Windows 95";
}
elseif (eregi('win 9x',$Agent) && strpos($Agent, '4.90')) {
$os="Windows ME";
}
elseif (eregi('win',$Agent) && ereg('98',$Agent)) {
$os="Windows 98";
}
elseif (eregi('win',$Agent) && eregi('nt 5\.0',$Agent)) { 
$os="Windows 2000"; 
} 
elseif (eregi('win',$Agent) && eregi('nt 5\.1',$Agent)) { 
$os="Windows XP"; 
} 
elseif (eregi('win',$Agent) && eregi('nt',$Agent)) {
$os="Windows NT";
}
elseif (eregi('win',$Agent) && ereg('32',$Agent)) {
$os="Windows 32";
}
elseif (eregi('linux',$Agent)) {
$os="Linux";
}
elseif (eregi('unix',$Agent)) {
$os="Unix";
}
elseif (eregi('sun',$Agent) && eregi('os',$Agent)) {
$os="SunOS";
}
elseif (eregi('ibm',$Agent) && eregi('os',$Agent)) {
$os="IBM OS/2";
}
elseif (eregi('Mac',$Agent) && eregi('PC',$Agent)) {
$os="Macintosh";
}
elseif (eregi('PowerPC',$Agent)) {
$os="PowerPC";
}
elseif (eregi('AIX',$Agent)) {
$os="AIX";
}
elseif (eregi('HPUX',$Agent)) {
$os="HPUX";
}
elseif (eregi('NetBSD',$Agent)) {
$os="NetBSD";
}
elseif (eregi('BSD',$Agent)) {
$os="BSD";
}
elseif (ereg('OSF1',$Agent)) {
$os="OSF1";
}
elseif (ereg('IRIX',$Agent)) {
$os="IRIX";
}
elseif (eregi('FreeBSD',$Agent)) {
$os="FreeBSD";
}
 if ($os=='') $os = "Unknown";
return $os;
}

//+------------------------------------------------------------------------------------------------------
//+--------Date & Time Functions-----------------------------------------
function get_date_chi($datetime){
	$t=getdate($datetime);
	$year=$t['year'];
	$mon=$t['mon'];
	$mday=$t['mday'];
	return "{$year}年{$mon}月{$mday}日 ";
}
function getfulldate($datetime){
	$t=getdate($datetime);
	$hour=$t['hours'];
	$min=$t['minutes'];
	if (strlen($min)==1) $min="0".$min;
	$year=$t['year'];
	$mon=$t['mon'];
	$mday=$t['mday'];
	return "{$year}年{$mon}月{$mday}日{$hour}:{$min}";
}
function get_time($datetime){
	$t=getdate($datetime);
	$hour=$t['hours'];
	$min=$t['minutes'];
	if (strlen($min)==1) $min="0".$min;
	return "{$hour}:{$min}";
}
function get_date($datetime){
	$t=getdate($datetime);
	$year=$t['year'];
	$mon=$t['mon'];
	$mday=$t['mday'];
	return "{$year}-{$mon}-{$mday}";
}

