#!/usr/bin/perl

use strict;
use Template;
use CGI::Lite;
use FCGI;
use MIME::Lite;
use Digest::MD5 qw(md5_hex);
use Compress::Zlib;
use Time::HiRes qw(gettimeofday tv_interval);
use Net::DNS;
use String::Similarity;
                                
require LWP;
require "/www/mine.sk/wowaccount/modules/globals.pm";
require "/www/mine.sk/wowaccount/modules/sqldb.pm";
require "/www/mine.sk/wowaccount/modules/sessions.pm";
require "/www/mine.sk/wow/chat/modules/tt.pm";
require "/www/mine.sk/wowaccount/modules/system.pm";

use Geo::IP;

my $gi = Geo::IP->new(GEOIP_STANDARD);
my $ret_template = tt::init($globals::fs_dir.'/templates');
my $tt = $ret_template->{template};
my $dbh = sqldb::init($globals'mysql->{database},$globals'mysql->{interface},$globals'mysql->{user}, $globals'mysql->{password});
my $cgi = CGI::Lite->new();
my $md5 = Digest::MD5->new();
$cgi->set_directory ($globals::datadir.'/tmp');

my $request = FCGI::Request();



my $chat_server_id;

my $ret_wget = system::wget('http://www.anthoria.net/rules.php');
my $rules = {'18' => $ret_wget->{content}, };


$main::get_servers->execute();
my $servers = $main::get_servers->fetchall_arrayref({});
foreach my $server (@{$servers}) {
   $chat_server_id->{$server->{armory_server_id}} = $server->{server_id};
}
$main::get_servers->finish();

my $map_class_id_to_mask = {
	'1' => 1,
	'2' => 2,
	'3' => 4,
	'4' => 8,
	'5' => 16,
	'6' => 32,
	'7' => 64,
	'8' => 128,
	'9' => 256,
	'11' => 1024,
};

my $map_class_mask_to_id = {
	'1' => 1,
	'2' => 2,
	'4' => 3,
	'8' => 4,
	'16' => 5,
	'32' => 6,
	'64' => 7,
	'128' => 8,
	'256' => 9,
	'1024' => 11,
};


while($request->Accept() >= 0) {

#   my $utime = time();
   my $t0 = [gettimeofday];

   $dbh = &sqldb::check($dbh);
   if ($dbh eq 0) {
      warn "sql connection problem";
      print "Location: /chat/static.html\n\n";
      next;

   } else {


      my ($html,$page,$template,$mime);
      my $q = $cgi->parse_new_form_data();
      my $c = $cgi->parse_cookies();

      if (!defined($q->{lang_id}) || $q->{lang_id} !~ /^\d+$/) {$q->{lang_id} = 1;}
      if (!defined($q->{page}) || $q->{page} eq '') {$q->{page} = 'game_account';}

      my $template = $tt->{$q->{lang_id}};
      my $ccode = $gi->country_code_by_addr($ENV{REMOTE_ADDR});


      my $usegzip;
      foreach my $enc ( split( /\s*,\s*/, $ENV{HTTP_ACCEPT_ENCODING} )) {
         $enc =~ s/;.*$//s;
         $usegzip = $enc if ( $enc =~ /^(x-)?gzip$/ );
      }

      my $is_ssl = 0;
      if ($ENV{SERVER_PORT} == 443) {
         $is_ssl = 1;
      }

      my $hua = 0;
      if ($ENV{HTTP_USER_AGENT} =~ /MSIE/i) {
         $hua = 1;
      } elsif ($ENV{HTTP_USER_AGENT} =~ /Mozilla/i && $ENV{HTTP_USER_AGENT} !~ /MSIE/i) {
         $hua = 2;
      } elsif ($ENV{HTTP_USER_AGENT} =~ /Opera/i) {
         $hua = 3;
      }

      my $contenttype = 'text/html';


      if (!defined($q->{'s'}) || $q->{'s'} !~ /^\d+$/) {
         $q->{'s'} = 18;
      }


      $main::admin_get_server->execute($q->{'s'});
      my $server_info = $main::admin_get_server->fetchrow_hashref();
      $main::admin_get_server->finish();


      my $vars = {
		query => $q, 
		logged => 0,
		encoding => 'utf-8',
		gzip => $usegzip,
		is_ssl => $is_ssl,
		servers => $servers,
		doctype => 2,
		hua => $hua,
		server_info => $server_info,
      };



      my ($id,$login);

      if (!defined($q->{sid})) {
      } else {
         if (sessions::check_duplicate_session($dbh, { sid => $q->{sid}, ip => $ENV{REMOTE_ADDR}, }, ) ) {
         } else {
            sessions::prolong_session($dbh, {sid => $q->{sid}, }, );
            my $ret_session = sessions::get_session($dbh, { sid => $q->{sid}, ip => $ENV{REMOTE_ADDR}, }, );
            if ($ret_session->{ec} == 1) {
               if ($ret_session->{account_id} != 0) {
#warn "$ret_session->{session_id} $ret_session->{server_id} $ret_session->{account_id} LOGGED";
                  $vars->{session_id} = $ret_session->{session_id};
                  $vars->{server_id} = $ret_session->{server_id};
                  $vars->{account_id} = $ret_session->{account_id};
                  $vars->{logged} = 1;
                  $vars->{login} = $ret_session->{login};

warn "get_game_account_by_guid->execute($vars->{server_id},$vars->{account_id})";

                  $main::get_game_account_by_guid->execute($vars->{server_id},$vars->{account_id});
                  my $game_account = $main::get_game_account_by_guid->fetchrow_hashref();
                  $main::get_game_account_by_guid->finish();

                  $id = $game_account->{id};

#                  if (!defined($id)) {
#
#                     my $ret_wget = system::wget($server_info->{activations_url}.'?login='.$vars->{login}.'&action=get_gmlevel_by_username');
#
#                     if ($ret_wget->{content} =~ m/^([0-9]+)\|(\d+)\|(.*?)\|(\d+)\|(\d+)\|(\d+)$/) {
#                        my $account_id = $6;
#                        $main::set_game_account_account_id->{$account_id}
#warn "set_game_account_account_id->{$account_id},$vars->{account_id})";
#
#
#                     }
#                  }

                  $login = $vars->{login};

                  

#warn $login." Logged";


               } else {
                  $vars->{logged} = 0;
               }
            }
         }
      }




      my $hf = 0;
      my $location = '';
      my $set_cookie = '';

      my $browser_hash_id;

      $main::get_browser_hash_id->execute(md5_hex($ENV{HTTP_USER_AGENT}));
      if ($main::get_browser_hash_id->rows() == 0) {
         $main::add_browser_hash->execute(md5_hex($ENV{HTTP_USER_AGENT}),$ENV{HTTP_USER_AGENT});
         $browser_hash_id = $main::add_browser_hash->{mysql_insertid};
      } else {
         $browser_hash_id = $main::get_browser_hash_id->fetchrow();
      }



# create_session

=vut
warn $q->{page}.'!!!!!!';
      my $allow = 0;


      if ($q->{page} eq 'security' ||
         $q->{page} eq 'index' ||
         $q->{page} eq 'game_account' ||
         $q->{page} eq 'account_state' ||
         $q->{page} eq 'account_state_rules' ||
         $q->{page} eq 'account_state_characters' ||
         $q->{page} eq 'confirm_email' ||
         $q->{page} eq 'account_state_expansion' ||
         $q->{page} eq 'account_state_character_questlog' ||


         ($q->{page} eq 'redirect' && $q->{action} eq 'login' ) ||
         ($q->{page} eq 'redirect' && $q->{action} eq 'restore_char' )


      ) {
         $allow = 1;
      }

      if ($allow == 0) {
         print "Content-type: text/html\n\nTato cast account managementu je docasne nepristupna. a pochopenie dakujeme"; next;
      }

=cut

      if ($q->{page} eq 'index') {
         $hf = 1;
         $location = '';
         $vars->{title} = 22;


         if ($q->{v} == 2) {
            $vars->{background} = 'http://www.anthoria.net/themes/default/bgbg.jpg';
         } else {
            $vars->{background} = 'http://wow.mine.sk/temp/testbg.jpg';
         }


         $page = 'index';


      } elsif ($q->{page} eq 'redirect') {
         $hf = 0;

         if (defined($q->{action}) && $q->{action} eq 'login') {


            my $hash = md5_hex($q->{'password'});

            $main::get_game_account_state_by_login_password_hash->execute($q->{login},$hash);
            if ($main::get_game_account_state_by_login_password_hash->rows() == 1) {


                  my $game_account_state = $main::get_game_account_state_by_login_password_hash->fetchrow_hashref();


                  my $sid = sessions::create_session( $dbh, { server_id => $q->{'s'}, account_id =>  $game_account_state->{id}, ip => $ENV{REMOTE_ADDR}, login => $q->{login}, } );

                  sessions::set_session_data( $dbh, { session_hex => $sid, name => 'local_account_id', value => $game_account_state->{id}, }, );

                  $location = $globals::index_path.'?page=account_state&sid='.$sid;


            } else {
               $location = $globals::index_path.'?page=account_state&error=1';

            }

         } elsif (defined($q->{action}) && $q->{action} eq 'remove') {

            $main::unsubscribe->execute($q->{email},$q->{id});

            $location = $globals::index_path.'?page=delete';

         } elsif (defined($q->{action}) && $q->{action} eq 'logout') {

            sessions::remove_session( $dbh, { session_id => $vars->{session_id}, }, );

            $location = $globals::index_path.'?s='.$q->{'s'};


         } elsif (defined($q->{action}) && ($q->{action} eq 'send_offer' || $q->{action} eq 'cancel_offer' || $q->{action} eq 'reject_offer')) {



            my $ret_wget = system::wget($server_info->{activations_url}.'?account_id='.$vars->{account_id}.'&action=get_account_characters');
            if ($ret_wget->{content} =~ /^0$/) {

            } else {
               my $tmp;

               @{$tmp} = split(/\n/,$ret_wget->{content});
               my $owned_char;

               foreach (@{$tmp}) {
                  my ($guid,$name,$race,$class,$level,$online,$backuped,$backup_version) = split(/\|/,$_);
                  $owned_char->{$guid} = 1;
               }


#warn "   owned_char->{$q->{start_id}            if ($owned_char->{$q->{start_id}} == 1) {";


               if ($owned_char->{$q->{start_id}} == 1) {


                  $main::get_player_by_guid->execute($vars->{server_id},$q->{start_id});
                  my $start = $main::get_player_by_guid->fetchrow_hashref();
                  $main::get_player_by_guid->finish();

                  $main::get_player_by_guid->execute($vars->{server_id},$q->{dest_id});
                  my $dest = $main::get_player_by_guid->fetchrow_hashref();
                  $main::get_player_by_guid->finish();

                  my $src = $start->{name}.' (level '.$dest->{level}.' '.$globals::races->{$start->{race}}->{name}.' '.$globals::classes->{$start->{class}}.')';
                  my $dst = $dest->{name}.' (level '.$dest->{level}.' '.$globals::races->{$dest->{race}}->{name}.' '.$globals::classes->{$dest->{class}}.')';

#warn $q->{action};

                  if ($q->{action} eq 'send_offer') {
                     $main::is_offer_already_sent->execute($vars->{session_id},$q->{start_id},$q->{dest_id});
                     if ($main::is_offer_already_sent->rows() == 0) {

                        $main::add_game_accounts_trader_log->execute($vars->{server_id},$q->{start_id},$q->{dest_id},1,'Zaslal si ponuku '.$dst.' na vymenu za '.$src );
                        $main::add_game_accounts_trader_log->execute($vars->{server_id},$q->{dest_id},$q->{start_id},2,$src.' ti zaslal ponuku na vymenu za '.$dst );

                        my $ret_wget = system::wget($server_info->{activations_url}.'?guid='.$q->{dest_id}.'&action=get_account_by_character_guid');
                        if ($ret_wget->{content} =~ /^0$/) {

                        } else {
                           my $char_account_id = $ret_wget->{content};

                           $main::add_game_accounts_trader_offer->execute($vars->{server_id},$q->{start_id},$vars->{account_id},$q->{dest_id},$char_account_id);
                        }
                     }
                  } elsif ($q->{action} eq 'reject_offer') {

                     $main::add_game_accounts_trader_log->execute($vars->{server_id},$q->{dest_id},$q->{start_id},3,'Zamietol si ponuku '.$dst.') na vymenu za '.$src );
                     $main::add_game_accounts_trader_log->execute($vars->{server_id},$q->{start_id},$q->{dest_id},4,$src.' zamietol tvoju ponuku na vymenu za '.$dst );

                     $main::del_game_accounts_trader_offer->execute($q->{id},$vars->{server_id},$q->{dest_id},$q->{start_id});

                  } elsif ($q->{action} eq 'cancel_offer') {
                     $main::add_game_accounts_trader_log->execute($vars->{server_id},$q->{start_id},$q->{dest_id},5,'Stornoval si svoju ponuku '.$src.' na vymenu za '.$dst );
                     $main::add_game_accounts_trader_log->execute($vars->{server_id},$q->{dest_id},$q->{start_id},6,$src.' stornoval svoju ponuku na vymenu za '.$dst );

                     $main::del_game_accounts_trader_offer->execute($q->{id},$vars->{server_id},$q->{start_id},$q->{dest_id});

                  }


               }

            }

            $location = $globals::index_path.'?page=account_state_chartrader&sid='.$q->{sid};

         } elsif (defined($q->{action}) && $q->{action} eq 'restore_char') {



            if ($q->{restore_id} ne '') {
               my $ret_wget = system::wget($server_info->{activations_url}.'?action=restore_deleted_character&account_id='.$vars->{account_id}.'&guid='.$q->{restore_id});
               if ($ret_wget->{content} =~ /^1$/) {
                  $main::add_manager_log->execute('1',$vars->{account_id},$q->{restore_id});
               } else {
               }
            }

            $location = $globals::index_path.'?page=account_state_characters&sid='.$q->{sid};


         }

      } elsif ($q->{page} eq 'delete') {
#         $page = 'game_account';
         $hf = 1;
         $location = '';
         $vars->{title} = 22;

         $page = 'deleted';

      } elsif ($q->{page} eq 'game_account_invitation') {
#         $page = 'game_account';
         $hf = 1;
         $location = '';
         $vars->{title} = 22;

         $page = 'game_account_invitation';


         if ($q->{v} == 2) {
            $vars->{background} = 'http://www.anthoria.net/themes/default/bgbg.jpg';
         } else {
            $vars->{background} = 'http://wow.mine.sk/temp/testbg.jpg';
         }


         if (defined($q->{action}) && $q->{action} eq 'check') {

            my @errors;

            $main::get_nick->execute($q->{'login_invitation'},$q->{'password_invitation'});
            if ($main::get_nick->rows() != 1) {push @errors,'0';}
            my $nick_data = $main::get_nick->fetchrow_hashref();
            if ($nick_data->{state} == 0) {push @errors,'1';}
            $main::get_nick->finish();
            my $nick_id = $nick_data->{id};

            if (scalar(@errors) == 0) {

                        $main::create_invitation->execute();
                        my $invitation_id = $main::create_invitation->{mysql_insertid};


                        my $md5 = Digest::MD5->new;
                        $md5->add(lc($q->{'login_invitation'}));
                        $md5->add($invitation_id);
                        $md5->add($nick_id);
                        $md5->add('invite');
                        $md5->add('mega coder SoCo');
                        my $hash = $md5->hexdigest;

                        $main::update_invitation->execute($hash,undef,$vars->{server_id},undef,$nick_id,lc($q->{'login_invitation'}),$q->{'email_invitation'},$invitation_id);


                        my $msg = MIME::Lite->new(
				From			=> 'WebAccountInvitation',
				To			=> $q->{email_invitation},
				Subject			=> 'Pozvanka na '.$server_info->{server_name}.' (Automaticky generovany E-mail)',
				Type			=> 'text/plain',
        	                'Auto-Submitted:'	=> 'auto-generated',
                	        'X-Originating-IP:'	=> $ENV{REMOTE_ADDR},
				Data			=> 'Tento mail Vam bol odoslany na zaklade pozvania uzivatela serveru.'."\n\n".'ID Vasej pozvanky je: '.uc($hash)."\n\n".'Zaregistrovat sa mozete kliknutim na: '.$globals::index_path.'?page=game_account&invitation='.uc($hash).'&s='.$vars->{server_id}.' alebo neskor '.$globals::index_path.'?page=game_account&s='.$vars->{server_id}.' a vpisanim ID pozvanky do prislusneho policka.'."\n".'Ak neplanujete hrat na serveri '.$server_info->{server_name}.', povazujte tento E-mail za bezpredmetny'."\n\n".'Na tento E-mail neodpovedajte!'."\n\n".'S pozdravom Team '.$server_info->{server_name},
                        );
                        $msg->send("sendmail");

                   $vars->{ok} = 1;

            } else {
               $vars->{errors} = \@errors;
            }
         }



      } elsif ($q->{page} eq 'security') {
         $page = 'security';
         $hf = 1;
         $location = '';
         $vars->{title} = 22;

         $main::get_security_articles->execute();
         my $articles = $main::get_security_articles->fetchall_arrayref({});
         $main::get_security_articles->finish();
         $vars->{articles} = $articles;


      } elsif ($q->{page} eq 'game_account') {
#         $page = 'game_account';
         $hf = 1;
         $location = '';
         $vars->{title} = 22;



         if ($q->{v} == 2) {
            $vars->{background} = 'http://www.anthoria.net/themes/default/bgbg.jpg';
            $vars->{background} = 'http://wow.mine.sk/temp/testbg.jpg';
            $page = 'game_account';
         } else {
            $vars->{background} = 'http://wow.mine.sk/temp/testbg.jpg';
            $page = 'game_account';
         }
         
         if (!defined($vars->{server_id}) || $vars->{server_id} !~ /^\d+$/) {
            $vars->{server_id} = 18;
         }


         $main::get_today_created_game_accounts->execute($vars->{server_id});
         my $status = $main::get_today_created_game_accounts->fetchrow_hashref();
         $vars->{status} = $status;



         $vars->{rules} = $rules->{$vars->{server_id}};



         $vars->{post_title} = $server_info->{server_name};

         if (defined($q->{action}) && $q->{action} eq 'check') {
            my @errors;
            if ($q->{login} !~ /^[a-zA-Z0-9]+$/) {push @errors,'0';}
            if (length($q->{login}) < 2) {push @errors,'1';}
            if (length($q->{login}) > 16) {push @errors,'2';}
            if ($q->{login} !~ /^[a-zA-Z]/) {push @errors,'3';}
            if ($q->{login} !~ /[a-zA-Z0-9]$/) {push @errors,'4';}
#            if ($q->{email} !~ m/^['_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*\.(([a-z]{2,3})|(aero|coop|info|museum|name))$/i) {push @errors,'5';}
            if ($q->{email} !~ m/.+?\@gmail.com$/i) {push @errors,'23';}

            if (length($q->{password}) < 4) {push @errors,'6';}
            if (similarity (lc $q->{login}, lc $q->{password}) > 0.75) {push @errors,'7';}
            if (lc $ccode ne 'sk' && lc $ccode ne 'cz') {
               my $md5 = Digest::MD5->new;
               $md5->add($q->{salt});
               $md5->add('mega coder SoCo');
               my $ok_hash = $md5->hexdigest;
               if ($ok_hash ne $q->{ok_hash}) {
                  push @errors,'8';
               }
            }
            $main::is_game_account->execute($vars->{server_id},$q->{login});
            if ($main::is_game_account->rows() != 0) {push @errors,'9';}
            $main::is_game_account->finish();
            if ($q->{agree} ne '1') {push @errors,'10';}
            if ($status->{rph} > 20) {push @errors,'11';}
            if ($server_info->{registrations} != 1) {push @errors,'12';}
            $main::is_game_email_used->execute($q->{email},$vars->{server_id});
            if ($main::is_game_email_used->rows() != 0) {push @errors,'13';}
            if ($q->{question} eq '') {push @errors,'14';}
            if ($q->{answer} eq '') {push @errors,'15';}
            if ($q->{question} eq $q->{answer}) {push @errors,'19';}
            if ($q->{name} eq '') {push @errors,'20';}
            if ($q->{surname} eq '') {push @errors,'21';}
            if ($q->{address} eq '') {push @errors,'22';}

#            $main::get_invitation_by_hex->execute($q->{invitation});
#            my $invitation_ok = $main::get_invitation_by_hex->rows();
#            my $invitation = $main::get_invitation_by_hex->fetchrow_hashref();
            my $invitation;
            $invitation->{id} = 0;
#            if ($invitation->{used} != 0) {push @errors,'16';}
#            if ($invitation_ok == 0) {push @errors,'17';}

#            if (defined($invitation->{account_id}) && $invitation->{account_id} ne '') {
#               $main::get_game_account_state_by_id->execute($invitation->{account_id},$invitation->{server_id});
#               my $account_data = $main::get_game_account_state_by_id->fetchrow_hashref();
#               if ($account_data->{invited} != 0) {push @errors,'18';}
#            } elsif (defined($invitation->{nick_id}) && $invitation->{nick_id} ne '') {
#               $main::check_chat_invitation_used->execute($invitation->{nick_id});
#               if ($main::check_chat_invitation_used->rows() > 0) {push @errors,'18';}
#            }


            if (scalar(@errors) == 0) {

               my $arr = [qw/0 1 2 3 4 5 6 7 8 9 0 q w e r t y u i o p a s d f g h j k l z x c v b n m/];
               my $buf;
               for (my $i = 0; $i<=64; ++$i) {
                  $buf .= $arr->[rand(scalar(@{$arr}))];
               }



               my $hash = md5_hex($q->{password});
               $main::create_game_account->execute($vars->{server_id},$browser_hash_id,$ENV{REMOTE_ADDR},$q->{login},$hash,$q->{password},$q->{email},$q->{chat_nick},$q->{forum_nick},$q->{name},$q->{surname},$q->{address},$q->{phone},$q->{question},$q->{answer},$invitation->{id},$buf);
               my $record_id = $main::create_game_account->{mysql_insertid};
               $page = 'game_account_ok';

               my $md5 = Digest::MD5->new;
               $md5->add($q->{login});
               $md5->add($q->{email});
               $md5->add($record_id);
               $md5->add('mega coder SoCo');
               my $hash = $md5->hexdigest;


#               $main::set_invitation_used->execute($invitation->{id});
#               $main::set_account_invited->execute($invitation->{account_id});


               my $msg = MIME::Lite->new(
			From			=> 'WebAccountActivation',
			To			=> $q->{email},
			Subject			=> 'Overenie E-mailovej adresy (Automaticky generovany E-mail)',
			Type			=> 'text/plain',
			'Auto-Submitted:'	=> 'auto-generated',
			'X-Originating-IP:'	=> $ENV{REMOTE_ADDR},
			Data			=> 'Tento mail Vam bol odoslany na zaklade odoslania registracneho formularu so ziadostou o vytvorenie herneho konta na serveri WoW '.$server_info->{server_name}.' (z IP adresy '.$ENV{REMOTE_ADDR}.'). Ak ste ho odosielali Vy, kliknite na nasledovnu URL adresu a tym potvrdite ze ste majitelom tejto E-mailovej adresy '.$globals::index_path.'?page=confirm_email&hex='.$hash.'&id='.$record_id.'&s='.$vars->{server_id}."\n".'V pripade, ze ste o ziadne konto neziadali, povazujte tento E-mail za bezpredmetny.'."\n\n".'Na tento E-mail neodpovedajte!'."\n\n".'S pozdravom Team '.$server_info->{server_name},
               );

               $msg->send;

  

            } else {
               $vars->{errors} = \@errors;
            }
         }


         my $matrix;

         if (lc $ccode ne 'sk' && lc $ccode ne 'cz') {
            my $arr = [qw(q w e r t y u i o p a s d f g h j k l z x c v b n m 0 1 2 3 4 5 6 7 8 9)];
            my $ok_x = 1+int(rand(10));
            my $ok_y = 1+int(rand(10));
            my $ok_hash;
            for (my $y=1;$y<=10;++$y) {
               for (my $x=1;$x<=10;++$x) {
                  my $salt;
                  for (my $i=0; $i<5; ++$i) {$salt .= $arr->[int(rand(scalar(@{$arr})))]}
                  $matrix->{$x}->{$y} = $salt;
                  if ($x == $ok_x && $y == $ok_y) {
                     my $md5 = Digest::MD5->new;
                     $md5->add($salt);
                     $md5->add('mega coder SoCo');
                     $ok_hash = $md5->hexdigest;
                  }

               }
            }
            $vars->{matrix} = $matrix;
            $vars->{ok_hash} = $ok_hash;
            $vars->{ok_x} = $ok_x;
            $vars->{ok_y} = $ok_y;
         }


      } elsif ($q->{page} eq 'change_email_finish') {

         my $errors;


         my $ret_wget = system::wget($server_info->{activations_url}.'?login='.$q->{login}.'&password='.html_encode($q->{password}).'&action=check_by_password_and_login');

         if ($ret_wget->{content} =~ /^([^0]\d*)\|([0-9a-f_]+)$/) {
            my $account_id = $1;
            my $version = $2;

            my $sid = sessions::create_session( $dbh, { server_id => $q->{'s'}, account_id => $account_id, ip => $ENV{REMOTE_ADDR}, login => $q->{login}, } );
            $location = $globals::index_path.'?page=account_state_email&action=change_email_finish&sid='.$sid.'&hex='.$q->{hex}.'&new_email='.$q->{new_email};
         } else {
            $page = 'change_email_finish_bad';
         }



      } elsif ($q->{page} eq 'account_state_invitation') {
         $page = 'account_state';
         $vars->{input_page} = 'account_state_invitation';
         $hf = 1;
         $vars->{title} = 68;
         $location = '';
         $vars->{background} = 'http://wow.mine.sk/temp/testbg.jpg';

         if (!defined($vars->{server_id}) || $vars->{server_id} !~ /^\d+$/) {
            $vars->{server_id} = 18;
         }


         if ($vars->{logged} == 1) {

               my $ret_wget = system::wget($server_info->{activations_url}.'?account_id='.$vars->{account_id}.'&action=get_gmlevel_by_account_id');
               $ret_wget->{content} =~ m/^([0-9a-f_]+)\|(\d+)\|(\d+)\|(\d+)\|(.*)\|(\d+)\|(\d+)$/;
               my $version = $1;
               my $gmlevel = $2;
               my $vip = $3;
               my $locked = $4;
               my $last_ip = $5;
               my $expansion = $6;
               my $banned = $7;


               $vars->{gmlevel} = $gmlevel;
               $vars->{version} = $version;
               $vars->{vip} = $vip;
               $vars->{locked} = $locked;
               $vars->{last_ip} = $last_ip;
               $vars->{expansion} = $expansion;
               $vars->{banned} = $banned;


               if (defined($q->{action}) && $q->{action} eq 'send_invitation') {

                  my $level_enough = 0;

                  my $ret_wget = system::wget($server_info->{activations_url}.'?account_id='.$vars->{account_id}.'&action=get_account_characters');
                  if ($ret_wget->{content} =~ /^0$/) {

                  } else {
                     my $tmp;

                     @{$tmp} = split(/\n/,$ret_wget->{content});
                     my $characters;
                     foreach (@{$tmp}) {
                        my ($guid,$name,$race,$class,$level,$online,$backuped,$backup_version) = split(/\|/,$_);
                        push @{$characters}, { guid => $guid, name => $name, race => $race, class => $class, level => $level, online => $online, backuped => $backuped, backup_version => $backup_version };
                        if ($level >= 55) {$level_enough = 1;}
                     }
                  }

                  if ($level_enough == 1 && $banned != 1) {

                     my $errors;

                     $main::get_game_account_state_by_id->execute($id,$vars->{server_id});
                     my $account_data = $main::get_game_account_state_by_id->fetchrow_hashref();
                        
                     my $ret_wget = system::wget($server_info->{activations_url}.'?login='.$login.'&password='.html_encode($q->{password}).'&action=set_email_by_user_and_password&email='.$account_data->{email});

                     if ($ret_wget->{content} =~ /^1$/) {
                        $main::get_game_account_state_by_login->execute($login,$vars->{server_id});
                        my $game_account_state = $main::get_game_account_state_by_login->fetchrow_hashref();

                        $main::create_invitation->execute();
                        my $invitation_id = $main::create_invitation->{mysql_insertid};


                        my $md5 = Digest::MD5->new;
                        $md5->add(lc($q->{'login'}));
                        $md5->add($invitation_id);
                        $md5->add($game_account_state->{id});
                        $md5->add('invite');
                        $md5->add('mega coder SoCo');
                        my $hash = $md5->hexdigest;

                        $main::update_invitation->execute($hash,$game_account_state->{'id'},$vars->{server_id},lc($q->{'login'}),undef,undef,$q->{'invite_email'},$invitation_id);


                        my $msg = MIME::Lite->new(
				From			=> 'WebAccountInvitation',
				To			=> $q->{invite_email},
				Subject			=> 'Pozvanka na '.$server_info->{server_name}.' (Automaticky generovany E-mail)',
				Type			=> 'text/plain',
        	                'Auto-Submitted:'	=> 'auto-generated',
                	        'X-Originating-IP:'	=> $ENV{REMOTE_ADDR},
				Data			=> 'Tento mail Vam bol odoslany na zaklade pozvania uzivatela serveru.'."\n\n".'ID Vasej pozvanky je: '.uc($hash)."\n\n".'Zaregistrovat sa mozete kliknutim na: '.$globals::index_path.'?page=game_account&invitation='.uc($hash).'&s='.$vars->{server_id}.' alebo neskor '.$globals::index_path.'?page=game_account&s='.$vars->{server_id}.' a vpisanim ID pozvanky do prislusneho policka.'."\n".'Ak neplanujete hrat na serveri '.$server_info->{server_name}.', povazujte tento E-mail za bezpredmetny'."\n\n".'Na tento E-mail neodpovedajte!'."\n\n".'S pozdravom Team '.$server_info->{server_name},
                        );
                        $msg->send;

                        $vars->{ec7} = 1;
                     } else {
                        push @${errors},'1';
                     }
                                       
                     $vars->{errors7} = $errors;


                  }

               }





               my $level_enough = 0;

               my $ret_wget = system::wget($server_info->{activations_url}.'?account_id='.$vars->{account_id}.'&action=get_account_characters');
               if ($ret_wget->{content} =~ /^0$/) {

               } else {
                  my $tmp;
                  @{$tmp} = split(/\n/,$ret_wget->{content});
                  foreach my $ln (@{$tmp}) {
                     my ($guid,$name,$race,$class,$level,$online,$backuped,$backup_version) = split(/\|/,$ln);
                     if ($level >= 55) {$level_enough = 1;}
                  }
               }
               $vars->{level_enough} = $level_enough;


               $main::get_invitation_state_by_login->execute($vip,$vip,$login,$vars->{server_id});
               my $invite_state = $main::get_invitation_state_by_login->fetchrow_hashref();
               $vars->{invite_state} = $invite_state;

               if ($invite_state->{do_reset} == 1) {
                  $main::reset_invitation_state_by_login->execute($login,$vars->{server_id});
               }



               $main::get_game_account_state_by_login->execute($login,$vars->{server_id});
               my $account_state = $main::get_game_account_state_by_login->fetchrow_hashref();
               $account_state->{email} =~ m/^(.).*?(\@.+)$/;
               $account_state->{masked_email} = $1.'...'.$2;
                                          
               $vars->{account_state} = $account_state;



         }


      } elsif ($q->{page} eq 'account_state_character_questlog') {
         $page = 'account_state';
         $vars->{input_page} = 'account_state_character_questlog';

         $hf = 1;
         $vars->{title} = 68;
         $location = '';
         $vars->{background} = 'http://wow.mine.sk/temp/testbg.jpg';

         if (!defined($vars->{server_id}) || $vars->{server_id} !~ /^\d+$/) {
            $vars->{server_id} = 18;
         }


         if ($vars->{logged} == 1) {

               if (defined($q->{action}) && $q->{action} eq 'delete_quest') {
                  my $ret_wget = system::wget($server_info->{activations_url}.'?account_id='.$vars->{account_id}.'&action=delete_character_quest&guid='.$q->{id}.'&quest_id='.$q->{quest_id});
#warn $server_info->{activations_url}.'?account_id='.$vars->{account_id}.'&action=delete_character_quest&guid='.$q->{id}.'&quest_id='.$q->{quest_id};
               } elsif (defined($q->{action}) && $q->{action} eq 'check_quest') {
                  my $ret_wget = system::wget($server_info->{activations_url}.'?action=get_character_quest_done&guid='.$q->{id}.'&quest_id='.$q->{quest_id});
                  $vars->{rewards} = $ret_wget->{content};
               }

               my $ret_wget = system::wget($server_info->{activations_url}.'?account_id='.$vars->{account_id}.'&action=get_gmlevel_by_account_id');
               $ret_wget->{content} =~ m/^([0-9a-f_]+)\|(\d+)\|(\d+)\|(\d+)\|(.*)\|(\d+)\|(\d+)$/;
               my $version = $1;
               my $gmlevel = $2;
               my $vip = $3;
               my $locked = $4;
               my $last_ip = $5;
               my $expansion = $6;
               my $banned = $7;


               $vars->{gmlevel} = $gmlevel;
               $vars->{version} = $version;
               $vars->{vip} = $vip;
               $vars->{locked} = $locked;
               $vars->{last_ip} = $last_ip;
               $vars->{expansion} = $expansion;
               $vars->{banned} = $banned;

               my $quests;
               my $ret_wget = system::wget($server_info->{activations_url}.'?action=get_character_quests&guid='.$q->{id});
               my $tmp;
               @{$tmp} = split(/\n/,$ret_wget->{content});
               foreach my $quest_line (@{$tmp}) {
                  my ($id,$state,$name) = split(/\|/,$quest_line,3);
                  push @{$quests}, { name => $name, state => $state, id => $id, };
               }
               $vars->{quests} = $quests;
         }



      } elsif ($q->{page} eq 'account_state_changes') {
         $page = 'account_state';
         $vars->{input_page} = 'account_state_changes';

         $hf = 1;
         $vars->{title} = 68;
         $location = '';
         $vars->{background} = 'http://wow.mine.sk/temp/testbg.jpg';

         if (!defined($vars->{server_id}) || $vars->{server_id} !~ /^\d+$/) {
            $vars->{server_id} = 18;
         }


         if ($vars->{logged} == 1) {

            $main::get_account_data_changes->execute($id,$vars->{server_id});
            my $account_data_changes = $main::get_account_data_changes->fetchall_arrayref({});

            foreach (@{$account_data_changes}) {
               if ($_->{name} eq 'email' || $_->{name} eq 'email2') {
                  $_->{value} =~ m/^(.).*?(\@.+)$/;
                  $_->{value} = $1.'...'.$2;

                  $_->{new_value} =~ m/^(.).*?(\@.+)$/;
                  $_->{new_value} = $1.'...'.$2;

               }
            }

            $vars->{account_data_changes} = $account_data_changes;
            $main::get_account_data_changes->finish();




         }


      } elsif ($q->{page} eq 'account_state_accesses') {
         $page = 'account_state';
         $vars->{input_page} = 'account_state_accesses';

         $hf = 1;
         $vars->{title} = 68;
         $location = '';
         $vars->{background} = 'http://wow.mine.sk/temp/testbg.jpg';

         if (!defined($vars->{server_id}) || $vars->{server_id} !~ /^\d+$/) {
            $vars->{server_id} = 18;
         }


         if ($vars->{logged} == 1) {


               my $ret_wget = system::wget($server_info->{activations_url}.'?account_id='.$vars->{account_id}.'&action=get_gmlevel_by_account_id');
               $ret_wget->{content} =~ m/^([0-9a-f_]+)\|(\d+)\|(\d+)\|(\d+)\|(.*)\|(\d+)\|(\d+)$/;
               my $version = $1;
               my $gmlevel = $2;
               my $vip = $3;
               my $locked = $4;
               my $last_ip = $5;
               my $expansion = $6;
               my $banned = $7;


               $vars->{gmlevel} = $gmlevel;
               $vars->{version} = $version;
               $vars->{vip} = $vip;
               $vars->{locked} = $locked;
               $vars->{last_ip} = $last_ip;
               $vars->{expansion} = $expansion;
               $vars->{banned} = $banned;




               $main::get_game_account_accesses->execute($vars->{account_id});
               my $accesses = $main::get_game_account_accesses->fetchall_arrayref({});
               $vars->{accesses} = $accesses;
               $main::get_game_account_accesses->finish();





         }


      } elsif ($q->{page} eq 'account_state_password') {
         $page = 'account_state';
         $vars->{input_page} = 'account_state_password';

         $hf = 1;
         $vars->{title} = 68;
         $location = '';
         $vars->{background} = 'http://wow.mine.sk/temp/testbg.jpg';

         if (!defined($vars->{server_id}) || $vars->{server_id} !~ /^\d+$/) {
            $vars->{server_id} = 18;
         }


         if ($vars->{logged} == 1) {


               my $ret_wget = system::wget($server_info->{activations_url}.'?account_id='.$vars->{account_id}.'&action=get_gmlevel_by_account_id');
               $ret_wget->{content} =~ m/^([0-9a-f_]+)\|(\d+)\|(\d+)\|(\d+)\|(.*)\|(\d+)\|(\d+)$/;
               my $version = $1;
               my $gmlevel = $2;
               my $vip = $3;
               my $locked = $4;
               my $last_ip = $5;
               my $expansion = $6;
               my $banned = $7;


               $vars->{gmlevel} = $gmlevel;
               $vars->{version} = $version;
               $vars->{vip} = $vip;
               $vars->{locked} = $locked;
               $vars->{last_ip} = $last_ip;
               $vars->{expansion} = $expansion;
               $vars->{banned} = $banned;



               if (defined($q->{action}) && $q->{action} eq 'change_password') {

                  my $errors = [];


                  my $ret_wget = system::wget($server_info->{activations_url}.'?login='.$login.'&password='.html_encode($q->{password}).'&action=check_by_password_and_login');
                  if ($ret_wget->{content} !~ /^[^0]\d*\|.+$/) {push @${errors},'6';}
                  if (length($q->{new_password1}) < 4) {push @${errors},'1';}
                  if (similarity (lc $login, lc $q->{new_password1}) > 0.75) {push @${errors},'2';}
                  if ($q->{new_password1} ne $q->{new_password2}) {push @${errors},'3';}


                  if (scalar(@${errors}) == 0) {

                     $main::get_game_account_state_by_id->execute($id,$vars->{server_id});
                     my $account_state = $main::get_game_account_state_by_id->fetchrow_hashref();


                     my $ret_wget = system::wget($server_info->{activations_url}.'?action=set_password_by_user_and_email&login='.$login.'&email='.$account_state->{email}.'&password='.html_encode($q->{new_password1}));


#warn $server_info->{activations_url}.'?action=set_password_by_user_and_email&login='.$login.'&email='.$account_state->{email}.'&password='.$q->{new_password1};

                     if ($ret_wget->{state} == 0) {
                        push @${errors},'4';
                     } else {
                        if ($ret_wget->{content} =~ /^1$/) {



                           my $hash = md5_hex($q->{new_password1});
                           $main::set_game_account_password->execute($hash,$q->{new_password1},$id,$vars->{server_id});

warn "set_game_account_password->execute($hash,$q->{new_password1},$id,$vars->{server_id})";
                           my $msg = MIME::Lite->new(
					From			=> 'WebAccountRecovery',
					To			=> $account_state->{email},
					Subject			=> 'Vygenerovanie noveho hesla (Automaticky generovany E-mail)',
					Type			=> 'text/plain',
					'Auto-Submitted:'	=> 'auto-generated',
					'X-Originating-IP:'	=> $ENV{REMOTE_ADDR},
 					Data			=> 'Tento mail Vam bol odoslany na zaklade odoslania formularu so ziadostou o zmenu hesla pre pristup na '.$server_info->{server_name}.' WoW (z IP adresy '.$ENV{REMOTE_ADDR}.')'."\n\n".'Nove heslo je: '.html_encode($q->{new_password1})."\n\n".'Na tento E-mail neodpovedajte!'."\n\n".'S pozdravom Team '.$server_info->{server_name},
                           );

                           $main::log_game_account_data_change->execute($id,$vars->{server_id},$ENV{REMOTE_ADDR},'password',$q->{password},$q->{new_password1},0);
warn "log_game_account_data_change->execute($id,$vars->{server_id},$ENV{REMOTE_ADDR},'password',$q->{password},$q->{new_password1},0)";

                           
                           $msg->send;

                           $vars->{ec2} = 1;

                        } else {
                           push @${errors},'5';
                        }
                     }
                  }

                  $vars->{errors2} = $errors;
               }
         }




      } elsif ($q->{page} eq 'account_state_data') {
         $page = 'account_state';
         $vars->{input_page} = 'account_state_data';

         $hf = 1;
         $vars->{title} = 68;
         $location = '';
         $vars->{background} = 'http://wow.mine.sk/temp/testbg.jpg';

         if (!defined($vars->{server_id}) || $vars->{server_id} !~ /^\d+$/) {
            $vars->{server_id} = 18;
         }


         if ($vars->{logged} == 1) {


               my $ret_wget = system::wget($server_info->{activations_url}.'?account_id='.$vars->{account_id}.'&action=get_gmlevel_by_account_id');
               $ret_wget->{content} =~ m/^([0-9a-f_]+)\|(\d+)\|(\d+)\|(\d+)\|(.*)\|(\d+)\|(\d+)$/;
               my $version = $1;
               my $gmlevel = $2;
               my $vip = $3;
               my $locked = $4;
               my $last_ip = $5;
               my $expansion = $6;
               my $banned = $7;


               $vars->{gmlevel} = $gmlevel;
               $vars->{version} = $version;
               $vars->{vip} = $vip;
               $vars->{locked} = $locked;
               $vars->{last_ip} = $last_ip;
               $vars->{expansion} = $expansion;
               $vars->{banned} = $banned;




               if (defined($q->{action}) && $q->{action} eq 'change_data') {

                  my $errors;

                  $main::get_game_account_state_by_id->execute($id,$vars->{server_id});
                  my $account_data = $main::get_game_account_state_by_id->fetchrow_hashref();
                        
                  my $ret_wget = system::wget($server_info->{activations_url}.'?login='.$login.'&password='.html_encode($q->{password}).'&action=set_email_by_user_and_password&email='.$account_data->{email});

                  if ($ret_wget->{content} =~ /^1$/) {
                     $main::get_game_account_state_by_login->execute($login,$vars->{server_id});
                     my $game_account_state = $main::get_game_account_state_by_login->fetchrow_hashref();


                     $main::get_game_account_additional_data->execute($vars->{server_id},$id);
                     my $game_account_data = $main::get_game_account_additional_data->fetchrow_hashref();
                     foreach ('chat_nick','forum_nick','name','surname','address','phone') {
                        if ($game_account_data->{$_} ne $q->{$_}) {
warn "log_game_account_data_change->execute($id,$vars->{server_id},$ENV{REMOTE_ADDR},$_,$game_account_data->{$_},$q->{$_},0);";

                           $main::log_game_account_data_change->execute($id,$vars->{server_id},$ENV{REMOTE_ADDR},$_,$game_account_data->{$_},$q->{$_},0);
                        }
                     }

#                     $main::set_game_account_data->execute($q->{email},$q->{chat_nick},$q->{forum_nick},$q->{name},$q->{surname},$q->{address},$q->{phone},$id,$vars->{server_id});
                     $main::set_game_account_data->execute($game_account_state->{email},$q->{chat_nick},$q->{forum_nick},$q->{name},$q->{surname},$q->{address},$q->{phone},$id,$vars->{server_id});


                     my $msg = MIME::Lite->new(
				From			=> 'WebAccountActivation',
				To			=> $game_account_state->{email},
				Subject			=> 'Zmena udajov k hernemu uctu '.$server_info->{server_name}.' (Automaticky generovany E-mail)',
				Type			=> 'text/plain',
        	                'Auto-Submitted:'	=> 'auto-generated',
                	        'X-Originating-IP:'	=> $ENV{REMOTE_ADDR},
				Data			=> 'Tento mail Vam bol odoslany na zaklade ziadosti (z IP adresy '.$ENV{REMOTE_ADDR}.') o zmenu udajov o vlastnikovi herneho uctu.'."\n\n".'Email: '.$q->{email}."\n".'Chat nick: '.$q->{chat_nick}."\n".'Forum nick '.$q->{forum_nick}."\n".'Meno: '.$q->{name}."\n".'Priezvisko: '.$q->{surname}."\n".'Adresa: '.$q->{address}."\n".'Telefon: '.$q->{phone}."\n\n".'Ak ste tieto udaje nemenili Vy, kontaktujte urychlene GM alebo spravcu servera'."\n\n".'Na tento E-mail neodpovedajte!'."\n\n".'S pozdravom Team '.$server_info->{server_name},
                     );
                     $msg->send;

                     


                     $vars->{ec1} = 1;

                  } else {
                     
                     push @${errors},'1';

                  }

                  $vars->{errors1} = $errors;

               } elsif (defined($q->{action}) && $q->{action} eq 'delete_request') {

                  $main::get_game_account_state_by_login->execute($login,$vars->{server_id});
                  my $account_data = $main::get_game_account_state_by_login->fetchrow_hashref();
                  if ($account_data->{email_ok} == 0) {

                     $main::delete_game_account_request->execute($account_data->{id},$vars->{server_id});
                     $main::delete_game_account_invitation->execute($account_data->{invited_via},$vars->{server_id});
                     sessions::remove_session( $dbh, { session_id => $vars->{session_id}, }, );
                     $location = $globals::index_path.'?s='.$q->{'s'};
                  }


               } elsif (defined($q->{action}) && $q->{action} eq 'resend_email') {

                  my $tmp_session_data = sessions::get_session_data( $dbh, { session_id => $vars->{session_id}, }, );
#warn "sessions::get_session_data( $dbh, { session_id => $vars->{session_id}, }, );";
                  my $local_account_id = $tmp_session_data->{session_data}->{local_account_id};
#warn "session_data = $tmp_session_data->{session_data}";
#warn "local_account_id = $local_account_id";

                  $main::get_game_account_state_by_id->execute($local_account_id,$q->{'s'});
                  my $account_data = $main::get_game_account_state_by_id->fetchrow_hashref();

                  my $md5 = Digest::MD5->new;
                  $md5->add($account_data->{login});
                  $md5->add($account_data->{email});
                  $md5->add($account_data->{id});
                  $md5->add('mega coder SoCo');
                  my $hash = $md5->hexdigest;

                  my $msg = MIME::Lite->new(
			From                    => 'WebAccountActivation',
			To                      => $account_data->{email},
			Subject                 => 'Overenie E-mailovej adresy [2] (Automaticky generovany E-mail)',
			Type                    => 'text/plain',
			'Auto-Submitted:'       => 'auto-generated',
			'X-Originating-IP:'     => $ENV{REMOTE_ADDR},
			Data                    => 'Tento mail Vam bol odoslany na zaklade ziadosti (z IP adresy '.$ENV{REMOTE_ADDR}.') o opatovane zaslanie kontrolnej URL adresy '.$globals::misc_path.'?page=confirm_email&hex='.$hash.'&id='.$account_data->{id}.'&s='.$q->{'s'}.' na ktoru musite kliknut a tym potvrdit, ze tato E-mailova adresa patri Vam.'."\n\n".'Na tento E-mail neodpovedajte!'."\n\n".'S pozdravom Team '.$server_info->{server_name},
                  );

                  $msg->send;
                  $vars->{account_state} = $account_data;

                  $vars->{ec1} = 2;

               } elsif (defined($q->{action}) && $q->{action} eq 'i_say_no') {

                  $main::get_game_account_state_by_id->execute($id,$vars->{server_id});
                  my $account_data = $main::get_game_account_state_by_id->fetchrow_hashref();

                  $main::get_game_account_state_by_login->execute($login,$vars->{server_id});
                  my $game_account_state = $main::get_game_account_state_by_login->fetchrow_hashref();

                  $main::set_game_account_data->execute($game_account_state->{email},$game_account_state->{chat_nick},$game_account_state->{forum_nick},'x','x','x','x',$id,$vars->{server_id});


                  $main::get_game_account_additional_data->execute($vars->{server_id},$id);
                  my $game_account_data = $main::get_game_account_additional_data->fetchrow_hashref();
                  foreach ('name','surname','address','phone') {
                     if ($game_account_data->{$_} ne 'x') {
                        $main::log_game_account_data_change->execute($id,$vars->{server_id},$ENV{REMOTE_ADDR},$_,$game_account_data->{$_},'x',0);
                     }
                  }


               }


               $main::get_game_account_state_by_login->execute($login,$vars->{server_id});
               my $account_state = $main::get_game_account_state_by_login->fetchrow_hashref();
               $account_state->{email} =~ m/^(.).*?(\@.+)$/;
               $account_state->{masked_email} = $1.'...'.$2;

               $vars->{account_state} = $account_state;

warn "get_first_account_owner->execute($id)";

               $main::get_first_account_owner->execute($id);
               my $first_account_owner;
               foreach (@{$main::get_first_account_owner->fetchall_arrayref({})}) {
                  if ($_->{value} ne '' && $_->{value} ne 'x') {
                     $first_account_owner->{$_->{name}} = $_->{value};
warn "first_account_owner->{$_->{name}} = $_->{value}";

                  } else {
                     $first_account_owner->{$_->{name}} = $_->{new_value};
warn "first_account_owner->{$_->{name}} = $_->{new_value}";

                  }
               }

               $vars->{first_account_owner} = $first_account_owner;




         }






      } elsif ($q->{page} eq 'account_state_delete') {
         $page = 'account_state';
         $vars->{input_page} = 'account_state_delete';

         $hf = 1;
         $vars->{title} = 68;
         $location = '';
         $vars->{background} = 'http://wow.mine.sk/temp/testbg.jpg';

         if (!defined($vars->{server_id}) || $vars->{server_id} !~ /^\d+$/) {
            $vars->{server_id} = 18;
         }


         if ($vars->{logged} == 1) {

               my $ret_wget = system::wget($server_info->{activations_url}.'?account_id='.$vars->{account_id}.'&action=get_gmlevel_by_account_id');
               $ret_wget->{content} =~ m/^([0-9a-f_]+)\|(\d+)\|(\d+)\|(\d+)\|(.*)\|(\d+)\|(\d+)$/;
               my $version = $1;
               my $gmlevel = $2;
               my $vip = $3;
               my $locked = $4;
               my $last_ip = $5;
               my $expansion = $6;
               my $banned = $7;


               $vars->{gmlevel} = $gmlevel;
               $vars->{version} = $version;
               $vars->{vip} = $vip;
               $vars->{locked} = $locked;
               $vars->{last_ip} = $last_ip;
               $vars->{expansion} = $expansion;
               $vars->{banned} = $banned;

               my $errors = [];

               if (defined($q->{action}) && $q->{action} eq 'delete_account') {

#DELETE

                  $main::delete_account->execute($vars->{account_id});

                  $hf = 0;

                  sessions::remove_session( $dbh, { session_id => $vars->{session_id}, }, );

                  $location = $globals::index_path.'?s='.$q->{'s'};




                  warn "delete accouunt $vars->{account_id}";
                  $vars->{ec7} = 1;
               } else {
                  push @${errors},'1';
               }

               $vars->{errors7} = $errors;

         }






      } elsif ($q->{page} eq 'account_state_email') {
         $page = 'account_state';
         $vars->{input_page} = 'account_state_email';

         $hf = 1;
         $vars->{title} = 68;
         $location = '';
         $vars->{background} = 'http://wow.mine.sk/temp/testbg.jpg';

         if (!defined($vars->{server_id}) || $vars->{server_id} !~ /^\d+$/) {
            $vars->{server_id} = 18;
         }


         if ($vars->{logged} == 1) {




               my $ret_wget = system::wget($server_info->{activations_url}.'?account_id='.$vars->{account_id}.'&action=get_gmlevel_by_account_id');
               $ret_wget->{content} =~ m/^([0-9a-f_]+)\|(\d+)\|(\d+)\|(\d+)\|(.*)\|(\d+)\|(\d+)$/;
               my $version = $1;
               my $gmlevel = $2;
               my $vip = $3;
               my $locked = $4;
               my $last_ip = $5;
               my $expansion = $6;
               my $banned = $7;


               $vars->{gmlevel} = $gmlevel;
               $vars->{version} = $version;
               $vars->{vip} = $vip;
               $vars->{locked} = $locked;
               $vars->{last_ip} = $last_ip;
               $vars->{expansion} = $expansion;
               $vars->{banned} = $banned;



               $main::get_game_account_state_by_login->execute($login,$vars->{server_id});
               my $account_state = $main::get_game_account_state_by_login->fetchrow_hashref();
               $account_state->{email} =~ m/^(.).*?(\@.+)$/;
               $account_state->{masked_email} = $1.'...'.$2;

               $vars->{account_state} = $account_state;


               if (defined($q->{action}) && $q->{action} eq 'change_email') {

                  my $errors = [];



                  my $ret_wget = system::wget($server_info->{activations_url}.'?login='.$login.'&password='.html_encode($q->{password}).'&action=check_by_password_and_login');


                  if ($ret_wget->{content} =~ /^[^0]\d*\|.+$/) {


                     if ($q->{new_email} =~ /^.+\@(.+)$/) {
                        my $res = Net::DNS::Resolver->new;
                        my @mx = mx($res, $1);
                        if (!@mx) {push @${errors},'4';}
                     } else {
                        push @${errors},'3';
                     }
                                   
                     $main::is_game_email_used->execute($q->{new_email},$vars->{server_id});
                     if ($main::is_game_email_used->rows() != 0) {push @{$errors},'5';}
                     $main::is_game_email_used->finish();
                                                                                                                     
                     if (scalar(@{$errors}) == 0) {

                        $main::get_game_account_state_by_login->execute($login,$vars->{server_id});
                        my $game_account_state = $main::get_game_account_state_by_login->fetchrow_hashref();

                        my $md5 = Digest::MD5->new;
                        $md5->add(lc($login));
                        $md5->add(lc($game_account_state->{email}));
                        $md5->add($game_account_state->{id});
                        $md5->add(lc($q->{new_email}));
                        $md5->add('mega coder SoCo');
                        my $hash = $md5->hexdigest;

                        if (!defined($game_account_state->{email}) || $game_account_state->{email} eq '') {

                        $vars->{hash} = $hash;
                        $vars->{ec4} = 3;

#c_path.'?page=confirm_new_email&hex='.$hash.'&new_email='.$q->{new_email}.'&id='.$game_account_state->{id}.'&s='.$vars->{server_id}.

                        } else {

                           my $msg = MIME::Lite->new(
				From			=> 'WebAccountActivation',
				To			=> $game_account_state->{email},
				Subject			=> 'Zmena E-mailu k hernemu uctu '.$server_info->{server_name}.' (Automaticky generovany E-mail)',
				Type			=> 'text/plain',
        	                'Auto-Submitted:'	=> 'auto-generated',
                	        'X-Originating-IP:'	=> $ENV{REMOTE_ADDR},
				Data			=> 'Tento mail Vam bol odoslany na zaklade ziadosti (z IP adresy '.$ENV{REMOTE_ADDR}.') o zmenu E-mailovej adresy k herneho uctu.'."\n\n".'Login: '.$login."\n".'Povodny E-mail: '.$game_account_state->{email}."\n\n".'Novy E-mail: '.$q->{new_email}."\n\n".'Aby bola E-mailova adresa zmenena, musite kliknut na nasledovnu URL adresu: '.$globals::index_path.'?page=confirm_new_email&hex='.$hash.'&new_email='.$q->{new_email}.'&id='.$game_account_state->{id}.'&s='.$vars->{server_id}."\n".'Ak ste o zmenu E-mailovej adresy neziadali Vy, kontaktujte urychlene GM alebo spravcu servera'."\n\n".'Na tento E-mail neodpovedajte!'."\n\n".'S pozdravom Team '.$server_info->{server_name},
                           );
                           $msg->send;

                           $vars->{ec4} = 1;
                        }            

                     }

                  } else {
                     
                     push @${errors},'1';

                  }

                  $vars->{errors4} = $errors;





               } elsif (defined($q->{action}) && $q->{action} eq 'change_email_finish') {

                  my $errors = [];

                  $main::get_game_account_state_by_login->execute($login,$vars->{server_id});
                  my $game_account_state = $main::get_game_account_state_by_login->fetchrow_hashref();

#warn "get_game_account_state_by_login->execute($login,$vars->{server_id})";

#warn " md5->add(lc($login))";
#warn " add(lc($game_account_state->{email}))";
#warn " add($game_account_state->{id})";
#warn " add(lc($q->{new_email}))";

                  my $md5 = Digest::MD5->new;
                  $md5->add(lc($login));
                  $md5->add(lc($game_account_state->{email}));
                  $md5->add($game_account_state->{id});
                  $md5->add(lc($q->{new_email}));
                  $md5->add('mega coder SoCo');
                  my $hash = $md5->hexdigest;

                  if ($q->{hex} eq $hash) {

                     my $msg = MIME::Lite->new(
			From			=> 'WebAccountActivation',
			To			=> $game_account_state->{email},
			Subject			=> 'Zmena E-mailu k hernemu uctu '.$server_info->{server_name}.' (Automaticky generovany E-mail)',
			Type			=> 'text/plain',
       	                'Auto-Submitted:'	=> 'auto-generated',
               	        'X-Originating-IP:'	=> $ENV{REMOTE_ADDR},
			Data			=> 'Emailova adresa k uctu: "'.$login.'" bola uspesne zmenena'."\n\n".'Na tento E-mail neodpovedajte!'."\n\n".'S pozdravom Team '.$server_info->{server_name},
                     );
                     $msg->send;

                     
                     my $ret_wget = system::wget($server_info->{activations_url}.'?account_id='.$vars->{account_id}.'&action=set_email_by_account_id&email='.$q->{new_email});
                     $main::set_game_account_email->execute($q->{new_email},$game_account_state->{id},$vars->{server_id});
                     $main::log_game_account_data_change->execute($game_account_state->{id},$vars->{server_id},$ENV{REMOTE_ADDR},'email',$game_account_state->{email},$q->{new_email},0);
                     $main::set_deliverable_by_email->execute(0,$q->{new_email});

                     $vars->{ec4} = 2;
                  } else {
                     push @${errors},'2';
                  }


                  $vars->{errors4} = $errors;
              }





         }




      } elsif ($q->{page} eq 'account_state_chartrader_sent') {
         $page = 'account_state';
         $vars->{input_page} = 'account_state_chartrader_sent';

         $hf = 1;
         $vars->{title} = 68;
         $location = '';
         $vars->{background} = 'http://wow.mine.sk/temp/testbg.jpg';

         if (!defined($vars->{server_id}) || $vars->{server_id} !~ /^\d+$/) {
            $vars->{server_id} = 18;
         }


         if ($vars->{logged} == 1) {




               my $ret_wget = system::wget($server_info->{activations_url}.'?account_id='.$vars->{account_id}.'&action=get_gmlevel_by_account_id');
               $ret_wget->{content} =~ m/^([0-9a-f_]+)\|(\d+)\|(\d+)\|(\d+)\|(.*)\|(\d+)\|(\d+)$/;
               my $version = $1;
               my $gmlevel = $2;
               my $vip = $3;
               my $locked = $4;
               my $last_ip = $5;
               my $expansion = $6;
               my $banned = $7;


               $vars->{gmlevel} = $gmlevel;
               $vars->{version} = $version;
               $vars->{vip} = $vip;
               $vars->{locked} = $locked;
               $vars->{last_ip} = $last_ip;
               $vars->{expansion} = $expansion;
               $vars->{banned} = $banned;




               $main::get_all_my_game_accounts_trader_offers->execute($vars->{server_id},$vars->{account_id});
               my $offers = $main::get_all_my_game_accounts_trader_offers->fetchall_arrayref({});
               $main::get_all_my_game_accounts_trader_offers->finish();
               $vars->{offers} = $offers;
         }





=cut
      } elsif ($q->{page} eq 'account_state_chartrader_progreess') {
         $page = 'account_state';
         $vars->{input_page} = 'account_state_chartrader_progress';

         $hf = 1;
         $vars->{title} = 68;
         $location = '';
         $vars->{background} = 'http://wow.mine.sk/temp/testbg.jpg';

         if (!defined($vars->{server_id}) || $vars->{server_id} !~ /^\d+$/) {
            $vars->{server_id} = 18;
         }


         if ($vars->{logged} == 1) {




               my $ret_wget = system::wget($server_info->{activations_url}.'?account_id='.$vars->{account_id}.'&action=get_gmlevel_by_account_id');
               $ret_wget->{content} =~ m/^([0-9a-f_]+)\|(\d)\|(\d)\|(\d)\|(.*)\|(\d)\|(\d)$/;
               my $version = $1;
               my $gmlevel = $2;
               my $vip = $3;
               my $locked = $4;
               my $last_ip = $5;
               my $expansion = $6;
               my $banned = $7;


               $vars->{gmlevel} = $gmlevel;
               $vars->{version} = $version;
               $vars->{vip} = $vip;
               $vars->{locked} = $locked;
               $vars->{last_ip} = $last_ip;
               $vars->{expansion} = $expansion;
               $vars->{banned} = $banned;




         }
=cut

      } elsif ($q->{page} eq 'account_state_chartrader_accept') {
         $page = 'account_state';
         $vars->{input_page} = 'account_state_chartrader_accept';

         $hf = 1;
         $vars->{title} = 68;
         $location = '';
         $vars->{background} = 'http://wow.mine.sk/temp/testbg.jpg';

         if (!defined($vars->{server_id}) || $vars->{server_id} !~ /^\d+$/) {
            $vars->{server_id} = 18;
         }


         if ($vars->{logged} == 1) {




               my $ret_wget = system::wget($server_info->{activations_url}.'?account_id='.$vars->{account_id}.'&action=get_gmlevel_by_account_id');
               $ret_wget->{content} =~ m/^([0-9a-f_]+)\|(\d+)\|(\d+)\|(\d+)\|(.*)\|(\d+)\|(\d+)$/;
               my $version = $1;
               my $gmlevel = $2;
               my $vip = $3;
               my $locked = $4;
               my $last_ip = $5;
               my $expansion = $6;
               my $banned = $7;


               $vars->{gmlevel} = $gmlevel;
               $vars->{version} = $version;
               $vars->{vip} = $vip;
               $vars->{locked} = $locked;
               $vars->{last_ip} = $last_ip;
               $vars->{expansion} = $expansion;
               $vars->{banned} = $banned;

               $main::get_trader_offer_by_id->execute($vars->{server_id},$q->{id});
               my $trader_offer = $main::get_trader_offer_by_id->fetchrow_hashref();

               my $errors = [];

               if ($banned == 1) {push @{$errors}, '1';}
               if ($trader_offer->{char_account_id} != $vars->{account_id}) {push @{$errors}, '2';}

               my $ret_wget = system::wget($server_info->{activations_url}.'?account_id='.$trader_offer->{starter_account_id}.'&action=get_gmlevel_by_account_id');
               $ret_wget->{content} =~ m/^([0-9a-f_]+)\|(\d+)\|(\d+)\|(\d+)\|(.*)\|(\d+)\|(\d+)$/;
               my $version2 = $1;
               my $gmlevel2 = $2;
               my $vip2 = $3;
               my $locked2 = $4;
               my $last_ip2 = $5;
               my $expansion2 = $6;
               my $banned2 = $7;

               if ($banned2 == 1) {push @{$errors}, '3';}
               if ($vip != 1) {push @{$errors}, '4';}

               $vars->{errors} = $errors;


         }


      } elsif ($q->{page} eq 'account_state_chartrader') {
         $page = 'account_state';
         $vars->{input_page} = 'account_state_chartrader';

         $hf = 1;
         $vars->{title} = 68;
         $location = '';
         $vars->{background} = 'http://wow.mine.sk/temp/testbg.jpg';

         if (!defined($vars->{server_id}) || $vars->{server_id} !~ /^\d+$/) {
            $vars->{server_id} = 18;
         }


         if ($vars->{logged} == 1) {




               my $ret_wget = system::wget($server_info->{activations_url}.'?account_id='.$vars->{account_id}.'&action=get_gmlevel_by_account_id');
               $ret_wget->{content} =~ m/^([0-9a-f_]+)\|(\d+)\|(\d+)\|(\d+)\|(.*)\|(\d+)\|(\d+)$/;
               my $version = $1;
               my $gmlevel = $2;
               my $vip = $3;
               my $locked = $4;
               my $last_ip = $5;
               my $expansion = $6;
               my $banned = $7;


               $vars->{gmlevel} = $gmlevel;
               $vars->{version} = $version;
               $vars->{vip} = $vip;
               $vars->{locked} = $locked;
               $vars->{last_ip} = $last_ip;
               $vars->{expansion} = $expansion;
               $vars->{banned} = $banned;


               if (defined($q->{action}) && $q->{action} eq 'chartrader_setup') {



                  $main::get_game_account_trade_offers->execute($vars->{server_id},$vars->{account_id});
                  my $chartrader_offers = $main::get_game_account_trade_offers->fetchall_arrayref({});
                  foreach (@{$chartrader_offers}) {

                     if ($_->{state} == 0) {	# nenastavene
                        if ($q->{'action_'.$_->{char_guid}} == 1) {
                           # zaradit do db
                           $main::set_game_account_trade_state->execute(1,$vars->{server_id},$vars->{account_id},$_->{char_guid});

                        } elsif ($q->{'action_'.$_->{char_guid}} == 2) {
                           $main::del_game_account_trade->execute($vars->{server_id},$vars->{account_id},$_->{char_guid});
                           # zrusit z traderu
                        }
                        # save zmien

                        my $new_class = $q->{'new_class_'.$_->{char_guid}};

                        if (ref($new_class) ne 'ARRAY') {$new_class = [$new_class];}
                        
                        
                        my $new_class_mask = 0;
                        foreach (@{$new_class}) {
                           if ($_ == 0) {$new_class_mask = 0; last;}
                           $new_class_mask += $map_class_id_to_mask->{$_};
                        }

                        $main::set_game_account_trade->execute($q->{'new_faction_'.$_->{char_guid}},$new_class_mask,$vars->{server_id},$vars->{account_id},$_->{char_guid});

                     } elsif ($_->{state} == 1) {

                        if ($q->{'action_'.$_->{char_guid}} == 3) {
                           # zmazat z db
                           $main::del_game_account_trade->execute($vars->{server_id},$vars->{account_id},$_->{char_guid});
                        } elsif ($q->{'action_'.$_->{char_guid}} == 4) {
                           # zmenit parametre
                           $main::set_game_account_trade_state->execute(0,$vars->{server_id},$vars->{account_id},$_->{char_guid});
                        }

                     } elsif ($_->{state} == 2) {
                        if ($q->{'action_'.$_->{char_guid}} == 5) {
                           # zamietnut
                        } elsif ($q->{'action_'.$_->{char_guid}} == 6) {
                           # prijat
                        }

                     }

                  }
                  $main::get_game_account_trade_offers->finish();
               }














               my ($deleted_characters_hash,$characters_hash);

               my $ret_wget = system::wget($server_info->{activations_url}.'?account_id='.$vars->{account_id}.'&action=get_account_characters');
               if ($ret_wget->{content} =~ /^0$/) {

               } else {
                  my $tmp;

                  @{$tmp} = split(/\n/,$ret_wget->{content});
                  my $characters;

                  foreach my $ln (@{$tmp}) {
                     my ($guid,$name,$race,$class,$level,$online,$live,$expire) = split(/\|/,$ln);
                     if ($live == 0) {
#warn "$name = DELETED";
                        $deleted_characters_hash->{$guid} = {name => $name, race => $race, class => $class, level => $level,};
                     } else {
#warn "$name = ALIVE";
                        $characters_hash->{$guid} = {name => $name, race => $race, class => $class, level => $level,};
                     }
                  }
                  $vars->{characters_hash} = $characters_hash;
                  $vars->{deleted_characters_hash} = $deleted_characters_hash;

               }




               my $traded_char;
               $main::get_game_account_trade_offers->execute($vars->{server_id},$vars->{account_id});
               my $chartrader_offers = $main::get_game_account_trade_offers->fetchall_arrayref({});
               foreach (@{$chartrader_offers}) {
                  $_->{char} = $characters_hash->{$_->{char_guid}};
                  $traded_char->{$_->{char_guid}} = 1;


#                  foreach (keys(%{%map_class_mask_to_id})) {
#                  }

                  $main::get_game_account_trade_suggestions->execute(
			$globals::races->{$_->{char}->{race}}->{fraction},#moja faction_id
			$map_class_id_to_mask->{$_->{char}->{class}},#bitmaska mojho charu
			$vars->{server_id},
			$_->{new_class},# multiclassmask ktore hladam
			$_->{new_class},# multiclassmask ktore hladam
			$_->{new_faction},# faction ktore hladam
			$_->{new_faction},# faction ktore hladam
			$_->{char}->{level},$_->{char}->{level},
			$_->{char}->{class},$_->{char}->{class}
                  );


                  my $suggestions = $main::get_game_account_trade_suggestions->fetchall_arrayref({});
                  foreach my $sug (@{$suggestions}) {
                     my $want = [];
                     if ($sug->{new_class} != 0) {
                        foreach my $cm (keys(%{$map_class_mask_to_id})) {
#warn "if ($cm & $sug->{new_class}) ";
                           if ($sug->{new_class} & $cm) {push @{$want}, $map_class_mask_to_id->{$cm};}
                        }
                     }
                     $sug->{want} = $want;
                  }
                  $_->{suggestions} = $suggestions;




                  if ($_->{new_class} != 0) {
                     my $tmp_classes;
                     foreach my $class_mask (keys(%{$map_class_mask_to_id})) {
                        if ($_->{new_class} & $class_mask) {
                           $tmp_classes->{$map_class_mask_to_id->{$class_mask}} = 1;
                        }
                     }
                     $_->{new_class} = $tmp_classes;
                  } else {
                     my $tmp_classes;
                     $tmp_classes->{'0'} = 1;
                     $_->{new_class} = $tmp_classes;
                  }

                  $main::get_my_game_accounts_trader_offers->execute($vars->{server_id},$_->{char_guid});
                  my $my_offers = $main::get_my_game_accounts_trader_offers->fetchall_arrayref({});
                  my $my_offer_hash;
                  foreach my $myoff (@{$my_offers}) {
                     $my_offer_hash->{$myoff->{char_guid}} = $myoff;
                  }
                  $main::get_my_game_accounts_trader_offers->finish();
                  $_->{my_offer_hash} = $my_offer_hash;

                  $main::get_game_accounts_trader_offers_to_me->execute($vars->{server_id},$_->{char_guid});
                  my $to_me_offers = $main::get_game_accounts_trader_offers_to_me->fetchall_arrayref({});
                  $main::get_game_accounts_trader_offers_to_me->finish();
                  $_->{to_me_offers} = $to_me_offers;



               }
               $main::get_game_account_trade_offers->finish();
               $vars->{chartrader_offers} = $chartrader_offers;
               $vars->{traded_char} = $traded_char;










         }



      } elsif ($q->{page} eq 'account_state_expansion') {
         $page = 'account_state';
         $vars->{input_page} = 'account_state_expansion';

         $hf = 1;
         $vars->{title} = 68;
         $location = '';
         $vars->{background} = 'http://wow.mine.sk/temp/testbg.jpg';

         if (!defined($vars->{server_id}) || $vars->{server_id} !~ /^\d+$/) {
            $vars->{server_id} = 18;
         }


         if ($vars->{logged} == 1) {




               my $ret_wget = system::wget($server_info->{activations_url}.'?account_id='.$vars->{account_id}.'&action=get_gmlevel_by_account_id');
               $ret_wget->{content} =~ m/^([0-9a-f_]+)\|(\d+)\|(\d+)\|(\d+)\|(.*)\|(\d+)\|(\d+)$/;
               my $version = $1;
               my $gmlevel = $2;
               my $vip = $3;
               my $locked = $4;
               my $last_ip = $5;
               my $expansion = $6;
               my $banned = $7;


               $vars->{gmlevel} = $gmlevel;
               $vars->{version} = $version;
               $vars->{vip} = $vip;
               $vars->{locked} = $locked;
               $vars->{last_ip} = $last_ip;
               $vars->{expansion} = $expansion;
               $vars->{banned} = $banned;


               if (defined($q->{action}) && $q->{action} eq 'change_expansion') {

                  my $errors;


#                     if ($vip == 1 && $q->{expansion} == 2) {
                        my $ret_wget = system::wget($server_info->{activations_url}.'?login='.$login.'&expansion='.$q->{expansion}.'&action=set_expansion_by_username');
                        $vars->{ec6} = 1;
#                     } else {
#                        push @${errors},'2';
#                     }
                     
                     $vars->{expansion} = $q->{expansion};


                  $vars->{errors6} = $errors;          

               }



        }




      } elsif ($q->{page} eq 'account_state') {
         $page = 'account_state';
         $vars->{input_page} = 'account_state_info';

         $hf = 1;
         $vars->{title} = 68;
         $location = '';
         $vars->{background} = 'http://wow.mine.sk/temp/testbg.jpg';

         if (!defined($vars->{server_id}) || $vars->{server_id} !~ /^\d+$/) {
            $vars->{server_id} = 18;
         }



         if ($vars->{logged} == 1) {

               $main::get_system_info_by_account->execute($id);
               my $system_info = $main::get_system_info_by_account->fetchall_arrayref({});
               foreach (@{$system_info}) {
                  $_->{hdd_total} = system::size($_->{hdd_total});
                  $_->{hdd_free} = system::size($_->{hdd_free});

               }
               $vars->{system_info} = $system_info;

               $main::get_system_info_by_account->finish();


               $main::get_game_account_additional_data->execute($vars->{server_id},$id);
               my $game_account_data = $main::get_game_account_additional_data->fetchrow_hashref();
               $vars->{game_account_data} = $game_account_data;


               my $ret_wget = system::wget($server_info->{activations_url}.'?account_id='.$vars->{account_id}.'&action=get_gmlevel_by_account_id');

               $ret_wget->{content} =~ m/^([0-9a-f_]+)\|(\d+)\|(\d+)\|(\d+)\|(.*)\|(\d+)\|(\d+)$/;
               my $version = $1;
               my $gmlevel = $2;
               my $vip = $3;
               my $locked = $4;
               my $last_ip = $5;
               my $expansion = $6;
               my $banned = $7;


               $vars->{gmlevel} = $gmlevel;
               $vars->{version} = $version;
               $vars->{vip} = $vip;
               $vars->{locked} = $locked;
               $vars->{last_ip} = $last_ip;
               $vars->{expansion} = $expansion;
               $vars->{banned} = $banned;


               $main::get_game_account_additional_data->execute($vars->{server_id},$id);
               my $game_account_data = $main::get_game_account_additional_data->fetchrow_hashref();


               $game_account_data->{email} =~ m/^.*?\@(.+)$/;
               my $domain = $1;
               my $email_security = 0;

               if ($domain =~ /azet\.sk$|pokec\.sk$/) {
                  $email_security = 10;
               } elsif ($domain =~ /zoznam\.sk$|seznam\.cz$|atlas\.sk$|atlas\.cz$|centrum\.sk$|centrum\.cz$|inmail\.sk$/) {
                  $email_security = 5;
               } elsif ($domain =~ /\.gmail.com$/) {
                  $email_security = 1;
               } else {
                  $email_security = 3;
               }


               $vars->{email_security} = $email_security;

         }


      } elsif ($q->{page} eq 'account_state_rules') {
         $page = 'account_state';
         $vars->{input_page} = 'account_state_rules';

         $hf = 1;
         $vars->{title} = 68;
         $location = '';
         $vars->{background} = 'http://wow.mine.sk/temp/testbg.jpg';

         if (!defined($vars->{server_id}) || $vars->{server_id} !~ /^\d+$/) {
            $vars->{server_id} = 18;
         }

         $vars->{rules} = $rules->{$vars->{server_id}};


         if ($vars->{logged} == 1) {

               $main::get_game_account_additional_data->execute($vars->{server_id},$id);
               my $game_account_data = $main::get_game_account_additional_data->fetchrow_hashref();



               if (defined($q->{action}) && $q->{action} eq 'agree') {
                  if ($q->{agree} eq '1') {
                     $main::set_game_account_rules_ok->execute($id);
warn "main::set_game_account_rules_ok->execute($id)";

                     $main::get_game_account_additional_data->execute($vars->{server_id},$id);
                     $game_account_data = $main::get_game_account_additional_data->fetchrow_hashref();

                  }
               }

               $vars->{game_account_data} = $game_account_data;


               my $ret_wget = system::wget($server_info->{activations_url}.'?account_id='.$vars->{account_id}.'&action=get_gmlevel_by_account_id');

               $ret_wget->{content} =~ m/^([0-9a-f_]+)\|(\d+)\|(\d+)\|(\d+)\|(.*)\|(\d+)\|(\d+)$/;
               my $version = $1;
               my $gmlevel = $2;
               my $vip = $3;
               my $locked = $4;
               my $last_ip = $5;
               my $expansion = $6;
               my $banned = $7;


               $vars->{gmlevel} = $gmlevel;
               $vars->{version} = $version;
               $vars->{vip} = $vip;
               $vars->{locked} = $locked;
               $vars->{last_ip} = $last_ip;
               $vars->{expansion} = $expansion;
               $vars->{banned} = $banned;


         }




      } elsif ($q->{page} eq 'account_state_characters') {
         $page = 'account_state';
         $vars->{input_page} = 'account_state_characters';

#warn "!!!!!!!!!! dopice";
         $hf = 1;
         $vars->{title} = 68;
         $location = '';
         $vars->{background} = 'http://wow.mine.sk/temp/testbg.jpg';

         if (!defined($vars->{server_id}) || $vars->{server_id} !~ /^\d+$/) {
            $vars->{server_id} = 18;
         }


         if ($vars->{logged} == 1) {


            $vars->{post_title} = $server_info->{server_name};


               if (defined($q->{action}) && $q->{action} eq 'reset_position') {

                  my $errors;

                  if ($q->{reset_id} =~ /^\d+$/) {

                     if ($q->{'butt'} eq 'Resetnut poziciu') {
                    
                        my $ret_wget = system::wget($server_info->{activations_url}.'?login='.$login.'&action=reset_starting_position&guid='.$q->{reset_id});
                        if ($ret_wget->{content} =~ /^1$/) {
                           $vars->{ec3} = 1;
                        } else {
                           push @{$errors},'1';
                        }
                     } elsif ($q->{'butt'} eq 'Zalohovat') {

                        my $ret_wget = system::wget($server_info->{activations_url}.'?action=backup_character&login='.$login.'&account_id='.$vars->{account_id}.'&guid='.$q->{reset_id});
                        if ($ret_wget->{content} =~ /^1$/) {
                           $vars->{ec3} = 2;
                        } else {
                           push @{$errors},'3';
                        }

                     } elsif ($q->{'butt'} eq 'Chartrader') {



                        $main::is_game_account_in_trade_offers->execute($vars->{server_id},$vars->{account_id},$q->{reset_id});
                        if ($main::is_game_account_in_trade_offers->rows() == 0 ) {
                           $main::add_game_account_trade->execute($vars->{server_id},$vars->{account_id},$q->{reset_id});
                        }
                        $main::is_game_account_in_trade_offers->finish();



                     }

                  } else {
                     push @{$errors},'2';
                  }
                  $vars->{errors3} = $errors;
               }


               my $ret_wget = system::wget($server_info->{activations_url}.'?account_id='.$vars->{account_id}.'&action=get_gmlevel_by_account_id');

               $ret_wget->{content} =~ m/^([0-9a-f_]+)\|(\d+)\|(\d+)\|(\d+)\|(.*)\|(\d+)\|(\d+)$/;
               my $version = $1;
               my $gmlevel = $2;
               my $vip = $3;
               my $locked = $4;
               my $last_ip = $5;
               my $expansion = $6;
               my $banned = $7;


               $vars->{gmlevel} = $gmlevel;
               $vars->{version} = $version;
               $vars->{vip} = $vip;
               $vars->{locked} = $locked;
               $vars->{last_ip} = $last_ip;
               $vars->{expansion} = $expansion;
               $vars->{banned} = $banned;


               my $level_enough = 0;
               my ($characters_hash,$deleted_characters_hash);


#warn "!!!!!!!!!!!!!!";

               my $ret_wget = system::wget($server_info->{activations_url}.'?account_id='.$vars->{account_id}.'&action=get_account_characters	');
               if ($ret_wget->{content} =~ /^0$/) {

               } else {
                  my $tmp;

                  @{$tmp} = split(/\n/,$ret_wget->{content});
                  my ($deleted_characters,$characters);

                  foreach my $ln (@{$tmp}) {
                     my ($guid,$name,$race,$class,$level,$online,$live,$expire) = split(/\|/,$ln);
                     if ($live == 0) {
#warn "$name = DELETED";
                        push @{$deleted_characters}, { guid => $guid, name => $name, race => $race, class => $class, level => $level, deleted => $online, expire => $expire, };
                        $deleted_characters_hash->{$guid} = {name => $name, race => $race, class => $class, level => $level,};
                     } else {
#warn "$name = ALIVE";
                        push @{$characters}, { guid => $guid, name => $name, race => $race, class => $class, level => $level, online => $online, expire => $expire, };
                        $characters_hash->{$guid} = {name => $name, race => $race, class => $class, level => $level,};
                     }
                  }
                  $vars->{characters} = $characters;
                  $vars->{characters_hash} = $characters_hash;
                  $vars->{deleted_characters} = $deleted_characters;
                  $vars->{deleted_characters_hash} = $deleted_characters_hash;
               }






               my $traded_char;
               $main::get_game_account_trade_offers->execute($vars->{server_id},$vars->{account_id});
               my $chartrader_offers = $main::get_game_account_trade_offers->fetchall_arrayref({});
               foreach (@{$chartrader_offers}) {
                  $_->{char} = $characters_hash->{$_->{char_guid}};
                  $traded_char->{$_->{char_guid}} = 1;

               }

               $vars->{traded_char} = $traded_char;



               $main::get_character_deletes->execute($vars->{account_id});
               my $char_deletes;
               foreach (@{$main::get_character_deletes->fetchall_arrayref({})}) {
                  $char_deletes->{$_->{char_id}} = $_->{times};
               }
               $main::get_character_deletes->finish();
               $vars->{char_deletes} = $char_deletes;



         }














=cut

      } elsif ($q->{page} eq 'account_state') {
         $page = 'account_state';
         $hf = 1;
         $vars->{title} = 68;
         $location = '';
         $vars->{background} = 'http://wow.mine.sk/temp/testbg.jpg';

         if (!defined($vars->{server_id}) || $vars->{server_id} !~ /^\d+$/) {
            $vars->{server_id} = 18;
         }


         if ($vars->{logged} == 1) {


            $vars->{post_title} = $server_info->{server_name};



               my $ret_wget = system::wget($server_info->{activations_url}.'?account_id='.$vars->{account_id}.'&action=get_gmlevel_by_account_id');
               $ret_wget->{content} =~ m/^([0-9a-f_]+)\|(\d)\|(\d)\|(\d)\|(.*)\|(\d)\|(\d)$/;
               my $version = $1;
               my $gmlevel = $2;
               my $vip = $3;
               my $locked = $4;
               my $last_ip = $5;
               my $expansion = $6;
               my $banned = $7;


               $vars->{gmlevel} = $gmlevel;
               $vars->{version} = $version;
               $vars->{vip} = $vip;
               $vars->{locked} = $locked;
               $vars->{last_ip} = $last_ip;
               $vars->{expansion} = $expansion;
               $vars->{banned} = $banned;






               if (defined($q->{action}) && $q->{action} eq 'change_password') {

                  my $errors = [];


                  my $ret_wget = system::wget($server_info->{activations_url}.'?login='.$login.'&password='.$q->{password}.'&action=check_by_password_and_login');
                  if ($ret_wget->{content} !~ /^[^0]\d*\|.+$/) {push @${errors},'6';}
                  if (length($q->{new_password1}) < 4) {push @${errors},'1';}
                  if (similarity (lc $login, lc $q->{new_password1}) > 0.75) {push @${errors},'2';}
                  if ($q->{new_password1} ne $q->{new_password2}) {push @${errors},'3';}


                  if (scalar(@${errors}) == 0) {

                     $main::get_game_account_state_by_id->execute($id,$vars->{server_id});
                     my $account_state = $main::get_game_account_state_by_id->fetchrow_hashref();


                     my $ret_wget = system::wget($server_info->{activations_url}.'?action=set_password_by_user_and_email&login='.$login.'&email='.$account_state->{email}.'&password='.$q->{new_password1});

                     if ($ret_wget->{state} == 0) {
                        push @${errors},'4';
                     } else {
                        if ($ret_wget->{content} =~ /^1$/) {



                           my $hash = md5_hex($q->{new_password1});
                           $main::set_game_account_password->execute($hash,$q->{new_password1},$id,$vars->{server_id});

                           my $msg = MIME::Lite->new(
					From			=> 'WebAccountRecovery',
					To			=> $account_state->{email},
					Subject			=> 'Vygenerovanie noveho hesla (Automaticky generovany E-mail)',
					Type			=> 'text/plain',
					'Auto-Submitted:'	=> 'auto-generated',
					'X-Originating-IP:'	=> $ENV{REMOTE_ADDR},
					Data			=> 'Tento mail Vam bol odoslany na zaklade odoslania formularu so ziadostou o zmenu hesla pre pristup na '.$server_info->{server_name}.' WoW (z IP adresy '.$ENV{REMOTE_ADDR}.')'."\n\n".'Nove heslo je: '.$q->{new_password1}."\n\n".'Na tento E-mail neodpovedajte!'."\n\n".'S pozdravom Team '.$server_info->{server_name},
                           );

                           $main::log_game_account_data_change->execute($id,$vars->{server_id},$ENV{REMOTE_ADDR},'password',$q->{new_password1});
                           
                           $msg->send;

                           $vars->{ec2} = 1;

                        } else {
                           push @${errors},'5';
                        }
                     }
                  }

                  $vars->{errors2} = $errors;


               } elsif (defined($q->{action}) && $q->{action} eq 'change_email_finish') {

                  my $errors = [];

                  $main::get_game_account_state_by_login->execute($login,$vars->{server_id});
                  my $game_account_state = $main::get_game_account_state_by_login->fetchrow_hashref();

warn "get_game_account_state_by_login->execute($login,$vars->{server_id})";

warn " md5->add(lc($login))";
warn " add(lc($game_account_state->{email}))";
warn " add($game_account_state->{id})";
warn " add(lc($q->{new_email}))";

                  my $md5 = Digest::MD5->new;
                  $md5->add(lc($login));
                  $md5->add(lc($game_account_state->{email}));
                  $md5->add($game_account_state->{id});
                  $md5->add(lc($q->{new_email}));
                  $md5->add('mega coder SoCo');
                  my $hash = $md5->hexdigest;

                  if ($q->{hex} eq $hash) {

                     my $msg = MIME::Lite->new(
			From			=> 'WebAccountActivation',
			To			=> $game_account_state->{email},
			Subject			=> 'Zmena E-mailu k hernemu uctu '.$server_info->{server_name}.' (Automaticky generovany E-mail)',
			Type			=> 'text/plain',
       	                'Auto-Submitted:'	=> 'auto-generated',
               	        'X-Originating-IP:'	=> $ENV{REMOTE_ADDR},
			Data			=> 'Emailova adresa k uctu: "'.$login.'" bola uspesne zmenena'."\n\n".'Na tento E-mail neodpovedajte!'."\n\n".'S pozdravom Team '.$server_info->{server_name},
                     );
                     $msg->send;

                     
                     my $ret_wget = system::wget($server_info->{activations_url}.'?account_id='.$vars->{account_id}.'&action=set_email_by_account_id&email='.$q->{new_email});
                     $main::set_game_account_email->execute($q->{new_email},$game_account_state->{id},$vars->{server_id});
                     $main::log_game_account_data_change->execute($game_account_state->{id},$vars->{server_id},$ENV{REMOTE_ADDR},'email',$q->{new_email});

                     $vars->{ec4} = 2;
                  } else {
                     push @${errors},'2';
                  }


                  $vars->{errors4} = $errors;




               } elsif (defined($q->{action}) && $q->{action} eq 'send_invitation') {

                  my $level_enough = 0;

                  my $ret_wget = system::wget($server_info->{activations_url}.'?account_id='.$vars->{account_id}.'&action=get_account_characters');
                  if ($ret_wget->{content} =~ /^0$/) {

                  } else {
                     my $tmp;

                     @{$tmp} = split(/\n/,$ret_wget->{content});
                     my $characters;
                     foreach (@{$tmp}) {
                        my ($guid,$name,$race,$class,$level,$online,$backuped,$backup_version) = split(/\|/,$_);
                        push @{$characters}, { guid => $guid, name => $name, race => $race, class => $class, level => $level, online => $online, backuped => $backuped, backup_version => $backup_version };
                        if ($level >= 55) {$level_enough = 1;}
                     }
                  }

                  if ($level_enough == 1 && $banned != 1) {

                     my $errors;

                     $main::get_game_account_state_by_id->execute($id,$vars->{server_id});
                     my $account_data = $main::get_game_account_state_by_id->fetchrow_hashref();
                        
                     my $ret_wget = system::wget($server_info->{activations_url}.'?login='.$login.'&password='.$q->{password}.'&action=set_email_by_user_and_password&email='.$account_data->{email});

                     if ($ret_wget->{content} =~ /^1$/) {
                        $main::get_game_account_state_by_login->execute($login,$vars->{server_id});
                        my $game_account_state = $main::get_game_account_state_by_login->fetchrow_hashref();

                        $main::create_invitation->execute();
                        my $invitation_id = $main::create_invitation->{mysql_insertid};


                        my $md5 = Digest::MD5->new;
                        $md5->add(lc($q->{'login'}));
                        $md5->add($invitation_id);
                        $md5->add($game_account_state->{id});
                        $md5->add('invite');
                        $md5->add('mega coder SoCo');
                        my $hash = $md5->hexdigest;

                        $main::update_invitation->execute($hash,$game_account_state->{'id'},$vars->{server_id},lc($q->{'login'}),undef,undef,$q->{'invite_email'},$invitation_id);


                        my $msg = MIME::Lite->new(
				From			=> 'WebAccountInvitation',
				To			=> $q->{invite_email},
				Subject			=> 'Pozvanka na '.$server_info->{server_name}.' (Automaticky generovany E-mail)',
				Type			=> 'text/plain',
        	                'Auto-Submitted:'	=> 'auto-generated',
                	        'X-Originating-IP:'	=> $ENV{REMOTE_ADDR},
				Data			=> 'Tento mail Vam bol odoslany na zaklade pozvania uzivatela serveru.'."\n\n".'ID Vasej pozvanky je: '.uc($hash)."\n\n".'Zaregistrovat sa mozete kliknutim na: '.$globals::index_path.'?page=game_account&invitation='.uc($hash).'&s='.$vars->{server_id}.' alebo neskor '.$globals::index_path.'?page=game_account&s='.$vars->{server_id}.' a vpisanim ID pozvanky do prislusneho policka.'."\n".'Ak neplanujete hrat na serveri '.$server_info->{server_name}.', povazujte tento E-mail za bezpredmetny'."\n\n".'Na tento E-mail neodpovedajte!'."\n\n".'S pozdravom Team '.$server_info->{server_name},
                        );
                        $msg->send;

                        $vars->{ec7} = 1;
                     } else {
                        push @${errors},'1';
                     }
                                       
                     $vars->{errors7} = $errors;


                  }

               } elsif (defined($q->{action}) && $q->{action} eq 'unlock') {

                  my $errors;

                  $main::get_game_account_state_by_id->execute($id,$vars->{server_id});
                  my $account_data = $main::get_game_account_state_by_id->fetchrow_hashref();
                        
                  my $ret_wget = system::wget($server_info->{activations_url}.'?login='.$login.'&password='.$q->{password}.'&action=set_email_by_user_and_password&email='.$account_data->{email});

                  if ($ret_wget->{content} =~ /^1$/) {
                     $main::get_game_account_state_by_login->execute($login,$vars->{server_id});
                     my $game_account_state = $main::get_game_account_state_by_login->fetchrow_hashref();

                     my $md5 = Digest::MD5->new;
                     $md5->add(lc($login));
                     $md5->add(lc($game_account_state->{email}));
                     $md5->add($game_account_state->{id});
                     $md5->add('unlock');
                     $md5->add('mega coder SoCo');
                     my $hash = $md5->hexdigest;

                     if (!defined($game_account_state->{email}) || $game_account_state->{email} eq '') {

                        $vars->{hash} = $hash;
                        $vars->{ec5} = 3;

#c_path.'?page=confirm_new_email&hex='.$hash.'&new_email='.$q->{new_email}.'&id='.$game_account_state->{id}.'&s='.$vars->{server_id}.

                     } else {

                        my $msg = MIME::Lite->new(
				From			=> 'WebAccountUnlock',
				To			=> $game_account_state->{email},
				Subject			=> 'Odomknutie herneho uctu na '.$server_info->{server_name}.' (Automaticky generovany E-mail)',
				Type			=> 'text/plain',
        	                'Auto-Submitted:'	=> 'auto-generated',
                	        'X-Originating-IP:'	=> $ENV{REMOTE_ADDR},
				Data			=> 'Tento mail Vam bol odoslany na zaklade ziadosti (z IP adresy '.$ENV{REMOTE_ADDR}.') o odomknutie pristupu k hernemu uctu.'."\n\n".'Login: '.$login."\n\n".'Aby bol ucet odomknuty, musite kliknut na nasledovnu URL adresu: '.$globals::index_path.'?page=confirm_unlock&hex='.$hash.'&id='.$game_account_state->{id}.'&s='.$vars->{server_id}."\n".'Ak ste o odomknutie herneho uctu neziadali Vy, kontaktujte urychlene GM alebo spravcu servera'."\n\n".'Na tento E-mail neodpovedajte!'."\n\n".'S pozdravom Team '.$server_info->{server_name},
                        );
                        $msg->send;

                        $vars->{ec5} = 1;
                     }
                  } else {
                     push @${errors},'1';
                  }
                                       
                  $vars->{errors5} = $errors;
                                                         
                                                         

               } elsif (defined($q->{action}) && $q->{action} eq 'change_data') {

                  my $errors;

                  $main::get_game_account_state_by_id->execute($id,$vars->{server_id});
                  my $account_data = $main::get_game_account_state_by_id->fetchrow_hashref();
                        
                  my $ret_wget = system::wget($server_info->{activations_url}.'?login='.$login.'&password='.$q->{password}.'&action=set_email_by_user_and_password&email='.$account_data->{email});

                  if ($ret_wget->{content} =~ /^1$/) {
                     $main::get_game_account_state_by_login->execute($login,$vars->{server_id});
                     my $game_account_state = $main::get_game_account_state_by_login->fetchrow_hashref();


                     $main::get_game_account_additional_data->execute($vars->{server_id},$id);
                     my $game_account_data = $main::get_game_account_additional_data->fetchrow_hashref();
                     foreach ('email','chat_nick','forum_nick','name','surname','address','phone') {
                        if ($game_account_data->{$_} ne $q->{$_}) {
                           $main::log_game_account_data_change->execute($id,$vars->{server_id},$ENV{REMOTE_ADDR},$_,$q->{$_});
                        }
                     }

#                     $main::set_game_account_data->execute($q->{email},$q->{chat_nick},$q->{forum_nick},$q->{name},$q->{surname},$q->{address},$q->{phone},$id,$vars->{server_id});
                     $main::set_game_account_data->execute($game_account_state->{email},$q->{chat_nick},$q->{forum_nick},$q->{name},$q->{surname},$q->{address},$q->{phone},$id,$vars->{server_id});


                     my $msg = MIME::Lite->new(
				From			=> 'WebAccountActivation',
				To			=> $game_account_state->{email},
				Subject			=> 'Zmena udajov k hernemu uctu '.$server_info->{server_name}.' (Automaticky generovany E-mail)',
				Type			=> 'text/plain',
        	                'Auto-Submitted:'	=> 'auto-generated',
                	        'X-Originating-IP:'	=> $ENV{REMOTE_ADDR},
				Data			=> 'Tento mail Vam bol odoslany na zaklade ziadosti (z IP adresy '.$ENV{REMOTE_ADDR}.') o zmenu udajov o vlastnikovi herneho uctu.'."\n\n".'Email: '.$q->{email}."\n".'Chat nick: '.$q->{chat_nick}."\n".'Forum nick '.$q->{forum_nick}."\n".'Meno: '.$q->{name}."\n".'Priezvisko: '.$q->{surname}."\n".'Adresa: '.$q->{address}."\n".'Telefon: '.$q->{phone}."\n\n".'Ak ste tieto udaje nemenili Vy, kontaktujte urychlene GM alebo spravcu servera'."\n\n".'Na tento E-mail neodpovedajte!'."\n\n".'S pozdravom Team '.$server_info->{server_name},
                     );
                     $msg->send;

                     


                     $vars->{ec1} = 1;

                  } else {
                     
                     push @${errors},'1';

                  }

                  $vars->{errors1} = $errors;


               } elsif (defined($q->{action}) && $q->{action} eq 'delete_account_data') {

                  my $ret_wget = system::wget($server_info->{activations_url}.'?login='.$login.'&password='.$q->{password}.'&action=delete_account_data_by_login_and_password');







               } elsif (defined($q->{action}) && $q->{action} eq 'chartrader_setup') {



                  $main::get_game_account_trade_offers->execute($vars->{server_id},$vars->{account_id});
                  my $chartrader_offers = $main::get_game_account_trade_offers->fetchall_arrayref({});
                  foreach (@{$chartrader_offers}) {

                     if ($_->{state} == 0) {	# nenastavene
                        if ($q->{'action_'.$_->{char_guid}} == 1) {
                           # zaradit do db
                           $main::set_game_account_trade_state->execute(1,$vars->{server_id},$vars->{account_id},$_->{char_guid});

                        } elsif ($q->{'action_'.$_->{char_guid}} == 2) {
                           $main::del_game_account_trade->execute($vars->{server_id},$vars->{account_id},$_->{char_guid});
                           # zrusit z traderu
                        }
                        # save zmien

                        my $new_class = $q->{'new_class_'.$_->{char_guid}};

                        if (ref($new_class) ne 'ARRAY') {$new_class = [$new_class];}
                        
                        
                        my $new_class_mask = 0;
                        foreach (@{$new_class}) {
                           if ($_ == 0) {$new_class_mask = 0; last;}
                           $new_class_mask += $map_class_id_to_mask->{$_};
                        }

                        $main::set_game_account_trade->execute($q->{'new_faction_'.$_->{char_guid}},$new_class_mask,$vars->{server_id},$vars->{account_id},$_->{char_guid});

                     } elsif ($_->{state} == 1) {

                        if ($q->{'action_'.$_->{char_guid}} == 3) {
                           # zmazat z db
                           $main::del_game_account_trade->execute($vars->{server_id},$vars->{account_id},$_->{char_guid});
                        } elsif ($q->{'action_'.$_->{char_guid}} == 4) {
                           # zmenit parametre
                           $main::set_game_account_trade_state->execute(0,$vars->{server_id},$vars->{account_id},$_->{char_guid});
                        }

                     } elsif ($_->{state} == 2) {
                        if ($q->{'action_'.$_->{char_guid}} == 5) {
                           # zamietnut
                        } elsif ($q->{'action_'.$_->{char_guid}} == 6) {
                           # prijat
                        }

                     }

                  }
                  $main::get_game_account_trade_offers->finish();








               } elsif (defined($q->{action}) && $q->{action} eq 'reset_position') {

                  my $errors;

                  if ($q->{reset_id} =~ /^\d+$/) {

                     if ($q->{'butt'} eq 'Resetnut poziciu') {
                    
                        my $ret_wget = system::wget($server_info->{activations_url}.'?login='.$login.'&action=reset_starting_position&guid='.$q->{reset_id});
                        if ($ret_wget->{content} =~ /^1$/) {
                           $vars->{ec3} = 1;
                        } else {
                           push @{$errors},'1';
                        }
                     } elsif ($q->{'butt'} eq 'Zalohovat') {

                        my $ret_wget = system::wget($server_info->{activations_url}.'?action=backup_character&login='.$login.'&account_id='.$vars->{account_id}.'&guid='.$q->{reset_id});
                        if ($ret_wget->{content} =~ /^1$/) {
                           $vars->{ec3} = 2;
                        } else {
                           push @{$errors},'3';
                        }

                     } elsif ($q->{'butt'} eq 'Chartrader') {



                        $main::is_game_account_in_trade_offers->execute($vars->{server_id},$vars->{account_id},$q->{reset_id});
                        if ($main::is_game_account_in_trade_offers->rows() == 0 ) {
                           $main::add_game_account_trade->execute($vars->{server_id},$vars->{account_id},$q->{reset_id});
                        }
                        $main::is_game_account_in_trade_offers->finish();



                     }

                  } else {
                     push @{$errors},'2';
                  }
                  $vars->{errors3} = $errors;

               } elsif (defined($q->{action}) && $q->{action} eq 'change_email') {

                  my $errors = [];



                  my $ret_wget = system::wget($server_info->{activations_url}.'?login='.$login.'&password='.$q->{password}.'&action=check_by_password_and_login');


                  if ($ret_wget->{content} =~ /^[^0]\d*\|.+$/) {


                     if ($q->{new_email} =~ /^.+\@(.+)$/) {
                        my $res = Net::DNS::Resolver->new;
                        my @mx = mx($res, $1);
                        if (!@mx) {push @${errors},'4';}
                     } else {
                        push @${errors},'3';
                     }
                                   
                     $main::is_game_email_used->execute($q->{new_email},$vars->{server_id});
                     if ($main::is_game_email_used->rows() != 0) {push @{$errors},'5';}
                     $main::is_game_email_used->finish();
                                                                                                                     
                     if (scalar(@{$errors}) == 0) {

                        $main::get_game_account_state_by_login->execute($login,$vars->{server_id});
                        my $game_account_state = $main::get_game_account_state_by_login->fetchrow_hashref();

                        my $md5 = Digest::MD5->new;
                        $md5->add(lc($login));
                        $md5->add(lc($game_account_state->{email}));
                        $md5->add($game_account_state->{id});
                        $md5->add(lc($q->{new_email}));
                        $md5->add('mega coder SoCo');
                        my $hash = $md5->hexdigest;

                        if (!defined($game_account_state->{email}) || $game_account_state->{email} eq '') {

                        $vars->{hash} = $hash;
                        $vars->{ec4} = 3;

#c_path.'?page=confirm_new_email&hex='.$hash.'&new_email='.$q->{new_email}.'&id='.$game_account_state->{id}.'&s='.$vars->{server_id}.

                        } else {

                           my $msg = MIME::Lite->new(
				From			=> 'WebAccountActivation',
				To			=> $game_account_state->{email},
				Subject			=> 'Zmena E-mailu k hernemu uctu '.$server_info->{server_name}.' (Automaticky generovany E-mail)',
				Type			=> 'text/plain',
        	                'Auto-Submitted:'	=> 'auto-generated',
                	        'X-Originating-IP:'	=> $ENV{REMOTE_ADDR},
				Data			=> 'Tento mail Vam bol odoslany na zaklade ziadosti (z IP adresy '.$ENV{REMOTE_ADDR}.') o zmenu E-mailovej adresy k herneho uctu.'."\n\n".'Login: '.$login."\n".'Povodny E-mail: '.$game_account_state->{email}."\n\n".'Novy E-mail: '.$q->{new_email}."\n\n".'Aby bola E-mailova adresa zmenena, musite kliknut na nasledovnu URL adresu: '.$globals::index_path.'?page=confirm_new_email&hex='.$hash.'&new_email='.$q->{new_email}.'&id='.$game_account_state->{id}.'&s='.$vars->{server_id}."\n".'Ak ste o zmenu E-mailovej adresy neziadali Vy, kontaktujte urychlene GM alebo spravcu servera'."\n\n".'Na tento E-mail neodpovedajte!'."\n\n".'S pozdravom Team '.$server_info->{server_name},
                           );
                           $msg->send;

                           $vars->{ec4} = 1;
                        }            

                     }

                  } else {
                     
                     push @${errors},'1';

                  }

                  $vars->{errors4} = $errors;




               } elsif (defined($q->{action}) && $q->{action} eq 'change_expansion') {

                  my $errors;


#                     if ($vip == 1 && $q->{expansion} == 2) {
                        my $ret_wget = system::wget($server_info->{activations_url}.'?login='.$login.'&expansion='.$q->{expansion}.'&action=set_expansion_by_username');
                        $vars->{ec6} = 1;
#                     } else {
#                        push @${errors},'2';
#                     }
                     
                     $vars->{expansion} = $q->{expansion};


                  $vars->{errors6} = $errors;          




               } elsif (defined($q->{action}) && $q->{action} eq 'unlock_finish') {

                  my $errors;

                  my $ret_wget = system::wget($server_info->{activations_url}.'?login='.$login.'&password='.$q->{password}.'&action=check_by_password_and_login');


                  if ($ret_wget->{content} =~ /^[^0]\d*\|.+$/) {
                     $main::get_game_account_state_by_login->execute($login,$vars->{server_id});
                     my $game_account_state = $main::get_game_account_state_by_login->fetchrow_hashref();


                     my $md5 = Digest::MD5->new;
                     $md5->add(lc($login));
                     $md5->add(lc($game_account_state->{email}));
                     $md5->add($game_account_state->{id});
                     $md5->add('unlock');
                     $md5->add('mega coder SoCo');
                     my $hash = $md5->hexdigest;

                     if ($q->{hex} eq $hash) {

                        my $msg = MIME::Lite->new(
				From			=> 'WebAccountUnlock',
				To			=> $game_account_state->{email},
				Subject			=> 'Odomknutie herneho uctu na '.$server_info->{server_name}.' (Automaticky generovany E-mail)',
				Type			=> 'text/plain',
        	                'Auto-Submitted:'	=> 'auto-generated',
                	        'X-Originating-IP:'	=> $ENV{REMOTE_ADDR},
				Data			=> 'Herny ucet "'.$login.'" bol uspesne odomknuty'."\n\n".'Na tento E-mail neodpovedajte!'."\n\n".'S pozdravom Team '.$server_info->{server_name},
                        );
                        $msg->send;

                     
                        my $ret_wget = system::wget($server_info->{activations_url}.'?login='.$login.'&password='.$q->{password}.'&action=unlock_account_by_login_and_password');


                        $vars->{ec5} = 2;
                     } else {
                        push @${errors},'2';
                     }

                  } else {
                     
                     push @${errors},'1';

                  }

                  $vars->{errors5} = $errors;



               }


               $main::get_game_account_state_by_login->execute($login,$vars->{server_id});
               my $account_state = $main::get_game_account_state_by_login->fetchrow_hashref();
               $account_state->{email} =~ m/^(.).*?(\@.+)$/;
               $account_state->{masked_email} = $1.'...'.$2;
                                          
               $vars->{account_state} = $account_state;


               $main::get_invitation_state_by_login->execute($vip,$vip,$login,$vars->{server_id});
               my $invite_state = $main::get_invitation_state_by_login->fetchrow_hashref();
               $vars->{invite_state} = $invite_state;

               if ($invite_state->{do_reset} == 1) {
                  $main::reset_invitation_state_by_login->execute($login,$vars->{server_id});
               }




               $main::get_game_account_accesses->execute($vars->{account_id});
               my $accesses = $main::get_game_account_accesses->fetchall_arrayref({});
               $vars->{accesses} = $accesses;
               $main::get_game_account_accesses->finish();               




#               my $md5 = Digest::MD5->new;
#               $md5->add($login);
#               $md5->add($q->{password});
#               $md5->add($vars->{account_state}->{id});
#               $md5->add('mega coder SoCo');
#               my $hash = $md5->hexdigest;
#
#               $vars->{passwd_hash} = $hash;

               my $level_enough = 0;
               my $characters_hash;

warn $server_info->{activations_url}.'?account_id='.$vars->{account_id}.'&action=get_account_characters';
               my $ret_wget = system::wget($server_info->{activations_url}.'?account_id='.$vars->{account_id}.'&action=get_account_characters');
               if ($ret_wget->{content} =~ /^0$/) {

               } else {
                  my $tmp;

                  @{$tmp} = split(/\n/,$ret_wget->{content});
                  my $characters;

                  foreach my $ln (@{$tmp}) {
                     my ($guid,$name,$race,$class,$level,$online,$backuped,$backup_version) = split(/\|/,$ln);
                     push @{$characters}, { guid => $guid, name => $name, race => $race, class => $class, level => $level, online => $online, backuped => $backuped, backup_version => $backup_version };
                     $characters_hash->{$guid} = {name => $name, race => $race, class => $class, level => $level,};
warn " $vars->{account_id} ::::: guid => $guid, name => $name, race => $race, class => $class, level => $level, online => $online, backuped => $backuped, backup_version => $backup_version";
warn "$ln";
                     if ($level >= 55) {$level_enough = 1;}
                  }
                  $vars->{characters} = $characters;
                  $vars->{characters_hash} = $characters_hash;
               }
               $vars->{level_enough} = $level_enough;



               my $traded_char;
               $main::get_game_account_trade_offers->execute($vars->{server_id},$vars->{account_id});
               my $chartrader_offers = $main::get_game_account_trade_offers->fetchall_arrayref({});
               foreach (@{$chartrader_offers}) {
                  $_->{char} = $characters_hash->{$_->{char_guid}};
                  $traded_char->{$_->{char_guid}} = 1;


#                  foreach (keys(%{%map_class_mask_to_id})) {
#                  }

                  $main::get_game_account_trade_suggestions->execute(
			$globals::races->{$_->{char}->{race}}->{fraction},#moja faction_id
			$map_class_id_to_mask->{$_->{char}->{class}},#bitmaska mojho charu
			$vars->{server_id},
			$_->{new_class},# multiclassmask ktore hladam
			$_->{new_class},# multiclassmask ktore hladam
			$_->{new_faction},# faction ktore hladam
			$_->{new_faction},# faction ktore hladam
			$_->{char}->{level},$_->{char}->{level},
			$_->{char}->{class},$_->{char}->{class}
                  );


                  my $suggestions = $main::get_game_account_trade_suggestions->fetchall_arrayref({});
                  foreach my $sug (@{$suggestions}) {
                     my $want = [];
                     if ($sug->{new_class} != 0) {
                        foreach my $cm (keys(%{$map_class_mask_to_id})) {
#warn "if ($cm & $sug->{new_class}) ";
                           if ($sug->{new_class} & $cm) {push @{$want}, $map_class_mask_to_id->{$cm};}
                        }
                     }
                     $sug->{want} = $want;
                  }
                  $_->{suggestions} = $suggestions;




                  if ($_->{new_class} != 0) {
                     my $tmp_classes;
                     foreach my $class_mask (keys(%{$map_class_mask_to_id})) {
                        if ($_->{new_class} & $class_mask) {
                           $tmp_classes->{$map_class_mask_to_id->{$class_mask}} = 1;
                        }
                     }
                     $_->{new_class} = $tmp_classes;
                  } else {
                     my $tmp_classes;
                     $tmp_classes->{'0'} = 1;
                     $_->{new_class} = $tmp_classes;
                  }

                  $main::get_my_game_accounts_trader_offers->execute($vars->{server_id},$_->{char_guid});
                  my $my_offers = $main::get_my_game_accounts_trader_offers->fetchall_arrayref({});
                  my $my_offer_hash;
                  foreach my $myoff (@{$my_offers}) {
                     $my_offer_hash->{$myoff->{char_guid}} = $myoff;
                  }
                  $main::get_my_game_accounts_trader_offers->finish();
                  $_->{my_offer_hash} = $my_offer_hash;

                  $main::get_game_accounts_trader_offers_to_me->execute($vars->{server_id},$_->{char_guid});
                  my $to_me_offers = $main::get_game_accounts_trader_offers_to_me->fetchall_arrayref({});
                  $main::get_game_accounts_trader_offers_to_me->finish();
                  $_->{to_me_offers} = $to_me_offers;



               }
               $main::get_game_account_trade_offers->finish();
               $vars->{chartrader_offers} = $chartrader_offers;
               $vars->{traded_char} = $traded_char;












































         }

=cut



      } elsif ($q->{page} eq 'confirm_unlock') {
         $hf = 1;
         $vars->{title} = 69;
         $location = '';
         $vars->{background} = 'http://wow.mine.sk/temp/testbg.jpg';

         $main::get_game_account_state_by_id->execute($id,$vars->{server_id});
         my $game_account = $main::get_game_account_state_by_id->fetchrow_hashref();
         $main::get_game_account_state_by_id->finish();


         my $md5 = Digest::MD5->new;

         $md5->add(lc($game_account->{login}));
         $md5->add(lc($game_account->{email}));
         $md5->add($game_account->{id});
         $md5->add('unlock');
         $md5->add('mega coder SoCo');
         my $hash = $md5->hexdigest;

         if ($hash eq $q->{hex}) {

            $vars->{game_account} = $game_account;

            $page = 'confirm_unlock_ok';
         } else {
            $page = 'confirm_unlock_bad';
         }



      } elsif ($q->{page} eq 'confirm_new_email') {
         $hf = 1;
         $vars->{title} = 69;
         $location = '';
         $vars->{background} = 'http://wow.mine.sk/temp/testbg.jpg';

         $main::get_game_account_state_by_id->execute($q->{id},$q->{'s'});
         my $game_account = $main::get_game_account_state_by_id->fetchrow_hashref();
         $main::get_game_account_state_by_id->finish();


#warn "md5->add(lc($game_account->{login}));";
#warn "md5->add(lc($game_account->{email}));";
#warn "md5->add($game_account->{id});";
#warn "md5->add(lc($q->{new_email}));";
#warn "md5->add('mega coder SoCo');";


         my $md5 = Digest::MD5->new;

         $md5->add(lc($game_account->{login}));
         $md5->add(lc($game_account->{email}));
         $md5->add($game_account->{id});
         $md5->add(lc($q->{new_email}));
         $md5->add('mega coder SoCo');
         my $hash = $md5->hexdigest;

         if ($hash eq $q->{hex}) {

            $vars->{game_account} = $game_account;

            $page = 'confirm_new_email_ok';
         } else {
            $page = 'confirm_new_email_bad';
         }




      } elsif ($q->{page} eq 'confirm_email') {
         $hf = 1;
         $vars->{title} = 69;
         $location = '';

         $main::get_game_account_no_email->execute($q->{id});
         my $ret_data = $main::get_game_account_no_email->fetchrow_hashref();
         $main::get_game_account_no_email->finish();

         my $md5 = Digest::MD5->new;
         $md5->add($ret_data->{login});
         $md5->add($ret_data->{email});
         $md5->add($q->{id});
         $md5->add('mega coder SoCo');
         my $hash = $md5->hexdigest;

#warn "$ret_data->{login} + $ret_data->{email} + $q->{id} + mega coder SoCo";
#warn $hash;

         if ($hash eq $q->{hex}) {
            $page = 'confirm_email_ok';


            $main::admin_get_inactive_game_account->execute($q->{id},$q->{'s'});
            my $ret_account = $main::admin_get_inactive_game_account->fetchrow_hashref();


            my $error = 0;

            $main::admin_get_inactive_game_account->execute($q->{id},$q->{'s'});
            my $ret_game_account = $main::admin_get_inactive_game_account->fetchrow_hashref();
            $main::admin_get_inactive_game_account->finish();


#warn $server_info->{activations_url}.'?login='.$ret_account->{login}.'&password='.$ret_account->{plain_passwd}.'&ip='.$ret_game_account->{ip}.'&email='.$ret_game_account->{email};
###########            my $ret_wget = system::wget($server_info->{activations_url}.'?login='.$ret_account->{login}.'&password='.html_encode($ret_account->{plain_passwd}).'&ip='.$ret_game_account->{ip}.'&email='.html_encode($ret_game_account->{email}));
#            my $ret_wget = { state => 1 , content => '1', };
 my $ret_wget = {};

            if ($ret_wget->{state} == 0) {

               $page = 'confirm_email_noconnect';

            } else {
               if ($ret_wget->{content} =~ /^\d+$/ && $ret_wget->{content} ne '0') {
#                  $main::admin_set_active_game_account->execute($vars->{nick_id},$id,$q->{server_id});

                  $main::set_game_account_account_id->execute($ret_wget->{content},$q->{id});

                  my $msg = MIME::Lite->new(
				From			=> 'AccountActivation',
				To			=> $ret_game_account->{email},
				Subject			=> 'Aktivacia herneho uctu na WoW '.$server_info->{server_name}.' (Automaticky generovany E-mail)',
				Type			=> 'text/plain',
				'Auto-Submitted:'	=> 'auto-generated',
				'X-Originating-IP:'	=> $ret_game_account->{ip},
				Data			=> 'Bola spracovana ziadost o vytvorenie WoW konta (z IP adresy '.$ret_game_account->{ip}.'). Konto s prihlasovacime menom "'.$ret_game_account->{login}.'" bolo aktivovane a mozete sa prihlasit. Prajeme prijemne chvile stravene pri WoW.'."\n".'Realmlist je: '.$server_info->{ip}."\n".'Ak ste si ziadne WoW konto neregistrovali, povazujte tento mail za bezpredmetny.'."\n\n".'Na tento E-mail neodpovedajte!'."\n\n".'S pozdravom Team '.$server_info->{server_name},
                   );

                   $msg->send;
                   $main::set_game_account_email_ok->execute($q->{id});
               }
            }

         } else {
            $page = 'confirm_email_bad';
         }


      } elsif ($q->{page} eq 'account_login_recovery') {
         $page = 'account_login_recovery';
         $hf = 1;
         $vars->{title} = 71;
         $location = '';
         $vars->{background} = 'http://wow.mine.sk/temp/testbg.jpg';

         if (defined($q->{action}) && $q->{action} eq 'check') {
            my $errors = [];

            $main::get_login_by_email->execute($q->{email});

            if ($main::get_login_by_email->rows() == 0) {push @{$errors},'0';}
            if (!defined($q->{email}) || $q->{email} eq '') {push @{$errors},'1';}

            $q->{email} =~ m/^.*?\@(.+)$/;
            my $domain = $1;

            if ($domain =~ /azet\.sk$|pokec\.sk$/) {push @{$errors},'2';}




            my $logins = $main::get_login_by_email->fetchall_arrayref({});

            if (scalar(@{$errors}) == 0) {
               $vars->{rv} = 1;

my $plain_logins = [];
foreach (@{$logins}) {
   push @{$plain_logins}, $_->{login};
}

                 my $msg = MIME::Lite->new(
				From			=> 'LoginRecovery',
				To			=> $q->{email},
				Subject			=> 'Pripomenutie prihlasovacieho mena k uctu na WoW '.$server_info->{server_name}.' (Automaticky generovany E-mail)',
				Type			=> 'text/plain',
				'Auto-Submitted:'	=> 'auto-generated',
				'X-Originating-IP:'	=> $ENV{REMOTE_ADDR},
				Data			=> 'Bola spracovana ziadost o zaslanie prihlasovacieho mena (z IP adresy '.$ENV{REMOTE_ADDR}.'), ktore sa viaze na tuto E-mailovu adresu.'."\n".'Konto: '. join('; ',@{$plain_logins}).' (ak je uctov viac, su oddelene bodkociarkou)'."\n\n".'Ak ste o zaslanie prihlasovacieho mena neziadali, povazujte tento mail za bezpredmetny.'."\n\n".'Na tento E-mail neodpovedajte!'."\n\n".'S pozdravom Team '.$server_info->{server_name},


                   );

                   $msg->send;




            } else {
               $vars->{errors} = $errors;
            }








         }


      } elsif ($q->{page} eq 'account_password_recovery2') {
         $page = 'account_password_recovery2';
         $hf = 1;
         $vars->{title} = 71;
         $location = '';
         $vars->{background} = 'http://wow.mine.sk/temp/testbg.jpg';


         if (!defined($vars->{server_id}) || $vars->{server_id} !~ /^\d+$/) {
            $vars->{server_id} = 18;
         }


         if (defined($q->{action}) && $q->{action} eq 'check') {

               my $errors = [];

               my $ret_wget = system::wget($server_info->{activations_url}.'?login='.$q->{login}.'&action=get_gmlevel_by_username');

               if ($ret_wget->{content} =~ m/^([0-9]+)\|(\d+)\|(.*?)\|(\d+)\|(\d+)\|(\d+)$/) {
                  my $gmlevel = $1;
                  my $vip = $2;
                  my $locked = $3;
                  my $last_ip = $4;
                  my $expansion = $5;
                  my $banned = $6;
                  my $account_id = $7;


warn "gmlevel = $gmlevel";
warn "locked = $locked";
warn "last_ip = $last_ip";
warn "expansion = $expansion";
warn "banned = $banned";
warn "account_id = $account_id";

                  if ($gmlevel > 3) {
                     push @{$errors},'2';
                  }
                  if ($vip == 0) {
                     push @{$errors},'9';
                  }


               } elsif ($ret_wget->{content} eq '-1') {
                  push @{$errors},'0';
               }


               $q->{email} =~ m/^.*?\@(.+)$/;
               my $domain = $1;

               if ($domain =~ /azet\.sk$|pokec\.sk$/) {push @{$errors},'10';}




               $main::get_game_account_state_by_login->execute($q->{login},$q->{'s'});
               my $game_account_state = $main::get_game_account_state_by_login->fetchrow_hashref();

               if (lc($game_account_state->{email}) ne lc($q->{email})) {
                  push @{$errors},'1';
               }



               if ($game_account_state->{email_disabled} == 0 && lc($game_account_state->{email}) eq lc($q->{email}) && scalar(@{$errors}) == 0) {

warn "game_account_state->{email_checkable} = $game_account_state->{email_checkable}";
                  if ($game_account_state->{email_checkable} == 1) {

                     push @{$errors},'7';

warn "sending mail from deliverycheck\@anthoria.net";
warn "to recipient $game_account_state->{email}";
warn 'Prave prebehla ziadost o zmenu E-amilovej adresy k hernemu uctu "' .$game_account_state->{login} . '"(z IP adresy '.$ENV{REMOTE_ADDR}.').';

                     my $msg = MIME::Lite->new(
			From                    => 'deliverycheck@anthoria.net',
			To                      => $game_account_state->{email},
			Subject                 => 'Kontrolny E-mail existencie mailovej schranky (Automaticky generovany E-mail)',
			Type                    => 'text/plain',
			'Reply-To'		=> 'support@anthoria.net',
			'Return-Path'		=> 'deliverycheck@anthoria.net',
			'Auto-Submitted:'       => 'auto-generated',
			'X-Originating-IP:'     => $ENV{REMOTE_ADDR},
			'Errors-To'		=> 'deliverycheck@anthoria.net',
			Data   			=> 'Prave prebehla ziadost o zmenu E-amilovej adresy k hernemu uctu "'.$game_account_state->{login}.'" (z IP adresy '.$ENV{REMOTE_ADDR}.') pomocou formularu http://wowaccount.mine.sk/index.fcgi?page=account_password_recovery2 .'."\n\n".'Na tento E-mail neodpovedajte. Ak mate nejake otazky, smerujte ich na adresu support@anthoria.net',
                     );
                     $msg->add("Return-Path" => 'deliverycheck@anthoria.net');
                     $msg->send_by_sendmail(SetSender => 1);
                     $main::set_email_check_stamp->execute($game_account_state->{email});
                  } else {
                     push @{$errors},'8';
                  }
               }



               if (defined($q->{action2}) && $q->{action} eq 'check') {

                  if ($game_account_state->{answer} ne $q->{answer} || $game_account_state->{answer} eq '') {
                     push @{$errors},'3';
                     $main::set_last_answer_try->execute($game_account_state->{id},$q->{'s'});
                  }


                  if ($q->{new_email} !~ m/^['_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*\.(([a-z]{2,3})|(aero|coop|info|museum|name))$/i) {push @{$errors},'5';}
                  $main::is_game_email_used->execute($q->{'new_email'},$q->{'s'});
                  if ($main::is_game_email_used->rows() != 0) {push @{$errors},'6';}
                  $main::is_game_email_used->finish();


               }

               if ($game_account_state->{answerable} != 1) {
                  $errors = ['4'];
                  $q->{action2} = '';
               }


               $vars->{rv} = 0;
               if (scalar(@{$errors}) == 0) {
                  $vars->{rv} = 1;
                  if (defined($q->{action2}) && $q->{action} eq 'check') {
                     $vars->{rv} = 2;

                     my $ret_wget = system::wget($server_info->{activations_url}.'?account_id='.$game_account_state->{account_id}.'&login='.$game_account_state->{login}.'&action=set_email_by_account_id&email='.$q->{new_email});
                     $main::set_game_account_email->execute($q->{new_email},$game_account_state->{id},$q->{'s'});

                     $main::log_game_account_data_change->execute($game_account_state->{id},$q->{'s'},$ENV{REMOTE_ADDR},'email2',$game_account_state->{email},$q->{new_email},1);
                     $main::set_deliverable_by_email->execute(0,$q->{new_email});

                     warn "ok";
                  }
               } else {

warn "!! LEN TERAZ !!";
# email_change_attempts
                  $vars->{errors} = $errors;
               }

               $vars->{game_account_state} = $game_account_state;

         }

               


      } elsif ($q->{page} eq 'account_password_recovery') {
         $page = 'account_password_recovery';
         $hf = 1;
         $vars->{title} = 71;
         $location = '';
         $vars->{background} = 'http://wow.mine.sk/temp/testbg.jpg';


         $vars->{post_title} = $server_info->{server_name};

         if (defined($q->{action}) && $q->{action} eq 'check') {

            my $error = 0;
            if (defined($server_info->{activations_url}) && $server_info->{activations_url} ne '') {
warn "system::wget($server_info->{activations_url}.'?action=check_by_password_and_email&login='.$q->{login}.'&email='.$q->{email})";
               my $ret_wget = system::wget($server_info->{activations_url}.'?action=check_by_password_and_email&login='.$q->{login}.'&email='.html_encode($q->{email}));



               $q->{email} =~ m/^.*?\@(.+)$/;
               my $domain = $1;


               if ($ret_wget->{state} == 0 || $domain =~ /azet\.sk$|pokec\.sk$/) {
                  $vars->{return} = 2;
               } else {
                  if ($ret_wget->{content} =~ /^1$/) {

                     srand;
                     my $rid = int(rand(4294967290));

                     $main::add_random_to_game_account->execute($rid,$q->{'s'},$q->{login},$q->{email});

                     my $msg = MIME::Lite->new(
				From			=> 'WebAccountRecovery',
				To			=> $q->{email},
				Subject			=> 'Instrukcie ku vygenerovaniu noveho hesla na '.$server_info->{server_name}.' WoW (Automaticky generovany E-mail)',
				Type			=> 'text/plain',
				'Auto-Submitted:'	=> 'auto-generated',
				'X-Originating-IP:'	=> $ENV{REMOTE_ADDR},
				Data			=> 'Tento mail Vam bol odoslany na zaklade odoslania formularu so ziadostou o vygenerovanie noveho hesla pre pristup na '.$server_info->{server_name}.' WoW (z IP adresy '.$ENV{REMOTE_ADDR}.'). Ak si zelate vygenerovat nove heslo, kliknite na nasledujucu URL adresu: '.$globals::index_path.'?page=account_password_recovery&action=new&login='.$q->{login}.'&email='.$q->{email}.'&id='.$rid.'&s='.$q->{'s'}.' a nove heslo Vam bude zaslane v E-maile o par sekund. Pokial ste o heslo neziadali Vy, tak tento E-mail jednoducho ignorujte.'."\n\n".'Na tento E-mail neodpovedajte!'."\n\n".'S pozdravom Team '.$server_info->{server_name},
                     );

                     $msg->send;

                     $vars->{return} = 1;
                  } else {
                     $vars->{return} = 0;
                  }
               }
            }
         } elsif (defined($q->{action}) && $q->{action} eq 'new') {
            my $error = 0;
            if (defined($server_info->{activations_url}) && $server_info->{activations_url} ne '') {
               my $ret_wget = system::wget($server_info->{activations_url}.'?action=check_by_password_and_email&login='.$q->{login}.'&email='.html_encode($q->{email}));
               if ($ret_wget->{state} == 0) {
                  $vars->{return} = 2;
               } else {
                  if ($ret_wget->{content} =~ /^1$/) {
                     $main::check_game_account_by_random->execute($q->{id},$q->{'s'},$q->{login},$q->{email});
                     if ($main::check_game_account_by_random->rows() == 1) {
                        my $user_id = $main::check_game_account_by_random->fetchrow();

                        my $arr = [qw(q w e r t y u i p a s d f g h k z x c v b n 2 3 4 5 6 7 9 A C E F H K L M P R T U X Y Z)];
                        my $salt;
                        for (my $i=0; $i<8; ++$i) {$salt .= $arr->[int(rand(scalar(@{$arr})))]}

                        my $ret_wget = system::wget($server_info->{activations_url}.'?action=set_password_by_user_and_email&login='.$q->{login}.'&email='.html_encode($q->{email}).'&password='.$salt);

#warn 'system::wget('.$server_info->{activations_url}.'?action=set_password_by_user_and_email&login='.$q->{login}.'&email='.$q->{email}.'&password='.$salt.')';
                        if ($ret_wget->{state} == 0) {
                           $vars->{return} = 2;
                        } else {
                           if ($ret_wget->{content} =~ /^1$/) {


                              my $hash = md5_hex($salt);
                              $main::set_game_account_password->execute($hash,$salt,$user_id,$q->{'s'});
#warn "main::set_game_account_password->execute($hash,$salt,$user_id,$q->{'s'})";
                           
                              my $msg = MIME::Lite->new(
					From			=> 'WebAccountRecovery',
					To			=> $q->{email},
					Subject			=> 'Vygenerovanie noveho hesla (Automaticky generovany E-mail)',
					Type			=> 'text/plain',
					'Auto-Submitted:'	=> 'auto-generated',
					'X-Originating-IP:'	=> $ENV{REMOTE_ADDR},
					Data			=> 'Tento mail Vam bol odoslany na zaklade odoslania formularu so ziadostou o vygenerovanie noveho hesla pre pristup na '.$server_info->{server_name}.' WoW (z IP adresy '.$ENV{REMOTE_ADDR}.')'."\n\n".'Nove heslo je: '.$salt."\n\n".'Na tento E-mail neodpovedajte!'."\n\n".'S pozdravom Team '.$server_info->{server_name},
                              );


#warn "main::get_game_account_state_by_id->execute($user_id,$q->{'s'});";

                              $main::get_game_account_state_by_id->execute($user_id,$q->{'s'});
                              my $account_data = $main::get_game_account_state_by_id->fetchrow_hashref();



                              $main::log_game_account_data_change->execute($user_id,$q->{'s'},$ENV{REMOTE_ADDR},'password',$account_data->{password},$salt,1);
                           
                              $msg->send;

                              $vars->{return} = 3;
                           } else {
                              $vars->{return} = 0;
                           }
                        }
                     } else {
                        $vars->{return} = 4;
                     }
                  } else {
                     $vars->{return} = 0;
                  }
               }
            }


         }


      } elsif ($q->{page} eq 'serve_syscheck') {

         if ($vars->{logged} == 1) {
            $hf = 0;
            $location = '';

            $main::get_game_account_state_by_id->execute($id,$vars->{server_id});
            my $account_data = $main::get_game_account_state_by_id->fetchrow_hashref();

            open (FILE, 'syscheck._xe');
            binmode(FILE);
            while (my $line = <FILE>) {
               if ($line =~ /0123456789012345678901234567890123456789012345678901234567890123/) {
                  $line =~ s/0123456789012345678901234567890123456789012345678901234567890123/$account_data->{random_hex}/gsm;
               }
               $html .= $line;
            }
            close (FILE);

            $mime = 'application/octet-stream'."\n".'Content-transfer-encodig: binary'."\n".'Content-disposition: attachment; filename=syscheck.exe'."\n".'Content-length:'.length($html);
         } else {
            $hf = 1;
            $page = 'account_state';

         }



      } elsif ($q->{page} eq 'submit_info') {
         $hf = 0;
         $location = '';

warn $q->{'l'};
         $main::get_account_by_hex->execute($q->{'l'});
         my $account_id = $main::get_account_by_hex->fetchrow();
         $main::get_account_by_hex->finish();

         my $insert = 1;
         $main::get_system_info_by_account->execute($account_id);
         foreach (@{$main::get_system_info_by_account->fetchall_arrayref({})}) {
            if ($_->{'resolution'} eq $q->{'d'} && $_->{'cpu_name'} eq $q->{'c'} && $_->{'ram_size'} eq $q->{'r'} && $_->{'hdd_total'} eq $q->{'t'} && $_->{'vga_name'} eq $q->{'v'} && $_->{'win_major'} eq $q->{'j'} && $_->{'win_minor'} eq $q->{'n'} && $_->{'win_name'} eq $q->{'e'} && $_->{'win_build'} eq $q->{'b'}) {
               $insert = 0;
            }
         }
         $main::get_system_info_by_account->execute($account_id);

         $mime = 'text/plain';
         if ($account_id) {

            if ($insert == 1) {
               $main::add_info_to_account->execute($account_id,$q->{'d'},$q->{'c'},$q->{'z'},$q->{'r'},$q->{'t'},$q->{'f'},$q->{'v'},$q->{'j'},$q->{'n'},$q->{'e'},$q->{'b'});
               warn " main::add_info_to_account->execute($account_id,$q->{'d'},$q->{'c'},$q->{'z'},$q->{'r'},$q->{'t'},$q->{'f'},$q->{'v'},$q->{'j'},$q->{'n'},$q->{'e'},$q->{'b'}";
               $html = "Udaje boli ulozene\n";
            } else {
               $html = "CHYBA: Udaje NEBOLI ulozene - na serveri uz existuju identicke udaje\n";
            }
         } else {
            $html = "CHYBA: Udaje NEBOLI ulozene - stiahnite si novy syscheck.exe\n";

         }


      }





      if ($location eq '' && -f $globals::datadir."/templates/".$page.".tt2" && $mime eq '') {
         $vars->{elapsed} = &tv_interval($t0);;
         $vars->{utime} = $t0->[0];
         if ($usegzip) {print 'Content-Encoding: '.$usegzip."\n";}
         print 'Content-type: '.$contenttype."\n\n";
         if ($hf == 1) {$template->process('header.tt2', $vars, \$html) || warn $template->error();}
         $template->process($page.'.tt2', $vars, \$html) || warn $template->error();
         if ($hf == 1) {$template->process('footer.tt2', $vars, \$html) || warn $template->error();}
         if ($usegzip) {$html = Compress::Zlib::memGzip( $html );}
         print $html;
         $html = '';
      } elsif ($location ne '' && $mime eq '') {
         if ($set_cookie ne '') {print $set_cookie;}
         print 'Location: '.$location."\n\n";
         $request->Finish();
         next;
      } elsif ($location eq '' && $mime ne '') {
         print 'Content-type: '.$mime."\n\n";
         print $html;
         $request->Finish();
         next;
      } else {
         print 'Content-type: text/html'."\n\n".'bububu';
      }
   }
}



sub plain_time_format($) {
   my ($utime) = @_;

   my $h = int($utime / 3600);
   my $stack = $utime % 3600;
   my $m = int($stack / 60);
   my $s= $stack  % 60;

   my $out;
   if ($h != 0) {$out = $h.'h ';}
   $out .= $m.'m ';

   return $out;
}

sub html_encode($) {
   my ($str) = @_;
   $str =~ s/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg;
   return $str;
}
