<?php
// no direct access
defined('_JEXEC') or die('Restricted access');
jimport('joomla.application.component.helper');

require_once(JPATH_ROOT.DS.'administrator'.DS.'components'.DS.'com_kip'.DS.'classes'.DS.'config.php');

define('ERROR_BANNED_IP', 1729);

class KipHelperIpfilter
{
	/*
	* Check is IP blocked for given client_id
	
    * @param  string $ip IP address in the format x.x.x.x
	* @param  int $client_id 0: frontend, 1: backend
    * @return boolean true if IP address blocked, otherwise false
	*/
	
	function isIpBlocked($ip, $client_id)
	{
		$site = $client_id ? 'admin' : 'site';
		$config = & KipConfig::getInstance();
		if(!$config->get($site.'.filtering_mode'))
			return false;

		$db= JFactory::getDBO();
		$db->setQuery('
			SELECT id 
			FROM #__kip_filters
			WHERE userid = 0
			AND published = 1
			AND client_id = '.$client_id.'
			AND '.ip2long($ip).' BETWEEN iplong_from AND iplong_to
			LIMIT 1
		');
		//echo $db->getQuery();

		return !((intval($config->get($site.'.filtering_mode'))-1) xor $db->loadResult());
	}
	
	/*
	* Check is IP blocked for given userid
	
    * @param  string $ip IP address in the format x.x.x.x
	* @param  int $userid
    * @return boolean true if IP address blocked, otherwise false
	*/
	
	function isIpBlockedForUser($ip, $userid)
	{
		$ipBlocked = true;
		$config = & KipConfig::getInstance();
		if(!$config->get('users.filtering_mode'))
			return false;
		
		$app = & JFactory::getApplication();
		$kip = $app->getUserState('session.kip');

		if($ip==$kip)
			return false;
		
		$db= JFactory::getDBO();
		$db->setQuery('
			SELECT id 
			FROM #__kip_filters
			WHERE userid = '.$userid.'
			AND published = 1
			LIMIT 1
		');
		if($db->loadResult())
		{
			$db->setQuery('
				SELECT id 
				FROM #__kip_filters
				WHERE userid = '.$userid.'
				AND '.ip2long($ip).' BETWEEN iplong_from AND iplong_to
				AND published = 1
				LIMIT 1
			');
			//echo $db->getQuery();
			if(!$db->loadResult())
				return true;
		}

		$app->setUserState('session.kip', $ip);
		return false;
	}
	
	function terminateApplication($client_id)
	{
		$config = & KipConfig::getInstance();
		$site = $client_id ? 'admin' : 'site';
		$action = $config->get($site.'.filtering_action');
		switch($action){
			case 1:
				$redirect_path = '';
			break;
			case 2:
				$redirect_path = JURI::base();
			break;
			case 3:
				$config =& JFactory::getConfig();
				$config->setValue('config.offline', 1);
				return;
			break;
			default:
				$redirect_path = $action;
		}
		$app = &JFactory::getApplication();
		$app->logout();

		$msg = JText::sprintf('IP blocked', $_SERVER['REMOTE_ADDR']);
		if(!empty($redirect_path))
		{
			$uri = &JURI::getInstance();
			if($redirect_path != $uri->toString())
				$app->redirect($redirect_path);
			elseif($config->get($site.'.filtering_warning'))
				JError::raiseWarning(ERROR_BANNED_IP, $msg);
		}else
		{
			if($config->get($site.'.filtering_warning'))
				echo $msg;
			$app->close();
		}
	}
	
	function logUserOut($userid, $client_id){
		$my =& JFactory::getUser();
		$app = & JFactory::getApplication();
		$options['clientid'][] = $client_id;
		$app->logout($userid, $options); 
		if($my->id == $userid)
		{
			$session =& JFactory::getSession();
			$session->restart();
			
			/* 
			Workaround for the bug in the implementation of database session storage. If JSession::destroy() followed by JSession::restart()
			is called, session data would not be saved correctly.
			*/
			jimport('joomla.database.table');
			$storage = & JTable::getInstance('session');
			$session->set('registry',	new JRegistry('session'));
			$session->set('user',		new JUser());
			$storage->insert( $session->getId(), $app->getClientId());
		}
	}
}
?>
