<?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();

$NAV_PAGES = array();

//Template Variables
$IMG_NEWTOPIC_TITLE = lang("btn_new_topic");
$IMG_REPLY_TITLE = lang("btn_reply");
$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("btn_profile");
$IMG_SENDMAIL_TITLE = lang("btn_email");
$IMG_PM_TITLE = lang("btn_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);

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

$SBBTopics->GetPosts(); 

if ($board_config->en_likes || $board_config->en_dislikes) $TEMPLATE_FLAGS["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)) $TEMPLATE_FLAGS["ENEDIT"] = true;

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


  //socialrow
  if ($board_config->socialrow) {
    $TEMPLATE_FLAGS["SOCIALROW"] = true;
    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>";
  }

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

  //Template Varialbles
  
  //hide maintopic on higher pages
  $TEMPLATE_FLAGS["MAINTOPIC"] = true;
  if ($SBBTopics->p > 1) $TEMPLATE_FLAGS["MAINTOPIC"] = false;
  
  //poll?
  $TEMPLATE_FLAGS["POLL"] = true;
  if (!$board_config->en_polls) $TEMPLATE_FLAGS["POLL"] = false;
  elseif (!$row->poll_title) $TEMPLATE_FLAGS["POLL"] = false;
  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)); 
    }
    
    $POLL_RADCHK = ($row->poll_multiple ? "checkbox" : "radio");
    $POLL_HEADLINE = $row->poll_title;
    $TOTAL_VOTES = $row->poll_totals;
    $TEMPLATE_FLAGS["POLLFORM"] = $TEMPLATE_FLAGS["POLLRESULTS"] = true;
    if ($voted_for || !$f_auth_uid || $section == "viewresults" || $poll_ended)$TEMPLATE_FLAGS["POLLFORM"] = false;
    else $TEMPLATE_FLAGS["POLLRESULTS"] = false;
  }
  
  //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";
    }
    $TEMPLATE_FLAGS["ATTACHMENTS"] = true;
  }

  //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) $tlusrlink = ($board_config->seo ? "$main_path/sbb-user" . $tlusr->id . adddescr2link($tlusr->user) . ".html" : "$main_script?todo=showprofile&amp;uid=" . $tlusr->id);
        else $tlusrlink = false;
        $LIKES[] = array("USERNAME" => $tlusr ? $tlusr->user : $tl->username,
          "USERLINK" => $tlusrlink);
      }
      $TEMPLATE_FLAGS["ANYLIKES"] = true;
    }
    //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) $tlusrlink = ($board_config->seo ? "$main_path/sbb-user" . $tlusr->id . adddescr2link($tlusr->user) . ".html" : "$main_script?todo=showprofile&amp;uid=" . $tlusr->id);
        else $tlusrlink = false;
        $DISLIKES[] = array("USERNAME" => $tlusr ? $tlusr->user : $tl->username,
          "USERLINK" => $tlusrlink);
      }
      $TEMPLATE_FLAGS["ANYLIKES"] = true;
    }
  }
  
  //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";
    $IMG_REPLY_TITLE = lang("btn_reply_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) {
    $TEMPLATE_FLAGS["TOPICSTARTER"] = true;
    $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;
      $TEMPLATE_FLAGS["TOPICSTARTERAGE"] = true;
    } elseif ($this_user->birthday) {
      $TOPIC_STARTER_BDAY = date($board_config->date_md_format,strtotime($this_user->birthday));
      $TEMPLATE_FLAGS["TOPICSTARTERBDAY"] = true;
    }
    if ($this_user->location) $TOPIC_STARTER_LOCATION = $this_user->location;
    if ($this_user->usermail && $board_config->en_user_mess) $TEMPLATE_FLAGS["TOPICSTARTEREMAIL"] = true;
  }

  $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);
    $TEMPLATE_FLAGS["USERRANK"] = true;
  }
  
  $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";
      }
      
      //attachments
      $pids[] = $row->a_id;
      $attcnt = count($row->attachments);
      $RATTACHMENTS = array();
      if ($attcnt) {
        for ($m=0;$m<$attcnt;$m++) {
          $thisatt = $row->attachments[$m];
          $ext = strtolower(substr(strrchr($thisatt->file_name,"."),1));
          $RATTACHMENTS[] = 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";
        }
      }
      
      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);
      }
      

      //RATINGS
      if ($TEMPLATE_FLAGS["SHOWLIKES"]) {
        $ratingInfo = rating_permission(intval($this_user->id),$tid,$row->a_id);
        if ($ratingInfo === false) $row->ENLIKE = $row->ENDISLIKE = false;
        elseif ($ratingInfo === 0) $row->ENLIKE = $row->ENDISLIKE = true;
        else {
          switch($ratingInfo) {
            case 1: $row->RATEDFOR = lang("i_liked_this_post"); break;
            case -1: $row->RATEDFOR = lang("i_didnt_like_this_post"); break;
          }
          $row->RATEDVAL = $ratingInfo;
        }
        if (!$board_config->en_likes) $row->ENLIKE = false;
        if (!$board_config->en_dislikes) $row->ENDISLIKE = false;
      }
      $ANYLIKES = false;
      $RDISLIKES = $RLIKES = array();
      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) $tlusrlink = ($board_config->seo ? "$main_path/sbb-user" . $tlusr->id . adddescr2link($tlusr->user) . ".html" : "$main_script?todo=showprofile&amp;uid=" . $tlusr->id);
            else $tlusrlink = false;
            $RLIKES[] = array("USERNAME" => $tlusr ? $tlusr->user : $tl->username,
              "USERLINK" => $tlusrlink);
          }
          $ANYLIKES = true;
        }
        //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) $tlusrlink = ($board_config->seo ? "$main_path/sbb-user" . $tlusr->id . adddescr2link($tlusr->user) . ".html" : "$main_script?todo=showprofile&amp;uid=" . $tlusr->id);
            else $tlusrlink = false;
            $RDISLIKES[] = array("USERNAME" => $tlusr ? $tlusr->user : $tl->username,
              "USERLINK" => $tlusrlink);
          }
          $ANYLIKES = true;
        }
      }


      //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" => $this_user->birthday ? date($board_config->date_md_format,strtotime($this_user->birthday)) : false,
                         "REPLY_POSTER_LOCATION" => $this_user->location,
                         "RATTACHMENTS" => $RATTACHMENTS,
                         "RENEDIT" => has_edit_permission($row->question_id,$row->a_id),
                         "REPLY_ENMAIL" => $this_user->usermail && $board_config->en_user_mess ? true : false,
                         "RATEDFOR" => $row->RATEDFOR,
                         "RATEDVAL" => $row->RATEDVAL,
                         "ENLIKE" => $row->ENLIKE,
                         "ENDISLIKE" => $row->ENDISLIKE,
                         "ANYLIKES" => $ANYLIKES,
                         "LIKES" => $RLIKES,
                         "DISLIKES" => $RDISLIKES,
                         );
    }    
  }
  
  //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 = pagination2ndGen($SBBTopics->pages,$SBBTopics->p);

  $CURRENT_PAGE = $SBBTopics->p;
  
  //txt export
  $LINK_PRINT_TXT = ($board_config->seo ? "$main_path/sbbt" . $tid . ".txt" : "$rel_path/txt.php?tid=$tid");
  
  /************
   * initiate template and assign variables
   ************/  
  $viewtopicVARS = array('IMG_NEWTOPIC_TITLE' => $IMG_NEWTOPIC_TITLE,
                        'IMG_REPLY_TITLE' => $IMG_REPLY_TITLE,
                        'IMG_PROFILE_TITLE' => $IMG_PROFILE_TITLE,
                        'IMG_SENDMAIL_TITLE' => $IMG_SENDMAIL_TITLE,
                        'IMG_DELETE_SINGLE_TITLE' => $IMG_DELETE_SINGLE_TITLE,
                        'IMG_EDIT_SINGLE_POST' => $IMG_EDIT_SINGLE_POST,
                        'TOPIC_LOCK' => $TOPIC_LOCK,
                        'IMG_QUOTE' => $IMG_QUOTE,
                        'DESCR_AUTHOR' => $DESCR_AUTHOR,
                        'DESCR_MESSAGE' => $DESCR_MESSAGE,
                        'DESCR_COMPOSED' => $DESCR_COMPOSED,
                        'DESCR_TITLE' => $DESCR_TITLE,
                        'DESCR_DELTOPIC' => $DESCR_DELTOPIC,
                        'CONFIRM_DELPOST' => $CONFIRM_DELPOST,
                        'CONFIRM_DELTOPIC' => $CONFIRM_DELTOPIC,
                        'DESCR_LOCKTOPIC' => $DESCR_LOCKTOPIC,
                        'IMG_LOCK_PREFIX' => $IMG_LOCK_PREFIX,
                        'IMG_REPY_SUFFIX' => $IMG_REPY_SUFFIX,
                        'DESCR_JOINED' => $DESCR_JOINED,
                        'DESCR_POSTS' => $DESCR_POSTS,
                        'DESCR_AGE' => $DESCR_AGE,
                        'DESCR_BDAY' => $DESCR_BDAY,
                        'DESCR_LOCATION' => $DESCR_LOCATION,
                        'DESCR_WATCHTOPIC' => $DESCR_WATCHTOPIC,
                        'IMG_WATCH_PREFIX' => $IMG_WATCH_PREFIX,
                        'DESCR_ATTACHMENTS' => $DESCR_ATTACHMENTS,
                        'TOPIC_ID' => $TOPIC_ID,
                        'FORUM_ID' => $FORUM_ID,
                        'TOPIC_TITLE' => $TOPIC_TITLE,
                        'TOPIC_STARTER' => $TOPIC_STARTER,
                        'TOPIC_STARTER_CLASS' => $TOPIC_STARTER_CLASS,
                        'TOPIC_DATE' => $TOPIC_DATE,
                        'TOPIC_MESSAGE' => $TOPIC_MESSAGE,
                        'TOPIC_STARTER_UID' => $TOPIC_STARTER_UID,
                        'TOPIC_NOT_FOUND' => $TOPIC_NOT_FOUND,
                        'IMG_STARTER_AVATAR' => $IMG_STARTER_AVATAR,
                        'STARTER_SIGNATURE' => $STARTER_SIGNATURE,
                        'TOPIC_STARTER_MEMBER_SINCE' => $TOPIC_STARTER_MEMBER_SINCE,
                        'TOPIC_STARTER_POSTS' => $TOPIC_STARTER_POSTS,
                        'TOPIC_STARTER_AGE' => $TOPIC_STARTER_AGE,
                        'TOPIC_STARTER_BDAY' => $TOPIC_STARTER_BDAY,
                        'TOPIC_STARTER_LOCATION' => $TOPIC_STARTER_LOCATION,
                        'LINK_PMSTART' => $LINK_PMSTART,
                        'IMG_PM_TITLE' => $IMG_PM_TITLE,
                        'LINK_ADD_TOPIC' => $LINK_ADD_TOPIC,
                        'LINK_REPLY' => $LINK_REPLY,
                        'LINK_STARTER' => $LINK_STARTER,
                        'LINK_SENDSTART' => $LINK_SENDSTART,
                        'LINK_REPLY_QUOTE_T' => $LINK_REPLY_QUOTE_T,
                        'LINK_REPLY_QUOTE_R' => $LINK_REPLY_QUOTE_R,
                        'LINK_PAGE_PREFIX' => $LINK_PAGE_PREFIX,
                        'DESCR_PRINT' => $DESCR_PRINT,
                        'LINK_PRINT_TXT' => $LINK_PRINT_TXT,
                        'POLL_RADCHK' => $POLL_RADCHK,
                        'POLL_HEADLINE' => $POLL_HEADLINE,
                        'DESCR_SUBMIT_VOTE' => $DESCR_SUBMIT_VOTE,
                        'DESCR_VIEW_RESULTS' => $DESCR_VIEW_RESULTS,
                        'LINK_RESULTS' => $LINK_RESULTS,
                        'DESCR_TOTAL_VOTES' => $DESCR_TOTAL_VOTES,
                        'TOTAL_VOTES' => $TOTAL_VOTES,
                        'DESCR_VOTED_FOR' => $DESCR_VOTED_FOR,
                        'POLL_SUBHEADLINE' => $POLL_SUBHEADLINE,
                        'DESCR_MOVETOPIC' => $DESCR_MOVETOPIC,
                        'STARTER_RANK_IMG_SRC' => $STARTER_RANK_IMG_SRC,
                        'STARTER_RANK' => $STARTER_RANK,

                        'CONF' => $GLOBAL_TEMPLATE_VARS,
                        'FLAGS' => $TEMPLATE_FLAGS,
                        'ATTACHMENTS' => $ATTACHMENTS,
                        'LIKES' => $LIKES,
                        'DISLIKES' => $DISLIKES,
                        'POLL_OPTIONS' => $POLL_OPTIONS,
                        'POLL_OPTIONS2' => $POLL_OPTIONS2,
                        'NAV_PAGES' => $NAV_PAGES,
                        "REPLIES" => $REPLIES,
                        "SOCIAL_FACEBOOK" => $SOCIAL_FACEBOOK,
                        "SOCIAL_TWITTER" => $SOCIAL_TWITTER,
                        "SOCIAL_GOOGLE" => $SOCIAL_GOOGLE,
                        "RATEDFOR" => $RATEDFOR,
                        "RATEDVAL" => $RATEDVAL,
                        "IMG_ILIKETHISPOST" => $IMG_ILIKETHISPOST,
                        "IMG_IDONTLIKETHISPOST" => $IMG_IDONTLIKETHISPOST,
                        'CURRENT_PAGE' => $CURRENT_PAGE
                        );
  $QuickSkin = new QuickSkin("viewtopic.html");
  $QuickSkin->assign($viewtopicVARS);
  $content .= $QuickSkin->result();
  unset($QuickSkin);

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

    

?>