<?php

/******************************************************************************
 *
 *	PROJECT: Flynax Classifieds Software
 *	VERSION: 4.0
 *	LISENSE: http://www.flynax.com/license-agreement.html
 *	PRODUCT: General Classifieds
 *	
 *	FILE: RLAJAXADMIN.CLASS.PHP
 *
 *	This script is a commercial software and any kind of using it must be 
 *	coordinate with Flynax Owners Team and be agree to Flynax License Agreement
 *
 *	This block may not be removed from this file or any other files with out 
 *	permission of Flynax respective owners.
 *
 *	Copyrights Flynax Classifieds Software | 2012
 *	http://www.flynax.com/
 *
 ******************************************************************************/

class rlAjaxAdmin extends reefless
{
	/**
	* @var language class object
	**/
	var $rlLang;
	
	/**
	* @var validator class object
	**/
	var $rlValid;
	
	/**
	* @var configurations class object
	**/
	var $rlConfig;
	
	/**
	* @var administrator controller class object
	**/
	var $rlAdmin;
	
	/**
	* class constructor
	**/
	function rlAjaxAdmin()
	{
		global $rlLang, $rlValid, $rlConfig, $rlAdmin;
		
		$this -> rlLang   = & $rlLang;
		$this -> rlValid  = & $rlValid;
		$this -> rlConfig = & $rlConfig;
		$this -> rlAdmin = & $rlAdmin;
	}
	
	/**
	* check admin panel logining
	*
	* @package ajax
	*
	* @param mixed $user - admin username
	* @param MD5 $pass - admin user password in HEX format
	* @param varchar $lang - language inerface
	*
	**/
	function ajaxLogIn( $user = null, $pass = null, $language = null )
	{
		global $_response, $config, $lang;
		
		$_response -> setCharacterEncoding('UTF-8');
		
		$sec_key = md5($config['security_key']);
		$user = $this -> rlValid -> xSql( $user );
		$user_info = $this -> fetch( '*', array('User' => $user, 'Status' => 'active'), null, null, 'admins', 'row' );

		$real_password = $sec_key.$user_info['Pass'];

		if ( $pass == $real_password)
		{
			$this -> rlAdmin -> LogIn( $user_info );
			
			$query_string = $_SESSION['query_string'] ? '?' . $_SESSION['query_string'] : '';
			$pos = strpos($_SESSION['query_string'], 'session_expired');
			
			if ( $pos !== false )
			{
				$query_string = '?'. substr($_SESSION['query_string'], 0, $pos);
			}
			
			$query_string = $query_string ? $query_string .'&language=' .$language : '?language=' .$language;
			$_response -> redirect( RL_URL_HOME . ADMIN . '/index.php' . $query_string );
		}
		else 
		{
			//set message
			$_response -> script( "fail_alert('#login_notify', '{$lang['rl_logging_error']}')" );
			
			//hide loading
			$_response -> script( "$('#login_button').val('{$lang['login']}')" );
		}
		
		return $_response;
	}
	
	/**
	* administrator log out
	*
	* @package ajax
	* 
	**/
	function ajaxLogOut( $user = null, $pass = null, $lang = null )
	{
		global $_response;
		
		// check admin session expire
		if ( $this -> checkSessionExpire() === false )
		{
			$redirect_url = RL_URL_HOME . ADMIN ."/index.php";
			$redirect_url .= empty($_SERVER['QUERY_STRING']) ? '?session_expired' : '?'. $_SERVER['QUERY_STRING'] .'&session_expired';
			$_response -> redirect( $redirect_url );
		}
		
		$this -> rlAdmin -> LogOut( $user_info );
		$_response -> redirect( RL_URL_HOME . ADMIN . '/' );

		return $_response;
	}
	
	
}