###############################################################################
# CP_DoModifyAdministrator.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 File::Copy;

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

sub DoModifyAdministrator {

  my ($Account) = &DB::BinarySelect (
    TABLE   => "Accounts",
    COLUMNS => ["USERNAME", "NICKNAME", "EMAIL", "GROUP"],
    KEY     => $UB::MEMBER{'USERNAME'}
  );

  my ($Profile) = &DB::BinarySelect (
    TABLE   => "Profiles",
    COLUMNS => ["USERNAME", "PHOTO"],
    KEY     => $UB::MEMBER{'USERNAME'}
  );

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

  my $Address = "Members Accounts Management / Manage member accounts / Modify member account";

  # checking missing fields
  if ($UB::QUERY{'FORM_NICKNAME'}  eq "") { &HTML::Error ($Address, "You forgot to fill the \"Nickname\" field.") };
  if ($UB::QUERY{'FORM_GROUP'}     eq "") { &HTML::Error ($Address, "You forgot to choose the \"Group\" field.")  };
  if ($UB::QUERY{'FORM_EMAIL'}     eq "") { &HTML::Error ($Address, "You forgot to fill the \"Email\" field.")    };

  # checking invalid nickname
  if (
      ($UB::QUERY{'FORM_NICKNAME'} !~ /^[A-Za-z0-9_-\s]+$/) ||
      (length ($UB::QUERY{'FORM_NICKNAME'}) > 32)           ||
      (length ($UB::QUERY{'FORM_NICKNAME'}) <  2)
     )
  {
    &HTML::Error ($Address, "The nickname must be more than 2 characters and no more than 32 characters.");
  }

  if ($UB::QUERY{'FORM_PASSWORD'}) {
    # checking invalid password
    if (
        ($UB::QUERY{'FORM_PASSWORD'} !~ /^[A-Za-z0-9]+$/)  ||
        (length ($UB::QUERY{'FORM_PASSWORD'}) > 16)        ||
        (length ($UB::QUERY{'FORM_PASSWORD'}) <  4)
       )
    {
      &HTML::Error ($Address, "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\-]+/) {
    &HTML::Error ($Address, "The email address must be contain letters, numbers, or '_' character only.");
  }

  # checking invalid homepage
  if ($UB::QUERY{'FORM_HOMEPAGE'} ne "") {
    if ($UB::QUERY{'FORM_HOMEPAGE'} !~ /^http:\/\/.+/) {
      &HTML::Error ($Address, "The homepage address must be start with 'http://'.");
    }
  }

  # checking invalid age
  if ($UB::QUERY{'FORM_AGE'} ne "") {
    if ($UB::QUERY{'FORM_AGE'} =~ /\D/) {
      &HTML::Error ($Address, "The age is invalid.");
    }
  }

  # checking invalid icq number
  if ($UB::QUERY{'FORM_ICQ'} ne "") {
    if ($UB::QUERY{'FORM_ICQ'} =~ /\D/) {
      &HTML::Error ($Address, "The icq number is invalid.");
    }
  }

  # checking invalid comments
  if (length($UB::QUERY{'FORM_COMMENTS'}) > $GENERAL::MAX_COMMENTS_LENGTH) {
    &HTML::Error ($Address, "The comments must be no more than $GENERAL::MAX_COMMENTS_LENGTH characters.");
  }

  # checking invalid signature
  if (length($UB::QUERY{'FORM_SIGNATURE'}) > $GENERAL::MAX_SIGNATURE_LENGTH) {
    &HTML::Error ($Address, "The signature must be no more than $GENERAL::MAX_SIGNATURE_LENGTH characters.");
  }

  my $Photo;
  my ($PhotoFile, $tmpPhotoFile);
  if ($UB::QUERY{'FORM_PHOTO_ACTION'} eq "UPLOAD") {
    $PhotoFile = $UB::CGI->param('FORM_PHOTO');
    if ($PhotoFile) {
      my $Type = $UB::CGI->uploadInfo($PhotoFile)->{'Content-Type'};

      if (($Type ne "image/gif") && ($Type ne "image/jpeg") && ($Type ne "image/pjpeg") && ($Type ne "image/x-png")) {
        &HTML::Error ($Address, "The profile photo must be saved as .gif, .jpg, or .png file, the size cannot larger than $GENERAL::MAX_PHOTO_X x $GENERAL::MAX_PHOTO_Y, and cannot larger than $GENERAL::MAX_PHOTO_LENGTH bytes.");
      }

      $tmpPhotoFile = $UB::CGI->tmpFileName($PhotoFile);

      my ($Size) = (stat ($tmpPhotoFile))[7];

      my ($X, $Y) = &Image::Size::imgsize($tmpPhotoFile);

      if (($X > $GENERAL::MAX_PHOTO_X) || ($Y > $GENERAL::MAX_PHOTO_Y)) {
        &HTML::Error ($Address, "The profile photo must be saved as .gif, .jpg, or .png file, the size cannot larger than $GENERAL::MAX_PHOTO_X x $GENERAL::MAX_PHOTO_Y, and cannot larger than $GENERAL::MAX_PHOTO_LENGTH bytes.");
      }

      if ($Size > ($GENERAL::MAX_PHOTO_LENGTH * 1024)) {
        &HTML::Error ($Address, "The profile photo must be saved as .gif, .jpg, or .png file, the size cannot larger than $GENERAL::MAX_PHOTO_X x $GENERAL::MAX_PHOTO_Y, and cannot larger than $GENERAL::MAX_PHOTO_LENGTH bytes.");
      }

      $Photo = "Photo-".$X."x".$Y."-".$UB::QUERY{'UID'}.".".$SYSTEM::MINE{$Type}[0];
    }
  }

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

  $FORM{'USERNAME'}   = $UB::MEMBER{'USERNAME'};

  if ($UB::QUERY{'FORM_PASSWORD'}) {
    $FORM{'PASSWORD'} = &Standard::CryptPassword($UB::MEMBER{'USERNAME'}, $UB::QUERY{'FORM_PASSWORD'});
  }

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

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

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

  $FORM{'LEVEL'}      = 100;                         # id of team members

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

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

  $FORM{'STATUS'}     = 50;

  $FORM{'HOMEPAGE'}   = $UB::QUERY{'FORM_HOMEPAGE'};
  $FORM{'HOMEPAGE'}   = &Standard::EncodeHTML($FORM{'HOMEPAGE'});

  $FORM{'LOCATION'}   = $UB::QUERY{'FORM_LOCATION'};
  $FORM{'LOCATION'}   = &Standard::EncodeHTML($FORM{'LOCATION'});

  $FORM{'OCCUPATION'} = $UB::QUERY{'FORM_OCCUPATION'};
  $FORM{'OCCUPATION'} = &Standard::EncodeHTML($FORM{'OCCUPATION'});

  $FORM{'INTEREST'}   = $UB::QUERY{'FORM_INTERESTS'};
  $FORM{'INTEREST'}   = &Standard::EncodeHTML($FORM{'INTEREST'});

  $FORM{'ICQ'}        = $UB::QUERY{'FORM_ICQ'};
  $FORM{'ICQ'}        = &Standard::EncodeHTML($FORM{'ICQ'});

  $FORM{'AGE'}        = $UB::QUERY{'FORM_AGE'};
  $FORM{'AGE'}        = &Standard::EncodeHTML($FORM{'AGE'});

  $FORM{'COMMENTS'}   = $UB::QUERY{'FORM_COMMENTS'};
  $FORM{'COMMENTS'}   = &Standard::EncodeHTML($FORM{'COMMENTS'});

  $FORM{'SIGNATURE'}  = $UB::QUERY{'FORM_SIGNATURE'};
  $FORM{'SIGNATURE'}  = &Standard::EncodeHTML($FORM{'SIGNATURE'});

  if ($UB::QUERY{'FORM_PHOTO_ACTION'} eq "UPLOAD") {
    $FORM{'PHOTO'}    = $Photo;
  } elsif ($UB::QUERY{'FORM_PHOTO_ACTION'} eq "REMOVE") {
    $FORM{'PHOTO'}    = "";
  }

# =========================================================================== #
# Update Member Information from Database

  if ($UB::QUERY{'FORM_PHOTO_ACTION'} eq "UPLOAD") {
    unlink ($SYSTEM::UPLOAD_PATH."/".$Profile->{'PHOTO'});

    chmod (0777, $SYSTEM::UPLOAD_PATH);

    copy ($tmpPhotoFile, $SYSTEM::UPLOAD_PATH."/".$Photo);
    close ($PhotoFile);

    chmod (0777, $SYSTEM::UPLOAD_PATH."/".$Photo);

  } elsif ($UB::QUERY{'FORM_PHOTO_ACTION'} eq "KEEP") {
    $FORM{'PHOTO'} = $Profile->{'PHOTO'};

  } elsif ($UB::QUERY{'FORM_PHOTO_ACTION'} eq "REMOVE") {
    unlink ($SYSTEM::UPLOAD_PATH."/".$Profile->{'PHOTO'});
  }

  ( # update member information from accounts database
    &DB::Update (
      TABLE   => "Accounts",
      VALUES  => {
        PASSWORD          => ($UB::QUERY{'FORM_PASSWORD'}) ? $FORM{'PASSWORD'} : "",
        NICKNAME          => $FORM{'NICKNAME'}  ,
        EMAIL             => $FORM{'EMAIL'}     ,
        GROUP             => $FORM{'GROUP'}     ,
        LEVEL             => $FORM{'LEVEL'}     ,
        GROUP_TEXT        => $FORM{'GROUP_TEXT'},
        LEVEL_TEXT        => $FORM{'LEVEL_TEXT'},
        HIDE_EMAIL        => $FORM{'HIDE_EMAIL'}
      },
      KEY     => $UB::MEMBER{'USERNAME'}
    )
  ) || &HTML::Error ("Internal Database Error", $DB::Error);

  ( # update member information from profile database
    &DB::Update (
      TABLE   => "Profiles",
      VALUES  => {
        HOMEPAGE    => $FORM{'HOMEPAGE'}  ,
        LOCATION    => $FORM{'LOCATION'}  ,
        OCCUPATION  => $FORM{'OCCUPATION'},
        INTEREST    => $FORM{'INTEREST'}  ,
        ICQ         => $FORM{'ICQ'}       ,
        AGE         => $FORM{'AGE'}       ,
        COMMENTS    => $FORM{'COMMENTS'}  ,
        SIGNATURE   => $FORM{'SIGNATURE'} ,
        PHOTO       => $FORM{'PHOTO'}
      },
      KEY     => $UB::MEMBER{'USERNAME'}
    )
  ) || &HTML::Error ("Internal Database Error", $DB::Error);

  if ($Account->{'GROUP'} ne $FORM{'GROUP'}) {
    &DB::Update (
      TABLE   => "Groups",
      VALUES  => {
        TOTAL_MEMBERS => "\${TOTAL_MEMBERS} - 1",
      },
      KEY     => $Account->{'GROUP'}
    );

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

  &Standard::UpdateFromList($SYSTEM::DB_PATH."/accounts.nickname", $Account->{'NICKNAME'}, $FORM{'NICKNAME'}) if ($FORM{'NICKNAME'} ne $Account->{'NICKNAME'});
  &Standard::UpdateFromList($SYSTEM::DB_PATH."/accounts.email"   , $Account->{'EMAIL'}   , $FORM{'EMAIL'}   ) if ($FORM{'EMAIL'}    ne $Account->{'EMAIL'}   );

  &Success(\%FORM);
}

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

  (
    my ($DB) = &DB::BinarySelect (
      TABLE   => "Groups",
      COLUMNS => ["ID", "TITLE"],
      KEY     => $FORM->{'GROUP'}
    )
  ) || &HTML::Error("Members Accounts Management / Manage member accounts / Modify 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'};

  if (($FORM->{'PHOTO'}) && (($UB::QUERY{'FORM_PHOTO_ACTION'} eq "UPLOAD") || ($UB::QUERY{'FORM_PHOTO_ACTION'} eq "KEEP"))) {
    $FORM->{'PHOTO'} = "<a href=\"$SYSTEM::UPLOAD_URL/".$FORM->{'PHOTO'}."\" target=\"PHOTO\">$FORM->{'PHOTO'}</a><br>";
  } elsif (($UB::QUERY{'FORM_PHOTO_ACTION'} ne "UPLOAD") && ($UB::QUERY{'FORM_PHOTO_ACTION'} ne "KEEP")) {
    $FORM->{'PHOTO'} = "n/a";
  }

  $FORM->{'SIGNATURE'}  = &UltraBoard::Encode($FORM->{'SIGNATURE'}, 1);

  &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 / Manage member account / Modify member acccount
    </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 administrator account has been modified, following is the information of administrator 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>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>
                  </table>
                </td>
              </tr>
            </table>
            &nbsp;<br>
            &nbsp;&nbsp;Personal 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>Homepage</td>
                      <td width="100%">$FORM->{'HOMEPAGE'}</td>
                    </tr>
                    <tr>
                      <td width="100" valign="top" nowrap>Location</td>
                      <td width="100%">$FORM->{'LOCATION'}</td>
                    </tr>
                    <tr>
                      <td width="100" valign="top" nowrap>Occupation</td>
                      <td width="100%">$FORM->{'OCCUPATION'}</td>
                    </tr>
                    <tr>
                      <td width="100" valign="top" nowrap>Interests</td>
                      <td width="100%">$FORM->{'INTERESTS'}</td>
                    </tr>
                    <tr>
                      <td width="100" valign="top" nowrap>ICQ Number</td>
                      <td width="100%">$FORM->{'ICQ'}</td>
                    </tr>
                    <tr>
                      <td width="100" valign="top" nowrap>Age</td>
                      <td width="100%">$FORM->{'AGE'}</td>
                    </tr>
                    <tr>
                      <td width="100" valign="top" nowrap>Comments</td>
                      <td width="100%">$FORM->{'COMMENTS'}</td>
                    </tr>
                    <tr>
                      <td width="100" valign="top" nowrap>Signature</td>
                      <td width="100%">$FORM->{'SIGNATURE'}</td>
                    </tr>
                    <tr>
                      <td width="100" valign="top" nowrap>Photo</td>
                      <td width="100%">$FORM->{'PHOTO'}</td>
                    </tr>
                  </table>
                </td>
              </tr>
            </table>
          </td>
        </tr>
      </table>
    </td>
  </tr>
</table>
</body>
</html>
HTML

  exit;
}

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