<?PHP
////////////////////////////////////////////////////////////////////////////////
//                                                                            //
//   Copyright (C) 2000  Phorum Development Team                              //
//   http://www.phorum.org                                                    //
//                                                                            //
//   This program is free software. You can redistribute it and/or modify     //
//   it under the terms of the Phorum License Version 1.0.                    //
//                                                                            //
//   This program is distributed in the hope that it will be useful,          //
//   but WITHOUT ANY WARRANTY, without even the implied warranty of           //
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                     //
//                                                                            //
//   You should have received a copy of the Phorum License                    //
//   along with this program.                                                 //
////////////////////////////////////////////////////////////////////////////////

  require "./common.php";

  if($num==0 || $ForumName==''){
    Header("Location: $forum_url/$forum_page.$ext?$GetVars");
    exit;
  }

  /////////////////////////////////////////////////////////////////
  // build the search terms array
  // this will build the array to pass to build_sql()
  
  function build_search_terms($search, $match) {
    $terms=array();
    
    // if this is an exact phrase match
    if($match==3){
      $terms[]=$search;
    }
    // not exact phrase, break up the terms
    else{
      if ( strstr( $search, '"' ) ){
        //first pull out all the double quoted strings
        if(strstr($search, "\"")){
          $search_string=$search;
          while(ereg('-*"[^"]*"', $search_string, $match)){
            $terms[]=trim(str_replace("\"", "", $match[0]));
            $search_string=substr(strstr($search_string, $match[0]), strlen($match[0]));
          }
        }
        $search = ereg_replace('-*"[^"]*"', '', $search );
      }
  
      //pull out the rest words in the string 
      $regular_terms = explode( " ", $search);
  
      //merge them all together and return
      while (list ($key, $val) = each ($regular_terms)) {
        if($val!="")
          $terms[]=trim($val);
      }  
    }
    return $terms;
  }
      
  /////////////////////////////////////////////////////////////////
  // build the sql statement's where clause
  // this will build the sql based on the given information

  function  build_terms_clause($terms, $date, $fields, $match){

    static $where_clause;
    
    if(empty($where_clause)){
      if($date!=0){
        $cutoff=date("Y-m-d", mktime(0,0,0,date("m"),date("d")-$date));
        $where_clause .= " datestamp >= '$cutoff' AND ";
      }
    
      while (list ($junk, $term) = each ($terms)) {
        $cmpfunc="LIKE";                
        if(substr($term, 0, 1)=="-"){
          $term=substr($term, 1);
          $cmpfunc="NOT LIKE";
        }
        reset($fields);
        unset($likeArray);
        while (list ($key, $val) = each ($fields)) {
          $likeArray[]=" $val $cmpfunc '%$term%' ";
        }
        $termArray[] = " (".implode( $likeArray, " OR " ).") ";
      }

      $cmptype="AND";
      if($match!=1) $cmptype="OR";
      $where_clause.= " (".implode( $termArray, " $cmptype " ).") ";

      $where_clause.="order by datestamp desc";
    }
    
    return $where_clause;

  }

  /////////////////////////////////////////////////////////////////
  // build the sql statement
  // this will build the sql based on the given information

  function  build_sql($table_name, $terms, $date, $fields, $match){

    GLOBAL $ForumTableName;


    $SQL = "select $table_name.id, $table_name.thread, author, subject, datestamp, body from $table_name, $table_name"."_bodies where $table_name.id = $table_name"."_bodies.id and $table_name.approved='Y' AND";

    $SQL.=build_terms_clause($terms, $date, $fields, $match);
    
    return $SQL;
  }

  if (!isset($search)){
    $search="";
  }

  $search=trim(stripslashes($search));
  $searchtext = $search;

  $searchtext = htmlentities($searchtext);

  if(!isset($fldauthor) && !isset($fldsubject) && !isset($fldbody)){
    $fields[] = "subject";
    $fields[] = "body";
    $fldauthor=0;
    $fldsubject=1;
    $fldbody=1;
  }
  else{
    isset($fldauthor) ? $fields[] = "author" : $fldauthor=0;
    isset($fldsubject) ? $fields[] = "subject" : $fldsubject=0;
    isset($fldbody) ? $fields[] = "body" : $fldbody=0;
  }

  initvar("date", 30);
  initvar("match", 1);
  initvar("start_num", 0);
  
  if($search!=""){
    $terms = build_search_terms($search, $match);
    if(count($terms)>0){
      if(isset($x)){
        list($action,$start_num)=explode(",", $x);
      }
    
      $SQL=build_sql($ForumTableName, $terms, $date, $fields, $match);

//      echo "\n<!-- $SQL -->\n";

      $q->query($DB, $SQL);

      if($err=$q->error()){
        echo $err;
      }
      else{
        $totalFound=$q->numrows();
        $q->seek($start_num);
        $message = $q->getrow();
        $rowcount=0;
        while(is_array($message) && $rowcount<$ForumDisplay){
          $rowcount++;
          $messages[]=$message;
          $message = $q->getrow();
        }
        $q->free();
      }
      $rows = @count($messages);
    }

  }

  $sTitle=" ".strtolower($lSearch);

  if(file_exists("$include_path/header_$ForumConfigSuffix.php")){
    include "$include_path/header_$ForumConfigSuffix.php";
  }
  else{
    include "$include_path/header.php";
  }    

  if($ActiveForums>1){
    $nav = "<a  href=\"".$sess->link3($FORUM_URL.$forum_page.".".$ext,"f=$ForumParent$GetVars")."\">".$lForumList."</a>&nbsp;&nbsp;|&nbsp;&nbsp;<a  href=\"".$sess->link3($FORUM_URL.$post_page.".".$ext,"f=$num$GetVars")."\">".$lStartTopic."</a>&nbsp;&nbsp;|&nbsp;&nbsp;<a  href=\"".$sess->link3($FORUM_URL.$list_page.".".$ext,"f=$num$GetVars")."\">".$lGoToTop."</a>&nbsp;";
  }
  else{
    $nav = "<a  href=\"".$sess->link3($FORUM_URL.$post_page.".".$ext,"f=$num$GetVars")."\">".$lStartTopic."</a>&nbsp;&nbsp;|&nbsp;&nbsp;<a  href=\"".$sess->link3($FORUM_URL.$list_page.".".$ext,"f=$num$GetVars")."\">".$lGoToTop."</a>&nbsp;";
  }


?>
<?PHP
  if(@is_array($terms))
 {
 table_ktf_up( $lSearchResults." : ".$totalFound, "100%", 'green',1,'','','#FFFFFF' ); 

    if($rows>0){
      $message=current($messages);
      $count=$start_num;
      While(is_array($message)){
        $count=$count+1;
        if(!isset($top_id)){
          $top_id=$message["id"];
        }
        echo "<dl><dt><b>$count. </b><a href=\"$read_page.$ext?f=$num&i=".$message["id"]."&t=".$message["thread"]."$GetVars\"><b>".chop($message["subject"])."</b></a> - ".chop($message["author"])."<br>\n<dd>";
        $text=chop(substr($message["body"], 0, 200));
        if(function_exists("strip_tags")){
          $text=strip_tags($text);
        }
        else{
          $text=ereg_replace("<[^>]*>", "", $text);
          $text=ereg_replace("^<[^>]*>", "", $text);
          $text=ereg_replace("<[^>]*>$", "", $text);
        }        
        echo $text."<br>";
        echo "<font size=-2>$lDate: ".old_date_format($message["datestamp"])."</font><br>\n";
        echo "</dl><p>\n";
        $last_id=$message["id"];
        $message=next($messages);
      }
    }
    else{
      echo $lNoMatches;
    }
	
	
	
    $prevmatch='';
    $morematch='';
    if($start_num >= $ForumDisplay){
      $start_num=$start_num-$ForumDisplay;
      $prevmatch="<a href=\"$search_page.$ext?f=$num&search=".urlencode($search)."&match=$match&date=$date&fldauthor=$fldauthor&fldsubject=$fldsubject&fldbody=$fldbody&x=2,$start_num$GetVars\"><FONT color=\"$ForumNavFontColor\">$lPrevMatches</font></a>";
    }
    if($rows>=$ForumDisplay){
      $morematch="<a href=\"$search_page.$ext?f=$num&search=".urlencode($search)."&match=$match&date=$date&fldauthor=$fldauthor&fldsubject=$fldsubject&fldbody=$fldbody&x=1,$count$GetVars\"><FONT color=\"$ForumNavFontColor\">$lMoreMatches</font></a>";
    }
    if($prevmatch || $morematch){
      echo "<center><br><br><div class=smallDark><FONT color=\"$ForumNavFontColor\"><b>";
      if($prevmatch) echo $prevmatch;
      if($prevmatch && $morematch) echo '&nbsp;&nbsp;|&nbsp;&nbsp;';
      if($morematch) echo $morematch;
      echo "</font></div><br><br></center>";
    }
	table_ktf_down('',1);


  }



?>

<form action="<?PHP echo $sess->link3($FORUM_URL."$search_page.$ext","f=$num$GetVars"); ?>" method="GET">
<?PHP echo $PostVars; ?>
<input type="Hidden" name="f" value="<?PHP echo $num; ?>">


<?php
table_ktf_up( $lSearch, "100%", 'green',1,'','','#FFFFFF' ); 

?>

<table width="<?PHP echo $ForumTableWidth; ?>" border="0" cellspacing="0" cellpadding="3">
  <tr>
    <td class=forumnav align=center valign="TOP" nowrap><?PHP echo $nav; ?></td>
  </tr>
</table>

<table  width="<?PHP echo $ForumTableWidth; ?>" border="0" cellspacing="0" cellpadding="2">
<!--  <tr>
    <td <?PHP echo bgcolor($ForumTableHeaderColor); ?> valign="TOP" nowrap><font color="<?PHP echo $ForumTableHeaderFontColor; ?>">&nbsp;<?PHP echo $lSearch;?></font></td>
  </tr>
 -->
  <tr>
    <td align="CENTER" valign="MIDDLE" >
	    <br>
		<table cellspacing="0" cellpadding="2" border="0">
			<tr>
			    <td class=smallDark align="right">&nbsp;&nbsp;<?PHP echo $lSearch;?>:&nbsp;&nbsp;</td>
			    <td valign=absmiddle> <input class=inputBg type="Text" name="search" size="30" value="<?PHP echo $searchtext; ?>">&nbsp;
				<!-- <input type="Submit" value="<?PHP echo $lSearch;?>"> -->
				<input align=absmiddle type="image" name="<?PHP echo $lSearch;?>" src="<?php echo $IMAGES_DIR . "boutons/btn_forum_ok.gif"?>"  border="0" alt="<?PHP echo $lSearch;?>">&nbsp;&nbsp;</td>
			</tr>
			<tr>
			    <td align="right">&nbsp;</td>
			    <td class=smallDark><select class=inputBg name="match"><option value="1" <?PHP if($match==1) echo "selected"; ?>><?PHP echo $lSearchAllWords ?></option><option value="2" <?PHP if($match==2) echo "selected"; ?>><?PHP echo $lSearchAnyWords ?></option><option value="3" <?PHP if($match==3) echo "selected"; ?>><?PHP echo $lSearchPhrase ?></option></select>&nbsp;&nbsp;&nbsp;&nbsp;<select class=inputBg name="date"><option value="30" <?PHP if($date==30) echo "selected"; ?>><?PHP echo $lSearchLast30; ?></option><option value="60" <?PHP if($date==60) echo "selected"; ?>><?PHP echo $lSearchLast60; ?></option><option value="90" <?PHP if($date==90) echo "selected"; ?>><?PHP echo $lSearchLast90; ?></option><option value="180" <?PHP if($date==180) echo "selected"; ?>><?PHP echo $lSearchLast180; ?></option><option value="0" <?PHP if($date==0) echo "selected"; ?>><?PHP echo $lSearchAllDates; ?></option></select></td>
			</tr>
			<tr>
			    <td align="right">&nbsp;</td>
			    <td class=smallDark><input type="checkbox" name="fldauthor" value="1" <?PHP if($fldauthor==1)  echo "checked"; ?>> <?PHP echo $lAuthor; ?>&nbsp;&nbsp;&nbsp;<input type="checkbox" name="fldsubject" value="1" <?PHP if($fldsubject==1)  echo "checked"; ?>> <?PHP echo $lFormSubject; ?>&nbsp;&nbsp;&nbsp;<input type="checkbox" name="fldbody" value="1" <?PHP if($fldbody==1)  echo "checked"; ?>> <?PHP echo $lMessageBodies; ?>&nbsp;&nbsp;&nbsp;</td>
</form>
			</tr>
		</table>
	</td>
  </tr>
</table>
<?php 
	table_ktf_down('',1);
	echo "<br>";
	table_ktf_up( $lSearchTips, "100%", 'green',1,'','','#FFFFFF' );
	
?>

<table width="<?PHP echo $ForumTableWidth; ?>" border="0" cellspacing="0" cellpadding="2">
<!--  <tr>
    <td <?PHP echo bgcolor($ForumTableHeaderColor); ?> valign="TOP" nowrap><font color="<?PHP echo $ForumTableHeaderFontColor; ?>">&nbsp;<?PHP echo $lSearchTips;?></font></td>
  </tr>
  <tr>
-->
    <td class=smallDark width="<?PHP echo $ForumTableWidth; ?>" align="LEFT" valign="MIDDLE" ><?PHP echo $lTheSearchTips;?></td>
  </tr>
</table>

<?PHP
table_ktf_down('',1);

  if(file_exists("$include_path/footer_$ForumConfigSuffix.php")){
    include "$include_path/footer_$ForumConfigSuffix.php";
  }
  else{
    include "$include_path/footer.php";
  }

?>