<?php

/*
 
 File: folders.php3
 $Author: chuck $
 $Revision: 1.57 $
 $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 = 0;
$db_lib_loaded = 0;
if (!defined("HORDE_LIB")) include ('../lib/horde.lib');
require( './lib/imp.lib' );           /* IMPlib is the IMP function library  */
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/folders.lang");
require ("./locale/local/folders.lang");
$help_file = 'folders.help';
require ("./locale/defines/$help_file");

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

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();
$mailbox = '';

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

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


$preamble = $folders.$default->personal_folders;
$errormsg = '';

/* Run through the action handlers */

if (isset($actionID)) {
   switch($actionID) {
    case NO_ACTION:
      break;
      
    case DELETE_FOLDER:
      if ($dead_mailbox) {
	 if ($default->use_imap_subscribe) { $boxes = mailbox_list('INBOX'); }
	 if (!(imap_deletemailbox($mbox, "{$server:$port}"."$dead_mailbox"))) {
	    $errormsg = $lang->delete_folder_error;
	 } else {
	    if ($default->use_imap_subscribe) {
	       if (strstr($boxes, "\"$dead_mailbox\"")) {
		  if(!(imap_unsubscribe($mbox, "{$server:$port}$dead_mailbox"))) {
		     $errormsg = $lang->unsub_folder_error;
		  }
	       }
	    }
	 }
      }
      break;
      
    case CREATE_FOLDER:
      if ($new_mailbox) {
	 if (!(imap_createmailbox($mbox, "{$server:$port}$preamble$new_mailbox"))) {
	    $errormsg = $lang->create_folder_error;
	 } else {
	    if ($default->use_imap_subscribe) {
	       if(!(imap_subscribe($mbox,"{$server:$port}$preamble$new_mailbox"))) {
		  $errormsg = $lang->subscribe_folder_error;
	       }
	    }
	 }
      }
      break;
      
    case RENAME_FOLDER:
      if ($old_mailbox && $moved_mailbox) {
	 if (!(imap_renamemailbox($mbox, "{$server:$port}$old_mailbox", "{$server:$port}$preamble"."$moved_mailbox"))) {
	    $errormsg = $lang->rename_folder_error;
	 }
      }
      break;
    case SUBSCRIBE_FOLDER:
      if ($subscribe_mailbox) {
	 if(!(imap_subscribe($mbox,"{$server:$port}$subscribe_mailbox"))) {
	    $errormsg = $lang->subscribe_folder_error;
	 }
      }
      break;
    case UNSUBSCRIBE_FOLDER:
      if($unsubscribe_mailbox) {
	 if(!(imap_unsubscribe($mbox, "{$server:$port}$unsubscribe_mailbox"))) {
            $errormsg = $lang->unsub_folder_error;
	 }
      }
      break;
   }
}

/* close and reopen the imap stream so the mailbox list is up to date */
imap_close($mbox);
$mbox = validate_login($server, $port, $mailbox, $user, $pass, OP_HALFOPEN);

$title = $lang->folders_title;
require("$default->include_dir/folders/header.inc");

$options = mailbox_list('INBOX');
if($default->use_imap_subscribe) {
   $newlist = full_mailbox_list('INBOX');
   include("$default->include_dir/folders/subscribe.inc");
   include("$default->include_dir/folders/unsubscribe.inc");
} else {
   $newlist=$options;
}
require("$default->include_dir/folders/create.inc");
require("$default->include_dir/folders/rename.inc");
require("$default->include_dir/folders/delete.inc");
require("$default->include_dir/generic-footer.inc");

?>
