#!/usr/bin/perl 

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

$OBJECT_RESELLER        =1;
$OBJECT_USER		=2;
$OBJECT_DOMAIN		=3;
$OBJECT_MLLIST		=4;
$OBJECT_POP3		=5;
$OBJECT_IP		=6;
$OBJECT_IPRULE		=7;

$OBJECT_MAILTRAFFIC     =8;
$OBJECT_IPTRAFFIC 	=9;
$OBJECT_WEBTRAFFIC 	=10;
$OBJECT_FTPTRAFFIC 	=11;

$OBJECT_LIMITALERT 	=12;
$OBJECT_SERVER		=13;
$OBJECT_PRIMARY		=14;
$OBJECT_SECONDARY	=15;

$EVENT_NEW		=106;
$EVENT_CHANGE		=107;
$EVENT_DELETE		=108;

$EVENT_MLNEW		=109;
$EVENT_MLCHANGE		=110;
$EVENT_MLDELETE		=111;

$EVENT_GO		=112;

$MAX_TYPE_ID = 112;

sub install_types {
  my $dbh = shift;
  unless( ref($dbh)  ){
	return 0;
  }
  $dbh->do("delete from types");

  $dbh->do("insert into types (id,grp,code) values ($OBJECT_RESELLER,'OBJECT','RESELLER')");
  $dbh->do("insert into types (id,grp,code) values ($OBJECT_USER,'OBJECT','USER')");
  $dbh->do("insert into types (id,grp,code) values ($OBJECT_DOMAIN,'OBJECT','DOMAIN')");
  $dbh->do("insert into types (id,grp,code) values ($OBJECT_MLLIST,'OBJECT','MLLIST')");
  $dbh->do("insert into types (id,grp,code) values ($OBJECT_POP3,'OBJECT','POP3')");
  $dbh->do("insert into types (id,grp,code) values ($OBJECT_IP,'OBJECT','IP')");
  $dbh->do("insert into types (id,grp,code) values ($OBJECT_IPRULE,'OBJECT','IPRULE')");
  $dbh->do("insert into types (id,grp,code) values ($OBJECT_MAILTRAFFIC,'OBJECT','MAILTRAFFIC')");
  $dbh->do("insert into types (id,grp,code) values ($OBJECT_IPTRAFFIC,'OBJECT','IPTRAFFIC')");
  $dbh->do("insert into types (id,grp,code) values ($OBJECT_WEBTRAFFIC,'OBJECT','WEBTRAFFIC')");
  $dbh->do("insert into types (id,grp,code) values ($OBJECT_FTPTRAFFIC,'OBJECT','FTPTRAFFIC')");
  $dbh->do("insert into types (id,grp,code) values ($OBJECT_LIMITALERT,'OBJECT','LIMITALERT')");

  $dbh->do("insert into types (id,grp,code) values ($OBJECT_SERVER,'OBJECT','SERVER')");
  $dbh->do("insert into types (id,grp,code) values ($OBJECT_PRIMARY,'OBJECT','PRIMARY')");
  $dbh->do("insert into types (id,grp,code) values ($OBJECT_SECONDARY,'OBJECT','SECONDARY')");
  
  
  $dbh->do("insert into types (id,grp,code) values ($EVENT_NEW,'EVENT','NEW')");
  $dbh->do("insert into types (id,grp,code) values ($EVENT_CHANGE,'EVENT','CHANGE')");
  $dbh->do("insert into types (id,grp,code) values ($EVENT_DELETE,'EVENT','DELETE')");

  $dbh->do("insert into types (id,grp,code) values ($EVENT_MLNEW,'EVENT','MLNEW')");
  $dbh->do("insert into types (id,grp,code) values ($EVENT_MLCHANGE,'EVENT','MLCHANGE')");
  $dbh->do("insert into types (id,grp,code) values ($EVENT_MLDELETE,'EVENT','MLDELETE')");

  $dbh->do("insert into types (id,grp,code) values ($EVENT_GO,'EVENT','GO')");


  if($dbType eq "psql"){
    $dbh->do("select setval ('\"types_id_seq\"',$MAX_TYPE_ID,'t')");
  }


  return $MAX_TYPE_ID;
}

sub set_event {
  my $dbh = shift;
  
  my %args = ( 'dbh' => undef,
	'obj_type' => undef,
	'obj_id' => undef,
	'obj_prop' => undef,
	'event_type' => undef,
	'event' => undef,
	'event_prop' => undef,
	@_   );
  
  $dbh=$args{'dbh'} if exists($args{'dbh'});
  unless( ref($dbh)  ){
	return 0;
  }
  
  $obj_type=$args{'obj_type'};
  $event_type=$args{'event_type'};
  $obj_id=$args{'obj_id'};
  $obj_prop=$args{'obj_prop'};
  $event=$args{'event'};
  $event_prop=$args{'event_prop'};
  
  unless(defined($obj_type) && defined($event_type)
	     && ( defined($obj_id)||defined($obj_prop) ) 
		 && (defined($event)||defined($event_prop) )
  ){
	return 0;  
  }
  my $where="object_type=$obj_type and event_type=$event_type and ";
  if (defined($obj_id)){
	$where.=" object_id=$obj_id";
  }else{
	$where.=" object_prop='$obj_prop'";
  }
  my $sth=$dbh->prepare("select id from events where $where");
  $sth->execute;
  my $sql;
  if ($sth->rows){
	$sql = "update events set ";
	if  (defined($event) ){
	  $sql.="event=$event,";
	}
	if  (defined($event_prop) ){
	  $sql.=" event_prop='$event_prop',";
	}
	$sql=~s/,$//;
	$sql.=" where $where";
  }else{
	$sql = "insert into events (object_type, event_type";
	if (defined($obj_id)){
	  $sql.=", object_id";
	}else{
	  $sql.=", object_prop";
	}
	if (defined($event)){
	  $sql.=", event";
	}
	if (defined($event_prop)){
	  $sql.=", event_prop";
	}
	$sql.=") values ($obj_type,$event_type";
	if (defined($obj_id)){
	  $sql.=", $obj_id";
	}else{
	  $sql.=", '$obj_prop'";
	}
	if (defined($event)){
	  $sql.=", $event";
	}
	if (defined($event_prop)){
	  $sql.=", '$event_prop'";
	}
	$sql.=")";
  }
  $sth->finish;
  $dbh->do($sql);
  return 1;
}

sub eventGet {
  my $myDbh = shift;
  unless (ref($myDbh)=~/DBI/){
    unshift @_,$myDbh;
    $myDbh = $::dbh; 
  }
  my %args = ( 'dbh' => undef,
	'object_type' => undef,
	'object_id' => undef,
	'object_prop' =>undef,
	'event_type' => undef,
	'event' => undef,
	'event_prop' => undef,
	'count'=>undef,
	@_   );

  $myDbh=$args{'dbh'} if (ref($args{'dbh'})=~/DBI/);
  unless( ref($myDbh)=~/DBI/  ){
    return 0;
  }
  
  $obj_type=$args{'object_type'}||$args{'obj_type'};
  $event_type=$args{'event_type'};
  $obj_id=$args{'object_id'}||$args{'obj_id'};
  $obj_prop=defined($args{'object_prop'})?$args{'object_prop'}:$args{'obj_prop'};
  $event=$args{'event'};
  $event_prop=$args{'event_prop'};

  unless(defined($obj_type) && defined($event_type)){
    return 0;
  }

  my $where="(object_type=$obj_type) and (event_type=$event_type)";
  if (defined($obj_id)){
    $where.=" and (object_id=$obj_id)";
  }elsif(defined($obj_prop)){
    $where.=" and (object_prop='$obj_prop')";
  }
  my $sth=$myDbh->prepare("select * from events where $where");
  $sth->execute;
  my ($ret);
  if ($sth->rows){
    $ret=$sth->fetchrow_hashref;
  }
  $sth->finish;
  return $ret;
}

sub eventAdd {
  my $myDbh = shift;
  unless (ref($myDbh)=~/DBI/){
    unshift @_,$myDbh;
    $myDbh = $::dbh; 
  }
  my %args = ( 'dbh' => undef,
	'object_type' => undef,
	'object_id' => undef,
	'object_prop' =>'',
	'event_type' => undef,
	'event' => undef,
	'event_prop' => undef,
	'count'=>undef,       
	@_   );
  
  $myDbh=$args{'dbh'} if (ref($args{'dbh'})=~/DBI/);
  unless( ref($myDbh)=~/DBI/  ){
    return 0;
  }
  
  $obj_type=$args{'object_type'}||$args{'obj_type'};
  $event_type=$args{'event_type'};
  $obj_id=$args{'object_id'}||$args{'obj_id'};
  $obj_prop=defined($args{'object_prop'})?$args{'object_prop'}:$args{'obj_prop'};
  $event=$args{'event'};
  $event_prop=$args{'event_prop'};
  $count=$args{'count'};
  
  unless(defined($obj_type) && defined($event_type)
	     && ( defined($obj_id)||defined($obj_prop) ) 
##		 && (defined($event)||defined($event_prop) )
  ){
    return 0;
  }
  my $where="object_type=$obj_type and event_type=$event_type and ";
  if (defined($obj_id)){
	$where.=" object_id=$obj_id";
  }else{
	$where.=" object_prop='$obj_prop'";
  }
  my $sth=$myDbh->prepare("select id from events where $where");
  $sth->execute;
  my ($sql,$id);
  if ($sth->rows){
    ($id)=$sth->fetchrow;
    $where = "id=$id";
    if (defined($count)){
      $sql = "update events set count=$count";
    }else{
      $sql = "update events set count=count+1";
    }
    if  (defined($event) ){
      $sql.=",event=$event";
    }
    if  (defined($event_prop) ){
      $sql.=",event_prop='$event_prop'";
    }

    $sql.=" where $where";
  }else{
    $sql = "insert into events (object_type, event_type";
    if (defined($obj_id)){
      $sql.=", object_id";
    }else{
      $sql.=", object_prop";
    }
    if (defined($event)){
      $sql.=", event";
    }
    if (defined($event_prop)){
      $sql.=", event_prop";
    }
    if (defined($count)){
      $sql.=", count";
    }
    $sql.=") values ($obj_type,$event_type";
    if (defined($obj_id)){
      $sql.=", $obj_id";
    }else{
      $sql.=", '$obj_prop'";
    }
    if (defined($event)){
      $sql.=", $event";
    }
    if (defined($event_prop)){
      $sql.=", '$event_prop'";
    }
    if (defined($count)){
      $sql.=", $count";
    }
    $sql.=")";
  }
  $sth->finish;
  if ($myDbh->do($sql)){
    return 1;
  }esle{
    print STDERR "Error sql-request: $sql\nDBI::errstr\n";
    return 0;
  }
}

sub getEventsCount {
  my ($objType,$myDbh);

  $myDbh=shift;
  if(ref($myDbh)=~/DBI/){
    $objType =shift;
  }else{
    $objType =$myDbh;
    $myDbh=shift||$::dbh;
  }
  my $eventType = shift;
  return undef unless ($objType=~/^\d+$/);
  my $sql = "select e.id,e.count from events e where (object_type=$objType) and (e.count>0)";
  if ($eventType=~/^\d+$/){
    $sql.=" and (event_type=$eventType)";
  }
  my $sth=$myDbh->prepare($sql);
  $sth->execute  
    or return undef;
  my ($id,$count,%ret);
  while (($id,$count)=$sth->fetchrow){
    $ret{$id}=$count;
  }
  $sth->finish;
  return \%ret;
}

sub cleanEvents {
 
  my ($ptrCounts,$myDbh);

  $myDbh=shift;
  if(ref($myDbh)=~/DBI/){
    $ptrCounts =shift;
  }else{
    $ptrCounts =$myDbh;
    $myDbh=shift||$::dbh;
  }

#
# check agruments
#
  return 0 unless ((ref($ptrCounts)=~/HASH/) && (ref($myDbh)=~/DBI/));

  my ($id,$count,$sql);
  while (($id,$count)=each(%{$ptrCounts})){
    next unless ($id=~/^\d+$/);
    if($count=~/^\d+$/){
      $sql ="update events set count=count-$count where id=$id";
    }else{
      $sql ="update events set count=0 where id=$id";
    }
    $myDbh->do($sql) 
      or print STDERR "Error sql-request: $sql\nDBI::errstr\n";
  }
}

1;
