<?
########## Confixx(R) 3.0 Professional ############
####### Copyright SWsoft, Inc. 2004-2005 ##########
#### http://www.sw-soft.com - info@sw-soft.com ####

/*
  -------------------- Name conventions --------------------
  Function names (except methods):

  ff_DescriptionOfFunction


  Constant names:

  cc_DescriptionOfConstant


  Variable names:

  $ST_DescriptionOfVariable

  where S is a scope, T is a type.

  The following scopes should be used:
  g - global
  s - static
  l - local
  m - member of class
  p - parameter

  The following types should be used:
  s - scalar
  a - array
  r - resource
  o - object
  m - mixed

  -------------------- end Name conventions --------------------
*/





include_once ( dirname(__FILE__) . DIRECTORY_SEPARATOR . ".." . DIRECTORY_SEPARATOR . "class.Pager.php" );
include_once ( dirname(__FILE__) . DIRECTORY_SEPARATOR . ".." . DIRECTORY_SEPARATOR . "class.InputTextColumn.php" );
include_once ( dirname(__FILE__) . DIRECTORY_SEPARATOR . ".." . DIRECTORY_SEPARATOR . "class.InputCheckboxColumn.php" );
include_once ( dirname(__FILE__) . DIRECTORY_SEPARATOR . ".." . DIRECTORY_SEPARATOR . "class.PostValuesSource.php" );





class IPRestrictionPager extends Pager
{
    /* -------------------- Members -------------------- */
    /* -------------------- end Members -------------------- */





    /* -------------------- Constructor -------------------- */
    function IPRestrictionPager ( &$po_Source )
    {
	$this->Pager ( $po_Source );
	$this->setText ( "noresults", ltext("ipr_norestrictions") );
    }
    /* -------------------- end Constructor -------------------- */





    /* -------------------- definitions of "abstract" methods -------------------- */
    function ShouldShowSummary()
    {
	return FALSE;
    }

    function ShouldShowAddForm()
    {
	return TRUE;
    }

    function GetSummary()
    {
	return "";
    }

    function GetAddColumns()
    {
	return array( new InputTextColumn( "ip", ltext("ipr_ipaddress"), "", array( "style"=>"width: 100%" ), "80%" ),
		new InputCheckboxColumn( "accept", "rest", ltext("ipr_accept"), "accept", array(), "10%" ) );
    }

    function GetBrowseColumns()
    {
	$la_Result = array( new InputTextColumn( "ip", ltext("ipr_ipaddress"), "", array( "style"=>"width: 100%" ), "80%" ),
		new InputCheckboxColumn( "accept", "rest", ltext("ipr_accept"), "", array(), "10%" ) );
	$la_Result[0]->SetSortable (TRUE);
	$la_Result[1]->SetSortable (TRUE);
	return $la_Result;
    }

    function GetSearchColumns()
    {
	return array(
		new InputTextColumn( "ipsearch", ltext("ipr_ipaddress"), "", array( "style"=>"width: 100%" ), "90%" ) );
    }

    function GetHiddenFields()
    {
	return array();
    }
    /* -------------------- end definitions of "abstract" methods -------------------- */





    /* -------------------- AddElement & related -------------------- */
    function AddElement()
    {
	if ( $this->MyIP( $_POST["ip"] ) && ( !array_key_exists("rest",$_POST) || $_POST["rest"] != "accept" )
		&& ( !array_key_exists("force",$_POST) || $_POST["force"]!="Y" ) )
	{
	    $this->addMessage ( WARN, ltext( "ipr_yourip", array($_POST["ip"]) ) );
	    $this->show ( "admin", "iprestriction", "confirm_add" );
	    exit;
	}
	Pager::AddElement();
    }

    function MyIP( $ps_IP )
    {
	$ls_Pattern = str_replace ( ".", "\.", $ps_IP );
	$ls_Pattern = str_replace ( "*", ".+", $ls_Pattern );
	return preg_match ( "/^$ls_Pattern$/", $_SERVER["REMOTE_ADDR"] );
    }
    /* -------------------- end AddElement & related -------------------- */





    /* -------------------- EditElements & related -------------------- */
    function EditElements()
    {
	$ls_Matching = "";
	$ls_Matches = 0;
	$this->mo_Source->LockEdit();
	if ( ! array_key_exists ( "force", $_POST ) || $_POST["force"] != "Y" )
	    foreach ( $_POST["ip"] as $ls_ID => $ls_IP )
		if ( $this->MyIP( $ls_IP ) && ( !array_key_exists("rest",$_POST) ||
			!array_key_exists($ls_ID,$_POST["rest"]) || $_POST["rest"][$ls_ID] != "accept" ) &&
			$this->mo_Source->NewDeny($ls_ID,$ls_IP) )
		{
		    $ls_Matching .= ", $ls_IP";
		    $ls_Matches++;
		}

	if ( $ls_Matches )
	{
	    $this->mo_Source->UnLock();
	    $ls_Matching = substr( $ls_Matching, 2 );
	    $this->mo_Source = $this->GetPostSource ( $this->GetBrowseColumns() );
	    $this->addMessage ( WARN, ltext( $ls_Matches==1 ? "ipr_yourip" : "ipr_yourips", array($ls_Matching) ) );
	    $this->show ( "admin", "iprestriction", "confirm_edit" );
	    exit;
	}
	Pager::EditElements (FALSE);
    }
    /* -------------------- end EditElements & related -------------------- */

}
?>
