[ Index ]

PHP Cross Reference of MyBB 1.8.17

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


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