<?
require("datafile/config.php");
require("datafile/style.php");
require("global.php");

$add_title=" &gt; 搜索程式";

if (empty($keyword)) {
//---------input keyword---------
	include("header.php");
	print_bar();
	print_form();
	include("footer.php");
	exit;
}

$check=1;
if (strlen($keyword)<=3) {
	include("header.php");
	print_bar();
	print_err();
	include("footer.php");
	exit;
}

$keyword= str_replace("，",",",$keyword);
$keyword_list=explode(",",$keyword);
$keyc=count($keyword_list);
if (empty($forumid) || $forumid=="all") {
	$forumlist=file("datafile/forumdata.php");
	$fcount=count($forumlist);
	for ($i=0; $i<$fcount; $i++) {
		$detail=explode("|",$forumlist[$i]);
		if ($detail[0]!="category") $forumtosearch[]=$detail[3];
	}
}
	else $forumtosearch[]=$forumid;
	
$fcount=count($forumtosearch);

if (empty($page)) $page=1;
if (empty($method)) $method="or";

$maxresult=($page*$perpage);
$resultcount=0;
$more=0;

for ($i=0; $i<$fcount; $i++) {
	$listtosearch=file("forum$forumtosearch[$i]/list.php");
	$lcount=count($listtosearch);
	for ($j=0; $j<$lcount; $j++) {
		//-------or搜索-------------
		if ($method=="or") {
			for ( $k = 0; $k < $keyc; $k++) 
				if ( strpos( $listtosearch[$j] , $keyword_list[$k] ) !== false ) {
					$result[] = array($listtosearch[$j],$forumtosearch[$i]);
					$resultcount++;
					break;
				}
		}
		//-------and搜索------------
		elseif ($method=="and") {
			$bypass = 1;
			for ( $k = 0; $k < $keyc; $k++) {
				if ( strpos( $listtosearch[$j] , $keyword_list[$k] ) === false ) {
					$bypass=0;
					break;
				}
			}
			if ( $bypass==1 ) {
				$result[] = array($listtosearch[$j],$forumtosearch[$i]);
				$resultcount++;
			}
		}
	if ($resultcount==$maxresult) {$more=1; break;}
	}
if ($resultcount==$maxresult) break;
}

if (empty($result)) {
	include("header.php");
	print_bar();
	print_none();
	include("footer.php");
	exit;
}

/*$result_count=count($result);
if ($result_count%$perpage==0) $maxpageno=$result_count/$perpage;
	else $maxpageno=floor($result_count/$perpage)+1;
if ($page>$maxpageno) { $page=$maxpageno; $pagemax=$result_count-1; $pagemin=max(0,$result_count-$perpage);}
	elseif ($page==1) {$pagemin=0; $pagemax=min($result_count-1,$perpage-1); }
	else { $pagemin=min($result_count-1,$perpage*($page-1)); $pagemax=min($result_count-1,$pagemin+$perpage-1); }*/
//-----显示结果-------
include("header.php");
print_bar();
echo "<br>";
sline(); ?>
<table cellspacing=0 cellpadding=0 height=23 width=<?=$tablewidth?> align=center border=1 bgcolor=<?=$titlecolor?> bordercolor=<?=$bordercolor?>>
  <tr align=center> 
    <td width=32 background=images/style/catbg.gif><b>状态</b></td>
    <td width=* background=images/style/catbg.gif><b>主&nbsp;&nbsp;题</b> (点心情符为开新窗浏览)</td>
    <td width=85 background=images/style/catbg.gif><b>作 者</b></td>
    <td width=32 background=images/style/catbg.gif><b>回复</b></td>
    <td width=32 background=images/style/catbg.gif><b>点击</b></td>
    <td width=195 background=images/style/catbg.gif><b>最后参与 | 参与者</b></td>
  </tr>
</table>
<?
$maxresult=min($maxresult,$resultcount);
for ($i=max(0,$maxresult-$perpage); $i<$maxresult; $i++) {
	list($a_info,$forumid)=$result[$i];
	article_line();
}
?>
<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 cellspacing=2 cellpadding=0 width=<?=$tablewidth?> align=center border=0>
  <tr height=4></tr>
  <tr> 
    <td align=left>搜索信息：
<?
echo " 已经搜索到了 $resultcount 条信息";
      $nextpage=$page+1;
      $previouspage=$page-1;
if ($more) echo " --- [ <a href='search.php?keyword=$keyword&page=$nextpage&method=$method'>搜索下 $perpage 个结果</a> ]";
if ($page>1) echo " [ <a href='search.php?keyword=$keyword&page=$previouspage&method=$method'>返回上 $perpage 个结果</a> ]";
?></td></tr></table>
<br>
<?
include("footer.php");
exit;
function sline() {
global $bordercolor,$tablewidth;
print ("<table cellspacing=0 cellpadding=0 width=$tablewidth align=center bgcolor=$bordercolor border=0><tr><td height=1></td></tr></table>");
}
function article_line() {
global $a_info,$forumid,$list_color1,$list_color2,$bordercolor,$username,$tablewidth,$timestamp;
list($title,$author,$date,$des,$icon,$filename,$reply,$hit,$last_mod_data,$islock,$topic_type)=explode("|",$a_info);

if (strlen($author)>=12) $viewauthor=substr($author,0,9)."...";
	else $viewauthor=$author;

if ($author==$username) $mypost="<img src='images/system/mypost.gif' border=0 width=14 height=14>";
else $mypost="&nbsp;";

if ($icon=="ran") $icon=mt_rand(0,25).".gif";
$icon="<a target=_blank href='vibread.php?forumid=$forumid&filename=$filename'><img src='images/emotion/$icon' alt=开新窗口浏览此主题 height=13 width=13 border=0></a>";
if ($topic_type==1) {
	$stats="<img src='images/system/statistic.gif'>";
	if ($islock==1) $stats="<img src='images/system/closesta.gif'>";
} elseif ($topic_type==2) {
	$stats="<img src='images/system/ucommend.gif' width=13 height=16>";
	if ($islock==1) $stats="<img src='images/system/closeu.gif' width=13 height=16>";
} elseif ($topic_type>=3) {
	$stats="<img src='images/system/holdtopic.gif'>";
} else {
	if ($username!=$author) {
		$stats="<img src='images/system/topicnew.gif'>";
		if ($reply>=10) $stats="<img src='images/system/topichot.gif'>";
		if ($islock==1) $stats="<img src='images/system/topiclocked.gif'>";
	} else {
		$stats="<img src='images/system/mytopicnew.gif'>";
		if ($reply>=10) $stats="<img src='images/system/mytopichot.gif'>";
		if ($islock==1) $stats="<img src='images/system/mytopiclocked.gif'>";
	}
}

if (strlen($title)>=40) $title=substr($title,0,37)."...";
$title="<a href='vibread.php?forumid=$forumid&filename=$filename'\">$title</a>";
$lmd=explode(",",$last_mod_data);
$g=$timestamp-$lmd[2];
if ($g<=3600) $title.='<img src="images/system/newred.gif" width="34" height="15">';
elseif ($g<=(3600*24)) $title.='<img src="images/system/newblue.gif" width="34" height="15">';
elseif ($g<=(3600*48)) $title.='<img src="images/system/newgreen.gif" width="34" height="15">';
if ($lmd[2]==$date) $lmdauthor="------";
else $lmdauthor="<a href=\"vibprofile.php?job=show&target=$lmd[1]\">$lmd[1]</a>";
$lmdtime=get_date($lmd[2]).' '.get_time($lmd[2]);
print ("
<table cellspacing=0 cellpadding=0 width=$tablewidth align=center border=1 bordercolor=$bordercolor>
<tr align=center height=25 bgcolor=$list_color1>
<td width=32>$stats</td>
<td width=* bgcolor=$list_color2 align=left>&nbsp;$icon&nbsp;<acronym title=\"$des\">$title</acronym></td>
<td width=85 ><a href=\"vibprofile.php?job=show&target=$author\">$viewauthor</a></td>
<td width=32 bgcolor=$list_color2>$reply</td>
<td width=32 bgcolor=$list_color2>$hit</td>
<td width=195 align=left>&nbsp; &nbsp;{$lmdtime}&nbsp;<font color=#000066>|</font>&nbsp;$lmdauthor</td>
</tr>
</table>
");
}
function print_form() {
global $subcolor,$bordercolor,$titlecolor,$tablewidth;
?>
<table width=<?=$tablewidth?> border=0 cellspacing=0 cellpadding=0 align=center bgcolor=<?=$bordercolor?>>
<form action="search.php" method=post>
<tr>
<td>
<table width=100% border=0 cellspacing=1 cellpadding=3>
<tr bgcolor=<?=$titlecolor?>>
<td background=images/style/catbg.gif>SEARCH 搜索程式</td>
</tr>
<tr bgcolor=#FFFFFF> 
<td>
<br>
请输入关键字： <br>
<br>
<input type=text name=keyword size=20>
(3个字节以上,关键字用逗号分开)<br>
<br>
选择搜索方式：<br>
<br>
<input type=radio name="method" value="or" checked>
多关键字连续搜索方式 
<input type=radio name="method" value="and">
多关键字共同搜索方式<br>
<br>
选择要搜索的版块： <br>
<br>
<select name=forumid>
<?
$forumlist=file("datafile/forumdata.php");
$count=count($forumlist)-1;
for ($i=0; $i<=$count; $i++) {
	$forum_list_info=explode("|",$forumlist[$i]);
	if ($forum_list_info[0]!="category") echo "<OPTION value=\"$forum_list_info[3]\">$forum_list_info[1]";
}
?>
<OPTION value="all">全部版块
</OPTION>
</select>
</td>
</tr>
<tr align="center" bgcolor="<?=$subcolor?>">
<td>
<input type="submit" value="送出资料" style="height:20px;background-color:#f3f3f3;border:1 solid black" onMouseOver ="this.style.backgroundColor='#FFC864'" onMouseOut ="this.style.backgroundColor='#f3f3f3'">
&nbsp;
<input type="reset" value="清除重写" style="height:20px;background-color:#f3f3f3;border:1 solid black" onMouseOver ="this.style.backgroundColor='#FFC864'" onMouseOut ="this.style.backgroundColor='#f3f3f3'">
</td>
</tr>
</table>
</td>
</tr>
</form>
<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>
<?
}
function print_err() {
msg_box('SEARCH 搜索程式','
<br>
哎呀，你也真是调皮也<br>
<br>
刚才不是说了么 ~~~~ 长度要大于3<br>
<ul>
<li><a href="javascript:history.back(1);">返回前页</a></li>
</ul>');
}
function print_none() {
global $subcolor,$bordercolor,$titlecolor;
msg_box('SEARCH 搜索程式','
<br>
有没有搞错....... <br>
<br>
一条结果也没有 <br>
<ul>
<li><a href="javascript:history.back(1);">返回前页</a></li>
</ul>');
}
function print_bar() {
navi_bar('你现在正在<br>VIB 论坛<br>的SEARCH程式中','搜索程式','','no');
}
