[ Index ]

PHP Cross Reference of MyBB 1.8.37

title

Body

[close]

/inc/ -> functions_forumlist.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  /**
  12  * Build a list of forum bits.
  13  *
  14  * @param int $pid The parent forum to fetch the child forums for (0 assumes all)
  15  * @param int $depth The depth to return forums with.
  16  * @return array Array of information regarding the child forums of this parent forum
  17  */
  18  function build_forumbits($pid=0, $depth=1)
  19  {
  20      global $db, $fcache, $moderatorcache, $forumpermissions, $theme, $mybb, $templates, $bgcolor, $collapsed, $lang, $showdepth, $plugins, $parser, $forum_viewers;
  21      static $private_forums;
  22  
  23      $forum_listing = '';
  24  
  25      // If no forums exist with this parent, do nothing
  26      if(empty($fcache[$pid]) || !is_array($fcache[$pid]))
  27      {
  28          return;
  29      }
  30  
  31      $parent_counters['threads'] = 0;
  32      $parent_counters['posts'] = 0;
  33      $parent_counters['unapprovedposts'] = 0;
  34      $parent_counters['unapprovedthreads'] = 0;
  35      $parent_counters['viewers'] = 0;
  36      $forum_list = $comma = '';
  37      $donecount = 0;
  38  
  39      // Foreach of the forums in this parent
  40      foreach($fcache[$pid] as $parent)
  41      {
  42          foreach($parent as $forum)
  43          {
  44              $forum['viewers'] = 0;
  45              $subforums = $sub_forums = '';
  46              $lastpost_data = array(
  47                  'lastpost' => 0,
  48                  'lastposter' => '',
  49              );
  50              $forum_viewers_text = '';
  51              $forum_viewers_text_plain = '';
  52  
  53              // Get the permissions for this forum
  54              $permissions = $forumpermissions[$forum['fid']];
  55  
  56              // If this user doesnt have permission to view this forum and we're hiding private forums, skip this forum
  57              if($permissions['canview'] != 1 && $mybb->settings['hideprivateforums'] == 1)
  58              {
  59                  continue;
  60              }
  61  
  62              $forum = $plugins->run_hooks("build_forumbits_forum", $forum);
  63  
  64              // Build the link to this forum
  65              $forum_url = get_forum_link($forum['fid']);
  66  
  67              $hideinfo = $hidecounters = false;
  68              $hidelastpostinfo = false;
  69              $showlockicon = 0;
  70  
  71              // Hide post info if user cannot view forum or cannot view threads
  72              if($permissions['canview'] != 1 || (isset($permissions['canviewthreads']) && $permissions['canviewthreads'] != 1))
  73              {
  74                  $hideinfo = true;
  75              }
  76  
  77              if(isset($permissions['canonlyviewownthreads']) && $permissions['canonlyviewownthreads'] == 1)
  78              {
  79                  $hidecounters = true;
  80  
  81                  // If we only see our own threads, find out if there's a new post in one of them so the lightbulb shows
  82                  if(!is_array($private_forums))
  83                  {
  84                      $private_forums = $fids = array();
  85                      foreach($fcache as $fcache_p)
  86                      {
  87                          foreach($fcache_p as $parent_p)
  88                          {
  89                              foreach($parent_p as $forum_p)
  90                              {
  91                                  if($forumpermissions[$forum_p['fid']]['canonlyviewownthreads'])
  92                                  {
  93                                      $fids[] = $forum_p['fid'];
  94                                  }
  95                              }
  96                          }
  97                      }
  98  
  99                      if(!empty($fids))
 100                      {
 101                          $fids = implode(',', $fids);
 102                          $query = $db->simple_select("threads", "tid, fid, subject, lastpost, lastposter, lastposteruid", "uid = '{$mybb->user['uid']}' AND fid IN ({$fids}) AND visible != '-2'", array("order_by" => "lastpost", "order_dir" => "desc"));
 103  
 104                          while($thread = $db->fetch_array($query))
 105                          {
 106                              if(!isset($private_forums[$thread['fid']]))
 107                              {
 108                                  $private_forums[$thread['fid']] = $thread;
 109                              }
 110                          }
 111                      }
 112                  }
 113  
 114                  if(!empty($private_forums[$forum['fid']]['lastpost']))
 115                  {
 116                      $forum['lastpost'] = $private_forums[$forum['fid']]['lastpost'];
 117  
 118                      if(!$private_forums[$forum['fid']]['lastposteruid'] && !$private_forums[$forum['fid']]['lastposter'])
 119                      {
 120                          $private_forums[$forum['fid']]['lastposter'] = $lang->guest; // htmlspecialchars_uni'd when formatted later
 121                      }
 122  
 123                      $lastpost_data = array(
 124                          "lastpost" => $private_forums[$forum['fid']]['lastpost'],
 125                          "lastpostsubject" => $private_forums[$forum['fid']]['subject'],
 126                          "lastposter" => $private_forums[$forum['fid']]['lastposter'],
 127                          "lastposttid" => $private_forums[$forum['fid']]['tid'],
 128                          "lastposteruid" => $private_forums[$forum['fid']]['lastposteruid']
 129                      );
 130                  }
 131              }
 132              else
 133              {
 134                  if(!$forum['lastposteruid'] && !$forum['lastposter'])
 135                  {
 136                      $forum['lastposter'] = $lang->guest; // htmlspecialchars_uni'd when formatted later
 137                  }
 138  
 139                  $lastpost_data = array(
 140                      "lastpost" => $forum['lastpost'],
 141                      "lastpostsubject" => $forum['lastpostsubject'],
 142                      "lastposter" => $forum['lastposter'],
 143                      "lastposttid" => $forum['lastposttid'],
 144                      "lastposteruid" => $forum['lastposteruid']
 145                  );
 146              }
 147  
 148              // This forum has a password, and the user isn't authenticated with it - hide post information
 149              if(!forum_password_validated($forum, true))
 150              {
 151                  $hideinfo = true;
 152                  $showlockicon = 1;
 153              }
 154  
 155              if(is_array($forum_viewers) && isset($forum_viewers[$forum['fid']]) && $forum_viewers[$forum['fid']] > 0)
 156              {
 157                  $forum['viewers'] = $forum_viewers[$forum['fid']];
 158              }
 159  
 160              // Fetch subforums of this forum
 161              if(isset($fcache[$forum['fid']]))
 162              {
 163                  $forum_info = build_forumbits($forum['fid'], $depth+1);
 164  
 165                  // Increment forum counters with counters from child forums
 166                  $forum['threads'] += $forum_info['counters']['threads'];
 167                  $forum['posts'] += $forum_info['counters']['posts'];
 168                  $forum['unapprovedthreads'] += $forum_info['counters']['unapprovedthreads'];
 169                  $forum['unapprovedposts'] += $forum_info['counters']['unapprovedposts'];
 170  
 171                  if(!empty($forum_info['counters']['viewers']))
 172                  {
 173                      $forum['viewers'] += $forum_info['counters']['viewers'];
 174                  }
 175  
 176                  // If the child forums' lastpost is greater than the one for this forum, set it as the child forums greatest.
 177                  if(isset($forum_info['lastpost']['lastpost']) && $forum_info['lastpost']['lastpost'] > $lastpost_data['lastpost'])
 178                  {
 179                      $lastpost_data = $forum_info['lastpost'];
 180  
 181                      /*
 182                      // If our subforum is unread, then so must be our parents. Force our parents to unread as well
 183                      if(strstr($forum_info['lightbulb']['folder'], "on") !== false)
 184                      {
 185                          $forum['lastread'] = 0;
 186                      }
 187                      // Otherwise, if we  have an explicit record in the db, we must make sure that it is explicitly set
 188                      else
 189                      {
 190                          $lastpost_data['lastpost'] = $forum['lastpost'];
 191                      }*/
 192                  }
 193  
 194                  $sub_forums = $forum_info['forum_list'];
 195              }
 196  
 197              // If we are hiding information (lastpost) because we aren't authenticated against the password for this forum, remove them
 198              if($hideinfo == true || $hidelastpostinfo == true)
 199              {
 200                  // Used later for get_forum_lightbulb function call - Setting to 0 prevents the bulb from being lit up
 201                  // If hiding info or hiding lastpost info no "unread" posts indication should be shown to the user.
 202                  $lastpost_data = array(
 203                      'lastpost' => 0,
 204                      'lastposter' => '',
 205                  );
 206              }
 207  
 208              // If the current forums lastpost is greater than other child forums of the current parent and forum info isn't hidden, overwrite it
 209              if((!isset($parent_lastpost) || $lastpost_data['lastpost'] > $parent_lastpost['lastpost']) && $hideinfo != true)
 210              {
 211                  $parent_lastpost = $lastpost_data;
 212              }
 213  
 214              // Increment the counters for the parent forum (returned later)
 215              if($hideinfo != true && $hidecounters != true)
 216              {
 217                  $parent_counters['threads'] += $forum['threads'];
 218                  $parent_counters['posts'] += $forum['posts'];
 219                  $parent_counters['unapprovedposts'] += $forum['unapprovedposts'];
 220                  $parent_counters['unapprovedthreads'] += $forum['unapprovedthreads'];
 221  
 222                  if(!empty($forum['viewers']))
 223                  {
 224                      $parent_counters['viewers'] += $forum['viewers'];
 225                  }
 226              }
 227  
 228              // Done with our math, lets talk about displaying - only display forums which are under a certain depth
 229              if($depth > $showdepth)
 230              {
 231                  continue;
 232              }
 233  
 234              // Get the lightbulb status indicator for this forum based on the lastpost
 235              $lightbulb = get_forum_lightbulb($forum, $lastpost_data, $showlockicon);
 236  
 237              // Fetch the number of unapproved threads and posts for this forum
 238              if($hideinfo == true)
 239              {
 240                  $unapproved = array(
 241                      "unapproved_posts" => '',
 242                      "unapproved_threads" => '',
 243                  );
 244              }
 245              else
 246              {
 247                  $unapproved = get_forum_unapproved($forum);
 248              }
 249  
 250              // Sanitize name and description of forum.
 251              $forum['name'] = preg_replace("#&(?!\#[0-9]+;)#si", "&amp;", $forum['name']); // Fix & but allow unicode
 252              $forum['description'] = preg_replace("#&(?!\#[0-9]+;)#si", "&amp;", $forum['description']); // Fix & but allow unicode
 253              $forum['name'] = preg_replace("#&([^\#])(?![a-z1-4]{1,10};)#i", "&#038;$1", $forum['name']);
 254              $forum['description'] = preg_replace("#&([^\#])(?![a-z1-4]{1,10};)#i", "&#038;$1", $forum['description']);
 255  
 256              // If this is a forum and we've got subforums of it, load the subforums list template
 257              if($depth == 2 && $sub_forums)
 258              {
 259                  eval("\$subforums = \"".$templates->get("forumbit_subforums")."\";");
 260              }
 261              // A depth of three indicates a comma separated list of forums within a forum
 262              else if($depth == 3)
 263              {
 264                  if($donecount < $mybb->settings['subforumsindex'])
 265                  {
 266                      $statusicon = '';
 267  
 268                      // Showing mini status icons for this forum
 269                      if($mybb->settings['subforumsstatusicons'] == 1)
 270                      {
 271                          $lightbulb['folder'] = "mini".$lightbulb['folder'];
 272                          eval("\$statusicon = \"".$templates->get("forumbit_depth3_statusicon", 1, 0)."\";");
 273                      }
 274  
 275                      // Fetch the template and append it to the list
 276                      eval("\$forum_list .= \"".$templates->get("forumbit_depth3", 1, 0)."\";");
 277                      $comma = $lang->comma;
 278                  }
 279  
 280                  // Have we reached our max visible subforums? put a nice message and break out of the loop
 281                  ++$donecount;
 282                  if($donecount == $mybb->settings['subforumsindex'])
 283                  {
 284                      if(subforums_count($fcache[$pid]) > $donecount)
 285                      {
 286                          $forum_list .= $comma.$lang->sprintf($lang->more_subforums, (subforums_count($fcache[$pid]) - $donecount));
 287                      }
 288                  }
 289                  continue;
 290              }
 291  
 292              // Forum is a category, set template type
 293              if($forum['type'] == 'c')
 294              {
 295                  $forumcat = '_cat';
 296              }
 297              // Forum is a standard forum, set template type
 298              else
 299              {
 300                  $forumcat = '_forum';
 301              }
 302  
 303              if($forum['linkto'] == '')
 304              {
 305                  // No posts have been made in this forum - show never text
 306                  if($lastpost_data['lastpost'] == 0 && $hideinfo != true)
 307                  {
 308                      eval("\$lastpost = \"".$templates->get("forumbit_depth2_forum_lastpost_never")."\";");
 309                  }
 310                  elseif($hideinfo != true)
 311                  {
 312                      // Format lastpost date and time
 313                      $lastpost_date = my_date('relative', $lastpost_data['lastpost']);
 314  
 315                      // Set up the last poster, last post thread id, last post subject and format appropriately
 316                      $lastpost_data['lastposter'] = htmlspecialchars_uni($lastpost_data['lastposter']);
 317                      $lastpost_profilelink = build_profile_link($lastpost_data['lastposter'], $lastpost_data['lastposteruid']);
 318                      $lastpost_link = get_thread_link($lastpost_data['lastposttid'], 0, "lastpost");
 319                      $lastpost_subject = $full_lastpost_subject = $parser->parse_badwords($lastpost_data['lastpostsubject']);
 320                      if(my_strlen($lastpost_subject) > 25)
 321                      {
 322                          $lastpost_subject = my_substr($lastpost_subject, 0, 25)."...";
 323                      }
 324                      $lastpost_subject = htmlspecialchars_uni($lastpost_subject);
 325                      $full_lastpost_subject = htmlspecialchars_uni($full_lastpost_subject);
 326  
 327                      // Call lastpost template
 328                      if($depth != 1)
 329                      {
 330                          eval("\$lastpost = \"".$templates->get("forumbit_depth{$depth}_forum_lastpost")."\";");
 331                      }
 332                  }
 333  
 334                  if($mybb->settings['showforumviewing'] != 0 && $forum['viewers'] > 0)
 335                  {
 336                      if($forum['viewers'] == 1)
 337                      {
 338                          $forum_viewers_text = $lang->viewing_one;
 339                      }
 340                      else
 341                      {
 342                          $forum_viewers_text = $lang->sprintf($lang->viewing_multiple, $forum['viewers']);
 343                      }
 344                      $forum_viewers_text_plain = $forum_viewers_text;
 345                      eval("\$forum_viewers_text = \"".$templates->get("forumbit_depth2_forum_viewers")."\";");
 346                  }
 347              }
 348              // If this forum is a link or is password protected and the user isn't authenticated, set counters to "-"
 349              if($forum['linkto'] != '' || $hideinfo == true || $hidecounters == true)
 350              {
 351                  $posts = "-";
 352                  $threads = "-";
 353              }
 354              // Otherwise, format thread and post counts
 355              else
 356              {
 357                  $posts = my_number_format($forum['posts']);
 358                  $threads = my_number_format($forum['threads']);
 359              }
 360  
 361              // If this forum is a link or is password protected and the user isn't authenticated, set lastpost to "-"
 362              if($forum['linkto'] != '' || $hideinfo == true || $hidelastpostinfo == true)
 363              {
 364                  eval("\$lastpost = \"".$templates->get("forumbit_depth2_forum_lastpost_hidden")."\";");
 365              }
 366  
 367              // Moderator column
 368              $modlist = '';
 369              if($mybb->settings['modlist'] != 0)
 370              {
 371                  $done_moderators = array(
 372                      "users" => array(),
 373                      "groups" => array()
 374                  );
 375                  $moderators = '';
 376                  // Fetch list of moderators from this forum and its parents
 377                  $parentlistexploded = explode(',', $forum['parentlist']);
 378                  foreach($parentlistexploded as $mfid)
 379                  {
 380                      // This forum has moderators
 381                      if(isset($moderatorcache[$mfid]) && is_array($moderatorcache[$mfid]))
 382                      {
 383                          // Fetch each moderator from the cache and format it, appending it to the list
 384                          foreach($moderatorcache[$mfid] as $modtype)
 385                          {
 386                              foreach($modtype as $moderator)
 387                              {
 388                                  if($moderator['isgroup'])
 389                                  {
 390                                      if(in_array($moderator['id'], $done_moderators['groups']))
 391                                      {
 392                                          continue;
 393                                      }
 394  
 395                                      $moderator['title'] = htmlspecialchars_uni($moderator['title']);
 396  
 397                                      eval("\$moderators .= \"".$templates->get("forumbit_moderators_group", 1, 0)."\";");
 398                                      $done_moderators['groups'][] = $moderator['id'];
 399                                  }
 400                                  else
 401                                  {
 402                                      if(in_array($moderator['id'], $done_moderators['users']))
 403                                      {
 404                                          continue;
 405                                      }
 406  
 407                                      $moderator['profilelink'] = get_profile_link($moderator['id']);
 408                                      $moderator['username'] = htmlspecialchars_uni($moderator['username']);
 409  
 410                                      eval("\$moderators .= \"".$templates->get("forumbit_moderators_user", 1, 0)."\";");
 411                                      $done_moderators['users'][] = $moderator['id'];
 412                                  }
 413                                  $comma = $lang->comma;
 414                              }
 415                          }
 416                      }
 417                  }
 418                  $comma = '';
 419  
 420                  // If we have a moderators list, load the template
 421                  if($moderators)
 422                  {
 423                      eval("\$modlist = \"".$templates->get("forumbit_moderators")."\";");
 424                  }
 425              }
 426  
 427              // Descriptions aren't being shown - blank them
 428              if($mybb->settings['showdescriptions'] == 0)
 429              {
 430                  $forum['description'] = '';
 431              }
 432  
 433              // Check if this category is either expanded or collapsed and hide it as necessary.
 434              $expdisplay = '';
 435              $collapsed_name = "cat_{$forum['fid']}_e";
 436              if(isset($collapsed[$collapsed_name]) && $collapsed[$collapsed_name] == "display: none;")
 437              {
 438                  $expcolimage = "collapse_collapsed.png";
 439                  $expdisplay = "display: none;";
 440                  $expthead = " thead_collapsed";
 441                  $expaltext = $lang->expcol_expand;
 442              }
 443              else
 444              {
 445                  $expcolimage = "collapse.png";
 446                  $expthead = "";
 447                  $expaltext = $lang->expcol_collapse;
 448              }
 449  
 450              // Swap over the alternate backgrounds
 451              $bgcolor = alt_trow();
 452  
 453              // Add the forum to the list
 454              eval("\$forum_list .= \"".$templates->get("forumbit_depth$depth$forumcat")."\";");
 455          }
 456      }
 457  
 458      if(!isset($parent_lastpost))
 459      {
 460          $parent_lastpost = 0;
 461      }
 462  
 463      if(!isset($lightbulb))
 464      {
 465          $lightbulb = '';
 466      }
 467  
 468      // Return an array of information to the parent forum including child forums list, counters and lastpost information
 469      return array(
 470          "forum_list" => $forum_list,
 471          "counters" => $parent_counters,
 472          "lastpost" => $parent_lastpost,
 473          "lightbulb" => $lightbulb,
 474      );
 475  }
 476  
 477  /**
 478   * Fetch the status indicator for a forum based on its last post and the read date
 479   *
 480   * @param array $forum Array of information about the forum
 481   * @param array $lastpost Array of information about the lastpost date
 482   * @param int $locked Whether or not this forum is locked or not
 483   * @return array Array of the folder image to be shown and the alt text
 484   */
 485  function get_forum_lightbulb($forum, $lastpost, $locked=0)
 486  {
 487      global $mybb, $lang, $db, $unread_forums;
 488  
 489      // This forum is a redirect, so override the folder icon with the "offlink" icon.
 490      if(!empty($forum['linkto']))
 491      {
 492          $folder = "offlink";
 493          $altonoff = $lang->forum_redirect;
 494      }
 495      // This forum is closed, so override the folder icon with the "offclose" icon.
 496      elseif($forum['open'] == 0 || $locked)
 497      {
 498          $folder = "offclose";
 499          $altonoff = $lang->forum_closed;
 500      }
 501      else
 502      {
 503          // Fetch the last read date for this forum
 504          if(!empty($forum['lastread']))
 505          {
 506              $forum_read = $forum['lastread'];
 507          }
 508          elseif(!empty($mybb->cookies['mybb']['readallforums']))
 509          {
 510              // We've hit the read all forums as a guest, so use the lastvisit of the user
 511              $forum_read = $mybb->cookies['mybb']['lastvisit'];
 512          }
 513          else
 514          {
 515              $forum_read = 0;
 516              $threadcut = TIME_NOW - 60*60*24*$mybb->settings['threadreadcut'];
 517  
 518              // If the user is a guest, do they have a forumsread cookie?
 519              if(!$mybb->user['uid'] && isset($mybb->cookies['mybb']['forumread']))
 520              {
 521                  // If they've visited us before, then they'll have this cookie - otherwise everything is unread...
 522                  $forum_read = my_get_array_cookie("forumread", $forum['fid']);
 523              }
 524              else if($mybb->user['uid'] && $mybb->settings['threadreadcut'] > 0 && $threadcut > $lastpost['lastpost'])
 525              {
 526                  // We have a user, the forum's unread and we're over our threadreadcut limit for the lastpost - we mark these as read
 527                  $forum_read = $lastpost['lastpost'] + 1;
 528              }
 529          }
 530  
 531          //if(!$forum_read)
 532          //{
 533              //$forum_read = $mybb->user['lastvisit'];
 534          //}
 535  
 536           // If the lastpost is greater than the last visit and is greater than the forum read date, we have a new post
 537          if($lastpost['lastpost'] > $forum_read && $lastpost['lastpost'] != 0)
 538          {
 539              $unread_forums++;
 540              $folder = "on";
 541              $altonoff = $lang->new_posts;
 542          }
 543          // Otherwise, no new posts
 544          else
 545          {
 546              $folder = "off";
 547              $altonoff = $lang->no_new_posts;
 548          }
 549      }
 550  
 551      return array(
 552          "folder" => $folder,
 553          "altonoff" => $altonoff
 554      );
 555  }
 556  
 557  /**
 558   * Fetch the number of unapproved posts, formatted, from a forum
 559   *
 560   * @param array $forum Array of information about the forum
 561   * @return array Array containing formatted string for posts and string for threads
 562   */
 563  function get_forum_unapproved($forum)
 564  {
 565      global $lang, $templates;
 566  
 567      $unapproved_threads = $unapproved_posts = '';
 568  
 569      // If the user is a moderator we need to fetch the count
 570      if(is_moderator($forum['fid'], "canviewunapprove"))
 571      {
 572          // Forum has one or more unaproved posts, format language string accordingly
 573          if($forum['unapprovedposts'])
 574          {
 575              if($forum['unapprovedposts'] > 1)
 576              {
 577                  $unapproved_posts_count = $lang->sprintf($lang->forum_unapproved_posts_count, $forum['unapprovedposts']);
 578              }
 579              else
 580              {
 581                  $unapproved_posts_count = $lang->sprintf($lang->forum_unapproved_post_count, 1);
 582              }
 583  
 584              $forum['unapprovedposts'] = my_number_format($forum['unapprovedposts']);
 585              eval("\$unapproved_posts = \"".$templates->get("forumbit_depth2_forum_unapproved_posts")."\";");
 586          }
 587          // Forum has one or more unapproved threads, format language string accordingly
 588          if($forum['unapprovedthreads'])
 589          {
 590              if($forum['unapprovedthreads'] > 1)
 591              {
 592                  $unapproved_threads_count = $lang->sprintf($lang->forum_unapproved_threads_count, $forum['unapprovedthreads']);
 593              }
 594              else
 595              {
 596                  $unapproved_threads_count = $lang->sprintf($lang->forum_unapproved_thread_count, 1);
 597              }
 598  
 599              $forum['unapprovedthreads'] = my_number_format($forum['unapprovedthreads']);
 600              eval("\$unapproved_threads = \"".$templates->get("forumbit_depth2_forum_unapproved_threads")."\";");
 601          }
 602      }
 603      return array(
 604          "unapproved_posts" => $unapproved_posts,
 605          "unapproved_threads" => $unapproved_threads
 606      );
 607  }


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