<?php

/* Table Structure for MySQL version:
 
 Preferences Table:
 field:     type:        description:
 user       text         joe@imap.doe.com format username
 fullname   text         'Joe Doe' 
 replyto    text         joe@doe.com format
 lang       varchar(30)  users's preferred language (en, es, ...)
 sig        text         signature
 
 Address Table:
 field:     type:      description:
 user       text       joe@doe.com identifier of who this entry belongs to
 address    text       address@mail.server.com - the actual address
 nickname   text       any entered nickname - ie "bonnie".
 fullname   text       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 = mysql_pconnect($default->db_server_name, $default->db_user_name, $default->db_password))) { return false; }
    if (!($imp = mysql_select_db($default->db_name, $db))) { return false; }
    if (!($result = mysql_query("delete from $default->db_address_table where user='$user@$server' and address='$address'", $db))) { return false; }
    
    if (!($result = mysql_query("insert into $default->db_address_table (user, address, nickname, fullname) 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 user from $default->db_pref_table where user='$user@$server'";
	if (!($db = mysql_pconnect($default->db_server_name,
							   $default->db_user_name, $default->db_password))) {
		$_imp_prefs_exist = false;
	}
	elseif (!($imp = mysql_select_db($default->db_name, $db))) { $_imp_prefs_exist = false; }
	elseif (!($result = mysql_query($sql, $db))) { $_imp_prefs_exist = false; }
	elseif (mysql_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 = mysql_pconnect($default->db_server_name, $default->db_user_name, $default->db_password))) { return false; }
   if (!($imp = mysql_select_db($default->db_name, $db))) { return false; }
   if (!($result = mysql_query("delete from $default->db_address_table where user='$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 = mysql_pconnect($default->db_server_name, $default->db_user_name, $default->db_password))) { return false; }
   if (!($imp = mysql_select_db($default->db_name, $db))) { return false; }
   if (!($result = mysql_query("select address, nickname, fullname from $default->db_address_table where user='$user@$server' order by nickname", $db))) { return false; }
   
   while ($rows = mysql_fetch_row($result)) {
      $return[] = $rows;
   }
   if(isset($return))
     return($return);
   else
     return(false);
}

function imp_get_from ($user, $server) {
	global $_imp_from, $default;
	
	if (isset($_imp_from)) return $_imp_from;
	
	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 = mysql_pconnect($default->db_server_name, $default->db_user_name, $default->db_password))) { $_imp_from = false; }
	elseif (!($imp = mysql_select_db($default->db_name, $db))) { $_imp_from = false; }
	elseif (!($result = mysql_query("select replyto from $default->db_pref_table where user='$user@$server'", $db))) { $_imp_from = false; }
	elseif (mysql_num_rows($result)>0)
		$_imp_from = mysql_result($result, 0, 0);
	else
		$_imp_from = false;
	
	if (empty($_imp_from)) $_imp_from = false;
	return $_imp_from;
}

function imp_get_fullname ($user, $server) {
	global $_imp_fullname, $default;
	
	if (isset($_imp_fullname)) return $_imp_fullname;
	
	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 = mysql_pconnect($default->db_server_name, $default->db_user_name, $default->db_password))) { $_imp_fullname = false; }
	elseif (!($imp = mysql_select_db($default->db_name, $db))) { $_imp_fullname = false; }
	elseif (!($result = mysql_query("select fullname from $default->db_pref_table where user='$user@$server'", $db))) { $_imp_fullname = false; }
	elseif (mysql_num_rows($result)>0) {
		$_imp_fullname = mysql_result($result, 0, 0);
	} else {
		$_imp_fullname = false;
	}
	
	if (empty($_imp_fullname)) $_imp_fullname = false;
	return $_imp_fullname;
}

function imp_get_lang ($user, $server) {
	global $_imp_lang, $default;
	
	if (isset($_imp_lang)) return $_imp_lang;
	
	/* post: returns the signature for the database key $user@$server
	   (a string), or false on failure.
    */
	
	if (!($db = mysql_pconnect($default->db_server_name, $default->db_user_name, $default->db_password))) { $_imp_lang = false; }
	elseif (!($imp = mysql_select_db($default->db_name, $db))) { $_imp_lang = false; }
	elseif (!($result = mysql_query("select lang from $default->db_pref_table where user='$user@$server'", $db))) { $_imp_lang = false; }
	elseif ((mysql_num_rows($result)>0) && (mysql_result($result, 0, 0)))
		$_imp_lang = mysql_result($result, 0, 0);
	else
		$_imp_lang = false;
	
	if (empty($_imp_lang)) $_imp_lang = false;
	return $_imp_lang;
}

function imp_get_signature ($user, $server) {
	global $_imp_signature, $default;
	
	if (isset($_imp_signature)) {
		return $_imp_signature;
	}
	
	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 = mysql_pconnect($default->db_server_name, $default->db_user_name, $default->db_password))) { $_imp_signature = false; }
	elseif (!($imp = mysql_select_db($default->db_name, $db))) { $_imp_signature = false; }
	elseif (!($result = mysql_query("select sig from $default->db_pref_table where user='$user@$server'", $db))) { $_imp_signature = false; }
	elseif (mysql_num_rows($result) > 0)
		$_imp_signature = mysql_result($result, 0, 0);
	else
		$_imp_signature = false;
	
	if (empty($_imp_signature)) $_imp_signature = false;
	return $_imp_signature;
}

function imp_set_from ($from, $user, $server) {
   global $default;
   $from = addslashes($from);
   
   /* 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 (user, replyto) values ('$user@$server','$from')";
   } else {
      $sql = "update $default->db_pref_table set replyto='$from' where user='$user@$server'";
   }
   
   if (!($db = mysql_pconnect($default->db_server_name, $default->db_user_name, $default->db_password))) { return false; }
   if (!($imp = mysql_select_db($default->db_name, $db))) { return false; }
   if (!($result = mysql_query($sql, $db))) { return false; }
   
   global $_imp_prefs_exist;
   $_imp_prefs_exist = true;
   return true;
}

function imp_set_fullname ($fullname, $user, $server) {
   global $default;
   $fullname = addslashes($fullname);
   
   /* 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 user='$user@$server'";
   } else {
      $sql = "insert into $default->db_pref_table (user, fullname) values ('$user@$server','$fullname')";
   }
   
   if (!($db = mysql_pconnect($default->db_server_name, $default->db_user_name, $default->db_password))) { return false; }
   if (!($imp = mysql_select_db($default->db_name, $db))) { return false; }
   if (!($result = mysql_query($sql, $db))) { return false; }
   
   global $_imp_prefs_exist;
   $_imp_prefs_exist = true;
   return true;
}

function imp_set_lang ($lang, $user, $server) {
   global $default;
   $lang = addslashes($lang);
   
   /* 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 user='$user@$server'";
   } else {
      $sql = "insert into $default->db_pref_table (user, lang) values ('$user@$server', '$lang')";
   }
   
   if (!($db = mysql_pconnect($default->db_server_name, $default->db_user_name, $default->db_password))) { return false; }
   if (!($imp = mysql_select_db($default->db_name, $db))) { return false; }
   if (!($result = mysql_query($sql, $db))) { return false; }
   
   global $_imp_prefs_exist;
   $_imp_prefs_exist = true;
   return true;
}

function imp_set_signature ($signature, $user, $server) {
   global $default;
   $signature = addslashes($signature);
   
   /* 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 user='$user@$server'";
   } else {
      $sql = "insert into $default->db_pref_table (user, sig) values ('$user@$server', '$signature')";
   }
   
   if (!($db = mysql_pconnect($default->db_server_name, $default->db_user_name, $default->db_password))) { return false; }
   if (!($imp = mysql_select_db($default->db_name, $db))) { return false; }
   if (!($result = mysql_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;
   $old_address = addslashes($old_address);
   $address = addslashes($address);
   $nickname = addslashes($nickname);
   $fullname = addslashes($fullname);
   
   /* post: changes the entry for $old_address to $address, $nickname, $fullname.
    returns true on success and false on failure
    */
   
   if (!($db = mysql_pconnect($default->db_server_name, $default->db_user_name, $default->db_password))) { return false; }
   if (!($imp = mysql_select_db($default->db_name, $db))) { return false; }
   if (!($result = mysql_query("update $default->db_address_table set address='$address', nickname='$nickname', fullname='$fullname' where user='$user@$server' and address='$old_address'", $db))) { return false; }

   return true;
}

?>
