###############################################################################
# UB_Reply.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 Reply {
  my ($ERROR) = @_;

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

  (
    my ($Board) = &DB::BinarySelect (
      TABLE   => "Boards",
      COLUMNS => ["ID", "TITLE", "CATEGORY", "STATUS", "UB_CODES", "HTML_TAGS", "FILE_ATTACH"],
      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);

  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 $POST_LEVEL_00 = join ("", <DATA>);
      close (DATA);

      if ($POST_LEVEL_00 ne "*") {
        if ($UB::MEMBER{'LOGIN'} == 1) {
          &Login unless &Standard::GetBoardPermission ($Board->{'ID'}, "REPLY", $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 ($Topic) = &DB::BinarySelect (
      TABLE   => "B".$Board->{'ID'},
      COLUMNS => [
        "ID"              ,
        "SUBJECT"         ,
        "USERNAME"        ,
        "NICKNAME"        ,
        "EMAIL"           ,
        "MEMBER_LEVEL"    ,
        "MEMBER_GROUP"    ,
        "NUMBER_REPLIES"  ,
        "POST_DATE"       ,
        "POST_TIME"       ,
        "LAST_UPDATE_DATE",
        "LAST_UPDATE_TIME",
        "TOTAL_MODIFIED"  ,
        "HOST"            ,
        "IP"              ,
        "SYMBOL"          ,
        "APROVED"         ,
        "READ_ONLY"
      ],
      KEY     => $UB::QUERY{'TID'}
    )
  ) || &HTML::Error(MESSAGE => $DB::Error);

  &Standard::FileOpen(*POST, "r", $SYSTEM::DB_PATH."/B".$Board->{'ID'}."/".$Topic->{'ID'}.".src");
    $Topic->{'MESSAGE'} = join ("", <POST>);
  close (POST);

  if ($Topic->{'APROVED'} != 1) {
    &HTML::Error(CODE => "POST_NOT_APPROVED");
  }

  if ($Topic->{'READ_ONLY'} == 1) {
    &HTML::Error(CODE => "POST_READ_ONLY");
  }

  my $Post;
  if (($UB::QUERY{'PID'}) && ($UB::QUERY{'PID'} ne $UB::QUERY{'TID'})) {
    (
      ($Post) = &DB::BinarySelect (
        TABLE   => "B".$Board->{'ID'},
        COLUMNS => [
          "ID"              ,
          "NICKNAME"        ,
          "SUBJECT"         ,
          "LAST_UPDATE_DATE",
          "LAST_UPDATE_TIME",
          "ROOT"            ,
          "APROVED"
        ],
        KEY     => $UB::QUERY{'PID'}
      )
    ) || &HTML::Error(MESSAGE => $DB::Error);

    &Standard::FileOpen(*POST, "r", $SYSTEM::DB_PATH."/B".$Board->{'ID'}."/".$Post->{'ID'}.".src");
      $Post->{'MESSAGE'} = join ("", <POST>);
    close (POST);

    if ($Post->{'APROVED'} != 1) {
      &HTML::Error(CODE => "POST_NOT_APPROVED");
    }

    if ($Post->{'ROOT'} != $UB::QUERY{'TID'}) {
      $Post = {};
    }
  }

# =========================================================================== #
# Printing Reply Form

  &Standard::Require("UB_Reply.src", "$UB::INTERFACES_PATH/$SYSTEM::DEFAULT_THEME/UB_Reply.src");

  if ($UB::QUERY{'PID'}) {
    if ($UB::QUERY{'PID'} eq $UB::QUERY{'TID'}) {
      $Post->{'MESSAGE'} = "[quote][b]$Topic->{'NICKNAME'} ($Topic->{'LAST_UPDATE_DATE'} $Topic->{'LAST_UPDATE_TIME'}):[/b]\n".$Topic->{'MESSAGE'}."[/quote]\n\n";
    } else {
      $Post->{'MESSAGE'} = "[quote][b]$Post->{'NICKNAME'} ($Post->{'LAST_UPDATE_DATE'} $Post->{'LAST_UPDATE_TIME'}):[/b]\n".$Post->{'MESSAGE'}."[/quote]\n\n";
    }
  }

  &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 $BoardOption = &Standard::GetBoardsList("", "", "", 1, "", $Board->{'ID'});

  my %FORM = (
    "CATEGORY.ID"       => $Category->{'ID'}      ,
    "CATEGORY.TITLE"    => $Category->{'TITLE'}   ,

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

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

    "TOPIC.ID"          => $UB::QUERY{'TID'}      ,
    "TOPIC.USERNAME"    => $Topic->{'USERNAME'}   ,
    "TOPIC.NICKNAME"    => $Topic->{'NICKNAME'}   ,
    "TOPIC.LEVEL"       => $Topic->{'LEVEL'}      ,
    "TOPIC.GROUP"       => $Topic->{'GROUP'}      ,
    "TOPIC.EMAIL"       => $Topic->{'EMAIL'}      ,
    "TOPIC.DATE"        => $Topic->{'DATE'}       ,
    "TOPIC.TIME"        => $Topic->{'TIME'}       ,
    "TOPIC.IP"          => $Topic->{'IP'}         ,
    "TOPIC.HOST"        => $Topic->{'HOST'}       ,
    "TOPIC.SUBJECT"     => $Topic->{'SUBJECT'}    ,
    "TOPIC.MESSAGE"     => $Topic->{'MESSAGE'}    ,

    "PAGE"              => $UB::QUERY{'P'}        ,

    "POST.ID"           => $UB::QUERY{'PID'}      ,

    "SHOW.BOARDS.LIST"  => 1                      ,
    "BOARDS.LIST"       => $BoardOption,

    "ADDRESS"           => "<a href=\"$SYSTEM::UB_URL/UltraBoard.$SYSTEM::EXT?action=Category&CID=".$Category->{'ID'}."&SID=".$UB::MEMBER{'SESSION'}."\" style=\"color:$UB::STYLE{'NORMAL_TEXT_COLOR'}\">".$Category-> {'TITLE'}."</a>".
                           " / ".
                           "<a href=\"$SYSTEM::UB_URL/UltraBoard.$SYSTEM::EXT?action=Headlines&BID=".$Board->{'ID'}."&SID=".$UB::MEMBER{'SESSION'}."\" style=\"color:$UB::STYLE{'NORMAL_TEXT_COLOR'}\">".$Board-> {'TITLE'}."</a>",

    &Standard::ExtraVariables()
  );

  $FORM{'FORM.NICKNAME'}    = $UB::QUERY{'FORM_NICKNAME'}    || $UB::MEMBER{'NICKNAME'} || $UB::COOKIES{'NICKNAME'}; #|| $UB::CGI->cookie('NICKNAME');
  $FORM{'FORM.EMAIL'}       = $UB::QUERY{'FORM_EMAIL'}       || $UB::MEMBER{'EMAIL'}    || $UB::COOKIES{'EMAIL'}   ; #|| $UB::CGI->cookie('EMAIL');
  $FORM{'FORM.SUBJECT'}     = $UB::QUERY{'FORM_SUBJECT'}     || "Re:".($Post->{'SUBJECT'} || $Topic->{'SUBJECT'});
  $FORM{'FORM.MESSAGE'}     = $UB::QUERY{'FORM_MESSAGE'}     || $Post->{'MESSAGE'} || "";

  if (length ($FORM{'FORM.SUBJECT'}) > $GENERAL::MAX_SUBJECT_LENGTH) {
    $FORM{'FORM.SUBJECT'} = substr ($FORM{'FORM.SUBJECT'}, 0, $GENERAL::MAX_SUBJECT_LENGTH);
  }
  $FORM{'FORM.SUBJECT'} =~ s/\"/&quot;/g;

  $FORM{'FORM.CHECKBOX.EMAIL_NOTIFY'}      = &HTML::Checkbox (
    NAME    => "FORM_EMAIL_NOTIFY"               ,
    VALUE   => "1"                               ,
    CHECKED => $UB::QUERY{'FORM_EMAIL_NOTIFY'} || ""
  );
  $FORM{'FORM.CHECKBOX.SIGNATURE_INCLUDE'} = &HTML::Checkbox (
    NAME    => "FORM_SIGNATURE_INCLUDE"          ,
    VALUE   => "1"                               ,
    CHECKED => $UB::QUERY{'FORM_SIGNATURE_INCLUDE'} || ""
  );

  if (($UB::MEMBER{'LOGIN'} == 1) && ($Board->{'FILE_ATTACH'} == 1)) {
    my $Group;
    if (($UB::MEMBER{'LEVEL'} ne "70") && ($UB::MEMBER{'LEVEL'} ne "100")) {
      ($Group) = &DB::BinarySelect (
        TABLE   => "Groups",
        COLUMNS => ["ID", "MAX_FILE_SIZE"],
        KEY     => $UB::MEMBER{'GROUP'}
      );
    }
    if ((($UB::MEMBER{'LEVEL'} eq "70") || ($UB::MEMBER{'LEVEL'} eq "100")) || ($Group->{'MAX_FILE_SIZE'} > 0)) {
      if ($ENV{'HTTP_USER_AGENT'} =~/Mozilla\/(\d+)/) {
        my $Version = $1;
        if ($Version >= 4) {
          $FORM{'FORM.FILE_ATTACH'} = 1;
        }
      }
    }
  }

  if ($UB::STYLE{'MESSGAE_ICONS'}) {
    $FORM{'FORM.SYMBOL'} .= "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr>";

    $FORM{'FORM.SYMBOL'} .= "<td width=\"20\">";
    $FORM{'FORM.SYMBOL'} .= &HTML::Radio (
      NAME    => "FORM_SYMBOL"  ,
      VALUE   => "Default"      ,
      CHECKED => $UB::QUERY{'FORM_SYMBOL'} || "Default"
    );
    $FORM{'FORM.SYMBOL'} .= "</td><td width=\"20\">";

    if ($UB::STYLE{'ICONS'}->{'Default'}) {
      my $URL = $UB::STYLE{'ICONS'}->{'Default'};
         $URL = $SYSTEM::IMAGE_URL."/".$SYSTEM::DEFAULT_THEME."/".$URL unless $URL =~ /^http:\/\//;
      $FORM{'FORM.SYMBOL'} .= "<img src=\"$URL\" border=\"0\" alt=\"Default\">";
    }
    $FORM{'FORM.SYMBOL'} .= "</td>";

    my $COUNTER = 1;
    foreach my $SYMBOL (sort keys %{ $UB::STYLE{'ICONS'} }) {
      next if $SYMBOL eq "Default";

      $FORM{'FORM.SYMBOL'} .= "</tr><tr>" if $COUNTER >= $UB::STYLE{'ICONS_PER_ROW'};

      if ($UB::STYLE{'ICONS'}->{ $SYMBOL }) {
        $FORM{'FORM.SYMBOL'} .= "<td width=\"20\">";
        $FORM{'FORM.SYMBOL'} .= &HTML::Radio (
          NAME    => "FORM_SYMBOL"  ,
          VALUE   => "$SYMBOL"      ,
          CHECKED => $UB::QUERY{'FORM_SYMBOL'} || "Default"
        );
        $FORM{'FORM.SYMBOL'} .= "</td><td width=\"20\">";

        my $URL = $UB::STYLE{'ICONS'}->{ $SYMBOL };
           $URL = $SYSTEM::IMAGE_URL."/".$SYSTEM::DEFAULT_THEME."/".$URL unless $URL =~ /^http:\/\//;
        $FORM{'FORM.SYMBOL'} .= "<img src=\"$URL\" border=\"0\" alt=\"$SYMBOL\">";
        $FORM{'FORM.SYMBOL'} .= "</td>";
      }
    }

    $FORM{'FORM.SYMBOL'} .= "<td>&nbsp;</td><td>&nbsp;</td>" x ($UB::STYLE{'ICONS_PER_ROW'} * $COUNTER + 1);

    $FORM{'FORM.SYMBOL'} .= "</tr></table>";
  }

  $FORM{'ERROR.MESSAGE'} = ((exists $Reply::DYNAMIC{"LANGUAGE.".$ERROR}) ? $Reply::DYNAMIC{"LANGUAGE.".$ERROR} : $ERROR);

  &Standard::PrintHTMLHeader();
  print &Reply::Parse(\%FORM);
}

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

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