<?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();
unset($_SESSION['database']);

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;
$_SESSION['societe'] = "";

if($_GET['capplication'] != ""){
	$_SESSION['app_id'] = "".$_GET['capplication'];
}else{
	$_SESSION['app_id'] = "".$_POST['capplication'];
}

$sql_query = "SELECT ".DB_NAME.".APP.App_Id,".DB_NAME.".APP.Nom AS App_Name ,".DB_NAME.".APP.Host,".DB_NAME.".APP.Db,".DB_NAME.".APP_ACCESS.Role_Id, ".DB_NAME.".ROLE.Nom AS Role_Name FROM ".DB_NAME.".APP,".DB_NAME.".APP_ACCESS,".DB_NAME.".ROLE WHERE ".DB_NAME.".APP_ACCESS.App_Id='".$_SESSION['app_id']."' AND ".DB_NAME.".APP_ACCESS.User_Id=".$_SESSION['user_id']." AND ".DB_NAME.".APP_ACCESS.App_Id = ".DB_NAME.".APP.App_Id AND ".DB_NAME.".APP_ACCESS.Role_Id = ".DB_NAME.".ROLE.Role_Id";

$rapps = $db->db_query($sql_query);
$rapp = $db->db_fetch_object($rapps);

$_SESSION['app_id'] = $rapp->App_Id;
$_SESSION['app_name'] = $rapp->App_Name;
$_SESSION['role_id']= $rapp->Role_Id;
$_SESSION['role_name'] = $rapp->Role_Name;
$_SESSION['database']=$rapp->Db;
$_SESSION['host'] = $rapp->Host;
$_SESSION['current_db'] = $rapp->Db;

$smarty->assign('page',"".$rapp->Host."?application=".$_SESSION['app_id']);
$smarty->assign('nom',"".$rapp->Nom);

if($rapp->Host == ""){
	$smarty->assign('page',"/index.php");
}
$smarty->display('redirect.tpl');
?>
