<?php

/*
 
 File: view.php3
 $Author: chuck $
 $Revision: 1.52 $
 $Date: 1999/04/11 01:56:31 $
 
 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.
 
 */

// if $id=2.1, returns $structure->parts[1]->parts[0]
function partFromId($structure,$id) {
   $res=$structure;
   if(isset($res->parts[0])) {
      $piece = explode(".",$id);
      $num=count($piece);
      $ind=0;
      //echo "<b>id=$id nbniv=$num</b>";
      $j=0;
      while($j < $num){
	 $ind=$piece[$j]-1;
	 //echo "<b> piece $ind</b>";
	 $res=$res->parts[$ind];
	 $j++;
      }
   }
   return $res;
}

$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 ('./lib/mimetypes.lib');     /* Mime functions */
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();

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

/* this apparently breaks downloads on some browsers
 */
caching_headers();

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

$mbox = validate_login($server, $port, $mailbox, $user, $pass, OP_READONLY);


$structure = imap_fetchstructure ($mbox, imap_msgno($mbox, $index));  
if (($actionID != SAVE_MESSAGE) && ($actionID != VIEW_SOURCE)) $part = partFromId($structure,$bodypart);

/* Run through action handlers */
if (isset($actionID)) {
   switch($actionID) {
    case NO_ACTION:
      break;
      
    case DOWNLOAD_ATTACH:
      $mime = new mime_part_data;
      $mime->parse($part);
      
      /* This should force a save file dialog...  */
      /* doesn't seem to force save file dialog, using RFC822 does
       * even for IE 
       * header("Content-type: application/octet-stream"); */
      header("Content-type: application/RFC822");
      break;
      
    case VIEW_ATTACH: {
       $mime = new mime_part_data;
       $mime->parse( $part );
       $mime->set_imap_data( $mailbox, $index, $bodypart );
       Header("content-type: text/html");
       if (isset($mime->action["view_function"]) ) {
	  $title = "MIME Attachment View";
	  include( "$default->include_dir/generic-header.inc" );
	  eval( "echo ".$mime->action["view_function"].'($mime);' );
	  include( "$default->include_dir/generic-footer.inc" );
	  exit;
       }
       
       if (($mime->subtype == 'jpeg') || ($mime->subtype == 'gif') || ($mime->subtype == 'png')) {
	  header("Content-type: image/$mime->subtype");
       } else {
	  /* do not set the encoding type to 1 here because it
	   * potentially blows away our shot at decoding the
	   * attachment when we need to.
	   * $mime->encoding = 1; 
	   */
	  header("Content-type: text/plain");
       }
    } break;
      
    case VIEW_SOURCE:
      if( !$default->view_message_source ) {
	 echo "Not Authorized.";
      } else {
	 header("Content-type: text/plain");
	 echo imap_fetchheader ($mbox, imap_msgno($mbox, $index)) . "\n" . imap_body ($mbox, $index, FT_UID );
	 imap_close($mbox);
	 exit; 
      }
      break;
      
    case SAVE_MESSAGE:
      $h = imap_header($mbox, imap_msgno($mbox, $index));
      $mime = new mime_part_data;
      
      if (isset($h->subject))
	$mime->name = chop(decode_mime_string($h->subject));
      else
	$mime->name = 'mail';
      
      $from = chop(decode_mime_string($h->fromaddress));
      $msg = "From " . $from . "\n";
      $msg = $msg . imap_fetchheader($mbox, $index, FT_PREFETCHTEXT|FT_UID);
      $body = $msg . imap_body ($mbox, $index, FT_UID);
      
      header("Content-type: application/RFC822");
      break;
      
   }
}

header("Content-Disposition: attachment; filename=$mime->name");

if ($actionID != SAVE_MESSAGE) {
   $body = imap_fetchbody($mbox, $index, $bodypart, FT_UID);
   
   if ($mime->encoding == 3)
     $body = imap_base64($body);
   elseif ($mime->encoding == 4 && ($qprint_msg = imap_qprint($body)))
     $body = $qprint_msg;
}

echo $body;

?>
