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

include_once("auth.php");
include("email_functions.php");
include("email_config.php");
include("email_header.php");
include_once( "../include/class.SQLString.php" );
include_once( "../include/class.SQLInteger.php" );
include_once( "../include/class.SQLFieldSet.php" );

$PHP_SELF = 'email_adlist.php';
//$res = ;
$loKunde = new SQLString($kunde);
$spam_row = db_fetch_array( safe_query2( "SELECT spamfilter FROM kunden WHERE kunde=?", $loKunde ) );
$loCnf = new SQLString( $cnf_user );
if( $spam_row["spamfilter"] && $whitelist_save ) {
	$auto_whitelist *=1;
	safe_query2( "UPDATE pop3 SET auto_whitelist=$auto_whitelist WHERE account=?", $loCnf );
}
if( $spam_row["spamfilter"] ) {
	$res = safe_query2( "SELECT auto_whitelist FROM pop3 WHERE account=?", $loCnf );
	$row = db_fetch_array($res);
	$auto_whitelist_db = $row["auto_whitelist"];
}

if($editID == "" && $editform && ($name == "" || $email == "")) {
	$editID = $editform;
?>	
	<hr noshade align="left">
	<table>
	<tr>
		<td class="inform_title"><?echo ltext('errtype_error');?></td>
		<td class="inform_message" width="100%"><? echo ltext('wbm_ad_emptyrecord', array('"'.ltext('wbm_ad_form_name').'"', '"'.ltext('wbm_ad_email').'"') );?></td>	
	</tr>
	</table>
	<hr noshade align="left">
<?
																																																		}

// ADRESS-LIST:
if( $editID == "" ) { 
?>
  <table class="tblbgcolor" align="left" border="1" cellpadding="3" cellspacing="0" bordercolorlight="#CCCCCC" bordercolordark="#FFFFFF">
  <tr><td>
<?
  if($editform) {
  	
  	if( $auto_whitelist_db ) {
	  	$emailaddress = $email;
			$error=-1;
			$preference = "whitelist_from";
			$username = $cnf_user;
			if(!$emailaddress) {
				$error++;
			}
			if($emailaddress &&
				 ( 
					(!ereg("^[a-zA-Z0-9_\.\-]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$",  $emailaddress)) &&
					(!ereg("^\*@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$",  $emailaddress)) ) ) {
				$error++;
			}
	
			$loName = new SQLString($username);
			$loEmail = new SQLString($emailaddress);
			$cmd = "SELECT * FROM spampref WHERE username = ? AND value = ?";
			$result = safe_query2( $cmd, $loName, $loEmail );
			$num_rows = db_num_rows($result);
			if($num_rows) {
				safe_query2( "DELETE FROM spampref WHERE username = ? AND value = ?", $loName, $loEmail );
			}
	
			if($error < 0){
				$cmd = "INSERT INTO spampref (preference, value, username) VALUES ( ?,?,?)";
				$result= safe_query2( $cmd, new SQLString($preference), $loEmail, $loName );
			//echo db_error($result);
			}
  	}

		$loFields = new SQLFieldSet( new SQLField( 'pop3', $cnf_PHP_AUTH_USER, 'C'),
																 new SQLField( 'name', $name, 'C' ),
																 new SQLField( 'email', $email, 'C' ),
																 new SQLField( 'first_name', $first_name, 'C' ),
																 new SQLField( 'last_name', $last_name, 'C' ),
																 new SQLField( 'tel_home', $tel_home, 'C' ),
																 new SQLField( 'tel_work', $tel_work, 'C' ),
																 new SQLField( 'tel_mobile', $tel_mobile, 'C' ),
																 new SQLField( 'tel_fax', $tel_fax, 'C' ),
																 new SQLField( 'address', $address, 'C' )
																 );

    if( $editform == "new" ) {
			list( $lcFlds, $lcVals ) = $loFields->getSQLInsert();

			db_query( "INSERT INTO $cnf_db_ab ($lcFlds) VALUES ($lcVals)");
      echo $lang_text["wbm_ad_newentry"]."<br>&nbsp;<br>";

    } else {
			$loFields->shiftField();
			$lcUpdate = $loFields->getSQLUpdate();
 
			safe_query2( "UPDATE $cnf_db_ab SET $lcUpdate WHERE ident = ? AND pop3 = ?", 
									  new SQLInteger( $editform ),  new SQLString( $cnf_PHP_AUTH_USER ) );
      echo $lang_text["wbm_ad_editentry"]."<br>&nbsp;<br>";
    }
  }

	$loCnfUser = new SQLString( $cnf_PHP_AUTH_USER );
  if( $delID ) {
		$loID = new SQLInteger( $delID );
		$check = db_fetch_array(safe_query2( "SELECT pop3 FROM $cnf_db_ab WHERE ident = ?", $loID ));
		if ( $check["pop3"] == $cnf_PHP_AUTH_USER) {
  	  echo $lang_text["wbm_ad_deletemsg"]."<br>&nbsp;<br>";
			safe_query2( "DELETE FROM $cnf_db_ab WHERE ident = ? AND pop3 = ?",  $loID, $loCnfUser );
		} else {
			echo $lang_text["wbm_ad_deletemsg_insecure"]."<br>&nbsp;<br>";
		}  
  }

  $query_adlist = safe_query( "SELECT * FROM $cnf_db_ab WHERE pop3 = ? ORDER BY name", $loCnfUser );

  $adlist['counter'] = mysql_num_rows($query_adlist);

  if ($adlist['counter'] == 0) {
    echo $lang_text["wbm_ad_status0"];
  } else {
    if($adlist['counter'] == 1) {
      echo $lang_text["wbm_ad_status1"];
    } else {
      echo str_replace("%numAdress%",$adlist['counter'],$lang_text["wbm_ad_status2"]);
    }
  }
?>
  <table class="tblbgcolor" width="100%" border="1" cellpadding="3" cellspacing="0" bordercolorlight="#CCCCCC" bordercolordark="#FFFFFF">
  <tr>
  <td class="titel-3" background="<?=$strSkinName?>/pics/bg_tbl_header.jpg"><? echo $lang_text["wbm_ad_name"] ?></td>
  <td class="titel-3" background="<?=$strSkinName?>/pics/bg_tbl_header.jpg"><? echo $lang_text["wbm_ad_email"] ?></td>
  <td class="titel-3" background="<?=$strSkinName?>/pics/bg_tbl_header.jpg"><? echo $lang_text["wbm_ad_actions"] ?></td>
  </tr>
  <?
  while($adlist = db_fetch_array($query_adlist)) {
  ?>
  <tr>
  <td><a class="data" href="email_neu.php?ab_id=<? echo $adlist['ident'] ?>"><? if($adlist['name']) { echo $adlist['name']; } else { echo "&nbsp;"; } ?></a></td>
  <td><? if($adlist['email']) { echo $adlist['email']; } else { echo "&nbsp;"; } ?></td>
  <td align="center"><a href="email_neu.php?ab_id=<? echo $adlist['ident'] ?>"><img src="<?=$strSkinName?>/webmail_icons/icon_new_mail.gif" alt="<? echo $lang_text["m_neuemail"] ?>" border="0"></a> <a href="<? echo $PHP_SELF ?>?editID=<? echo $adlist['ident'] ?>"><img src="<?=$strSkinName?>/webmail_icons/icon_edit.gif" alt="<? echo $lang_text["wbm_ad_edit"] ?>" border="0"></a> <a href="<? echo $PHP_SELF ?>?delID=<? echo $adlist['ident'] ?>"><img src="<?=$strSkinName?>/webmail_icons/icon_delete.gif" alt="<? echo $lang_text["wbm_ad_delete"] ?>" border="0"></a></td>
  </tr>
  <? } ?>
  </table>
  <form name="newentry" action="email_adlist.php?editID=new" method="POST">
  <? echo std_button(ltext("wbm_ad_new"), 'javascript:document.newentry.submit()'); ?>
  </form>
  </td></tr><tr><td>
  <table border="0">
  <tr><td><b><? echo $lang_text["wbm_ad_info"] ?></b></td></tr>
  <tr><td><img src="<?=$strSkinName?>/webmail_icons/icon_new_mail.gif"> <? echo $lang_text["wbm_ad_newmail"] ?></td></tr>
  <tr><td><img src="<?=$strSkinName?>/webmail_icons/icon_edit.gif"> <? echo $lang_text["wbm_ad_edit"] ?></td></tr>
  <tr><td><img src="<?=$strSkinName?>/webmail_icons/icon_delete.gif"> <? echo $lang_text["wbm_ad_delete"] ?></td></tr>
  </table>
  </td></tr>

<? if($spam_row["spamfilter"]): ?>
  <form name="savewhitelist" action="email_adlist.php" method="POST">
  <input type="hidden" name="whitelist_save" value="1">
  <tr><td>
  <table class="tblbgcolor" width="100%" border="1" cellpadding="3" cellspacing="0" bordercolorlight="#CCCCCC" bordercolordark="#FFFFFF">
<? if($whitelist_save): ?>
  <tr>
  <td colspan="2"><? echo $lang_text["wbm_whitelist_saved"]; ?></td>
  </tr>
<? endif; ?>  
  <tr>
  <td><? echo $lang_text["wbm_whitelist_text"]; ?></td><td><INPUT type="hidden" name="auto_whitelist" value="0"><INPUT type="checkbox" name="auto_whitelist" value="1"<? if($auto_whitelist_db) echo " checked"; ?>></td>
  </tr>
  <tr>
  <td colspan="2"><? echo std_button(ltext("wbm_save_whitelist"), 'javascript:document.savewhitelist.submit()'); ?></td>
  </tr>
  </table>
  </td></tr>
  </form>
<? endif; ?>

  </table>
<?
// EDIT/ADD ADRESS:
} else {
  if($editID == "new") {
    $editform = "new";
    if($from_email != "") {
      //$email = $from_email;
	  $from_email = str_replace("&lt", "<", $from_email);
 	  $from_email = str_replace("&gt", ">", $from_email);
 	  $pos_lt = strpos($from_email,"<");
 	  if ($pos_lt === false) $email = $from_email;
 	  else {
 	  	$email = substr($from_email,$pos_lt+2,-2);
 	  	$name =  substr($from_email,0,$pos_lt);
 	  }      
    }
  } else {
    $editform = $editID;
	$query_edit = safe_query( "SELECT * FROM $cnf_db_ab WHERE pop3 = ? AND ident = ?", TRUE,
			new SQLString( $PHP_AUTH_USER ), new SQLInteger( $editID ) );
    $edit = mysql_fetch_array($query_edit);
    $name = $edit['name'];
    $email = $edit['email'];
  }
?>
  <form method="post" action="<? echo $PHP_SELF ?>">
  <input type=hidden name="editform" value="<? echo $editform ?>">
  <table class="tblbgcolor" border="1" cellpadding="3" cellspacing="0" bordercolorlight="#CCCCCC" bordercolordark="#FFFFFF">
  <tr>
    <td><? echo $lang_text["wbm_ad_form_name"] ?></td>
    <td><input name="name" value="<? echo $name ?>"></td>
  </tr><tr>
    <td><? echo $lang_text["wbm_ad_email"] ?></td>
    <td><input name="email" value="<? echo $email ?>"></td>
  </tr><tr>
    <td class="titel-3" background="<?=$strSkinName?>/pics/bg_tbl_header.jpg" colspan="2"><? echo $lang_text["wbm_ad_form_person"] ?></td>
  </tr><tr>
    <td><? echo $lang_text["wbm_ad_form_firstname"] ?></td>
    <td><input name="first_name" value="<? echo $edit['first_name'] ?>"></td>
  </tr><tr>
    <td><? echo $lang_text["wbm_ad_form_lastname"] ?></td>
    <td><input name="last_name" value="<? echo $edit['last_name'] ?>"></td>
  </tr><tr>
    <td class="titel-3" background="<?=$strSkinName?>/pics/bg_tbl_header.jpg" colspan="2"><? echo $lang_text["wbm_ad_form_tel"] ?></td>
  </tr><tr>
    <td><? echo $lang_text["wbm_ad_form_telhome"] ?></td>
    <td><input name="tel_home" value="<? echo $edit['tel_home'] ?>"></td>
  </tr><tr>
    <td><? echo $lang_text["wbm_ad_form_telwork"] ?></td>
    <td><input name="tel_work" value="<? echo $edit['tel_work'] ?>"></td>
  </tr><tr>
    <td><? echo $lang_text["wbm_ad_form_telmobile"] ?></td>
    <td><input name="tel_mobile" value="<? echo $edit['tel_mobile'] ?>"></td>
  </tr><tr>
    <td><? echo $lang_text["wbm_ad_form_fax"] ?></td>
    <td><input name="tel_fax" value="<? echo $edit['tel_fax'] ?>"></td>
  </tr><tr>
    <td class="titel-3" background="<?=$strSkinName?>/pics/bg_tbl_header.jpg" colspan="2"><? echo $lang_text["wbm_ad_form_adress"] ?></td>
  </tr><tr valign=top>
    <td>&nbsp;</td>
    <td><textarea name="address" cols=20 rows=5><? echo $edit['address'] ?></textarea></td>
  </tr><tr>
    <td align=right>		
    	<? echo std_button(ltext("wbm_ad_form_reset"), 'javascript:document.forms[0].reset()'); ?></td>
    <td><? echo std_button(ltext("wbm_ad_form_submit"), 'javascript:document.forms[0].submit()'); ?></td>
  </tr>
  </table>
  </FORM>
<?
}

include("email_footer.php");
?>
