<?php
  $w = $_REQUEST["w"];
  $p = intval($_REQUEST["p"]);
  if (!$p) $p = 1;

  include ("$www_dir/classes/search.class.php");
  $SBBSearch = new SBBSearch;
  
  $NAV_PAGES = array();
/***************
 *  Search for Userposts [+]
 ***************/
  if ($w == "userposts") {
    $uid = intval($_REQUEST["uid"]);
    
    $this_user = forum_user($uid);

    if (!$this_user) {
    
    /************
     * initiate template and assign variables
     ************/         
    $QuickSkin = new QuickSkin("status_messages.html");
    $QuickSkin->assign("INFO",lang("usernotfound"));
    $content .= $QuickSkin->result();
    unset($QuickSkin);
    
    } else {

      $searched = true;

      $this_user_posts = forum_user_posts($this_user->id);
      
      $sbb_add2title .= " - " . sprintf(lang("add2usearchtitle"), he($this_user->user));
      
      
      //Template Variables
      $HEADLINE = sprintf(lang("hl_usearch"), $this_user->user);

      if ($this_user_posts) {
      
        //Template Variables
        $DESCR_AUTHOR = lang("author");
        $DESCR_MESSAGE = lang("message");
        $DESCR_TITLE = lang("title");
        $DESCR_FORUM = lang("forum") . ":";
        $DESCR_MAINTITLE = lang("topic") . ":";
        $DESCR_COMPOSED = lang("composedon");
        
        $posts = $this_user_posts["content"];
        $keys = $this_user_posts["keys"];
        rsort($keys);
        
        $total = forum_user_post_count($this_user->id);
        
        $starti = 0; $endi = $total;
        //some pagecalculations
        if ($board_config->results_per_page) {
          if (($p * $board_config->results_per_page) >= ($total+$board_config->results_per_page)) $p = 1;       //if you are redirecting to page without ..., the page will be set to 1
          $starti = ($p-1)*$board_config->results_per_page;
          $endi = $p*$board_config->results_per_page;
          if ($endi > $total) $endi = $total;
          $pages = ceil($total/$board_config->results_per_page);
        }
        
        //pagination
        $LINK_PAGE_PREFIX = ($board_config->seo ? "$main_path/sbb-userposts" . $this_user->id . ".html?p=" : "$main_script?todo=search&amp;w=userposts&amp;uid=$uid&amp;p=");
        $NAV_PAGES = pagination2ndGen($pages,$p);

        
        for ($i=$starti;$i<$endi;$i++) {
          
          $post = $posts[$keys[$i]]["content"];
          //reply???
          if ($posts[$keys[$i]]["a_id"]) $post = getUreply($posts[$keys[$i]]["a_id"],$posts[$keys[$i]]["tid"]);
          //topic???
          else $post = getUtopic($posts[$keys[$i]]["tid"]);

          $post_user = forum_user($post->uid);
          list($ut,$cnt) = explode("_",$post->ut);
          
          //Template Variables
          $USERPOSTS[] = array("MAINTOPIC_TITLE" => $post->topic,
                               "FORUM_TITLE" => $forum[$post->fid]->title,
                               "LINK_FORUM" => ($board_config->seo ? "$main_path/sbbf" . $post->fid . adddescr2link($forum[$post->fid]->title) . ".html" : "$main_script?todo=show_topics&amp;fid=" . $post->fid),
                               "MAINTOPIC_ID" => $post->tid,
                               "POSTER_CLASS" => nick_class($post_user->is_admin, $post_user->is_mod),
                               "POSTER_NAME" => ($post_user ? $post_user->user : $post->name),
                               "POST_COMPOSED" => date($date_time_format,$ut),
                               "POST_ID" => $post->a_id,
                               "POST_TITLE" => ($post->type == "q" ?  $post->topic : ($post->a_topic ? $post->a_topic : "Re: " . $post->topic)),
                               "POST_MESSAGE" => nl2br(substr(strip_tags(strip_bbcodes($post->message)),0,255)) . "...",
                               "LINK_POST" => ($board_config->seo ? "$main_path/sbbt" . $post->tid . adddescr2link($post->topic) . ".html" : "$main_script?todo=viewtopic&amp;tid=" . $post->tid),
                               
                              );
        } //the posts loop
      } //posts found
    } //user found
  } //userposts

/***************
 *  Search for Userposts [-]
 ***************/

/***************
 *  Search for Strings [+]
 ***************/
  if ($w == "str") {
    $q = trim($_REQUEST["q"]);
    
    if (!$q) {
    /************
     * initiate template and assign variables
     ************/         
    $QuickSkin = new QuickSkin("status_messages.html");
    $QuickSkin->assign("INFO",lang("noquery"));
    $content .= $QuickSkin->result();
    unset($QuickSkin);
    
    } else {

      $searched = true;

      
      $sbb_add2title .= " - " . sprintf(lang("add2qsearchtitle"), he($q));
      
      
      //Template Variables
      $HEADLINE = sprintf(lang("hl_qsearch"), "'$q'");
            
      $qresults = $SBBSearch->GetStringResults($q);
      $posts = $qresults["posts"];
      $keys = $qresults["keys"];
      $total = count($keys);
        
      $starti = 0; $endi = $total;
      //some pagecalculations
      if ($board_config->results_per_page) {
        if (($p * $board_config->results_per_page) >= ($total+$board_config->results_per_page)) $p = 1;       //if you are redirecting to page without ..., the page will be set to 1
        $starti = ($p-1)*$board_config->results_per_page;
        $endi = $p*$board_config->results_per_page;
        if ($endi > $total) $endi = $total;
        $pages = ceil($total/$board_config->results_per_page);
      }
      
      //pagination
      $LINK_PAGE_PREFIX = "$main_script?todo=search&amp;w=str&amp;q=" . he($q) . "&amp;p=";
      $NAV_PAGES = pagination2ndGen($pages,$p);

      if ($posts) {
      
        //Template Variables
        $DESCR_AUTHOR = lang("author");
        $DESCR_MESSAGE = lang("message");
        $DESCR_TITLE = lang("title");
        $DESCR_FORUM = lang("forum") . ":";
        $DESCR_MAINTITLE = lang("topic") . ":";
        $DESCR_COMPOSED = lang("composedon");
        
        rsort($keys);
        for ($i=$starti;$i<$endi;$i++) {
          
          $post = $posts[$keys[$i]]["content"];
          $post_user = forum_user($post->uid);
          list($ut,$cnt) = explode("_",$post->ut);
          
          //Template Variables
          $USERPOSTS[] = array("MAINTOPIC_TITLE" => $post->topic,
                               "FORUM_TITLE" => $forum[$post->fid]->title,
                               "LINK_FORUM" => ($board_config->seo ? "$main_path/sbbf" . $post->fid . adddescr2link($forum[$post->fid]->title) . ".html" : "$main_script?todo=show_topics&amp;fid=" . $post->fid),
                               "MAINTOPIC_ID" => $post->tid,
                               "POSTER_CLASS" => nick_class($post_user->is_admin, $post_user->is_mod),
                               "POSTER_NAME" => ($post_user ? $post_user->user : $post->name),
                               "POST_COMPOSED" => date($date_time_format,$ut),
                               "POST_ID" => $post->a_id,
                               "POST_TITLE" => ($post->type == "q" ?  $post->topic : ($post->a_topic ? $post->a_topic : "Re: " . $post->topic)),
                               "POST_MESSAGE" => nl2br(substr(strip_tags(strip_bbcodes($post->message)),0,255)) . "...",
                               "LINK_POST" => ($board_config->seo ? "$main_path/sbbt" . $post->tid . adddescr2link($post->topic) . ".html?hl=$q" : "$main_script?todo=viewtopic&amp;tid=" . $post->tid . "&amp;hl=$q"),
                               
                              );
        } //the posts loop
      } //posts found
    } //user found
  } //str

/***************
 *  Search for Strings [-]
 ***************/

  if ($searched) {

    /************
     * initiate template and assign variables
     ************/ 
      $searchVARS = array('USER_NOT_FOUND' => $USER_NOT_FOUND,
                          'HEADLINE' => $HEADLINE,
                          'USERPOSTS_NOT_FOUND' => lang("nopostsfound"),
                          'DESCR_AUTHOR' => $DESCR_AUTHOR,
                          'DESCR_MESSAGE' => $DESCR_MESSAGE,
                          'DESCR_TITLE' => $DESCR_TITLE,
                          'DESCR_MAINTITLE' => $DESCR_MAINTITLE,
                          'DESCR_COMPOSED' => $DESCR_COMPOSED,
                          'DESCR_FORUM' => $DESCR_FORUM,
                          'LINK_PAGE_PREFIX' => $LINK_PAGE_PREFIX,

                          'CONF' => $GLOBAL_TEMPLATE_VARS,
                          'FLAGS' => $TEMPLATE_FLAGS,
                          'USERPOSTS' => $USERPOSTS,
                          'NAV_PAGES' => $NAV_PAGES
                          );
      $QuickSkin = new QuickSkin("search.html");
      $QuickSkin->assign($searchVARS);
      $content .= $QuickSkin->result();
      unset($QuickSkin);

  }
?>