<?php
// $Id: sasql_wblist.php,v 1.3 2004/03/19 18:22:32 bucovina Exp $

if (!defined ('SM_PATH')) { define('SM_PATH', '../../'); }

include_once(SM_PATH.'plugins/sasql/sasql_conf.php');
include_once(SM_PATH.'plugins/sasql/sasql_db.php');

include_once(SM_PATH.'include/validate.php');
include_once(SM_PATH.'functions/page_header.php');
include_once(SM_PATH.'include/load_prefs.php');
include_once(SM_PATH.'plugins/sasql/sasql_conf.php');
include_once(SM_PATH.'plugins/sasql/sasql_db.php');

// From sasql_conf.php
global $SqlDSN;
global $SqlTable;
global $useSA2_43;
global $allowed_params;
global $allowed_wl_params;
global $sa_params;
global $wl_params;
global $default_domain;

// from SM
global $username;

// Form
sqgetGlobalVar('action', $action, SQ_FORM);  //action
sqgetGlobalVar('address', $address, SQ_FORM);  // Address to work on
sqgetGlobalVar('mailbox', $mailbox, SQ_FORM);   // Current mailbox
sqgetglobalVar('passed_id', $passed_id, SQ_FORM); // Message id
sqgetGlobalVar('startMessage', $startMessage, SQ_FORM); // Paginator index.
sqgetGlobalVar('ent_id', $ent_id, SQ_FORM);    // Entity id (jump back to attachment)
sqgetGlobalVar('preference', $preference, SQ_FORM);

if ($action) {
  $error = -1;
  if (!$address) {
    $error++;
    $errmsg[$error] = _("Error: You must include an e-mail address.");
  }

  if ($address
      && !ereg ("^([*a-zA-Z0-9_\.'-]+@)?[*a-zA-Z0-9\.-]+$", $address)
      ) {
    $error++;
    $errmsg[$error] = _("Error: You have entered an invalid e-mail address.");
  }

  if (! $preference) {
    $error++;
    $errmsg[$error] = _("Error: Missing option.");
  }

  if ($error < 0) {
    $DBHandle = sasql_DBConnect($SqlDSN);
    if ($action == 'new') {
      sasql_AddPref ($DBHandle, $SqlTable, $username, $preference, $address);
    } elseif ($action == 'delete') {
      sasql_DelPref_value ($DBHandle, $SqlTable, $username, $preference, $address);
    }
    sasql_DBDisconnect($DBHandle);
  }
}

$redir_uri = SM_PATH . 'src/read_body.php?';
$redir_uri .= "&mailbox=$mailbox";
$redir_uri .= "&passed_id=$passed_id";
$redir_uri .= "&startMessage=$startMessage;";
if ($ent_id) { $redir_uri .= "&ent_id=$ent_id"; }

header("Location: $redir_uri");

?>
