###############################################################################
# UB_DoForward.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 DoForward {
  return if ((!$SYSTEM::SENDMAIL) || (!$GENERAL::FORWARD));

# =========================================================================== #
# 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 $SendMail = 0;
  if ($SYSTEM::SENDMAIL == 1) {
    if ( ($SYSTEM::MAIL_SYSTEM eq "SMTP") && ($SYSTEM::SMTP_PATH ne "") ) {
      $SendMail = &Standard::Require("SMTP.lib", "$UB::LIBRARIES_PATH/SMTP.lib");
    } elsif ( ($SYSTEM::MAIL_SYSTEM eq "Sendmail") && ($SYSTEM::SENDMAIL_PATH ne "") ) {
      $SendMail = &Standard::Require("Sendmail.lib", "$UB::LIBRARIES_PATH/Sendmail.lib");
    }
  }

# =========================================================================== #
# Verify Variables

  # checking missing fields
  if ($UB::QUERY{'FORM_SUBJECT'} eq "") { &Error("MISS_SUBJECT") };
  if ($UB::QUERY{'FORM_MESSAGE'} eq "") { &Error("MISS_MESSAGE") };

  if ($UB::QUERY{'FORM_YOUR_NICKNAME'} eq "") { &Error("MISS_YOUR_NICKNAME") };
  if ($UB::QUERY{'FORM_YOUR_EMAIL'}    eq "") { &Error("MISS_YOUR_EMAIL")    };

  if ($UB::QUERY{'FORM_FRIEND_NICKNAME'} eq "") { &Error("MISS_FRIEND_NICKNAME") };
  if ($UB::QUERY{'FORM_FRIEND_EMAIL'}    eq "") { &Error("MISS_FRIEND_EMAIL")    };

  # checking invalid nickname
  if (
    (length ($UB::QUERY{'FORM_YOUR_NICKNAME'}) > 32)          ||
    (length ($UB::QUERY{'FORM_YOUR_NICKNAME'}) <  2)
  ) {
    &Error("INVALID_NICKNAME");
  }

  # checking invalid email
  if (
    ($UB::QUERY{'FORM_YOUR_EMAIL'} !~ /[\.\w\-]+\@[\.\w\-]+\.[\.\w\-]+/) ||
    (length ($UB::QUERY{'FORM_YOUR_EMAIL'}) > 64)
  ) {
    &Error("INVALID_EMAIL");
  }

  # checking invalid nickname
  if (
    (length ($UB::QUERY{'FORM_FRIEND_NICKNAME'}) > 32)          ||
    (length ($UB::QUERY{'FORM_FRIEND_NICKNAME'}) <  2)
  ) {
    &Error("INVALID_NICKNAME");
  }

  # checking invalid email
  if (
    ($UB::QUERY{'FORM_FRIEND_EMAIL'} !~ /[\.\w\-]+\@[\.\w\-]+\.[\.\w\-]+/) ||
    (length ($UB::QUERY{'FORM_FRIEND_EMAIL'}) > 64)
  ) {
    &Error("INVALID_EMAIL");
  }

  # checking length of fields
  if (length($UB::QUERY{'FORM_SUBJECT'}) > 64) {
    &Error("OVER_SUBJECT");
  }

  if (length($UB::QUERY{'FORM_MESSAGE'}) > 4096) {
    &Error("OVER_MESSAGE");
  }

# =========================================================================== #
# Assign Variables

  my %FORM;

  $FORM{'YOUR_NICKNAME'}    = $UB::QUERY{'FORM_YOUR_NICKNAME'};
  $FORM{'YOUR_NICKNAME'}    = &Standard::EncodeHTML($FORM{'YOUR_NICKNAME'});

  $FORM{'YOUR_EMAIL'}       = lc ($UB::QUERY{'FORM_YOUR_EMAIL'});

  $FORM{'FRIEND_NICKNAME'}  = $UB::QUERY{'FORM_FRIEND_NICKNAME'};
  $FORM{'FRIEND_NICKNAME'}  = &Standard::EncodeHTML($FORM{'FRIEND_NICKNAME'});

  $FORM{'FRIEND_EMAIL'}     = lc ($UB::QUERY{'FORM_FRIEND_EMAIL'});


  $FORM{'SUBJECT'}       = $UB::QUERY{'FORM_SUBJECT'};
  $FORM{'SUBJECT'}       = &Standard::EncodeHTML($FORM{'SUBJECT'});

  $FORM{'MESSAGE'}       = $UB::QUERY{'FORM_MESSAGE'};
  $FORM{'MESSAGE'}       = &Standard::EncodeHTML($FORM{'MESSAGE'});

  ($FORM{'SUBJECT'}, $FORM{'MESSAGE'}) = &Standard::WordFilter($FORM{'SUBJECT'}, $FORM{'MESSAGE'});

# =========================================================================== #
# Mailing to ...

  &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 %FORWARD = (
    "CATEGORY.ID"       => $Category->{'ID'}   ,
    "CATEGORY.TITLE"    => $Category->{'TITLE'},

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

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

    "FORWARD.YOUR.NICKNAME"     => $FORM{'YOUR_NICKNAME'},
    "FORWARD.YOUR.EMAIL"        => $FORM{'YOUR_EMAIL'},
    "FORWARD.FRIEND.NICKNAME"   => $FORM{'FRIEND_NICKNAME'},
    "FORWARD.FRIEND.EMAIL"      => $FORM{'FRIEND_EMAIL'},
    "FORWARD.SUBJECT"           => $FORM{'SUBJECT'},
    "FORWARD.MESSAGE"           => $FORM{'MESSAGE'},

    "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()
  );

  if (($SYSTEM::SENDMAIL == 1) && ($SendMail == 1)) {
    $FORWARD{'FORWARD.MESSAGE'} =~ s/\n\n/<p>/g;
    $FORWARD{'FORWARD.MESSAGE'} =~ s/\n/<br>/g;

    &Mail::Send (
      SUBJECT => $FORWARD{'FORWARD.SUBJECT'},
      MESSAGE => $FORWARD{'FORWARD.MESSAGE'},
      FROM    => "$FORM{'YOUR_NICKNAME'} <$FORM{'YOUR_EMAIL'}>",
      TO      => "$FORM{'FRIEND_NICKNAME'} <$FORM{'FRIEND_EMAIL'}>"
    );
  }

  &Success(\%FORWARD);
}

###############################################################################
# Error function
# - function of printing error message of forward topic
sub Error {
  my ($code) = @_;

  require "$UB::SOURCES_PATH/UB_Forward.src";
  &Source::Forward($code);

  exit;
}

###############################################################################
# Success function
# - function of printing success message of forward topic
sub Success {
  my ($POST) = @_;

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

  &Standard::PrintHTMLHeader();
  print &Forward_Success::Parse($POST);
  exit;
}

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

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