<?php
/**
* MiniClick
* Gestionnaire de tableau collaboratif
* Copyright (C) 2006 Guillaume Monet <gm@ibreizh.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 
*
* @package miniclick
* @version 5.0.0
*/

session_start();
include("config.php");
include("db/db_interface.php");
$db = db_interface::getInstance();
$db->db_connect();
$db->db_select(DB_NAME);

require(SMARTY_LIBS_DIR.'Smarty.class.php');
$smarty = new Smarty;
$smarty->template_dir = SMARTY_TEMPLATE_DIR;
$smarty->compile_dir = SMARTY_COMPILE_DIR;
$smarty->config_dir = SMARTY_CONFIG_DIR;
$smarty->cache_dir = SMARTY_CACHE_DIR;

$smarty->assign('css',MINICLICK_HOST_URL.'css/login.css');

$table=$db->db_query("SELECT User_Id,Login,Nom,Prenom FROM USER WHERE Login='".$_POST['login']."' and Passwd=PASSWORD('".$_POST['password']."')");


if($ligne = $db->db_fetch_object($table)){
	
	$_SESSION['user_id'] = $ligne->User_Id;
	$_SESSION['login'] = $ligne->Login;
	$_SESSION['nom'] = $ligne->Nom;
	$_SESSION['prenom'] = $ligne->Prenom;

	$db->db_query("UPDATE USER SET LastConnexion='".date('Y-m-d H:i:s')."' WHERE User_Id='".$ligne->User_Id."'");
	$sql_query = "SELECT App_Id,Role_Id FROM APP_ACCESS WHERE User_Id = '".$ligne->User_Id."'";
	
	$apps = $db->db_query($sql_query);

	if($db->db_num_rows($apps) > 1){

		$applications_id = array();
		$applications_name = array();
		$auth_app = array();

		while($app = $db->db_fetch_object($apps)){;
			$query_app = "SELECT Nom,Host,Db FROM APP WHERE App_Id=".$app->App_Id;
			$res_apps = $db->db_query($query_app);
			$res_app = $db->db_fetch_object($res_apps);
			array_push($applications_name,"".$res_app->Nom);
			array_push($applications_id,"".$app->App_Id);
			$tmp = array();
			array_push($tmp,$app->App_Id);
			array_push($tmp,$res_app->Nom);
			array_push($auth_app,$tmp);
		}
		$_SESSION['auth_apps'] = $auth_app;

		$smarty->assign('applications_id',$applications_id);
		$smarty->assign('applications_name',$applications_name);
		$smarty->assign('title',"Choisir une application");
		$smarty->assign('css',MINICLICK_HOST_URL.'css/login.css');
		$smarty->display('choisir_app.tpl');
	}else{
		$app = $db->db_fetch_object($apps);
		$query_app = "SELECT App_Id,Nom,Host,Db FROM APP WHERE App_Id=".$app->App_Id;
		$res_apps = $db->db_query($query_app);
		$res_app = $db->db_fetch_object($res_apps);
		$_SESSION['app_id'] = $res_app->App_Id;
		$_SESSION['app_name']=$res_app->Nom;
		$_SESSION['role_id']=$app->Role_Id;
		$_SESSION['database']=$res_app->Db;
		$smarty->assign('page',$res_app->Host);
		$smarty->display('redirect.tpl');
	}
}else{
	session_destroy();
	$smarty->assign('page',MINICLICK_HOST_URL.'index.php');
	$smarty->display('redirect.tpl');
}
?>
