###############################################################################
# CP_DoCreateAccount.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 DoCreateAccount {

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

  # checking missing fields
  if ($UB::QUERY{'FORM_USERNAME'}  eq "") { &Error("You forgot to fill the \"Username\" field.") };
  if ($UB::QUERY{'FORM_NICKNAME'}  eq "") { &Error("You forgot to fill the \"Nickname\" field.") };
  if ($UB::QUERY{'FORM_PASSWORD'}  eq "") { &Error("You forgot to fill the \"Password\" field.") };
  if ($UB::QUERY{'FORM_LEVEL'}     eq "") { &Error("You forgot to choose the \"Level\" field.")  };
  if ($UB::QUERY{'FORM_GROUP'}     eq "") { &Error("You forgot to choose the \"Group\" field.")  };
  if ($UB::QUERY{'FORM_EMAIL'}     eq "") { &Error("You forgot to fill the \"Email\" field.")    };
  if ($UB::QUERY{'FORM_STATUS'}    eq "") { &Error("You forgot to choose the \"Status\" field.") };

  # checking invalid username
  if (
      ($UB::QUERY{'FORM_USERNAME'} =~ /\W/)      ||
      (length ($UB::QUERY{'FORM_USERNAME'}) > 16) ||
      (length ($UB::QUERY{'FORM_USERNAME'}) <  2)
     )
  {
    &Error("The username must be more than 2 characters and no more than 16 characters with letters, numbers, or '_' character only.");
  }

  # checking invalid nickname
  if (
      (length ($UB::QUERY{'FORM_NICKNAME'}) > 32) ||
      (length ($UB::QUERY{'FORM_NICKNAME'}) <  2)
     )
  {
    &Error("The nickname must be more than 2 characters and no more than 32 characters.");
  }

  # 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("The password must be more than 4 characters and no more than 16 characters with letters or numbers only.");
  }

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

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

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

  my $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'});

  if (
    ($UB::QUERY{'FORM_LEVEL'} eq      "30")   ||  # id of normal members
    ($UB::QUERY{'FORM_LEVEL'} eq  "normal")   ||
    ($UB::QUERY{'FORM_LEVEL'} eq  "Normal")
  ) {
    $FORM{'LEVEL'}      = 30;                         # id of normal members

    $FORM{'LEVEL_TEXT'} = $GENERAL::MEMBER_LEVEL_30;
  } elsif (
    ($UB::QUERY{'FORM_LEVEL'} eq      "50")   ||  # id of premium members
    ($UB::QUERY{'FORM_LEVEL'} eq "premium")   ||
    ($UB::QUERY{'FORM_LEVEL'} eq "Premium")
  ) {
    $FORM{'LEVEL'}      = 50;                         # id of premium members

    $FORM{'LEVEL_TEXT'} = $GENERAL::MEMBER_LEVEL_50;
  } elsif (
    ($UB::QUERY{'FORM_LEVEL'} eq      "70")   ||  # id of team members
    ($UB::QUERY{'FORM_LEVEL'} eq    "team")   ||
    ($UB::QUERY{'FORM_LEVEL'} eq    "Team")
  ) {
    $FORM{'LEVEL'}      = 70;                         # id of team members

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

  $FORM{'GROUP'}      = $UB::QUERY{'FORM_GROUP'};

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

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

  if (
    ($UB::QUERY{'FORM_STATUS'} eq        "50") || # id of activated status
    ($UB::QUERY{'FORM_STATUS'} eq "Activated")
  ) {
    $FORM{'STATUS'}   = 50;                           # id of activated status
  } else {
    $FORM{'STATUS'}   = 30;                           # id of deactivated status
  }

  $FORM{'REGISTERED_SECOND'} = time;

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

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

  if ($GENERAL::USERNAME_FILTER == 1) {
    if ( &Standard::UsernameFilter ($FORM{'USERNAME'}) ) {
      &Error ("Your username was not available to use.");
    }
  }

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

  foreach (@Usernames) {
    if (/^$FORM{'USERNAME'}$/i) {
      &Error ("Your username has been taken.");
    }
  }
  @Usernames = ();

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

  if ($GENERAL::NICKNAME_FILTER == 1) {
    if ( &Standard::NicknameFilter ($FORM{'NICKNAME'}) ) {
      &Error ("Your nickname was not available to use.");
    }
  }

  unless ($GENERAL::NICKNAME_DUPLICATE == 1) {
    my @Nicknames = &Standard::GetList ($SYSTEM::DB_PATH."/accounts.nickname");
    foreach (@Nicknames) {
      if (/^$FORM{'NICKNAME'}$/i) {
        &Error ("Your nickname has been taken.");
      }
    }
    @Nicknames = ();
  }

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

  if ($GENERAL::EMAIL_FILTER == 1) {
    if ( &Standard::EmailFilter ($FORM{'EMAIL'}) ) {
      &Error ("Your email was not available to use.");
    }
  }

  unless ($GENERAL::EMAIL_DUPLICATE == 1) {
    my @Emails = &Standard::GetList ($SYSTEM::DB_PATH."/accounts.email");
    foreach (@Emails) {
      if (/^$FORM{'EMAIL'}$/i) {
        &Error ("Your email has been taken.");
      }
    }
    @Emails = ();
  }

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

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

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

  ( # update member group information from groups database
    &DB::Update(
      TABLE  => "Groups",
      VALUES => {
        TOTAL_MEMBERS => "++\${TOTAL_MEMBERS}"
      },
      KEY    => $FORM{'GROUP'}
    )
  ) || &HTML::Error ("Internal Database Error", $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'}   );

  &Success(\%FORM);
}

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

  require "$UB::SOURCES_PATH/CP_CreateAccount.src";
  &Source::CreateAccount($message);

  exit;
}

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

  (
    my ($DB) = &DB::BinarySelect (
      TABLE   => "Groups",
      COLUMNS => ["ID", "TITLE"],
      KEY     => $FORM->{'GROUP'}
    )
  ) || &HTML::Error("Members Accounts Management / Create a new member account", "No group available for you to choose yet.<br>Sorry, you have to <a href=\"$SYSTEM::CP_URL/Panel.$SYSTEM::EXT?action=CreateGroup\" target=\"UBBODY\">create a member group</a> first!");

  my $Group = $DB->{'TITLE'};

  my %Level = (
    30 => $GENERAL::MEMBER_LEVEL_30,
    50 => $GENERAL::MEMBER_LEVEL_50,
    70 => $GENERAL::MEMBER_LEVEL_70,
  );

  my %Status = (
    30 => "Deactivated",
    50 => "Activated"  ,
  );

  &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> / Members Accounts Management / Create a new member account
    </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 member account has been created, following is the information of this member account.</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>Username</td>
                      <td width="100%">$FORM->{'USERNAME'}</td>
                    </tr>
                    <tr>
                      <td width="100" valign="top" nowrap>Nickname</td>
                      <td width="100%">$FORM->{'NICKNAME'}</td>
                    </tr>
                    <tr>
                      <td width="100" valign="top" nowrap>Password</td>
                      <td width="100%">$FORM->{'PASSWORD'}</td>
                    </tr>
                    <tr>
                      <td width="100" valign="top" nowrap>Level</td>
                      <td width="100%">$Level{ $FORM->{'LEVEL'} }</td>
                    </tr>
                    <tr>
                      <td width="100" valign="top" nowrap>Member Group</td>
                      <td width="100%">$Group</td>
                    </tr>
                    <tr>
                      <td width="100" valign="top" nowrap>Email</td>
                      <td width="100%">$FORM->{'EMAIL'}</td>
                    </tr>
                    <tr>
                      <td width="100" valign="top" nowrap>Status</td>
                      <td width="100%">$Status{ $FORM->{'STATUS'} }</td>
                    </tr>
                  </table>
                </td>
              </tr>
            </table>
          </td>
        </tr>
      </table>
    </td>
  </tr>
</table>
</body>
</html>
HTML

  exit;
}

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