<?php

/* Table Structure for Sybase version:
 
 Preferences Table:
 field:     type:        description:
 UserName   varchar(255) not null    joe@imap.doe.com format username
 Sig        text null                signature
 FullName   varchar(255) null        'Joe Doe' 
 ReplyTo    varchar(255) null        joe@doe.com format
 Lang       varchar(30)  null        users's preferred language (en, es, ...)
 
 Address Table:
 field:     type:      description:
 UserName   varchar(255) not null      joe@doe.com identifier of who this entry belongs to
 Address    varchar(255) null      address@mail.server.com - the actual address
 NickName   varchar(255) null      any entered nickname - ie "bonnie".
 FullName   varchar(255) null      full name - "My Bonnie".
 */

function imp_add_address ($address, $nickname, $fullname, $user, $server) {
	global $default;
   
	/* post: adds $address, $nickname, $fullname to the addressbook for $user@$server
	   returns true on success and false on failure
    */
   
	if (!($db = sybase_pconnect($default->db_server_name, $default->db_user_name, $default->db_password))) { return false; }
	if (!($imp = sybase_select_db($default->db_name, $db))) { return false; }
	if (!($result = sybase_query("delete from $default->db_address_table where username='$user@$server' and address='$address'", $db))) { return false; }

	if (!($result = sybase_query("insert into $default->db_address_table values ('$user@$server', '$address', '$nickname', '$fullname')", $db))) { return false; }
   
	return true;
}

function imp_check_prefs($user,$server) {
	/* Checks for an existing prefs line in the db.
	   Returns true or false. */
   
	global $_imp_prefs_exist, $default;
	
	if (isset($_imp_prefs_exist)) {
		return $_imp_prefs_exist;
	}
	
	$sql="select username from $default->db_pref_table where username='$user@$server'";
	if (!($db = sybase_pconnect($default->db_server_name,
								$default->db_user_name, $default->db_password))) { 
		$_imp_prefs_exist = false; 
	}
	elseif (!($imp = sybase_select_db($default->db_name, $db))) { $_imp_prefs_exist = false; }
	elseif (!($result = sybase_query($sql, $db))) { $_imp_prefs_exist = false; }
	elseif (sybase_num_rows($result)>0) $_imp_prefs_exist = true;
	else $_imp_prefs_exist = false;
	
	return $_imp_prefs_exist;
}

function imp_delete_address ($address, $user, $server) {
   global $default;
   
   /* post: deletes $address from the addressbook of $user@$server
    returns true on success and false on failure
    */
   
   if (!($db = sybase_pconnect($default->db_server_name, $default->db_user_name, $default->db_password))) { return false; }
   if (!($imp = sybase_select_db($default->db_name, $db))) { return false; }
   if (!($result = sybase_query("delete from $default->db_address_table where username='$user@$server' and address='$address'", $db))) { return false; }
   
   return true;
}

function imp_get_addresses ($user, $server) {
   global $default;
   
   /* post: returns a 2d array of addresses where each
    element is an array in which element 0 is the address,
    element 1 is the nickname, and element 2 is the fullname.
    it returns false on failure.
    */
   
   if (!($db = sybase_pconnect($default->db_server_name, $default->db_user_name, $default->db_password))) { return false; }
   if (!($imp = sybase_select_db($default->db_name, $db))) { return false; }
   if (!($result = sybase_query("select address, nickname, fullname from $default->db_address_table where username='$user@$server' order by nickname", $db))) { return false; }
   
   while ($rows = sybase_fetch_row($result)) {
      $return[] = $rows;
   }
   if(isset($return))
     return($return);
   else
     return(false);
}

function imp_get_from ($user, $server) {
   global $default;
   
   if(!imp_check_prefs($user,$server)) return false;
   
   /* post: returns the replyto for the database key $user@$server
    (a string), or false on failure.
    */
   
   if (!($db = sybase_pconnect($default->db_server_name, $default->db_user_name, $default->db_password))) { return false; }
   if (!($imp = sybase_select_db($default->db_name, $db))) { return false; }
   if (!($result = sybase_query("select replyto from $default->db_pref_table where username='$user@$server'", $db))) { return false; }
   if (sybase_num_rows($result)>0)
     return(sybase_result($result, 0, 0));
   else
     return false;
}

function imp_get_fullname ($user, $server) {
   global $default;
   
   if(!imp_check_prefs($user,$server)) return false;
   
   /* post: returns the fullname for the database key $user@$server
    (a string), or false on failure.
    */
   
   if (!($db = sybase_pconnect($default->db_server_name, $default->db_user_name, $default->db_password))) { return false; }
   if (!($imp = sybase_select_db($default->db_name, $db))) { return false; }
   if (!($result = sybase_query("select fullname from $default->db_pref_table where username='$user@$server'", $db))) { return false; }
   
   if (sybase_num_rows($result)>0)
     return(sybase_result($result, 0, 0));
   else
     return false;
}

function imp_get_lang ($user, $server) {
   sybase_min_server_severity(11);
   sybase_min_client_severity(11);

   global $default;
   
   /* post: returns the signature for the database key $user@$server
    (a string), or false on failure.
    */
   
   if (!($db = sybase_pconnect($default->db_server_name, $default->db_user_name, $default->db_password))) { return false; }
   if (!($imp = sybase_select_db($default->db_name, $db))) { return false; }
   if (!($result = sybase_query("select lang from $default->db_pref_table where username='$user@$server'", $db))) { return false; }
   
   if (sybase_num_rows($result)>0)
     return(sybase_result($result, 0, 0));
   else
     return false;
}

function imp_get_signature ($user, $server) {
   global $default;
   
   if(!imp_check_prefs($user,$server)) return false;
   
   /* post: returns the signature for the database key $user@$server
    (a string), or false on failure.
    */
   
   if (!($db = sybase_pconnect($default->db_server_name, $default->db_user_name, $default->db_password))) { return false; }
   if (!($imp = sybase_select_db($default->db_name, $db))) { return false; }
   if (!($result = sybase_query("select sig from $default->db_pref_table where username='$user@$server'", $db))) { return false; }
   if (sybase_num_rows($result)>0)
     return(sybase_result($result, 0, 0));
   else
     return false;
}

function imp_set_from ($from, $user, $server) {
   global $default;
   
   /* post: sets the signature to $signature for the database key $user@$server
    returns true on success and false on failure
    */
   
   if(!(imp_check_prefs($user,$server))) {
      $sql = "insert into $default->db_pref_table (username, replyto) values ('$user@$server','$from')";
   } else {
      $sql = "update $default->db_pref_table set replyto='$from' where username='$user@$server'";
   }
   
   if (!($db = sybase_pconnect($default->db_server_name, $default->db_user_name, $default->db_password))) { return false; }
   if (!($imp = sybase_select_db($default->db_name, $db))) { return false; }
   if (!($result = sybase_query($sql, $db))) { return false; }
   
   global $_imp_prefs_exist;
   $_imp_prefs_exist = true;
   return true;
}

function imp_set_fullname ($fullname, $user, $server) {
   global $default;
   
   /* post: sets the signature to $signature for the database key $user@$server
    returns true on success and false on failure
    */
   
   if (imp_check_prefs($user,$server)) {
      $sql = "update $default->db_pref_table set fullname='$fullname' where username='$user@$server'";
   } else {
      $sql = "insert into $default->db_pref_table (username, fullname) values ('$user@$server','$fullname')";
   }
   
   if (!($db = sybase_pconnect($default->db_server_name, $default->db_user_name, $default->db_password))) { return false; }
   if (!($imp = sybase_select_db($default->db_name, $db))) { return false; }
   if (!($result = sybase_query($sql, $db))) { return false; }
   
   $_imp_prefs_exist = true;
   return true;
}

function imp_set_lang ($lang, $user, $server) {
   global $default;
   
   /* post: sets the language to $lang for the database key $user@$server
    returns true on success and false on failure
    */
   
   if (imp_check_prefs($user, $server)) {
      $sql = "update $default->db_pref_table set lang='$lang' where username='$user@$server'";
   } else {
      $sql = "insert into $default->db_pref_table (username, lang) values ('$user@$server', '$lang')";
   }
   
   if (!($db = sybase_pconnect($default->db_server_name, $default->db_user_name, $default->db_password))) { return false; }
   if (!($imp = sybase_select_db($default->db_name, $db))) { return false; }
   if (!($result = sybase_query($sql, $db))) { return false; }
   
   global $_imp_prefs_exist;
   $_imp_prefs_exist = true;
   return true;
}

function imp_set_signature ($signature, $user, $server) {
   global $default;
   
   /* post: sets the signature to $signature for the database key $user@$server
    returns true on success and false on failure
    */
   
   if (imp_check_prefs($user,$server)) {
      $sql = "update $default->db_pref_table set sig='$signature' where username='$user@$server'";
   } else {
      $sql = "insert into $default->db_pref_table (username, sig) values ('$user@$server', '$signature')";
   }
   
   if (!($db = sybase_pconnect($default->db_server_name, $default->db_user_name, $default->db_password))) { return false; }
   if (!($imp = sybase_select_db($default->db_name, $db))) { return false; }
   if (!($result = sybase_query($sql, $db))) { return false; }
   
   global $_imp_prefs_exist;
   $_imp_prefs_exist = true;
   return true;
}

function imp_update_address ($old_address, $address, $nickname, $fullname, $user, $server) {
   global $default;
   
   /* post: changes the entry for $old_address to $address, $nickname, $fullname.
    returns true on success and false on failure
    */
   
   if (!($db = sybase_pconnect($default->db_server_name, $default->db_user_name, $default->db_password))) { return false; }
   if (!($imp = sybase_select_db($default->db_name, $db))) { return false; }
   if (!($result = sybase_query("update $default->db_address_table set address='$address', nickname='$nickname', fullname='$fullname' where username='$user@$server' and address='$old_address'", $db))) { return false; }
   
   return true;
}

?>
