<?php

/*
 
 File: contacts.php3
 $Author: chuck $
 $Revision: 1.10 $
 $Date: 1999/04/11 01:56:30 $
 
 IMP: Copyright 1999 Charles J. Hagenbuch <chuck@horde.org>
 
 You should have received a copy of the GNU Public
 License along with this package; if not, write to the
 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 Boston, MA 02111-1307, USA.
 
 */


$need_horde_db = 1;
$db_lib_loaded = 0;
if (!defined("HORDE_LIB")) include ('../lib/horde.lib');
require ('./lib/imp.lib');              /* IMPlib is the IMP function library  */
require( './config/ldap.php3' );
require ('./config/defaults.php3');     /* Defaults Configuration File */
/* Html styles configuration */
require('./config/html.php3');
/* Mailbox configuration */
require('./config/mailbox.php3');
/* include mime and language configuration */
require('./config/mime.php3');
require('./config/lang.php3');

$language = select_lang();
require ("./locale/$language/contacts.lang");
require ("./locale/local/contacts.lang");
/* Setup the this_client object */
$this_client = new WebClient;

error_reporting($default->error_level);  /* set error level from imp.lib   */

/* send headers to ensure that the page will reload correctly. */
caching_headers();

/* retrieve authentication information from session */
$session = create_session_object();
$user    = $session->get_user();
$pass    = $session->get_password();
$server  = $session->get_server();
$port    = $session->get_port();
$folders = $session->get_folder();
if (!isset($mailbox)) { $mailbox = 'INBOX'; }

/* proceed only with a valid login */
$mbox = validate_login($server, $port, $mailbox, $user, $pass, OP_READONLY);

/* doctype */
require("$default->include_dir/doctype.inc");


/* Run through the action handlers */
if (isset($actionID)) {
   if (strcspn($actionID, '0123456789')) { $actionID = $actions[$actionID]; }

   switch($actionID) {
    case NO_ACTION:
      break;
 
    case DELETE_ADDR:
      if ($new_address) {
	 if (!(imp_delete_address($new_address, $user, $server))) {
	    $errormsg = $lang->error_delete_address;
	 }
      }
      $new_address = '';
      break;
      
    case ADD_CONTACT:
      if ($mailbox && $index && $bodypart) {
         $h = imap_header($mbox, imap_msgno($mbox, $index));
         if (isset($h->reply_toaddress)) { $new_address = chop(htmlspecialchars($h->reply_toaddress)); }
         elseif (isset($h->fromaddress)) { $new_address = chop(htmlspecialchars($h->fromaddress )); }
         else $new_address = '';

      }
      break;
      
    case ADD_ADDR:
      if ($new_address && $new_nickname && $new_fullname) {
	 $clean_nickname = addslashes($new_nickname);
	 $clean_fullname = addslashes($new_fullname);
	 $clean_address = addslashes($new_address);
	 
	 $errormsg = '';
	 if ($clean_address != $new_address)   $errormsg .= $lang->error_address_chars;
	 if ($clean_nickname != $new_nickname) $errormsg .= $lang->error_nickname_chars;
	 if ($clean_fullname != $new_fullname) $errormsg .= $lang->error_fullname_chars;
	 if (!(imp_add_address($clean_address, $clean_nickname, $clean_fullname, $user, $server))) {
	    $errormsg .= $lang->error_create_address;
	 }
      }
      $new_address = '';
      break;
      
    case UPDATE_ADDR:
      if ($old_address && $new_address && $new_nickname && $new_fullname) {
	 if (!(imp_update_address($old_address, $new_address, $new_nickname, $new_fullname, $user, $server))) {
	    $errormsg = $lang->error_update_address;
	 }
      }
      $new_address = '';
      break;
   }
}


$title = $lang->addressbook_title;
$sidebar = false;
require("$default->include_dir/generic-header.inc");

if (!isset($new_address)) { $new_address = ''; }


$addresses = imp_get_addresses($user, $server);

$options = '';
if (isset($addresses) && is_array($addresses)) {
   for ($i = 0; $i < count($addresses); $i++) {
      if (is_array($addresses[$i]) && sizeof($addresses[$i] > 1)) {
	 $options .= '    <option value="' . $addresses[$i][0] . '">' . $addresses[$i][1] . ' &lt;' . $addresses[$i][0] . "&gt;\n";
      }
   }
}

if ($default->use_db):
  if ($default->user_use_addressbook == true):
    require("$default->include_dir/contacts/addr-php3.inc");
    if ($default->use_ldap_search):
      require("$default->include_dir/contacts/ldap-php3.inc");
    endif;
  else:
    if ($default->use_ldap_search):
      require("$default->include_dir/contacts/ldap-php3.inc");
    endif;
  endif;
else:
  if ($default->use_ldap_search):
    require("$default->include_dir/contacts/ldap-php3.inc");
  endif; 
endif;

require("$default->include_dir/generic-footer.inc");
require("$default->include_dir/contacts/javascript.inc");
?>
