[ Index ]

PHP Cross Reference of MyBB 1.8.27

title

Body

[close]

/ -> showthread.php (source)

   1  <?php
   2  /**
   3   * MyBB 1.8
   4   * Copyright 2014 MyBB Group, All Rights Reserved
   5   *
   6   * Website: http://www.mybb.com
   7   * License: http://www.mybb.com/about/license
   8   *
   9   */
  10  
  11  define("IN_MYBB", 1);
  12  define('THIS_SCRIPT', 'showthread.php');
  13  
  14  $templatelist = "showthread,postbit,postbit_author_user,postbit_author_guest,showthread_newthread,showthread_newreply,showthread_newreply_closed,postbit_avatar,postbit_find,postbit_pm,postbit_www,postbit_email,postbit_edit,postbit_quote,postbit_report";
  15  $templatelist .= ",multipage,multipage_breadcrumb,multipage_end,multipage_jump_page,multipage_nextpage,multipage_page,multipage_page_current,multipage_page_link_current,multipage_prevpage,multipage_start,showthread_inlinemoderation_softdelete,showthread_poll_editpoll";
  16  $templatelist .= ",postbit_editedby,showthread_similarthreads,showthread_similarthreads_bit,postbit_iplogged_show,postbit_iplogged_hiden,postbit_profilefield,showthread_quickreply,showthread_printthread,showthread_add_poll,showthread_send_thread,showthread_inlinemoderation_restore";
  17  $templatelist .= ",forumjump_advanced,forumjump_special,forumjump_bit,postbit_attachments,postbit_attachments_attachment,postbit_attachments_thumbnails,postbit_attachments_images_image,postbit_attachments_images,showthread_quickreply_options_stick,postbit_status";
  18  $templatelist .= ",postbit_inlinecheck,showthread_inlinemoderation,postbit_attachments_thumbnails_thumbnail,postbit_ignored,postbit_multiquote,showthread_moderationoptions_custom_tool,showthread_moderationoptions_custom,showthread_inlinemoderation_custom_tool";
  19  $templatelist .= ",showthread_usersbrowsing,showthread_usersbrowsing_user,showthread_poll_option,showthread_poll,showthread_quickreply_options_signature,showthread_threaded_bitactive,showthread_threaded_bit,postbit_attachments_attachment_unapproved";
  20  $templatelist .= ",showthread_moderationoptions_openclose,showthread_moderationoptions_stickunstick,showthread_moderationoptions_delete,showthread_moderationoptions_threadnotes,showthread_moderationoptions_manage,showthread_moderationoptions_deletepoll";
  21  $templatelist .= ",postbit_userstar,postbit_reputation_formatted_link,postbit_warninglevel_formatted,postbit_quickrestore,forumdisplay_password,forumdisplay_password_wrongpass,postbit_purgespammer,showthread_inlinemoderation_approve,forumdisplay_thread_icon";
  22  $templatelist .= ",showthread_moderationoptions_softdelete,showthread_moderationoptions_restore,post_captcha,post_captcha_recaptcha_invisible,post_captcha_nocaptcha,post_captcha_hcaptcha_invisible,post_captcha_hcaptcha,showthread_moderationoptions,showthread_inlinemoderation_standard,showthread_inlinemoderation_manage";
  23  $templatelist .= ",showthread_ratethread,postbit_posturl,postbit_icon,postbit_editedby_editreason,attachment_icon,global_moderation_notice,showthread_poll_option_multiple,postbit_gotopost,postbit_rep_button,postbit_warninglevel,showthread_threadnoteslink";
  24  $templatelist .= ",showthread_moderationoptions_approve,showthread_moderationoptions_unapprove,showthread_inlinemoderation_delete,showthread_moderationoptions_standard,showthread_quickreply_options_close,showthread_inlinemoderation_custom,showthread_search";
  25  $templatelist .= ",postbit_profilefield_multiselect_value,postbit_profilefield_multiselect,showthread_subscription,postbit_deleted_member,postbit_away,postbit_warn,postbit_classic,postbit_reputation,postbit_deleted,postbit_offline,postbit_online,postbit_signature";
  26  $templatelist .= ",postbit_editreason,postbit_quickdelete,showthread_threadnotes_viewnotes,showthread_threadedbox,showthread_poll_resultbit,showthread_poll_results,showthread_threadnotes,showthread_classic_header,showthread_poll_undovote,postbit_groupimage";
  27  
  28  require_once  "./global.php";
  29  require_once  MYBB_ROOT."inc/functions_post.php";
  30  require_once  MYBB_ROOT."inc/functions_indicators.php";
  31  require_once  MYBB_ROOT."inc/class_parser.php";
  32  $parser = new postParser;
  33  
  34  // Load global language phrases
  35  $lang->load("showthread");
  36  
  37  // If there is no tid but a pid, trick the system into thinking there was a tid anyway.
  38  if(!empty($mybb->input['pid']) && !isset($mybb->input['tid']))
  39  {
  40      // see if we already have the post information
  41      if(isset($style) && $style['pid'] == $mybb->get_input('pid', MyBB::INPUT_INT) && $style['tid'])
  42      {
  43          $mybb->input['tid'] = $style['tid'];
  44          unset($style['tid']); // stop the thread caching code from being tricked
  45      }
  46      else
  47      {
  48          $options = array(
  49              "limit" => 1
  50          );
  51          $query = $db->simple_select("posts", "fid,tid,visible", "pid=".$mybb->get_input('pid', MyBB::INPUT_INT), $options);
  52          $post = $db->fetch_array($query);
  53  
  54          if(
  55              empty($post) ||
  56              (
  57                  $post['visible'] == 0 && !(
  58                      is_moderator($post['fid'], 'canviewunapprove') ||
  59                      ($mybb->user['uid'] && $post['uid'] == $mybb->user['uid'] && $mybb->settings['showownunapproved'])
  60                  )
  61              ) ||
  62              ($post['visible'] == -1 && !is_moderator($post['fid'], 'canviewdeleted'))
  63          )
  64          {
  65              // post does not exist --> show corresponding error
  66              error($lang->error_invalidpost);
  67          }
  68  
  69          $mybb->input['tid'] = $post['tid'];
  70      }
  71  }
  72  
  73  // Get the thread details from the database.
  74  $thread = get_thread($mybb->get_input('tid', MyBB::INPUT_INT));
  75  
  76  if(!$thread || substr($thread['closed'], 0, 6) == "moved|")
  77  {
  78      error($lang->error_invalidthread);
  79  }
  80  
  81  // Get thread prefix if there is one.
  82  $thread['threadprefix'] = '';
  83  $thread['displayprefix'] = '';
  84  if($thread['prefix'] != 0)
  85  {
  86      $threadprefix = build_prefixes($thread['prefix']);
  87  
  88      if(!empty($threadprefix['prefix']))
  89      {
  90          $thread['threadprefix'] = htmlspecialchars_uni($threadprefix['prefix']).'&nbsp;';
  91          $thread['displayprefix'] = $threadprefix['displaystyle'].'&nbsp;';
  92      }
  93  }
  94  
  95  $reply_subject = $parser->parse_badwords($thread['subject']);
  96  $thread['subject'] = htmlspecialchars_uni($reply_subject);
  97  // Subject too long? Shorten it to avoid error message
  98  if(my_strlen($reply_subject) > 85)
  99  {
 100      $reply_subject = my_substr($reply_subject, 0, 82).'...';
 101  }
 102  $reply_subject = htmlspecialchars_uni($reply_subject);
 103  $tid = $thread['tid'];
 104  $fid = $thread['fid'];
 105  
 106  if(!$thread['username'])
 107  {
 108      $thread['username'] = $lang->guest;
 109  }
 110  $thread['username'] = htmlspecialchars_uni($thread['username']);
 111  
 112  $forumpermissions = forum_permissions($thread['fid']);
 113  
 114  // Set here to fetch only approved/deleted posts (and then below for a moderator we change this).
 115  $visible_states = array("1");
 116  
 117  if($forumpermissions['canviewdeletionnotice'] != 0)
 118  {
 119      $visible_states[] = "-1";
 120  }
 121  
 122  // Is the currently logged in user a moderator of this forum?
 123  if(is_moderator($fid))
 124  {
 125      $ismod = true;
 126      if(is_moderator($fid, "canviewdeleted") == true)
 127      {
 128          $visible_states[] = "-1";
 129      }
 130      if(is_moderator($fid, "canviewunapprove") == true)
 131      {
 132          $visible_states[] = "0";
 133      }
 134  }
 135  else
 136  {
 137      $ismod = false;
 138  }
 139  
 140  $visible_condition = "visible IN (".implode(',', array_unique($visible_states)).")";
 141  
 142  // Allow viewing own unapproved threads for logged in users
 143  if($mybb->user['uid'] && $mybb->settings['showownunapproved'])
 144  {
 145      $own_unapproved = ' AND (%1$s'.$visible_condition.' OR (%1$svisible=0 AND %1$suid='.(int)$mybb->user['uid'].'))';
 146  
 147      $visibleonly = sprintf($own_unapproved, null);
 148      $visibleonly_p = sprintf($own_unapproved, 'p.');
 149      $visibleonly_p_t = sprintf($own_unapproved, 'p.').sprintf($own_unapproved, 't.');
 150  }
 151  else
 152  {
 153      $visibleonly = " AND ".$visible_condition;
 154      $visibleonly_p = " AND p.".$visible_condition;
 155      $visibleonly_p_t = "AND p.".$visible_condition." AND t.".$visible_condition;
 156  }
 157  
 158  // Make sure we are looking at a real thread here.
 159  if(($thread['visible'] != 1 && $ismod == false) || ($thread['visible'] == 0 && !is_moderator($fid, "canviewunapprove")) || ($thread['visible'] == -1 && !is_moderator($fid, "canviewdeleted")))
 160  {
 161      // Allow viewing own unapproved thread
 162      if (!($mybb->user['uid'] && $mybb->settings['showownunapproved'] && $thread['visible'] == 0 && ($thread['uid'] == $mybb->user['uid'])))
 163      {
 164          error($lang->error_invalidthread);
 165      }
 166  }
 167  
 168  // Does the user have permission to view this thread?
 169  if($forumpermissions['canview'] != 1 || $forumpermissions['canviewthreads'] != 1)
 170  {
 171      error_no_permission();
 172  }
 173  
 174  if(isset($forumpermissions['canonlyviewownthreads']) && $forumpermissions['canonlyviewownthreads'] == 1 && $thread['uid'] != $mybb->user['uid'])
 175  {
 176      error_no_permission();
 177  }
 178  
 179  $archive_url = build_archive_link("thread", $tid);
 180  
 181  // Does the thread belong to a valid forum?
 182  $forum = get_forum($fid);
 183  if(!$forum || $forum['type'] != "f")
 184  {
 185      error($lang->error_invalidforum);
 186  }
 187  
 188  $threadnoteslink = '';
 189  if(is_moderator($fid, "canmanagethreads") && !empty($thread['notes']))
 190  {
 191      eval('$threadnoteslink = "'.$templates->get('showthread_threadnoteslink').'";');
 192  }
 193  
 194  // Check if this forum is password protected and we have a valid password
 195  check_forum_password($forum['fid']);
 196  
 197  // If there is no specific action, we must be looking at the thread.
 198  if(!$mybb->get_input('action'))
 199  {
 200      $mybb->input['action'] = "thread";
 201  }
 202  
 203  // Jump to the unread posts.
 204  if($mybb->input['action'] == "newpost")
 205  {
 206      // First, figure out what time the thread or forum were last read
 207      $query = $db->simple_select("threadsread", "dateline", "uid='{$mybb->user['uid']}' AND tid='{$thread['tid']}'");
 208      $thread_read = $db->fetch_field($query, "dateline");
 209  
 210      if($mybb->settings['threadreadcut'] > 0 && $mybb->user['uid'])
 211      {
 212          $query = $db->simple_select("forumsread", "dateline", "fid='{$fid}' AND uid='{$mybb->user['uid']}'");
 213          $forum_read = $db->fetch_field($query, "dateline");
 214  
 215          $read_cutoff = TIME_NOW-$mybb->settings['threadreadcut']*60*60*24;
 216          if($forum_read == 0 || $forum_read < $read_cutoff)
 217          {
 218              $forum_read = $read_cutoff;
 219          }
 220      }
 221      else
 222      {
 223          $forum_read = (int)my_get_array_cookie("forumread", $fid);
 224      }
 225  
 226      if($mybb->settings['threadreadcut'] > 0 && $mybb->user['uid'] && $thread['lastpost'] > $forum_read)
 227      {
 228          $cutoff = TIME_NOW-$mybb->settings['threadreadcut']*60*60*24;
 229          if($thread['lastpost'] > $cutoff)
 230          {
 231              if($thread_read)
 232              {
 233                  $lastread = $thread_read;
 234              }
 235              else
 236              {
 237                  // Set $lastread to zero to make sure 'lastpost' is invoked in the last IF
 238                  $lastread = 0;
 239              }
 240          }
 241      }
 242  
 243      if(!$lastread)
 244      {
 245          $readcookie = $threadread = (int)my_get_array_cookie("threadread", $thread['tid']);
 246          if($readcookie > $forum_read)
 247          {
 248              $lastread = $readcookie;
 249          }
 250          else
 251          {
 252              $lastread = $forum_read;
 253          }
 254      }
 255  
 256     if($cutoff && $lastread < $cutoff)
 257     {
 258            $lastread = $cutoff;
 259     }
 260  
 261      // Next, find the proper pid to link to.
 262      $options = array(
 263          "limit_start" => 0,
 264          "limit" => 1,
 265          "order_by" => "dateline, pid",
 266      );
 267  
 268      $lastread = (int)$lastread;
 269      $query = $db->simple_select("posts", "pid", "tid='{$tid}' AND dateline > '{$lastread}' {$visibleonly}", $options);
 270      $newpost = $db->fetch_array($query);
 271  
 272      if($newpost['pid'] && $lastread)
 273      {
 274          $highlight = '';
 275          if($mybb->get_input('highlight'))
 276          {
 277              $string = "&";
 278              if($mybb->seo_support == true)
 279              {
 280                  $string = "?";
 281              }
 282  
 283              $highlight = $string."highlight=".$mybb->get_input('highlight');
 284          }
 285  
 286          header("Location: ".htmlspecialchars_decode(get_post_link($newpost['pid'], $tid)).$highlight."#pid{$newpost['pid']}");
 287      }
 288      else
 289      {
 290          // show them to the last post
 291          $mybb->input['action'] = "lastpost";
 292      }
 293  }
 294  
 295  // Jump to the last post.
 296  if($mybb->input['action'] == "lastpost")
 297  {
 298      if(my_strpos($thread['closed'], "moved|"))
 299      {
 300          $query = $db->query("
 301              SELECT p.pid
 302              FROM ".TABLE_PREFIX."posts p
 303              LEFT JOIN ".TABLE_PREFIX."threads t ON(p.tid=t.tid)
 304              WHERE t.fid='".$thread['fid']."' AND t.closed NOT LIKE 'moved|%' {$visibleonly_p_t}
 305              ORDER BY p.dateline DESC, p.pid DESC
 306              LIMIT 1
 307          ");
 308          $pid = $db->fetch_field($query, "pid");
 309      }
 310      else
 311      {
 312          $options = array(
 313              'order_by' => 'dateline DESC, pid DESC',
 314              'limit_start' => 0,
 315              'limit' => 1
 316          );
 317          $query = $db->simple_select('posts', 'pid', "tid={$tid} {$visibleonly}", $options);
 318          $pid = $db->fetch_field($query, "pid");
 319      }
 320      header("Location: ".htmlspecialchars_decode(get_post_link($pid, $tid))."#pid{$pid}");
 321      exit;
 322  }
 323  
 324  // Jump to the next newest posts.
 325  if($mybb->input['action'] == "nextnewest")
 326  {
 327      $options = array(
 328          "limit_start" => 0,
 329          "limit" => 1,
 330          "order_by" => "lastpost"
 331      );
 332      $query = $db->simple_select('threads', '*', "fid={$thread['fid']} AND lastpost > {$thread['lastpost']} {$visibleonly} AND closed NOT LIKE 'moved|%'", $options);
 333      $nextthread = $db->fetch_array($query);
 334  
 335      // Are there actually next newest posts?
 336      if(!$nextthread['tid'])
 337      {
 338          error($lang->error_nonextnewest);
 339      }
 340      $options = array(
 341          "limit_start" => 0,
 342          "limit" => 1,
 343          "order_by" => "dateline DESC, pid DESC",
 344      );
 345      $query = $db->simple_select('posts', 'pid', "tid='{$nextthread['tid']}'", $options);
 346  
 347      // Redirect to the proper page.
 348      $pid = $db->fetch_field($query, "pid");
 349      header("Location: ".htmlspecialchars_decode(get_post_link($pid, $nextthread['tid']))."#pid{$pid}");
 350      exit;
 351  }
 352  
 353  // Jump to the next oldest posts.
 354  if($mybb->input['action'] == "nextoldest")
 355  {
 356      $options = array(
 357          "limit" => 1,
 358          "limit_start" => 0,
 359          "order_by" => "lastpost",
 360          "order_dir" => "desc"
 361      );
 362      $query = $db->simple_select("threads", "*", "fid=".$thread['fid']." AND lastpost < ".$thread['lastpost']." {$visibleonly} AND closed NOT LIKE 'moved|%'", $options);
 363      $nextthread = $db->fetch_array($query);
 364  
 365      // Are there actually next oldest posts?
 366      if(!$nextthread['tid'])
 367      {
 368          error($lang->error_nonextoldest);
 369      }
 370      $options = array(
 371          "limit_start" => 0,
 372          "limit" => 1,
 373          "order_by" => "dateline DESC, pid DESC",
 374      );
 375      $query = $db->simple_select("posts", "pid", "tid='".$nextthread['tid']."'", $options);
 376  
 377      // Redirect to the proper page.
 378      $pid = $db->fetch_field($query, "pid");
 379      header("Location: ".htmlspecialchars_decode(get_post_link($pid, $nextthread['tid']))."#pid{$pid}");
 380      exit;
 381  }
 382  
 383  $pid = $mybb->input['pid'] = $mybb->get_input('pid', MyBB::INPUT_INT);
 384  
 385  // Forumdisplay cache
 386  $forum_stats = $cache->read("forumsdisplay");
 387  
 388  $breadcrumb_multipage = array();
 389  if($mybb->settings['showforumpagesbreadcrumb'])
 390  {
 391      // How many pages are there?
 392      if(!$mybb->settings['threadsperpage'] || (int)$mybb->settings['threadsperpage'] < 1)
 393      {
 394          $mybb->settings['threadsperpage'] = 20;
 395      }
 396  
 397      $query = $db->simple_select("forums", "threads, unapprovedthreads, deletedthreads", "fid = '{$fid}'", array('limit' => 1));
 398      $forum_threads = $db->fetch_array($query);
 399      $threadcount = $forum_threads['threads'];
 400  
 401  
 402      if(is_moderator($fid, "canviewdeleted") == true || is_moderator($fid, "canviewunapprove") == true)
 403      {
 404          if(is_moderator($fid, "canviewdeleted") == true)
 405          {
 406              $threadcount += $forum_threads['deletedthreads'];
 407          }
 408          if(is_moderator($fid, "canviewunapprove") == true)
 409          {
 410              $threadcount += $forum_threads['unapprovedthreads'];
 411          }
 412      }
 413      elseif($forumpermissions['canviewdeletionnotice'] != 0)
 414      {
 415          $threadcount += $forum_threads['deletedthreads'];
 416      }
 417  
 418      // Limit to only our own threads
 419      $uid_only = '';
 420      if(isset($forumpermissions['canonlyviewownthreads']) && $forumpermissions['canonlyviewownthreads'] == 1)
 421      {
 422          $uid_only = " AND uid = '".$mybb->user['uid']."'";
 423  
 424          $query = $db->simple_select("threads", "COUNT(tid) AS threads", "fid = '$fid' $visibleonly $uid_only", array('limit' => 1));
 425          $threadcount = $db->fetch_field($query, "threads");
 426      }
 427  
 428      // If we have 0 threads double check there aren't any "moved" threads
 429      if($threadcount == 0)
 430      {
 431          $query = $db->simple_select("threads", "COUNT(tid) AS threads", "fid = '$fid' $visibleonly $uid_only", array('limit' => 1));
 432          $threadcount = $db->fetch_field($query, "threads");
 433      }
 434  
 435      $stickybit = " OR sticky=1";
 436      if($thread['sticky'] == 1)
 437      {
 438          $stickybit = " AND sticky=1";
 439      }
 440  
 441      // Figure out what page the thread is actually on
 442      switch($db->type)
 443      {
 444          case "pgsql":
 445              $query = $db->query("
 446                  SELECT COUNT(tid) as threads
 447                  FROM ".TABLE_PREFIX."threads
 448                  WHERE fid = '$fid' AND (lastpost >= '".(int)$thread['lastpost']."'{$stickybit}) {$visibleonly} {$uid_only}
 449                  GROUP BY lastpost
 450                  ORDER BY lastpost DESC
 451              ");
 452              break;
 453          default:
 454              $query = $db->simple_select("threads", "COUNT(tid) as threads", "fid = '$fid' AND (lastpost >= '".(int)$thread['lastpost']."'{$stickybit}) {$visibleonly} {$uid_only}", array('order_by' => 'lastpost', 'order_dir' => 'desc'));
 455      }
 456  
 457      $thread_position = $db->fetch_field($query, "threads");
 458      $thread_page = ceil(($thread_position/$mybb->settings['threadsperpage']));
 459  
 460      $breadcrumb_multipage = array(
 461          "num_threads" => $threadcount,
 462          "current_page" => $thread_page
 463      );
 464  }
 465  
 466  // Build the navigation.
 467  build_forum_breadcrumb($fid, $breadcrumb_multipage);
 468  add_breadcrumb($thread['displayprefix'].$thread['subject'], get_thread_link($thread['tid']));
 469  
 470  $plugins->run_hooks("showthread_start");
 471  
 472  // Show the entire thread (taking into account pagination).
 473  if($mybb->input['action'] == "thread")
 474  {
 475      // This is a workaround to fix threads which data may get "corrupted" due to lag or other still unknown reasons
 476      if($thread['firstpost'] == 0 || $thread['dateline'] == 0)
 477      {
 478          update_first_post($tid);
 479      }
 480  
 481      // Does this thread have a poll?
 482      if($thread['poll'])
 483      {
 484          $options = array(
 485              "limit" => 1
 486          );
 487          $query = $db->simple_select("polls", "*", "pid='".$thread['poll']."'", $options);
 488          $poll = $db->fetch_array($query);
 489          $poll['timeout'] = $poll['timeout']*60*60*24;
 490          $expiretime = $poll['dateline'] + $poll['timeout'];
 491          $now = TIME_NOW;
 492  
 493          // If the poll or the thread is closed or if the poll is expired, show the results.
 494          if($poll['closed'] == 1 || $thread['closed'] == 1 || ($expiretime < $now && $poll['timeout'] > 0) || $forumpermissions['canvotepolls'] != 1)
 495          {
 496              $showresults = 1;
 497          }
 498  
 499          if($forumpermissions['canvotepolls'] != 1)
 500          {
 501              $nopermission = 1;
 502          }
 503  
 504          // Check if the user has voted before...
 505          if($mybb->user['uid'])
 506          {
 507              $user_check = "uid='{$mybb->user['uid']}'";
 508          }
 509          else
 510          {
 511              $user_check = "uid='0' AND ipaddress=".$db->escape_binary($session->packedip);
 512          }
 513  
 514          $query = $db->simple_select("pollvotes", "*", "{$user_check} AND pid='".$poll['pid']."'");
 515          while($votecheck = $db->fetch_array($query))
 516          {
 517              $alreadyvoted = 1;
 518              $votedfor[$votecheck['voteoption']] = 1;
 519          }
 520  
 521          $optionsarray = explode("||~|~||", $poll['options']);
 522          $votesarray = explode("||~|~||", $poll['votes']);
 523          $poll['question'] = htmlspecialchars_uni($poll['question']);
 524          $polloptions = '';
 525          $totalvotes = 0;
 526          $poll['totvotes'] = 0;
 527  
 528          for($i = 1; $i <= $poll['numoptions']; ++$i)
 529          {
 530              $poll['totvotes'] = $poll['totvotes'] + $votesarray[$i-1];
 531          }
 532  
 533          // Loop through the poll options.
 534          for($i = 1; $i <= $poll['numoptions']; ++$i)
 535          {
 536              // Set up the parser options.
 537              $parser_options = array(
 538                  "allow_html" => $forum['allowhtml'],
 539                  "allow_mycode" => $forum['allowmycode'],
 540                  "allow_smilies" => $forum['allowsmilies'],
 541                  "allow_imgcode" => $forum['allowimgcode'],
 542                  "allow_videocode" => $forum['allowvideocode'],
 543                  "filter_badwords" => 1
 544              );
 545  
 546              if($mybb->user['uid'] != 0 && $mybb->user['showimages'] != 1 || $mybb->settings['guestimages'] != 1 && $mybb->user['uid'] == 0)
 547              {
 548                  $parser_options['allow_imgcode'] = 0;
 549              }
 550  
 551              if($mybb->user['uid'] != 0 && $mybb->user['showvideos'] != 1 || $mybb->settings['guestvideos'] != 1 && $mybb->user['uid'] == 0)
 552              {
 553                  $parser_options['allow_videocode'] = 0;
 554              }
 555  
 556              $option = $parser->parse_message($optionsarray[$i-1], $parser_options);
 557              $votes = $votesarray[$i-1];
 558              $totalvotes += $votes;
 559              $number = $i;
 560  
 561              // Mark the option the user voted for.
 562              if(!empty($votedfor[$number]))
 563              {
 564                  $optionbg = "trow2 poll_votedfor";
 565                  $votestar = "*";
 566              }
 567              else
 568              {
 569                  $optionbg = "trow1";
 570                  $votestar = "";
 571              }
 572  
 573              // If the user already voted or if the results need to be shown, do so; else show voting screen.
 574              if(isset($alreadyvoted) || isset($showresults))
 575              {
 576                  if((int)$votes == "0")
 577                  {
 578                      $percent = "0";
 579                  }
 580                  else
 581                  {
 582                      $percent = number_format($votes / $poll['totvotes'] * 100, 2);
 583                  }
 584                  $imagewidth = round($percent);
 585                  eval("\$polloptions .= \"".$templates->get("showthread_poll_resultbit")."\";");
 586              }
 587              else
 588              {
 589                  if($poll['multiple'] == 1)
 590                  {
 591                      eval("\$polloptions .= \"".$templates->get("showthread_poll_option_multiple")."\";");
 592                  }
 593                  else
 594                  {
 595                      eval("\$polloptions .= \"".$templates->get("showthread_poll_option")."\";");
 596                  }
 597              }
 598          }
 599  
 600          // If there are any votes at all, all votes together will be 100%; if there are no votes, all votes together will be 0%.
 601          if($poll['totvotes'])
 602          {
 603              $totpercent = "100%";
 604          }
 605          else
 606          {
 607              $totpercent = "0%";
 608          }
 609  
 610          // Check if user is allowed to edit posts; if so, show "edit poll" link.
 611          $edit_poll = '';
 612          if(is_moderator($fid, 'canmanagepolls'))
 613          {
 614              eval("\$edit_poll = \"".$templates->get("showthread_poll_editpoll")."\";");
 615          }
 616  
 617          // Decide what poll status to show depending on the status of the poll and whether or not the user voted already.
 618          if(isset($alreadyvoted) || isset($showresults) || isset($nopermission))
 619          {
 620              if($alreadyvoted)
 621              {
 622                  $pollstatus = $lang->already_voted;
 623  
 624                  $undovote = '';
 625                  if($mybb->usergroup['canundovotes'] == 1)
 626                  {
 627                      eval("\$undovote = \"".$templates->get("showthread_poll_undovote")."\";");
 628                  }
 629              }
 630              elseif($nopermission)
 631              {
 632                  $pollstatus = $lang->no_voting_permission;
 633              }
 634              else
 635              {
 636                  $pollstatus = $lang->poll_closed;
 637              }
 638  
 639              $lang->total_votes = $lang->sprintf($lang->total_votes, $totalvotes);
 640              eval("\$pollbox = \"".$templates->get("showthread_poll_results")."\";");
 641              $plugins->run_hooks("showthread_poll_results");
 642          }
 643          else
 644          {
 645              $closeon = '&nbsp;';
 646              if($poll['timeout'] != 0)
 647              {
 648                  $closeon = $lang->sprintf($lang->poll_closes, my_date($mybb->settings['dateformat'], $expiretime));
 649              }
 650  
 651              $publicnote = '&nbsp;';
 652              if($poll['public'] == 1)
 653              {
 654                  $publicnote = $lang->public_note;
 655              }
 656  
 657              eval("\$pollbox = \"".$templates->get("showthread_poll")."\";");
 658              $plugins->run_hooks("showthread_poll");
 659          }
 660  
 661      }
 662      else
 663      {
 664          $pollbox = "";
 665      }
 666  
 667      // Create the forum jump dropdown box.
 668      if($mybb->settings['enableforumjump'] != 0)
 669      {
 670          $forumjump = build_forum_jump("", $fid, 1);
 671      }
 672  
 673      // Fetch some links
 674      $next_oldest_link = get_thread_link($tid, 0, "nextoldest");
 675      $next_newest_link = get_thread_link($tid, 0, "nextnewest");
 676  
 677      // Mark this thread as read
 678      mark_thread_read($tid, $fid);
 679  
 680      // If the forum is not open, show closed newreply button unless the user is a moderator of this forum.
 681      $newthread = $newreply = '';
 682      if($forum['open'] != 0 && $forum['type'] == "f")
 683      {
 684          if($forumpermissions['canpostthreads'] != 0 && $mybb->user['suspendposting'] != 1)
 685          {
 686              eval("\$newthread = \"".$templates->get("showthread_newthread")."\";");
 687          }
 688  
 689          // Show the appropriate reply button if this thread is open or closed
 690          if($forumpermissions['canpostreplys'] != 0 && $mybb->user['suspendposting'] != 1 && ($thread['closed'] != 1 || is_moderator($fid, "canpostclosedthreads")) && ($thread['uid'] == $mybb->user['uid'] || empty($forumpermissions['canonlyreplyownthreads'])))
 691          {
 692              eval("\$newreply = \"".$templates->get("showthread_newreply")."\";");
 693          }
 694          elseif($thread['closed'] == 1)
 695          {
 696              eval("\$newreply = \"".$templates->get("showthread_newreply_closed")."\";");
 697          }
 698      }
 699  
 700      // Create the admin tools dropdown box.
 701      if($ismod == true)
 702      {
 703          $closeoption = $closelinkch = $stickch = '';
 704  
 705          if($thread['closed'] == 1)
 706          {
 707              $closelinkch = ' checked="checked"';
 708          }
 709  
 710          if($thread['sticky'])
 711          {
 712              $stickch = ' checked="checked"';
 713          }
 714  
 715          if(is_moderator($thread['fid'], "canopenclosethreads"))
 716          {
 717              eval("\$closeoption .= \"".$templates->get("showthread_quickreply_options_close")."\";");
 718          }
 719  
 720          if(is_moderator($thread['fid'], "canstickunstickthreads"))
 721          {
 722              eval("\$closeoption .= \"".$templates->get("showthread_quickreply_options_stick")."\";");
 723          }
 724  
 725          $inlinecount = "0";
 726          $inlinecookie = "inlinemod_thread".$tid;
 727  
 728          $plugins->run_hooks("showthread_ismod");
 729      }
 730      else
 731      {
 732          $modoptions = "&nbsp;";
 733          $inlinemod = $closeoption = '';
 734      }
 735  
 736      // Increment the thread view.
 737      if(
 738          (
 739              $mybb->user['uid'] == 0 &&
 740              (
 741                  ($session->is_spider == true && $mybb->settings['threadviews_countspiders'] == 1) ||
 742                  ($session->is_spider == false && $mybb->settings['threadviews_countguests'] == 1)
 743              )
 744          ) ||
 745          (
 746              $mybb->user['uid'] != 0 &&
 747              ($mybb->settings['threadviews_countthreadauthor'] == 1 || $mybb->user['uid'] != $thread['uid'])
 748          )
 749      )
 750      {
 751          if($mybb->settings['delayedthreadviews'] == 1)
 752          {
 753              $db->shutdown_query("INSERT INTO ".TABLE_PREFIX."threadviews (tid) VALUES('{$tid}')");
 754          }
 755          else
 756          {
 757              $db->shutdown_query("UPDATE ".TABLE_PREFIX."threads SET views=views+1 WHERE tid='{$tid}'");
 758          }
 759          ++$thread['views'];
 760      }
 761  
 762      // Work out the thread rating for this thread.
 763      $rating = $ratethread = '';
 764      if($mybb->settings['allowthreadratings'] != 0 && $forum['allowtratings'] != 0)
 765      {
 766          $rated = 0;
 767          $lang->load("ratethread");
 768          if($thread['numratings'] <= 0)
 769          {
 770              $thread['width'] = 0;
 771              $thread['averagerating'] = 0;
 772              $thread['numratings'] = 0;
 773          }
 774          else
 775          {
 776              $thread['averagerating'] = (float)round($thread['totalratings']/$thread['numratings'], 2);
 777              $thread['width'] = (int)round($thread['averagerating'])*20;
 778              $thread['numratings'] = (int)$thread['numratings'];
 779          }
 780  
 781          if($thread['numratings'])
 782          {
 783              // At least >someone< has rated this thread, was it me?
 784              // Check if we have already voted on this thread - it won't show hover effect then.
 785              $query = $db->simple_select("threadratings", "uid", "tid='{$tid}' AND uid='{$mybb->user['uid']}'");
 786              $rated = $db->fetch_field($query, 'uid');
 787          }
 788  
 789          $not_rated = '';
 790          if(!$rated)
 791          {
 792              $not_rated = ' star_rating_notrated';
 793          }
 794  
 795          $ratingvotesav = $lang->sprintf($lang->rating_average, $thread['numratings'], $thread['averagerating']);
 796          eval("\$ratethread = \"".$templates->get("showthread_ratethread")."\";");
 797      }
 798  
 799      // Can this user perform searches? If so, we can show them the "Search thread" form
 800      if($forumpermissions['cansearch'] != 0)
 801      {
 802          eval("\$search_thread = \"".$templates->get("showthread_search")."\";");
 803      }
 804  
 805      // Fetch the ignore list for the current user if they have one
 806      $ignored_users = array();
 807      if($mybb->user['uid'] > 0 && $mybb->user['ignorelist'] != "")
 808      {
 809          $ignore_list = explode(',', $mybb->user['ignorelist']);
 810          foreach($ignore_list as $uid)
 811          {
 812              $ignored_users[$uid] = 1;
 813          }
 814      }
 815  
 816      // Which thread mode is our user using by default?
 817      if(!empty($mybb->user['threadmode']))
 818      {
 819          $defaultmode = $mybb->user['threadmode'];
 820      }
 821      else if($mybb->settings['threadusenetstyle'] == 1)
 822      {
 823          $defaultmode = 'threaded';
 824      }
 825      else
 826      {
 827          $defaultmode = 'linear';
 828      }
 829  
 830      // If mode is unset, set the default mode
 831      if(!isset($mybb->input['mode']))
 832      {
 833          $mybb->input['mode'] = $defaultmode;
 834      }
 835  
 836      // Threaded or linear display?
 837      $threadexbox = '';
 838      if($mybb->get_input('mode') == 'threaded')
 839      {
 840          $thread_toggle = 'linear';
 841          $isfirst = 1;
 842  
 843          // Are we linked to a specific pid?
 844          if($mybb->input['pid'])
 845          {
 846              $where = "AND p.pid='".$mybb->input['pid']."'";
 847          }
 848          else
 849          {
 850              $where = " ORDER BY dateline, pid LIMIT 0, 1";
 851          }
 852          $query = $db->query("
 853              SELECT u.*, u.username AS userusername, p.*, f.*, r.reporters, eu.username AS editusername
 854              FROM ".TABLE_PREFIX."posts p
 855              LEFT JOIN ".TABLE_PREFIX."reportedcontent r ON (r.id=p.pid AND r.type='post' AND r.reportstatus != 1)
 856              LEFT JOIN ".TABLE_PREFIX."users u ON (u.uid=p.uid)
 857              LEFT JOIN ".TABLE_PREFIX."userfields f ON (f.ufid=u.uid)
 858              LEFT JOIN ".TABLE_PREFIX."users eu ON (eu.uid=p.edituid)
 859              WHERE p.tid='$tid' $visibleonly_p $where
 860          ");
 861          $showpost = $db->fetch_array($query);
 862  
 863          // Choose what pid to display.
 864          if(!$mybb->input['pid'])
 865          {
 866              $mybb->input['pid'] = $showpost['pid'];
 867          }
 868  
 869          // Is there actually a pid to display?
 870          if(!$showpost['pid'])
 871          {
 872              error($lang->error_invalidpost);
 873          }
 874  
 875          $attachcache = array();
 876          if($mybb->settings['enableattachments'] == 1 && $thread['attachmentcount'] > 0 || is_moderator($fid, 'caneditposts'))
 877          {
 878              // Get the attachments for this post.
 879              $query = $db->simple_select("attachments", "*", "pid=".$mybb->input['pid']);
 880              while($attachment = $db->fetch_array($query))
 881              {
 882                  $attachcache[$attachment['pid']][$attachment['aid']] = $attachment;
 883              }
 884          }
 885  
 886          $multipage = '';
 887  
 888          // Build the threaded post display tree.
 889          $query = $db->query("
 890              SELECT p.username, p.uid, p.pid, p.replyto, p.subject, p.dateline
 891              FROM ".TABLE_PREFIX."posts p
 892              WHERE p.tid='$tid'
 893              $visibleonly_p
 894              ORDER BY p.dateline, p.pid
 895          ");
 896          $postsdone = array();
 897          while($post = $db->fetch_array($query))
 898          {
 899              if(empty($postsdone[$post['pid']]))
 900              {
 901                  if($post['pid'] == $mybb->input['pid'] || ($isfirst && !$mybb->input['pid']))
 902                  {
 903                      $postcounter = count($postsdone);
 904                      $isfirst = 0;
 905                  }
 906                  $tree[$post['replyto']][$post['pid']] = $post;
 907                  $postsdone[$post['pid']] = 1;
 908              }
 909          }
 910  
 911          $threadedbits = buildtree();
 912          $posts = build_postbit($showpost);
 913          eval("\$threadexbox = \"".$templates->get("showthread_threadedbox")."\";");
 914          $plugins->run_hooks("showthread_threaded");
 915      }
 916      else // Linear display
 917      {
 918          $thread_toggle = 'threaded';
 919          $threadexbox = '';
 920          if(!$mybb->settings['postsperpage'] || (int)$mybb->settings['postsperpage'] < 1)
 921          {
 922              $mybb->settings['postsperpage'] = 20;
 923          }
 924  
 925          // Figure out if we need to display multiple pages.
 926          $page = 1;
 927          $perpage = $mybb->settings['postsperpage'];
 928          if($mybb->get_input('page', MyBB::INPUT_INT) && $mybb->get_input('page') != "last")
 929          {
 930              $page = $mybb->get_input('page', MyBB::INPUT_INT);
 931          }
 932  
 933          if(!empty($mybb->input['pid']))
 934          {
 935              $post = get_post($mybb->input['pid']);
 936              if(
 937                  empty($post) ||
 938                  (
 939                      $post['visible'] == 0 && !(
 940                          is_moderator($post['fid'], 'canviewunapprove') ||
 941                          ($mybb->user['uid'] && $post['uid'] == $mybb->user['uid'] && $mybb->settings['showownunapproved'])
 942                      )
 943                  ) ||
 944                  ($post['visible'] == -1 && !is_moderator($post['fid'], 'canviewdeleted') && $forumpermissions['canviewdeletionnotice'] == 0)
 945              )
 946              {
 947                  $footer .= '<script type="text/javascript">$(function() { $.jGrowl(\''.$lang->error_invalidpost.'\', {theme: \'jgrowl_error\'}); });</script>';
 948              }
 949              else
 950              {
 951                  $query = $db->query("
 952                      SELECT COUNT(p.dateline) AS count FROM ".TABLE_PREFIX."posts p
 953                      WHERE p.tid = '{$tid}'
 954                      AND p.dateline <= '{$post['dateline']}'
 955                      {$visibleonly_p}
 956                  ");
 957                  $result = $db->fetch_field($query, "count");
 958                  if(($result % $perpage) == 0)
 959                  {
 960                      $page = $result / $perpage;
 961                  }
 962                  else
 963                  {
 964                      $page = (int)($result / $perpage) + 1;
 965                  }
 966              }
 967          }
 968  
 969          // Recount replies if user is a moderator or can see the deletion notice to take into account unapproved/deleted posts.
 970          if($visible_states != array("1"))
 971          {
 972              $cached_replies = $thread['replies']+$thread['unapprovedposts']+$thread['deletedposts'];
 973  
 974              $query = $db->simple_select("posts p", "COUNT(*) AS replies", "p.tid='$tid' $visibleonly_p");
 975              $thread['replies'] = $db->fetch_field($query, 'replies')-1;
 976  
 977              if(in_array('-1', $visible_states) && in_array('0', $visible_states))
 978              {
 979                  // The counters are wrong? Rebuild them
 980                  // This doesn't cover all cases however it is a good addition to the manual rebuild function
 981                  if($thread['replies'] != $cached_replies)
 982                  {
 983                      require_once  MYBB_ROOT."/inc/functions_rebuild.php";
 984                      rebuild_thread_counters($thread['tid']);
 985                  }
 986              }
 987          }
 988  
 989          $postcount = (int)$thread['replies']+1;
 990          $pages = $postcount / $perpage;
 991          $pages = ceil($pages);
 992  
 993          if($mybb->get_input('page') == "last")
 994          {
 995              $page = $pages;
 996          }
 997  
 998          if($page > $pages || $page <= 0)
 999          {
1000              $page = 1;
1001          }
1002  
1003          if($page)
1004          {
1005              $start = ($page-1) * $perpage;
1006          }
1007          else
1008          {
1009              $start = 0;
1010              $page = 1;
1011          }
1012          $upper = $start+$perpage;
1013  
1014          // Work out if we have terms to highlight
1015          $highlight = "";
1016          $threadmode = "";
1017          if($mybb->seo_support == true)
1018          {
1019              if($mybb->get_input('highlight'))
1020              {
1021                  $highlight = "?highlight=".urlencode($mybb->get_input('highlight'));
1022              }
1023  
1024              if($defaultmode != "linear")
1025              {
1026                  if($mybb->get_input('highlight'))
1027                  {
1028                      $threadmode = "&amp;mode=linear";
1029                  }
1030                  else
1031                  {
1032                      $threadmode = "?mode=linear";
1033                  }
1034              }
1035          }
1036          else
1037          {
1038              if(!empty($mybb->input['highlight']))
1039              {
1040                  if(is_array($mybb->input['highlight']))
1041                  {
1042                      foreach($mybb->input['highlight'] as $highlight_word)
1043                      {
1044                          $highlight .= "&amp;highlight[]=".urlencode($highlight_word);
1045                      }
1046                  }
1047                  else
1048                  {
1049                      $highlight = "&amp;highlight=".urlencode($mybb->get_input('highlight'));
1050                  }
1051              }
1052  
1053              if($defaultmode != "linear")
1054              {
1055                  $threadmode = "&amp;mode=linear";
1056              }
1057          }
1058  
1059          $multipage = multipage($postcount, $perpage, $page, str_replace("{tid}", $tid, THREAD_URL_PAGED.$highlight.$threadmode));
1060  
1061          // Lets get the pids of the posts on this page.
1062          $pids = "";
1063          $comma = '';
1064          $query = $db->simple_select("posts p", "p.pid", "p.tid='$tid' $visibleonly_p", array('order_by' => 'p.dateline, p.pid', 'limit_start' => $start, 'limit' => $perpage));
1065          while($getid = $db->fetch_array($query))
1066          {
1067              // Set the ID of the first post on page to $pid if it doesn't hold any value
1068              // to allow this value to be used for Thread Mode/Linear Mode links
1069              // and ensure the user lands on the correct page after changing view mode
1070              if(empty($pid))
1071              {
1072                  $pid = $getid['pid'];
1073              }
1074              // Gather a comma separated list of post IDs
1075              $pids .= "$comma'{$getid['pid']}'";
1076              $comma = ",";
1077          }
1078          if($pids)
1079          {
1080              $pids = "pid IN($pids)";
1081  
1082              $attachcache = array();
1083              if($mybb->settings['enableattachments'] == 1 && $thread['attachmentcount'] > 0 || is_moderator($fid, 'caneditposts'))
1084              {
1085                  // Now lets fetch all of the attachments for these posts.
1086                  $query = $db->simple_select("attachments", "*", $pids);
1087                  while($attachment = $db->fetch_array($query))
1088                  {
1089                      $attachcache[$attachment['pid']][$attachment['aid']] = $attachment;
1090                  }
1091              }
1092          }
1093          else
1094          {
1095              // If there are no pid's the thread is probably awaiting approval.
1096              error($lang->error_invalidthread);
1097          }
1098  
1099          // Get the actual posts from the database here.
1100          $posts = '';
1101          $query = $db->query("
1102              SELECT u.*, u.username AS userusername, p.*, f.*, r.reporters, eu.username AS editusername
1103              FROM ".TABLE_PREFIX."posts p
1104              LEFT JOIN ".TABLE_PREFIX."reportedcontent r ON (r.id=p.pid AND r.type='post' AND r.reportstatus != 1)
1105              LEFT JOIN ".TABLE_PREFIX."users u ON (u.uid=p.uid)
1106              LEFT JOIN ".TABLE_PREFIX."userfields f ON (f.ufid=u.uid)
1107              LEFT JOIN ".TABLE_PREFIX."users eu ON (eu.uid=p.edituid)
1108              WHERE $pids
1109              ORDER BY p.dateline, p.pid
1110          ");
1111          while($post = $db->fetch_array($query))
1112          {
1113              if($thread['firstpost'] == $post['pid'] && $thread['visible'] == 0)
1114              {
1115                  $post['visible'] = 0;
1116              }
1117              $posts .= build_postbit($post);
1118              $post = '';
1119          }
1120          $plugins->run_hooks("showthread_linear");
1121      }
1122      $lang->thread_toggle = $lang->{$thread_toggle};
1123  
1124      // Show the similar threads table if wanted.
1125      $similarthreads = '';
1126      if($mybb->settings['showsimilarthreads'] != 0)
1127      {
1128          $own_perm = '';
1129          if($forumpermissions['canonlyviewownthreads'] == 1)
1130          {
1131              $own_perm = " AND t.uid={$mybb->user['uid']}";
1132          }
1133  
1134          switch($db->type)
1135          {
1136              case "pgsql":
1137                  $query = $db->query("
1138                      SELECT t.*, t.username AS threadusername, u.username
1139                      FROM ".TABLE_PREFIX."threads t
1140                      LEFT JOIN ".TABLE_PREFIX."users u ON (u.uid = t.uid), plainto_tsquery ('".$db->escape_string($thread['subject'])."') AS query
1141                      WHERE t.fid='{$thread['fid']}' AND t.tid!='{$thread['tid']}' AND t.visible='1' AND t.closed NOT LIKE 'moved|%' AND t.subject @@ query{$own_perm}
1142                      ORDER BY t.lastpost DESC
1143                      OFFSET 0 LIMIT {$mybb->settings['similarlimit']}
1144                  ");
1145                  break;
1146              default:
1147                  $query = $db->query("
1148                      SELECT t.*, t.username AS threadusername, u.username, MATCH (t.subject) AGAINST ('".$db->escape_string($thread['subject'])."') AS relevance
1149                      FROM ".TABLE_PREFIX."threads t
1150                      LEFT JOIN ".TABLE_PREFIX."users u ON (u.uid = t.uid)
1151                      WHERE t.fid='{$thread['fid']}' AND t.tid!='{$thread['tid']}' AND t.visible='1' AND t.closed NOT LIKE 'moved|%'{$own_perm} AND MATCH (t.subject) AGAINST ('".$db->escape_string($thread['subject'])."') >= '{$mybb->settings['similarityrating']}'
1152                      ORDER BY t.lastpost DESC
1153                      LIMIT 0, {$mybb->settings['similarlimit']}
1154                  ");
1155          }
1156  
1157          $count = 0;
1158          $similarthreadbits = '';
1159          $icon_cache = $cache->read("posticons");
1160          while($similar_thread = $db->fetch_array($query))
1161          {
1162              ++$count;
1163              $trow = alt_trow();
1164              if($similar_thread['icon'] > 0 && $icon_cache[$similar_thread['icon']])
1165              {
1166                  $icon = $icon_cache[$similar_thread['icon']];
1167                  $icon['path'] = str_replace("{theme}", $theme['imgdir'], $icon['path']);
1168                  $icon['path'] = htmlspecialchars_uni($icon['path']);
1169                  $icon['name'] = htmlspecialchars_uni($icon['name']);
1170                  eval("\$icon = \"".$templates->get("forumdisplay_thread_icon")."\";");
1171              }
1172              else
1173              {
1174                  $icon = "&nbsp;";
1175              }
1176              if(!$similar_thread['username'])
1177              {
1178                  $similar_thread['username'] = $similar_thread['profilelink'] = htmlspecialchars_uni($similar_thread['threadusername']);
1179              }
1180              else
1181              {
1182                  $similar_thread['username'] = htmlspecialchars_uni($similar_thread['username']);
1183                  $similar_thread['profilelink'] = build_profile_link($similar_thread['username'], $similar_thread['uid']);
1184              }
1185  
1186              // If this thread has a prefix, insert a space between prefix and subject
1187              if($similar_thread['prefix'] != 0)
1188              {
1189                  $prefix = build_prefixes($similar_thread['prefix']);
1190                  if(!empty($prefix))
1191                  {
1192                      $similar_thread['threadprefix'] = $prefix['displaystyle'].'&nbsp;';
1193                  }
1194              }
1195  
1196              $similar_thread['subject'] = $parser->parse_badwords($similar_thread['subject']);
1197              $similar_thread['subject'] = htmlspecialchars_uni($similar_thread['subject']);
1198              $similar_thread['threadlink'] = get_thread_link($similar_thread['tid']);
1199              $similar_thread['lastpostlink'] = get_thread_link($similar_thread['tid'], 0, "lastpost");
1200  
1201              $lastpostdate = my_date('relative', $similar_thread['lastpost']);
1202              $lastposter = $similar_thread['lastposter'];
1203              $lastposteruid = $similar_thread['lastposteruid'];
1204  
1205              // Don't link to guest's profiles (they have no profile).
1206              if($lastposteruid == 0)
1207              {
1208                  $lastposterlink = $lastposter;
1209              }
1210              else
1211              {
1212                  $lastposterlink = build_profile_link($lastposter, $lastposteruid);
1213              }
1214              $similar_thread['replies'] = my_number_format($similar_thread['replies']);
1215              $similar_thread['views'] = my_number_format($similar_thread['views']);
1216              eval("\$similarthreadbits .= \"".$templates->get("showthread_similarthreads_bit")."\";");
1217          }
1218          if($count)
1219          {
1220              eval("\$similarthreads = \"".$templates->get("showthread_similarthreads")."\";");
1221          }
1222      }
1223  
1224      // Decide whether or not to show quick reply.
1225      $quickreply = '';
1226      if($forumpermissions['canpostreplys'] != 0 && $mybb->user['suspendposting'] != 1 && ($thread['closed'] != 1 || is_moderator($fid, "canpostclosedthreads")) && $mybb->settings['quickreply'] != 0 && $mybb->user['showquickreply'] != '0' && $forum['open'] != 0 && ($thread['uid'] == $mybb->user['uid'] || empty($forumpermissions['canonlyreplyownthreads'])))
1227      {
1228          $query = $db->simple_select("posts", "pid", "tid='{$tid}'", array("order_by" => "pid", "order_dir" => "desc", "limit" => 1));
1229          $last_pid = $db->fetch_field($query, "pid");
1230  
1231          // Show captcha image for guests if enabled
1232          $captcha = '';
1233          if($mybb->settings['captchaimage'] && !$mybb->user['uid'])
1234          {
1235              require_once  MYBB_ROOT.'inc/class_captcha.php';
1236              $post_captcha = new captcha(true, "post_captcha");
1237  
1238              if($post_captcha->html)
1239              {
1240                  $captcha = $post_captcha->html;
1241              }
1242          }
1243  
1244          $postoptionschecked = array('signature' => '', 'emailnotify' => '');
1245          if($mybb->user['signature'])
1246          {
1247              $postoptionschecked['signature'] = 'checked="checked"';
1248          }
1249  
1250          // Hide signature option if no permission
1251          $option_signature = '';
1252          if($mybb->usergroup['canusesig'] && !$mybb->user['suspendsignature'])
1253          {
1254              eval("\$option_signature = \"".$templates->get('showthread_quickreply_options_signature')."\";");
1255          }
1256  
1257          if(isset($mybb->user['emailnotify']) && $mybb->user['emailnotify'] == 1)
1258          {
1259              $postoptionschecked['emailnotify'] = 'checked="checked"';
1260          }
1261  
1262          $trow = alt_trow();
1263          if($thread['closed'] == 1)
1264          {
1265              $trow = 'trow_shaded';
1266          }
1267  
1268          $moderation_notice = '';
1269          if(!is_moderator($forum['fid'], "canapproveunapproveposts"))
1270          {
1271              if($forumpermissions['modposts'] == 1)
1272              {
1273                  $moderation_text = $lang->moderation_forum_posts;
1274                  eval('$moderation_notice = "'.$templates->get('global_moderation_notice').'";');
1275              }
1276  
1277              if($mybb->user['moderateposts'] == 1)
1278              {
1279                  $moderation_text = $lang->moderation_user_posts;
1280                  eval('$moderation_notice = "'.$templates->get('global_moderation_notice').'";');
1281              }
1282          }
1283  
1284              $posthash = md5($mybb->user['uid'].random_str());
1285  
1286          if(!isset($collapsedthead['quickreply']))
1287          {
1288              $collapsedthead['quickreply'] = '';
1289          }
1290          if(!isset($collapsedimg['quickreply']))
1291          {
1292              $collapsedimg['quickreply'] = '';
1293          }
1294          if(!isset($collapsed['quickreply_e']))
1295          {
1296              $collapsed['quickreply_e'] = '';
1297          }
1298  
1299          $expaltext = (in_array("quickreply", $collapse)) ? $lang->expcol_expand : $lang->expcol_collapse;
1300          eval("\$quickreply = \"".$templates->get("showthread_quickreply")."\";");
1301      }
1302  
1303      $moderationoptions = '';
1304      $threadnotesbox = $viewnotes = '';
1305  
1306      // If the user is a moderator, show the moderation tools.
1307      if($ismod)
1308      {
1309          $customthreadtools = $customposttools = $standardthreadtools = $standardposttools = '';
1310  
1311          if(!empty($thread['notes']))
1312          {
1313              $thread['notes'] = nl2br(htmlspecialchars_uni($thread['notes']));
1314  
1315              if(strlen($thread['notes']) > 200)
1316              {
1317                  eval("\$viewnotes = \"".$templates->get("showthread_threadnotes_viewnotes")."\";");
1318                  $thread['notes'] = my_substr($thread['notes'], 0, 200)."... {$viewnotes}";
1319              }
1320  
1321              $expaltext = (in_array("threadnotes", $collapse)) ? $lang->expcol_expand : $lang->expcol_collapse;
1322              eval("\$threadnotesbox = \"".$templates->get("showthread_threadnotes")."\";");
1323          }
1324  
1325          if(is_moderator($forum['fid'], "canusecustomtools") && (!empty($forum_stats[-1]['modtools']) || !empty($forum_stats[$forum['fid']]['modtools'])))
1326          {
1327              $gids = explode(',', $mybb->user['additionalgroups']);
1328              $gids[] = $mybb->user['usergroup'];
1329              $gids = array_filter(array_unique($gids));
1330              $gidswhere = '';
1331              switch($db->type)
1332              {
1333                  case "pgsql":
1334                  case "sqlite":
1335                      foreach($gids as $gid)
1336                      {
1337                          $gid = (int)$gid;
1338                          $gidswhere .= " OR ','||groups||',' LIKE '%,{$gid},%'";
1339                      }
1340                      $query = $db->simple_select("modtools", 'tid, name, type', "(','||forums||',' LIKE '%,$fid,%' OR ','||forums||',' LIKE '%,-1,%' OR forums='') AND (groups='' OR ','||groups||',' LIKE '%,-1,%'{$gidswhere})");
1341                      break;
1342                  default:
1343                      foreach($gids as $gid)
1344                      {
1345                          $gid = (int)$gid;
1346                          $gidswhere .= " OR CONCAT(',',`groups`,',') LIKE '%,{$gid},%'";
1347                      }
1348                      $query = $db->simple_select("modtools", 'tid, name, type', "(CONCAT(',',forums,',') LIKE '%,$fid,%' OR CONCAT(',',forums,',') LIKE '%,-1,%' OR forums='') AND (`groups`='' OR CONCAT(',',`groups`,',') LIKE '%,-1,%'{$gidswhere})");
1349                      break;
1350              }
1351  
1352              while($tool = $db->fetch_array($query))
1353              {
1354                  $tool['name'] = htmlspecialchars_uni($tool['name']);
1355                  if($tool['type'] == 'p')
1356                  {
1357                      eval("\$customposttools .= \"".$templates->get("showthread_inlinemoderation_custom_tool")."\";");
1358                  }
1359                  else
1360                  {
1361                      eval("\$customthreadtools .= \"".$templates->get("showthread_moderationoptions_custom_tool")."\";");
1362                  }
1363              }
1364  
1365              // Build inline moderation dropdown
1366              if(!empty($customposttools))
1367              {
1368                  eval("\$customposttools = \"".$templates->get("showthread_inlinemoderation_custom")."\";");
1369              }
1370          }
1371  
1372          $inlinemodsoftdelete = $inlinemodrestore = $inlinemoddelete = $inlinemodmanage = $inlinemodapprove = '';
1373  
1374          if(is_moderator($forum['fid'], "cansoftdeleteposts"))
1375          {
1376              eval("\$inlinemodsoftdelete = \"".$templates->get("showthread_inlinemoderation_softdelete")."\";");
1377          }
1378  
1379          if(is_moderator($forum['fid'], "canrestoreposts"))
1380          {
1381              eval("\$inlinemodrestore = \"".$templates->get("showthread_inlinemoderation_restore")."\";");
1382          }
1383  
1384          if(is_moderator($forum['fid'], "candeleteposts"))
1385          {
1386              eval("\$inlinemoddelete = \"".$templates->get("showthread_inlinemoderation_delete")."\";");
1387          }
1388  
1389          if(is_moderator($forum['fid'], "canmanagethreads"))
1390          {
1391              eval("\$inlinemodmanage = \"".$templates->get("showthread_inlinemoderation_manage")."\";");
1392          }
1393  
1394          if(is_moderator($forum['fid'], "canapproveunapproveposts"))
1395          {
1396              eval("\$inlinemodapprove = \"".$templates->get("showthread_inlinemoderation_approve")."\";");
1397          }
1398  
1399          if(!empty($inlinemodsoftdelete) || !empty($inlinemodrestore) || !empty($inlinemoddelete) || !empty($inlinemodmanage) || !empty($inlinemodapprove))
1400          {
1401              eval("\$standardposttools = \"".$templates->get("showthread_inlinemoderation_standard")."\";");
1402          }
1403  
1404          // Only show inline mod menu if there's options to show
1405          if(!empty($standardposttools) || !empty($customposttools))
1406          {
1407              eval("\$inlinemod = \"".$templates->get("showthread_inlinemoderation")."\";");
1408          }
1409  
1410          // Build thread moderation dropdown
1411          if(!empty($customthreadtools))
1412          {
1413              eval("\$customthreadtools = \"".$templates->get("showthread_moderationoptions_custom")."\";");
1414          }
1415  
1416          $openclosethread = $stickunstickthread = $deletethread = $threadnotes = $managethread = $adminpolloptions = $approveunapprovethread = $softdeletethread = '';
1417  
1418          if(is_moderator($forum['fid'], "canopenclosethreads"))
1419          {
1420              eval("\$openclosethread = \"".$templates->get("showthread_moderationoptions_openclose")."\";");
1421          }
1422  
1423          if(is_moderator($forum['fid'], "canstickunstickthreads"))
1424          {
1425              eval("\$stickunstickthread = \"".$templates->get("showthread_moderationoptions_stickunstick")."\";");
1426          }
1427  
1428          if(is_moderator($forum['fid'], "candeletethreads"))
1429          {
1430              eval("\$deletethread = \"".$templates->get("showthread_moderationoptions_delete")."\";");
1431          }
1432  
1433          if(is_moderator($forum['fid'], "canmanagethreads"))
1434          {
1435              eval("\$threadnotes = \"".$templates->get("showthread_moderationoptions_threadnotes")."\";");
1436              eval("\$managethread = \"".$templates->get("showthread_moderationoptions_manage")."\";");
1437          }
1438  
1439          if($pollbox && is_moderator($forum['fid'], "canmanagepolls"))
1440          {
1441              eval("\$adminpolloptions = \"".$templates->get("showthread_moderationoptions_deletepoll")."\";");
1442          }
1443  
1444          if(is_moderator($forum['fid'], "canapproveunapprovethreads"))
1445          {
1446              if($thread['visible'] == 0)
1447              {
1448                  eval("\$approveunapprovethread = \"".$templates->get("showthread_moderationoptions_approve")."\";");
1449              }
1450              else
1451              {
1452                  eval("\$approveunapprovethread = \"".$templates->get("showthread_moderationoptions_unapprove")."\";");
1453              }
1454          }
1455  
1456          if(is_moderator($forum['fid'], "cansoftdeletethreads") && $thread['visible'] != -1)
1457          {
1458              eval("\$softdeletethread = \"".$templates->get("showthread_moderationoptions_softdelete")."\";");
1459          }
1460          elseif(is_moderator($forum['fid'], "canrestorethreads") && $thread['visible'] == -1)
1461          {
1462              eval("\$softdeletethread = \"".$templates->get("showthread_moderationoptions_restore")."\";");
1463          }
1464  
1465          if(!empty($openclosethread) || !empty($stickunstickthread) || !empty($deletethread) || !empty($managethread) || !empty($adminpolloptions) || !empty($approveunapprovethread) || !empty($softdeletethread))
1466          {
1467              eval("\$standardthreadtools = \"".$templates->get("showthread_moderationoptions_standard")."\";");
1468          }
1469  
1470          // Only show mod menu if there's any options to show
1471          if(!empty($standardthreadtools) || !empty($customthreadtools))
1472          {
1473              eval("\$moderationoptions = \"".$templates->get("showthread_moderationoptions")."\";");
1474          }
1475      }
1476  
1477      eval("\$printthread = \"".$templates->get("showthread_printthread")."\";");
1478  
1479      // Display 'send thread' link if permissions allow
1480      $sendthread = '';
1481      if($mybb->usergroup['cansendemail'] == 1)
1482      {
1483          eval("\$sendthread = \"".$templates->get("showthread_send_thread")."\";");
1484      }
1485  
1486      // Display 'add poll' link to thread creator (or mods) if thread doesn't have a poll already
1487      $addpoll = '';
1488      $time = TIME_NOW;
1489      if(!$thread['poll'] && ($thread['uid'] == $mybb->user['uid'] || $ismod == true) && $forumpermissions['canpostpolls'] == 1 && $forum['open'] != 0 && $thread['closed'] != 1 && ($ismod == true || $thread['dateline'] > ($time-($mybb->settings['polltimelimit']*60*60)) || $mybb->settings['polltimelimit'] == 0))
1490      {
1491          eval("\$addpoll = \"".$templates->get("showthread_add_poll")."\";");
1492      }
1493  
1494      // Subscription status
1495      $add_remove_subscription = 'add';
1496      $add_remove_subscription_text = $lang->subscribe_thread;
1497  
1498      if($mybb->user['uid'])
1499      {
1500          $query = $db->simple_select("threadsubscriptions", "tid", "tid='".(int)$tid."' AND uid='".(int)$mybb->user['uid']."'", array('limit' => 1));
1501  
1502          if($db->num_rows($query) > 0)
1503          {
1504              $add_remove_subscription = 'remove';
1505              $add_remove_subscription_text = $lang->unsubscribe_thread;
1506          }
1507  
1508          eval("\$addremovesubscription = \"".$templates->get("showthread_subscription")."\";");
1509      }
1510      else
1511      {
1512          $addremovesubscription = '';
1513      }
1514  
1515      $classic_header = '';
1516      if($mybb->settings['postlayout'] == "classic")
1517      {
1518          eval("\$classic_header = \"".$templates->get("showthread_classic_header")."\";");
1519      }
1520  
1521      // Get users viewing this thread
1522      if($mybb->settings['browsingthisthread'] != 0)
1523      {
1524          $timecut = TIME_NOW - $mybb->settings['wolcutoff'];
1525  
1526          $comma = '';
1527          $guestcount = 0;
1528          $membercount = 0;
1529          $inviscount = 0;
1530          $onlinemembers = '';
1531          $doneusers = array();
1532  
1533          $query = $db->simple_select("sessions", "COUNT(DISTINCT ip) AS guestcount", "uid = 0 AND time > $timecut AND location2 = $tid AND nopermission != 1");
1534          $guestcount = $db->fetch_field($query, 'guestcount');
1535  
1536          $query = $db->query("
1537              SELECT
1538                  s.ip, s.uid, s.time, u.username, u.invisible, u.usergroup, u.displaygroup
1539              FROM
1540                  ".TABLE_PREFIX."sessions s
1541                  LEFT JOIN ".TABLE_PREFIX."users u ON (s.uid=u.uid)
1542              WHERE s.uid != 0 AND s.time > '$timecut' AND location2='$tid' AND nopermission != 1
1543              ORDER BY u.username ASC, s.time DESC
1544          ");
1545  
1546          while($user = $db->fetch_array($query))
1547          {
1548              if(empty($doneusers[$user['uid']]) || $doneusers[$user['uid']] < $user['time'])
1549              {
1550                  ++$membercount;
1551                  $doneusers[$user['uid']] = $user['time'];
1552  
1553                  $invisiblemark = '';
1554                  if($user['invisible'] == 1 && $mybb->usergroup['canbeinvisible'] == 1)
1555                  {
1556                      $invisiblemark = "*";
1557                      ++$inviscount;
1558                  }
1559  
1560                  if($user['invisible'] != 1 || $mybb->usergroup['canviewwolinvis'] == 1 || $user['uid'] == $mybb->user['uid'])
1561                  {
1562                      $user['profilelink'] = get_profile_link($user['uid']);
1563                      $user['username'] = format_name(htmlspecialchars_uni($user['username']), $user['usergroup'], $user['displaygroup']);
1564                      $user['reading'] = my_date($mybb->settings['timeformat'], $user['time']);
1565  
1566                      eval("\$onlinemembers .= \"".$templates->get("showthread_usersbrowsing_user", 1, 0)."\";");
1567                      $comma = $lang->comma;
1568                  }
1569              }
1570          }
1571  
1572          $guestsonline = '';
1573          if($guestcount)
1574          {
1575              $guestsonline = $lang->sprintf($lang->users_browsing_thread_guests, $guestcount);
1576          }
1577  
1578          $invisonline = '';
1579          if($mybb->user['invisible'] == 1)
1580          {
1581              // the user was counted as invisible user --> correct the inviscount
1582              $inviscount -= 1;
1583          }
1584          if($inviscount && $mybb->usergroup['canviewwolinvis'] != 1)
1585          {
1586              $invisonline = $lang->sprintf($lang->users_browsing_thread_invis, $inviscount);
1587          }
1588  
1589          $onlinesep = '';
1590          if($invisonline != '' && $onlinemembers)
1591          {
1592              $onlinesep = $lang->comma;
1593          }
1594  
1595          $onlinesep2 = '';
1596          if($invisonline != '' && $guestcount || $onlinemembers && $guestcount)
1597          {
1598              $onlinesep2 = $lang->comma;
1599          }
1600  
1601          eval("\$usersbrowsing = \"".$templates->get("showthread_usersbrowsing")."\";");
1602      }
1603  
1604      $thread_deleted = 0;
1605      if($thread['visible'] == -1)
1606      {
1607          $thread_deleted = 1;
1608      }
1609  
1610      $plugins->run_hooks("showthread_end");
1611  
1612      eval("\$showthread = \"".$templates->get("showthread")."\";");
1613      output_page($showthread);
1614  }
1615  
1616  /**
1617   * Build a navigation tree for threaded display.
1618   *
1619   * @param int $replyto
1620   * @param int $indent
1621   * @return string
1622   */
1623  function buildtree($replyto=0, $indent=0)
1624  {
1625      global $tree, $mybb, $theme, $mybb, $pid, $tid, $templates, $parser, $lang;
1626  
1627      $indentsize = 13 * $indent;
1628  
1629      ++$indent;
1630      $posts = '';
1631      if(is_array($tree[$replyto]))
1632      {
1633          foreach($tree[$replyto] as $key => $post)
1634          {
1635              $postdate = my_date('relative', $post['dateline']);
1636              $post['subject'] = htmlspecialchars_uni($parser->parse_badwords($post['subject']));
1637  
1638              if(!$post['subject'])
1639              {
1640                  $post['subject'] = "[".$lang->no_subject."]";
1641              }
1642  
1643              $post['username'] = htmlspecialchars_uni($post['username']);
1644              $post['profilelink'] = build_profile_link($post['username'], $post['uid']);
1645  
1646              if($mybb->input['pid'] == $post['pid'])
1647              {
1648                  eval("\$posts .= \"".$templates->get("showthread_threaded_bitactive")."\";");
1649              }
1650              else
1651              {
1652                  eval("\$posts .= \"".$templates->get("showthread_threaded_bit")."\";");
1653              }
1654  
1655              if(!empty($tree[$post['pid']]))
1656              {
1657                  $posts .= buildtree($post['pid'], $indent);
1658              }
1659          }
1660          --$indent;
1661      }
1662      return $posts;
1663  }


2005 - 2021 © MyBB.de | Alle Rechte vorbehalten! | Sponsor: netcup Cross-referenced by PHPXref