###############################################################################
# UB_DoRegister.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 DoRegister {

  my $SendMail = 0;
  if ($SYSTEM::SENDMAIL == 1) {
    if ( ($SYSTEM::MAIL_SYSTEM eq "SMTP") && ($SYSTEM::SMTP_PATH ne "") ) {
      $SendMail = &Standard::Require("SMTP.lib", "$UB::LIBRARIES_PATH/SMTP.lib");
    } elsif ( ($SYSTEM::MAIL_SYSTEM eq "Sendmail") && ($SYSTEM::SENDMAIL_PATH ne "") ) {
      $SendMail = &Standard::Require("Sendmail.lib", "$UB::LIBRARIES_PATH/Sendmail.lib");
    }
  }

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

  # checking missing fields
  if ($UB::QUERY{'FORM_USERNAME'}        eq "") { &Error("MISS_USERNAME"        ) };
  if ($UB::QUERY{'FORM_NICKNAME'}        eq "") { &Error("MISS_NICKNAME"        ) };

  if ((!$GENERAL::VERIFY_EMAIL) && (!$GENERAL::REVIEW_ACCOUNT)) {
    if ($UB::QUERY{'FORM_PASSWORD'}        eq "") { &Error("MISS_PASSWORD"        ) };
    if ($UB::QUERY{'FORM_VERIFY_PASSWORD'} eq "") { &Error("MISS_VERIFY_PASSWORD" ) };
  }

  if ($UB::QUERY{'FORM_EMAIL'}           eq "") { &Error("MISS_EMAIL"           ) };

  # checking invalid username
  if (
      ($UB::QUERY{'FORM_USERNAME'} =~ /\W/)       ||
      (length ($UB::QUERY{'FORM_USERNAME'}) > 16) ||
      (length ($UB::QUERY{'FORM_USERNAME'}) <  2)
     )
  {
    &Error("INVALID_USERNAME");
  }

  # checking invalid nickname
  if (
      (length ($UB::QUERY{'FORM_NICKNAME'}) > 32)           ||
      (length ($UB::QUERY{'FORM_NICKNAME'}) <  2)
     )
  {
    &Error("INVALID_NICKNAME");
  }

  if ($GENERAL::VERIFY_EMAIL or $GENERAL::REVIEW_ACCOUNT) {
    # checking invalid password
    if (
        ($UB::QUERY{'FORM_PASSWORD'} !~ /^[A-Za-z0-9]+$/) ||
        (length ($UB::QUERY{'FORM_PASSWORD'}) > 16)       ||
        (length ($UB::QUERY{'FORM_PASSWORD'}) <  4)
       )
    {
      &Error("INVALID_PASSWORD");
    }

    # checking unmatch password
    if ($UB::QUERY{'FORM_PASSWORD'} ne $UB::QUERY{'FORM_VERIFY_PASSWORD'})  {
      &Error("UNMATCH_PASSWORD");
    }
  }

  # checking invalid email
  if (
    ($UB::QUERY{'FORM_EMAIL'} !~ /[\.\w\-]+\@[\.\w\-]+\.[\.\w\-]+/) ||
    (length ($UB::QUERY{'FORM_EMAIL'}) > 64)
  ) {
    &Error("INVALID_EMAIL");
  }

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

  $FORM{'USERNAME'}   = lc ($UB::QUERY{'FORM_USERNAME'});

  my $Password;
  if (($GENERAL::VERIFY_EMAIL == 1) || ($GENERAL::REVIEW_ACCOUNT == 1)) {
    $Password         = &Standard::RandomPassword();
  } else {
    $Password         = $UB::QUERY{'FORM_PASSWORD'};
  }
  $FORM{'PASSWORD'}   = &Standard::CryptPassword($FORM{'USERNAME'}, $Password);

  $FORM{'NICKNAME'}   = $UB::QUERY{'FORM_NICKNAME'};
  $FORM{'NICKNAME'}   = &Standard::EncodeHTML($FORM{'NICKNAME'});

  $FORM{'EMAIL'}      = lc ($UB::QUERY{'FORM_EMAIL'});

  $FORM{'LEVEL'}      = 30;                   # id of normal member level

  $FORM{'LEVEL_TEXT'} = $GENERAL::MEMBER_LEVEL_30;

  $FORM{'GROUP'}    = $GENERAL::DEFAULT_GROUP;

  my ($Group) = &DB::BinarySelect (
    TABLE   => "Groups",
    COLUMNS => ["ID", "TITLE"],
    KEY     => $FORM{'GROUP'}
  );

  $FORM{'GROUP_TEXT'} = $Group->{'TITLE'};

  $FORM{'STATUS'}     = 50;                   # id of activated status

  $FORM{'REGISTERED_SECOND'} = time;

  $FORM{'REGISTERED_DATE'}   = &Standard::ConvertEpochToDate($FORM{'REGISTERED_SECOND'});
  $FORM{'REGISTERED_TIME'}   = &Standard::ConvertEpochToTime($FORM{'REGISTERED_SECOND'});

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

  if ($GENERAL::REVIEW_ACCOUNT == 1) {
    $FORM{'APROVED'}  = 0;
  } else {
    $FORM{'APROVED'}  = 1;
  }

  $FORM{'IP'}         = $ENV{'REMOTE_ADDR'};

  $FORM{'HOST'}       = $ENV{'REMOTE_HOST'};

# =========================================================================== #
# Check the username is it existed

  if ($GENERAL::USERNAME_FILTER) {
    if ( &Standard::UsernameFilter ($FORM{'USERNAME'}) ) {
      &Error("NOT_AVAILABLE_USERNAME");
    }
  }

  my @Usernames = &Standard::GetList($SYSTEM::DB_PATH."/accounts.username");

  foreach (@Usernames) {
    if (/^$FORM{'USERNAME'}$/io) {
      &Error("TAKEN_USERNAME");
    }
  }
  @Usernames = ();

# =========================================================================== #
# Check the nickname is it existed

  if ($GENERAL::NICKNAME_FILTER) {
    if ( &Standard::NicknameFilter ($FORM{'NICKNAME'}) ) {
      &Error ("NOT_AVAILABLE_NICKNAME");
    }
  }

  unless ($GENERAL::NICKNAME_DUPLICATE == 1) {
    my @Nicknames = &Standard::GetList ($SYSTEM::DB_PATH."/accounts.nickname");

    foreach (@Nicknames) {
      if (/^$FORM{'NICKNAME'}$/i) {
        &Error ("TAKEN_NICKNAME");
      }
    }
    @Nicknames = ();
  }

# =========================================================================== #
# Check the email is it existed

  if ($GENERAL::EMAIL_FILTER) {
    if ( &Standard::EmailFilter ($FORM{'EMAIL'}) ) {
      &Error ("NOT_AVAILABLE_EMAIL");
    }
  }

  unless ($GENERAL::EMAIL_DUPLICATE == 1) {
    my @Emails = &Standard::GetList ($SYSTEM::DB_PATH."/accounts.email");

    foreach (@Emails) {
      if (/^$FORM{'EMAIL'}$/i) {
        &Error ("TAKEN_EMAIL");
      }
    }
    @Emails = ();
  }

# =========================================================================== #
# Add Member to Database

  ( # insert member information into accounts database
    &DB::Insert (
      TABLE  => "Accounts",
      VALUES => \%FORM
    )
  ) || &HTML::Error(MESSAGE => $DB::Error);

  ( # insert member information into profile database
    &DB::Insert (
      TABLE  => "Profiles",
      VALUES => \%FORM
    )
  ) || &HTML::Error(MESSAGE => $DB::Error);

  (
    &DB::Update (
      TABLE   => "Groups",
      VALUES  => {
        TOTAL_MEMBERS => "++\${TOTAL_MEMBERS}"
      },
      KEY     => $FORM{'GROUP'}
    )
  ) || &HTML::Error(MESSAGE => $DB::Error);

  &Standard::InsertToList($SYSTEM::DB_PATH."/accounts.username", $FORM{'USERNAME'});
  &Standard::InsertToList($SYSTEM::DB_PATH."/accounts.nickname", $FORM{'NICKNAME'});
  &Standard::InsertToList($SYSTEM::DB_PATH."/accounts.email"   , $FORM{'EMAIL'}   );

  my %ACCOUNT = (
    "ACCOUNT.USERNAME"        => $FORM{'USERNAME'}          ,
    "ACCOUNT.PASSWORD"        => $UB::QUERY{'FORM_PASSWORD'},
    "ACCOUNT.GROUP"           => $FORM{'GROUP'}             ,
    "ACCOUNT.NICKNAME"        => $FORM{'NICKNAME'}          ,
    "ACCOUNT.EMAIL"           => $FORM{'EMAIL'}             ,
    "ACCOUNT.REGISTERED_DATE" => $FORM{'REGISTERED_DATE'}   ,
    "ACCOUNT.REGISTERED_TIME" => $FORM{'REGISTERED_TIME'}   ,
    "ACCOUNT.IP"              => $FORM{'IP'}                ,
    "ACCOUNT.HOST"            => $FORM{'HOST'}
  );

  if (($SYSTEM::SENDMAIL == 1) && ($SendMail == 1)) {
    if ($GENERAL::REVIEW_ACCOUNT == 1) {
      &Standard::Require("UB_Admin_Review_Account.src", "$UB::INTERFACES_PATH/$SYSTEM::DEFAULT_THEME/UB_Admin_Review_Account.src");

      &Mail::Send (
        SUBJECT => $Admin_Review_Account::DYNAMIC{'LANGUAGE.SUBJECT'},
        MESSAGE => &Admin_Review_Account::Parse(\%ACCOUNT),
#       HEADER  => $GENERAL::EMAIL_FORMAT,
        TO      => $SYSTEM::ADMIN_EMAIL
      );
    } else {
      if ($GENERAL::NOTIFY_REGISTER == 1) {
        &Standard::Require("UB_Admin_Notify_Account.src", "$UB::INTERFACES_PATH/$SYSTEM::DEFAULT_THEME/UB_Admin_Notify_Account.src");

        &Mail::Send (
          SUBJECT => $Admin_Notify_Account::DYNAMIC{'LANGUAGE.SUBJECT'},
          MESSAGE => &Admin_Notify_Account::Parse(\%ACCOUNT),
#         HEADER  => $GENERAL::EMAIL_FORMAT,
          TO      => $SYSTEM::ADMIN_EMAIL
        );
      }

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

      &Mail::Send (
        SUBJECT => $Login_Information::DYNAMIC{'LANGUAGE.SUBJECT'},
        MESSAGE => &Login_Information::Parse(\%ACCOUNT),
#       HEADER  => $GENERAL::EMAIL_FORMAT,
        TO      => $FORM{'EMAIL'}
      );
    }
  }

  if ($GENERAL::VERIFY_EMAIL or $GENERAL::REVIEW_ACCOUNT) {
    if (
      ($UB::QUERY{'FORM_REMEMBER_PASSWORD'} ==    1) ||
      ($UB::QUERY{'FORM_REMEMBER_PASSWORD'} eq "on")
    ) {
      #push (
      #  @UB::COOKIES,
      #  $UB::CGI->cookie(
      #    -name     => "Username",
      #    -value    => $FORM{'USERNAME'},
      #    -expires  => '+1Y'
      #  )
      #);

      #push (
      #  @UB::COOKIES,
      #  $UB::CGI->cookie(
      #    -name     => "Password",
      #    -value    => $FORM{'PASSWORD'},
      #    -expires  => '+1Y'
      #  )
      #);

      $UB::COOKIES{'Username'} = $FORM{'USERNAME'};
      $UB::COOKIES{'Password'} = $FORM{'PASSWORD'};
    }
  }

  &Success(\%ACCOUNT);
}

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

  require "$UB::SOURCES_PATH/UB_Register.src";
  &Source::Register($code);

  exit;
}

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

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

  &Standard::PrintHTMLHeader();
  print &Register_Success::Parse($ACCOUNT);
  exit;
}

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