###############################################################################
# UB_Read.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;

use strict;

sub Read {

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

  (
    my ($Board) = &DB::BinarySelect (
      TABLE   => "Boards",
      COLUMNS => ["ID", "TITLE", "CATEGORY", "STATUS"],
      KEY     => $UB::QUERY{'BID'}
    )
  ) || &HTML::Error(MESSAGE => $DB::Error);

  (
    my ($Category) = &DB::BinarySelect (
      TABLE   => "Categories",
      COLUMNS => ["ID", "TITLE"],
      KEY     => $Board->{'CATEGORY'}
    )
  ) || &HTML::Error(MESSAGE => $DB::Error);

  (
    my ($Topic) = &DB::BinarySelect (
      TABLE   => "B".$Board->{'ID'},
      COLUMNS => ["ID", "READ_ONLY"],
      KEY     => $UB::QUERY{'TID'}
    )
  ) || &HTML::Error (CODE => "POST_NOT_FOUND");

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

      if ($READ_LEVEL_00 ne "*") {
        if ($UB::MEMBER{'LOGIN'} == 1) {
          &Login unless &Standard::GetBoardPermission ($Board->{'ID'}, "READ", $UB::MEMBER{'LEVEL'}, $UB::MEMBER{'GROUP'});
        } else {
          &Login;
        }
      }
    } elsif ($Board->{'STATUS'} == 30) {
      &HTML::Error(CODE => "BOARD_INACTIVATE");
    } elsif ($Board->{'STATUS'} != 70) {
      &HTML::Error(CODE => "BOARD_INACTIVATE");
    }
  }

  my ($Reply, $Post);

  if ( ( ($UB::MEMBER{'LOGIN'} == 1) && ($UB::MEMBER{'LEVEL'} ne "70") && ($UB::MEMBER{'LEVEL'} ne "100") ) || ($UB::MEMBER{'LOGIN'} == 0)) {
    if (($Board->{'STATUS'} == 50) || ($Board->{'STATUS'} == 70)) {
      &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;
      }
    }
  } elsif (($UB::MEMBER{'LEVEL'} ne "70") || ($UB::MEMBER{'LEVEL'} ne "100")) {
    $Reply = 1;
    $Post  = 1;
  }

  if ($Topic->{'READ_ONLY'} == 1) {
    $Reply = 0;
  }

# =========================================================================== #
# Printing Topic

  my $Page = 1;

  if ((!$UB::QUERY{'P'}) && ($UB::CGI->cookie ("T".$Board->{'ID'}."-".$UB::QUERY{'TID'}."-P"))) {
    $Page = $UB::CGI->cookie ("T".$Board->{'ID'}."-".$UB::QUERY{'TID'}."-P");
  } else {
    $Page = $UB::QUERY{'P'} if $UB::QUERY{'P'};
  }

  $Page = 1 if $Page < 1;

  push (
    @UB::TIDS,
    $UB::CGI->cookie (
      -name     => "T".$Board->{'ID'}."-".$UB::QUERY{'TID'}."-P",
      -value    => $Page,
      -expires  => '+7d'
    )
  );

  if ($UB::QUERY{'action'} eq "Read") {
    unless (-e $SYSTEM::DB_PATH."/B".$UB::QUERY{'BID'}."/".$UB::QUERY{'TID'}."-".$Page.".topic") {
      &Standard::Require($UB::QUERY{'TID'}."-1.topic", $SYSTEM::DB_PATH."/B".$UB::QUERY{'BID'}."/".$UB::QUERY{'TID'}."-1.topic");
    } elsif (-e $SYSTEM::DB_PATH."/B".$UB::QUERY{'BID'}."/".$UB::QUERY{'TID'}."-".$Page.".topic") {
      &Standard::Require($UB::QUERY{'TID'}."-".$Page.".topic", $SYSTEM::DB_PATH."/B".$UB::QUERY{'BID'}."/".$UB::QUERY{'TID'}."-".$Page.".topic");
    } else {
      &HTML::Error (CODE => "POST_NOT_FOUND");
    }
  } elsif ($UB::QUERY{'action'} eq "Print") {
    if (-e $SYSTEM::DB_PATH."/B".$UB::QUERY{'BID'}."/".$UB::QUERY{'TID'}.".print") {
      &Standard::Require($UB::QUERY{'TID'}.".print", $SYSTEM::DB_PATH."/B".$UB::QUERY{'BID'}."/".$UB::QUERY{'TID'}.".print");
    } else {
      &HTML::Error (CODE => "POST_NOT_FOUND");
    }
  }

  my $Topic = &DB::Update (
    TABLE  => "B".$Board->{'ID'},
    VALUES => {
      HITS => "\${HITS} + 1"
    },
    KEY    => $UB::QUERY{'TID'}
  );

  my $time = time;
  push (
    @UB::TIDS,
    $UB::CGI->cookie (
      -name     => "T".$Board->{'ID'}."-".$UB::QUERY{'TID'},
      -value    => $time,
      -expires  => '+25h'
    )
  );

  my $BoardOption = &Standard::GetBoardsList("", "", "", 1, "", $Board->{'ID'});

  my %TOPIC = (
    "CATEGORY.ID"       => $Category->{'ID'}      ,
    "CATEGORY.TITLE"    => $Category->{'TITLE'} . @UB::TIDS,

    "BOARD.ID"          => $Board->{'ID'}         ,
    "BOARD.TITLE"       => $Board->{'TITLE'}      ,

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

    "TOPIC_VISITED"     => $time                  ,
    "TOPIC.HITS"        => $Topic->{'HITS'}       ,

    "BOARDS.LIST"       => $BoardOption           ,

    &Standard::ExtraVariables()
  );

  &Standard::PrintHTMLHeader();
  print &Topic::Parse(\%TOPIC);
}

sub Login {
  print "Location: $SYSTEM::UB_URL/UltraBoard.$SYSTEM::EXT?action=Login&refer=Read&BID=".$UB::QUERY{'BID'}."&TID=".$UB::QUERY{'TID'}."\n\n";
}

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