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

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

sub DoModifyProfile {
  &Login unless ($UB::MEMBER{'LOGIN'} == 1);

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

  # checking missing fields
  if ($UB::QUERY{'FORM_NICKNAME'}       =~ /^\s*$/) { &Error("MISS_NICKNAME"        ) };
  if ($UB::QUERY{'FORM_EMAIL'}          =~ /^\s*$/) { &Error("MISS_EMAIL"         ) };

  # 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)
     )
  {
    &Error("INVALID_NICKNAME");
  }

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

  # checking invalid homepage
  if ($UB::QUERY{'FORM_HOMEPAGE'} ne "") {
    if ($UB::QUERY{'FORM_HOMEPAGE'} !~ /^http:\/\/.+/) {
      &Error("INVALID_HOMEPAGE");
    }
  }

  # checking invalid age
  if ($UB::QUERY{'FORM_AGE'} ne "") {
    if ($UB::QUERY{'FORM_AGE'} =~ /\D/) {
      &Error("INVALID_AGE");
    }
  }

  # checking invalid icq number
  if ($UB::QUERY{'FORM_ICQ'} ne "") {
    if ($UB::QUERY{'FORM_ICQ'} =~ /\D/) {
      &Error("INVALID_ICQ");
    }
  }

  # checking invalid comments
  if (length($UB::QUERY{'FORM_COMMENTS'}) > $GENERAL::MAX_COMMENTS_LENGTH) {
    &Error("INVALID_COMMENTS");
  }

  # checking invalid signature
  if (length($UB::QUERY{'FORM_SIGNATURE'}) > $GENERAL::MAX_SIGNATURE_LENGTH) {
    &Error("INVALID_SIGNATURE");
  }

  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")) {
        &Error("INVALID_PHOTO");
      }

      $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)) {
        &Error("INVALID_PHOTO");
      }

      if ($Size > ($GENERAL::MAX_PHOTO_LENGTH * 1024)) {
        &Error("INVALID_PHOTO");
      }

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

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

  $FORM{'USERNAME'} = $UB::QUERY{'UID'};

  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{'IP'}         = $ENV{'REMOTE_ADDR'};

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

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

# =========================================================================== #
# Censor Words Filter

  ( $FORM{'LOCATION'}   ,
    $FORM{'OCCUPATION'} ,
    $FORM{'INTEREST'}   ,
    $FORM{'COMMENTS'}   ,
    $FORM{'SIGNATURE'}
  ) = &Standard::WordFilter (
    $FORM{'LOCATION'}   ,
    $FORM{'OCCUPATION'} ,
    $FORM{'INTEREST'}   ,
    $FORM{'COMMENTS'}   ,
    $FORM{'SIGNATURE'}
  );

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

  if ($FORM{'NICKNAME'} ne $UB::MEMBER{'NICKNAME'}) {
    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 ($FORM{'EMAIL'} ne $UB::MEMBER{'EMAIL'}) {
    if ( ( ($UB::MEMBER{'LOGIN'} == 1) && ($UB::MEMBER{'LEVEL'} ne "70") && ($UB::MEMBER{'LEVEL'} ne "100") ) || ($UB::MEMBER{'LOGIN'} == 0)) {
      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 = ();
      }
    }
  }

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

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

  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) if ($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'}  : undef,
        NICKNAME          => $FORM{'NICKNAME'}  ,
        EMAIL             => $FORM{'EMAIL'}     ,
        HOST              => $FORM{'HOST'}      ,
        IP                => $FORM{'IP'}        ,
        HIDE_EMAIL        => $FORM{'HIDE_EMAIL'}
      },
      KEY     => $UB::MEMBER{'USERNAME'}
    )
  ) || &HTML::Error(MESSAGE => $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(MESSAGE => $DB::Error);

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

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

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

  my %ACCOUNT = (
    "CATEGORY.ID"     => $UB::QUERY{'CID'} || ""      ,
    "BOARD.ID"        => $UB::QUERY{'BID'} || ""      ,
    "TOPIC.ID"        => $UB::QUERY{'TID'} || ""      ,
    "POST.ID"         => $UB::QUERY{'PID'} || ""      ,
    "DAY_PRUNE"       => $UB::QUERY{'DayPrune'} || "" ,
    "PAGE"            => $UB::QUERY{'P'} || ""        ,

    "ACCOUNT.USERNAME"          => $FORM{'USERNAME'}        ,
    "ACCOUNT.PASSWORD"          => $FORM{'PASSWORD'}        ,
    "ACCOUNT.NICKNAME"          => $FORM{'NICKNAME'}        ,
    "ACCOUNT.EMAIL"             => $FORM{'EMAIL'}           ,
    "ACCOUNT.HOMEPAGE"          => $FORM{'HOMEPAGE'}        ,
    "ACCOUNT.LOCATION"          => $FORM{'LOCATION'}        ,
    "ACCOUNT.OCCUPATION"        => $FORM{'OCCUPATION'}      ,
    "ACCOUNT.INTEREST"          => $FORM{'INTEREST'}        ,
    "ACCOUNT.ICQ"               => $FORM{'ICQ'}             ,
    "ACCOUNT.AGE"               => $FORM{'AGE'}             ,
    "ACCOUNT.COMMENTS"          => $FORM{'COMMENTS'}        ,
    "ACCOUNT.SIGNATURE"         => $FORM{'SIGNATURE'}       ,
    "ACCOUNT.PHOTO"             => $FORM{'PHOTO'}           ,

    &Standard::ExtraVariables()
  );

  &Success(\%ACCOUNT);
}

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

  require "$UB::SOURCES_PATH/UB_ModifyProfile.src";
  &Source::ModifyProfile($code);

  exit;
}

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

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

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

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