###############################################################################
# CP_DoModifyMessage.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 DoModifyMessage {
  my ($BID, $PID) = ($UB::QUERY{'BID'}, $UB::QUERY{'PID'});

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

  # checking missing fields
  if ($UB::QUERY{'FORM_SUBJECT'} eq "") { &Error("You forgot to fill in the \"Subject\" field.") };
  if ($UB::QUERY{'FORM_MESSAGE'} eq "") { &Error("You forgot to fill in the \"Message\" field.") };

  # checking length of fields
  if (length($UB::QUERY{'FORM_SUBJECT'}) > $GENERAL::MAX_SUBJECT_LENGTH) {
    &HTML::Error ("Messages Management / Manage Messages / Modify Message", "The \"Subject\" field cannot more than \$GENERAL::MAX_SUBJECT_LENGTH characters.");
  }

  if (length($UB::QUERY{'FORM_MESSAGE'}) > $GENERAL::MAX_MESSAGE_LENGTH) {
    &HTML::Error ("Messages Management / Manage Messages / Modify Message", "The \"Message\" field cannot more than \$GENERAL::MAX_MESSAGE_LENGTH characters.");
  }

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

    if ($AttachFile) {
      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) {
        &HTML::Error ("Messages Management / Manage Messages / Modify Message", "The attachment was invalid, maybe your file was formatting correctly and/or the size was too larger.");
      }

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

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

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

  $FORM{'USERNAME'}      = $UB::QUERY{'UID'};

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

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

  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;
  }

  if ($UB::QUERY{'FORM_ATTACHMENT_ACTION'} eq "UPLOAD") {
    $FORM{'ATTACHMENT'}    = $FileName;
  } else {
    $FORM{'ATTACHMENT'}    = "";
  }

  $FORM{'SYMBOL'} = $UB::QUERY{'FORM_SYMBOL'};

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

  my ($Post) = &DB::BinarySelect (
    TABLE   => "B".$BID,
    COLUMNS => ["ID", "ATTACHMENT"],
    KEY     => $PID
  );

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

    copy ($tmpAttachFile, $SYSTEM::UPLOAD_PATH."/Post-".$BID."-".$PID."-".$FileName);
    close ($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-".$BID."-".$PID."-".$Post->{'ATTACHMENT'});
  }

  (
    &DB::Update (
      TABLE   => "B".$BID,
      VALUES  => {
        SUBJECT             => $FORM{'SUBJECT'}             ,
        ATTACHMENT          => $FORM{'ATTACHMENT'}          ,
        REPLY_NOTIFY        => $FORM{'REPLY_NOTIFY'}        ,
        SIGNATURE           => $FORM{'SIGNATURE'}
      },
      KEY     => $PID
    )
  ) || &HTML::Error ("Internal Database Error", $DB::Error);

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

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

  if (($FORM{'SIGNATURE'} == 1) && ($FORM{'USERNAME'})) {
    my ($Profile) = &DB::BinarySelect (
      TABLE   => "Profiles",
      COLUMNS => ["USERNAME", "SIGNATURE"],
      KEY     => $FORM{'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".$BID."/".$UB::QUERY{'PID'}.".post");
    print POST $FORM{'MESSAGE'};
  close (POST);

  &Thread::Build (
    BOARD     => $BID,
    THREAD    => $PID,
  );

  &Success(\%FORM);
}

###############################################################################
# Success function
# - function of printing success message of modifying members account
sub Success {
  my ($FORM) = @_;

  if (($FORM->{'ATTACHMENT'}) && (($UB::QUERY{'FORM_ATTACHMENT_ACTION'} eq "UPLOAD") || ($UB::QUERY{'FORM_ATTACHMENT_ACTION'} eq "KEEP"))) {
    $FORM->{'ATTACHMENT'} = "<a href=\"$SYSTEM::UPLOAD_URL/Post-".$UB::QUERY{'BID'}."-".$UB::QUERY{'PID'}."-$FORM->{'ATTACHMENT'}\" target=\"ATTACHMENT\">$FORM->{'ATTACHMENT'}</a><br>";
  } elsif (($UB::QUERY{'FORM_ATTACHMENT_ACTION'} ne "UPLOAD") && ($UB::QUERY{'FORM_ATTACHMENT_ACTION'} ne "KEEP")) {
    $FORM->{'ATTACHMENT'} = "n/a";
  }

  &Standard::PrintHTMLHeader();
  print &HTML::Header;
  print<<HTML;
<table width="100%" border="0" cellspacing="0" cellpadding="5">
  <tr bgcolor="Gray">
    <td><font color="White"><b>
      <a href="$SYSTEM::CP_URL/Panel.$SYSTEM::EXT?action=body" target="UBBODY" class="address">Control Panel</a> / Messages Management / Manage Messages / Modify Message
    </b></font></td>
  </tr>
</table>
<table width="100%" border="0" cellspacing="0" cellpadding="3">
  <tr>
    <td>
      <table width="100%" border="0" cellspacing="0" cellpadding="2">
        <tr>
          <td>
            <table width="100%" border="1" cellspacing="0" cellpadding="3" bordercolordark="White" bordercolorlight="Gray">
              <tr>
                <td>
                  <table width="100%" border="0" cellspacing="0" cellpadding="2">
                    <tr>
                      <td>The message has been modified, following is the information of this message.</td>
                    </tr>
                  </table>
                </td>
              </tr>
            </table>
            &nbsp;<br>
            &nbsp;&nbsp;General Information
            <table width="100%" border="1" cellspacing="0" cellpadding="3" bordercolordark="White" bordercolorlight="Gray">
              <tr>
                <td>
                  <table width="100%" border="0" cellspacing="0" cellpadding="2">
                    <tr>
                      <td width="100" valign="top" nowrap>Subject</td>
                      <td width="100%">$FORM->{'SUBJECT'}</td>
                    </tr>
                    <tr>
                      <td width="100" valign="top" nowrap>Message</td>
                      <td width="100%">$FORM->{'MESSAGE'}</td>
                    </tr>
                    <tr>
                      <td width="100" valign="top" nowrap>Attachment</td>
                      <td width="100%">$FORM->{'ATTACHMENT'}</td>
                    </tr>
                  </table>
                </td>
              </tr>
            </table>
          </td>
        </tr>
      </table>
    </td>
  </tr>
</table>
</body>
</html>
HTML

  exit;
}

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