<?php

// $Id: bbsearch.php,v 1.10 2001/04/06 06:26:15 nick Exp nick $
// $Log: bbsearch.php,v $
// Revision 1.10  2001/04/06 06:26:15  nick
// Added user pagination, fixed icon for next and previous.
//
// Revision 1.9  2001/04/05 00:12:48  nick
// Fixed problems with UTC code.
//
// Revision 1.8  2001/04/04 00:54:59  nick
// Added UTC time handling.
//
// Revision 1.7  2001/03/16 04:31:03  nick
// Changed file suffices to .php rather than .php4
//
// Revision 1.6  2001/03/08 22:56:46  nick
// More enhancements
//
// Revision 1.5  2001/03/07 03:21:34  nick
// Changes for section moderators, adding topics, etc.
//
// Revision 1.4  2001/03/06 20:59:28  nick
// Further enhancements
//
// Revision 1.3  2001/03/04 22:49:12  nick
// Further bug fixes following production testing
//
// Revision 1.2  2001/02/27 21:45:26  nick
// Improvements made during testing
//
// Revision 1.1  2001/02/23 00:11:07  nick
// Initial revision
//

/*

Copyright © 2001 Nick Gammon.

  Author: Nick Gammon <nick@gammon.com.au>
  Web:    http://www.gammon.com.au/
  Date:   February 2001

  This program is free software; you can redistribute it and/or modify 
  it under the terms of the GNU General Public License as published by 
  the Free Software Foundation; either version 2 of the License, 
  or (at your option) any later version. 

  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. 
  See the GNU General Public License for more details. 

  You should have received a copy of the GNU General Public License 
  along with this program; if not, write to 

  The Free Software Foundation, Inc., 
  59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.


  The Free Software Foundation maintains a web page at: http://www.fsf.org

  See the file gpl.txt for the full GNU General Public License.

*/

require ("config.php");
require ($CONFIG_PATH . "general_config.php");
require ($INCLUDE_DIRECTORY . "common.php");

Init ("Forum search", "forum, search"); 

$forum_name = $control ['forum_name'];

function ShowForm ()
  {
  global $PHP_SELF, $searchfor, $bbtopic_id;
  
 ?>
<hr>
<h3>Search for word or phrase</h3>

<p>Enter a word or phrase in the box below to find messages that match.</p>

<form METHOD="get" ACTION="<?php echo $PHP_SELF;?>"> 
<p>Search for: &nbsp;&nbsp; <input type=text name=searchfor 
   value="<?php echo stripslashes ($searchfor); ?>" size=50 maxlength=55>
</p>
<p>Look in  topic: &nbsp; <select name=bbtopic_id size=1>

<?php

  $result = mysql_query ("SELECT bbtopic.bbtopic_id, "
                       . "  concat(section_name, ': ', topic_name) "
                       . "AS description "
                       . "FROM bbtopic, bbsection "
                       . "WHERE bbsection.bbsection_id = bbtopic.bbsection_id  "
                       . "  AND bbtopic.bbtopic_id > 0  "
                       . "ORDER BY section_name, topic_name") 
    or Problem ("Select of topics failed: " . mysql_error ());
    
  echo "<option value=0 selected>(All topics)\n";
    
  while ($row = mysql_fetch_array ($result))
    {
    $id = $row ['bbtopic_id'];
    $desc = $row ['description'];
    if ($id == $bbtopic_id)
      $selected = 'selected';
    else
      $selected = '';
      
    echo "<option value=\"$id\" $selected>$desc\n";
    } // end of doing each row
  
  mysql_free_result ($result);
?>
</select>
</p>
<p><input Type=submit Value="Search"> </p>
<input type=hidden name=action value=search>
<?php  FormSession (); ?>

</form>

<?php
  }   // end of ShowForm


// ---------------- start of main section -------------------

// DEBUG

// DebugVars ();

// validate input so we don't get sneaky SQL problems

CheckField ("topic", $bbtopic_id);
   
echo "<h1>Search the forum</h1><h2>" . htmlentities ($forum_name) . "</h2>\n";
   
ShowForm ();    
   
if ($action == 'search')
  {   
    
  // don't let them slip in semicolons, like ; drop database xxx; 
  $searchfor = str_replace (";", " ", $searchfor);
  
  $searchfor = trim ($searchfor); // get rid of spaces
  
  // don't let them use % or _ in the string
  $searchforfixed = addcslashes ($searchfor, "%_");            
  
  if (strlen ($searchfor) == 0)
    ShowError ("You must specify something to search for");
  else
    {
    // show search results
    
    $datetimeformat = $control ['datetimeformat'];
    $tz = $foruminfo ['time_zone'];
    if (!$tz)
      $tz = 0;  // not logged in? Assume UTC
    
      
    $LIMIT = 50;
    
    if ($bbtopic_id > 0)
      $topic_where = "AND bbtopic.bbtopic_id = $bbtopic_id ";
    
    $query = "SELECT bbsubject.bbsubject_id, "
           . "  CONCAT(section_name, ': ', topic_name, ': ', subject_name) AS fullname, "
           . "DATE_FORMAT(DATE_ADD(post_date, INTERVAL $tz HOUR), '$datetimeformat') AS excerpt "
           . "FROM bbpost, bbsubject, bbtopic, bbsection "
           . "WHERE (post_text LIKE '%$searchfor%' "
           . "  OR   subject_name LIKE '%$searchfor%')  "
           . $topic_where
           . "      AND bbsection.bbsection_id = bbtopic.bbsection_id  "
           . "      AND bbtopic.bbtopic_id = bbsubject.bbtopic_id "
           . "      AND bbsubject.bbsubject_id = bbpost.bbsubject_id "
           . "GROUP BY bbsubject_id "
           . "ORDER BY section_name, topic_name, subject_name "
           . "LIMIT $LIMIT";
    
    
    echo "<hr><h4>Search results</h4>\n";
    
    $count = ShowList ($query, "bbsubject_id", "", "fullname", true, 
                       "<UL>", "</UL>", "<LI>", "</LI>", "bbshowpost.php");
    
    if ($count >= $LIMIT)
      echo "<p>Search limited to $count items.</p>";
        
    }   // end of something to search for
  } // end of search button pressed
  
echo "<hr>\n";
  
hLink ("Back to " . htmlentities ($forum_name), "bbshowpost.php");
        
?>

<hr>

<?php MessageTail (); ?>