Source for file compose.php

Documentation is available at compose.php

  1. <?php
  2.  
  3. /**
  4.  * compose.php
  5.  *
  6.  * This code sends a mail.
  7.  *
  8.  * There are 4 modes of operation:
  9.  *    - Start new mail
  10.  *    - Add an attachment
  11.  *    - Send mail
  12.  *    - Save As Draft
  13.  *
  14.  * @copyright 1999-2020 The SquirrelMail Project Team
  15.  * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  16.  * @version $Id: compose.php 14840 2020-01-07 07:42:38Z pdontthink $
  17.  * @package squirrelmail
  18.  */
  19.  
  20. /** This is the compose page */
  21. define('PAGE_NAME''compose');
  22.  
  23. /**
  24.  * Path for SquirrelMail required files.
  25.  * @ignore
  26.  */
  27. define('SM_PATH','../');
  28.  
  29. /* SquirrelMail required files. */
  30. require_once(SM_PATH 'include/validate.php');
  31. require_once(SM_PATH 'functions/global.php');
  32. require_once(SM_PATH 'functions/imap.php');
  33. require_once(SM_PATH 'functions/date.php');
  34. require_once(SM_PATH 'functions/mime.php');
  35. require_once(SM_PATH 'functions/plugin.php');
  36. require_once(SM_PATH 'functions/display_messages.php');
  37. require_once(SM_PATH 'class/deliver/Deliver.class.php');
  38. require_once(SM_PATH 'functions/addressbook.php');
  39. require_once(SM_PATH 'functions/forms.php');
  40. require_once(SM_PATH 'functions/identity.php');
  41.  
  42. /* --------------------- Get globals ------------------------------------- */
  43. /** COOKIE VARS */
  44. sqgetGlobalVar('key',       $key,           SQ_COOKIE);
  45.  
  46. /** SESSION VARS */
  47. sqgetGlobalVar('username',  $username,      SQ_SESSION);
  48. sqgetGlobalVar('onetimepad',$onetimepad,    SQ_SESSION);
  49. sqgetGlobalVar('base_uri',  $base_uri,      SQ_SESSION);
  50. sqgetGlobalVar('delimiter'$delimiter,     SQ_SESSION);
  51.  
  52. sqgetGlobalVar('composesession',    $composesession,    SQ_SESSION);
  53. sqgetGlobalVar('compose_messages',  $compose_messages,  SQ_SESSION);
  54.  
  55. // compose_messages only useful in SESSION when a forward-as-attachment 
  56. // has been preconstructed for us and passed in via that mechanism; once 
  57. // we have it, we can clear it from the SESSION
  58. // -- No, this is useful in other scenarios, too -- removing:
  59. // sqsession_unregister('compose_messages');
  60.  
  61. /** SESSION/POST/GET VARS */
  62. sqgetGlobalVar('send'$sendSQ_POST);
  63. // Send can only be achieved by setting $_POST var. If Send = true then
  64. // retrieve other form fields from $_POST
  65. if (isset($send&& $send{
  66.     $SQ_GLOBAL SQ_POST;
  67. else {
  68.     $SQ_GLOBAL SQ_FORM;
  69. }
  70. sqgetGlobalVar('smaction',$action$SQ_GLOBAL);
  71. if (!sqgetGlobalVar('smtoken',$submitted_token$SQ_GLOBAL)) {
  72.     $submitted_token '';
  73. }
  74. sqgetGlobalVar('session',$session$SQ_GLOBAL);
  75. sqgetGlobalVar('mailbox',$mailbox$SQ_GLOBAL);
  76. sqgetGlobalVar('identity',$orig_identity$SQ_GLOBAL);
  77. if !sqgetGlobalVar('identity',$identity$SQ_GLOBAL) ) {
  78.     $identity 0;
  79. }
  80. sqgetGlobalVar('send_to',$send_to$SQ_GLOBAL);
  81. sqgetGlobalVar('send_to_cc',$send_to_cc$SQ_GLOBAL);
  82. sqgetGlobalVar('send_to_bcc',$send_to_bcc$SQ_GLOBAL);
  83. sqgetGlobalVar('subject',$subject$SQ_GLOBAL);
  84. sqgetGlobalVar('body',$body$SQ_GLOBAL);
  85. sqgetGlobalVar('mailprio',$mailprio$SQ_GLOBAL);
  86. sqgetGlobalVar('request_mdn',$request_mdn$SQ_GLOBAL);
  87. sqgetGlobalVar('request_dr',$request_dr$SQ_GLOBAL);
  88. sqgetGlobalVar('html_addr_search',$html_addr_searchSQ_FORM);
  89. sqgetGlobalVar('mail_sent',$mail_sentSQ_FORM);
  90. sqgetGlobalVar('passed_id',$passed_id$SQ_GLOBAL);
  91. sqgetGlobalVar('passed_ent_id',$passed_ent_id$SQ_GLOBAL);
  92.  
  93. sqgetGlobalVar('attach',$attachSQ_POST);
  94. sqgetGlobalVar('draft',$draftSQ_POST);
  95. sqgetGlobalVar('draft_id',$draft_id$SQ_GLOBAL);
  96. sqgetGlobalVar('ent_num',$ent_num$SQ_GLOBAL);
  97. sqgetGlobalVar('saved_draft',$saved_draftSQ_FORM);
  98.  
  99. if sqgetGlobalVar('delete_draft',$delete_draft) ) {
  100.     $delete_draft = (int)$delete_draft;
  101. }
  102.  
  103. if sqgetGlobalVar('startMessage',$startMessage) ) {
  104.     $startMessage = (int)$startMessage;
  105. else {
  106.     $startMessage 1;
  107. }
  108.  
  109. /** POST VARS */
  110. sqgetGlobalVar('sigappend',             $sigappend,             SQ_POST);
  111. sqgetGlobalVar('from_htmladdr_search',  $from_htmladdr_search,  SQ_POST);
  112. sqgetGlobalVar('addr_search_done',      $html_addr_search_doneSQ_POST);
  113. sqgetGlobalVar('send_to_search',        $send_to_search,        SQ_POST);
  114. sqgetGlobalVar('do_delete',             $do_delete,             SQ_POST);
  115. sqgetGlobalVar('delete',                $delete,                SQ_POST);
  116. sqgetGlobalVar('attachments',           $attachments,           SQ_POST);
  117. // Not used any more, but left for posterity
  118. //sqgetGlobalVar('restoremessages',       $restoremessages,       SQ_POST);
  119. if sqgetGlobalVar('return'$tempSQ_POST) ) {
  120.     $html_addr_search_done 'Use Addresses';
  121. }
  122.  
  123. /** GET VARS */
  124. // (none)
  125.  
  126. /**
  127.  * Here we decode the data passed in from mailto.php.
  128.  */
  129. if sqgetGlobalVar('mailtodata'$mailtodataSQ_GET) ) {
  130.     $trtable array('to'       => 'send_to',
  131.                  'cc'           => 'send_to_cc',
  132.                  'bcc'          => 'send_to_bcc',
  133.                  'body'         => 'body',
  134.                  'subject'      => 'subject');
  135.     $mtdata unserialize($mailtodata);
  136.  
  137.     foreach ($trtable as $f => $t{
  138.         if !empty($mtdata[$f]) ) {
  139.             $$t $mtdata[$f];
  140.         }
  141.     }
  142.     unset($mailtodata,$mtdata$trtable);
  143. }
  144.  
  145. /* Location (For HTTP 1.1 Header("Location: ...") redirects) */
  146. $location get_location();
  147. /* Identities (fetch only once) */
  148. $idents get_identities();
  149.  
  150. /* --------------------- Specific Functions ------------------------------ */
  151.  
  152. function replyAllString($header{
  153.     global $include_self_reply_all$username$data_dir;
  154.     $excl_ar array();
  155.     /**
  156.      * 1) Remove the addresses we'll be sending the message 'to'
  157.      */
  158.     $url_replytoall_avoid_addrs '';
  159.     if (isset($header->reply_to&& is_array($header->reply_to&& count($header->reply_to)) {
  160.         $excl_ar $header->getAddr_a('reply_to');
  161.     else if (is_object($header->reply_to)) /* unneccesarry, just for failsafe purpose */
  162.         $excl_ar $header->getAddr_a('reply_to');
  163.     else {
  164.         $excl_ar $header->getAddr_a('from');
  165.     }
  166.     /**
  167.      * 2) Remove our identities from the CC list (they still can be in the
  168.      * TO list) only if $include_self_reply_all is turned off
  169.      */
  170.     if (!$include_self_reply_all{
  171.         global $idents;
  172.         foreach($idents as $id{
  173.             $excl_ar[strtolower(trim($id['email_address']))'';
  174.         }
  175.     }
  176.  
  177.     /**
  178.      * 3) get the addresses.
  179.      */
  180.     $url_replytoall_ar $header->getAddr_a(array('to','cc')$excl_ar);
  181.  
  182.     /**
  183.      * 4) generate the string.
  184.      */
  185.     $url_replytoallcc '';
  186.     foreach$url_replytoall_ar as $email => $personal{
  187.         if ($personal{
  188.             // always quote personal name (can't just quote it if
  189.             // it contains a comma separator, since it might still
  190.             // be encoded)
  191.             $url_replytoallcc .= ", \"$personal\" <$email>";
  192.         else {
  193.             $url_replytoallcc .= ', '$email;
  194.         }
  195.     }
  196.     $url_replytoallcc substr($url_replytoallcc,2);
  197.  
  198.     return $url_replytoallcc;
  199. }
  200.  
  201. function getReplyCitation($orig_from$orig_date{
  202.     global $reply_citation_style$reply_citation_start$reply_citation_end;
  203.  
  204.     // FIXME: why object is rewritten with string.
  205.  
  206.     if (!is_object($orig_from)) {
  207.         $orig_from '';
  208.     else {
  209.         $orig_from decodeHeader($orig_from->getAddress(false),false,false,true);
  210.     }
  211.  
  212.     /* First, return an empty string when no citation style selected. */
  213.     if (($reply_citation_style == ''|| ($reply_citation_style == 'none')) {
  214.         return '';
  215.     }
  216.  
  217.     /* Make sure our final value isn't an empty string. */
  218.     if ($orig_from == ''{
  219.         return '';
  220.     }
  221.  
  222.     /* Otherwise, try to select the desired citation style. */
  223.     switch ($reply_citation_style{
  224.     case 'author_said':
  225.         /**
  226.          * To translators: %s is for author's name
  227.          */
  228.         $full_reply_citation sprintf(_("%s wrote:"),$orig_from);
  229.         break;
  230.     case 'quote_who':
  231.         $start '<' _("quote"' ' _("who"'="';
  232.         $end   '">';
  233.         $full_reply_citation $start $orig_from $end;
  234.         break;
  235.     case 'date_time_author':
  236.         /**
  237.          * To translators:
  238.          *  first %s is for date string, second %s is for author's name. Date uses
  239.          *  formating from "D, F j, Y g:i a" and "D, F j, Y H:i" translations.
  240.          * Example string:
  241.          *  "On Sat, December 24, 2004 23:59, Santa wrote:"
  242.          * If you have to put author's name in front of date string, check comments about
  243.          * argument swapping at http://www.php.net/sprintf
  244.          */
  245.         $full_reply_citation sprintf(_("On %s, %s wrote:")getLongDateString($orig_date)$orig_from);
  246.         break;
  247.     case 'user-defined':
  248.         $start $reply_citation_start .
  249.             ($reply_citation_start == '' '' ' ');
  250.         $end   $reply_citation_end;
  251.         $full_reply_citation $start $orig_from $end;
  252.         break;
  253.     default:
  254.         return '';
  255.     }
  256.  
  257.     /* Add line feed and return the citation string. */
  258.     return ($full_reply_citation "\n");
  259. }
  260.  
  261. function getforwardHeader($orig_header{
  262.     global $editor_size;
  263.  
  264.     $display array_("Subject"=> strlen(_("Subject")),
  265.             _("From")    => strlen(_("From")),
  266.             _("Date")    => strlen(_("Date")),
  267.             _("To")      => strlen(_("To")),
  268.             _("Cc")      => strlen(_("Cc")) );
  269.     $maxsize max($display);
  270.     $indent str_pad('',$maxsize+2);
  271.     foreach($display as $key => $val{
  272.         $display[$key$key .': 'str_pad(''$maxsize $val);
  273.     }
  274.     $from decodeHeader($orig_header->getAddr_s('from',"\n$indent"),false,false,true);
  275.     $from str_replace('&nbsp;',' ',$from);
  276.     $to decodeHeader($orig_header->getAddr_s('to',"\n$indent"),false,false,true);
  277.     $to str_replace('&nbsp;',' ',$to);
  278.     $subject decodeHeader($orig_header->subject,false,false,true);
  279.     $subject str_replace('&nbsp;',' ',$subject);
  280.     $bodyTop =  str_pad(' '._("Original Message").' ',$editor_size -2,'-',STR_PAD_BOTH.
  281.         "\n"$display[_("Subject")$subject "\n" .
  282.         $display[_("From")$from "\n" .
  283.         $display[_("Date")getLongDateString$orig_header->date$orig_header->date_unparsed )"\n" .
  284.         $display[_("To")$to "\n";
  285.     if ($orig_header->cc != array(&& $orig_header->cc !=''{
  286.         $cc decodeHeader($orig_header->getAddr_s('cc',"\n$indent"),false,false,true);
  287.         $cc str_replace('&nbsp;',' ',$cc);
  288.         $bodyTop .= $display[_("Cc").$cc "\n";
  289.     }
  290.     $bodyTop .= str_pad(''$editor_size -'-'.
  291.         "\n\n";
  292.     return $bodyTop;
  293. }
  294. /* ----------------------------------------------------------------------- */
  295.  
  296. /*
  297.  * If the session is expired during a post this restores the compose session
  298.  * vars.
  299.  */
  300. $session_expired false;
  301. if (sqsession_is_registered('session_expired_post')) {
  302.     sqgetGlobalVar('session_expired_post'$session_expired_postSQ_SESSION);
  303.     /*
  304.      * extra check for username so we don't display previous post data from
  305.      * another user during this session.
  306.      */
  307.     if (!empty($session_expired_post['username']
  308.      && $session_expired_post['username'== $username{
  309.         // these are the vars that we can set from the expired composed session
  310.         $compo_var_list array ('send_to''send_to_cc''body''mailbox',
  311.             'startMessage''passed_body''use_signature''signature',
  312.             'attachments''subject''newmail''send_to_bcc''passed_id'
  313.             'from_htmladdr_search''identity''draft_id''delete_draft'
  314.             'mailprio''edit_as_new''request_mdn''request_dr'
  315.             'composesession'/* Not used any more: 'compose_messsages', */);
  316.  
  317.         foreach ($compo_var_list as $var{
  318.             if isset($session_expired_post[$var]&& !isset($$var) ) {
  319.                 $$var $session_expired_post[$var];
  320.             }
  321.         }
  322.  
  323.         if (!empty($attachments)) 
  324.             $attachments unserialize($attachments);
  325.  
  326.         sqsession_register($composesession,'composesession');
  327.  
  328.         if (isset($send)) {
  329.             unset($send);
  330.         }
  331.         $session_expired true;
  332.     }
  333.     unset($session_expired_post);
  334.     sqsession_unregister('session_expired_post');
  335.     if (!isset($mailbox)) {
  336.         $mailbox '';
  337.     }
  338.     if ($compose_new_win == '1'{
  339.         compose_Header($color$mailbox);
  340.     else {
  341.         displayPageHeader($color$mailbox);
  342.     }
  343.     showInputForm($sessionfalse);
  344.     exit();
  345. }
  346.  
  347. if (!isset($composesession)) {
  348.     $composesession 0;
  349.     sqsession_register(0,'composesession');
  350. else {
  351.     $composesession = (int)$composesession;
  352. }
  353.  
  354. if (!isset($session|| (isset($newmessage&& $newmessage)) {
  355.     sqsession_unregister('composesession');
  356.     $session "$composesession+1;
  357.     $composesession $session;
  358.     sqsession_register($composesession,'composesession');
  359. }
  360. if (!empty($compose_messages[$session])) {
  361.     $composeMessage $compose_messages[$session];
  362. else {
  363.     $composeMessage new Message();
  364.     $rfc822_header new Rfc822Header();
  365.     $composeMessage->rfc822_header $rfc822_header;
  366.     $composeMessage->reply_rfc822_header '';
  367. }
  368.  
  369. // re-add attachments that were already in this message
  370. // FIXME: note that technically this is very bad form - 
  371. // should never directly manipulate an object like this
  372. if (!empty($attachments)) {
  373.     $attachments unserialize($attachments);
  374.     if (!empty($attachments&& is_array($attachments)) {
  375.         // sanitize the "att_local_name" since it is user-supplied and used to access the file system
  376.         // it must be alpha-numeric and 32 characters long (see the use of GenerateRandomString() below)
  377.         foreach ($attachments as $i => $attachment{
  378.             if (empty($attachment->att_local_name|| strlen($attachment->att_local_name!== 32{
  379.                 unset($attachments[$i]);
  380.                 continue;
  381.             }
  382.             // probably marginal difference between (ctype_alnum + function_exists) and preg_match
  383.             if (function_exists('ctype_alnum')) {
  384.                 if (!ctype_alnum($attachment->att_local_name))
  385.                     unset($attachments[$i]);
  386.             }
  387.             else if (preg_match('/[^0-9a-zA-Z]/'$attachment->att_local_name))
  388.                 unset($attachments[$i]);
  389.         }
  390.         if (!empty($attachments))
  391.             $composeMessage->entities $attachments;
  392.     }
  393. }
  394.  
  395. if (!isset($mailbox|| $mailbox == '' || ($mailbox == 'None')) {
  396.     $mailbox 'INBOX';
  397. }
  398.  
  399. if ($draft{
  400.  
  401.     // validate security token
  402.     //
  403.     sm_validate_security_token($submitted_token-1TRUE);
  404.  
  405.     /*
  406.      * Set $default_charset to correspond with the user's selection
  407.      * of language interface.
  408.      */
  409.     set_my_charset();
  410.     if (deliverMessage($composeMessagetrue)) {
  411.         showInputForm($session);
  412.         exit();
  413.     else {
  414.         $draft_message _("Draft Email Saved");
  415.         /* If this is a resumed draft, then delete the original */
  416.         if(isset($delete_draft)) {
  417.             if !isset($pageheader_sent|| !$pageheader_sent {
  418.                 Header("Location: $location/delete_message.php?mailbox=urlencode($draft_folder.
  419.                         "&message=$delete_draft&sort=$sort&startMessage=1&saved_draft=yes&smtoken=sm_generate_security_token());
  420.             else {
  421.                 echo '   <br><br><center><a href="' $location
  422.                     . "/delete_message.php?mailbox=" urlencode($draft_folder)
  423.                     . "&message=$delete_draft&sort=$sort&startMessage=1&saved_draft=yes&smtoken=sm_generate_security_token("\">"
  424.                     . _("Return"'</a></center>';
  425.             }
  426.             exit();
  427.         }
  428.         else {
  429.             if ($compose_new_win == '1'{
  430.                 if !isset($pageheader_sent|| !$pageheader_sent {
  431.                     Header("Location: $location/compose.php?saved_draft=yes&session=$composesession");
  432.                 else {
  433.                     echo '   <br><br><center><a href="' $location
  434.                         . "/compose.php?saved_draft=yes&session=$composesession\">"
  435.                         . _("Return"'</a></center>';
  436.                 }
  437.                 exit();
  438.             }
  439.             else {
  440.                 if !isset($pageheader_sent|| !$pageheader_sent {
  441.                     Header("Location: $location/right_main.php?mailbox=urlencode($draft_folder.
  442.                         "&sort=$sort&startMessage=1&note=".urlencode($draft_message));
  443.                 else {
  444.                     echo '   <br><br><center><a href="' $location
  445.                         . "/right_main.php?mailbox=" urlencode($draft_folder)
  446.                         . "&sort=$sort&startMessage=1&note=".urlencode($draft_message)
  447.                         . "\">" _("Return"'</a></center>';
  448.                 }
  449.                 exit();
  450.             }
  451.         }
  452.     }
  453. }
  454.  
  455. if ($send{
  456.  
  457.     // validate security token
  458.     //
  459.     sm_validate_security_token($submitted_token-1TRUE);
  460.  
  461.     if (isset($_FILES['attachfile']&&
  462.             $_FILES['attachfile']['tmp_name'&&
  463.             $_FILES['attachfile']['tmp_name'!= 'none'{
  464.         $AttachFailure saveAttachedFiles($session);
  465.     }
  466.     if (checkInput(false&& !isset($AttachFailure)) {
  467.         if ($mailbox == "All Folders"{
  468.             /* We entered compose via the search results page */
  469.             $mailbox 'INBOX'/* Send 'em to INBOX, that's safe enough */
  470.         }
  471.         $urlMailbox urlencode($mailbox);
  472.         if (isset($passed_id)) {
  473.             $passed_id 0;
  474.         }
  475.         /**
  476.          * Set $default_charset to correspond with the user's selection
  477.          * of language interface.
  478.          */
  479.         set_my_charset();
  480.         /**
  481.          * This is to change all newlines to \n
  482.          * We'll change them to \r\n later (in the sendMessage function)
  483.          */
  484.         $body str_replace("\r\n""\n"$body);
  485.         $body str_replace("\r""\n"$body);
  486.  
  487.         /**
  488.          * Rewrap $body so that no line is bigger than $editor_size
  489.          * This should only really kick in the sqWordWrap function
  490.          * if the browser doesn't support "VIRTUAL" as the wrap type.
  491.          */
  492.         $body explode("\n"$body);
  493.         $newBody '';
  494.         foreach ($body as $line{
  495.             if$line <> '-- ' {
  496.                 $line rtrim($line);
  497.             }
  498.             if (sq_strlen($line$default_charset<= $editor_size 1{
  499.                 $newBody .= $line "\n";
  500.             else {
  501.                 sqWordWrap($line$editor_size$default_charset);
  502.                 $newBody .= $line "\n";
  503.  
  504.             }
  505.  
  506.         }
  507.         $body $newBody;
  508.  
  509.         $Result deliverMessage($composeMessage);
  510.         do_hook('compose_send_after'$Result$composeMessage);
  511.         if ($Result{
  512.             showInputForm($session);
  513.             exit();
  514.         }
  515.  
  516.         /* if it is resumed draft, delete draft message */
  517.         if isset($delete_draft)) {
  518.             Header("Location: $location/delete_message.php?mailbox=urlencode$draft_folder ).
  519.                     "&message=$delete_draft&sort=$sort&startMessage=1&mail_sent=yes&smtoken=sm_generate_security_token());
  520.             exit();
  521.         }
  522.         if ($compose_new_win == '1'{
  523.  
  524.             Header("Location: $location/compose.php?mail_sent=yes");
  525.         }
  526.         else {
  527.             global $return_to_message_after_reply;
  528.             if (($action === 'reply' || $action === 'reply_all' || $action === 'forward' || $action === 'forward_as_attachment')
  529.              && $return_to_message_after_reply)
  530.                 Header("Location: $location/read_body.php?passed_id=$passed_id&mailbox=$urlMailbox&sort=$sort".
  531.                         "&startMessage=$startMessage");
  532.             else
  533.                 Header("Location: $location/right_main.php?mailbox=$urlMailbox&sort=$sort".
  534.                         "&startMessage=$startMessage");
  535.         }
  536.     else {
  537.         if ($compose_new_win == '1'{
  538.             compose_Header($color$mailbox);
  539.         }
  540.         else {
  541.             displayPageHeader($color$mailbox);
  542.         }
  543.         if (isset($AttachFailure)) {
  544.             plain_error_message(_("Could not move/copy file. File not attached"),
  545.                     $color);
  546.         }
  547.         checkInput(true);
  548.         showInputForm($session);
  549.         /* sqimap_logout($imapConnection); */
  550.     }
  551. elseif (isset($html_addr_search_done)) {
  552.  
  553.     // validate security token
  554.     //
  555.     sm_validate_security_token($submitted_token-1TRUE);
  556.  
  557.     if ($compose_new_win == '1'{
  558.         compose_Header($color$mailbox);
  559.     }
  560.     else {
  561.         displayPageHeader($color$mailbox);
  562.     }
  563.  
  564.     if (isset($send_to_search&& is_array($send_to_search)) {
  565.         foreach ($send_to_search as $k => $v{
  566.             if (substr($k01== 'T'{
  567.                 if ($send_to{
  568.                     $send_to .= ', ';
  569.                 }
  570.                 $send_to .= $v;
  571.             }
  572.             elseif (substr($k01== 'C'{
  573.                 if ($send_to_cc{
  574.                     $send_to_cc .= ', ';
  575.                 }
  576.                 $send_to_cc .= $v;
  577.             }
  578.             elseif (substr($k01== 'B'{
  579.                 if ($send_to_bcc{
  580.                     $send_to_bcc .= ', ';
  581.                 }
  582.                 $send_to_bcc .= $v;
  583.             }
  584.         }
  585.     }
  586.     showInputForm($session);
  587. elseif (isset($html_addr_search)) {
  588.     if (isset($_FILES['attachfile']&&
  589.             $_FILES['attachfile']['tmp_name'&&
  590.             $_FILES['attachfile']['tmp_name'!= 'none'{
  591.         if(saveAttachedFiles($session)) {
  592.             plain_error_message(_("Could not move/copy file. File not attached")$color);
  593.         }
  594.     }
  595.     /*
  596.      * I am using an include so as to elminiate an extra unnecessary
  597.      * click.  If you can think of a better way, please implement it.
  598.      */
  599.     include_once('./addrbook_search_html.php');
  600. elseif (isset($attach)) {
  601.  
  602.     // validate security token
  603.     //
  604.     sm_validate_security_token($submitted_token-1TRUE);
  605.  
  606.     if (saveAttachedFiles($session)) {
  607.         plain_error_message(_("Could not move/copy file. File not attached")$color);
  608.     }
  609.     if ($compose_new_win == '1'{
  610.         compose_Header($color$mailbox);
  611.     else {
  612.         displayPageHeader($color$mailbox);
  613.     }
  614.     showInputForm($session);
  615. }
  616. elseif (isset($sigappend)) {
  617.  
  618.     // validate security token
  619.     //
  620.     sm_validate_security_token($submitted_token-1TRUE);
  621.  
  622.     $signature $idents[$identity]['signature'];
  623.     
  624.     $body .= "\n\n".($prefix_sig==true"-- \n":'').$signature;
  625.     if ($compose_new_win == '1'{
  626.         compose_Header($color$mailbox);
  627.     else {
  628.         displayPageHeader($color$mailbox);
  629.     }
  630.     showInputForm($session);
  631. elseif (isset($do_delete)) {
  632.  
  633.     // validate security token
  634.     //
  635.     sm_validate_security_token($submitted_token-1TRUE);
  636.  
  637.     if ($compose_new_win == '1'{
  638.         compose_Header($color$mailbox);
  639.     else {
  640.         displayPageHeader($color$mailbox);
  641.     }
  642.  
  643.     if (isset($delete&& is_array($delete)) {
  644.         foreach($delete as $index{
  645.             if (!empty($composeMessage->entities&& isset($composeMessage->entities[$index])) {
  646.                 $composeMessage->entities[$index]->purgeAttachments();
  647.                 // FIXME: one person reported that unset() didn't do anything at all here, so this is a work-around... but it triggers PHP notices if the unset() doesn't work, which should be fixed... but bigger question is if unset() doesn't work here, what about everywhere else?  Anyway, uncomment this if you think you need it
  648.                 //$composeMessage->entities[$index] = NULL;
  649.                 unset ($composeMessage->entities[$index]);
  650.             }
  651.         }
  652.         $new_entities array();
  653.         foreach ($composeMessage->entities as $entity{
  654.             $new_entities[$entity;
  655.         }
  656.         $composeMessage->entities $new_entities;
  657.     }
  658.     showInputForm($session);
  659. else {
  660.     /*
  661.      * This handles the default case as well as the error case
  662.      * (they had the same code) --> if (isset($smtpErrors))
  663.      */
  664.  
  665.     if ($compose_new_win == '1'{
  666.         compose_Header($color$mailbox);
  667.     else {
  668.         displayPageHeader($color$mailbox);
  669.     }
  670.  
  671.     $newmail true;
  672.  
  673.     if (!isset($passed_ent_id)) {
  674.         $passed_ent_id '';
  675.     }
  676.     if (!isset($passed_id)) {
  677.         $passed_id '';
  678.     }
  679.     if (!isset($mailbox)) {
  680.         $mailbox '';
  681.     }
  682.     if (!isset($action)) {
  683.         $action '';
  684.     }
  685.  
  686.     $values newMail($mailbox,$passed_id,$passed_ent_id$action$session);
  687.  
  688.     // forward as attachment - subject is in the message in session
  689.     //
  690.     if (sqgetGlobalVar('forward_as_attachment_init'$forward_as_attachment_initSQ_GET)
  691.      && $forward_as_attachment_init)
  692.         $subject $composeMessage->rfc822_header->subject;
  693.  
  694.     /* in case the origin is not read_body.php */
  695.     if (isset($send_to)) {
  696.         $values['send_to'$send_to;
  697.     }
  698.     if (isset($send_to_cc)) {
  699.         $values['send_to_cc'$send_to_cc;
  700.     }
  701.     if (isset($send_to_bcc)) {
  702.         $values['send_to_bcc'$send_to_bcc;
  703.     }
  704.     if (isset($subject)) {
  705.         $values['subject'$subject;
  706.     }
  707.     if (isset($mailprio)) {
  708.         $values['mailprio'$mailprio;
  709.     }
  710.     if (isset($orig_identity)) {
  711.         $values['identity'$orig_identity;
  712.     }
  713.     showInputForm($session$values);
  714. }
  715.  
  716. exit();
  717.  
  718. /**************** Only function definitions go below *************/
  719.  
  720.  
  721. /* This function is used when not sending or adding attachments */
  722. function newMail ($mailbox=''$passed_id=''$passed_ent_id=''$action=''$session=''{
  723.     global $editor_size$default_use_priority$body$idents,
  724.         $use_signature$composesession$data_dir$username,
  725.         $username$key$imapServerAddress$imapPort$imap_stream_options,
  726.         $composeMessage$body_quote$strip_sigs$do_not_reply_to_self;
  727.     global $languages$squirrelmail_language$default_charset$compose_messages;
  728.  
  729.     /*
  730.      * Set $default_charset to correspond with the user's selection
  731.      * of language interface. $default_charset global is not correct,
  732.      * if message is composed in new window.
  733.      */
  734.     set_my_charset();
  735.  
  736.     $send_to $send_to_cc $send_to_bcc $subject $identity '';
  737.     $mailprio 3;
  738.  
  739.     if ($passed_id{
  740.         $imapConnection sqimap_login($username$key$imapServerAddress,
  741.                 $imapPort0$imap_stream_options);
  742.  
  743.         sqimap_mailbox_select($imapConnection$mailbox);
  744.         $message sqimap_get_message($imapConnection$passed_id$mailbox);
  745.  
  746.         $body '';
  747.         if ($passed_ent_id{
  748.             /* redefine the messsage in case of message/rfc822 */
  749.             $message $message->getEntity($passed_ent_id);
  750.             /* message is an entity which contains the envelope and type0=message
  751.              * and type1=rfc822. The actual entities are childs from
  752.              * $message->entities[0]. That's where the encoding and is located
  753.              */
  754.  
  755.             $entities $message->entities[0]->findDisplayEntity
  756.                 (array()$alt_order array('text/plain'));
  757.             if (!count($entities)) {
  758.                 $entities $message->entities[0]->findDisplayEntity
  759.                     (array()$alt_order array('text/plain','text/html'));
  760.             }
  761.             $orig_header $message->rfc822_header/* here is the envelope located */
  762.             /* redefine the message for picking up the attachments */
  763.             $message $message->entities[0];
  764.  
  765.         else {
  766.             $entities $message->findDisplayEntity (array()$alt_order array('text/plain'));
  767.             if (!count($entities)) {
  768.                 $entities $message->findDisplayEntity (array()$alt_order array('text/plain','text/html'));
  769.             }
  770.             $orig_header $message->rfc822_header;
  771.         }
  772.  
  773.         $encoding $message->header->encoding;
  774.         $type0 $message->type0;
  775.         $type1 $message->type1;
  776.         foreach ($entities as $ent{
  777.             $unencoded_bodypart mime_fetch_body($imapConnection$passed_id$ent);
  778.             $body_part_entity $message->getEntity($ent);
  779.             $bodypart decodeBody($unencoded_bodypart,
  780.                     $body_part_entity->header->encoding);
  781.  
  782.             // type of the actual entity should be here;
  783.             // fall back to parent only if not
  784.             if (!empty($body_part_entity->type0))
  785.                 $type0 $body_part_entity->type0;
  786.             if (!empty($body_part_entity->type1))
  787.                 $type1 $body_part_entity->type1;
  788.  
  789.             if ($type1 == 'html'{
  790.                 $bodypart str_replace("\n"' '$bodypart);
  791.                 $bodypart preg_replace(array('/<\/?p>/i','/<div><\/div>/i','/<br\s*(\/)*>/i','/<\/?div>/i')"\n"$bodypart);
  792.                 $bodypart str_replace(array('&nbsp;','&gt;','&lt;'),array(' ','>','<'),$bodypart);
  793.                 $bodypart strip_tags($bodypart);
  794.             }
  795.             if (isset($languages[$squirrelmail_language]['XTRA_CODE']&&
  796.                     function_exists($languages[$squirrelmail_language]['XTRA_CODE'])) {
  797.                 if (mb_detect_encoding($bodypart!= 'ASCII'{
  798.                     $bodypart $languages[$squirrelmail_language]['XTRA_CODE']('decode'$bodypart);
  799.                 }
  800.             }
  801.  
  802.             // charset encoding in compose form stuff
  803.             if (isset($body_part_entity->header->parameters['charset'])) {
  804.                 $actual $body_part_entity->header->parameters['charset'];
  805.             else {
  806.                 $actual 'us-ascii';
  807.             }
  808.  
  809.             if $actual && is_conversion_safe($actual&& $actual != $default_charset){
  810.                 $bodypart charset_convert($actual,$bodypart,$default_charset,false);
  811.             }
  812.             // end of charset encoding in compose
  813.  
  814.             $body .= $bodypart;
  815.         }
  816.         if ($default_use_priority{
  817.             $mailprio substr($orig_header->priority,0,1);
  818.             if (!$mailprio{
  819.                 $mailprio 3;
  820.             }
  821.         else {
  822.             $mailprio '';
  823.         }
  824.  
  825.         $identity '';
  826.         $from_o $orig_header->from;
  827.         if (is_array($from_o)) {
  828.             if (isset($from_o[0])) {
  829.                 $from_o $from_o[0];
  830.             }
  831.         }
  832.         if (is_object($from_o)) {
  833.             $orig_from $from_o->getAddress();
  834.         else {
  835.             $orig_from '';
  836.         }
  837.  
  838.         $identities array();
  839.         if (count($idents1{
  840.             foreach($idents as $nr=>$data{
  841.                 $enc_from_name '"'.$data['full_name'].'" <'$data['email_address'].'>';
  842.                 $identities[$enc_from_name;
  843.             }
  844.  
  845.             $identity_match $orig_header->findAddress($identities);
  846.             if ($identity_match !== FALSE{
  847.                 $identity $identity_match;
  848.             }
  849.         }
  850.  
  851.         switch ($action{
  852.             case ('draft'):
  853.                 $use_signature FALSE;
  854.                 $composeMessage->rfc822_header $orig_header;
  855.                 $send_to decodeHeader($orig_header->getAddr_s('to'),false,false,true);
  856.                 $send_to_cc decodeHeader($orig_header->getAddr_s('cc'),false,false,true);
  857.                 $send_to_bcc decodeHeader($orig_header->getAddr_s('bcc'),false,false,true);
  858.                 $identity 0;
  859.                 if (count($idents1{
  860.                     $identity_match $orig_header->findAddress($identitiesTRUE);
  861.                     if ($identity_match !== FALSE{
  862.                         $identity $identity_match;
  863.                     }
  864.                 }
  865.                 $subject decodeHeader($orig_header->subject,false,false,true);
  866.                 /* remember the references and in-reply-to headers in case of an reply */
  867.                 $composeMessage->rfc822_header->more_headers['References'$orig_header->references;
  868.                 $composeMessage->rfc822_header->more_headers['In-Reply-To'$orig_header->in_reply_to;
  869.                 $body_ary explode("\n"$body);
  870.                 $cnt count($body_ary;
  871.                 $body '';
  872.                 for ($i=0$i $cnt$i++{
  873.                     if (!preg_match('/^[>\s]*$/'$body_ary[$i])  || !$body_ary[$i]{
  874.                         sqWordWrap($body_ary[$i]$editor_size$default_charset );
  875.                         $body .= $body_ary[$i"\n";
  876.                     }
  877.                     unset($body_ary[$i]);
  878.                 }
  879.                 sqUnWordWrap($body);
  880.                 $composeMessage getAttachments($message$composeMessage$passed_id$entities$imapConnection);
  881.                 if (!empty($orig_header->x_sm_flag_reply))
  882.                     $composeMessage->rfc822_header->more_headers['X-SM-Flag-Reply'$orig_header->x_sm_flag_reply;
  883. //TODO: completely unclear if should be using $compose_session instead of $session below
  884.                 $compose_messages[$session$composeMessage;
  885.                 sqsession_register($compose_messages,'compose_messages');
  886.                 break;
  887.             case ('edit_as_new'):
  888.                 $send_to decodeHeader($orig_header->getAddr_s('to'),false,false,true);
  889.                 $send_to_cc decodeHeader($orig_header->getAddr_s('cc'),false,false,true);
  890.                 $send_to_bcc decodeHeader($orig_header->getAddr_s('bcc'),false,false,true);
  891.                 $subject decodeHeader($orig_header->subject,false,false,true);
  892.                 $mailprio $orig_header->priority;
  893.                 $orig_from '';
  894.                 $composeMessage getAttachments($message$composeMessage$passed_id$entities$imapConnection);
  895.                 sqUnWordWrap($body);
  896.                 break;
  897.             case ('forward'):
  898.                 $send_to '';
  899.                 $subject decodeHeader($orig_header->subject,false,false,true);
  900.                 if ((substr(strtolower($subject)04!= 'fwd:'&&
  901.                     (substr(strtolower($subject)05!= '[fwd:'&&
  902.                     (substr(strtolower($subject)06!= '[ fwd:')) {
  903.                     $subject '[Fwd: ' $subject ']';
  904.                 }
  905.                 $body getforwardHeader($orig_header$body;
  906.                 $composeMessage getAttachments($message$composeMessage$passed_id$entities$imapConnection);
  907.                 $body "\n" $body;
  908.                 break;
  909.             case ('forward_as_attachment'):
  910.                 $subject decodeHeader($orig_header->subject,false,false,true);
  911.                 $subject trim($subject);
  912.                 if (substr(strtolower($subject)04!= 'fwd:'{
  913.                     $subject 'Fwd: ' $subject;
  914.                 }
  915.                 $composeMessage getMessage_RFC822_Attachment($message$composeMessage$passed_id$passed_ent_id$imapConnection);
  916.                 $body '';
  917.                 break;
  918.             case ('reply_all'):
  919.                 if(isset($orig_header->mail_followup_to&& $orig_header->mail_followup_to{
  920.                     $send_to $orig_header->getAddr_s('mail_followup_to');
  921.                 else {
  922.                     $send_to_cc replyAllString($orig_header);
  923.                     $send_to_cc decodeHeader($send_to_cc,false,false,true);
  924.                     $send_to_cc str_replace('""''"'$send_to_cc);
  925.                 }
  926.             case ('reply'):
  927.                 if (!$send_to{
  928.                     $send_to $orig_header->reply_to;
  929.                     if (is_array($send_to&& count($send_to)) {
  930.                         $send_to $orig_header->getAddr_s('reply_to'','FALSETRUE);
  931.                     else if (is_object($send_to)) /* unneccesarry, just for failsafe purpose */
  932.                         $send_to $orig_header->getAddr_s('reply_to'','FALSETRUE);
  933.                     else {
  934.                         $send_to $orig_header->getAddr_s('from'','FALSETRUE);
  935.                     }
  936.                 }
  937.                 $send_to decodeHeader($send_to,false,false,true);
  938.                 $send_to str_replace('""''"'$send_to);
  939.  
  940.  
  941.                 // If user doesn't want replies to her own messages
  942.                 // going back to herself (instead send again to the
  943.                 // original recipient of the message being replied to),
  944.                 // then iterate through identities, checking if the TO
  945.                 // field is one of them (if the reply is to ourselves)
  946.                 //
  947.                 // Note we don't bother if the original message doesn't
  948.                 // have anything in the TO field itself (because that's
  949.                 // what we use if we change the recipient to be that of
  950.                 // the previous message)
  951.                 //
  952.                 if ($do_not_reply_to_self && !empty($orig_header->to)) {
  953.  
  954.                     $orig_to '';
  955.  
  956.                     foreach($idents as $id{
  957.  
  958.                         if (!empty($id['email_address'])
  959.                          && strpos($send_to$id['email_address']!== FALSE{
  960.  
  961.                             // if this is a reply-all, the original recipient
  962.                             // is already in the CC field, so we can just blank
  963.                             // the recipient (TO field) (as long as the CC field
  964.                             // isn't empty that is)... but then move the CC into
  965.                             // the TO, so TO isn't empty
  966.                             //
  967.                             if ($action == 'reply_all' && !empty($send_to_cc)) {
  968.                                 $orig_to $send_to_cc;
  969.                                 $send_to_cc '';
  970.                                 break;
  971.                             }
  972.  
  973.                             $orig_to $orig_header->to;
  974.                             if (is_array($orig_to&& count($orig_to)) {
  975.                                 $orig_to $orig_header->getAddr_s('to'','FALSETRUE);
  976.                             else if (is_object($orig_to)) /* unneccesarry, just for failsafe purpose */
  977.                                 $orig_to $orig_header->getAddr_s('to'','FALSETRUE);
  978.                             else {
  979.                                 $orig_to '';
  980.                             }
  981.                             $orig_to decodeHeader($orig_to,false,false,true);
  982.                             $orig_to str_replace('""''"'$orig_to);
  983.  
  984.                             break;
  985.                         }
  986.                     }
  987.  
  988.                     // if the reply was addressed back to ourselves,
  989.                     // we will send it to the TO of the previous message
  990.                     //
  991.                     if (!empty($orig_to)) {
  992.  
  993.                         $send_to $orig_to;
  994.  
  995.                         // in this case, we also want to reset the FROM
  996.                         // identity as well (it should match the original
  997.                         // *FROM* header instead of TO or CC)
  998.                         //
  999.                         if (count($idents1{
  1000.                             $identity '';
  1001.                             foreach($idents as $i => $id{
  1002.                                 if (!empty($id['email_address'])
  1003.                                  && strpos($orig_from$id['email_address']!== FALSE{
  1004.                                     $identity $i;
  1005.                                     break;
  1006.                                 }
  1007.                             }
  1008.                         }
  1009.  
  1010.                     }
  1011.  
  1012.                 }
  1013.  
  1014.  
  1015.                 $subject decodeHeader($orig_header->subject,false,false,true);
  1016.                 $subject trim($subject);
  1017.                 if (substr(strtolower($subject)03!= 're:'{
  1018.                     $subject 'Re: ' $subject;
  1019.                 }
  1020.                 /* this corrects some wrapping/quoting problems on replies */
  1021.                 $rewrap_body explode("\n"$body);
  1022.                 $from (is_array($orig_header->from&& !empty($orig_header->from)) $orig_header->from[0$orig_header->from;
  1023.                 sqUnWordWrap($body);
  1024.                 $body '';
  1025.                 $cnt count($rewrap_body);
  1026.                 for ($i=0;$i<$cnt;$i++{
  1027.                     if ($strip_sigs && $rewrap_body[$i== '-- '{
  1028.                         break;
  1029.                     }
  1030.                     sqWordWrap($rewrap_body[$i]$editor_size$default_charset);
  1031.                     if (preg_match("/^(>+)/"$rewrap_body[$i]$matches)) {
  1032.                         $gt $matches[1];
  1033.                         $body .= $body_quote str_replace("\n""\n" $body_quote
  1034.                               . "$gt "rtrim($rewrap_body[$i])) ."\n";
  1035.                     else {
  1036.                         $body .= $body_quote (!empty($body_quote' ' ''str_replace("\n""\n" $body_quote (!empty($body_quote' ' '')rtrim($rewrap_body[$i])) "\n";
  1037.                     }
  1038.                     unset($rewrap_body[$i]);
  1039.                 }
  1040.                 $body getReplyCitation($from $orig_header->date$body;
  1041.                 $composeMessage->reply_rfc822_header $orig_header;
  1042.  
  1043.                 break;
  1044.             default:
  1045.                 break;
  1046.         }
  1047.         session_write_close();
  1048.         sqimap_logout($imapConnection);
  1049.     }
  1050.     $ret array'send_to' => $send_to,
  1051.             'send_to_cc' => $send_to_cc,
  1052.             'send_to_bcc' => $send_to_bcc,
  1053.             'subject' => $subject,
  1054.             'mailprio' => $mailprio,
  1055.             'body' => $body,
  1056.             'identity' => $identity );
  1057.  
  1058.     return ($ret);
  1059. /* function newMail() */
  1060.  
  1061. function getAttachments($message&$composeMessage$passed_id$entities$imapConnection{
  1062.     global $attachment_dir$username$data_dir$squirrelmail_language$languages;
  1063.     $hashed_attachment_dir getHashedDir($username$attachment_dir);
  1064.     if (!count($message->entities||
  1065.             ($message->type0 == 'message' && $message->type1 == 'rfc822')) {
  1066.         if !in_array($message->entity_id$entities&& $message->entity_id{
  1067.             switch ($message->type0{
  1068.                 case 'message':
  1069.                     if ($message->type1 == 'rfc822'{
  1070.                         $filename $message->rfc822_header->subject;
  1071.                         if ($filename == ""{
  1072.                             $filename "untitled-".$message->entity_id;
  1073.                         }
  1074.                         $filename .= '.eml';
  1075.                     else {
  1076.                         $filename $message->getFilename();
  1077.                     }
  1078.                     break;
  1079.                 default:
  1080.                     if (!$message->mime_header/* temporary hack */
  1081.                         $message->mime_header $message->header;
  1082.                     }
  1083.                     $filename $message->getFilename();
  1084.                     break;
  1085.             }
  1086.  
  1087.             $filename decodeHeader($filenamefalsefalsetrue);
  1088.             if (isset($languages[$squirrelmail_language]['XTRA_CODE']&&
  1089.                     function_exists($languages[$squirrelmail_language]['XTRA_CODE'])) {
  1090.                 $filename =  $languages[$squirrelmail_language]['XTRA_CODE']('encode'$filename);
  1091.             }
  1092.             $localfilename GenerateRandomString(32''7);
  1093.             $full_localfilename "$hashed_attachment_dir/$localfilename";
  1094.             while (file_exists($full_localfilename)) {
  1095.                 $localfilename GenerateRandomString(32''7);
  1096.                 $full_localfilename "$hashed_attachment_dir/$localfilename";
  1097.             }
  1098.             $fp fopen ("$hashed_attachment_dir/$localfilename"'wb');
  1099.  
  1100.             $message->att_local_name $localfilename;
  1101.  
  1102.             $composeMessage->initAttachment($message->type0.'/'.$message->type1,$filename,
  1103.                     $localfilename);
  1104.  
  1105.             /* Write Attachment to file 
  1106.                The function mime_print_body_lines writes directly to the 
  1107.                provided resource $fp. That prohibits large memory consumption in
  1108.                case of forwarding mail with large attachments.
  1109.             */
  1110.             mime_print_body_lines ($imapConnection$passed_id$message->entity_id$message->header->encoding$fp);
  1111.             fclose ($fp);
  1112.         }
  1113.     else {
  1114.         for ($i=0$entCount=count($message->entities)$i<$entCount;$i++{
  1115.             $composeMessage=getAttachments($message->entities[$i]$composeMessage$passed_id$entities$imapConnection);
  1116.         }
  1117.     }
  1118.     return $composeMessage;
  1119. }
  1120.  
  1121. function getMessage_RFC822_Attachment($message$composeMessage$passed_id,
  1122.         $passed_ent_id=''$imapConnection{
  1123.     global $attachment_dir$username$data_dir$uid_support;
  1124.     $hashed_attachment_dir getHashedDir($username$attachment_dir);
  1125.     if (!$passed_ent_id{
  1126.         $body_a sqimap_run_command($imapConnection,
  1127.                 'FETCH '.$passed_id.' RFC822',
  1128.                 TRUE$response$readmessage,
  1129.                 $uid_support);
  1130.     else {
  1131.         $body_a sqimap_run_command($imapConnection,
  1132.                 'FETCH '.$passed_id.' BODY['.$passed_ent_id.']',
  1133.                 TRUE$response$readmessage$uid_support);
  1134.         $message $message->parent;
  1135.     }
  1136.     if ($response == 'OK'{
  1137.         $subject encodeHeader($message->rfc822_header->subject);
  1138.         array_shift($body_a);
  1139.         array_pop($body_a);
  1140.         $body implode(''$body_a"\r\n";
  1141.  
  1142.         $localfilename GenerateRandomString(32'FILE'7);
  1143.         $full_localfilename "$hashed_attachment_dir/$localfilename";
  1144.  
  1145.         $fp fopen($full_localfilename'w');
  1146.         fwrite ($fp$body);
  1147.         fclose($fp);
  1148.         $composeMessage->initAttachment('message/rfc822',$subject.'.eml',
  1149.                 $localfilename);
  1150.     }
  1151.     return $composeMessage;
  1152. }
  1153.  
  1154. function showInputForm ($session$values=false{
  1155.     global $send_to$send_to_cc$body$startMessage$attachments,
  1156.         $session_expired,
  1157.         $passed_body$color$use_signature$signature$prefix_sig,
  1158.         $editor_size$editor_height$subject$newmail,
  1159.         $use_javascript_addr_book$send_to_bcc$passed_id$mailbox,
  1160.         $from_htmladdr_search$location_of_buttons$attachment_dir,
  1161.         $username$data_dir$identity$idents$draft_id$delete_draft,
  1162.         $mailprio$default_use_mdn$mdn_user_support$compose_new_win,
  1163.         $saved_draft$mail_sent$sig_first$edit_as_new$action,
  1164.         $username$composesession$default_charset$composeMessage,
  1165.         $javascript_on$compose_onsubmit;
  1166.  
  1167.     if ($javascript_on)
  1168.         $onfocus ' onfocus="alreadyFocused=true;"';
  1169.     else
  1170.         $onfocus '';
  1171.     
  1172.     if ($values{
  1173.         $send_to $values['send_to'];
  1174.         $send_to_cc $values['send_to_cc'];
  1175.         $send_to_bcc $values['send_to_bcc'];
  1176.         $subject $values['subject'];
  1177.         $mailprio $values['mailprio'];
  1178.         $body $values['body'];
  1179.         $identity = (int) $values['identity'];
  1180.     else {
  1181.         $send_to decodeHeader($send_totruefalse);
  1182.         $send_to_cc decodeHeader($send_to_cctruefalse);
  1183.         $send_to_bcc decodeHeader($send_to_bcctruefalse);
  1184.     }
  1185.  
  1186.     if ($use_javascript_addr_book{
  1187.         echo "\n"'<script language="JavaScript">'."\n<!--\n" .
  1188.             'function open_abook() { ' "\n" .
  1189.             '  var nwin = window.open("addrbook_popup.php","abookpopup",' .
  1190.             '"width=670,height=300,resizable=yes,scrollbars=yes");' "\n" .
  1191.             '  if((!nwin.opener) && (document.windows != null))' "\n" .
  1192.             '    nwin.opener = document.windows;' "\n" .
  1193.             "}\n" .
  1194.             "// -->\n</script>\n\n";
  1195.     }
  1196.  
  1197.     echo "\n" '<form name="compose" action="compose.php" method="post" ' .
  1198.         'enctype="multipart/form-data"';
  1199.  
  1200.     $compose_onsubmit array();
  1201.     do_hook('compose_form');
  1202.  
  1203.     // Plugins that use compose_form hook can add an array entry
  1204.     // to the globally scoped $compose_onsubmit; we add them up
  1205.     // here and format the form tag's full onsubmit handler.
  1206.     // Each plugin should use "return false" if they need to
  1207.     // stop form submission but otherwise should NOT use "return
  1208.     // true" to give other plugins the chance to do what they need
  1209.     // to do; SquirrelMail itself will add the final "return true".
  1210.     // Onsubmit text is enclosed inside of double quotes, so plugins
  1211.     // need to quote accordingly.
  1212.     //
  1213.     // Also, plugin authors should try to retain compatibility with
  1214.     // the Compose Extras plugin by resetting its compose submit
  1215.     // counter when preventing form submit.  Use this code:
  1216.     // if (your-code-here) { submit_count = 0; return false; }
  1217.     //
  1218.     if ($javascript_on{
  1219.         if (empty($compose_onsubmit))
  1220.             $compose_onsubmit array();
  1221.         else if (!is_array($compose_onsubmit))
  1222.             $compose_onsubmit array($compose_onsubmit);
  1223.  
  1224.         $onsubmit_text '';
  1225.         foreach ($compose_onsubmit as $text{
  1226.             $text trim($text);
  1227.             if (!empty($text)) {
  1228.                 if (substr($text-1!= ';' && substr($text-1!= '}')
  1229.                     $text .= '; ';
  1230.                 $onsubmit_text .= $text;
  1231.             }
  1232.         }
  1233.  
  1234.         if (!empty($onsubmit_text))
  1235.             echo ' onsubmit="' $onsubmit_text ' return true;"';
  1236.     }
  1237.  
  1238.     echo ">\n";
  1239.  
  1240.     echo addHidden('smtoken'sm_generate_security_token());
  1241.     echo addHidden('startMessage'$startMessage);
  1242.  
  1243.     if ($action == 'draft'{
  1244.         echo addHidden('delete_draft'$passed_id);
  1245.     }
  1246.     if (isset($delete_draft)) {
  1247.         echo addHidden('delete_draft'$delete_draft);
  1248.     }
  1249.     if (isset($session)) {
  1250.         echo addHidden('session'$session);
  1251.     }
  1252.  
  1253.     // NB: passed_id is set to empty string elsewhere, so this ALWAYS gets added, which is better anyway IMO
  1254.     if (isset($passed_id)) {
  1255.         echo addHidden('passed_id'$passed_id);
  1256.     }
  1257.  
  1258.     if ($saved_draft == 'yes'{
  1259.         echo '<br /><center><b>'_("Your draft has been saved.").'</center></b>';
  1260.     }
  1261.     if ($mail_sent == 'yes'{
  1262.         echo '<br /><center><b>'_("Your mail has been sent.").'</center></b>';
  1263.     }
  1264.     if ($compose_new_win == '1'{
  1265.         echo '<table align="center" bgcolor="'.$color[0].'" width="100%" border="0">'."\n" .
  1266.             '   <tr><td></td>'.html_tag'td''''right' ).
  1267.             '<input type="button" name="Close" onclick="return self.close()" value="'.
  1268.             _("Close").'" /></td></tr>'."\n";
  1269.     else {
  1270.         echo '<table align="center" cellspacing="0" border="0">' "\n";
  1271.     }
  1272.     if ($location_of_buttons == 'top'{
  1273.         showComposeButtonRow();
  1274.     }
  1275.     
  1276.     /* display select list for identities */
  1277.     if (count($idents1{
  1278.         echo '   <tr>' "\n" .
  1279.             html_tag'td''''right'$color[4]'width="10%"' .
  1280.             _("From:"'</td>' "\n" .
  1281.             html_tag'td''''left'$color[4]'width="90%"' .
  1282.             '         <select name="identity">' "\n";
  1283.  
  1284.         foreach($idents as $nr => $data{
  1285.             echo '<option value="' $nr '"';
  1286.             if (isset($identity&& $identity == $nr{
  1287.                 echo ' selected="selected"';
  1288.             }
  1289.             echo '>' sm_encode_html_special_chars(
  1290.                     $data['full_name'' <' .
  1291.                     $data['email_address''>'.
  1292.                 "</option>\n";
  1293.         }
  1294.  
  1295.         echo '</select>' "\n" .
  1296.             '      </td>' "\n" .
  1297.             '   </tr>' "\n";
  1298.     }
  1299.  
  1300.     echo '   <tr>' "\n" .
  1301.         html_tag'td''''right'$color[4]'width="10%"' .
  1302.         _("To:"'</td>' "\n" .
  1303.         html_tag'td''''left'$color[4]'width="90%"' .
  1304.         substr(addInput('send_to'$send_to60)0-3)$onfocus ' /><br />' "\n" .
  1305.         '      </td>' "\n" .
  1306.         '   </tr>' "\n" .
  1307.         '   <tr>' "\n" .
  1308.         html_tag'td''''right'$color[4.
  1309.         _("Cc:"'</td>' "\n" .
  1310.         html_tag'td''''left'$color[4.
  1311.         substr(addInput('send_to_cc'$send_to_cc60)0-3)$onfocus ' /><br />' "\n" .
  1312.         '      </td>' "\n" .
  1313.         '   </tr>' "\n" .
  1314.         '   <tr>' "\n" .
  1315.         html_tag'td''''right'$color[4.
  1316.         _("Bcc:"'</td>' "\n" .
  1317.         html_tag'td''''left'$color[4.
  1318.         substr(addInput('send_to_bcc'$send_to_bcc60)0-3)$onfocus ' /><br />' "\n" .
  1319.         '      </td>' "\n" .
  1320.         '   </tr>' "\n" .
  1321.         '   <tr>' "\n" .
  1322.         html_tag'td''''right'$color[4.
  1323.         _("Subject:"'</td>' "\n" .
  1324.         html_tag'td''''left'$color[4"\n";
  1325.     echo '         '.substr(addInput('subject'$subject60)0-3)$onfocus .
  1326.         ' />      </td>' "\n" .
  1327.         '   </tr>' "\n\n";
  1328.  
  1329.     if ($location_of_buttons == 'between'{
  1330.         showComposeButtonRow();
  1331.     }
  1332.  
  1333.     /* why this distinction? */
  1334.     if ($compose_new_win == '1'{
  1335.         echo '   <tr>' "\n" .
  1336.             '      <td bgcolor="' $color[0'" colspan="2" align="center">' "\n" .
  1337.             '         <textarea name="body" id="body" rows="' . (int)$editor_height .
  1338.             '" cols="' . (int)$editor_size '" wrap="virtual"' $onfocus ">\n";
  1339.     }
  1340.     else {
  1341.         echo '   <tr>' "\n" .
  1342.             '      <td bgcolor="' $color[4'" colspan="2">' "\n" .
  1343.             '         &nbsp;&nbsp;<textarea name="body" id="body" rows="' . (int)$editor_height .
  1344.             '" cols="' . (int)$editor_size '" wrap="virtual"' $onfocus ">\n";
  1345.     }
  1346.  
  1347.     if ($use_signature == true && $newmail == true && !isset($from_htmladdr_search)) {
  1348.         $signature $idents[$identity]['signature'];
  1349.  
  1350.         if ($sig_first == '1'{
  1351.             if ($default_charset == 'iso-2022-jp'{
  1352.                 echo "\n\n".($prefix_sig==true"-- \n":'').mb_convert_encoding($signature'EUC-JP');
  1353.             else {
  1354.                 echo "\n\n".($prefix_sig==true"-- \n":'').decodeHeader($signature,false,false,true);
  1355.             }
  1356.             echo "\n\n".sm_encode_html_special_chars(decodeHeader($body,false,false,true));
  1357.         }
  1358.         else {
  1359.             echo "\n\n".sm_encode_html_special_chars(decodeHeader($body,false,false,true));
  1360.             if ($default_charset == 'iso-2022-jp'{
  1361.                 echo "\n\n".($prefix_sig==true"-- \n":'').mb_convert_encoding($signature'EUC-JP');
  1362.             }else{
  1363.                 echo "\n\n".($prefix_sig==true"-- \n":'').decodeHeader($signature,false,false,true);
  1364.             }
  1365.         }
  1366.     else {
  1367.         echo sm_encode_html_special_chars(decodeHeader($body,false,false,true));
  1368.     }
  1369.     echo '</textarea><br />' "\n" .
  1370.         '      </td>' "\n" .
  1371.         '   </tr>' "\n";
  1372.  
  1373.  
  1374.     if ($location_of_buttons == 'bottom'{
  1375.         showComposeButtonRow();
  1376.     else {
  1377.         echo '   <tr>' "\n" .
  1378.             html_tag'td''''right''''colspan="2"' "\n" .
  1379.             '         ' addSubmit(_("Send")'send').
  1380.             '         &nbsp;&nbsp;&nbsp;&nbsp;<br /><br />' "\n" .
  1381.             '      </td>' "\n" .
  1382.             '   </tr>' "\n";
  1383.     }
  1384.  
  1385.     // composeMessage can be empty when coming from a restored session
  1386.     if (is_object($composeMessage&& $composeMessage->entities
  1387.         $attach_array $composeMessage->entities;
  1388.     if ($session_expired && !empty($attachments&& is_array($attachments))
  1389.         $attach_array $attachments;
  1390.  
  1391.     /* This code is for attachments */
  1392.     if ((bool) ini_get('file_uploads')) {
  1393.  
  1394.         /* Calculate the max size for an uploaded file.
  1395.          * This is advisory for the user because we can't actually prevent
  1396.          * people to upload too large files. */
  1397.         $sizes array();
  1398.         /* php.ini vars which influence the max for uploads */
  1399.         $configvars array('post_max_size''memory_limit''upload_max_filesize');
  1400.         foreach($configvars as $var{
  1401.             /* skip 0 or empty values, and -1 which means 'unlimited' */
  1402.             if$size getByteSize(ini_get($var)) ) {
  1403.                 if $size != '-1' {
  1404.                     $sizes[$size;
  1405.                 }
  1406.             }
  1407.         }
  1408.  
  1409.         if(count($sizes0{
  1410.             $maxsize_text '(max.&nbsp;' show_readable_sizemin$sizes ) ) ')';
  1411.             $maxsize_input addHidden('MAX_FILE_SIZE'min$sizes ));
  1412.         else {
  1413.             $maxsize_text $maxsize_input '';
  1414.         }
  1415.         echo '   <tr>' "\n" .
  1416.             '      <td colspan="2">' "\n" .
  1417.             '         <table width="100%" cellpadding="1" cellspacing="0" align="center"'.
  1418.             ' border="0" bgcolor="'.$color[9].'">' "\n" .
  1419.             '            <tr>' "\n" .
  1420.             '               <td>' "\n" .
  1421.             '                 <table width="100%" cellpadding="3" cellspacing="0" align="center"'.
  1422.             ' border="0">' "\n" .
  1423.             '                    <tr>' "\n" .
  1424.             html_tag'td''''right''''valign="middle"' .
  1425.             _("Attach:"'</td>' "\n" .
  1426.             html_tag'td''''left''''valign="middle"' .
  1427.             $maxsize_input .
  1428.             '                          <input name="attachfile" size="48" type="file" />' "\n" .
  1429.             '                          &nbsp;&nbsp;<input type="submit" name="attach"' .
  1430.             ' value="' _("Add".'" />' "\n" .
  1431.             $maxsize_text .
  1432.             '                       </td>' "\n" .
  1433.             '                    </tr>' "\n";
  1434.  
  1435.         $s_a array();
  1436.         global $username$attachment_dir;
  1437.         $hashed_attachment_dir getHashedDir($username$attachment_dir);
  1438.         if (!empty($attach_array)) {
  1439.             $attachment_count 0;
  1440.             foreach ($attach_array as $key => $attachment{
  1441.                 $attached_file $attachment->att_local_name;
  1442.                 if ($attachment->att_local_name || $attachment->body_part{
  1443.                     $attached_filename decodeHeader($attachment->mime_header->getParameter('name'));
  1444.                     $type $attachment->mime_header->type0.'/'.
  1445.                         $attachment->mime_header->type1;
  1446.  
  1447.                     $s_a['<table bgcolor="'.$color[0].
  1448.                         '" border="0"><tr><td>'.
  1449.                         addCheckBox('delete[]'FALSE$key'id="delete' . ++$attachment_count '"').
  1450.                         "</td><td><label for='delete" $attachment_count "'>\n" $attached_filename .
  1451.                         '</label></td><td><label for="delete' $attachment_count '">-</label></td><td><label for="delete' $attachment_count '"> ' $type '</label></td><td><label for="delete' $attachment_count '">('.
  1452.                         show_readable_sizefilesize$hashed_attachment_dir '/' $attached_file ) ) .
  1453.                         ')</label></td></tr></table>'."\n";
  1454.                 }
  1455.             }
  1456.         }
  1457.         if (count($s_a)) {
  1458.             foreach ($s_a as $s{
  1459.                 echo '<tr>' html_tag'td''''left'$color[0]'colspan="2"' $s .'</td></tr>';
  1460.             }
  1461.             echo '<tr><td colspan="2"><input type="submit" name="do_delete" value="' .
  1462.                 _("Delete selected attachments""\" />\n" .
  1463.                 '</td></tr>';
  1464.         }
  1465.         echo '                  </table>' "\n" .
  1466.             '               </td>' "\n" .
  1467.             '            </tr>' "\n" .
  1468.             '         </table>' "\n" .
  1469.             '      </td>' "\n" .
  1470.             '   </tr>' "\n";
  1471.     // End of file_uploads if-block
  1472.     /* End of attachment code */
  1473.     echo '</table>' "\n" .
  1474.         addHidden('username'$username).
  1475.         addHidden('smaction'$action).
  1476.         addHidden('mailbox'$mailbox);
  1477.     sqgetGlobalVar('QUERY_STRING'$queryStringSQ_SERVER);
  1478.     /*
  1479.        store the complete ComposeMessages array in a hidden input value
  1480.        so we can restore them in case of a session timeout.
  1481.      */
  1482.     echo addHidden('composesession'$composesession).
  1483.         addHidden('querystring'$queryString).
  1484.         (!empty($attach_array?
  1485.         addHidden('attachments'serialize($attach_array)) '').
  1486.         "</form>\n";
  1487.     if (!(bool) ini_get('file_uploads')) {
  1488.         /* File uploads are off, so we didn't show that part of the form.
  1489.            To avoid bogus bug reports, tell the user why. */
  1490.         echo '<p style="text-align:center">'
  1491.             . _("Because PHP file uploads are turned off, you can not attach files to this message. Please see your system administrator for details.")
  1492.             . "</p>\r\n";
  1493.     }
  1494.  
  1495.     do_hook('compose_bottom');
  1496.     echo '</body></html>' "\n";
  1497. }
  1498.  
  1499.  
  1500. function showComposeButtonRow({
  1501.     global $use_javascript_addr_book$save_as_draft,
  1502.         $default_use_priority$mailprio$default_use_mdn,
  1503.         $request_mdn$request_dr,
  1504.         $data_dir$username;
  1505.  
  1506.     echo '   <tr>' "\n" .
  1507.         '      <td></td>' "\n" .
  1508.         '      <td>' "\n";
  1509.     if ($default_use_priority{
  1510.         if(!isset($mailprio)) {
  1511.             $mailprio '3';
  1512.         }
  1513.         echo '          ' _("Priority".
  1514.             addSelect('mailprio'array(
  1515.                         '1' => _("High"),
  1516.                         '3' => _("Normal"),
  1517.                         '5' => _("Low") )$mailprioTRUE);
  1518.     }
  1519.     $mdn_user_support=getPref($data_dir$username'mdn_user_support',$default_use_mdn);
  1520.     if ($default_use_mdn{
  1521.         if ($mdn_user_support{
  1522.             echo '          ' _("Receipt".': '.
  1523.                 addCheckBox('request_mdn'$request_mdn == '1''1''id="request_mdn"''<label for="request_mdn">' _("On Read"'</label>' .
  1524.                 addCheckBox('request_dr',  $request_dr  == '1''1''id="request_dr"''<label for="request_dr">' _("On Delivery"'</label>';
  1525.         }
  1526.     }
  1527.  
  1528.     echo '      </td>' "\n" .
  1529.         '   </tr>' "\n" .
  1530.         '   <tr>'  "\n" .
  1531.         '      <td></td>' "\n" .
  1532.         '      <td>' "\n" .
  1533.         '         <input type="submit" name="sigappend" value="' _("Signature"'" />' "\n";
  1534.     if ($use_javascript_addr_book{
  1535.         echo "         <script language=\"JavaScript\"><!--\n document.write(\"".
  1536.             "            <input type=button value=\\\""._("Addresses").
  1537.             "\\\" onclick=\\\"javascript:open_abook();\\\" />\");".
  1538.             "            // --></script><noscript>\n".
  1539.             '            <input type="submit" name="html_addr_search" value="'.
  1540.             _("Addresses").'" />'.
  1541.             "         </noscript>\n";
  1542.     else {
  1543.         echo '         <input type="submit" name="html_addr_search" value="'.
  1544.             _("Addresses").'" />' "\n";
  1545.     }
  1546.  
  1547.     if ($save_as_draft{
  1548.         echo '         <input type="submit" name ="draft" value="' _("Save Draft""\" />\n";
  1549.     }
  1550.  
  1551.     echo '         <input type="submit" name="send" value="'_("Send"'" />' "\n";
  1552.     do_hook('compose_button_row');
  1553.  
  1554.     echo '      </td>' "\n" .
  1555.         '   </tr>' "\n\n";
  1556. }
  1557.  
  1558. function checkInput ($show{
  1559.     /*
  1560.      * I implemented the $show variable because the error messages
  1561.      * were getting sent before the page header.  So, I check once
  1562.      * using $show=false, and then when i'm ready to display the error
  1563.      * message, show=true
  1564.      */
  1565.     global $body$send_to$send_to_cc$send_to_bcc$subject$color;
  1566.  
  1567.     $send_to trim($send_to);
  1568.     $send_to_cc trim($send_to_cc);
  1569.     $send_to_bcc trim($send_to_bcc);
  1570.     if (empty($send_to&& empty($send_to_cc&& empty($send_to_bcc)) {
  1571.         if ($show{
  1572.             plain_error_message(_("You have not filled in the \"To:\" field.")$color);
  1573.         }
  1574.         return false;
  1575.     }
  1576.     return true;
  1577. /* function checkInput() */
  1578.  
  1579.  
  1580. /* True if FAILURE */
  1581. function saveAttachedFiles($session{
  1582.     global $_FILES$attachment_dir$username,
  1583.         $data_dir$composeMessage;
  1584.  
  1585.     /* get out of here if no file was attached at all */
  1586.     if (is_uploaded_file($_FILES['attachfile']['tmp_name']) ) {
  1587.         return true;
  1588.     }
  1589.  
  1590.     $hashed_attachment_dir getHashedDir($username$attachment_dir);
  1591.     $localfilename GenerateRandomString(32''7);
  1592.     $full_localfilename "$hashed_attachment_dir/$localfilename";
  1593.     while (file_exists($full_localfilename)) {
  1594.         $localfilename GenerateRandomString(32''7);
  1595.         $full_localfilename "$hashed_attachment_dir/$localfilename";
  1596.     }
  1597.  
  1598.     // FIXME: we SHOULD prefer move_uploaded_file over rename because
  1599.     // m_u_f works better with restricted PHP installs (safe_mode, open_basedir)
  1600.     if (!@rename($_FILES['attachfile']['tmp_name']$full_localfilename)) {
  1601.         if (!@move_uploaded_file($_FILES['attachfile']['tmp_name'],$full_localfilename)) {
  1602.             return true;
  1603.         }
  1604.     }
  1605.     $type strtolower($_FILES['attachfile']['type']);
  1606.     $name $_FILES['attachfile']['name'];
  1607.     $composeMessage->initAttachment($type$name$localfilename);
  1608. }
  1609.  
  1610. /* parse values like 8M and 2k into bytes */
  1611. function getByteSize($ini_size{
  1612.  
  1613.     if(!$ini_size{
  1614.         return FALSE;
  1615.     }
  1616.  
  1617.     $ini_size trim($ini_size);
  1618.  
  1619.     // if there's some kind of letter at the end of the string we need to multiply.
  1620.     if(!is_numeric(substr($ini_size-1))) {
  1621.  
  1622.         switch(strtoupper(substr($ini_size-1))) {
  1623.             case 'G':
  1624.                 $bytesize 1073741824;
  1625.                 break;
  1626.             case 'M':
  1627.                 $bytesize 1048576;
  1628.                 break;
  1629.             case 'K':
  1630.                 $bytesize 1024;
  1631.                 break;
  1632.         }
  1633.  
  1634.         return ($bytesize * (int)substr($ini_size0-1));
  1635.     }
  1636.  
  1637.     return $ini_size;
  1638. }
  1639.  
  1640.  
  1641. /**
  1642.  * temporary function to make use of the deliver class.
  1643.  * In the future the responsible backend should be automaticly loaded
  1644.  * and conf.pl should show a list of available backends.
  1645.  * The message also should be constructed by the message class.
  1646.  *
  1647.  * @param object $composeMessage The message being sent.  Please note
  1648.  *                                that it is passed by reference and
  1649.  *                                will be returned modified, with additional
  1650.  *                                headers, such as Message-ID, Date, In-Reply-To,
  1651.  *                                References, and so forth.
  1652.  *
  1653.  * @return boolean FALSE if delivery failed, or some non-FALSE value
  1654.  *                  upon success.
  1655.  *
  1656.  */
  1657. function deliverMessage(&$composeMessage$draft=false{
  1658.     global $send_to$send_to_cc$send_to_bcc$mailprio$subject$body,
  1659.         $username$popuser$usernamedata$identity$idents$data_dir,
  1660.         $request_mdn$request_dr$default_charset$color$useSendmail,
  1661.         $domain$action$default_move_to_sent$move_to_sent$session$compose_messages;
  1662.     global $imapServerAddress$imapPort$imap_stream_options$sent_folder$key;
  1663.  
  1664.     $rfc822_header $composeMessage->rfc822_header;
  1665.  
  1666.     $abook addressbook_init(falsetrue);
  1667.     $rfc822_header->to $rfc822_header->parseAddress($send_to,truearray()''$domainarray(&$abook,'lookup'));
  1668.     $rfc822_header->cc $rfc822_header->parseAddress($send_to_cc,true,array()'',$domainarray(&$abook,'lookup'));
  1669.     $rfc822_header->bcc $rfc822_header->parseAddress($send_to_bcc,truearray()'',$domainarray(&$abook,'lookup'));
  1670.     $rfc822_header->priority $mailprio;
  1671.     $rfc822_header->subject $subject;
  1672.  
  1673.     $special_encoding='';
  1674.     if (strtolower($default_charset== 'iso-2022-jp'{
  1675.         if (mb_detect_encoding($body== 'ASCII'{
  1676.             $special_encoding '8bit';
  1677.         else {
  1678.             $body mb_convert_encoding($body'JIS');
  1679.             $special_encoding '7bit';
  1680.         }
  1681.     }
  1682.     $composeMessage->setBody($body);
  1683.  
  1684.     if (preg_match('|^([^@%/]+)[@%/](.+)$|'$username$usernamedata)) {
  1685.         $popuser $usernamedata[1];
  1686.         $domain  $usernamedata[2];
  1687.         unset($usernamedata);
  1688.     else {
  1689.         $popuser $username;
  1690.     }
  1691.     $reply_to '';
  1692.     $from_mail $idents[$identity]['email_address'];
  1693.     $full_name $idents[$identity]['full_name'];
  1694.     $reply_to  $idents[$identity]['reply_to'];
  1695.     if (!$from_mail{
  1696.         $from_mail "$popuser@$domain";
  1697.     }
  1698.     $rfc822_header->from $rfc822_header->parseAddress($from_mail,true);
  1699.     if (!$rfc822_header->from[0]->host$rfc822_header->from[0]->host $domain;
  1700.     if ($full_name{
  1701.         $from $rfc822_header->from[0];
  1702.         $full_name_encoded encodeHeader('"' $full_name '"');
  1703.         if ($full_name_encoded != $full_name{
  1704.             $from_addr $full_name_encoded .' <'.$from->mailbox.'@'.$from->host.'>';
  1705.         else {
  1706.             $from_addr '"'.$full_name .'" <'.$from->mailbox.'@'.$from->host.'>';
  1707.         }
  1708.         $rfc822_header->from $rfc822_header->parseAddress($from_addr,true);
  1709.     }
  1710.     if ($reply_to{
  1711.         $rfc822_header->reply_to $rfc822_header->parseAddress($reply_to,true);
  1712.         if (!$rfc822_header->reply_to[0]->host$rfc822_header->reply_to[0]->host $domain;
  1713.     }
  1714.     /* Receipt: On Read */
  1715.     if (isset($request_mdn&& $request_mdn{
  1716.         $rfc822_header->dnt $rfc822_header->parseAddress($from_mail,true);
  1717.     }
  1718.  
  1719.     /* Receipt: On Delivery */
  1720.     if (isset($request_dr&& $request_dr{
  1721.         $rfc822_header->more_headers['Return-Receipt-To'$from_mail;
  1722.     }
  1723.  
  1724.     /* multipart messages */
  1725.     if (count($composeMessage->entities)) {
  1726.         $message_body new Message();
  1727.         $message_body->body_part $composeMessage->body_part;
  1728.         $composeMessage->body_part '';
  1729.         $mime_header new MessageHeader;
  1730.         $mime_header->type0 'text';
  1731.         $mime_header->type1 'plain';
  1732.         if ($special_encoding{
  1733.             $mime_header->encoding $special_encoding;
  1734.         else {
  1735.             $mime_header->encoding '8bit';
  1736.         }
  1737.         if ($default_charset{
  1738.             $mime_header->parameters['charset'$default_charset;
  1739.         }
  1740.         $message_body->mime_header $mime_header;
  1741.         array_unshift($composeMessage->entities$message_body);
  1742.         $content_type new ContentType('multipart/mixed');
  1743.     else {
  1744.         $content_type new ContentType('text/plain');
  1745.         if ($special_encoding{
  1746.             $rfc822_header->encoding $special_encoding;
  1747.         else {
  1748.             $rfc822_header->encoding '8bit';
  1749.         }
  1750.         if ($default_charset{
  1751.             $content_type->properties['charset']=$default_charset;
  1752.         }
  1753.     }
  1754.  
  1755.     $rfc822_header->content_type $content_type;
  1756.     $composeMessage->rfc822_header $rfc822_header;
  1757.     if ($action == 'reply' || $action == 'reply_all'{
  1758.         global $passed_id$passed_ent_id;
  1759.         $reply_id $passed_id;
  1760.         $reply_ent_id $passed_ent_id;
  1761.     else {
  1762.         $reply_id '';
  1763.         $reply_ent_id '';
  1764.     }
  1765.  
  1766.     /* Here you can modify the message structure just before we hand
  1767.        it over to deliver */
  1768.     $hookReturn do_hook('compose_send'$composeMessage$draft);
  1769.     /* Get any changes made by plugins to $composeMessage. */
  1770.     if is_object($hookReturn[1]) ) {
  1771.         $composeMessage $hookReturn[1];
  1772.     }
  1773.     /* Get any changes made by plugins to $draft. */
  1774.     if !empty($hookReturn[2]|| $hookReturn[2=== FALSE {
  1775.         $draft $hookReturn[2];
  1776.     }
  1777.  
  1778.     // remove special header if present and prepare to mark
  1779.     // a message that a draft was composed in reply to
  1780.     if (!empty($composeMessage->rfc822_header->x_sm_flag_reply&& !$draft{
  1781.         global $passed_id$mailbox$action;
  1782.         // tricks the code below that marks the reply
  1783.         list($action$passed_id$mailboxexplode('::'$rfc822_header->x_sm_flag_reply3);
  1784.         unset($composeMessage->rfc822_header->x_sm_flag_reply);
  1785.         unset($composeMessage->rfc822_header->more_headers['X-SM-Flag-Reply']);
  1786.     }
  1787.  
  1788.     if (!$useSendmail && !$draft{
  1789.         require_once(SM_PATH 'class/deliver/Deliver_SMTP.class.php');
  1790.         $deliver new Deliver_SMTP();
  1791.         global $smtpServerAddress$smtpPort$smtp_stream_options,
  1792.                $pop_before_smtp$pop_before_smtp_host;
  1793.  
  1794.         $authPop (isset($pop_before_smtp&& $pop_before_smtptrue false;
  1795.         
  1796.         $user '';
  1797.         $pass '';
  1798.         if (empty($pop_before_smtp_host))
  1799.             $pop_before_smtp_host $smtpServerAddress;
  1800.         
  1801.         get_smtp_user($user$pass);
  1802.  
  1803.         $stream $deliver->initStream($composeMessage,$domain,0,
  1804.                 $smtpServerAddress$smtpPort$user$pass$authPop$pop_before_smtp_host$smtp_stream_options);
  1805.     elseif (!$draft{
  1806.         require_once(SM_PATH 'class/deliver/Deliver_SendMail.class.php');
  1807.         global $sendmail_path$sendmail_args;
  1808.         // Check for outdated configuration
  1809.         if (!isset($sendmail_args)) {
  1810.             if ($sendmail_path=='/var/qmail/bin/qmail-inject'{
  1811.                 $sendmail_args '';
  1812.             else {
  1813.                 $sendmail_args '-i -t';
  1814.             }
  1815.         }
  1816.         $deliver new Deliver_SendMail(array('sendmail_args'=>$sendmail_args));
  1817.         $stream $deliver->initStream($composeMessage,$sendmail_path);
  1818.     elseif ($draft{
  1819.         global $draft_folder;
  1820.         $imap_stream sqimap_login($username$key$imapServerAddress,
  1821.                 $imapPort0$imap_stream_options);
  1822.         if (sqimap_mailbox_exists ($imap_stream$draft_folder)) {
  1823. //TODO: this can leak private information about folders and message IDs if messages are accessed/sent from another client --- should this feature be optional?
  1824.             // make note of the message to mark as having been replied to
  1825.             global $passed_id$mailbox$action;
  1826.             if ($action == 'reply' || $action == 'reply_all'{
  1827.                 $composeMessage->rfc822_header->more_headers['X-SM-Flag-Reply'$action '::' $passed_id '::' $mailbox;
  1828.             }
  1829.  
  1830.             require_once(SM_PATH 'class/deliver/Deliver_IMAP.class.php');
  1831.             $imap_deliver new Deliver_IMAP();
  1832.             $succes $imap_deliver->mail($composeMessage$imap_stream$reply_id$reply_ent_id$imap_stream$draft_folder);
  1833.             sqimap_logout($imap_stream);
  1834.             unset ($imap_deliver);
  1835.             $composeMessage->purgeAttachments();
  1836. //TODO: completely unclear if should be using $compose_session instead of $session below
  1837.             unset($compose_messages[$session]);
  1838.             sqsession_register($compose_messages,'compose_messages');
  1839.             return $succes;
  1840.         else {
  1841.             $msg  '<br />'.sprintf(_("Error: Draft folder %s does not exist."),
  1842.                 sm_encode_html_special_chars($draft_folder));
  1843.             plain_error_message($msg$color);
  1844.             return false;
  1845.         }
  1846.     }
  1847.     $succes false;
  1848.     if ($stream{
  1849.         $deliver->mail($composeMessage$stream$reply_id$reply_ent_id);
  1850.         $succes $deliver->finalizeStream($stream);
  1851.     }
  1852.     if (!$succes{
  1853.         $msg _("Message not sent."' ' _("Server replied:")
  1854.              . "\n<blockquote>\n"
  1855.              . (isset($deliver->dlv_msg$deliver->dlv_msg '')
  1856.              . '<br />'
  1857.              . (isset($deliver->dlv_ret_nr$deliver->dlv_ret_nr ' ' '')
  1858.              . (isset($deliver->dlv_server_msg$deliver->dlv_server_msg '')
  1859.              . "</blockquote>\n\n";
  1860.         plain_error_message($msg$color);
  1861.     else {
  1862.         unset ($deliver);
  1863.         $imap_stream sqimap_login($username$key$imapServerAddress$imapPort0$imap_stream_options);
  1864.  
  1865.  
  1866.         // mark original message as having been replied to if applicable
  1867.         global $passed_id$mailbox$action;
  1868.         if ($action == 'reply' || $action == 'reply_all'{
  1869.             // select errors here could be due to a draft reply being sent
  1870.             // after the original message's mailbox is moved or deleted
  1871.             $result sqimap_mailbox_select ($imap_stream$mailboxfalse);
  1872.             // a non-empty return from above means we can proceed
  1873.             if (!empty($result))
  1874.                 sqimap_messages_flag ($imap_stream$passed_id$passed_id'Answered'false);
  1875.         }
  1876.  
  1877.  
  1878.         // copy message to sent folder
  1879.         $move_to_sent getPref($data_dir,$username,'move_to_sent'$default_move_to_sent);
  1880.         if (isset($default_move_to_sent&& ($default_move_to_sent != 0)) {
  1881.             $svr_allow_sent true;
  1882.         else {
  1883.             $svr_allow_sent false;
  1884.         }
  1885.  
  1886.         if (isset($sent_folder&& (($sent_folder != ''|| ($sent_folder != 'none'))
  1887.                 && sqimap_mailbox_exists$imap_stream$sent_folder)) {
  1888.             $fld_sent true;
  1889.         else {
  1890.             $fld_sent false;
  1891.         }
  1892.  
  1893.         if ((isset($move_to_sent&& ($move_to_sent != 0)) || (!isset($move_to_sent))) {
  1894.             $lcl_allow_sent true;
  1895.         else {
  1896.             $lcl_allow_sent false;
  1897.         }
  1898.  
  1899.         if (($fld_sent && $svr_allow_sent && !$lcl_allow_sent|| ($fld_sent && $lcl_allow_sent)) {
  1900.             require_once(SM_PATH 'class/deliver/Deliver_IMAP.class.php');
  1901.             $imap_deliver new Deliver_IMAP();
  1902.             $imap_deliver->mail($composeMessage$imap_stream$reply_id$reply_ent_id$imap_stream$sent_folder);
  1903.             unset ($imap_deliver);
  1904.         }
  1905.         $composeMessage->purgeAttachments();
  1906. //TODO: completely unclear if should be using $compose_session instead of $session below
  1907.         unset($compose_messages[$session]);
  1908.         sqsession_register($compose_messages,'compose_messages');
  1909.         sqimap_logout($imap_stream);
  1910.     }
  1911.     return $succes;
  1912. }

Documentation generated on Mon, 13 Jan 2020 04:24:21 +0100 by phpDocumentor 1.4.3