<?php
include(dirname(__FILE__).'/../../../includes.php5');
header('Content-Type: text/html; charset=UTF-8');

include_once($project_path.'/class/user/user.class.php5');
session_start();
include_once($project_path.'/class/fw/dictionary.class.php5');

userUser::checkSession();

$action=(isset($_GET['action'])?$_GET['action']:'');
switch($action)
{
	case 'show':
		if(!$_SESSION['current_user']->isAnonymous())
		{
			$_SESSION['current_user']->displayJS();
			echo 'userPageDisplay(new Array("«HOME_PROFIL»", "«HOME_LOGOUT»", "«HOME_LOGIN»", "«HOME_REGISTER»"))';
		}
		else
		{
			//retreive the list of the best USERS.
			$users_knowned = userUser::getPreferedUsers();

			$datas = fwDbObject::getListFromFields('userUser', 's_token', $users_knowned);
			$tmp_users = array();
			foreach ($datas as $user)
			{
				$tmp_users[intval(array_search($user->token, $users_knowned))] = $user;
			}
			ksort($tmp_users);
			$users = array();
			$cpt=0;
			$first = null;
			foreach ($tmp_users as $index => $user)
			{
				if($user->id==0) continue;
				$user->loadPerson();
				if($first===null)
				{
					$first = $user->id;
				}
				$users[$index] = $user;
				if(++$cpt>=3)
				{
					break;
				}
			}

			include_once($project_path.'/class/fw/login/loginWll.class.php5');
			include_once($project_path.'/class/fw/login/loginYahoo.class.php5');

			if($first===null)
			{
				$first = 'other';
			}
			
			include_once($project_path.'/class/fw/smarty.class.php5');
			$smarty = new fwSmarty();

			if(isset($conf['login']['wll']))
			{
				$smarty->assign('login_wll_url', fwLoginWll::getLoginUrl());
			}
			if(isset($conf['login']['yahoo']))
			{
				$smarty->assign('login_yahoo_url', fwLoginYahoo::getLoginUrl());
			}
			$smarty->assign('users', $users);
			$smarty->assign('display_wll', isset($conf['login']['wll']));
			$smarty->assign('display_yahoo', isset($conf['login']['yahoo']));
			$smarty->assign('display_gmail', isset($conf['login']['gmail']));

			echo 'login_popup.show("'.fwConverter::text2JSString($smarty->getContents('user/login.tpl')).'");
login_last_selected_user = "'.$first.'";
loginInit();';
		}
		break;
	case 'login':
		$account=(isset($_POST['account'])?$_POST['account']:'');
		switch($account)
		{
			case 'gmail':
				include_once($project_path.'/class/fw/login/loginGmail.class.php5');
				list($responseOk, $login) = fwLoginGmail::processLogin($_POST);
				if(strpos($login, '@')===false)
				{
					$login.='@gmail.com';
				}
				if($responseOk)
				{
					userUser::login('gmail', $login, true, false, true);
					$_SESSION['current_user']->displayJS();
					echo 'login_popup.hide();
userConnectionRefreshLogin()';
				}
				else
				{
					echo 'loginError("«USER_LOGIN_ERROR»", null, true);';
				}
				break;
			case 'tt':
				include_once($project_path.'/class/fw/login/loginTt.class.php5');
				list($responseOk, $login) = fwLoginTt::processLogin(array('login' => (isset($_POST['login'])?$_POST['login']:''), 'password' => md5((isset($_POST['password'])?$_POST['password']:''))));
				if($responseOk)
				{
					userUser::login('tt', $login, true, true, true);
					if($_SESSION['current_user']->displayLogin != $_POST['login'])
					{
						$_SESSION['current_user']->loadPerson();
						$_SESSION['current_user']->displayLogin = $_POST['login'];
						if($_SESSION['current_user']->person==null)
						{
							$_SESSION['current_user']->person = new orgPerson();
						}
						$_SESSION['current_user']->person->name = $_POST['login'];
						$_SESSION['current_user']->person->save();
						$_SESSION['current_user']->save();
					}
					$_SESSION['current_user']->displayJS();
					echo 'login_popup.hide();
userConnectionRefreshLogin();';
				}
				else
				{
					echo 'loginError("«USER_LOGIN_ERROR»", null, true);';
				}
				break;
		}
		break;
	case 'logout':
		if($_SESSION['current_user']->authType=='wll')
		{
			include_once($project_path.'/class/fw/login/loginWll.class.php5');
			echo 'main_displayPopup("'.fwLoginWll::getLogoutUrl().'", "login_popup");';
		}
		else
		{
			userUser::logout();
			$_SESSION['current_user']->displayJS();
			echo 'userPageDisplay(new Array("«HOME_PROFIL»", "«HOME_LOGOUT»", "«HOME_LOGIN»", "«HOME_REGISTER»"));';
		}
		break;
}
?>