###############################################################################
# UB_Preview.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;
use File::Copy;

sub Preview {
# =========================================================================== #
# Checking Permission

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

  &Standard::Require("UltraBoard.lib", "$UB::LIBRARIES_PATH/UltraBoard.lib") if ($Board->{'UB_CODES'} == 1);

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

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

  if ($UB::MEMBER{'LOGIN'} != 1) {
    if ($UB::QUERY{'FORM_NICKNAME'} eq "") { &Error("MISS_NICKNAME") };
    if ($UB::QUERY{'FORM_EMAIL'}    eq "") { &Error("MISS_EMAIL")    };

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

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

  my $FileName = "";
  my ($AttachFile, $tmpAttachFile);
  $AttachFile = $UB::QUERY{'FORM_ATTACH'};
  if (($AttachFile) && ($UB::MEMBER{'LOGIN'} == 1) && ($Board->{'FILE_ATTACH'} == 1)) {
    my $Type = $UB::CGI->uploadInfo($AttachFile)->{'Content-Type'};

    $tmpAttachFile = $UB::CGI->tmpFileName($AttachFile);

    my $Found = 0;
    foreach (@GENERAL::ATTACH_TYPE) {
      if (/^$Type$/i) {
        $Found = 1;
      }
    }
    if ($Found == 0) {
      &Error("INVALID_ATTACH");
    }

    my ($Size) = (stat ($tmpAttachFile))[7];

    (
      my ($Group) = &DB::BinarySelect (
        TABLE   => "Groups",
        COLUMNS => ["ID", "MAX_FILE_SIZE"],
        KEY     => $UB::MEMBER{'GROUP'}
      )
    ) || &HTML::Error(MESSAGE => $DB::Error);

    if ($Size > ($Group->{'MAX_FILE_SIZE'} * 1024)) {
      &Error("INVALID_ATTACH");
    }

    $AttachFile =~ /([^\\\/]+)$/;
    $FileName = $1;
  }

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

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

# =========================================================================== #
# Assign Variables
  my %FORM;

  if ($UB::MEMBER{'LOGIN'} != 1) {
    $FORM{'NICKNAME'}    = $UB::QUERY{'FORM_NICKNAME'};
    $FORM{'NICKNAME'}    = &Standard::EncodeHTML($FORM{'NICKNAME'});

    $FORM{'EMAIL'}       = lc ($UB::QUERY{'FORM_EMAIL'});
  } else {
    $FORM{'USERNAME'}    = $UB::MEMBER{'USERNAME'};

    $FORM{'NICKNAME'}    = $UB::MEMBER{'NICKNAME'};

    unless ($UB::MEMBER{'HIDE_EMAIL'} == 1) {
      $FORM{'EMAIL'}       = lc ($UB::MEMBER{'EMAIL'});
    }

    $FORM{'MEMBER_LEVEL'} = $UB::MEMBER{'LEVEL_TEXT'};
    $FORM{'MEMBER_GROUP'} = $UB::MEMBER{'GROUP_TEXT'};
  }

  $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{'POST_SECOND'}   = time;

  $FORM{'POST_DATE'}     = &Standard::ConvertEpochToDate($FORM{'POST_SECOND'});
  $FORM{'POST_TIME'}     = &Standard::ConvertEpochToTime($FORM{'POST_SECOND'});

  $FORM{'LAST_POST_SECOND'}   = $FORM{'POST_SECOND'};
  $FORM{'LAST_POST_DATE'  }   = $FORM{ 'POST_DATE' };
  $FORM{'LAST_POST_TIME'  }   = $FORM{ 'POST_TIME' };

  $FORM{'LAST_UPDATE_SECOND'} = $FORM{'POST_SECOND'};
  $FORM{'LAST_UPDATE_DATE'  } = $FORM{ 'POST_DATE' };
  $FORM{'LAST_UPDATE_TIME'  } = $FORM{ 'POST_TIME' };

  if (
    ($UB::QUERY{'FORM_SIGNATURE_INCLUDE'} eq  "1") ||
    ($UB::QUERY{'FORM_SIGNATURE_INCLUDE'} eq "on")
  ) {
    $FORM{'SIGNATURE'}  = 1;
  } else {
    $FORM{'SIGNATURE'}  = 0;
  }

  $FORM{'IP'}         = $ENV{'REMOTE_ADDR'};

  $FORM{'HOST'}       = $ENV{'REMOTE_HOST'};

  if ($Board->{'APROVED'} == 1) {
    $FORM{'APROVED'}  = 0;
  } else {
    $FORM{'APROVED'}  = 1;
  }

  $FORM{'ATTACHMENT_ACTION'} = $UB::QUERY{'FORM_ATTACHMENT_ACTION'};

  if ($FileName) {
    $FORM{'ATTACHMENT'}   = $FileName;
  }

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

# =========================================================================== #
# Add Topic to Database

  my $time = time;

  if ($Board->{'UB_CODES'} == 1) {
    $FORM{'MESSAGE'} = &UltraBoard::Encode($FORM{'MESSAGE'}, 0, $Board->{'HTML_TAGS'}, $Board->{'ID'}, "", $time);
  }

  if (($FORM{'SIGNATURE'} == 1) && ($UB::MEMBER{'LOGIN'} == 1)) {
    my ($Profile) = &DB::BinarySelect (
      TABLE   => "Profiles",
      COLUMNS => ["USERNAME", "SIGNATURE"],
      KEY     => $UB::MEMBER{'USERNAME'}
    );
    if (length ($Profile->{'SIGNATURE'}) > 1) {
      $Profile->{'SIGNATURE'}  = &UltraBoard::Encode($Profile->{'SIGNATURE'}, 1);
      $FORM{'MESSAGE'} .= qq~\n<!-- SIGNATURE -->\n<!-- hr --><hr><!-- hr -->\n$Profile->{'SIGNATURE'}\n<!-- SIGNATURE -->~;
    }
  }

  if (-e $tmpAttachFile) {
    copy ($tmpAttachFile, $SYSTEM::TEMP_PATH."/Post-".$Board->{'ID'}."-".$time."-".$FileName);
  }

  close ($AttachFile) if ($AttachFile);

# =========================================================================== #

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

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

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

    "TOPIC.ID"          => $UB::QUERY{'TID'}              ,
    "TMP.ID"            => $time                          ,
    "POST.ID"           => $UB::QUERY{'PID'}              ,
    "POST.USERNAME"     => $FORM{'USERNAME'}              ,
    "POST.NICKNAME"     => $FORM{'NICKNAME'}              ,
    "POST.LEVEL"        => lc $UB::MEMBER{'LEVEL_TEXT'}   ,
    "POST.GROUP"        => lc $UB::MEMBER{'GROUP_TEXT'}   ,
    "POST.EMAIL"        => $FORM{'EMAIL'}                 ,
    "POST.DATE"         => $FORM{'POST_DATE'}             ,
    "POST.TIME"         => $FORM{'POST_TIME'}             ,
    "POST.IP"           => $FORM{'IP'}                    ,
    "POST.HOST"         => $FORM{'HOST'}                  ,
    "POST.SUBJECT"      => $FORM{'SUBJECT'}               ,
    "POST.MESSAGE"      => $FORM{'MESSAGE'}               ,
    "POST.ATTACHMENT_ACTION"
                        => $FORM{'ATTACHMENT_ACTION'}     ,
    "POST.ATTACHMENT"   => $FORM{'ATTACHMENT'}            ,
    "POST.SYMBOL"       => $UB::QUERY{'FORM_SYMBOL'}      ,

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

  $POST{'ACTION'}                 = $UB::QUERY{'action'}                 ;

  $POST{'FORM.NICKNAME'}          = $UB::QUERY{'FORM_NICKNAME'}          ;
  $POST{'FORM.EMAIL'}             = $UB::QUERY{'FORM_EMAIL'}             ;
  $POST{'FORM.SUBJECT'}           = $UB::QUERY{'FORM_SUBJECT'}           ;
  $POST{'FORM.MESSAGE'}           = $UB::QUERY{'FORM_MESSAGE'}           ;
  $POST{'FORM.EMAIL_NOTIFY'}      = $UB::QUERY{'FORM_EMAIL_NOTIFY'}      ;
  $POST{'FORM.SIGNATURE_INCLUDE'} = $UB::QUERY{'FORM_SIGNATURE_INCLUDE'} ;
  $POST{'FORM.SYMBOL'}            = $UB::QUERY{'FORM_SYMBOL'}            ;
  $POST{'FORM.ATTACHMENT_ACTION'} = $UB::QUERY{'FORM_ATTACHMENT_ACTION'} ;

  $POST{'FORM.NICKNAME'} =~s/\"/&quot;/g;
  $POST{'FORM.NICKNAME'} =~s/\>/\\\&gt;/g;
  $POST{'FORM.NICKNAME'} =~s/\</&lt;/g;

  $POST{'FORM.EMAIL'} =~s/\"/&quot;/g;
  $POST{'FORM.EMAIL'} =~s/\>/&gt;/g;
  $POST{'FORM.EMAIL'} =~s/\</&lt;/g;

  $POST{'FORM.SUBJECT'} =~s/\"/&quot;/g;
  $POST{'FORM.SUBJECT'} =~s/\>/&gt;/g;
  $POST{'FORM.SUBJECT'} =~s/\</&lt;/g;

  $POST{'FORM.MESSAGE'} =~s/\"/&quot;/g;
  $POST{'FORM.MESSAGE'} =~s/\>/&gt;/g;
  $POST{'FORM.MESSAGE'} =~s/\</&lt;/g;

  $POST{'FORM.EMAIL_NOTIFY'} =~s/\"/&quot;/g;
  $POST{'FORM.EMAIL_NOTIFY'} =~s/\>/&gt;/g;
  $POST{'FORM.EMAIL_NOTIFY'} =~s/\</&lt;/g;

  $POST{'FORM.SIGNATURE_INCLUDE'} =~s/\"/&quot;/g;
  $POST{'FORM.SIGNATURE_INCLUDE'} =~s/\>/&gt;/g;
  $POST{'FORM.SIGNATURE_INCLUDE'} =~s/\</&lt;/g;

  &Show(\%POST);
}

###############################################################################
# Error function
# - function of printing error message of creating members account
sub Error {
  my ($code) = @_;

  require "$UB::SOURCES_PATH/UB_Post.src";
  &Source::Post($code);

  exit;
}

###############################################################################
# Show function
# - function for previewing the post
sub Show {
  my ($POST) = @_;

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

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

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