<?php

/************************************************************************/
/* DOCEBO CORE - Framework												*/
/* ============================================							*/
/*																		*/
/* Copyright (c) 2004													*/
/* http://www.docebo.com												*/
/*																		*/
/* 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.		*/
/************************************************************************/

if(!defined('IN_DOCEBO')) die('You cannot access this file directly');

//check input
chkInput($_GET);
chkInput($_POST);
chkInput($_COOKIE);

if(!internalFirewall())
	die('Your ip address is not allowed on this site !');

$GLOBALS['modname'] = importVar('modname');
$GLOBALS['op'] = importVar('op');

if( !isset($_GET['no_redirect']) && !isset($_POST['no_redirect']) ) {
	if( ( ($GLOBALS['op'] == '') || ($GLOBALS['op'] == 'logout') || ($GLOBALS['op'] != 'confirm' ) ) && $GLOBALS['current_user']->isAnonymous()) {
		//$GLOBALS['op'] 		= 'login';
		//$GLOBALS['modname'] = 'login';
	}
}

// ip control
if(isset($_GLOBALS['framework']['session_ip_control']) && $_GLOBALS['framework']['session_ip_control'] == 'on') {

	if($GLOBALS['current_user']->isLoggedIn() && ($GLOBALS['current_user']->getLogIp() != $_SERVER['REMOTE_ADDR'])) {
		echo "logip: ".$GLOBALS['current_user']->getLogIp()."\n";
		echo "addr: ".$_SERVER['REMOTE_ADDR']."\n";
		die('Ip incoerent!');
	}
}
// NOTE: some special function
switch($GLOBALS['op']) {
	case "change_main" : {

		//change form a menu voice to another
		//$_SESSION['current_admin_id_menu'] = importVar('new_main');
		//$_SESSION['current_action_platform'] = importVar('of_platform');
	};break;
	case "confirm" : {

		if($GLOBALS['modname'] == 'login') {

			require_once($GLOBALS['where_framework'].'/lib/lib.usermanager.php');
			$manager = new UserManager();
			$login_data = $manager->getLoginInfo();
			$manager->saveUserLoginData();

			$GLOBALS['current_user'] =& DoceboUser::createDoceboUserFromLogin( 	$login_data['userid'],
																					$login_data['password'],
																					'crm',
																					$login_data['lang'] );

			if( $GLOBALS['current_user'] === FALSE ) {
				$GLOBALS['current_user'] =& DoceboUser::createDoceboUserFromSession('crm');
				$GLOBALS['access_fail'] = true;
				//$GLOBALS['op'] = 'login';
			} else {
				//echo $GLOBALS['current_user']->userid;
				$GLOBALS['modname'] = '';
				$GLOBALS['op'] = '';

				//loading related ST
				$GLOBALS['current_user']->loadUserSectionST('/crm/');
				$GLOBALS['current_user']->SaveInSession();
				$GLOBALS['just_login'] = true;
			}
		}
	};break;
	case "logout" : {

		$_SESSION = array();
		session_destroy();

 		// Recreate Anonymous user
 		$GLOBALS['current_user'] =& DoceboUser::createDoceboUserFromSession('crm');

		//$GLOBALS['op'] 		= 'login';
		//$GLOBALS['modname'] = 'login';
		$GLOBALS['logout'] 	= true;

	};break;
	case "config" : {
		if(isset($_POST['option']['defaultLanguage'])) {

			//setLanguage($_POST['option']['defaultLanguage']);
		}
		if(isset($_POST['option']['defaultTemplate'])) {

			setTemplate($_POST['option']['defaultTemplate']);
		}
		if(isset($_POST['option']['layout'])) {

			//setLayout($_POST['option']['layout']);
			$GLOBALS['layout'] = $_POST['option']['layout'];
		}
	};break;
}

?>
