<?php
// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die( 'Restricted access' );

class TableRules extends JTable
{
	var $id							= 0;
	var $iprange_txt				= '';
	var $iplong_from				= 0;
	var $iplong_to					= 0;
	var $description				= '';
	var $client_id					= 0;
	var $userid						= 0;
	var $published					= 0;
	var $checked_out				= 0;
	var $checked_out_time			= 0;
	var $created					= 0;
	var $created_by					= 0;
	var $modified					= 0;
	var $modified_by				= 0;
	
	function __construct( &$db )
	{
		parent::__construct( '#__kip_filters', 'id', $db );
	}

	function delete( $oid=null )
	{
		$k = $this->_tbl_key;
		if ( $oid ) {
			$this->$k = intval( $oid );
		}
		//$this->_db->setQuery('DELETE FROM #__kiodict_relations WHERE termid='.$this->$k);
		//$this->_db->query();
			
		parent::delete( $oid );
		return true;
	}

	function store( $oid=null )
	{
		$k = $this->_tbl_key;
		if ( $oid )
			$this->$k = intval( $oid );

		return parent::store( $oid );
	}

	function check()
	{
		require_once(JPATH_ROOT.DS.'administrator'.DS.'components'.DS.'com_kip'.DS.'classes'.DS.'netipv4.php');
		$ipv4 = new Net_IPv4();
		if(strpos($this->iprange_txt,'-')!=false){
			$ips = explode('-', $this->iprange_txt);
			$ips[0] = $ipv4->parseAddress(trim($ips[0]));
			$ips[1] = $ipv4->parseAddress(trim($ips[1]));
			if(JError::isError($ips[0]) || JError::isError($ips[1]))
				return false;
			$this->iplong_from = ip2long($ips[0]->ip);
			$this->iplong_to = ip2long($ips[1]->ip);
		}else{
			$ip = $ipv4->parseAddress($this->iprange_txt);
			//print_r($ip);exit;
			if(JError::isError($ip))
				return false;
			if($ip->network){
				$this->iplong_from = ip2long($ip->network);
				$this->iplong_to = ip2long($ip->broadcast);
			}else{
				$this->iplong_from = $this->iplong_to = ip2long($ip->ip);
			}
		}
		//print_r($ip);exit;
		
		/*$this->title = trim($this->title);
		if(empty($this->title)) {
			$this->setError(JText::_('Item must have a title'));
			return false;
		}
			
		$this->_db->setQuery('SELECT id FROM #__kiodict_terms WHERE id<> '.intval($this->id).' AND  title = '.$this->_db->Quote($this->title));
		if($this->_db->loadResult()){
			$this->setError(JText::_('Item alredy exists'));
			return false;
		}
		
		if(empty($this->alias))
			$this->alias = $this->title;
		
		$this->alias = JFilterOutput::stringURLSafe($this->alias);

		if(trim(str_replace('-','',$this->alias)) == ''){
			$datenow =& JFactory::getDate();
			$this->alias = $datenow->toFormat("%Y-%m-%d-%H-%M-%S");
		}

		if(empty($this->letter))
			$this->letter = $this->title;
		if(function_exists('mb_substr'))
			$this->letter = mb_strtoupper(mb_substr($this->letter, 0, 1));
		else
			$this->letter = strtoupper(substr($this->letter, 0, 1));
		
		*/
		$datenow =& JFactory::getDate();
		$user =& JFactory::getUser();
		
		if(!$this->id){
			$this->created 		= $datenow->toMySQL();
			$this->created_by 	= $user->get('id');
		}else{
			$this->modified 		= $datenow->toMySQL();
			$this->modified_by 	= $user->get('id');
		}
		
		return true;
	}
	
}
