###############################################################################
# CP_DoCfgSystem.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 DoCfgSystem {

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

  $FORM{'DB_PATH'}           = $UB::QUERY{'DB_PATH'};
  $FORM{'DB_PATH'}           =~s/\\/\//g;
  chop ($FORM{'DB_PATH'})      if $FORM{'DB_PATH'} =~ /\/$/;

  $FORM{'BACKUP'}            = $UB::QUERY{'BACKUP'};
  $FORM{'BACKUP'}            =~s/\\/\//g;
  chop ($FORM{'BACKUP'})       if $FORM{'BACKUP'} =~ /\/$/;

  $FORM{'THEME_PATH'}        = $UB::QUERY{'THEME_PATH'};
  $FORM{'THEME_PATH'}        =~s/\\/\//g;
  chop ($FORM{'THEME_PATH'})   if $FORM{'THEME_PATH'} =~ /\/$/;

  $FORM{'IMAGE_PATH'}        = $UB::QUERY{'IMAGE_PATH'};
  $FORM{'IMAGE_PATH'}        =~s/\\/\//g;
  chop ($FORM{'IMAGE_PATH'})   if $FORM{'IMAGE_PATH'} =~ /\/$/;

  $FORM{'UPLOAD_PATH'}       = $UB::QUERY{'UPLOAD_PATH'};
  $FORM{'UPLOAD_PATH'}       =~s/\\/\//g;
  chop ($FORM{'UPLOAD_PATH'})  if $FORM{'UPLOAD_PATH'} =~ /\/$/;

  $FORM{'CP_URL'}            = $UB::QUERY{'CP_URL'};
  chop ($FORM{'CP_URL'})       if $FORM{'CP_URL'} =~ /\/$/;

  $FORM{'UB_URL'}            = $UB::QUERY{'UB_URL'};
  chop ($FORM{'UB_URL'})       if $FORM{'UB_URL'} =~ /\/$/;

  $FORM{'UPLOAD_URL'}        = $UB::QUERY{'UPLOAD_URL'};
  chop ($FORM{'UPLOAD_URL'})   if $FORM{'UPLOAD_URL'} =~ /\/$/;

  $FORM{'IMAGE_URL'}         = $UB::QUERY{'IMAGE_URL'};
  chop ($FORM{'IMAGE_URL'})    if $FORM{'IMAGE_URL'} =~ /\/$/;

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

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

  $FORM{'MAIL_SYSTEM'}       = $UB::QUERY{'MAIL_SYSTEM'};
  $FORM{'MAIL_SYSTEM'}       = "SENDMAIL" if $FORM{'MAIL_SYSTEM'} ne "SMTP";

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

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

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

  $FORM{'LOCALTIME_OFFSET'}  = $UB::QUERY{'LOCALTIME_OFFSET'};
  $FORM{'LOCALTIME_OFFSET'}  =~s/[^\d\-\+]//g;

  $FORM{'DATE_FORMAT'}       = $UB::QUERY{'DATE_FORMAT'};
  $FORM{'DATE_FORMAT'}       = "US" if ($FORM{'DATE_FORMAT'} ne "USE") and ($FORM{'DATE_FORMAT'} ne "EU") and ($FORM{'DATE_FORMAT'} ne "EUE");

  $FORM{'TIME_FORMAT'}       = $UB::QUERY{'TIME_FORMAT'};
  $FORM{'TIME_FORMAT'}       = "24" if $FORM{'TIME_FORMAT'} ne "12";

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

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

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

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

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

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

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

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

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

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

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

  # checking missing fields
  if ($FORM{'DB_PATH'}       eq "") { &Error("You forgot to fill the \"Datebase Path\" field.") };
  if ($FORM{'BACKUP'}        eq "") { &Error("You forgot to fill the \"Backup Path\" field.")   };
  if ($FORM{'THEME_PATH'}    eq "") { &Error("You forgot to fill the \"Theme Path\" field.")    };
  if ($FORM{'IMAGE_PATH'}    eq "") { &Error("You forgot to fill the \"Images Path\" field.")   };
  if ($FORM{'UPLOAD_PATH'}   eq "") { &Error("You forgot to fill the \"Upload Path\" field.")   };

  if ($FORM{'CP_URL'}     eq "") { &Error("You forgot to fill the \"Control Panel URL\" field.") };
  if ($FORM{'UB_URL'}     eq "") { &Error("You forgot to fill the \"UltraBoard URL\" field.")    };
  if ($FORM{'UPLOAD_URL'} eq "") { &Error("You forgot to fill the \"Upload URL\" field.")        };
  if ($FORM{'IMAGE_URL'}  eq "") { &Error("You forgot to fill the \"Images URL\" field.")        };

  unless (-e $FORM{'DB_PATH'}    ) { &Error("The \"Datebase Path\" is not found.") };
  unless (-e $FORM{'BACKUP'}     ) { &Error("The \"Backup Path\" is not found.")   };
  unless (-e $FORM{'THEME_PATH'} ) { &Error("The \"Theme Path\" is not found.")    };
  unless (-e $FORM{'IMAGE_PATH'} ) { &Error("The \"Images Path\" is not found.")   };
  unless (-e $FORM{'UPLOAD_PATH'}) { &Error("The \"Upload Path\" is not found.")   };

  chmod (0777, $FORM{'DB_PATH'});
  chmod (0777, $FORM{'BACKUP'});
  chmod (0777, $FORM{'THEME_PATH'});
  chmod (0777, $FORM{'IMAGE_PATH'});
  chmod (0777, $FORM{'UPLOAD_PATH'});

  (opendir (DIR, $FORM{'DB_PATH'})     ) ? closedir (DIR) : &Error("Cannot open the directory \"Datebase Path\"!");
  (opendir (DIR, $FORM{'BACKUP'})      ) ? closedir (DIR) : &Error("Cannot open the directory \"Backup Path\"!");
  (opendir (DIR, $FORM{'THEME_PATH'})  ) ? closedir (DIR) : &Error("Cannot open the directory \"Theme Path\"!");
  (opendir (DIR, $FORM{'IMAGE_PATH'})  ) ? closedir (DIR) : &Error("Cannot open the directory \"Images Path\"!");
  (opendir (DIR, $FORM{'UPLOAD_PATH'}) ) ? closedir (DIR) : &Error("Cannot open the directory \"Upload Path\"!");

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

  if ($FORM{'SENDMAIL'}) {
    if ($FORM{'MAIL_SYSTEM'} eq "SMTP") {
      if ($FORM{'SMTP_PATH'}     eq "") { &Error("You forgot to fill the \"SMTP Address\" field.") };
    } else {
      if ($FORM{'SENDMAIL_PATH'} eq "") { &Error("You forgot to fill the \"Sendmail Path\" field.") };
    }
  }

  $SYSTEM::PERMISSION = 0777 if ((!$SYSTEM::PERMISSION) || ($SYSTEM::PERMISSION =~ /\D/));

# =========================================================================== #
# Update the System.cfg

  &Standard::Chmod("$UB::VARIABLES_PATH/System.cfg", "w");

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

\$SYSTEM::UB_URL           = "$FORM{'UB_URL'}";
\$SYSTEM::CP_URL           = "$FORM{'CP_URL'}";
\$SYSTEM::IMAGE_URL        = "$FORM{'IMAGE_URL'}";
\$SYSTEM::UPLOAD_URL       = "$FORM{'UPLOAD_URL'}";

\$SYSTEM::BACKUP           = "$FORM{'BACKUP'}";
\$SYSTEM::DB_PATH          = "$FORM{'DB_PATH'}";
\$SYSTEM::LOGS_PATH        = "$FORM{'DB_PATH'}/Logs";
\$SYSTEM::TEMP_PATH        = "$FORM{'DB_PATH'}/Temp";
\$SYSTEM::UPLOAD_PATH      = "$FORM{'UPLOAD_PATH'}";
\$SYSTEM::IMAGE_PATH       = "$FORM{'IMAGE_PATH'}";
\$SYSTEM::THEME_PATH       = "$FORM{'THEME_PATH'}";

\$SYSTEM::FLOCK            = "$FORM{'FLOCK'}";
\$SYSTEM::PERMISSION       = $SYSTEM::PERMISSION;

\$SYSTEM::SENDMAIL         = "$FORM{'SENDMAIL'}";
\$SYSTEM::MAIL_SYSTEM      = "$FORM{'MAIL_SYSTEM'}";
\$SYSTEM::SMTP_PATH        = "$FORM{'SMTP_PATH'}";
\$SYSTEM::SENDMAIL_PATH    = "$FORM{'SENDMAIL_PATH'}";

\$SYSTEM::ADMIN_EMAIL      = "$FORM{'ADMIN_EMAIL'}";

\$SYSTEM::LOCALTIME_OFFSET = "$FORM{'LOCALTIME_OFFSET'}";
\$SYSTEM::TIMEZONE         = "$FORM{'TIMEZONE'}";
\$SYSTEM::DATE_FORMAT      = "$FORM{'DATE_FORMAT'}";
\$SYSTEM::TIME_FORMAT      = "$FORM{'TIME_FORMAT'}";

\$SYSTEM::STATS            = "$FORM{'STATS'}";
\$SYSTEM::LOGS             = "$FORM{'LOGS'}";
\$SYSTEM::SESSIONS_EXPIRE  = "$FORM{'SESSIONS_EXPIRE'}";
\$SYSTEM::OFFLINE_MESSAGE  = "$FORM{'OFFLINE_MESSAGE'}";

\$SYSTEM::MAX_LOGS         = "$FORM{'MAX_LOGS'}";

\$SYSTEM::DEFAULT_THEME    = "$FORM{'DEFAULT_THEME'}";

\$SYSTEM::IP_FILTER        = qq~$FORM{'IP_FILTER'}~;
\$SYSTEM::HOST_FILTER      = qq~$FORM{'HOST_FILTER'}~;

\%SYSTEM::MINE = (
  "application/astound"             => ["asd", "asn"],
  "application/autocad"             => ["dwg"],
  "application/futuresplash"        => ["spl"],
  "application/mac-binhex40"        => ["hqx"],
  "application/mac-compactpro"      => ["cpt"],
  "application/msword"              => ["doc"],
  "application/octet-stream"        => ["bin", "class", "dms", "dll", "exe", "lha", "lzh", "z"],
  "application/pdf"                 => ["pdf"],
  "application/postscript"          => ["ai", "eps", "ps"],
  "application/powerpoint"          => ["ppt"],
  "application/vocaltec-media-desc" => ["vmd"],
  "application/vocaltec-media-file" => ["vmf"],
  "application/x-asap"              => ["asp"],
  "application/x-cdlink"            => ["vcd"],
  "application/x-chat"              => ["chat"],
  "application/x-compress"          => ["Z"],
  "application/x-cpio"              => ["cpio"],
  "application/x-director"          => ["dcr", "dir", "dxr", "fgd"],
  "application/x-gtar"              => ["gtar"],
  "application/x-gzip"              => ["gz"],
  "application/x-hdf"               => ["hdf"],
  "application/x-install"           => ["inst"],
  "application/x-javascript"        => ["js"],
  "application/x-netcdf"            => ["cdf", "nc"],
  "application/x-sh"                => ["sh"],
  "application/x-shockwave-flash"   => ["swf"],
  "application/x-tar"               => ["tar"],
  "application/x-tardist"           => ["tardist"],
  "application/x-wais-source"       => ["src"],
  "application/x-zip-compressed"    => ["zip"],

  "audio/mpeg"                  => ["mp2", "mpga"],
  "audio/x-mpeg"                => ["mp3"],
  "audio/x-mpegurl"             => ["m3u", "mp3url"],
  "audio/tsplayer"              => ["tsi"],
  "audio/voxware"               => ["vox"],
  "audio/x-aiff"                => ["aif", "aifc", "aiff"],
  "audio/x-midi"                => ["mid"],
  "audio/x-mpeg2"               => ["mp2a", "mpa2"],
  "audio/x-pn-realaudio"        => ["ram"],
  "audio/x-pn-realaudio-plugin" => ["rpm"],
  "audio/x-realaudio"           => ["ra"],
  "audio/x-pn-realaudio"        => ["rm"],
  "audio/x-wav"                 => ["wav"],

  "image/gif"         => ["gif"],
  "image/pjpeg"        => ["jpeg", "jpg"],
  "image/jpeg"        => ["jpeg", "jpg"],
  "image/ico"         => ["ico"],
  "image/tiff"        => ["tif", "tiff"],
  "image/x-png"       => ["png"],
  "image/x-MS-bmp"    => ["bmp"],
  "image/x-freehand"  => ["fh4", "fh5", "fh7", "fh", "fhc"],
  "image/x-photo-cd"  => ["pcd"],
  "image/x-rgb"       => ["rgb"],

  "text/html"       => ["htm", "html", "stm", "shtml"],
  "text/plain"      => ["txt"],
  "text/richtext"   => ["rtx"],
  "text/warez-info" => ["nfo"],
  "text/x-sgml"     => ["sgm", "sgml"],

  "video/mpeg"        => ["mpe", "mpeg", "mpg"],
  "video/quicktime"   => ["mov", "qt"],
  "video/vivo"        => ["viv", "vivo"],
  "video/x-msvideo"   => ["avi"],
  "video/x-sgi-movie" => ["movie", "mv"],

  "x-conference/x-cooltalk" => ["ice"],

  "x-world/x-vrml" => ["vrml", "wrl"]
);

1;
TEXT
  close (CFG);

  &Success(\%FORM);
}

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

  require "$UB::SOURCES_PATH/CP_CfgSystem.src";
  &Source::CfgSystem($message);

  exit;
}

###############################################################################
# Success function
# - function of printing success message of updating system 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 system 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 system options has been updated, however you may have to rebuild/compile all the data to apply these setting.</td>
                    </tr>
                  </table>
                </td>
              </tr>
            </table>
          </td>
        </tr>
      </table>
    </td>
  </tr>
</table>
</body>
</html>
HTML

  exit;
}

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