[ Index ]

PHP Cross Reference of MyBB 1.8.12

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


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