<?php
/**
 * File to handle the logout
 *
 * By clicking the logout link by the logged in user this file will clear
 * the session and redirect the user to the login page with success message.
 *
 * PHP version 5.0
 *
 * @category	###Anova###
 * @package		###Logout###
 * @author 		Karthiselvam_75ag08
 * @copyright	Copyright (c) 2009 {@link http://www.agriya.com Agriya Infoway}
 * @license		http://www.agriya.com Agriya Infoway Licence
 * @version		SVN: $Id:
 * @since 		2008-12-19
 */
/**
 * File having common configuration variables required for the entire project
 */
require_once('./common/configs/config.inc.php'); //configurations
if ($CFG['admin']['module']['facebook'])
	{
		if (version_compare(PHP_VERSION,'5','>=')) {
			require_once ('./facebook/client/facebook.php');
		} else {
		 	require_once ('./facebook/php4client/facebook.php');
		  	require_once ('./facebook/php4client/facebookapi_php4_restlib.php');
		}
	}
session_start();
if (isset($_SESSION['user']['user_id']))
	$user_id = $_SESSION['user']['user_id'];
// Unset all of the session variables.
$_SESSION = array();
// If it's desired to kill the session, also delete the session cookie.
// Note: This will destroy the session, and not just the session data!
if (isset($_COOKIE[session_name()]))
	{
	   setcookie(session_name(), '', time()-42000, '/');
	}
if (isset($_COOKIE[$CFG['cookie']['starting_text'].'_login']))
{
	setcookie($CFG['cookie']['starting_text'].'_login', '', time()+60*60*24*365, '/');
}
if (isset($_COOKIE['login']))
{
	setcookie('login', '', 0, '/');
}
// Finally, destroy the session.
session_destroy();
session_write_close();
setcookie($CFG['cookie']['starting_text'].'_bba', '', time()-42000, '/');
//$CFG['html']['is_use_header'] = true;
$CFG['html']['header'] = 'general/html_header.php';
//$CFG['html']['is_use_footer'] = true;
$CFG['html']['footer'] = 'general/html_footer.php';
//compulsory
$CFG['mods']['is_include_only']['html_header'] = false;
$CFG['html']['is_use_header'] = false;
/**
 * To include application top file
 */
require($CFG['site']['project_path'].'common/application_top.inc.php');
class LogoutFormHandler extends FormHandler
	{
		/**
		 * To update user logout information
		 * @param 		string $table_name table name
		 * @param 		string $ip ip address
		 * @param 		string $session_id session id
		 * @return 		void
		 * @access 		public
		 */
	 function updateUserLogOut($table_name, $user_id)
		{
			$sql = 'UPDATE '.$table_name .
				' SET '.
				' is_logged_in = \'No\','.
				' last_active = NOW()'.
				' WHERE '.
				' user_id= '.$user_id.'';
			// prepare sql
			$stmt = $this->dbObj->Prepare($sql);
			// execute sql
			$rs = $this->dbObj->Execute($stmt);
			//raise user error... fatal
			if (!$rs)
				trigger_db_error($this->dbObj);
		}
	}
$logoutfrm = new LogoutFormHandler();
if(isset($user_id))
	$logoutfrm->updateUserLogOut($CFG['db']['tbl']['users'], $user_id);

if ($logoutfrm->isFormGETed($_GET, 'inv'))
	Redirect2Url(getUrl('login', '?logout&inv', '?logout&inv', 'root'));
else
	Redirect2Url(getUrl('login', '?logout', '?logout', 'root'));

if (isset($CFG['admin']['module']['facebook']) and $CFG['admin']['module']['facebook'])
	{
		setcookie($CFG['facebook']['api_key'],'',time()-42000, '/');
		setcookie($CFG['facebook']['api_key'].'_'.'user','',time()-42000, '/');
		setcookie($CFG['facebook']['api_key'].'_'.'session_key','',time()-42000, '/');
		setcookie($CFG['facebook']['api_key'].'_'.'expires','',time()-42000, '/');
		setcookie($CFG['facebook']['api_key'].'_'.'ss','',time()-42000, '/');
		setcookie('fbsetting'.'_'.$CFG['facebook']['api_key'],'',time()-42000, '/');
	}

if (isset($CFG['admin']['module']['openid']) and $CFG['admin']['module']['openid'])
	{
		setcookie('__openid_selector_openid');
		setcookie('__openid_selector_op_id');
		setcookie('__openid_selector_uname');
	}

Redirect2Url(getUrl('login', '?logout', '?logout', 'root'));
//<<<<--------------------Code Ends----------------------//
//--------------------Page block templates begins-------------------->>>>>//
//includ the footer of the page
//<<<<<<--------------------Page block templates Ends--------------------//
require_once($CFG['site']['project_path'].'common/application_bottom.inc.php');
?>