<?php

$tid = intval($_REQUEST["tid"]);
$hl = trim($_REQUEST["hl"]);

include ("$www_dir/classes/topics.class.php");
$SBBTopics = new SBBTopics();
$SBBTopics->tid = $tid;

$SBBTopics->mark_topic_read();


//Template Variables
$IMG_NEWTOPIC_TITLE = lang("createtopic");
$IMG_REPLY_TITLE = lang("reply_to_topic");
$IMG_QUOTE = lang("cite_post");
$DESCR_AUTHOR = lang("author");
$DESCR_MESSAGE = lang("message");
$DESCR_COMPOSED = lang("composedon");
$DESCR_TITLE = lang("title");
$DESCR_DELTOPIC = lang("deltopic");
$IMG_PROFILE_TITLE = lang("viewprofile");
$IMG_SENDMAIL_TITLE = lang("send_message");
$IMG_PM_TITLE = lang("send_pm");
$IMG_DELETE_SINGLE_TITLE = lang("delpost");
$IMG_EDIT_SINGLE_POST = lang("editpost");
$CONFIRM_DELPOST = lang("confirm_delpost");
$CONFIRM_DELTOPIC = lang("confirm_deltopic");
$DESCR_JOINED = lang("registered");
$DESCR_POSTS = lang("posts");
$DESCR_AGE = lang("age");
$DESCR_BDAY = lang("birthday");
$DESCR_LOCATION = lang("location");
$DESCR_SUBMIT_VOTE = lang("submit_vote");
$DESCR_VIEW_RESULTS = lang("view_results");
$DESCR_TOTAL_VOTES = lang("total_votes");
$DESCR_VOTED_FOR = lang("voted_for");
$DESCR_ATTACHMENTS = lang("attachments");
$IMG_ILIKETHISPOST = lang("i_like_this_post");
$IMG_IDONTLIKETHISPOST = lang("i_dont_like_this_post");

$LINK_REPLY = ($board_config->seo ? "$main_path/sbb-reply$tid.html" : "$main_script?todo=add_topic&amp;tid=$tid");
$LINK_REPLY_QUOTE_R = ($board_config->seo ? "$main_path/sbb-reply$tid.html?quote=true&amp;pid=" : "$main_script?todo=add_topic&amp;tid=$tid&amp;quote=true&amp;pid=");
$LINK_REPLY_QUOTE_T = ($board_config->seo ? "$main_path/sbb-reply$tid.html?quote=true" : "$main_script?todo=add_topic&amp;tid=$tid&amp;quote=true");

$DESCR_PRINT = lang("print_txt");

//take the first forum - link will be overruled if topic is found
$LINK_ADD_TOPIC = ($board_config->seo ? "$main_path/sbb-add-topic.html?fid=" . $forums[0]->id : "$main_script?todo=add_topic&amp;fid=" . $forums[0]->id);

if (!$is_mod) $hide_blocks[] = "ISMOD";
if (!$f_auth_uid) $hide_blocks[] = "LOGGEDIN";

//in watchlist?
$watched = $SBBTopics->topic_watched();

$SBBTopics->GetPosts(); 

if (!$board_config->en_likes && !$board_config->en_dislikes) $hide_blocks[] = "SHOWLIKES";
else $SHOWLIKES = true;

if ($SBBTopics->posts["main"] && array_key_exists($SBBTopics->posts["main"]->fid,$forum)) {
  
  $row = $SBBTopics->posts["main"];
  $this_post_title = $row->topic;
  $this_user = forum_user($row->uid);
    
  $LINK_ADD_TOPIC = ($board_config->seo ? "$main_path/sbb-add-topic.html?fid=" . $row->fid : "$main_script?todo=add_topic&amp;fid=" . $row->fid);

  $LINK_RESULTS = ($board_config->seo ? "$main_path/sbbt" . $row->id . adddescr2link($row->topic) . ".html?section=viewresults" : "$main_script?todo=viewtopic&amp;tid=" . $row->id . "&amp;section=viewresults");

  
  if (!has_edit_permission($row->id)) $hide_blocks[] = "ENEDIT";

  //RATINGS
  if ($SHOWLIKES) {
    $ratingInfo = rating_permission($row->uid,$row->id,0);
    if ($ratingInfo === false) {
      $hide_blocks[] = "ENLIKE";
      $hide_blocks[] = "ENDISLIKE";
    } elseif (in_array($ratingInfo,array(1,-1))) {
      switch($ratingInfo) {
        case 1: $RATEDFOR = lang("i_liked_this_post"); break;
        case -1: $RATEDFOR = lang("i_didnt_like_this_post"); break;
      }
      $hide_blocks[] = "ENLIKE";
      $hide_blocks[] = "ENDISLIKE";
    }
    if (!$board_config->en_likes) $hide_blocks[] = "ENLIKE";
    if (!$board_config->en_dislikes) $hide_blocks[] = "ENDISLIKE";
  }
  if (!$RATEDFOR) $hide_blocks[] = "RATEDFOR";

  //socialrow
  if ($board_config->socialrow) {
    if (preg_match("/msie/i",$_SERVER["HTTP_USER_AGENT"])) {
      $SOCIAL_FACEBOOK = "<iframe class='facebook' src='http://www.facebook.com/plugins/like.php?href=" . urlencode("http://" . $_SERVER["HTTP_HOST"] . ($_SERVER["REDIRECT_URL"] ? $_SERVER["REDIRECT_URL"] : $_SERVER["REQUEST_URI"])) . "&amp;layout=button_count&amp;show_faces=false&amp;width145&amp;action=like&amp;font=verdana&amp;colorscheme=light&amp;height=35' scrolling='no' frameborder='0' allowTransparency='true'></iframe>";
    } else {     
      $SOCIAL_FACEBOOK = "<object class='facebook' data='http://www.facebook.com/plugins/like.php?href=" . urlencode("http://" . $_SERVER["HTTP_HOST"] . ($_SERVER["REDIRECT_URL"] ? $_SERVER["REDIRECT_URL"] : $_SERVER["REQUEST_URI"])) . "&amp;layout=button_count&amp;show_faces=false&amp;width145&amp;action=like&amp;font=verdana&amp;colorscheme=light&amp;height=35'></object>";
    }
    $SOCIAL_TWITTER = "<div class='twitter'><a href='https://twitter.com/share' class='twitter-share-button' data-lang='de'>Twittern</a><script type='text/javascript'>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src='//platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document,'script','twitter-wjs');</script></div>";
    $SOCIAL_GOOGLE = "<div class='google'><script type='text/javascript'> window.___gcfg = {lang: 'de'}; (function() { var po = document.createElement('script');po.type = 'text/javascript';po.async = true;po.src = 'https://apis.google.com/js/plusone.js';var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);})();</script><g:plusone size='medium'></g:plusone></div>";
  } else {
    $hide_blocks[] = "SOCIALROW";
  }

  $sbb_add2title .= " - " . he($this_post_title);
  
  $breadcrumps[] = "<a href='" . ($board_config->seo ? "$main_path/sbbf" . $row->fid . adddescr2link($forum[$row->fid]->title) . ".html" : "$main_script?todo=show_topics&amp;fid=" . $row->fid) . "'>" . $forum[$row->fid]->title . "</a>";
  $breadcrumps[] = "<a href='" . ($board_config->seo ? "$main_path/sbbt" . $tid . adddescr2link($row->topic) . ".html" : "$main_script?todo=viewtopic&amp;tid=$tid") . "'>" . $row->topic . "</a>";

  //Template Varialbles
  $hide_blocks[] = "TOPICNOTFOUND";
  
  //hide maintopic on higher pages
  if ($SBBTopics->p > 1) $hide_blocks[] = "MAINTOPIC"; 
  
  //poll?
  if (!$board_config->en_polls) $hide_blocks[] = "POLL";
  elseif (!$row->poll_title) $hide_blocks[] = "POLL";
  else {
    //checkout poll
    if ($row->poll_count) {
      //check bar width
      if ($row->max_votes) $barmultiplier = 200/$row->max_votes;
      else $barmultiplier = 0;
      $voted_for = voted($tid);
      if (!$voted_for) $voted_for = array();
      for ($i=0;$i<$row->poll_count;$i++) {
        $this_option = $row->poll_options[$i];
        $POLL_OPTIONS[] = array("POLL_OPTION" => $this_option->poll_option,
                                "POLL_OPTION_ID" => $this_option->id,
                                "COUNT" => $this_option->votes,
                                "PERCENT" => $row->poll_totals ? round(($this_option->votes/$row->poll_totals) * 100) : 0,
                                "BARWIDTH" => $barmultiplier * $this_option->votes,
                                "VOTED_FOR" => in_array($this_option->id,$voted_for)?"x":"");
      }
      $POLL_OPTIONS2 = $POLL_OPTIONS;
    }
    //check duraction
    if ($row->poll_duration) {
      //endtime
      $endtimestamp = strtotime($row->datetime) + 86400*$row->poll_duration;
      if (time() > $endtimestamp) {
        $poll_ended = true;
        $POLL_SUBHEADLINE = sprintf(lang("poll_ended"),date($date_time_format,$endtimestamp));
      } else $POLL_SUBHEADLINE = sprintf(lang("poll_till"),date($date_time_format,$endtimestamp)); 
    } else $hide_blocks[] = "SUBHEADLINE";
    
    $POLL_RADCHK = ($row->poll_multiple ? "checkbox" : "radio");
    $POLL_HEADLINE = $row->poll_title;
    $TOTAL_VOTES = $row->poll_totals;
    if ($voted_for || !$f_auth_uid || $section == "viewresults" || $poll_ended) $hide_blocks[] = "POLLFORM";
    else $hide_blocks[] = "POLLRESULTS";
  }
  
  //attachments
  if ($row->attachments) {
    $attcnt = count($row->attachments);
    for ($i=0;$i<$attcnt;$i++) {
      $thisatt = $row->attachments[$i];
      $ext = strtolower(substr(strrchr($thisatt->file_name,"."),1));
      $ATTACHMENTS[] = array("ATT_LINK" => ($board_config->seo ? "$main_path/sbbdl" . $thisatt->id . "t" . $thisatt->tid . ".$ext" : "$rel_path/d.php?id=" . $thisatt->id . "&amp;tid=" . $thisatt->tid),
                             "ATT_DESCR" => $thisatt->file_name,
                             "ATT_INFO" => he($thisatt->descr) . " " . fsize(filesize($www_data_dir . "/attachments/" . $thisatt->id . "/" . $thisatt->file_name)));
      //embedded image attachments
      if (in_array($ext,array("jpg","gif","png"))) $row->detail .= "\n\n[img]" . ($board_config->seo ? "$main_path/sbbdl" . $thisatt->id . "t" . $thisatt->tid . ".$ext" : "$rel_path/d.php?id=" . $thisatt->id . "&amp;tid=" . $thisatt->tid) . "[/img]\n\n";
    }
  } else {
    $hide_blocks[] = "ATTACHMENTS";
  }

  //likes
  if ($row->ratings) {
    //likes
    if ($row->ratings["likes"]) {
      $cntlikes = count($row->ratings["likes"]);
      for ($i=0;$i<$cntlikes;$i++) {
        $tl = $row->ratings["likes"][$i];
        $tlusr = forum_user($tl->uid);
        if ($tlusr) $LIKES .= "<a href='" . ($board_config->seo ? "$main_path/sbb-user" . $tlusr->id . adddescr2link($tlusr->user) . ".html" : "$main_script?todo=showprofile&amp;uid=" . $tlusr->id) . "'>" . $tlusr->user . "</a>\n";
        else $LIKES .= $tl->username . "\n";
      }
    } else {
      $hide_blocks[] = "LIKES";
    }
    //dislikes
    if ($row->ratings["dislikes"]) {
      $cntdislikes = count($row->ratings["dislikes"]);
      for ($i=0;$i<$cntdislikes;$i++) {
        $tl = $row->ratings["dislikes"][$i];
        $tlusr = forum_user($tl->uid);
        if ($tlusr) $DISLIKES .= "<a href='" . ($board_config->seo ? "$main_path/sbb-user" . $tlusr->id . adddescr2link($tlusr->user) . ".html" : "$main_script?todo=showprofile&amp;uid=" . $tlusr->id) . "'>" . $tlusr->user . "</a>\n";
        else $DISLIKES .= $tl->username . "\n";
      }
    } else {
      $hide_blocks[] = "DISLIKES";
    }
  }
  if (!$row->ratings["likes"] && !$row->ratings["dislikes"]) $hide_blocks[] = "ANYLIKES";

  //lock vars
  if ($row->locked) {
    $DESCR_LOCKTOPIC = lang("unlock_topic");
    $IMG_LOCK_PREFIX = "un";  //prefix for the lock image (unlock/lock)
    $IMG_REPY_SUFFIX = "_locked";
  } else {
    $DESCR_LOCKTOPIC = lang("lock_topic");
  }

  $DESCR_MOVETOPIC = lang("move_topic");
  
  if ($watched) {
    $DESCR_WATCHTOPIC = lang("unwatch_topic");
    $IMG_WATCH_PREFIX = "un";  //prefix for the lock image (unlock/lock)
  } else {
    $DESCR_WATCHTOPIC = lang("watch_topic");
  }
  
  $TOPIC_LOCK = $row->locked;
  $TOPIC_ID = $tid;
  $FORUM_ID = $row->fid;
  $TOPIC_TITLE = $row->topic;
  $TOPIC_STARTER_CLASS = nick_class($this_user->is_admin, $this_user->is_mod); //CSS-Class for poster
  $TOPIC_STARTER = ($this_user ? $this_user->user : $row->name);
  $TOPIC_DATE = date($date_time_format,strtotime($row->datetime));
  $TOPIC_MESSAGE = bbencode($row->detail,true,$hl);
  
  if ($this_user) {
    $this_user_post_count = forum_user_post_count($this_user->id);
    $TOPIC_STARTER_MEMBER_SINCE = date($date_format,strtotime($this_user->created));
    $TOPIC_STARTER_POSTS = $this_user_post_count;
    $TOPIC_STARTER_UID = $this_user->id;
    if ($this_user->age) {
      $TOPIC_STARTER_AGE = $this_user->age;
      $hide_blocks[] = "TOPICSTARTERBDAY";
    } else {
      $hide_blocks[] = "TOPICSTARTERAGE";
      if ($this_user->birthday) $TOPIC_STARTER_BDAY = date($board_config->date_md_format,strtotime($this_user->birthday));
      else $hide_blocks[] = "TOPICSTARTERBDAY";
    }
    if ($this_user->location) $TOPIC_STARTER_LOCATION = $this_user->location;
    else $hide_blocks[] = "TOPICSTARTERLOCATION";
    if ($this_user->usermail && $board_config->en_user_mess) {
    } else $hide_blocks[] = "TOPICSTARTEREMAIL";
    $hide_blocks[] = "NOTOPICSTARTER";
  } else $hide_blocks[] = "TOPICSTARTER";

  $LINK_STARTER = ($board_config->seo ? "$main_path/sbb-user" . $this_user->id . adddescr2link($this_user->user) . ".html" : "$main_script?todo=showprofile&amp;uid=" . $this_user->id);
  $LINK_SENDSTART = ($board_config->seo ? "$main_path/sbb-sendmail" . $this_user->id . ".html" : "$main_script?todo=sendmail&amp;uid=" . $this_user->id);
  $LINK_PMSTART = ($board_config->seo ? "$main_path/sbb-sendpm" . $this_user->id . ".html" : "$main_script?todo=sendpm&amp;uid=" . $this_user->id);
  
  if ($board_config->en_avatar) {
    if ($this_user->sbb_avatar) {
      $IMG_STARTER_AVATAR = "$rel_path/images/avatars/" . $this_user->sbb_avatar;
    } elseif ($this_user->gravatar) {
      $IMG_STARTER_AVATAR = gravatar($this_user->email);
    } elseif ($this_user->avatar) {
      $IMG_STARTER_AVATAR = $conf_data_dir . "/" . $this_user->id . "/avatar/" . $this_user->avatar;
    } elseif ($board_config->default_avatar) {
      $IMG_STARTER_AVATAR = $template_path . "/images/anonymous.png";
    } else {
      $IMG_STARTER_AVATAR = "$rel_path/images/spacer.png";
    }
  } else {
    $IMG_STARTER_AVATAR = "$rel_path/images/spacer.png";
  }
  
  if ($this_user->rank_id) {
    $STARTER_RANK_IMG_SRC = "$rel_path/images/ranks/" . $userrank[$this_user->rank_id]->img;
    $STARTER_RANK = he($userrank[$this_user->rank_id]->descr);
  } else $hide_blocks[] = "USERRANK";
  
  $STARTER_SIGNATURE = ($this_user->signature ? "<br /><br />---<br />" . make_clickable(bbencode($this_user->signature,false)) : "");
  
  //replies
  if ($SBBTopics->posts["replies"]) {
    for ($i=0;$i<$SBBTopics->count;$i++) {
      $row = $SBBTopics->posts["replies"][$i];
      $this_user = forum_user($row->a_uid);
      $this_user_post_count = forum_user_post_count($this_user->id);
      
      
      if ($board_config->en_avatar) {
        if ($this_user->sbb_avatar) {
          $this_avatar = "$rel_path/images/avatars/" . $this_user->sbb_avatar;
        } elseif ($this_user->gravatar) {
          $this_avatar = gravatar($this_user->email);
        } elseif ($this_user->avatar) {
          $this_avatar = $conf_data_dir . "/" . $this_user->id . "/avatar/" . $this_user->avatar;
        } elseif ($board_config->default_avatar) {
          $this_avatar = $template_path . "/images/anonymous.png";
        } else {
          $this_avatar = "$rel_path/images/spacer.png";
        }
      } else {
        $this_avatar = "$rel_path/images/spacer.png";
      }
      
      if (!has_edit_permission($row->question_id,$row->a_id)) $hide_blocks[] = "ENEDIT".$row->a_id;
      
      //attachments
      $pids[] = $row->a_id;
      $attcnt = count($row->attachments);
      if ($attcnt) {
        for ($m=0;$m<$attcnt;$m++) {
          $thisatt = $row->attachments[$m];
          $ext = strtolower(substr(strrchr($thisatt->file_name,"."),1));
          ${"RATTACHMENTS".$row->a_id}[] = array("ATT_LINK" => ($board_config->seo ? "$main_path/sbbdl" . $thisatt->id . "t" . $thisatt->tid . ".$ext" : "$rel_path/d.php?id=" . $thisatt->id . "&amp;tid=" . $thisatt->tid),
                                                 "ATT_DESCR" => $thisatt->file_name,
                                                 "ATT_INFO" => he($thisatt->descr) . " " . fsize(filesize($www_data_dir . "/attachments/" . $thisatt->id . "/" . $thisatt->file_name)));
          if (in_array($ext,array("jpg","gif","png"))) $row->a_answer .= "\n\n[img]" . ($board_config->seo ? "$main_path/sbbdl" . $thisatt->id . "t" . $thisatt->tid . ".$ext" : "$rel_path/d.php?id=" . $thisatt->id . "&amp;tid=" . $thisatt->tid) . "[/img]\n\n";
        }
      } else {
        ${"RATTACHMENTS".$row->a_id} = array();
        $hide_blocks[] = "RATTACHMENTS".$row->a_id;
      }
      
      unset($this_rank_img, $this_rank_descr);
      
      if ($this_user->rank_id) {
        $this_rank_img = "$rel_path/images/ranks/" . $userrank[$this_user->rank_id]->img;
        $this_rank_descr = he($userrank[$this_user->rank_id]->descr);
      } else $hide_blocks[] = "USERRANK" . intval($this_user->id);


      
      //RATINGS
      if ($SHOWLIKES) {
        $ratingInfo = rating_permission(intval($this_user->id),$tid,$row->a_id);
        if ($ratingInfo === false) {
          $hide_blocks[] = "ENLIKE" . $row->a_id;
          $hide_blocks[] = "ENDISLIKE" . $row->a_id;
        } elseif (in_array($ratingInfo,array(1,-1))) {
          switch($ratingInfo) {
            case 1: $row->RATEDFOR = lang("i_liked_this_post"); break;
            case -1: $row->RATEDFOR = lang("i_didnt_like_this_post"); break;
          }
          $hide_blocks[] = "ENLIKE" . $row->a_id;
          $hide_blocks[] = "ENDISLIKE" . $row->a_id;
        }
        if (!$board_config->en_likes) $hide_blocks[] = "ENLIKE" . $row->a_id;
        if (!$board_config->en_dislikes) $hide_blocks[] = "ENDISLIKE" . $row->a_id;
        if (!$row->RATEDFOR) $hide_blocks[] = "RATEDFOR" . $row->a_id;
      }
      $RDISLIKES = $RLIKES = false;
      if ($row->ratings) {
        //likes
        if ($row->ratings["likes"]) {
          $cntlikes = count($row->ratings["likes"]);
          for ($m=0;$m<$cntlikes;$m++) {
            $tl = $row->ratings["likes"][$m];
            $tlusr = forum_user($tl->uid);
            if ($tlusr) $RLIKES .= "<a href='" . ($board_config->seo ? "$main_path/sbb-user" . $tlusr->id . adddescr2link($tlusr->user) . ".html" : "$main_script?todo=showprofile&amp;uid=" . $tlusr->id) . "'>" . $tlusr->user . "</a>\n";
            else $RLIKES .= $tl->username . "\n";
          }
        } else {
          $hide_blocks[] = "LIKES" . $row->a_id;
        }
        //dislikes
        if ($row->ratings["dislikes"]) {
          $cntdislikes = count($row->ratings["dislikes"]);
          for ($m=0;$m<$cntdislikes;$m++) {
            $tl = $row->ratings["dislikes"][$m];
            $tlusr = forum_user($tl->uid);
            if ($tlusr) $RDISLIKES .= "<a href='" . ($board_config->seo ? "$main_path/sbb-user" . $tlusr->id . adddescr2link($tlusr->user) . ".html" : "$main_script?todo=showprofile&amp;uid=" . $tlusr->id) . "'>" . $tlusr->user . "</a>\n";
            else $RDISLIKES .= $tl->username . "\n";
          }
        } else {
          $hide_blocks[] = "DISLIKES" . $row->a_id;
        }
      }
      if (!$row->ratings["likes"] && !$row->ratings["dislikes"]) $hide_blocks[] = "ANYLIKES" . $row->a_id;

      if ($this_user) $hide_blocks[] = "NOREPLYER" . $row->a_id;

      //Template Vars
      $REPLIES[] = array("REPLY_POSTER" => ($this_user ? $this_user->user : $row->a_name),
                         "REPLY_POSTER_CLASS" => nick_class($this_user->is_admin, $this_user->is_mod), //CSS-Class for poster
                         "REPLY_DATE" => date($date_time_format,strtotime($row->a_datetime)),
                         "DESCR_REPLY_TITLE" => ($row->a_topic != "Re: $this_post_title" ? lang("title") : ""),
                         "REPLY_TITLE" => ($row->a_topic != "Re: $this_post_title" ? $row->a_topic : ""),
                         "REPLY_ID" => $row->a_id,
                         "LINK_REPLYER" => ($board_config->seo ? "$main_path/sbb-user" . intval($this_user->id) . adddescr2link($this_user->user) . ".html" : "$main_script?todo=showprofile&amp;uid=" . $this_user->id),
                         "LINK_SENDREPL" => ($board_config->seo ? "$main_path/sbb-sendmail" . intval($this_user->id) . ".html" : "$main_script?todo=sendmail&amp;uid=" . $this_user->id),
                         "LINK_PMREPL" => ($board_config->seo ? "$main_path/sbb-sendpm" . intval($this_user->id) . ".html" : "$main_script?todo=sendpm&amp;uid=" . intval($this_user->id)),
                         "REPLY_MESSAGE" => bbencode($row->a_answer,true,$hl),
                         "REPLY_UID" => intval($this_user->id),
                         "REPLY_RANK_IMG_SRC" => $this_rank_img,
                         "REPLY_RANK" => $this_rank_descr,
                         "IMG_POSTER_AVATAR" => $this_avatar,
                         "SIGNATURE" => ($this_user->signature ? "<br /><br />---<br />" . make_clickable(bbencode($this_user->signature,false)) : ""),
                         "REPLY_POSTER_MEMBER_SINCE" => ($this_user ? date($date_format,strtotime($this_user->created)) : ""),
                         "REPLY_POSTER_POSTS" => $this_user_post_count,
                         "REPLY_POSTER_AGE" => $this_user->age,
                         "REPLY_POSTER_BDAY" => date($board_config->date_md_format,strtotime($this_user->birthday)),
                         "REPLY_POSTER_LOCATION" => $this_user->location,
                         "RATEDFOR" => $row->RATEDFOR,
                         "LIKES" => $RLIKES,
                         "DISLIKES" => $RDISLIKES,
                         );
      
      //hide some infos
      if ($this_user->age) $hide_blocks[] = "REPLYER" . intval($this_user->id) . "BDAY";
      else {
        $hide_blocks[] = "REPLYER" . intval($this_user->id) . "AGE";
        if (!$this_user->birthday) $hide_blocks[] = "REPLYER" . intval($this_user->id) . "BDAY";
      }
      if (!$this_user->location) $hide_blocks[] = "REPLYER" . intval($this_user->id) . "LOCATION";
      if (!$this_user->usermail || !$board_config->en_user_mess) $hide_blocks[] = "REPLYEREMAIL" . intval($this_user->id);
    }
    //hide the not found userprofiles and ranks
    $hide_blocks[] = "REPLYER0";        
  } else {
    $hide_blocks[] = "REPLIESFOUND";
  }

  //Set the views for this topic - prevent of doublecounting by session
  $viewed_tids = $_SESSION["viewed_tids"];
  if (!$viewed_tids) $viewed_tids = array();
  
  if (!in_array($tid, $viewed_tids)) {
    $SBBTopics->updateViews();
  }
  $viewed_tids[] = $tid;
  $_SESSION["viewed_tids"] = $viewed_tids;
  
  //pagination
  $LINK_PAGE_PREFIX = ($board_config->seo ? "$main_path/sbbt" . $tid . adddescr2link($SBBTopics->posts["main"]->topic) . ".html?p=" : "$main_script?todo=viewtopic&amp;tid=$tid&amp;p=");
  $NAV_PAGES = pagination($SBBTopics->pages,$SBBTopics->p);
  
  $CURRENT_PAGE = $SBBTopics->p;

  //hide navblock if empty
  if (!$NAV_PAGES) $hide_blocks[] = "NAV_PAGES";
  
  
  //txt export
  $LINK_PRINT_TXT = ($board_config->seo ? "$main_path/sbbt" . $tid . ".txt" : "$rel_path/txt.php?tid=$tid");
  
  /************
   * initiate template and assign variables
   ************/  
  
    $tpl->load_file("viewtopic", "$template_path/viewtopic.html");
    $tpl->parse_loop('viewtopic', 'REPLIES');
    if ($POLL_OPTIONS) $tpl->parse_loop('viewtopic', 'POLL_OPTIONS');
    if ($POLL_OPTIONS2) $tpl->parse_loop('viewtopic', 'POLL_OPTIONS2');
    if ($NAV_PAGES) $tpl->parse_loop('viewtopic', 'NAV_PAGES');
    $tpl->parse_loop('viewtopic', 'ATTACHMENTS');
    for ($i=0;$i<count($pids);$i++) {
      $tpl->parse_loop('viewtopic', 'RATTACHMENTS' . $pids[$i]);
    }
    if ($hide_blocks) $tpl->parse_hide('viewtopic', $hide_blocks);
    //write the template-file to the content-var
    $content .= $tpl->pget('viewtopic', array('CONF_MAINSCRIPT', 'CONF_RELPATH', 'CONF_LANG', 'CONF_TEMPLATE_PATH', 
      'IMG_NEWTOPIC_TITLE', 'IMG_REPLY_TITLE', 'IMG_PROFILE_TITLE', 'IMG_SENDMAIL_TITLE', 'IMG_DELETE_SINGLE_TITLE', 'IMG_EDIT_SINGLE_POST', 'TOPIC_LOCK', 'IMG_QUOTE',
      'DESCR_AUTHOR', 'DESCR_MESSAGE', 'DESCR_COMPOSED', 'DESCR_TITLE', 'DESCR_DELTOPIC', 'CONFIRM_DELPOST', 'CONFIRM_DELTOPIC', 'DESCR_LOCKTOPIC', 'IMG_LOCK_PREFIX', 'IMG_REPY_SUFFIX', 'DESCR_JOINED', 'DESCR_POSTS', 'DESCR_AGE', 'DESCR_BDAY', 'DESCR_LOCATION', 'DESCR_WATCHTOPIC', 'IMG_WATCH_PREFIX', 'DESCR_ATTACHMENTS',
      'TOPIC_ID', 'FORUM_ID', 'TOPIC_TITLE', 'TOPIC_STARTER', 'TOPIC_STARTER_CLASS', 'TOPIC_DATE', 'TOPIC_MESSAGE', 'TOPIC_STARTER_UID', 'TOPIC_NOT_FOUND', 'IMG_STARTER_AVATAR', 'STARTER_SIGNATURE', 'TOPIC_STARTER_MEMBER_SINCE', 'TOPIC_STARTER_POSTS', 'TOPIC_STARTER_AGE', 'TOPIC_STARTER_BDAY', 'TOPIC_STARTER_LOCATION', 'LINK_PMSTART', 'IMG_PM_TITLE',
      'LINK_ADD_TOPIC', 'LINK_REPLY', 'LINK_STARTER', 'LINK_SENDSTART', 'LINK_REPLY_QUOTE_T', 'LINK_REPLY_QUOTE_R', 'LINK_PAGE_PREFIX', 'DESCR_PRINT', 'LINK_PRINT_TXT',
      'POLL_RADCHK', 'POLL_HEADLINE', 'DESCR_SUBMIT_VOTE', 'DESCR_VIEW_RESULTS', 'LINK_RESULTS', 'DESCR_TOTAL_VOTES', 'TOTAL_VOTES', 'DESCR_VOTED_FOR', 'POLL_SUBHEADLINE',
      'DESCR_MOVETOPIC',
      'STARTER_RANK_IMG_SRC', 'STARTER_RANK', 'SOCIAL_FACEBOOK', 'SOCIAL_GOOGLE', 'SOCIAL_TWITTER', 'IMG_ILIKETHISPOST', 'IMG_IDONTLIKETHISPOST', 'RATEDFOR','LIKES','DISLIKES','CURRENT_PAGE'));
} else {
  $content .= "<div class='infobox'>" . lang("topic_doesnt_exist") . "</div>";
}

    

?>