[ Index ]

PHP Cross Reference of MyBB

title

Body

[close]

/inc/ -> functions_forumlist.php (source)

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


Generated: Tue Oct 8 19:19:50 2013 Cross-referenced by PHPXref 0.7.1