<?php
//
// Copyright (C) 2003 Jan de Graaff
// All rights reserved.
//
// This program uses parts of the original Simpleboard Application
// 0.7.0b written by Josh Levine; http://www.joshlevine.net
//
// This source file is part of the SimpleBoard Component, a Mambo 4.5
// custom Component By Jan de Graaff - http://tsmf.jigsnet.com
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License (GPL)
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// Please note that the GPL states that any headers in files and
// Copyright notices as well as credits in headers, source files
// and output (screens, prints, etc.) can not be removed.
// You can extend them with your own credits, though...
//
// 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 "GNU General Public License" (GPL) is available at
// http://www.gnu.org/copyleft/gpl.html.
//
// Dont allow direct linking
defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

//Get right Language file
if (file_exists($map.'/administrator/components/com_simpleboard/language/'.$mosConfig_lang.'.php')){
  include($map.'/administrator/components/com_simpleboard/language/'.$mosConfig_lang.'.php');
} else {
  include($map.'/administrator/components/com_simpleboard/language/english.php');
}
$Itemid = 0;
$Itemid = $_GET['Itemid'];
?>
<link rel="stylesheet" href="components/com_simpleboard/template/<?php echo $sbConfig['template'];?>/forum.css" type="text/css" />
<?php
class forumsearch_html{

   function search($database, $searchword, $show, $forumcount, $usersearch){
      global $database, $my, $mosConfig_live_site;
      require_once( $map."configuration.php" );
      //Start with determining which forums the user can see
      if ($my->id > 0){
         $database->setQuery("SELECT allowed FROM #__sb_sessions WHERE userid='".$my->id."'");
         $ResultSet=$database->loadResult();
         
         if ($ResultSet=="na"){
            $url=$mosConfig_live_site.'/index.php?option=com_simpleboard&Itemid='.$_GET['Itemid'].'&func=listcat';
            echo "<script language=\"javascript\"> alert('"._LATEST_REDIRECT."');</script>";
            echo '<script language="javascript">setTimeout("location=\''.$url.'\'",1);</script>';
            return;
         }

         $allowed_forums = explode (',',$ResultSet);
      } else {
         $database->setQuery("SELECT id FROM #__sb_categories WHERE pub_access='0'");
         $ResultSet=$database->loadObjectList();
         $allowed_forums = array();
         $i=0;
         foreach ($ResultSet as $rs) {
            $allowed_forums[$i] = $rs->id;
            $i++;
         }
      }
 
 if (! $usersearch) {
      //require_once( "classes/database.php" );
      preg_match_all("/[A-Za-z0-9]+/", $searchword, $array);
      $wordcount = count($array[0]);
      $pieces = explode(" ", $searchword);
      $i=0;
      $preword=0;
      while($i < $wordcount){
         if($i==0){
            $namestr = "name like '%".$pieces[0]."%' ";
            $subjectstr = "subject like '%".$pieces[0]."%' ";
            $messagestr = "message like '%".$pieces[0]."%' ";
         } else {
            if($pieces[$i] == "OR"){
               $namestr = $namestr."".$pieces[$i]." ";
               $subjectstr = $subjectstr."".$pieces[$i]." ";
               $messagestr = $messagestr."".$pieces[$i]." ";
               $preword = 1;
            } else {
               if($pieces[$i] == "AND"){
                  $namestr = $namestr."".$pieces[$i]." ";
                  $subjectstr = $subjectstr."".$pieces[$i]." ";
                  $messagestr = $messagestr."".$pieces[$i]." ";
                  $preword = 1;
               } else {
                  if($preword == 1){
                     $namestr = $namestr."name like '%".$pieces[$i]."%' ";
                     $subjectstr = $subjectstr."subject like '%".$pieces[$i]."%' ";
                     $messagestr = $messagestr."message like '%".$pieces[$i]."%' ";
                     $preword = 0;
                  } else {
                     $namestr = $namestr." AND name like '%".$pieces[$i]."%' ";
                     $subjectstr = $subjectstr." AND subject like '%".$pieces[$i]."%' ";
                     $messagestr = $messagestr." AND message like '%".$pieces[$i]."%' ";
                  }
               }
            }
         }
      $i++;
      }

      $tabclass = array("sectiontableentry1", "sectiontableentry2");
      $k = 0;

      $sql = "select id, parent, name from #__sb_categories where ".$namestr." AND published='1'";
      $database->setQuery($sql);
      $res=$database->loadObjectList();

      $count=count($res);

      $i=0;
      $cellspacing=1;
      if($count<>0){
         if($show){
            echo "<table width=\"98%\" cellpadding=\"1\" cellspacing=\"0\" border=\"0\" class=\"searchintro\">";
            echo "<tr><td width=5>&nbsp;</td><td>"._FORUM_SEARCH." <strong>$searchword</strong><br />"._SEARCH_RESULTS." $forumcount "._SEARCH_HITS."</td></tr>";
            echo "</table>";
         }
         echo "<table width=\"98%\" cellpadding=\"1\" cellspacing=\"0\" border=\"0\" class=\"contentpane\">";
         foreach($res as $r){
            if ( in_array($r->id,$allowed_forums)){
               if($r->parent == 0){
                  if($show){
                     $url = sefRelToAbs('index.php?option=com_simpleboard&amp;Itemid='.$Itemid);
                     echo '<tr class="'.$tabclass[$k].'">';
                     $ressubject = $r->name;
                     $ressubject = preg_replace( "/$searchword/i", '<span style="color:red;font-weight:bold;">' . $searchword . '</span>', $ressubject );
                     echo "<td><a href=\"$url\">".$ressubject."</a></td>";
                     echo "<td width=\"25%\"><span class=\"small\">"._RESULTS_CATEGORY."</span></td>";
                     echo "</tr>";
                  }
                  $i++;
               } else {
                  if($show){
                     $url = sefRelToAbs('index.php?option=com_simpleboard&amp;Itemid='.$Itemid.'&amp;func=showcat&amp;catid='.$r->id);
                     echo '<tr class="'.$tabclass[$k].'">';
                     $ressubject = $r->name;
                     $ressubject = preg_replace( "/$searchword/i", '<span style="color:red;font-weight:bold;">' . $searchword . '</span>', $ressubject );
                     echo "<td><a href=\"$url\">".$ressubject."</a></td>";
                     echo "<td width=\"25%\"><span class=\"small\">"._RESULTS_FORUM_NAME."</span></td>";
                     echo "</tr>";
                  }
                  $i++;
               }
            $k=1-$k;
            }
         }
         echo "</table>";
      }

      $sql2 = "select a.id, a.parent, a.thread, a.catid, a.name, a.subject, a.time"
      . "\n from #__sb_messages AS a"
      . "\n LEFT JOIN #__sb_categories AS b"
      . "\n on a.catid=b.id"
      . "\n WHERE ".$subjectstr
      . "\n AND a.hold ='0'"
      . "\n AND b.published='1' AND a.moved!='1'"
      . "\n ORDER BY a.time DESC";
      $database->setQuery($sql2);
      $res2 = $database->loadObjectList();
      $count2 = count($res2);
      if($count2<>0){
         if($show){
            if($count==0){
                  echo "<table width=\"98%\" cellpadding=\"1\" cellspacing=\"0\" border=\"0\" class=\"searchintro\">";
                  echo "<tr><td width=5>&nbsp;</td><td> "._FORUM_SEARCH." <strong>$searchword</strong><br />"._SEARCH_RESULTS." $forumcount "._SEARCH_HITS."</td></tr>";
                  echo "</table>";
            } else {
               echo "<table width=\"98%\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\">";
               echo "<tr><td>&nbsp;</td></tr>";
               echo "</table>";
            }
         }
         echo "<table width=\"98%\" cellpadding=\"1\" cellspacing=\"0\" border=\"0\" class=\"contentpane\">";
         foreach($res2 as $r2){
            if ( in_array($r2->catid,$allowed_forums)){
               if($show){
                  $url = sefRelToAbs('index.php?option=com_simpleboard&amp;Itemid='.$Itemid.'&amp;func=view&amp;catid='.$r2->catid.'&amp;id='.$r2->id).'#'.$r2->id;
                  echo '<tr class="'.$tabclass[$k].'">';               
                $ressubject = $r2->subject;
                $ressubject = preg_replace( "/$searchword/i", '<span style="color:red;font-weight:bold;">' . $searchword . '</span>', $ressubject );
                  echo "<td><a href=\"$url\">".$ressubject.'</a> </td><td width="25%"> '.date(_DATETIME , $r2->time).'</td>';
                  echo "<td width=\"25%\"><span class=\"small\">"._RESULTS_TITLE."</span></td>";
                  echo "</tr>";
               }
            $i++;
            $k=1-$k;
           }
         }
         echo "</table>";
      }
      $sql3 = "select a.mesid, a.message, b.catid from #__sb_messages_text as a left join #__sb_messages as b on a.mesid=b.id where ".$messagestr." order by b.time DESC";
      $database->setQuery($sql3);
      $res3 = $database->loadObjectList();
      $count3 = count($res3);
      if($count3<>0){
         if($show){
            if($count==0){
               if($count2==0){
                  echo "<table width=\"98%\" cellpadding=\"1\" cellspacing=\"0\" border=\"0\" class=\"searchintro\">";
                  echo "<tr><td width=5>&nbsp;</td><td> "._FORUM_SEARCH." <strong>$searchword</strong><br />"._SEARCH_RESULTS." $forumcount "._SEARCH_HITS."</td></tr>";
                  echo "</table>";
               }
            } else {
               echo "<table width=\"98%\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\">";
               echo "<tr><td>&nbsp;</td></tr>";
               echo "</table>";
            }
         }
         foreach($res3 as $r3){
            if ( in_array($r3->catid,$allowed_forums)){
               $sql4 = "select a.id, a.parent, a.thread, a.catid, a.name, a.subject, a.time"
               . "\n from #__sb_messages AS a"
               . "\n LEFT JOIN #__sb_categories AS b"
               . "\n on a.catid=b.id"
               . "\n WHERE a.id='$r3->mesid'"
               . "\n AND a.hold = '0'"
               . "\n AND b.published='1'"
               . "\n ORDER BY a.time DESC";
               $database->setQuery($sql4);
               $res4 = $database->loadObjectList();
               $count4 = count($res4);

               if($count4<>0){
                  //$k=0;
                  echo '<table width="98%" cellpadding="1" cellspacing="0" border="0" class="contentpane">';
                  foreach ($res4 as $r4){
                     if($show){
                        $url = sefRelToAbs('index.php?option=com_simpleboard&amp;Itemid='.$Itemid.'&amp;func=view&amp;catid='.$r4->catid.'&amp;id='.$r4->id).'#'.$r4->id;
                        echo '<tr class="'.$tabclass[$k].'">';
                        $ressubject = $r4->subject;
						//eliminate some possible show stoppers; mf#2733
						$searchword = str_replace ("(","\(",$searchword);
						$searchword = str_replace (")","\)",$searchword);
					
                        $ressubject = preg_replace( "/$searchword/i", '<span style="color:red;font-weight:bold;">' . $searchword . '</span>', $ressubject );
                        echo '<td width="50%"><a href="'.$url.'">'.$ressubject.'</a> </td><td width="25%"> '.date(_DATETIME , $r4->time).'</td>';
                        echo '<td width="25%"><span class="small">'._RESULTS_CONTENT.'</span></td>';
                        echo "</tr>";
                     }
                     $i++;
                     $k=1-$k;
                  }
                  echo "</table>";
               }
            }
         }
      }
   return $i;
   }//if (!$usersearch)
   else
   {
      //search on username...
      
      //get userid from the tables
      $database->setQuery("SELECT id FROM #__users WHERE username='".$searchword."'");
      $searchuid=$database->loadResult();
      
      $database->setQuery("SELECT b.id, b.parent, b.thread, b.catid, b.name,b.subject, b.time FROM #__sb_messages AS b LEFT JOIN #__sb_users AS u ON b.userid = u.userid WHERE u.userid=$searchuid ORDER by b.time DESC");
      //$database->setQuery("SELECT select a.id, a.parent, a.thread, a.catid, a.name, a.subject FROM #__sb_messages AS a JOIN #__sb_users AS u ON a.userid = u.userid WHERE u.userid=$searchuid");
      $res=$database->loadObjectList();
      
      $count=count($res);
      $tabclass = array("sectiontableentry1", "sectiontableentry2");
      $k = 0;
      
      $i=0;
      $cellspacing=1;
      if($count<>0){
         if($show){
            echo "<table width=\"98%\" cellpadding=\"1\" cellspacing=\"0\" border=\"0\" class=\"searchintro\">";
            echo "<tr><td width=5>&nbsp;</td><td>"._FORUM_USERSEARCH." <strong>$searchword</strong><br />"._SEARCH_RESULTS." $forumcount "._SEARCH_HITS."</td></tr>";
            echo "</table>";
         }
         echo "<table width=\"98%\" cellpadding=\"1\" cellspacing=\"0\" border=\"0\" class=\"contentpane\">";
         foreach($res as $r){
            if ( in_array($r2->catid,$allowed_forums)){
               if($show){
                  $url = sefRelToAbs('index.php?option=com_simpleboard&amp;Itemid='.$Itemid.'&amp;func=view&amp;catid='.$r->catid.'&amp;id='.$r->id).'#'.$r->id;
                  echo '<tr class="'.$tabclass[$k].'">';
                  echo '<td width="50%"><a href="'.$url.'">'.$r->subject.'</a> </td><td width="25%"> '.date(_DATETIME , $r->time).'</td>';
                  echo '<td width="25%"><span class="small">'._RESULTS_USERNAME.'</span></td>';
                  echo "</tr>";
               }
               $i++;
               $k=1-$k;
            }
         }
         echo "</table>";
      }
   return $i;   
   }//end else
  }//end function search
}
?>
