[ Index ]

PHP Cross Reference of MyBB 1.8.15

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,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      if($thread['firstpost'] == 0)
 463      {
 464          update_first_post($tid);
 465      }
 466  
 467      // Does this thread have a poll?
 468      if($thread['poll'])
 469      {
 470          $options = array(
 471              "limit" => 1
 472          );
 473          $query = $db->simple_select("polls", "*", "pid='".$thread['poll']."'", $options);
 474          $poll = $db->fetch_array($query);
 475          $poll['timeout'] = $poll['timeout']*60*60*24;
 476          $expiretime = $poll['dateline'] + $poll['timeout'];
 477          $now = TIME_NOW;
 478  
 479          // If the poll or the thread is closed or if the poll is expired, show the results.
 480          if($poll['closed'] == 1 || $thread['closed'] == 1 || ($expiretime < $now && $poll['timeout'] > 0) || $forumpermissions['canvotepolls'] != 1)
 481          {
 482              $showresults = 1;
 483          }
 484  
 485          if($forumpermissions['canvotepolls'] != 1)
 486          {
 487              $nopermission = 1;
 488          }
 489  
 490          // Check if the user has voted before...
 491          if($mybb->user['uid'])
 492          {
 493              $user_check = "uid='{$mybb->user['uid']}'";
 494          }
 495          else
 496          {
 497              $user_check = "uid='0' AND ipaddress=".$db->escape_binary($session->packedip);
 498          }
 499  
 500          $query = $db->simple_select("pollvotes", "*", "{$user_check} AND pid='".$poll['pid']."'");
 501          while($votecheck = $db->fetch_array($query))
 502          {
 503              $alreadyvoted = 1;
 504              $votedfor[$votecheck['voteoption']] = 1;
 505          }
 506  
 507          $optionsarray = explode("||~|~||", $poll['options']);
 508          $votesarray = explode("||~|~||", $poll['votes']);
 509          $poll['question'] = htmlspecialchars_uni($poll['question']);
 510          $polloptions = '';
 511          $totalvotes = 0;
 512          $poll['totvotes'] = 0;
 513  
 514          for($i = 1; $i <= $poll['numoptions']; ++$i)
 515          {
 516              $poll['totvotes'] = $poll['totvotes'] + $votesarray[$i-1];
 517          }
 518  
 519          // Loop through the poll options.
 520          for($i = 1; $i <= $poll['numoptions']; ++$i)
 521          {
 522              // Set up the parser options.
 523              $parser_options = array(
 524                  "allow_html" => $forum['allowhtml'],
 525                  "allow_mycode" => $forum['allowmycode'],
 526                  "allow_smilies" => $forum['allowsmilies'],
 527                  "allow_imgcode" => $forum['allowimgcode'],
 528                  "allow_videocode" => $forum['allowvideocode'],
 529                  "filter_badwords" => 1
 530              );
 531  
 532              if($mybb->user['showimages'] != 1 && $mybb->user['uid'] != 0 || $mybb->settings['guestimages'] != 1 && $mybb->user['uid'] == 0)
 533              {
 534                  $parser_options['allow_imgcode'] = 0;
 535              }
 536  
 537              if($mybb->user['showvideos'] != 1 && $mybb->user['uid'] != 0 || $mybb->settings['guestvideos'] != 1 && $mybb->user['uid'] == 0)
 538              {
 539                  $parser_options['allow_videocode'] = 0;
 540              }
 541  
 542              $option = $parser->parse_message($optionsarray[$i-1], $parser_options);
 543              $votes = $votesarray[$i-1];
 544              $totalvotes += $votes;
 545              $number = $i;
 546  
 547              // Mark the option the user voted for.
 548              if(!empty($votedfor[$number]))
 549              {
 550                  $optionbg = "trow2";
 551                  $votestar = "*";
 552              }
 553              else
 554              {
 555                  $optionbg = "trow1";
 556                  $votestar = "";
 557              }
 558  
 559              // If the user already voted or if the results need to be shown, do so; else show voting screen.
 560              if(isset($alreadyvoted) || isset($showresults))
 561              {
 562                  if((int)$votes == "0")
 563                  {
 564                      $percent = "0";
 565                  }
 566                  else
 567                  {
 568                      $percent = number_format($votes / $poll['totvotes'] * 100, 2);
 569                  }
 570                  $imagewidth = round($percent);
 571                  eval("\$polloptions .= \"".$templates->get("showthread_poll_resultbit")."\";");
 572              }
 573              else
 574              {
 575                  if($poll['multiple'] == 1)
 576                  {
 577                      eval("\$polloptions .= \"".$templates->get("showthread_poll_option_multiple")."\";");
 578                  }
 579                  else
 580                  {
 581                      eval("\$polloptions .= \"".$templates->get("showthread_poll_option")."\";");
 582                  }
 583              }
 584          }
 585  
 586          // If there are any votes at all, all votes together will be 100%; if there are no votes, all votes together will be 0%.
 587          if($poll['totvotes'])
 588          {
 589              $totpercent = "100%";
 590          }
 591          else
 592          {
 593              $totpercent = "0%";
 594          }
 595  
 596          // Check if user is allowed to edit posts; if so, show "edit poll" link.
 597          $edit_poll = '';
 598          if(is_moderator($fid, 'canmanagepolls'))
 599          {
 600              eval("\$edit_poll = \"".$templates->get("showthread_poll_editpoll")."\";");
 601          }
 602  
 603          // Decide what poll status to show depending on the status of the poll and whether or not the user voted already.
 604          if(isset($alreadyvoted) || isset($showresults) || isset($nopermission))
 605          {
 606              if($alreadyvoted)
 607              {
 608                  $pollstatus = $lang->already_voted;
 609  
 610                  $undovote = '';
 611                  if($mybb->usergroup['canundovotes'] == 1)
 612                  {
 613                      eval("\$undovote = \"".$templates->get("showthread_poll_undovote")."\";");
 614                  }
 615              }
 616              elseif($nopermission)
 617              {
 618                  $pollstatus = $lang->no_voting_permission;
 619              }
 620              else
 621              {
 622                  $pollstatus = $lang->poll_closed;
 623              }
 624  
 625              $lang->total_votes = $lang->sprintf($lang->total_votes, $totalvotes);
 626              eval("\$pollbox = \"".$templates->get("showthread_poll_results")."\";");
 627              $plugins->run_hooks("showthread_poll_results");
 628          }
 629          else
 630          {
 631              $closeon = '&nbsp;';
 632              if($poll['timeout'] != 0)
 633              {
 634                  $closeon = $lang->sprintf($lang->poll_closes, my_date($mybb->settings['dateformat'], $expiretime));
 635              }
 636  
 637              $publicnote = '&nbsp;';
 638              if($poll['public'] == 1)
 639              {
 640                  $publicnote = $lang->public_note;
 641              }
 642  
 643              eval("\$pollbox = \"".$templates->get("showthread_poll")."\";");
 644              $plugins->run_hooks("showthread_poll");
 645          }
 646  
 647      }
 648      else
 649      {
 650          $pollbox = "";
 651      }
 652  
 653      // Create the forum jump dropdown box.
 654      if($mybb->settings['enableforumjump'] != 0)
 655      {
 656          $forumjump = build_forum_jump("", $fid, 1);
 657      }
 658  
 659      // Fetch some links
 660      $next_oldest_link = get_thread_link($tid, 0, "nextoldest");
 661      $next_newest_link = get_thread_link($tid, 0, "nextnewest");
 662  
 663      // Mark this thread as read
 664      mark_thread_read($tid, $fid);
 665  
 666      // If the forum is not open, show closed newreply button unless the user is a moderator of this forum.
 667      $newthread = $newreply = '';
 668      if($forum['open'] != 0 && $forum['type'] == "f")
 669      {
 670          if($forumpermissions['canpostthreads'] != 0 && $mybb->user['suspendposting'] != 1)
 671          {
 672              eval("\$newthread = \"".$templates->get("showthread_newthread")."\";");
 673          }
 674  
 675          // Show the appropriate reply button if this thread is open or closed
 676          if($forumpermissions['canpostreplys'] != 0 && $mybb->user['suspendposting'] != 1 && ($thread['closed'] != 1 || is_moderator($fid, "canpostclosedthreads")) && ($thread['uid'] == $mybb->user['uid'] || $forumpermissions['canonlyreplyownthreads'] != 1))
 677          {
 678              eval("\$newreply = \"".$templates->get("showthread_newreply")."\";");
 679          }
 680          elseif($thread['closed'] == 1)
 681          {
 682              eval("\$newreply = \"".$templates->get("showthread_newreply_closed")."\";");
 683          }
 684      }
 685  
 686      // Create the admin tools dropdown box.
 687      if($ismod == true)
 688      {
 689          $closelinkch = $stickch = '';
 690  
 691          if($thread['closed'] == 1)
 692          {
 693              $closelinkch = ' checked="checked"';
 694          }
 695  
 696          if($thread['sticky'])
 697          {
 698              $stickch = ' checked="checked"';
 699          }
 700  
 701          if(is_moderator($thread['fid'], "canopenclosethreads"))
 702          {
 703              eval("\$closeoption .= \"".$templates->get("showthread_quickreply_options_close")."\";");
 704          }
 705  
 706          if(is_moderator($thread['fid'], "canstickunstickthreads"))
 707          {
 708              eval("\$closeoption .= \"".$templates->get("showthread_quickreply_options_stick")."\";");
 709          }
 710  
 711          $inlinecount = "0";
 712          $inlinecookie = "inlinemod_thread".$tid;
 713  
 714          $plugins->run_hooks("showthread_ismod");
 715      }
 716      else
 717      {
 718          $modoptions = "&nbsp;";
 719          $inlinemod = $closeoption = '';
 720      }
 721  
 722      // Increment the thread view.
 723      if($mybb->settings['delayedthreadviews'] == 1)
 724      {
 725          $db->shutdown_query("INSERT INTO ".TABLE_PREFIX."threadviews (tid) VALUES('{$tid}')");
 726      }
 727      else
 728      {
 729          $db->shutdown_query("UPDATE ".TABLE_PREFIX."threads SET views=views+1 WHERE tid='{$tid}'");
 730      }
 731      ++$thread['views'];
 732  
 733      // Work out the thread rating for this thread.
 734      $rating = '';
 735      if($mybb->settings['allowthreadratings'] != 0 && $forum['allowtratings'] != 0)
 736      {
 737          $rated = 0;
 738          $lang->load("ratethread");
 739          if($thread['numratings'] <= 0)
 740          {
 741              $thread['width'] = 0;
 742              $thread['averagerating'] = 0;
 743              $thread['numratings'] = 0;
 744          }
 745          else
 746          {
 747              $thread['averagerating'] = (float)round($thread['totalratings']/$thread['numratings'], 2);
 748              $thread['width'] = (int)round($thread['averagerating'])*20;
 749              $thread['numratings'] = (int)$thread['numratings'];
 750          }
 751  
 752          if($thread['numratings'])
 753          {
 754              // At least >someone< has rated this thread, was it me?
 755              // Check if we have already voted on this thread - it won't show hover effect then.
 756              $query = $db->simple_select("threadratings", "uid", "tid='{$tid}' AND uid='{$mybb->user['uid']}'");
 757              $rated = $db->fetch_field($query, 'uid');
 758          }
 759  
 760          $not_rated = '';
 761          if(!$rated)
 762          {
 763              $not_rated = ' star_rating_notrated';
 764          }
 765  
 766          $ratingvotesav = $lang->sprintf($lang->rating_average, $thread['numratings'], $thread['averagerating']);
 767          eval("\$ratethread = \"".$templates->get("showthread_ratethread")."\";");
 768      }
 769      // Work out if we are showing unapproved posts as well (if the user is a moderator etc.)
 770      if($ismod && is_moderator($fid, "canviewdeleted") == true && is_moderator($fid, "canviewunapprove") == false)
 771      {
 772          $visible = "AND p.visible IN (-1,1)";
 773      }
 774      elseif($ismod && is_moderator($fid, "canviewdeleted") == false && is_moderator($fid, "canviewunapprove") == true)
 775      {
 776          $visible = "AND p.visible IN (0,1)";
 777      }
 778      elseif($ismod && is_moderator($fid, "canviewdeleted") == true && is_moderator($fid, "canviewunapprove") == true)
 779      {
 780          $visible = "AND p.visible IN (-1,0,1)";
 781      }
 782      elseif($forumpermissions['canviewdeletionnotice'] != 0 && $ismod == false)
 783      {
 784          $visible = "AND p.visible IN (-1,1)";
 785      }
 786      else
 787      {
 788          $visible = "AND p.visible='1'";
 789      }
 790  
 791      // Can this user perform searches? If so, we can show them the "Search thread" form
 792      if($forumpermissions['cansearch'] != 0)
 793      {
 794          eval("\$search_thread = \"".$templates->get("showthread_search")."\";");
 795      }
 796  
 797      // Fetch the ignore list for the current user if they have one
 798      $ignored_users = array();
 799      if($mybb->user['uid'] > 0 && $mybb->user['ignorelist'] != "")
 800      {
 801          $ignore_list = explode(',', $mybb->user['ignorelist']);
 802          foreach($ignore_list as $uid)
 803          {
 804              $ignored_users[$uid] = 1;
 805          }
 806      }
 807  
 808      // Fetch profile fields to display on postbit
 809      $pfcache = $cache->read('profilefields');
 810  
 811      if(is_array($pfcache))
 812      {
 813          foreach($pfcache as $profilefield)
 814          {
 815              if($profilefield['postbit'] != 1)
 816              {
 817                  continue;
 818              }
 819  
 820              $profile_fields[$profilefield['fid']] = $profilefield;
 821          }
 822      }
 823  
 824      // Which thread mode is our user using by default?
 825      if(!empty($mybb->user['threadmode']))
 826      {
 827          $defaultmode = $mybb->user['threadmode'];
 828      }
 829      else if($mybb->settings['threadusenetstyle'] == 1)
 830      {
 831          $defaultmode = 'threaded';
 832      }
 833      else
 834      {
 835          $defaultmode = 'linear';
 836      }
 837  
 838      // If mode is unset, set the default mode
 839      if(!isset($mybb->input['mode']))
 840      {
 841          $mybb->input['mode'] = $defaultmode;
 842      }
 843  
 844      // Threaded or linear display?
 845      $threadexbox = '';
 846      if($mybb->get_input('mode') == 'threaded')
 847      {
 848          $isfirst = 1;
 849  
 850          // Are we linked to a specific pid?
 851          if($mybb->input['pid'])
 852          {
 853              $where = "AND p.pid='".$mybb->input['pid']."'";
 854          }
 855          else
 856          {
 857              $where = " ORDER BY dateline LIMIT 0, 1";
 858          }
 859          $query = $db->query("
 860              SELECT u.*, u.username AS userusername, p.*, f.*, eu.username AS editusername
 861              FROM ".TABLE_PREFIX."posts p
 862              LEFT JOIN ".TABLE_PREFIX."users u ON (u.uid=p.uid)
 863              LEFT JOIN ".TABLE_PREFIX."userfields f ON (f.ufid=u.uid)
 864              LEFT JOIN ".TABLE_PREFIX."users eu ON (eu.uid=p.edituid)
 865              WHERE p.tid='$tid' $visible $where
 866          ");
 867          $showpost = $db->fetch_array($query);
 868  
 869          // Choose what pid to display.
 870          if(!$mybb->input['pid'])
 871          {
 872              $mybb->input['pid'] = $showpost['pid'];
 873          }
 874  
 875          // Is there actually a pid to display?
 876          if(!$showpost['pid'])
 877          {
 878              error($lang->error_invalidpost);
 879          }
 880  
 881          $attachcache = array();
 882          if($mybb->settings['enableattachments'] == 1 && $thread['attachmentcount'] > 0 || is_moderator($fid, 'caneditposts'))
 883          {
 884              // Get the attachments for this post.
 885              $query = $db->simple_select("attachments", "*", "pid=".$mybb->input['pid']);
 886              while($attachment = $db->fetch_array($query))
 887              {
 888                  $attachcache[$attachment['pid']][$attachment['aid']] = $attachment;
 889              }
 890          }
 891  
 892          // Build the threaded post display tree.
 893          $query = $db->query("
 894              SELECT p.username, p.uid, p.pid, p.replyto, p.subject, p.dateline
 895              FROM ".TABLE_PREFIX."posts p
 896              WHERE p.tid='$tid'
 897              $visible
 898              ORDER BY p.dateline
 899          ");
 900          while($post = $db->fetch_array($query))
 901          {
 902              if(!$postsdone[$post['pid']])
 903              {
 904                  if($post['pid'] == $mybb->input['pid'] || ($isfirst && !$mybb->input['pid']))
 905                  {
 906                      $postcounter = count($postsdone);
 907                      $isfirst = 0;
 908                  }
 909                  $tree[$post['replyto']][$post['pid']] = $post;
 910                  $postsdone[$post['pid']] = 1;
 911              }
 912          }
 913  
 914          $threadedbits = buildtree();
 915          $posts = build_postbit($showpost);
 916          eval("\$threadexbox = \"".$templates->get("showthread_threadedbox")."\";");
 917          $plugins->run_hooks("showthread_threaded");
 918      }
 919      else // Linear display
 920      {
 921          $threadexbox = '';
 922          if(!$mybb->settings['postsperpage'] || (int)$mybb->settings['postsperpage'] < 1)
 923          {
 924              $mybb->settings['postsperpage'] = 20;
 925          }
 926  
 927          // Figure out if we need to display multiple pages.
 928          $page = 1;
 929          $perpage = $mybb->settings['postsperpage'];
 930          if($mybb->get_input('page', MyBB::INPUT_INT) && $mybb->get_input('page') != "last")
 931          {
 932              $page = $mybb->get_input('page', MyBB::INPUT_INT);
 933          }
 934  
 935          if(!empty($mybb->input['pid']))
 936          {
 937              $post = get_post($mybb->input['pid']);
 938              if(empty($post) || ($post['visible'] == 0 && !is_moderator($post['fid'], 'canviewunapprove')) || ($post['visible'] == -1 && !is_moderator($post['fid'], 'canviewdeleted') && $forumpermissions['canviewdeletionnotice'] == 0))
 939              {
 940                  $footer .= '<script type="text/javascript">$(document).ready(function() { $.jGrowl(\''.$lang->error_invalidpost.'\', {theme: \'jgrowl_error\'}); });</script>';
 941              }
 942              else
 943              {
 944                  $query = $db->query("
 945                      SELECT COUNT(p.dateline) AS count FROM ".TABLE_PREFIX."posts p
 946                      WHERE p.tid = '{$tid}'
 947                      AND p.dateline <= '{$post['dateline']}'
 948                      {$visible}
 949                  ");
 950                  $result = $db->fetch_field($query, "count");
 951                  if(($result % $perpage) == 0)
 952                  {
 953                      $page = $result / $perpage;
 954                  }
 955                  else
 956                  {
 957                      $page = (int)($result / $perpage) + 1;
 958                  }
 959              }
 960          }
 961  
 962          // Recount replies if user is a moderator or can see the deletion notice to take into account unapproved/deleted posts.
 963          if($ismod || $forumpermissions['canviewdeletionnotice'] != 0)
 964          {
 965              $query = $db->simple_select("posts p", "COUNT(*) AS replies", "p.tid='$tid' $visible");
 966              $cached_replies = $thread['replies']+$thread['unapprovedposts']+$thread['deletedposts'];
 967              $thread['replies'] = $db->fetch_field($query, 'replies')-1;
 968  
 969              // The counters are wrong? Rebuild them
 970              // This doesn't cover all cases however it is a good addition to the manual rebuild function
 971              if($thread['replies'] != $cached_replies)
 972              {
 973                  require_once  MYBB_ROOT."/inc/functions_rebuild.php";
 974                  rebuild_thread_counters($thread['tid']);
 975              }
 976          }
 977          elseif($forumpermissions['canviewdeletionnotice'] != 0)
 978          {
 979              $thread['replies'] += $thread['deletedposts'];
 980          }
 981  
 982          $postcount = (int)$thread['replies']+1;
 983          $pages = $postcount / $perpage;
 984          $pages = ceil($pages);
 985  
 986          if($mybb->get_input('page') == "last")
 987          {
 988              $page = $pages;
 989          }
 990  
 991          if($page > $pages || $page <= 0)
 992          {
 993              $page = 1;
 994          }
 995  
 996          if($page)
 997          {
 998              $start = ($page-1) * $perpage;
 999          }
1000          else
1001          {
1002              $start = 0;
1003              $page = 1;
1004          }
1005          $upper = $start+$perpage;
1006  
1007          // Work out if we have terms to highlight
1008          $highlight = "";
1009          $threadmode = "";
1010          if($mybb->seo_support == true)
1011          {
1012              if($mybb->get_input('highlight'))
1013              {
1014                  $highlight = "?highlight=".urlencode($mybb->get_input('highlight'));
1015              }
1016  
1017              if($defaultmode != "linear")
1018              {
1019                  if($mybb->get_input('highlight'))
1020                  {
1021                      $threadmode = "&amp;mode=linear";
1022                  }
1023                  else
1024                  {
1025                      $threadmode = "?mode=linear";
1026                  }
1027              }
1028          }
1029          else
1030          {
1031              if(!empty($mybb->input['highlight']))
1032              {
1033                  if(is_array($mybb->input['highlight']))
1034                  {
1035                      foreach($mybb->input['highlight'] as $highlight_word)
1036                      {
1037                          $highlight .= "&amp;highlight[]=".urlencode($highlight_word);
1038                      }
1039                  }
1040                  else
1041                  {
1042                      $highlight = "&amp;highlight=".urlencode($mybb->get_input('highlight'));
1043                  }
1044              }
1045  
1046              if($defaultmode != "linear")
1047              {
1048                  $threadmode = "&amp;mode=linear";
1049              }
1050          }
1051  
1052          $multipage = multipage($postcount, $perpage, $page, str_replace("{tid}", $tid, THREAD_URL_PAGED.$highlight.$threadmode));
1053  
1054          // Lets get the pids of the posts on this page.
1055          $pids = "";
1056          $comma = '';
1057          $query = $db->simple_select("posts p", "p.pid", "p.tid='$tid' $visible", array('order_by' => 'p.dateline', 'limit_start' => $start, 'limit' => $perpage));
1058          while($getid = $db->fetch_array($query))
1059          {
1060              // Set the ID of the first post on page to $pid if it doesn't hold any value
1061              // to allow this value to be used for Thread Mode/Linear Mode links
1062              // and ensure the user lands on the correct page after changing view mode
1063              if(empty($pid))
1064              {
1065                  $pid = $getid['pid'];
1066              }
1067              // Gather a comma separated list of post IDs
1068              $pids .= "$comma'{$getid['pid']}'";
1069              $comma = ",";
1070          }
1071          if($pids)
1072          {
1073              $pids = "pid IN($pids)";
1074  
1075              $attachcache = array();
1076              if($mybb->settings['enableattachments'] == 1 && $thread['attachmentcount'] > 0 || is_moderator($fid, 'caneditposts'))
1077              {
1078                  // Now lets fetch all of the attachments for these posts.
1079                  $query = $db->simple_select("attachments", "*", $pids);
1080                  while($attachment = $db->fetch_array($query))
1081                  {
1082                      $attachcache[$attachment['pid']][$attachment['aid']] = $attachment;
1083                  }
1084              }
1085          }
1086          else
1087          {
1088              // If there are no pid's the thread is probably awaiting approval.
1089              error($lang->error_invalidthread);
1090          }
1091  
1092          // Get the actual posts from the database here.
1093          $posts = '';
1094          $query = $db->query("
1095              SELECT u.*, u.username AS userusername, p.*, f.*, eu.username AS editusername
1096              FROM ".TABLE_PREFIX."posts p
1097              LEFT JOIN ".TABLE_PREFIX."users u ON (u.uid=p.uid)
1098              LEFT JOIN ".TABLE_PREFIX."userfields f ON (f.ufid=u.uid)
1099              LEFT JOIN ".TABLE_PREFIX."users eu ON (eu.uid=p.edituid)
1100              WHERE $pids
1101              ORDER BY p.dateline
1102          ");
1103          while($post = $db->fetch_array($query))
1104          {
1105              if($thread['firstpost'] == $post['pid'] && $thread['visible'] == 0)
1106              {
1107                  $post['visible'] = 0;
1108              }
1109              $posts .= build_postbit($post);
1110              $post = '';
1111          }
1112          $plugins->run_hooks("showthread_linear");
1113      }
1114  
1115      // Show the similar threads table if wanted.
1116      $similarthreads = '';
1117      if($mybb->settings['showsimilarthreads'] != 0)
1118      {
1119          $own_perm = '';
1120          if($forumpermissions['canonlyviewownthreads'] == 1)
1121          {
1122              $own_perm = " AND t.uid={$mybb->user['uid']}";
1123          }
1124  
1125          switch($db->type)
1126          {
1127              case "pgsql":
1128                  $query = $db->query("
1129                      SELECT t.*, t.username AS threadusername, u.username
1130                      FROM ".TABLE_PREFIX."threads t
1131                      LEFT JOIN ".TABLE_PREFIX."users u ON (u.uid = t.uid), plainto_tsquery ('".$db->escape_string($thread['subject'])."') AS query
1132                      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}
1133                      ORDER BY t.lastpost DESC
1134                      OFFSET 0 LIMIT {$mybb->settings['similarlimit']}
1135                  ");
1136                  break;
1137              default:
1138                  $query = $db->query("
1139                      SELECT t.*, t.username AS threadusername, u.username, MATCH (t.subject) AGAINST ('".$db->escape_string($thread['subject'])."') AS relevance
1140                      FROM ".TABLE_PREFIX."threads t
1141                      LEFT JOIN ".TABLE_PREFIX."users u ON (u.uid = t.uid)
1142                      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']}'
1143                      ORDER BY t.lastpost DESC
1144                      LIMIT 0, {$mybb->settings['similarlimit']}
1145                  ");
1146          }
1147  
1148          $count = 0;
1149          $similarthreadbits = '';
1150          $icon_cache = $cache->read("posticons");
1151          while($similar_thread = $db->fetch_array($query))
1152          {
1153              ++$count;
1154              $trow = alt_trow();
1155              if($similar_thread['icon'] > 0 && $icon_cache[$similar_thread['icon']])
1156              {
1157                  $icon = $icon_cache[$similar_thread['icon']];
1158                  $icon['path'] = str_replace("{theme}", $theme['imgdir'], $icon['path']);
1159                  $icon['path'] = htmlspecialchars_uni($icon['path']);
1160                  $icon['name'] = htmlspecialchars_uni($icon['name']);
1161                  eval("\$icon = \"".$templates->get("forumdisplay_thread_icon")."\";");
1162              }
1163              else
1164              {
1165                  $icon = "&nbsp;";
1166              }
1167              if(!$similar_thread['username'])
1168              {
1169                  $similar_thread['username'] = $similar_thread['profilelink'] = htmlspecialchars_uni($similar_thread['threadusername']);
1170              }
1171              else
1172              {
1173                  $similar_thread['username'] = htmlspecialchars_uni($similar_thread['username']);
1174                  $similar_thread['profilelink'] = build_profile_link($similar_thread['username'], $similar_thread['uid']);
1175              }
1176  
1177              // If this thread has a prefix, insert a space between prefix and subject
1178              if($similar_thread['prefix'] != 0)
1179              {
1180                  $prefix = build_prefixes($similar_thread['prefix']);
1181                  if(!empty($prefix))
1182                  {
1183                      $similar_thread['threadprefix'] = $prefix['displaystyle'].'&nbsp;';
1184                  }
1185              }
1186  
1187              $similar_thread['subject'] = $parser->parse_badwords($similar_thread['subject']);
1188              $similar_thread['subject'] = htmlspecialchars_uni($similar_thread['subject']);
1189              $similar_thread['threadlink'] = get_thread_link($similar_thread['tid']);
1190              $similar_thread['lastpostlink'] = get_thread_link($similar_thread['tid'], 0, "lastpost");
1191  
1192              $lastpostdate = my_date('relative', $similar_thread['lastpost']);
1193              $lastposter = $similar_thread['lastposter'];
1194              $lastposteruid = $similar_thread['lastposteruid'];
1195  
1196              // Don't link to guest's profiles (they have no profile).
1197              if($lastposteruid == 0)
1198              {
1199                  $lastposterlink = $lastposter;
1200              }
1201              else
1202              {
1203                  $lastposterlink = build_profile_link($lastposter, $lastposteruid);
1204              }
1205              $similar_thread['replies'] = my_number_format($similar_thread['replies']);
1206              $similar_thread['views'] = my_number_format($similar_thread['views']);
1207              eval("\$similarthreadbits .= \"".$templates->get("showthread_similarthreads_bit")."\";");
1208          }
1209          if($count)
1210          {
1211              eval("\$similarthreads = \"".$templates->get("showthread_similarthreads")."\";");
1212          }
1213      }
1214  
1215      // Decide whether or not to show quick reply.
1216      $quickreply = '';
1217      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))
1218      {
1219          $query = $db->simple_select("posts", "pid", "tid='{$tid}'", array("order_by" => "pid", "order_dir" => "desc", "limit" => 1));
1220          $last_pid = $db->fetch_field($query, "pid");
1221  
1222          // Show captcha image for guests if enabled
1223          $captcha = '';
1224          if($mybb->settings['captchaimage'] && !$mybb->user['uid'])
1225          {
1226              require_once  MYBB_ROOT.'inc/class_captcha.php';
1227              $post_captcha = new captcha(true, "post_captcha");
1228  
1229              if($post_captcha->html)
1230              {
1231                  $captcha = $post_captcha->html;
1232              }
1233          }
1234  
1235          $postoptionschecked = array('signature' => '', 'emailnotify' => '');
1236          if($mybb->user['signature'])
1237          {
1238              $postoptionschecked['signature'] = 'checked="checked"';
1239          }
1240  
1241          // Hide signature option if no permission
1242          $option_signature = '';
1243          if($mybb->usergroup['canusesig'] && !$mybb->user['suspendsignature'])
1244          {
1245              eval("\$option_signature = \"".$templates->get('showthread_quickreply_options_signature')."\";");
1246          }
1247  
1248          if(isset($mybb->user['emailnotify']) && $mybb->user['emailnotify'] == 1)
1249          {
1250              $postoptionschecked['emailnotify'] = 'checked="checked"';
1251          }
1252  
1253          $trow = alt_trow();
1254          if($thread['closed'] == 1)
1255          {
1256              $trow = 'trow_shaded';
1257          }
1258  
1259          $moderation_notice = '';
1260          if(!is_moderator($forum['fid'], "canapproveunapproveposts"))
1261          {
1262              if($forumpermissions['modposts'] == 1)
1263              {
1264                  $moderation_text = $lang->moderation_forum_posts;
1265                  eval('$moderation_notice = "'.$templates->get('global_moderation_notice').'";');
1266              }
1267  
1268              if($mybb->user['moderateposts'] == 1)
1269              {
1270                  $moderation_text = $lang->moderation_user_posts;
1271                  eval('$moderation_notice = "'.$templates->get('global_moderation_notice').'";');
1272              }
1273          }
1274  
1275          $posthash = md5($mybb->user['uid'].random_str());
1276          eval("\$quickreply = \"".$templates->get("showthread_quickreply")."\";");
1277      }
1278  
1279      $moderationoptions = '';
1280  
1281      // If the user is a moderator, show the moderation tools.
1282      if($ismod)
1283      {
1284          $customthreadtools = $customposttools = $standardthreadtools = $standardposttools = '';
1285  
1286          $threadnotesbox = $viewnotes = '';
1287          if(!empty($thread['notes']))
1288          {
1289              $thread['notes'] = nl2br(htmlspecialchars_uni($thread['notes']));
1290  
1291              if(strlen($thread['notes']) > 200)
1292              {
1293                  eval("\$viewnotes = \"".$templates->get("showthread_threadnotes_viewnotes")."\";");
1294                  $thread['notes'] = my_substr($thread['notes'], 0, 200)."... {$viewnotes}";
1295              }
1296  
1297              eval("\$threadnotesbox = \"".$templates->get("showthread_threadnotes")."\";");
1298          }
1299  
1300          if(is_moderator($forum['fid'], "canusecustomtools") && (!empty($forum_stats[-1]['modtools']) || !empty($forum_stats[$forum['fid']]['modtools'])))
1301          {
1302              $gids = explode(',', $mybb->user['additionalgroups']);
1303              $gids[] = $mybb->user['usergroup'];
1304              $gids = array_filter(array_unique($gids));
1305              switch($db->type)
1306              {
1307                  case "pgsql":
1308                  case "sqlite":
1309                      foreach($gids as $gid)
1310                      {
1311                          $gid = (int)$gid;
1312                          $gidswhere .= " OR ','||groups||',' LIKE '%,{$gid},%'";
1313                      }
1314                      $query = $db->simple_select("modtools", 'tid, name, type', "(','||forums||',' LIKE '%,$fid,%' OR ','||forums||',' LIKE '%,-1,%' OR forums='') AND (groups='' OR ','||groups||',' LIKE '%,-1,%'{$gidswhere})");
1315                      break;
1316                  default:
1317                      foreach($gids as $gid)
1318                      {
1319                          $gid = (int)$gid;
1320                          $gidswhere .= " OR CONCAT(',',groups,',') LIKE '%,{$gid},%'";
1321                      }
1322                      $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})");
1323                      break;
1324              }
1325  
1326              while($tool = $db->fetch_array($query))
1327              {
1328                  $tool['name'] = htmlspecialchars_uni($tool['name']);
1329                  if($tool['type'] == 'p')
1330                  {
1331                      eval("\$customposttools .= \"".$templates->get("showthread_inlinemoderation_custom_tool")."\";");
1332                  }
1333                  else
1334                  {
1335                      eval("\$customthreadtools .= \"".$templates->get("showthread_moderationoptions_custom_tool")."\";");
1336                  }
1337              }
1338  
1339              // Build inline moderation dropdown
1340              if(!empty($customposttools))
1341              {
1342                  eval("\$customposttools = \"".$templates->get("showthread_inlinemoderation_custom")."\";");
1343              }
1344          }
1345  
1346          $inlinemodsoftdelete = $inlinemodrestore = $inlinemoddelete = $inlinemodmanage = $inlinemodapprove = '';
1347  
1348          if(is_moderator($forum['fid'], "cansoftdeleteposts"))
1349          {
1350              eval("\$inlinemodsoftdelete = \"".$templates->get("showthread_inlinemoderation_softdelete")."\";");
1351          }
1352  
1353          if(is_moderator($forum['fid'], "canrestoreposts"))
1354          {
1355              eval("\$inlinemodrestore = \"".$templates->get("showthread_inlinemoderation_restore")."\";");
1356          }
1357  
1358          if(is_moderator($forum['fid'], "candeleteposts"))
1359          {
1360              eval("\$inlinemoddelete = \"".$templates->get("showthread_inlinemoderation_delete")."\";");
1361          }
1362  
1363          if(is_moderator($forum['fid'], "canmanagethreads"))
1364          {
1365              eval("\$inlinemodmanage = \"".$templates->get("showthread_inlinemoderation_manage")."\";");
1366          }
1367  
1368          if(is_moderator($forum['fid'], "canapproveunapproveposts"))
1369          {
1370              eval("\$inlinemodapprove = \"".$templates->get("showthread_inlinemoderation_approve")."\";");
1371          }
1372  
1373          if(!empty($inlinemodsoftdelete) || !empty($inlinemodrestore) || !empty($inlinemoddelete) || !empty($inlinemodmanage) || !empty($inlinemodapprove))
1374          {
1375              eval("\$standardposttools = \"".$templates->get("showthread_inlinemoderation_standard")."\";");
1376          }
1377  
1378          // Only show inline mod menu if there's options to show
1379          if(!empty($standardposttools) || !empty($customposttools))
1380          {
1381              eval("\$inlinemod = \"".$templates->get("showthread_inlinemoderation")."\";");
1382          }
1383  
1384          // Build thread moderation dropdown
1385          if(!empty($customthreadtools))
1386          {
1387              eval("\$customthreadtools = \"".$templates->get("showthread_moderationoptions_custom")."\";");
1388          }
1389  
1390          $openclosethread = $stickunstickthread = $deletethread = $threadnotes = $managethread = $adminpolloptions = $approveunapprovethread = $softdeletethread = '';
1391  
1392          if(is_moderator($forum['fid'], "canopenclosethreads"))
1393          {
1394              eval("\$openclosethread = \"".$templates->get("showthread_moderationoptions_openclose")."\";");
1395          }
1396  
1397          if(is_moderator($forum['fid'], "canstickunstickthreads"))
1398          {
1399              eval("\$stickunstickthread = \"".$templates->get("showthread_moderationoptions_stickunstick")."\";");
1400          }
1401  
1402          if(is_moderator($forum['fid'], "candeletethreads"))
1403          {
1404              eval("\$deletethread = \"".$templates->get("showthread_moderationoptions_delete")."\";");
1405          }
1406  
1407          if(is_moderator($forum['fid'], "canmanagethreads"))
1408          {
1409              eval("\$threadnotes = \"".$templates->get("showthread_moderationoptions_threadnotes")."\";");
1410              eval("\$managethread = \"".$templates->get("showthread_moderationoptions_manage")."\";");
1411          }
1412  
1413          if($pollbox && is_moderator($forum['fid'], "canmanagepolls"))
1414          {
1415              eval("\$adminpolloptions = \"".$templates->get("showthread_moderationoptions_deletepoll")."\";");
1416          }
1417  
1418          if(is_moderator($forum['fid'], "canapproveunapprovethreads"))
1419          {
1420              if($thread['visible'] == 0)
1421              {
1422                  eval("\$approveunapprovethread = \"".$templates->get("showthread_moderationoptions_approve")."\";");
1423              }
1424              else
1425              {
1426                  eval("\$approveunapprovethread = \"".$templates->get("showthread_moderationoptions_unapprove")."\";");
1427              }
1428          }
1429  
1430          if(is_moderator($forum['fid'], "cansoftdeletethreads") && $thread['visible'] != -1)
1431          {
1432              eval("\$softdeletethread = \"".$templates->get("showthread_moderationoptions_softdelete")."\";");
1433          }
1434          elseif(is_moderator($forum['fid'], "canrestorethreads") && $thread['visible'] == -1)
1435          {
1436              eval("\$softdeletethread = \"".$templates->get("showthread_moderationoptions_restore")."\";");
1437          }
1438  
1439          if(!empty($openclosethread) || !empty($stickunstickthread) || !empty($deletethread) || !empty($managethread) || !empty($adminpolloptions) || !empty($approveunapprovethread) || !empty($softdeletethread))
1440          {
1441              eval("\$standardthreadtools = \"".$templates->get("showthread_moderationoptions_standard")."\";");
1442          }
1443  
1444          // Only show mod menu if there's any options to show
1445          if(!empty($standardthreadtools) || !empty($customthreadtools))
1446          {
1447              eval("\$moderationoptions = \"".$templates->get("showthread_moderationoptions")."\";");
1448          }
1449      }
1450  
1451      // Display 'send thread' link if permissions allow
1452      $sendthread = '';
1453      if($mybb->usergroup['cansendemail'] == 1)
1454      {
1455          eval("\$sendthread = \"".$templates->get("showthread_send_thread")."\";");
1456      }
1457  
1458      // Display 'add poll' link to thread creator (or mods) if thread doesn't have a poll already
1459      $addpoll = '';
1460      $time = TIME_NOW;
1461      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))
1462      {
1463          eval("\$addpoll = \"".$templates->get("showthread_add_poll")."\";");
1464      }
1465  
1466      // Subscription status
1467      $add_remove_subscription = 'add';
1468      $add_remove_subscription_text = $lang->subscribe_thread;
1469  
1470      if($mybb->user['uid'])
1471      {
1472          $query = $db->simple_select("threadsubscriptions", "tid", "tid='".(int)$tid."' AND uid='".(int)$mybb->user['uid']."'", array('limit' => 1));
1473  
1474          if($db->fetch_field($query, 'tid'))
1475          {
1476              $add_remove_subscription = 'remove';
1477              $add_remove_subscription_text = $lang->unsubscribe_thread;
1478          }
1479  
1480          eval("\$addremovesubscription = \"".$templates->get("showthread_subscription")."\";");
1481      }
1482  
1483      $classic_header = '';
1484      if($mybb->settings['postlayout'] == "classic")
1485      {
1486          eval("\$classic_header = \"".$templates->get("showthread_classic_header")."\";");
1487      }
1488  
1489      // Get users viewing this thread
1490      if($mybb->settings['browsingthisthread'] != 0)
1491      {
1492          $timecut = TIME_NOW - $mybb->settings['wolcutoff'];
1493  
1494          $comma = '';
1495          $guestcount = 0;
1496          $membercount = 0;
1497          $inviscount = 0;
1498          $onlinemembers = '';
1499          $doneusers = array();
1500  
1501          $query = $db->query("
1502              SELECT s.ip, s.uid, s.time, u.username, u.invisible, u.usergroup, u.displaygroup
1503              FROM ".TABLE_PREFIX."sessions s
1504              LEFT JOIN ".TABLE_PREFIX."users u ON (s.uid=u.uid)
1505              WHERE s.time > '$timecut' AND location2='$tid' AND nopermission != 1
1506              ORDER BY u.username ASC, s.time DESC
1507          ");
1508  
1509          while($user = $db->fetch_array($query))
1510          {
1511              if($user['uid'] == 0)
1512              {
1513                  ++$guestcount;
1514              }
1515              else if(empty($doneusers[$user['uid']]) || $doneusers[$user['uid']] < $user['time'])
1516              {
1517                  ++$membercount;
1518                  $doneusers[$user['uid']] = $user['time'];
1519  
1520                  $invisiblemark = '';
1521                  if($user['invisible'] == 1)
1522                  {
1523                      $invisiblemark = "*";
1524                      ++$inviscount;
1525                  }
1526  
1527                  if($user['invisible'] != 1 || $mybb->usergroup['canviewwolinvis'] == 1 || $user['uid'] == $mybb->user['uid'])
1528                  {
1529                      $user['profilelink'] = get_profile_link($user['uid']);
1530                      $user['username'] = format_name(htmlspecialchars_uni($user['username']), $user['usergroup'], $user['displaygroup']);
1531                      $user['reading'] = my_date($mybb->settings['timeformat'], $user['time']);
1532  
1533                      eval("\$onlinemembers .= \"".$templates->get("showthread_usersbrowsing_user", 1, 0)."\";");
1534                      $comma = $lang->comma;
1535                  }
1536              }
1537          }
1538  
1539          $guestsonline = '';
1540          if($guestcount)
1541          {
1542              $guestsonline = $lang->sprintf($lang->users_browsing_thread_guests, $guestcount);
1543          }
1544  
1545          $invisonline = '';
1546          if($mybb->user['invisible'] == 1)
1547          {
1548              // the user was counted as invisible user --> correct the inviscount
1549              $inviscount -= 1;
1550          }
1551          if($inviscount && $mybb->usergroup['canviewwolinvis'] != 1)
1552          {
1553              $invisonline = $lang->sprintf($lang->users_browsing_forum_invis, $inviscount);
1554          }
1555  
1556          $onlinesep = '';
1557          if($invisonline != '' && $onlinemembers)
1558          {
1559              $onlinesep = $lang->comma;
1560          }
1561  
1562          $onlinesep2 = '';
1563          if($invisonline != '' && $guestcount || $onlinemembers && $guestcount)
1564          {
1565              $onlinesep2 = $lang->comma;
1566          }
1567  
1568          eval("\$usersbrowsing = \"".$templates->get("showthread_usersbrowsing")."\";");
1569      }
1570  
1571      $plugins->run_hooks("showthread_end");
1572  
1573      eval("\$showthread = \"".$templates->get("showthread")."\";");
1574      output_page($showthread);
1575  }
1576  
1577  /**
1578   * Build a navigation tree for threaded display.
1579   *
1580   * @param int $replyto
1581   * @param int $indent
1582   * @return string
1583   */
1584  function buildtree($replyto=0, $indent=0)
1585  {
1586      global $tree, $mybb, $theme, $mybb, $pid, $tid, $templates, $parser, $lang;
1587  
1588      $indentsize = 13 * $indent;
1589  
1590      ++$indent;
1591      $posts = '';
1592      if(is_array($tree[$replyto]))
1593      {
1594          foreach($tree[$replyto] as $key => $post)
1595          {
1596              $postdate = my_date('relative', $post['dateline']);
1597              $post['subject'] = htmlspecialchars_uni($parser->parse_badwords($post['subject']));
1598  
1599              if(!$post['subject'])
1600              {
1601                  $post['subject'] = "[".$lang->no_subject."]";
1602              }
1603  
1604              $post['username'] = htmlspecialchars_uni($post['username']);
1605              $post['profilelink'] = build_profile_link($post['username'], $post['uid']);
1606  
1607              if($mybb->input['pid'] == $post['pid'])
1608              {
1609                  eval("\$posts .= \"".$templates->get("showthread_threaded_bitactive")."\";");
1610              }
1611              else
1612              {
1613                  eval("\$posts .= \"".$templates->get("showthread_threaded_bit")."\";");
1614              }
1615  
1616              if($tree[$post['pid']])
1617              {
1618                  $posts .= buildtree($post['pid'], $indent);
1619              }
1620          }
1621          --$indent;
1622      }
1623      return $posts;
1624  }


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