###############################################################################
# UB_DoModifyPost.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 DoModifyPost {
  &Login unless ($UB::MEMBER{'LOGIN'} == 1);

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

  (
    my ($Post) = &DB::BinarySelect (
      TABLE   => "B".$UB::QUERY{'BID'},
      COLUMNS => [
        "ID"              ,
        "SUBJECT"         ,
        "USERNAME"        ,
        "NICKNAME"        ,
        "EMAIL"           ,
        "ROOT"            ,
        "REPLY_NOTIFY"    ,
        "SIGNATURE"       ,
        "SYMBOL"          ,
        "APROVED"         ,
        "READ_ONLY"       ,
        "ATTACHMENT"
      ],
      KEY     => $UB::QUERY{'PID'}
    )
  ) || &HTML::Error(MESSAGE => $DB::Error);

  unless ($Post->{'ROOT'} > 0) {
    $Post->{'ROOT'} = $Post->{'ID'};
  }

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

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

  if ($Post->{'USERNAME'} eq "") {
    &HTML::Error(CODE => "GUEST_MODIFY");
  }

  unless ($UB::MEMBER{'USERNAME'} eq $Post->{'USERNAME'}) {
    &HTML::Error(CODE => "WRONG_MEMBER_MODIFY");
  }

  &Standard::Require("UltraBoard.lib", "$UB::LIBRARIES_PATH/UltraBoard.lib");
  &Standard::Require("Thread.lib", "$UB::LIBRARIES_PATH/Thread.lib");

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

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

  my $FileName = "";
  my ($AttachFile, $tmpAttachFile);
  if ($UB::QUERY{'FORM_ATTACHMENT_ACTION'} eq "UPLOAD") {
    $AttachFile = $UB::CGI->param('FORM_ATTACHMENT');

    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;
    } elsif (($UB::QUERY{'TMP'}) && ($UB::QUERY{'ATTACH'}) && (-e $SYSTEM::TEMP_PATH."/Post-".$Board->{'ID'}."-".$UB::QUERY{'TMP'}."-".$UB::QUERY{'ATTACH'})) {
      $FileName = $UB::QUERY{'ATTACH'};
    }
  }

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

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

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

  $FORM{'LAST_UPDATE_SECOND'} = time;

  $FORM{'LAST_UPDATE_DATE'  } = &Standard::ConvertEpochToDate($FORM{'LAST_UPDATE_SECOND'});
  $FORM{'LAST_UPDATE_TIME'  } = &Standard::ConvertEpochToTime($FORM{'LAST_UPDATE_SECOND'});

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

  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 ($UB::QUERY{'FORM_ATTACHMENT_ACTION'} eq "UPLOAD") {
    $FORM{'ATTACHMENT'}    = $FileName;
  } else {
    $FORM{'ATTACHMENT'}    = "";
  }

# =========================================================================== #
# Censor Words Filter

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

# =========================================================================== #
# Update Message Information from Database

  if ($UB::QUERY{'FORM_ATTACHMENT_ACTION'} eq "UPLOAD") {
    unlink ($SYSTEM::UPLOAD_PATH."/Post-".$Board->{'ID'}."-".$UB::QUERY{'PID'}."-".$Post->{'ATTACHMENT'});

    if (-e $tmpAttachFile) {
      chmod (0777, $SYSTEM::UPLOAD_PATH);
      my $New = $SYSTEM::UPLOAD_PATH."/Post-".$Board->{'ID'}."-".$UB::QUERY{'PID'}."-".$FileName;
      copy ($tmpAttachFile, $New);
      chmod (0777, $New);
    } elsif (($UB::QUERY{'TMP'}) && ($UB::QUERY{'ATTACH'}) && (-e $SYSTEM::TEMP_PATH."/Post-".$Board->{'ID'}."-".$UB::QUERY{'TMP'}."-".$UB::QUERY{'ATTACH'})) {
      chmod (0777, $SYSTEM::TEMP_PATH);
      chmod (0777, $SYSTEM::UPLOAD_PATH);
      my $Old = $SYSTEM::TEMP_PATH."/Post-".$Board->{'ID'}."-".$UB::QUERY{'TMP'}."-".$UB::QUERY{'ATTACH'};
      my $New = $SYSTEM::UPLOAD_PATH."/Post-".$Board->{'ID'}."-".$UB::QUERY{'PID'}."-".$UB::QUERY{'ATTACH'};
      move ($Old, $New);
      chmod (0777, $New);
    }

    close ($AttachFile) if ($AttachFile);

  } elsif ($UB::QUERY{'FORM_ATTACHMENT_ACTION'} eq "KEEP") {
    $FORM{'ATTACHMENT'} = $Post->{'ATTACHMENT'};

  } elsif ($UB::QUERY{'FORM_ATTACHMENT_ACTION'} eq "REMOVE") {
    unlink ($SYSTEM::UPLOAD_PATH."/Post-".$Board->{'ID'}."-".$UB::QUERY{'PID'}."-".$Post->{'ATTACHMENT'});
  }

  (
    &DB::Update (
      TABLE   => "B".$Board->{'ID'},
      VALUES  => {
        LAST_UPDATE_SECOND  => $FORM{'LAST_UPDATE_SECOND'}  ,
        LAST_UPDATE_DATE    => $FORM{'LAST_UPDATE_DATE'}    ,
        LAST_UPDATE_TIME    => $FORM{'LAST_UPDATE_TIME'}    ,
        TOTAL_MODIFIED      => "++\${TOTAL_MODIFIED}"       ,
        HOST                => $FORM{'HOST'}                ,
        IP                  => $FORM{'IP'}                  ,
        REPLY_NOTIFY        => $FORM{'REPLY_NOTIFY'}        ,
        SIGNATURE           => $FORM{'SIGNATURE'}           ,
        ATTACHMENT          => $FORM{'ATTACHMENT'}
      },
      KEY     => $UB::QUERY{'PID'}
    )
  ) || &HTML::Error(MESSAGE => $DB::Error);

  (
    &DB::Update (
      TABLE   => "Categories",
      VALUES  => {
        LAST_UPDATE_SECOND  => $FORM{'LAST_UPDATE_SECOND'}  ,
        LAST_UPDATE_DATE    => $FORM{'LAST_UPDATE_DATE'}    ,
        LAST_UPDATE_TIME    => $FORM{'LAST_UPDATE_TIME'}
      },
      KEY     => $Board->{'CATEGORY'}
    )
  ) || &HTML::Error(MESSAGE => $DB::Error);

  (
    &DB::Update (
      TABLE   => "Boards",
      VALUES  => {
        LAST_UPDATE_SECOND  => $FORM{'LAST_UPDATE_SECOND'}  ,
        LAST_UPDATE_DATE    => $FORM{'LAST_UPDATE_DATE'}    ,
        LAST_UPDATE_TIME    => $FORM{'LAST_UPDATE_TIME'}
      },
      KEY     => $Board->{'ID'}
    )
  ) || &HTML::Error(MESSAGE => $DB::Error);

  my $Counter = 0;
  while (unlink ($SYSTEM::DB_PATH."/B".$Board->{'ID'}."/".$UB::QUERY{'PID'}."-".++$Counter.".html")) {}

  &Standard::FileOpen(*POST, "w", $SYSTEM::DB_PATH."/B".$Board->{'ID'}."/".$UB::QUERY{'PID'}.".src");
    print POST $FORM{'MESSAGE'};
  close (POST);

  $FORM{'MESSAGE'} = &UltraBoard::Encode($FORM{'MESSAGE'});

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

  &Standard::FileOpen(*POST, "w", $SYSTEM::DB_PATH."/B".$Board->{'ID'}."/".$UB::QUERY{'PID'}.".post");
    print POST $FORM{'MESSAGE'};
  close (POST);

# =========================================================================== #
# Printing Topic into Static HTML Page

  &Thread::Build (
    BOARD     => $Board->{'ID'} ,
    THREAD    => $Post->{'ROOT'},
  );

  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'}              || "",

    "TOPIC.ID"          => $Post->{'ROOT'}                || "",

    "POST.ID"           => $Post->{'ID'}                  || "",
    "POST.USERNAME"     => $Post->{'USERNAME'}            || "",
    "POST.NICKNAME"     => $Post->{'NICKNAME'}            || "",
    "POST.EMAIL"        => $Post->{'EMAIL'}               || "",
    "POST.UPDATE_DATE"  => $FORM{'LAST_UPDATE_DATE'}      || "",
    "POST.UPDATE_TIME"  => $FORM{'LAST_UPDATE_TIME'}      || "",
    "POST.IP"           => $FORM{'IP'}                    || "",
    "POST.HOST"         => $FORM{'HOST'}                  || "",
    "POST.SUBJECT"      => $Post->{'SUBJECT'}             || "",
    "POST.MESSAGE"      => $FORM{'MESSAGE'}               || "",
    "POST.ATTACHMENT"   => $FORM{'ATTACHMENT'}            || "",

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

  &Success(\%POST);
}

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

  require "$UB::SOURCES_PATH/UB_ModifyPost.src";
  &Source::ModifyPost($code);

  exit;
}

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

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

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

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