<?
########## Confixx(R) 3.0 Professional ############
####### Copyright SWsoft, Inc. 2004-2005 ##########
#### http://www.sw-soft.com - info@sw-soft.com ####

#
#
#
$DEBUG=0;
#
#
#

if ($DEBUG){
  echo "login.php<br>\n";
}

include("settings.inc.php");
include("functions.inc.php");
include('sessions.inc.php');
$miscf = $default_language."/misc.inc.php";
include(dirname(__FILE__) . "/languages/$miscf");

$noflash=1;
if ($DEBUG){
  echo "host: $db_host, user: $db_user, pass: $db_pass<br>\n";
}
db_connect($db_host, $db_user, $db_pass);

sessao_start();


if($_POST['username'] && $_POST['password']){
  if ($DEBUG){
    echo "username: ".$_POST['username'].", password: ".$_POST['password']."<br>\n";
  }

  if(get_magic_quotes_gpc()){
    $_POST['username'] = stripslashes($_POST['username']);
    $_POST['password'] = stripslashes($_POST['password']);
  }
  if(!checkIPRest($_SERVER['REMOTE_ADDR'])){
    $_SESSION['_error'] = ltext('form_bad_ip');
    header('Location: index.php');
    newLocation('index.php');
    exit();
  }
 
  // check that user 1) exists, 2) not locked and 3) autentified
  $account_info =array();
  $account_info = checkLogin($_POST['username']);
  if ($account_info != false
    && !$account_info['locked']
    && checkPassword($account_info, $_POST['password'])) 
  {
	// we should not use $_POST['username'] below, because the entered login,
	// could be not real login, but alias
	
    switch ($account_info['type']){
    case USERTYPE_ADMIN:
      if(array_key_exists('_cat', $_POST) && $_POST['_cat'] == 'reg')
      {
        $url = 'reg/';
      }
      else
      {
        $url = 'admin/';
      }
      break;
			
    case USERTYPE_RESELLER:
      $url = 'reseller/' . $account_info['login'] . '/';
      break;
			
    case USERTYPE_USER:
      if(array_key_exists('_cat', $_POST) && $_POST['_cat'] == 'ftp')
      {
      	if(session_is_registered('ftp_path') && array_key_exists($account_info['login'], $_SESSION['ftp_path']))
      	{
      		unset($_SESSION['ftp_path'][$account_info['login']]);
      	}
      	$url = 'ftplogin/' . $account_info['login'] . '/';
      }
      else 
      {
      	$url = 'user/' . $account_info['login'] . '/';
      }
      break;
    
    case USERTYPE_FTPUSER:
      if(session_is_registered('ftp_path') && array_key_exists($account_info['login'], $_SESSION['ftp_path']))
      {
      	unset($_SESSION['ftp_path'][$account_info['login']]);
      }
      $url = 'ftplogin/' . $account_info['login'] . '/';
      break;

    case USERTYPE_POPUSER:
      $url = 'poplogin/' . $account_info['login'] . '/';
      break;
			
    default:
      $url = 'index.php';
    }
	// as user successfully logged in, we can clean temporary passwords
    clearForgotPwd($account_info['login']);
    addSUser($account_info['login'], $_POST['password'], $account_info['type']);
    db_close();
    header('Location: ' . $url);
		//echo 'all ok';
    newLocation($url);
    exit();
  }
}

$_SESSION['_error'] = ltext('form_bad_username');

$url = 'index.php';
if(array_key_exists('_cat', $_POST)) {
	switch ($_POST['_cat']) {
	case 'ftp':
		$url = 'ftplogin/';
		break;
	case 'pop3':
		$url = 'poplogin/';
		break;
	default:
	}
}

header('Location: ' . $url);
newLocation('index.php');

?>
