###############################################################################
# CP_DoCfgGeneral.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;

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

sub DoCfgGeneral {

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

  $FORM{'SITE_NAME'}  = $UB::QUERY{'SITE_NAME'};
  $FORM{'SITE_URL'}   = $UB::QUERY{'SITE_URL'};

  $FORM{'ULTRABOARD_NAME'}        = $UB::QUERY{'ULTRABOARD_NAME'};
  $FORM{'ULTRABOARD_DESCRIPTION'} = $UB::QUERY{'ULTRABOARD_DESCRIPTION'};

  $FORM{'CONTACT_EMAIL'} = $UB::QUERY{'CONTACT_EMAIL'};

  $FORM{'WORD_FILTER'} = $UB::QUERY{'WORD_FILTER'};
  $FORM{'WORD_FILTER'} =~ s/\|\|//g;
  $FORM{'WORD_FILTER'} =~ s/^\||\|$//g;

  $FORM{'MEMBER_LEVEL_30'}  = $UB::QUERY{'MEMBER_LEVEL_30'};
  $FORM{'MEMBER_LEVEL_50'}  = $UB::QUERY{'MEMBER_LEVEL_50'};
  $FORM{'MEMBER_LEVEL_70'}  = $UB::QUERY{'MEMBER_LEVEL_70'};
  $FORM{'MEMBER_LEVEL_100'} = $UB::QUERY{'MEMBER_LEVEL_100'};

  $FORM{'UPGRADE_LEVEL'} = $UB::QUERY{'UPGRADE_LEVEL'};
  $FORM{'UPGRADE_LEVEL'} =~s/\D//g;
  $FORM{'UPGRADE_LEVEL'} = 5 if $FORM{'UPGRADE_LEVEL'} < 5;

  $FORM{'PRINT'} = $UB::QUERY{'PRINT'};
  $FORM{'PRINT'} = 0 if $FORM{'PRINT'} ne "1";

  $FORM{'FORWARD'} = $UB::QUERY{'FORWARD'};
  $FORM{'FORWARD'} = 0 if (($SYSTEM::SENDMAIL != 1) || ($FORM{'FORWARD'} ne "1"));

  $FORM{'INDEX'}     = $UB::QUERY{'INDEX'};
  $FORM{'INDEX'}     = 0 if $FORM{'INDEX'} ne "1";
  $FORM{'INDEX_URL'} = $UB::QUERY{'INDEX_URL'};

  $FORM{'LOGIN'} = $UB::QUERY{'LOGIN'};
  $FORM{'LOGIN'} = 0 if (($FORM{'INDEX'} == 0) || ($FORM{'LOGIN'} ne "1"));

  $FORM{'CATEGORY_ONLY'} = $UB::QUERY{'CATEGORY_ONLY'};
  $FORM{'CATEGORY_ONLY'} = 0 if $FORM{'CATEGORY_ONLY'} ne "1";

  $FORM{'HEADLINES_PER_PAGE'} = $UB::QUERY{'HEADLINES_PER_PAGE'};
  $FORM{'HEADLINES_PER_PAGE'} =~s/\D//g;
  $FORM{'HEADLINES_PER_PAGE'} = 5 if $FORM{'HEADLINES_PER_PAGE'} < 5;

  $FORM{'DAY_PRUNE'} = $UB::QUERY{'DAY_PRUNE'};
  if ($FORM{'DAY_PRUNE'} ne "*") {
    $FORM{'DAY_PRUNE'} =~s/\D//g;
    $FORM{'DAY_PRUNE'} = 1 if $FORM{'DAY_PRUNE'} < 1;
  }

  $FORM{'HOT'} = $UB::QUERY{'HOT'};
  $FORM{'HOT'} =~s/\D//g;
  $FORM{'HOT'} = 5 if $FORM{'HOT'} < 5;

  $FORM{'POP'} = $UB::QUERY{'POP'};
  $FORM{'POP'} =~s/\D//g;
  $FORM{'POP'} = 5 if $FORM{'POP'} < 5;

  $FORM{'POST_PER_PAGE'} = $UB::QUERY{'POST_PER_PAGE'};
  $FORM{'POST_PER_PAGE'} =~s/\D//g;
  $FORM{'POST_PER_PAGE'} = 2 if $FORM{'POST_PER_PAGE'} < 2;

  $FORM{'NOTIFY_REGISTER'} = $UB::QUERY{'NOTIFY_REGISTER'};
  $FORM{'NOTIFY_REGISTER'} = 0 if (($SYSTEM::SENDMAIL != 1) || ($FORM{'NOTIFY_REGISTER'} ne "1"));

  $FORM{'DEFAULT_GROUP'} = $UB::QUERY{'DEFAULT_GROUP'};
  $FORM{'DEFAULT_GROUP'} =~s/\D//g;

  $FORM{'VERIFY_EMAIL'} = $UB::QUERY{'VERIFY_EMAIL'};
  $FORM{'VERIFY_EMAIL'} = 0 if (($SYSTEM::SENDMAIL != 1) || ($FORM{'VERIFY_EMAIL'} ne "1"));

  $FORM{'REVIEW_ACCOUNT'} = $UB::QUERY{'REVIEW_ACCOUNT'};
  $FORM{'REVIEW_ACCOUNT'} = 0 if (($SYSTEM::SENDMAIL != 1) || ($FORM{'VERIFY_EMAIL'} ne "1"));

  $FORM{'NICKNAME_DUPLICATE'} = $UB::QUERY{'NICKNAME_DUPLICATE'};
  $FORM{'NICKNAME_DUPLICATE'} = 0 if $FORM{'NICKNAME_DUPLICATE'} ne "1";

  $FORM{'EMAIL_DUPLICATE'} = $UB::QUERY{'EMAIL_DUPLICATE'};
  $FORM{'EMAIL_DUPLICATE'} = 0 if $FORM{'EMAIL_DUPLICATE'} ne "1";

  $FORM{'USERNAME_FILTER'} = $UB::QUERY{'USERNAME_FILTER'};
  $FORM{'USERNAME_FILTER'} =~ s/\|\|//g;
  $FORM{'USERNAME_FILTER'} =~ s/^\||\|$//g;

  $FORM{'NICKNAME_FILTER'} = $UB::QUERY{'NICKNAME_FILTER'};
  $FORM{'NICKNAME_FILTER'} =~ s/\|\|//g;
  $FORM{'NICKNAME_FILTER'} =~ s/^\||\|$//g;

  $FORM{'EMAIL_FILTER'} = $UB::QUERY{'EMAIL_FILTER'};
  $FORM{'EMAIL_FILTER'} =~ s/\|\|//g;
  $FORM{'EMAIL_FILTER'} =~ s/^\||\|$//g;

  $FORM{'MAX_COMMENTS_LENGTH'} = $UB::QUERY{'MAX_COMMENTS_LENGTH'};
  $FORM{'MAX_COMMENTS_LENGTH'} =~s/\D//g;
  $FORM{'MAX_COMMENTS_LENGTH'} = 512 if $FORM{'MAX_COMMENTS_LENGTH'} > 512;

  $FORM{'MAX_SIGNATURE_LENGTH'} = $UB::QUERY{'MAX_SIGNATURE_LENGTH'};
  $FORM{'MAX_SIGNATURE_LENGTH'} =~s/\D//g;
  $FORM{'MAX_SIGNATURE_LENGTH'} = 512 if $FORM{'MAX_SIGNATURE_LENGTH'} > 512;

  $FORM{'MAX_PHOTO_LENGTH'} = $UB::QUERY{'MAX_PHOTO_LENGTH'};
  $FORM{'MAX_PHOTO_LENGTH'} =~s/\D//g;
  $FORM{'MAX_PHOTO_LENGTH'} = 1 if $FORM{'MAX_PHOTO_LENGTH'} < 1;

  $FORM{'MAX_PHOTO_X'} = $UB::QUERY{'MAX_PHOTO_X'};
  $FORM{'MAX_PHOTO_X'} =~s/\D//g;
  $FORM{'MAX_PHOTO_X'} = 5 if $FORM{'MAX_PHOTO_X'} < 5;

  $FORM{'MAX_PHOTO_Y'} = $UB::QUERY{'MAX_PHOTO_Y'};
  $FORM{'MAX_PHOTO_Y'} =~s/\D//g;
  $FORM{'MAX_PHOTO_Y'} = 5 if $FORM{'MAX_PHOTO_Y'} < 5;

  $FORM{'NOTIFY_POST'} = $UB::QUERY{'NOTIFY_POST'};
  $FORM{'NOTIFY_POST'} = 0 if (($SYSTEM::SENDMAIL != 1) || ($FORM{'NOTIFY_POST'} ne "1"));

  $FORM{'NOTIFY_MEMBER_POST'} = $UB::QUERY{'NOTIFY_MEMBER_POST'};
  $FORM{'NOTIFY_MEMBER_POST'} = 0 if (($SYSTEM::SENDMAIL != 1) || ($FORM{'NOTIFY_MEMBER_POST'} ne "1"));

  $FORM{'MAX_SUBJECT_LENGTH'} = $UB::QUERY{'MAX_SUBJECT_LENGTH'};
  $FORM{'MAX_SUBJECT_LENGTH'} =~s/\D//g;
  $FORM{'MAX_SUBJECT_LENGTH'} = 16 if $FORM{'MAX_SUBJECT_LENGTH'} < 16;
  $FORM{'MAX_SUBJECT_LENGTH'} = 512 if $FORM{'MAX_SUBJECT_LENGTH'} > 512;

  $FORM{'MAX_MESSAGE_LENGTH'} = $UB::QUERY{'MAX_MESSAGE_LENGTH'};
  $FORM{'MAX_MESSAGE_LENGTH'} =~s/\D//g;
  $FORM{'MAX_MESSAGE_LENGTH'} = 64 if $FORM{'MAX_MESSAGE_LENGTH'} < 64;
  $FORM{'MAX_MESSAGE_LENGTH'} = (1024 * 8) if $FORM{'MAX_MESSAGE_LENGTH'} > (1024 * 8);

  $FORM{'MESSAGE_ICONS'} = $UB::QUERY{'MESSAGE_ICONS'};
  $FORM{'MESSAGE_ICONS'} = 0 if $FORM{'MESSAGE_ICONS'} ne "1";

  $FORM{'SHOW_IP_HOST'} = $UB::QUERY{'SHOW_IP_HOST'};
  $FORM{'SHOW_IP_HOST'} = 0 if $FORM{'SHOW_IP_HOST'} ne "1";

  %FORM = %{ &Encode (\%FORM) };

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

  # checking missing fields
  if ($FORM{'SITE_NAME'}       eq "") { &Error("You forgot to fill the \"Web Site Name\" field.")   };
  if ($FORM{'SITE_URL'}        eq "") { &Error("You forgot to fill the \"Web Site URL\" field.")    };
  if ($FORM{'ULTRABOARD_NAME'} eq "") { &Error("You forgot to fill the \"UltraBoard Name\" field.") };

  # checking invalid email
  if ($FORM{'CONTACT_EMAIL'} !~ /[\.\w\-]+\\\@[\.\w\-]+\.[\.\w\-]+/)  {
    &Error("The contact email address must be contain letters, numbers, or '_' character only.");
  }

# =========================================================================== #
# Update the General.cfg

  &Standard::FileOpen(*CFG, "w", "$UB::VARIABLES_PATH/General.cfg");
    print CFG <<TEXT;
package GENERAL;

\$GENERAL::SITE_NAME  = qq~$FORM{'SITE_NAME'}~;
\$GENERAL::SITE_URL   = qq~$FORM{'SITE_URL'}~;

\$GENERAL::ULTRABOARD_NAME        = qq~$FORM{'ULTRABOARD_NAME'}~;
\$GENERAL::ULTRABOARD_DESCRIPTION = qq~$FORM{'ULTRABOARD_DESCRIPTION'}~;

\$GENERAL::CONTACT_EMAIL  = qq~$FORM{'CONTACT_EMAIL'}~;

\$GENERAL::WORD_FILTER = qq~$FORM{'WORD_FILTER'}~;

\$GENERAL::MEMBER_LEVEL_30  = qq~$FORM{'MEMBER_LEVEL_30'}~;
\$GENERAL::MEMBER_LEVEL_50  = qq~$FORM{'MEMBER_LEVEL_50'}~;
\$GENERAL::MEMBER_LEVEL_70  = qq~$FORM{'MEMBER_LEVEL_70'}~;
\$GENERAL::MEMBER_LEVEL_100 = qq~$FORM{'MEMBER_LEVEL_100'}~;

\$GENERAL::UPGRADE_LEVEL   = \"$FORM{'UPGRADE_LEVEL'}\";

\$GENERAL::PRINT          = \"$FORM{'PRINT'}\";
\$GENERAL::FORWARD        = \"$FORM{'FORWARD'}\";

\$GENERAL::LOGIN         = \"$FORM{'LOGIN'}\";
\$GENERAL::CATEGORY_ONLY = \"$FORM{'CATEGORY_ONLY'}\";
\$GENERAL::INDEX         = \"$FORM{'INDEX'}\";
\$GENERAL::INDEX_URL     = qq~$FORM{'INDEX_URL'}~;

\$GENERAL::HEADLINES_PER_PAGE = \"$FORM{'HEADLINES_PER_PAGE'}\";
\$GENERAL::DAY_PRUNE          = \"$FORM{'DAY_PRUNE'}\";

\$GENERAL::HOT          = \"$FORM{'HOT'}\";
\$GENERAL::POP          = \"$FORM{'POP'}\";

\$GENERAL::POST_PER_PAGE    = \"$FORM{'POST_PER_PAGE'}\";

\$GENERAL::NOTIFY_REGISTER  = \"$FORM{'NOTIFY_REGISTER'}\";

\$GENERAL::DEFAULT_GROUP    = \"$FORM{'DEFAULT_GROUP'}\";

\$GENERAL::VERIFY_EMAIL     = \"$FORM{'VERIFY_EMAIL'}\";
\$GENERAL::REVIEW_ACCOUNT   = \"$FORM{'REVIEW_ACCOUNT'}\";

\$GENERAL::NICKNAME_DUPLICATE = \"$FORM{'NICKNAME_DUPLICATE'}\";
\$GENERAL::EMAIL_DUPLICATE    = \"$FORM{'EMAIL_DUPLICATE'}\";

\$GENERAL::USERNAME_FILTER = qq~$FORM{'USERNAME_FILTER'}~;
\$GENERAL::NICKNAME_FILTER = qq~$FORM{'NICKNAME_FILTER'}~;
\$GENERAL::EMAIL_FILTER    = qq~$FORM{'EMAIL_FILTER'}~;

\$GENERAL::MAX_COMMENTS_LENGTH  = \"$FORM{'MAX_COMMENTS_LENGTH'}\";
\$GENERAL::MAX_SIGNATURE_LENGTH = \"$FORM{'MAX_SIGNATURE_LENGTH'}\";

\$GENERAL::MAX_PHOTO_LENGTH = \"$FORM{'MAX_PHOTO_LENGTH'}\";
\$GENERAL::MAX_PHOTO_X      = \"$FORM{'MAX_PHOTO_X'}\";
\$GENERAL::MAX_PHOTO_Y      = \"$FORM{'MAX_PHOTO_Y'}\";

\$GENERAL::NOTIFY_POST          = \"$FORM{'NOTIFY_POST'}\";
\$GENERAL::NOTIFY_MEMBER_POST   = \"$FORM{'NOTIFY_MEMBER_POST'}\";

\$GENERAL::MAX_SUBJECT_LENGTH     = \"$FORM{'MAX_SUBJECT_LENGTH'}\";
\$GENERAL::MAX_MESSAGE_LENGTH     = \"$FORM{'MAX_MESSAGE_LENGTH'}\";

\$GENERAL::MESSAGE_ICONS          = \"$FORM{'MESSAGE_ICONS'}\";

\$GENERAL::SHOW_IP_HOST           = \"$FORM{'SHOW_IP_HOST'}\";

\@GENERAL::ATTACH_TYPE = (
TEXT
    my @AttachType = split (/\s*\n\s*/, $UB::QUERY{'ATTACH_TYPE'});

    chomp (@AttachType);
    chomp (@AttachType);
    for (my $index = 0; $index <= $#AttachType; $index++) {
      $AttachType[$index] = "  \"".$AttachType[$index]."\"";
    }
    print CFG join (",\n", @AttachType);
    print CFG<<TEXT;

);

1;
TEXT
  close (CFG);

  &Success(\%FORM);
}

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

  require "$UB::SOURCES_PATH/CP_CfgGeneral.src";
  &Source::CfgGeneral($message);

  exit;
}

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

  &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> / General Management / Configure general options
    </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 general options has been updated, however you may have to rebuild/compile all the data to apply some of these setting.</td>
                    </tr>
                  </table>
                </td>
              </tr>
            </table>
          </td>
        </tr>
      </table>
    </td>
  </tr>
</table>
</body>
</html>
HTML

  exit;
}

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