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

  &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"           ,
        "REPLY_NOTIFY"    ,
        "SIGNATURE"       ,
        "SYMBOL"          ,
        "APROVED"         ,
        "READ_ONLY"       ,
        "ATTACHMENT"
      ],
      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->{'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");
  }

# =========================================================================== #
# Printing Modify Post Form

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

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

    "POST.ID"           => $Post->{'ID'},

    "SHOW.BOARDS.LIST"  => 1                     ,
    "BOARDS.LIST"       => &Standard::GetBoardsList("", "", "", 1, "", $Board->{'ID'}),

    "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'}    || $Post->{'NICKNAME'};
  $FORM{'FORM.EMAIL'}       = $UB::QUERY{'FORM_EMAIL'}       || $Post->{'EMAIL'};
  $FORM{'FORM.SUBJECT'}     = $Post->{'SUBJECT'};
  $FORM{'FORM.MESSAGE'}     = $UB::QUERY{'FORM_MESSAGE'}     || $Post->{'MESSAGE'};
  $FORM{'FORM.ATTACHMENT'}  = $Post->{'ATTACHMENT'};

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

  $FORM{'FORM.CHECKBOX.ATTACHMENT.KEEP'} = &HTML::Radio (
    NAME    => "FORM_ATTACHMENT_ACTION"          ,
    VALUE   => "KEEP"                               ,
    CHECKED => $UB::QUERY{'FORM_ATTACHMENT_ACTION'} || ($Post->{'ATTACHMENT'} ? "KEEP" : undef) || "UPLOAD"
  );
  $FORM{'FORM.CHECKBOX.ATTACHMENT.REMOVE'} = &HTML::Radio (
    NAME    => "FORM_ATTACHMENT_ACTION"          ,
    VALUE   => "REMOVE"                               ,
    CHECKED => $UB::QUERY{'FORM_ATTACHMENT_ACTION'} || ($Post->{'ATTACHMENT'} ? "KEEP" : undef) || "UPLOAD"
  );
  $FORM{'FORM.CHECKBOX.ATTACHMENT.UPLOAD'} = &HTML::Radio (
    NAME    => "FORM_ATTACHMENT_ACTION"          ,
    VALUE   => "UPLOAD"                               ,
    CHECKED => $UB::QUERY{'FORM_ATTACHMENT_ACTION'} || ($Post->{'ATTACHMENT'} ? "KEEP" : undef) || "UPLOAD"
  );

  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{'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 => $Post->{'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 => $Post->{'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'}    = ($Modify_Post::DYNAMIC{"LANGUAGE.".$ERROR}) ? $Modify_Post::DYNAMIC{"LANGUAGE.".$ERROR} : $ERROR;

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

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

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