Source for file read_body.php

Documentation is available at read_body.php

  1. <?php
  2.  
  3. /**
  4.  * read_body.php
  5.  *
  6.  * This file is used for reading the msgs array and displaying
  7.  * the resulting emails in the right frame.
  8.  *
  9.  * @copyright &copy; 1999-2006 The SquirrelMail Project Team
  10.  * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  11.  * @version $Id: read_body.php,v 1.386 2006/10/06 21:58:17 stevetruckstuff Exp $
  12.  * @package squirrelmail
  13.  */
  14.  
  15. /**
  16.  * Include the SquirrelMail initialization file.
  17.  */
  18. require('../include/init.php');
  19.  
  20. /* SquirrelMail required files. */
  21. require_once(SM_PATH 'functions/imap.php');
  22. require_once(SM_PATH 'functions/imap_asearch.php')// => move to mailbox_display
  23. require_once(SM_PATH 'functions/mime.php');
  24. require_once(SM_PATH 'functions/date.php');
  25. require_once(SM_PATH 'functions/url_parser.php');
  26. require_once(SM_PATH 'functions/identity.php');
  27. require_once(SM_PATH 'functions/mailbox_display.php');
  28. require_once(SM_PATH 'functions/forms.php');
  29. require_once(SM_PATH 'functions/attachment_common.php');
  30.  
  31. /**
  32.  * Given an IMAP message id number, this will look it up in the cached
  33.  * and sorted msgs array and return the index of the next message
  34.  *
  35.  * @param int $passed_id The current message UID
  36.  * @return the index of the next valid message from the array
  37.  */
  38. function findNextMessage($uidset,$passed_id='backwards'{
  39.     if (!is_array($uidset)) {
  40.         return -1;
  41.     }
  42.     if ($passed_id=='backwards' || !is_array($uidset)) // check for backwards compattibilty gpg plugin
  43.         $passed_id $uidset;
  44.     }
  45.     $result sqm_array_get_value_by_offset($uidset,$passed_id,1);
  46.     if ($result === false{
  47.         return -1;
  48.     else {
  49.         return $result;
  50.     }
  51. }
  52.  
  53. /**
  54.  * Given an IMAP message id number, this will look it up in the cached
  55.  * and sorted msgs array and return the index of the previous message
  56.  *
  57.  * @param int $passed_id The current message UID
  58.  * @return the index of the next valid message from the array
  59.  */
  60.  
  61. function findPreviousMessage($uidset$passed_id{
  62.     if (!is_array($uidset)) {
  63.         return -1;
  64.     }
  65.     $result sqm_array_get_value_by_offset($uidset,$passed_id,-1);
  66.     if ($result === false{
  67.         return -1;
  68.     else {
  69.         return $result;
  70.     }
  71. }
  72.  
  73. function html_toggle_href ($mailbox$passed_id$passed_ent_id$message{
  74.     global $base_uri$show_html_default;
  75.  
  76.     $has_html false;
  77.     if ($message->header->type0 == 'message' && $message->header->type1 == 'rfc822'{
  78.         $type0 $message->rfc822_header->content_type->type0;
  79.         $type1 $message->rfc822_header->content_type->type1;
  80.     else {
  81.         $type0 $message->header->type0;
  82.         $type1 $message->header->type1;
  83.     }
  84.     if($type0 == 'multipart' &&
  85.        ($type1 == 'alternative' || $type1 == 'mixed' || $type1 == 'related' || $type1=='signed')) {
  86.         if ($message->findDisplayEntity(array()array('text/html')true)) {
  87.             $has_html true;
  88.         }
  89.     }
  90.     /*
  91.      * Normal single part message so check its type.
  92.      */
  93.     else {
  94.         if($type0 == 'text' && $type1 == 'html'{
  95.             $has_html true;
  96.         }
  97.     }
  98.     if($has_html == true{
  99.         $vars array('passed_ent_id''show_more''show_more_cc''override_type0''override_type1''startMessage','where''what');
  100.  
  101.         $new_link $base_uri 'src/read_body.php?passed_id=' urlencode($passed_id.
  102.                     '&amp;passed_ent_id=' urlencode($passed_ent_id.
  103.                     '&amp;mailbox=' urlencode($mailbox);
  104.         foreach($vars as $var{
  105.             if(sqgetGlobalVar($var$temp)) {
  106.                 $new_link .= '&amp;' $var '=' urlencode($temp);
  107.             }
  108.         }
  109.  
  110.         if($show_html_default == 1{
  111.             $new_link .= '&amp;show_html_default=0';
  112.         else {
  113.             $new_link .= '&amp;show_html_default=1';
  114.         }
  115.         return $new_link;
  116.     }
  117.     return '';
  118. }
  119.  
  120. function ServerMDNSupport($aFlags{
  121.     /* escaping $ doesn't work -> \x36 */
  122.     return in_array('$mdnsent',$aFlags,true||
  123.              in_array('\\*',$aFlags,true) ) ;
  124. }
  125.  
  126. function SendMDN $mailbox$passed_id$sender$message$imapConnection{
  127.     global $username$attachment_dir$popuser$username$color,
  128.            $version$squirrelmail_language$default_charset,
  129.            $languages$useSendmail$domain$sent_folder;
  130.  
  131.     sqgetGlobalVar('SERVER_NAME'$SERVER_NAMESQ_SERVER);
  132.  
  133.     $header $message->rfc822_header;
  134.  
  135.     $rfc822_header new Rfc822Header();
  136.     $content_type  new ContentType('multipart/report');
  137.     $content_type->properties['report-type']='disposition-notification';
  138.  
  139.     set_my_charset();
  140.     if ($default_charset{
  141.         $content_type->properties['charset']=$default_charset;
  142.     }
  143.     $rfc822_header->content_type $content_type;
  144.     $rfc822_header->to[$header->dnt;
  145.     $rfc822_header->subject _("Read:"' ' encodeHeader($header->subject);
  146.  
  147.     // Patch #793504 Return Receipt Failing with <@> from Tim Craig (burny_md)
  148.     // This merely comes from compose.php and only happens when there is no
  149.     // email_addr specified in user's identity (which is the startup config)
  150.     if (ereg("^([^@%/]+)[@%/](.+)$"$username$usernamedata)) {
  151.        $popuser $usernamedata[1];
  152.        $domain  $usernamedata[2];
  153.        unset($usernamedata);
  154.     else {
  155.        $popuser $username;
  156.     }
  157.  
  158.     $reply_to '';
  159.     $ident get_identities();
  160.     if(!isset($identity)) $identity 0;
  161.     $full_name $ident[$identity]['full_name'];
  162.     $from_mail $ident[$identity]['email_address'];
  163.     $from_addr '"'.$full_name.'" <'.$from_mail.'>';
  164.     $reply_to  $ident[$identity]['reply_to'];
  165.  
  166.     if (!$from_mail{
  167.        $from_mail "$popuser@$domain";
  168.        $from_addr $from_mail;
  169.     }
  170.     $rfc822_header->from $rfc822_header->parseAddress($from_addr,true);
  171.     if ($reply_to{
  172.        $rfc822_header->reply_to $rfc822_header->parseAddress($reply_to,true);
  173.     }
  174.  
  175.     // part 1 (RFC2298)
  176.     $senton getLongDateString$header->date );
  177.     $to_array $header->to;
  178.     $to '';
  179.     foreach ($to_array as $line{
  180.         $to .= ' '.$line->getAddress();
  181.     }
  182.     $now getLongDateStringtime() );
  183.     set_my_charset();
  184.     $body _("Your message""\r\n\r\n" .
  185.             "\t" _("To"': ' decodeHeader($to,false,false"\r\n" .
  186.             "\t" _("Subject"': ' decodeHeader($header->subject,false,false"\r\n" .
  187.             "\t" _("Sent"': ' $senton "\r\n" .
  188.             "\r\n" .
  189.             sprintf_("Was displayed on %s")$now );
  190.  
  191.     $special_encoding '';
  192.     if (isset($languages[$squirrelmail_language]['XTRA_CODE']&&
  193.         function_exists($languages[$squirrelmail_language]['XTRA_CODE''_encode')) {
  194.         $body call_user_func($languages[$squirrelmail_language]['XTRA_CODE''_encode'$body);
  195.         if (strtolower($default_charset== 'iso-2022-jp'{
  196.             if (mb_detect_encoding($body== 'ASCII'{
  197.                 $special_encoding '8bit';
  198.             else {
  199.                 $body mb_convert_encoding($body'JIS');
  200.                 $special_encoding '7bit';
  201.             }
  202.         }
  203.     elseif (sq_is8bit($body)) {
  204.         $special_encoding '8bit';
  205.     }
  206.     $part1 new Message();
  207.     $part1->setBody($body);
  208.     $mime_header new MessageHeader;
  209.     $mime_header->type0 'text';
  210.     $mime_header->type1 'plain';
  211.     if ($special_encoding{
  212.         $mime_header->encoding $special_encoding;
  213.     else {
  214.         $mime_header->encoding 'us-ascii';
  215.     }
  216.     if ($default_charset{
  217.         $mime_header->parameters['charset'$default_charset;
  218.     }
  219.     $part1->mime_header $mime_header;
  220.  
  221.     // part2  (RFC2298)
  222.     $original_recipient  $to;
  223.     $original_message_id $header->message_id;
  224.  
  225.     $report "Reporting-UA : $SERVER_NAME ; SquirrelMail (version $version) \r\n";
  226.     if ($original_recipient != ''{
  227.         $report .= "Original-Recipient : $original_recipient\r\n";
  228.     }
  229.     $final_recipient $sender;
  230.     $report .= "Final-Recipientrfc822$final_recipient\r\n.
  231.               "Original-Message-ID : $original_message_id\r\n.
  232.               "Disposition: manual-action/MDN-sent-manually; displayed\r\n";
  233.  
  234.     $part2 new Message();
  235.     $part2->setBody($report);
  236.     $mime_header new MessageHeader;
  237.     $mime_header->type0 'message';
  238.     $mime_header->type1 'disposition-notification';
  239.     $mime_header->encoding 'us-ascii';
  240.     $part2->mime_header $mime_header;
  241.  
  242.     $composeMessage new Message();
  243.     $composeMessage->rfc822_header $rfc822_header;
  244.     $composeMessage->addEntity($part1);
  245.     $composeMessage->addEntity($part2);
  246.  
  247.  
  248.     if ($useSendmail{
  249.         require_once(SM_PATH 'class/deliver/Deliver_SendMail.class.php');
  250.         global $sendmail_path$sendmail_args;
  251.         // Check for outdated configuration
  252.         if (!isset($sendmail_args)) {
  253.             if ($sendmail_path=='/var/qmail/bin/qmail-inject'{
  254.                 $sendmail_args '';
  255.             else {
  256.                 $sendmail_args '-i -t';
  257.             }
  258.         }
  259.         $deliver new Deliver_SendMail(array('sendmail_args'=>$sendmail_args));
  260.         $stream $deliver->initStream($composeMessage,$sendmail_path);
  261.     else {
  262.         require_once(SM_PATH 'class/deliver/Deliver_SMTP.class.php');
  263.         $deliver new Deliver_SMTP();
  264.         global $smtpServerAddress$smtpPort$pop_before_smtp;
  265.         $authPop (isset($pop_before_smtp&& $pop_before_smtptrue false;
  266.         get_smtp_user($user$pass);
  267.         $stream $deliver->initStream($composeMessage,$domain,0,
  268.                                        $smtpServerAddress$smtpPort$user$pass$authPop);
  269.     }
  270.     $success false;
  271.     if ($stream{
  272.         $length  $deliver->mail($composeMessage$stream);
  273.         $success $deliver->finalizeStream($stream);
  274.     }
  275.     if (!$success{
  276.         $msg $deliver->dlv_msg;
  277.         if (empty($deliver->dlv_server_msg)) {
  278.             $msg.= '<br />' .
  279.                 _("Server replied:"' ' $deliver->dlv_ret_nr ' ' .
  280.                 nl2br(htmlspecialchars($deliver->dlv_server_msg));
  281.         }
  282.         plain_error_message($msg$color);
  283.     else {
  284.         unset ($deliver);
  285.         if (sqimap_mailbox_exists ($imapConnection$sent_folder)) {
  286.             $sid sqimap_append ($imapConnection$sent_folder$length);
  287.             require_once(SM_PATH 'class/deliver/Deliver_IMAP.class.php');
  288.             $imap_deliver new Deliver_IMAP();
  289.             $imap_deliver->mail($composeMessage$imapConnection);
  290.             sqimap_append_done ($imapConnection$sent_folder);
  291.             unset ($imap_deliver);
  292.         }
  293.     }
  294.     return $success;
  295. }
  296.  
  297. function ToggleMDNflag ($set ,$imapConnection$mailbox$passed_id{
  298.     $sg   =  $set?'+':'-';
  299.     $cmd  'STORE ' $passed_id ' ' $sg 'FLAGS ($MDNSent)';
  300.     $read sqimap_run_command ($imapConnection$cmdtrue$response,
  301.                                 $readmessageTRUE);
  302. }
  303.  
  304. function formatRecipientString($recipients$item {
  305.     global $show_more_cc$show_more$show_more_bcc,
  306.            $PHP_SELF$oTemplate;
  307.  
  308.     $string '';
  309.     if ((is_array($recipients)) && (isset($recipients[0]))) {
  310.         $show false;
  311.  
  312.         if ($item == 'to'{
  313.             if ($show_more{
  314.                 $show true;
  315.                 $url set_url_var($PHP_SELF'show_more',0);
  316.             else {
  317.                 $url set_url_var($PHP_SELF'show_more',1);
  318.             }
  319.         else if ($item == 'cc'{
  320.             if ($show_more_cc{
  321.                 $show true;
  322.                 $url set_url_var($PHP_SELF'show_more_cc',0);
  323.             else {
  324.                 $url set_url_var($PHP_SELF'show_more_cc',1);
  325.             }
  326.         else if ($item == 'bcc'{
  327.             if ($show_more_bcc{
  328.                 $show true;
  329.                 $url set_url_var($PHP_SELF'show_more_bcc',0);
  330.             else {
  331.                 $url set_url_var($PHP_SELF'show_more_bcc',1);
  332.             }
  333.         }
  334.         
  335.         $a array();
  336.         foreach ($recipients as $r{
  337.             $a[array(
  338.                             'Name'  => htmlspecialchars($r->getAddress(false)),
  339.                             'Email' => htmlspecialchars($r->getEmail()),
  340.                             'Full'  => htmlspecialchars($r->getAddress(true))
  341.                         );
  342.         }
  343.         
  344.         $oTemplate->assign('which_field'$item);
  345.         $oTemplate->assign('recipients'$a);
  346.         $oTemplate->assign('more_less_toggle_href'$url);
  347.         $oTemplate->assign('show_more'$show);
  348.         
  349.         $string $oTemplate->fetch('read_recipient_list.tpl');
  350.     }
  351.     return $string;
  352. }
  353.  
  354. function formatEnvheader($aMailbox$passed_id$passed_ent_id$message,
  355.                          $color$FirstTimeSee{
  356.            $show_xmailer_default$mdn_user_support$PHP_SELF$javascript_on,
  357.            $squirrelmail_language$oTemplate;
  358.  
  359.     $mailbox $aMailbox['NAME'];
  360.  
  361.     $header $message->rfc822_header;
  362.     $env array();
  363.     $env[_("Subject")str_replace("&nbsp;"," ",decodeHeader($header->subject));
  364.  
  365.     $from_name $header->getAddr_s('from');
  366.     if (!$from_name)
  367.         $from_name $header->getAddr_s('sender');
  368.     if (!$from_name)
  369.         $env[_("From")_("Unknown sender");
  370.     else
  371.         $env[_("From")decodeHeader($from_name);
  372.     $env[_("Date")getLongDateString($header->date);
  373.     $env[_("To")formatRecipientString($header->to"to");
  374.     $env[_("Cc")formatRecipientString($header->cc"cc");
  375.     $env[_("Bcc")formatRecipientString($header->bcc"bcc");
  376.     if ($default_use_priority{
  377.         $oTemplate->assign('message_priority'$header->priority);
  378.         $env[_("Priority")$oTemplate->fetch('read_message_priority.tpl');
  379.     }
  380.     if ($show_xmailer_default{
  381.         $oTemplate->assign('xmailer'decodeHeader($header->xmailer));
  382.         $env[_("Mailer")$oTemplate->fetch('read_xmailer.tpl');
  383.     }
  384.     if ($default_use_mdn{
  385.         if ($mdn_user_support{
  386.             if ($header->dnt{
  387.                 $mdn_url $PHP_SELF;
  388.                 $mdn_url set_url_var($PHP_SELF'mailbox'urlencode($mailbox));
  389.                 $mdn_url set_url_var($PHP_SELF'passed_id'$passed_id);
  390.                 $mdn_url set_url_var($PHP_SELF'passed_ent_id'$passed_ent_id);
  391.                 $mdn_url set_url_var($PHP_SELF'sendreceipt'1);
  392.  
  393.                 $oTemplate->assign('read_receipt_sent'$message->is_mdnsent);
  394.                 $oTemplate->assign('first_time_reading'$FirstTimeSee);
  395.                 $oTemplate->assign('send_receipt_href'$mdn_url);
  396.                 
  397.                 $env[_("Read Receipt")$oTemplate->fetch('read_handle_receipt.tpl');
  398.             }
  399.         }
  400.     }
  401.     $env[_("Options")formatToolbar($mailbox$passed_id$passed_ent_id$message$color);
  402.  
  403.     $oTemplate->assign('headers_to_display'$env);
  404.     
  405.     $oTemplate->display('read_headers.tpl');
  406. }
  407.  
  408. /**
  409.  * Format message toolbar
  410.  *
  411.  * @param string $mailbox Name of current mailbox
  412.  * @param int $passed_id UID of current message
  413.  * @param int $passed_ent_id Id of entity within message
  414.  * @param object $message Current message object
  415.  * @param object $mbx_response 
  416.  */
  417. function formatMenubar($aMailbox$passed_id$passed_ent_id$message$removedVar$nav_on_top TRUE{
  418.     global $base_uri$draft_folder$where$what$sort,
  419.            $startMessage$PHP_SELF$save_as_draft,
  420.            $enable_forward_as_attachment$imapConnection$lastTargetMailbox,
  421.            $delete_prev_next_display$show_copy_buttons,
  422.            $compose_new_win$javascript_on$compose_width$compose_height,
  423.            $oTemplate;
  424.  
  425.     //FIXME cleanup argument list, use $aMailbox where possible
  426.     $mailbox $aMailbox['NAME'];
  427.  
  428.     $urlMailbox urlencode($mailbox);
  429.  
  430.     $msgs_url $base_uri 'src/';
  431.  
  432.     // Create Prev & Next links
  433.     // Handle nested entities first (i.e. Mime Attach parts)
  434.     $prev_href $next_href $up_href $del_href $del_prev_href $del_next_href '';
  435.     $msg_list_href $search_href $view_msg_href ''
  436.     if (isset($passed_ent_id&& $passed_ent_id{
  437.         // code for navigating through attached message/rfc822 messages
  438.         $url set_url_var($PHP_SELF'passed_ent_id',0);
  439.         $entities     array();
  440.         $entity_count array();
  441.         $c&nbs