<?php
/*
\* ===========================================
\* 项目: CTB文本论坛
\* 版本: 1.3
\* 团队: 实易数码
\* 小组: CTB文本论坛小组
\* 作者: 险情
\* 功能: 搜索模块
\* ===========================================
\* Copyright (c) 2002 
\* 团队主页: http://www.11cn.org
\* 团队信箱: -------------------
\* 创建日期: 2002.6
\* 修改日期: 2003-4-10
\* 修改说明: 修复分页bug,部分重复代码分开为单独函数,
\*            新函数为 doSearch($forumid){}
\* 版权声明: 这个程序是免费软件.
\* ===========================================
*/

class Module extends CommonClass
{
    
    /**
    *构造函数
    *
    */
    function execute()
    {
		$this->openLimit();
        switch($this->input['action']) {
            case 'search':
                $this->searchShow();
                break;
            case 'search_act':
                $this->search();
                break;
            default:
                $this->searchShow();
                break;
        }
    } // end func


    /**
     *功能: 
     *参数: 
     *返回: 
     *说明: 
     *创建: 
     *修改: 
     **/
    function search() {
        $keyword = $this->input['keyword'];
        if (!trim($keyword)) {
            $this->showMsg("请输入您要搜索的关键字！");
        }
        if (strlen($keyword) > 45) { 
            $this->showMsg("您填写的关键字太长了!"); 
        }
        $keyword=trim($keyword); 
        $BadKeywordArray = array("?", " ", "[", "]", "{", "}", "(", ")", "", "+", "\\", "|", "$", "<", ">"); 
		$this->file = $this->set[dataPath]."/badword.php";
		$bad_word_lines = $this->read_file();
		if (count($bad_word_lines)) {
			foreach ($bad_word_lines as $bad_word_line) {
				$bad_word_code = $this->make_array($bad_word_line);
				$bad_word[] = $bad_word_code[2];
			}
			$BadKeywordArray = array_merge($BadKeywordArray, $bad_word);
		}
		for ($j = 0 ; $j < sizeof($BadKeywordArray); $j++) {
            if (strstr($keyword, $BadKeywordArray[$j])) { 
                $keyword = ""; 
            }
        }
        if (empty($keyword)) {
            $this->showMsg("您填入了非法字符，如：空格、标点符号、不雅词语等");  // changed by cx
        }
        if (empty($this->input['content']) && empty($this->input['topic']) && empty($this->input['author'])) {
            $this->showMsg("请选择搜索类型");
        }
        if (empty($this->input['title']) && empty($this->input['reply'])) {
            $this->showMsg("请选择帖子类型");
        }
        if ($this->input['forumid'] != "all") {
            $this->searchForum();   
        } else {
            $this->searchForums();
        }
    }

    /**
    *显示搜索页面
    *218.15.123.55
    */
    function searchShow()
    {

        $this->BAN = " &gt; 论坛搜索";
        $header = $this->header("论坛搜索");
    
        $this->file = $this->set[dataPath]."/forums.php";
        $forum = $this->read_file();
        $optionLine = "";
        foreach ($forum as $line) {
            $lineCode = $this->make_array($line);
            if ($lineCode[1] == "forum") {
                $optionLine .= "<option value=".$lineCode[2].">　|-".$lineCode[3]."</option>";
            }
        }
        
        eval("\$this->outPut(\"".$this->getTemplate("search")."\");");
    } // end func    

    /**
    *搜索一个版块
    *
    */
    function searchForum()
    {

        $this->file = $this->set[dataPath]."/forums.php";
        $forums = $this->read_file();
        $optionLine = "<option value=\"no\">跳转论坛至...</option>";
        foreach ($forums as $line) {
            $lineCode = $this->make_array($line);
            if ($lineCode[1] == "forum") {
                $optionLine .= "<option value=".$lineCode[2].">　|-".$lineCode[3]."</option>";
            }
            if ($lineCode[1] == "cat") {
                $optionLine .= "<option value=\"no\">╋".$lineCode[3]."</option>";
            }
            if($lineCode[2] == $this->input['forumid']){
                $forum = $lineCode;
            }
        }
        $this->forumList = "<select name='forumid'  class='input5'>\n\t".$optionLine."\n</select>";
        $check = $this->checkForum($this->input['forumid']);
        if ($check[0] == "0") {
            $this->showMsg("您无权查看这个版块,若想查看，请联系管理员","./index.php");
        }
            
        if (!empty($this->input['author'])) {
            $this->file = $this->set[dataPath]."/users/list.php";
	        $userInfo = $this->select_line("4",$this->input['keyword']);
            $this->authorId = $userInfo[1];
        } 
        $IDX = $this->doSearch($this->input['forumid']);

        $this->BAN = " &gt; 搜索结果 &gt; ".$forum[3];
        $header = $this->header("搜索结果");

        $totalFile = count($IDX);
        $recnum = $this->set[topicList];
        $totalNum = ($totalFile % $recnum) == "0" ? intval(($totalFile)/$recnum) : intval(($totalFile)/$recnum)+1;
        $p = $this->input['p'];
        if ( !$p ) { $p = 1; }
        for( $i = ($p-1)*$recnum; $i < $p*$recnum ;$i++) {
            $searchFile = $this->make_array($IDX[$i]);
            $this->file = $this->set[dataPath]."/".$this->input['forumid']."/".$searchFile[1]."_.php";
            if (file_exists($this->file)) {
                $line = $this->get();
                $postTitle = $line[2];
				$postTitle = $this->set[replaceWhenPost] == 'Y' ? $postTitle : $this->replaceBadWord($postTitle);
                $id = $line[1];
                $clickNumber = $line[4];
                $topicNumber = $line[3];
                $reUserName = $line[5];
                $lastTime = date($this->set[dateformat],$line[6]);
                $topicPic = $line[7];
                $topicAuthor = str_replace($this->input['keyword'],"<font color=red>".$this->input['keyword']."</font>",$searchFile[2]);;
                $postTitle = str_replace($this->input['keyword'],"<font color=red>".$this->input['keyword']."</font>",$line[2]);
                
                if (empty($topicPic)) {
                    $topicPic = "smileyface.gif";
                }
                if (($this->sess['sess_last_login'] != "") && ($line[6] > $this->sess['sess_last_login'])) {
                    $newMode = "<img src=\"{$this->tplPath}/images/system/topic_new.gif\">";
                } else {
                    $newMode = "";
                }
                if ($topicCode[5] == "1" && $topicCode[4]!="1"){
                    $newFlag = "foldertop.gif";
                }elseif ($topicCode[4] == "1" && $topicCode[5]=="1"){
                    $newFlag = "folder_lock_top.gif";
                }elseif ($topicCode[4] == "1" && $topicCode[5]!="1"){
                    $newFlag = "folderlock.gif";
                }elseif ($topicNum >= 10 && $topicNum <20){
                    $newFlag = "folderhot.gif";
                }elseif ($topicNum >= 20){
                    $newFlag = "folderhot1.gif";
                }else{
                    $newFlag = "folder.gif";
                }

                $countFile = $line[3];
                $pageNum = ($countFile % $this->set[postList]) == "0" ? intval(($countFile)/$this->set[postList]) : intval(($countFile)/$this->set[postList])+1;
                if ($pageNum < 2) {
                    $lastPage = "";
                } else {
                    $lastPage = "&#171; <a href=./index.php?mods=topicdisplay&forumid={$this->input['forumid']}&postid=$id&p=$pageNum>$pageNum</a> &#187;";
                }
                eval ("\$postList .= \"".$this->getTemplate("forumPost")."\";");
            }
        }

        $page = $this->getPage("$PHP_SELF?mods=search&action=search_act&forumid={$this->input['forumid']}&content={$this->input['content']}&keyword={$this->input['keyword']}&title={$this->input['title']}&reply={$this->input['reply']}&topic={$this->input['topic']}&author={$this->input['author']}",$totalNum);
        eval("\$this->outPut(\"".$this->getTemplate("forum")."\");");

    }// end func  
    
    /**
    *搜索全部版块
    *
    */
    function searchForums()
    {

        if (!empty($this->input['author'])) {

            $this->file = $this->set[dataPath]."/users/list.php";
	        $userInfo = $this->select_line("4",$this->input['keyword']);
            $this->authorId = $userInfo[1];
        } 

        $this->file = $this->set[dataPath]."/forums.php";
        $forumLines = $this->read_file();
        $optionLine = "";
        foreach ($forumLines as $forumLine) {
            $forum = $this->make_array($forumLine);
            if ($forum[1] == "cat") {
                $optionLine .= "<option value=\"no\">╋".$forum[3]."</option>";
                }
            if ($forum[1] == "forum") {
                $forumid = $forum[2];
                $optionLine .= "<option value=".$forum[2].">　|-".$forum[3]."</option>";

                $check = $this->checkForum($forumid);
                if ($check[0] == "0")
                    continue;

                $tmpIdx = $this->doSearch($forumid);
                if ($tmpIdx != "") {
                    foreach ($tmpIdx as $tmpLine ) {
                        $IDX[] = $tmpLine;        
                    }
                }
            }
        }
        $forumList = "<select name='forumid'  class='input5'>".$optionLine."</select>";
        $this->BAN = " &gt; <a href=\"./index.php?mods=search&action=search\">文章搜索</a> &gt; 所有版块";
        $header = $this->header("搜索结果");

        $totalFile = count($IDX);
        for ($i=0;$i<$totalFile;$i++) {
            $line = $this->make_array($IDX[$i]);
            $lines[$IDX[$i]] = $line[3];
        }
        @arsort($lines);
        @reset($lines);
        $recnum = $this->set[topicList];
        $totalNum = ($totalFile % $recnum) == "0" ? intval(($totalFile)/$recnum) : intval(($totalFile)/$recnum)+1;
        $p = $this->input['p'];
        if ( !$p ) { $p = 1; }
        for( $i = "0"; $i < $p*$recnum ;$i++) {
            if ($i >= ($p-1)*$recnum) {
                @$newIdx = key($lines);
            
                $forumNew = $this->make_array($newIdx);
                $this->file = $this->set[dataPath]."/forums.php";
                $forums = $this->read_file();
                $optionLine = "";
                foreach ($forums as $line) {
                    $lineCode = $this->make_array($line);
                    if($lineCode[2] == $forumNew[12]){
                        $forum = $lineCode;
                        break;
                    }
                }
                $forumName = $forum[3];
                $this->file = $this->set[dataPath]."/".$forumNew[12]."/".$forumNew[1]."_.php";
                if (file_exists($this->file)) {
                    $line = $this->get();
                    $postTitle = str_replace($this->input['keyword'],"<font color=red>".$this->input['keyword']."</font>",$line[2]);
					$postTitle = $this->set[replaceWhenPost] == 'Y' ? $postTitle : $this->replaceBadWord($postTitle);
                    $id = $line[1];
                    $clickNum = $line[4];
                    $topicNum = $line[3];
                    $reUserName = $line[5];
                    $lastTime = date($this->set[dateformat],$line[6]);
                    $topicPic = $line[7];
                    $forumid = $forumNew[12];
                    $topicAuthor = str_replace($this->input['keyword'],"<font color=red>".$this->input['keyword']."</font>",$forumNew[2]);
                    if (empty($topicPic)) {
                        $topicPic = "smileyface.gif";
                    }
                    if (($this->sesss['sess_last_login'] != "") && ($line[6] > $this->sesss['sess_last_login'])) {
                        $newMode = "<img src='{$this->tplPath}/images/system/topic_new.gif'>";
                    } else {
                        $newMode = "";
                    }
                    if ($topicCode[5] == "1" && $topicCode[4] != "1"){
                        $newFlag = "foldertop.gif";
                    }elseif ($topicCode[4] == "1" && $topicCode[5] == "1"){
                        $newFlag = "folder_lock_top.gif";
                    }elseif ($topicCode[4] == "1" && $topicCode[5] != "1"){
                        $newFlag = "folderlock.gif";
                    }elseif ($topicNum >= 10 && $topicNum <20){
                        $newFlag = "folderhot.gif";
                    }elseif ($topicNum >= 20){
                        $newFlag = "folderhot1.gif";
                    }else{
                        $newFlag = "folder.gif";
                    }
                    $countFile = $line[3];
                    $pageNum = ($countFile % $this->set[postList]) == "0" ? intval(($countFile)/$this->set[postList]) : intval(($countFile)/$this->set[postList])+1;
                    if ($pageNum < 2) {
                        $lastPage = "";
                    } else {
                        $lastPage = "&#171; <a href=./index.php?mods=topicdisplay&forumid=$forumid&postid=$id&p=$pageNum>$pageNum</a> &#187;";
                    }
                    eval ("\$postList .= \"".$this->getTemplate("searchPost")."\";");
                }
            }
            if (!(@next($lines))){
                break;
            }
        }

        $page = $this->getPage("$PHP_SELF?mods=search&action=search_act&forumid=all&content={$this->input['content']}&keyword={$this->input['keyword']}&title={$this->input['title']}&reply={$this->input['reply']}&topic={$this->input['topic']}&author={$this->input['author']}",$totalNum);
        eval("\$this->outPut(\"".$this->getTemplate("searchShowAll")."\");");

    } // end func 

    //=============================================================================
    function doSearch($forumid)
    {

        @$SEARCH_IDX = file($this->set[dataPath]."/".$forumid."/list.php");
        $end = count ($SEARCH_IDX);
    	for($i=0; $i<=$end-1; $i++) {
            $searchFile = $this->make_array($SEARCH_IDX[$i]);
        	if(file_exists($this->set[dataPath]."/".$forumid."/".$searchFile[1].".php")) {
                $FILES_ARRAY_DATA = file($this->set[dataPath]."/".$forumid."/".$searchFile[1].".php");
                $searchCount = count($FILES_ARRAY_DATA);
            	if (!empty($this->input['title']) && empty($this->input['reply'])) {
                    $FILES_ARRAY = $this->make_array($FILES_ARRAY_DATA[0]);
                	if (!empty($this->input['topic']) && empty($this->input['content']) && empty ($this->input['author'])) {
                    	if ( eregi($this->input['keyword'], $FILES_ARRAY[2])) {
                            $IDX[] = $SEARCH_IDX[$i]."\x0E".$forumid;
                        }
                    }
                	if (empty($this->input['topic']) && !empty($this->input['content']) && empty($this->input['author'])) {
                    	if ( eregi($this->input['keyword'], $FILES_ARRAY[3])) {
                            $IDX[] = $SEARCH_IDX[$i]."\x0E".$forumid;
                        }
                    }
                	if (empty($this->input['topic']) && empty($this->input['content']) && !empty($this->input['author'])) {
                    	if ($this->authorId == $FILES_ARRAY[5]) {
                            $IDX[] = $SEARCH_IDX[$i]."\x0E".$forumid;
                        }
                    }
                	if (empty($this->input['topic']) && !empty($this->input['content']) && !empty($this->input['author'])) {
                    	if ($this->authorId == $FILES_ARRAY[5] || eregi($this->input['keyword'], $FILES_ARRAY[3])) {
                            $IDX[] = $SEARCH_IDX[$i]."\x0E".$forumid;
                        }
                    }
                	if (!empty($this->input['topic']) && empty($this->input['content']) && !empty($this->input['author'])) {
                    	if (eregi($this->input['keyword'], $FILES_ARRAY[2]) || $this->authorId == $FILES_ARRAY[5]) {
                                $IDX[] = $SEARCH_IDX[$i]."\x0E".$forumid;
                        }
                    }
                	if (!empty($this->input['topic']) && !empty($this->input['content']) && empty($this->input['author'])) {
                    	if (eregi($this->input['keyword'], $FILES_ARRAY[2]) || eregi($this->input['keyword'], $FILES_ARRAY[3])) {
                            $IDX[] = $SEARCH_IDX[$i]."\x0E".$forumid;
                        }
                    }
                	if (!empty($this->input['topic']) && !empty($this->input['content']) && !empty($this->input['author'])) {
                    	if (eregi($this->input['keyword'], $FILES_ARRAY[2]) || eregi($this->input['keyword'], $FILES_ARRAY[3]) || $this->authorId==$FILES_ARRAY[5]) {
                            $IDX[] = $SEARCH_IDX[$i]."\x0E".$forumid;
                        }
                    }
                }
            	if (empty($this->input['title']) && !empty($this->input['reply'])) {
                	for ($j=1;$j<=$searchCount-1;$j++) {
                        $FILES_ARRAY = $this->make_array($FILES_ARRAY_DATA[$j]);
                        $check="0";
                    	if (!empty($this->input['topic']) && empty($this->input['content']) && empty ($this->input['author'])) {
                        	if (eregi($this->input['keyword'], $FILES_ARRAY[2])) {
                                $IDX[] = $SEARCH_IDX[$i]."\x0E".$forumid;
                                $check = "1";
                            }
                        }
                    	if (empty($this->input['topic']) && !empty($this->input['content']) && empty($this->input['author'])) {
                        	if (eregi($this->input['keyword'], $FILES_ARRAY[3])) {
                                $IDX[] = $SEARCH_IDX[$i]."\x0E".$forumid;
                                $check = "1";
                            }
                        }
                    	if (empty($this->input['topic']) && empty($this->input['content']) && !empty($this->input['author'])) {
                        	if ($this->authorId == $FILES_ARRAY[5]) {
                                $IDX[] = $SEARCH_IDX[$i]."\x0E".$forumid;
                                $check = "1";
                            }
                        }
                    	if (empty($this->input['topic']) && !empty($this->input['content']) && !empty($this->input['author'])) {
                        	if ($this->authorId == $FILES_ARRAY[5] || eregi($this->input['keyword'], $FILES_ARRAY[3])) {
                                $IDX[] = $SEARCH_IDX[$i]."\x0E".$forumid;
                                $check = "1";
                            }
                        }
                    	if (!empty($this->input['topic']) && empty($this->input['content']) && !empty($this->input['author'])) {
                        	if (eregi($this->input['keyword'], $FILES_ARRAY[2]) || $this->authorId == $FILES_ARRAY[5]) {
                                $IDX[] = $SEARCH_IDX[$i]."\x0E".$forumid;
                                $check = "1";
                            }
                        }
                    	if (!empty($this->input['topic']) && !empty($this->input['content']) && empty($this->input['author'])) {
                        	if (eregi($this->input['keyword'], $FILES_ARRAY[2]) || eregi($this->input['keyword'], $FILES_ARRAY[3])) {
                                $IDX[] = $SEARCH_IDX[$i]."\x0E".$forumid;
                                $check = "1";
                            }
                        }
                    	if (!empty($this->input['topic']) && !empty($this->input['content']) && !empty($this->input['author'])) {
                        	if (eregi($this->input['keyword'], $FILES_ARRAY[2]) || eregi($this->input['keyword'], $FILES_ARRAY[3]) || $this->authorId == $FILES_ARRAY[5]) {
                                $IDX[] = $SEARCH_IDX[$i]."\x0E".$forumid;
                                $check = "1";
                            }
                        }
                            
                    	if ($check == "1")
                        	break;
                    }
                }
                               
            	if (!empty ($this->input['title']) && !empty($this->input['reply'])) {
                	for ($j=0;$j<=$searchCount-1;$j++) {
                        $FILES_ARRAY = $this->make_array($FILES_ARRAY_DATA[$j]);
                        $check = "0";
                    	if (!empty($this->input['topic']) && empty($this->input['content']) && empty ($this->input['author'])) {
                        	if (eregi($this->input['keyword'], $FILES_ARRAY[2])) {
                                $IDX[] = $SEARCH_IDX[$i]."\x0E".$forumid;
                                $check = "1";
                            }
                        }
                    	if (empty($this->input['topic']) && !empty($this->input['content']) && empty($this->input['author'])) {
                        	if ( eregi($this->input['keyword'], $FILES_ARRAY[3])) {
                                $IDX[] = $SEARCH_IDX[$i]."\x0E".$forumid;
                                $check = "1";
                            }
                        }
                    	if (empty($this->input['topic']) && empty($this->input['content']) && !empty($this->input['author'])) {
                        	if ($this->authorId == $FILES_ARRAY[5]) {
                                $IDX[] = $SEARCH_IDX[$i]."\x0E".$forumid;
                                $check = "1";
                            }
                        }
                    	if (empty($this->input['topic']) && !empty($this->input['content']) && !empty($this->input['author'])) {
                        	if ($this->authorId == $FILES_ARRAY[5] || eregi($this->input['keyword'], $FILES_ARRAY[3])) {
                                $IDX[] = $SEARCH_IDX[$i]."\x0E".$forumid;
                                $check = "1";
                            }
                        }
                    	if (!empty($this->input['topic']) && empty($this->input['content']) && !empty($this->input['author'])) {
                        	if (eregi($this->input['keyword'], $FILES_ARRAY[2]) || $this->authorId == $FILES_ARRAY[5]) {
                                $IDX[] = $SEARCH_IDX[$i]."\x0E".$forumid;
                                $check = "1";
                            }
                        }
                    	if (!empty($this->input['topic']) && !empty($this->input['content']) && empty($this->input['author'])) {
                        	if (eregi($this->input['keyword'], $FILES_ARRAY[2]) || eregi($this->input['keyword'], $FILES_ARRAY[3])) {
                                $IDX[] = $SEARCH_IDX[$i]."\x0E".$forumid;
                                $check = "1";
                            }
                        }
                    	if (!empty($this->input['topic']) && !empty($this->input['content']) && !empty($this->input['author'])) {
                        	if (eregi($this->input['keyword'], $FILES_ARRAY[2]) || eregi($this->input['keyword'], $FILES_ARRAY[3]) || $this->authorId == $FILES_ARRAY[5]) {
                                $IDX[] = $SEARCH_IDX[$i]."\x0E".$forumid;
                                $check = "1";
                            }
                        }
                       
                    	if ($check == "1"){
                        	break;
                        }
                    }
                }
            }
        }
    	return $IDX;

    } // end func
} // end class

?>