#!/usr/bin/perl
# index.pl
# Function: Open the signup screen and newuser page
# atmail@CGIsupport.com - July 16 2001

# Source-code (C) CGIsupport.com 1999-2001 . All rights reserved
# See http://cgisupport.com/license.ehmtl for license agreement

use strict;
use lib "/var/www/vhosts/bestmail.gr/httpdocs/webemail/libs/"; # Change to your path

use lib "libs"; # Comment out line if running Mod_perl
$|++;

use Atmail::Global;
use Atmail::Config;

my (%var);

if ( $ENV{HTTP_USER_AGENT} =~ /MSIE/ ) {
    $var{browser} = "ie";
}
else {
    $var{browser} = "ns";
}

$var{version} = $pref{version};

my $atmail = new Atmail::Global();

$var{func} = $atmail->param('func');

$var{func} = "ext" if ( !$var{func} && $pref{install_type} eq "standalone" );

$var{mailtype} = $atmail->print_mailtypes();
$var{advanced} = $atmail->param('advanced');

# If the user if logging off, print a cookie header with
# a blank SessionID. Delete the Session for the DB too
if ( $var{func} eq "logout" ) {

    print $atmail->cookie_header_delete();
    $var{Error} =
      "- Logged Off <script language='Javascript'>window.focus();</script>";
    $atmail->clean_tmp();
}
else {

    # Otherwise, a standard header
    print $atmail->header();
}

my $auth = new Atmail::Auth;

#$auth->{debug} = 1;
$atmail->cookie_read($auth);
$auth->getuser( $atmail->{SessionID} );

print $atmail->{SessionID};

$var{username} = $atmail->param('username');
$var{pop3host} = $atmail->param('pop3host');

$var{domainbox} = $atmail->domains();
$var{domainbox} =~
  s/value='$auth->{pop3host}'/value='$auth->{pop3host}' selected/g;

$var{languagebox} = $atmail->languages('1');

if ( $var{func} eq "demo" ) {

    $var{expire} = $reg{expiry} - time();
    $var{expire} = $var{expire} / 86400;
    $var{expire} =~ s/\..*//g;

    if ( $var{expire} eq "0" ) {
        $var{expire} = "Today";
    }
    elsif ( $reg{expiry} eq "never" ) {
        $var{expire} = "Never";
    }
    else {
        $var{expire} = "in $var{expire} days";
    }

    print $atmail->parse( "html/demo.html", expire => $var{expire} );
    $atmail->cleanup();
}

if ( $var{func} eq "signup" && $pref{allow_Signup} ) {

    # Build the custom language popup. Inc/ the default language selected
    $var{languagebox} = $atmail->languages();
    print $atmail->parse( "html/$atmail->{Language}/signup.html", %var );
}

# Extra module for iBill Customers
elsif ( $var{func} eq "signuppay" && $pref{allow_Signup} ) {

    # Build the custom language popup. Inc/ the default language selected
    $var{languagebox} = $atmail->languages();
    print $atmail->parse( "html/$atmail->{Language}/signuppay.html", %var );
}

elsif ( $var{func} eq "usage" ) {
    print $atmail->parse( "html/$atmail->{Language}/usage.html", %var );
}
elsif ( $var{func} eq "passutil" && $pref{allow_Passutil} ) {

    use Atmail::SQL;
    my $sql = new Atmail::SQL;
    $sql->table_names( $atmail->{username} );

    # The user clicked the password hint button
    if ( $atmail->param('Hint') ) {

        if ( $pref{sql_type} ) {

            $var{status} =
              "Password Hint: "
              . $sql->getvalue( "select PasswordQuestion from Users
    where Account='$var{username}\@$var{pop3host}'" );

        }
        else {

            my %db =
              Atmail::Flat->gethash(
              "$pref{user_dir}/users/$var{username}\@$var{pop3host}/user.db");

            $var{status} = "Password Hint: $db{PasswordQuestion} ";

        }

        # The user has selected to change the password
    }
    elsif ( $atmail->param('Update') ) {

        $var{newpass} = $atmail->param('newpass');
        $var{oldpass} = $atmail->param('oldpass');
        $var{PasswordQuestion} = $atmail->param('PasswordQuestion');

        $auth->{username} = $var{username};
        $auth->{pop3host} = $var{pop3host};

        my $status =
          $auth->changepass( $var{newpass}, $var{oldpass},
          $var{PasswordQuestion} );

        if ( $status eq "1" ) {

            # Password matches
            $var{status} =
              $atmail->parse("html/$atmail->{Language}/msg/chpasswdok.html");
        }
        else {
            $var{status} =
              $atmail->parse("html/$atmail->{Language}/msg/chpasswderror.html");
        }

    }

    print $atmail->parse( "html/$atmail->{Language}/passutil.html", %var );
}

# If the user has defined a new logout_url . To another site or a local file
elsif ( $var{func} eq "logout" && $pref{logout_url} !~ /index\.pl/ ) {
    print
"<script language='JavaScript'> location.href='$pref{logout_url}'; </script>";

}

# If the @Mail software is configured for standalone use, always display the
# login-ext.html page
elsif ( $var{func} eq "ext"
  || !$domains{ $atmail->{pop3host} } && $atmail->{pop3host} )
{

    $var{mailtypes} = $atmail->print_mailtypes();

    print $atmail->parse( "html/$atmail->{Language}/login-ext.html", %var );
}
elsif ( $var{func} eq "disclaimer" && $pref{disclaimer} ) {
    print $atmail->parse( "$pref{disclaimer}", %var );
}

else {
    print $atmail->parse( "html/$atmail->{Language}/login.html", %var );
}

# We're running under mod_perl            
if ( exists $ENV{MOD_PERL} ) {
    print "<!-- MOD PERL -->";
}
