<?php
/* $Id: index_ntlm.php,v 1.25 2004/05/24 13:54:02 liedekef Exp $ */

// These line is not standard module a line...dont use it elsewhere
// --------------------------------------------------------------------
if(!file_exists("config.inc.php")) exit("<html><head><title>Error</title><link rel='stylesheet' type='text/css' href='./setup/templates/default/css/setup.css'></head><body bgcolor='#c8c9d4'><table align='center' width='500'><tr><td><br /><table border='0' cellpadding='6' cellspacing='1' bgcolor='black'><tr><td align='center' bgcolor='#ededed'style='font: 12px Arial;'><strong>ERROR</strong></td></tr><tr><td bgcolor='#ededed'style='font: 12px Arial;'><br />Please <a href='setup/index.php'>run setup</a> first.<br /><br />Have a <a href='docs/html/en/index.html'>look at the manual</a> for more information. Chances are, that you need to <a href='http://www.moregroupware.org/manual/mgw-manual-en.tar.gz'>download</a> it first - or <a href='http://www.moregroupware.org/manual/en/index.html'>read it online</a>.</td></tr></table></td></tr></table></body></html>");
// --------------------------------------------------------------------
$myEnv["module"] = "general";
$myEnv["output"] = "html";
$myEnv["translate"] = true;
$myEnv["stdsmenu"] = false;
$myEnv["auth"] = false;

include('config.inc.php');
include(INCLUDEPATH ."container.inc.php");

$num = 0;
$conn = connect_database();

if (!isset($_SERVER["REMOTE_USER"]) || !$appconf["use_ntlm_auth"]) { redirect("index.php"); }
$login = str_replace("\\","",substr($_SERVER["REMOTE_USER"],strpos($_SERVER["REMOTE_USER"],"\\")));

if (isset($_SESSION["MGW"]->username) && $_SESSION["MGW"]->username != "" && $login != $_SESSION["MGW"]->username) {
  $_SESSION = array();
  $_SESSION["MGW"]= new MGW();
  session_destroy();	
}

$smarty->assign("mylogin", $login);

if($_SERVER["REQUEST_METHOD"] == "POST"){
    $loginlang = $_POST["language"]; 
    $lsql = "SELECT charset FROM mgw_languages WHERE langcode=".$conn->QMagic($_POST["language"]);
    if (!$lres = $conn->Execute($lsql)) exit(showSQLerror($lsql, $conn->ErrorMsg(), __LINE__, __FILE__));
    $lrow = $lres->FetchRow();
    $logincharset = $lrow["charset"];
} else {
    $loginlang = $appconf["def_language"];
    $logincharset = $appconf["def_charset"];
}
Lang::setLocaledText($loginlang,"general");
$smarty->assign("charset",$logincharset);

$sql = "SELECT is_ldap FROM mgw_users WHERE username=".$conn->QMagic($login);
if (!$res = $conn->Execute($sql)) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
$row = $res->FetchRow();

if ($res->RecordCount()==0) { //the username is not present in the database // 
    if ($appconf["create_accounts_on_login"]) {
	// now check with ldap auth
	include(INCLUDEPATH.'Auth/ldap.inc.php');
	if (!authentify_user($login, $password, $encrypted_pwd)) {
	    include(INCLUDEPATH . "mSelectBox.class.php");
	    $lsb = new mSelectBox("language");
	    $lsb->setContentType("sql");
	    $lsb->setSQLvalue("langcode");
	    $lsb->setSQLcontent("description");
	    $lsb->addContent("SELECT langcode, description FROM mgw_languages ORDER BY description");
	    $smarty->assign("lsb", $lsb->draw($loginlang, 'onchange="document.f1.submit()"'));

	    $smarty->assign('loginerrortext', Lang::getLanguageString("loginerror"));
	    $smarty->display("login.tpl");
	    exit();
	}
	else{
	    create_user_account_on_login($login);
	}
    } else { //the user is not allow to create a new account
	redirect("index.php");
    }
}
if ($row = get_user_info($login)) $num=1;

if ($num == 0) { redirect("index.php"); }

if ($row["lang"] == "login" && !isset($_POST["loginbutton"])) {
  include(INCLUDEPATH . "mSelectBox.class.php");

  $lsb = new mSelectBox("language");
  $lsb->setContentType("sql");
  $lsb->setSQLvalue("langcode");
  $lsb->setSQLcontent("description");
  $lsb->addContent("SELECT langcode, description FROM mgw_languages ORDER BY description");
  $smarty->assign("lsb", $lsb->draw($loginlang, 'onchange="document.f1.submit()"'));
  $smarty->display("login2.tpl");

} else if (isset($_SESSION['MGW']) && isset($_SESSION['MGW']->username) && $_SESSION['MGW']->username!="" && isset($_POST["loginbutton"])) {
  redirect("modules/".$_SESSION["MGW"]->settings["def_module"]."/index.php?".SID);
} else {
    
  $_SESSION = array();
  $_SESSION["MGW"]= new MGW();
  $_SESSION["lastin"] = $conn->UnixTimeStamp($row["lastin"]);
  $sql = "UPDATE mgw_users SET lu_date=lu_date, lastin=".$conn->DBTimeStamp(time())." WHERE id=".(int)$row["id"];
  if (!$conn->Execute($sql)) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));

  $_SESSION["checkip"] = (isset($_POST["checkip"]) && $_POST["checkip"]==1) ? 1 : 0;
  $_SESSION["remoteip"] = get_remote_addr();
  $_SESSION["http_user_agent"] = $_SERVER["HTTP_USER_AGENT"];
  if($row["lang"] != "login"){
    $_SESSION["MGW"]->spkz = $row["lang"];
  } else {
    $_SESSION["MGW"]->spkz = $_POST["language"];
  }
  $_SESSION["MGW"]->charset = $row["charset"];

  $_SESSION["MGW"]->fullusername = $row["firstname"] . " " . $row["lastname"];
  $_SESSION["MGW"]->email = $row["email"];
  $_SESSION["MGW"]->username = $row["username"];
  $_SESSION["MGW"]->userid   = $row["id"];
  $_SESSION["MGW"]->login = $login;
  $_SESSION["MGW"]->modules = array();
  $_SESSION["MGW"]->modules = getInstalledModules();
  $_SESSION["MGW"]->groups = get_user_groups();
  $_SESSION["MGW"]->groups_names = get_user_groups_names();
  $_SESSION["MGW"]->browser = browser_detect();
  $smarty->assign("charset", $_SESSION["MGW"]->charset);
		
  getUserSettings();

  redirect("modules/".$_SESSION["MGW"]->settings["def_module"]."/index.php?".SID);
}
?>
