<?php
//
// Copyright (C) 2003 Jan de Graaff
// All rights reserved.
//
// This program uses parts of the original Simpleboard Application
// 0.7.0b written by Josh Levine; http://www.joshlevine.net
//
// This source file is part of the SimpleBoard Component, a Mambo 4.5
// custom Component By Jan de Graaff - http://tsmf.jigsnet.com
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License (GPL)
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// Please note that the GPL states that any headers in files and
// Copyright notices as well as credits in headers, source files
// and output (screens, prints, etc.) can not be removed.
// You can extend them with your own credits, though...
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
//
// The "GNU General Public License" (GPL) is available at
// http://www.gnu.org/copyleft/gpl.html.
//
// Dont allow direct linking
defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );
//securing passed form elements
$catid=(int)$catid;
$Itemid=(int)$Itemid;
require_once($sbp.'/sb_auth.php');
//resetting some things:
$moderatedForum=0;
$lockedForum=0;
// Start getting the categories
$database->setQuery("SELECT * FROM #__sb_categories WHERE parent= '0' and published='1' ORDER BY ordering");
$allCat=$database->loadObjectList();

if ( $bPageTitle ) { $mainframe->setPageTitle( _GEN_FORUMLIST.' - '.$sbConfig['board_title'] ); }
//debug("timecookie=".date(_DATETIME ,$settings[prevvisit]));
foreach ($allCat as $category)
{
  $threadids[]=$category->id;
  $categories[$category->parent][]=$category;
}

//Let's check if the only thing we need to show is 1 category
if (in_array($catid,$threadids)) { 
   //Yes, so now $threadids should contain only the current $catid:
   unset($threadids);
   $threadids[]=$catid;
   //get new categories list for this category only:
   unset($categories);
   $database->setQuery("SELECT * FROM #__sb_categories WHERE parent= '0' and published='1' and id='$catid' ORDER BY ordering");
   $categories[$category->parent]=$database->loadObjectList();
} 
?>

<link rel="stylesheet" href="components/com_simpleboard/template/<?php echo $sbConfig['template']; ?>/forum.css" type="text/css" />

 <table border="0" width="100%" align="center">
    <?php include ($sbp.'/searchbox.php');?>
    <tr>
      <td align="center">
        <b><font size="2"><?php echo $board_title;?></font></b>
        <?php echo $sbConfig['board_offline'] ? '<br /><font color="red">' ._FORUM_IS_OFFLINE. '</font>' : '&nbsp;' ;?>
      </td>
    </tr>
  </table>
<br />
<div align="center">
<?php
    //get the db data with allowed forums and turn it into an array
    if ($sbSession->allowed != "na" && ! $new_sb_user)
      {
         $allow_forum=explode(',',$sbSession->allowed);
      }
      else
      {
         $allow_forum = array();
      }

?>
   <a href="<?php echo sefRelToAbs('index.php?option=com_simpleboard&amp;Itemid='.$Itemid);?>">
      <?php echo $sbIcons['home'] ? '<img src="'.$mls.'/modules/mod_sbicons/'.$sbIcons['home'].'" border="0" alt="'._HOME.'"  title="'._HOME.'" />' : _HOME; ?>
   </a>

  <?php
  if ($my_id != 0)
  {
    echo $sbIcons['profile'] ?'':'| ';?><a href="<?php if ($sbConfig['cb_profile']) { echo sefRelToAbs('index.php?option=com_comprofiler&amp;Itemid='.$cbitemid); } else { echo sefRelToAbs('index.php?option=com_simpleboard&amp;Itemid='.$Itemid.'&amp;func=userprofile&amp;do=show'); }?>">
       <?php echo $sbIcons['profile'] ? '<img src="'.$mls.'/modules/mod_sbicons/'.$sbIcons['profile'].'" border="0" alt="'._GEN_MYPROFILE.'" title="'._GEN_MYPROFILE.'"/>' : _GEN_MYPROFILE; ?>
    </a>
    <?php
  }
  echo $sbIcons['showlatest'] ?'':'| ';?><a href="<?php echo sefRelToAbs('index.php?option=com_simpleboard&amp;Itemid='.$Itemid.'&amp;func=latest');?>">
     <?php echo $sbIcons['showlatest'] ? '<img src="'.$mls.'/modules/mod_sbicons/'.$sbIcons['showlatest'].'" border="0" alt="'._GEN_LATEST_POSTS.'" title="'._GEN_LATEST_POSTS.'"/>' : _GEN_LATEST_POSTS; ?>
  </a>
  <?php echo $sbIcons['help'] ?'':'| ';?><a href="<?php echo sefRelToAbs('index.php?option=com_simpleboard&amp;Itemid='.$Itemid.'&amp;func=faq');?>">
     <?php echo $sbIcons['help'] ? '<img src="'.$mls.'/modules/mod_sbicons/'.$sbIcons['help'].'" border="0" alt="'._GEN_HELP.'" title="'._GEN_HELP.'"/>' : _GEN_HELP; ?>
  </a>
 <?php
 if ( $sbConfig['enableRulesPage'] ) {
   	echo $sbIcons['rules'] ?'':'| ';
	echo '<a href="'.sefRelToAbs('index.php?option=com_simpleboard&Itemid='.$Itemid.'&func=rules').'">';
    echo $sbIcons['rules'] ? '<img src="'.$mls.'/modules/mod_sbicons/'.$sbIcons['RULES'].'" border=0 alt="'._GEN_RULES.'" title="'._GEN_RULES.'"/>' : _GEN_RULES; 
	echo '</a>';
 }?>  
</div>
<br />

<table width="100%" border="0" cellspacing="1" cellpadding="3" class="contentpane" style="text-align: left;">
<?php

  if (count($categories[0]) > 0)
  {
    foreach($categories[0] as $cat)
    {


     $is_Mod=0;
     if (!$is_admin)
     {
        $database->setQuery("SELECT userid FROM #__sb_moderation WHERE catid='$cat->id' and userid='$my->id'");
        if ($database->loadResult() ) {$is_Mod=1;}
     }else{ $is_Mod=1;} //superadmins always are

     $letPass=0;
     //Do user identification based upon the ACL; but don't bother for moderators
     if (!$is_Mod) {$letPass=sb_auth::validate_user($cat, $allow_forum, $aro_group->group_id, $acl);}

     if ($letPass || $is_Mod)
     {
       //we're in, now set a cookie that this user is allowed to get in here
       //so we can skip these checks for the rest of this session to avoid performance problems
       if($allowed_forums=="")
       {
          $allowed_forums=$cat->id;
       }
       else
       {
          $allowed_forums=$allowed_forums.','.$cat->id;
       }

       $database->setQuery("UPDATE #__sb_sessions SET allowed='$allowed_forums' WHERE userid=$my->id");
       $database->query();
      ?>
      <tr>
         <th colspan="<?php if ($sbConfig['showNew'] && $my->id != 0){echo '6';} else {echo '5';}?>" class="sb_catname"><?php echo '<a href="'.sefRelToAbs('index.php?option=com_simpleboard&amp;Itemid='.$Itemid.'&amp;task=listcat&amp;catid='.$cat->id).'">'.$cat->name.'</a>';?></th>
      </tr>
      <tr>
      <?php if ($sbConfig['showNew'] && $my->id != 0){echo '<td width=10  class="sectiontableheader">&nbsp;</td>';}?>
        <td class="sectiontableheader" width="" align="left"><strong><?php echo _GEN_FORUM;?></strong></td>
        <td class="sectiontableheader" width="15" align="center"><strong><?php echo _GEN_TOPICS;?></strong></td>
        <td class="sectiontableheader" width="15" align="center"><strong><?php echo _GEN_REPLIES;?></strong></td>
        <td class="sectiontableheader" width="110" align="center" colspan="2"><strong><?php echo _GEN_LAST_POST;?></strong></td>
      </tr>

      <?php
        //show forums within the categories
        $database->setQuery( "SELECT id, name, locked,review, pub_access, pub_recurse, admin_access, admin_recurse FROM #__sb_categories WHERE parent='$cat->id'  and published='1' order by ordering");
        $rows = $database->loadObjectList();

        $tabclass = array("sectiontableentry1", "sectiontableentry2");
        $k=0;

        foreach($rows as $singlerow)
        {
         $is_Mod=0;
        if (!$is_admin)
        {
           $database->setQuery("SELECT userid FROM #__sb_moderation WHERE catid='$singlerow->id' and userid='$my->id'");
           if ($database->loadResult() ) {$is_Mod=1;}
        }else{ $is_Mod=1;} //superadmins always are

            //Do user identification based upon the ACL; but don't bother for moderators
            $letPass=0;
            if (!$is_Mod) {$letPass=sb_auth::validate_user($singlerow, $allow_forum, $aro_group->group_id, $acl);}

            if ($letPass || $is_Mod)
            {
               //we're in, now set a cookie that this user is allowed to get in here
               //so we can skip these checks for the rest of this session to avoid performance problems
               if($allowed_forums=="")
               {
                  $allowed_forums=$singlerow->id;
               }
               else
               {
                  $allowed_forums=$allowed_forums.','.$singlerow->id;
               }

             $database->setQuery("UPDATE #__sb_sessions SET allowed='$allowed_forums' WHERE userid=$my->id");
             $database->query();

            //$k=for alternating row colours:
            $k=1-$k;

            //get previous visit time from cookie; if there's no cookie yet, use database stored value
            if ($settings[prevvisit]==0 && $previousVisit !=0)
            { $prevCheck= $previousVisit;}else{$prevCheck=$settings[prevvisit];}

            //count the number of topics posted in each forum
            $database->setQuery("SELECT id FROM #__sb_messages WHERE catid='$singlerow->id' and parent='0' and hold='0'");
            $num = $database->loadObjectList();
            $numtopics= count($num);

            //count the number of replies posted in each forum
            $database->setQuery("SELECT id FROM #__sb_messages WHERE catid='$singlerow->id' and parent!='0' and hold='0'");
            $num = $database->loadObjectList();
            $numreplies= count($num);

            //Get the last post from each forum
            $database->setQuery("SELECT MAX(time) from #__sb_messages WHERE catid='$singlerow->id' and hold='0' AND moved!='1'");
            $lastPosttime=$database->loadResult();
            $lastPosttime=(int)$lastPosttime;

            if ($my->id !=0){
               //get all threads with posts after the users last visit; don't bother for guests
               $database->setQuery("SELECT thread from #__sb_messages where catid='$singlerow->id' and hold='0' and time>$prevCheck group by thread");
               $newThreadsAll=$database->loadObjectList();

               //Get the topics this user has already read this session from #__sb_sessions
               $readTopics="";
               $database->setQuery("SELECT readtopics FROM #__sb_sessions WHERE userid=$my->id");
               $readTopics=$database->loadResult();
                  //make it into an array
                  $read_topics=explode(',',$readTopics);
            }
            //Get the forumdescription
            $database->setQuery("SELECT description from #__sb_categories where id='$singlerow->id'");
            $forumDesc=$database->loadResult();
            //get pending messages if user is a Moderator for that forum
            $database->setQuery("SELECT userid FROM #__sb_moderation WHERE catid='$singlerow->id'");
            $moderatorList=$database->loadObjectList();

            $modIDs[]=array();
            array_splice($modIDs,0);

            if(count($moderatorList)>0)
            {
              foreach($moderatorList as $ml)
              {
                $modIDs[]=$ml->userid;
              }
            }

            $nummodIDs=count($modIDs);
            $numPending=0;
            if ((in_array($my_id, $modIDs)) || $is_admin==1)
            {
              $database->setQuery("select count(*) from #__sb_messages where catid='$singlerow->id' and hold='1'");
              $numPending=$database->loadResult();
              $is_Mod=1;
            }
            $numPending=(int)$numPending;

            //get latest post info
            $latestname  = "";
            $latestcatid = "";
            $latestid    = "";
            $database->setQuery("SELECT id, catid,name from #__sb_messages WHERE time=$lastPosttime and hold='0' and moved!='1'");
            $latestpost=$database->loadObjectList();

            foreach ($latestpost as $lp)
            {
               $latestname  = $lp->name;
               $latestcatid = $lp->catid;
               $latestid    = $lp->id;
            }

            echo '<tr class="'. $tabclass[$k].'">';

            if ($sbConfig['showNew'] && $my->id !=0){


               //Check if unread threads are in any of the forums topics
               $newPostsAvailable=0;
               foreach ($newThreadsAll as $nta)
               {
                  if ( in_array($nta->thread,$read_topics)){
                     //$newPostsAvailable=0;
                  }else{$newPostsAvailable++;}

               }

               if ( $newPostsAvailable>0 && count($newThreadsAll)!=0 ){
                  echo '<td width="1%" class="sb_new">';
                  echo $sbIcons['unreadforum'] ? '<img src="'.$mls.'/modules/mod_sbicons/'.$sbIcons['unreadforum'].'" border="0" alt="'._GEN_FORUM_NEWPOST.'" title="'._GEN_FORUM_NEWPOST.'"/>' : $sbConfig['newChar'];
                  echo '</td>';
               }else{
                  echo '<td width="1%" class="sb_notnew">';
                  echo $sbIcons['readforum'] ? '<img src="'.$mls.'/modules/mod_sbicons/'.$sbIcons['readforum'].'" border="0" alt="'._GEN_FORUM_NOTNEW.'" title="'._GEN_FORUM_NOTNEW.'"/>' : $sbConfig['newChar'];
                  echo '</td>';
               }
            }
            echo '<td><a href="'.sefRelToAbs('index.php?option=com_simpleboard&amp;Itemid='.$Itemid.'&amp;func=showcat&amp;catid='.$singlerow->id).'">'.$singlerow->name.'</a>';
            if ($singlerow->locked)
            {
               echo $sbIcons['forumlocked'] ? '&nbsp;&nbsp;<img src="'.$mls.'/modules/mod_sbicons/'.$sbIcons['forumlocked'].'" border="0" alt="'._GEN_LOCKED_FORUM.'" title="'._GEN_LOCKED_FORUM.'"/>' : '&nbsp;&nbsp;<img src="'.$sbs.'/emoticons/lock.gif"  border="0"  width="15" height="15" alt="'._GEN_LOCKED_FORUM.'">';
               $lockedForum=1;
            }else{echo "";}
            if ($singlerow->review)
            {
               echo $sbIcons['forummoderated'] ? '&nbsp;&nbsp;<img src="'.$mls.'/modules/mod_sbicons/'.$sbIcons['forummoderated'].'" border="0" alt="'._GEN_MODERATED.'" title="'._GEN_MODERATED.'"/>' : '&nbsp;&nbsp;<img src="'.$sbs.'/emoticons/review.gif" border="0"  width="15" height="15" alt="'._GEN_MODERATED.'">';
               $moderatedForum=1;
            }else{echo "";}

            if ($forumDesc != "")
            {
              echo "<br />$forumDesc";
            }

            if ($numPending>0)
            {
              echo '<br /><font color="red">'.$numPending.' '._LISTCAT_PENDING.'</font>';
            }

            echo "</td>";
            echo "<td align=\"center\">$numtopics</td>";
            echo "<td align=\"center\">$numreplies</td>";
            if ($numtopics != 0)
            {
              ?>
                <td width="100">
                  <span class="createdate"><?php echo date(_DATETIME , $lastPosttime);?> <br /><?php echo _GEN_BY;?> <?php echo $latestname;?></span>
                </td>
                <td width="10">
                  <a href="<?php echo sefRelToAbs('index.php?option=com_simpleboard&amp;Itemid='.$Itemid.'&amp;func=view&amp;catid='.$latestcatid.'&amp;id='.$latestid).'#'.$latestid;?>">
                    <?php
                    echo $sbIcons['latestpost'] ? '<img src="'.$mls.'/modules/mod_sbicons/'.$sbIcons['latestpost'].'" border="0" alt="'._SHOW_LAST.'" title="'._SHOW_LAST.'" />' : '  <img src="'.$sbs.'/emoticons/icon_newest_reply.gif" border="0"  width="18" height="9" alt="'._SHOW_LAST.'" />';
                    ?>
                  </a>
            </td></tr>
              <?php
            }
            else
            {
              echo' <td colspan="2"><span class="createdate">' ._NO_POSTS. '</span></td></tr>';
            }
          }
        }

        //echo "</tr>";
      }
     }
     if ($my->id != 0){
         echo '<tr><td colspan="6" align="left">';
         echo '<form action="'.sefRelToAbs('index.php?option=com_simpleboard&amp;Itemid='.$Itemid).'" name="markAllForumsRead" method="post"><input type="hidden" name="markaction" value="allread"> <input type="submit" class="button" value="'._GEN_MARK_ALL_FORUMS_READ.'">';
         //echo $sbIcons['markAllForumsRead'] ? '<img src="'.$mls.'/modules/mod_sbicons/'.$sbIcons['markAllForumsRead'].'" border="0" alt="'._GEN_MARK_ALL_FORUMS_READ.'" title="'._GEN_MARK_ALL_FORUMS_READ.'" /> - ' : _GEN_MARK_ALL_FORUMS_READ;
         echo '</form></td></tr>';
        echo '<tr><td colspan="6">';
           echo '<span class="sb_new">';
           echo $sbIcons['unreadforum'] ? '<img src="'.$mls.'/modules/mod_sbicons/'.$sbIcons['unreadforum'].'" border="0" alt="'._GEN_FORUM_NEWPOST.'" title="'._GEN_FORUM_NEWPOST.'"/>' : $sbConfig['newChar'];
           echo '</span><small> - '._GEN_FORUM_NEWPOST.'</small><br /><span class="sb_notnew">';
           echo $sbIcons['readforum'] ? '<img src="'.$mls.'/modules/mod_sbicons/'.$sbIcons['readforum'].'" border="0" alt="'._GEN_FORUM_NOTNEW.'" title="'._GEN_FORUM_NOTNEW.'"/>' : $sbConfig['newChar'];
           echo '</span><small> - '._GEN_FORUM_NOTNEW.'</small></td></tr>';
     }
     if ($lockedForum==1) { ?>
      <tr>
         <td colspan="<?php if ($sbConfig['showNew'] && $my->id != 0){echo '6';} else {echo '5';}?>">
         <?php
         echo $sbIcons['forumlocked'] ? '<img src="'.$mls.'/modules/mod_sbicons/'.$sbIcons['forumlocked'].'" border="0" alt="'._GEN_LOCKED_FORUM.'" title="'._GEN_LOCKED_FORUM.'" /> - '._GEN_LOCKED_FORUM.'</td></tr>' : '  <img src="'.$sbs.'/emoticons/lock.gif" border="0"  width="15" height="15" alt="'._GEN_LOCKED_FORUM.'" /> - '._GEN_LOCKED_FORUM.'</td></tr>';
		}
		if ($moderatedForum==1) { ?>
		<tr>
         <td colspan="<?php if ($sbConfig['showNew'] && $my->id != 0){echo '6';} else {echo '5';}?>">
			<?php
         echo $sbIcons['forummoderated'] ? '<img src="'.$mls.'/modules/mod_sbicons/'.$sbIcons['forummoderated'].'" border="0" alt="'._GEN_MODERATED.'" title="'._GEN_MODERATED.'" /> - '._GEN_MODERATED.'</td></tr>' : '  <img src="'.$sbs.'/emoticons/review.gif" border="0" width="15" height="15" alt="'._GEN_MODERATED.'" /> - '._GEN_MODERATED.'</td></tr>';
      }
   }
   else
   {
     ?>
     <tr><td colspan="6">
     <p align=\"center\"><center>
     <?php
       echo _LISTCAT_NO_CATS.'<br />';
       echo _LISTCAT_ADMIN.'<br />';
       echo _LISTCAT_PANEL.'<br /><br />';
       echo _LISTCAT_INFORM.'<br /><br />';
       echo _LISTCAT_DO.' <img src="'.$sbs.'/emoticons/wink.gif" width="19" height="19" alt="" border="0" />';
     ?>
     </center></p>
     </td></tr>
     <?php
   }
if ($sbConfig['enableRSS']) echo '<tr><td colspan="3"><a href="'.$mosConfig_live_site.'/index2.php?option=com_simpleboard&amp;func=sb_rss&amp;no_html=1" target="_blank"><img src="'.$mosConfig_live_site.'/images/M_images/rss.png" border="0" alt="'._LISTCAT_RSS.'" title="'._LISTCAT_RSS.'" /> - '._LISTCAT_RSS.'</a></td></tr>';
?>
</table>