###############################################################################
# CP_DoDisApproveAccounts.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 DoDisApproveAccounts {

  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");
    }
  }

# =========================================================================== #
# Approve/Disapprove members accounts from Database

  if ($UB::QUERY{'Submit'} eq "Disapprove") {
    my @Index;
    my @UIDS = $UB::CGI->param('UID');
    for (my $i = 0; $i <= $#UIDS; $i++) {
      (
        my ($Account) = &DB::BinarySelect (
          TABLE   => "Accounts",
          COLUMNS => ["USERNAME", "NICKNAME", "EMAIL", "GROUP"],
          KEY     => $UIDS[$i]
        )
      ) || &HTML::Error("Internal Database Error", $DB::Error);

      push (@Index , $UIDS[$i]);
      &Standard::UpdateFromList($SYSTEM::DB_PATH."/accounts.username", $Account->{'USERNAME'}, "");
      &Standard::UpdateFromList($SYSTEM::DB_PATH."/accounts.nickname", $Account->{'NICKNAME'}, "");
      &Standard::UpdateFromList($SYSTEM::DB_PATH."/accounts.email"   , $Account->{'EMAIL'}   , "");

      (
        &DB::Update (
          TABLE   => "Groups",
          VALUES  => {
            TOTAL_MEMBERS => "--\${TOTAL_MEMBERS}"
          },
          KEY     => $Account->{'GROUP'}
        )
      ) || &HTML::Error("Internal Database Error", $DB::Error);
    }

    (
      &DB::Delete (
        TABLE  => "Accounts",
        KEYS   => \@Index
      )
    ) || &HTML::Error("Internal Database Error", $DB::Error);

    (
      &DB::Delete (
        TABLE  => "Profiles",
        KEYS   => \@Index
      )
    ) || &HTML::Error("Internal Database Error", $DB::Error);
  } else {
    my @UIDS = $UB::CGI->param('UID');
    for (my $i = 0; $i <= $#UIDS; $i++) {
      (
        my ($Accounts) = &DB::BinarySelect (
          TABLE   => "Accounts",
          COLUMNS => ["USERNAME", "NICKNAME", "EMAIL", "GROUP", "REGISTERED_DATE", "REGISTERED_TIME", "IP", "HOST"],
          KEY     => $UIDS[$i]
        )
      ) || &HTML::Error("Internal Database Error", $DB::Error);

      my $Password = &Standard::RandomPassword();

      (
        &DB::Update (
          TABLE  => "Accounts",
          VALUES => {
            PASSWORD => $Password,
            APROVED  => 1
          },
          KEY    => $UIDS[$i]
        )
      ) || &HTML::Error("Internal Database Error", $DB::Error);

      if (($SYSTEM::SENDMAIL == 1) && ($SendMail == 1)) {
        my %ACCOUNT = (
          "ACCOUNT.USERNAME"        => $Accounts->{'USERNAME'}        ,
          "ACCOUNT.PASSWORD"        => $Password                      ,
          "ACCOUNT.GROUP"           => $Accounts->{'GROUP'}           ,
          "ACCOUNT.NICKNAME"        => $Accounts->{'NICKNAME'}        ,
          "ACCOUNT.EMAIL"           => $Accounts->{'EMAIL'}           ,
          "ACCOUNT.REGISTERED_DATE" => $Accounts->{'REGISTERED_DATE'} ,
          "ACCOUNT.REGISTERED_TIME" => $Accounts->{'REGISTERED_TIME'} ,
          "ACCOUNT.IP"              => $Accounts->{'IP'}              ,
          "ACCOUNT.HOST"            => $Accounts->{'HOST'}
        );

        &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      => $Accounts->{'EMAIL'}
        );
      }
    }
  }

  &Success();
}

###############################################################################
# Success function
# - function of printing success message of de/activating members accounts
sub Success {
  my $text = "approved";
     $text = "disapproved" if ($UB::QUERY{'Submit'} eq "Disapprove");

  &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>
    </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>Those members accounts have been $text.</td>
                    </tr>
                  </table>
                </td>
              </tr>
            </table>
          </td>
        </tr>
      </table>
    </td>
  </tr>
</table>
</body>
</html>
HTML

  exit;
}

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