<?
// Wriiten by Spike^ekipS <spike@spikeekips.net>

//-------------------------------------------------- global include
include_once "./setting.php";

// include
include_once $global["root"] . "/include/handle.php";
include_once $global["root"] . "/include/print_array.php";

// lib
include_once $global["root"] . "/lib/Site.php";
include_once $global["root"] . "/lib/Item.php";
include_once $global["root"] . "/lib/Authentication.php";
include_once $global["root"] . "/lib/Authentication_Mail.php";
include_once $global["root"] . "/lib/Authentication_Domain.php";
include_once $global["root"] . "/lib/State.php";

include_once sprintf("%s/lang/%s.php", $global["root"], $global["default_language"]);

$Auth = new Authentication;
$Auth->escape("_GET");

//-------------------------------------------------- argument parsing
extract($_POST);

if (!$address) {
	goprev();
}

if (!$password) {
	goprev();
}

if (!ereg("@", $address)) {
	goprev();
}

$Site = new Site;
$Site->connect_db();
$Site->start_session();

$Auth->set_address($address);
$var = $Auth->verify($password);

if ($var === -2) {
	$Auth = new Authentication_Domain($address);
	$Auth->assign();

	$_SESSION["user_authenticated"] = true;

	// set timeout.
	$_SESSION["user_timeout"] = $_SESSION["config_timeout"] + mktime();

	//$url = "Location: " . $_SESSION["config_www_name"] . "/domain";
	$url = sprintf("http://%s/domain", $_SESSION["config_www_name"]);

	$Site->end();

	Header("Location: " . $url);
	exit;

} else if ($var == true) {

	//------------------------------ session registering.
	// register to $_SESSION
	$Auth = new Authentication_Mail($address);
	$Auth->assign();

	$_SESSION["user_authenticated"] = true;

	$item = new Item();
	$item->set_item_type();

	$State = new State;
	$State->assign_saved_sort();

	// set timeout.
	if (array_key_exists("config_timeout", $_SESSION)) {
		$_SESSION["user_timeout"] = $_SESSION["config_timeout"] + mktime();
	}

	$url = sprintf("http://%s/mail", $_SESSION["config_www_name"]);

	$Site->end();

	Header("Location: " . $url);
	exit;

} else if ($var == "2") {
	$_SESSION["user_msg"]["content"] = $string["login4"];
	$_SESSION["user_msg"]["type"] = "2";

	$Site->end1();

	goprev();

} else {
	$_SESSION["user_msg"]["content"] = $string["login3"];
	//$_SESSION["user_msg"]["type"] = "2";

	$Site->end1();
	goprev();
}

?>
