###############################################################################
# UB_Index.src                                                                #
###############################################################################
# UltraBoard 2000 by UltraScripts.com, Inc.                                   #
# Scripts written by Jacky Wing Wa Yung, WebMaster@UltraScripts.com           #
# Available from http://www.UltraBoard.org/                                   #
# =========================================================================== #
# COPYRIGHT NOTICE :                                                          #
#                                                                             #
# Copyright (c) 1999-2000 UltraScripts.com, Inc. All Rights Reserved.         #
#                                                                             #
# This program is being distributed as shareware. It may be used and modified #
# free for personal, government or non-profit. Any commercial use must be     #
# registered. However, modified versions cannot be sold or distributed. You   #
# cannot alter the copyright and "powered by" notices throughout the scripts. #
# These notices must be clearly visible to the end users.                     #
#                                                                             #
# WARRANTY DISCLAIMER:                                                        #
#                                                                             #
# 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.                                           #
###############################################################################

package Source;

sub Index {
  my (%Index);

# =========================================================================== #
# Checking Permission

  my ($Categories, $CategoriesIndex) = &DB::Query (
    TABLE   => "Categories" ,
    SORT    => "POSITION"   ,
    BY      => "A-Z"
  );

  my ($Boards, $BoardsIndex) = &DB::Query (
    TABLE   => "Boards"   ,
    SORT    => "POSITION" ,
    BY      => "A-Z"
  );

  %Index = %{ &Standard::GetBoardsList($Categories, $Boards, $BoardsIndex, "", "", "") };

# =========================================================================== #
# Printing Categories and Boards

  &Standard::Require("UB_Index.src", "$UB::INTERFACES_PATH/$SYSTEM::DEFAULT_THEME/UB_Index.src");
  if ($GENERAL::CATEGORY_ONLY == 1) {
    &Standard::Require("UB_Index_Categories.src", "$UB::INTERFACES_PATH/$SYSTEM::DEFAULT_THEME/UB_Index_Categories.src");
  } else {
    &Standard::Require("UB_Index_Boards.src", "$UB::INTERFACES_PATH/$SYSTEM::DEFAULT_THEME/UB_Index_Boards.src");
    &Standard::Require("UB_Index_Category.src", "$UB::INTERFACES_PATH/$SYSTEM::DEFAULT_THEME/UB_Index_Category.src");
  }

  my %EXTRA = &Standard::ExtraVariables();

  my %INDEX = %EXTRA;

  CATEGORY: for (my $c = 0; $c <= $#{ $Categories }; $c++) {
    my $Category = $Categories->[$c];

    if ( exists $Index { $Category->{'ID'} } ) {
      my $LAST_VISITED = $UB::COOKIES{"C".$Category->{'ID'}."_LAST_VISITED"};

      my %CATEGORY = (
        "CATEGORY.ID"                => $Category->{'ID'              },
        "CATEGORY.TITLE"             => $Category->{'TITLE'           },
        "CATEGORY.DESCRIPTION"       => $Category->{'DESCRIPTION'     },
        "CATEGORY.TOTAL_BOARDS"      => $Category->{'TOTAL_BOARDS'    },
        "CATEGORY.TOTAL_POSTS"       => $Category->{'TOTAL_POSTS'     },
        "CATEGORY.TOTAL_TOPICS"      => $Category->{'TOTAL_TOPICS'    },
        "CATEGORY.LAST_UPDATE_DATE"  => $Category->{'LAST_UPDATE_DATE'},
        "CATEGORY.LAST_UPDATE_TIME"  => $Category->{'LAST_UPDATE_TIME'},

        "CATEGORY.IS_NEW"           => (($LAST_VISITED - (2 * 60 * 60)) < $Category->{'LAST_UPDATE_SECOND'}) ? "1" : "0",

        %EXTRA
      );

      if ($GENERAL::CATEGORY_ONLY != 1) {
        BOARD: for (my $b = 0; $b <= $#{ $Index { $Category->{'ID'} } }; $b++ ) {
          my $Board = $Boards->[ $BoardsIndex->{ $Index { $Category->{'ID'} }->[$b] } ];

          my ($Reply, $Post);

          if ( ( ($UB::MEMBER{'LOGIN'} == 1) && ($UB::MEMBER{'LEVEL'} ne "70") && ($UB::MEMBER{'LEVEL'} ne "100") ) || ($UB::MEMBER{'LOGIN'} == 0)) {
            &Standard::FileOpen(*DATA, "r", "$SYSTEM::DB_PATH/B".$Board->{'ID'}."/REPLY-LEVEL-00.dat");
              my $REPLY_LEVEL_00 = join ("", <DATA>);
            close (DATA);

            if ($REPLY_LEVEL_00 ne "*") {
              $Reply = 0;
              if ($UB::MEMBER{'LOGIN'} == 1) {
                $Reply = 1 if &Standard::GetBoardPermission ($Board->{'ID'}, "REPLY", $UB::MEMBER{'LEVEL'}, $UB::MEMBER{'GROUP'});
              }
            } else {
              $Reply = 1;
            }

            &Standard::FileOpen(*DATA, "r", "$SYSTEM::DB_PATH/B".$Board->{'ID'}."/POST-LEVEL-00.dat");
              my $POST_LEVEL_00 = join ("", <DATA>);
            close (DATA);

            if ($POST_LEVEL_00 ne "*") {
              $Post = 0;
              if ($UB::MEMBER{'LOGIN'} == 1) {
                $Post = 1 if &Standard::GetBoardPermission ($Board->{'ID'}, "POST", $UB::MEMBER{'LEVEL'}, $UB::MEMBER{'GROUP'});
              }
            } else {
              $Post  = 1;
            }
          } else {
            $Reply = 1;
            $Post  = 1;
          }

          &Standard::FileOpen(*HEADER, "r", "$SYSTEM::DB_PATH/B".$Board->{'ID'}."/header.inc");
            my $HEADER = join ("", <HEADER>);
          close (HEADER);

          &Standard::FileOpen(*FOOTER, "r", "$SYSTEM::DB_PATH/B".$Board->{'ID'}."/footer.inc");
            my $FOOTER = join ("", <FOOTER>);
          close (FOOTER);

          #my $LAST_VISITED = $UB::CGI->cookie("B".$Board->{'ID'}."_LAST_UPDATE");
          my $LAST_VISITED = $UB::COOKIES{"B".$Board->{'ID'}."_LAST_VISITED"};

          my %BOARD = (
            "BOARD.ID"                        => $Board->{'ID'                      },
            "BOARD.TITLE"                     => $Board->{'TITLE'                   },
            "BOARD.DESCRIPTION"               => $Board->{'DESCRIPTION'             },
            "BOARD.MAJOR_MODERATOR.USERNAME"  => $Board->{'MAJOR_MODERATOR'         },
            "BOARD.MAJOR_MODERATOR.NICKNAME"  => $Board->{'MAJOR_MODERATOR_NICKNAME'},
            "BOARD.MAJOR_MODERATOR.EMAIL"     => $Board->{'MAJOR_MODERATOR_EMAIL'   },
            "BOARD.TOTAL_POSTS"               => $Board->{'TOTAL_POSTS'             },
            "BOARD.TOTAL_TOPICS"              => $Board->{'TOTAL_TOPICS'            },
            "BOARD.LAST_POST_DATE"            => $Board->{'LAST_POST_DATE'          },
            "BOARD.LAST_POST_TIME"            => $Board->{'LAST_POST_TIME'          },
            "BOARD.LAST_UPDATE_DATE"          => $Board->{'LAST_UPDATE_DATE'        },
            "BOARD.LAST_UPDATE_TIME"          => $Board->{'LAST_UPDATE_TIME'        },

            "BOARD.HEADER"                    => $HEADER                             ,
            "BOARD.FOOTER"                    => $FOOTER                             ,

            "BOARD.READ"                      => 1                                   ,
            "BOARD.REPLY"                     => $Reply                              ,
            "BOARD.POST"                      => $Post                               ,

            "BOARD.IS_NEW"                   => (($LAST_VISITED - (2 * 60 * 60)) < $Board->{'LAST_UPDATE_SECOND'}) ? "1" : "0",

            %EXTRA
          );

          $CATEGORY{'CATEGORY.BOARDS'} .= &Index_Boards::Parse(\%BOARD);
        }

        $INDEX{'INDEX.CATEGORIES'} .= &Index_Category::Parse(\%CATEGORY);
      } else {
        $INDEX{'INDEX.CATEGORIES'} .= &Index_Categories::Parse(\%CATEGORY);
      }
    }
  }

  &Standard::PrintHTMLHeader();
  print &Index::Parse(\%INDEX);
}

###############################################################################
1; # end of UB_Index.src file
###############################################################################