| 1 | <?php |
| 2 | /** |
| 3 | * compose.php |
| 4 | * |
| 5 | * This code sends a mail. |
| 6 | * |
| 7 | * There are 4 modes of operation: |
| 8 | * - Start new mail |
| 9 | * - Add an attachment |
| 10 | * - Send mail |
| 11 | * - Save As Draft |
| 12 | * |
| 13 | * @copyright © 1999-2006 The SquirrelMail Project Team |
| 14 | * @license http://opensource.org/licenses/gpl-license.php GNU Public License |
| 15 | * @version $Id$ |
| 16 | * @package squirrelmail |
| 17 | */ |
| 18 | |
| 19 | /** |
| 20 | * Include the SquirrelMail initialization file. |
| 21 | */ |
| 22 | require('../include/init.php'); |
| 23 | |
| 24 | /* SquirrelMail required files. */ |
| 25 | require_once(SM_PATH . 'functions/imap_general.php'); |
| 26 | require_once(SM_PATH . 'functions/imap_messages.php'); |
| 27 | require_once(SM_PATH . 'functions/date.php'); |
| 28 | require_once(SM_PATH . 'functions/mime.php'); |
| 29 | require_once(SM_PATH . 'class/deliver/Deliver.class.php'); |
| 30 | require_once(SM_PATH . 'functions/addressbook.php'); |
| 31 | require_once(SM_PATH . 'functions/forms.php'); |
| 32 | require_once(SM_PATH . 'functions/identity.php'); |
| 33 | |
| 34 | /* --------------------- Get globals ------------------------------------- */ |
| 35 | |
| 36 | /** SESSION VARS */ |
| 37 | sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION); |
| 38 | |
| 39 | sqgetGlobalVar('composesession', $composesession, SQ_SESSION); |
| 40 | sqgetGlobalVar('compose_messages', $compose_messages, SQ_SESSION); |
| 41 | sqgetGlobalVar('delayed_errors', $delayed_errors, SQ_SESSION); |
| 42 | |
| 43 | // Turn on delayed error handling in case we wind up redirecting below |
| 44 | $oErrorHandler->setDelayedErrors(true); |
| 45 | |
| 46 | /** SESSION/POST/GET VARS */ |
| 47 | sqgetGlobalVar('session',$session); |
| 48 | sqgetGlobalVar('mailbox',$mailbox); |
| 49 | if(!sqgetGlobalVar('identity',$identity)) { |
| 50 | $identity=0; |
| 51 | } |
| 52 | sqgetGlobalVar('send_to',$send_to); |
| 53 | sqgetGlobalVar('send_to_cc',$send_to_cc); |
| 54 | sqgetGlobalVar('send_to_bcc',$send_to_bcc); |
| 55 | sqgetGlobalVar('subject',$subject); |
| 56 | sqgetGlobalVar('body',$body); |
| 57 | sqgetGlobalVar('mailprio',$mailprio); |
| 58 | sqgetGlobalVar('request_mdn',$request_mdn); |
| 59 | sqgetGlobalVar('request_dr',$request_dr); |
| 60 | sqgetGlobalVar('html_addr_search',$html_addr_search); |
| 61 | sqgetGlobalVar('mail_sent',$mail_sent); |
| 62 | sqgetGlobalVar('passed_id',$passed_id); |
| 63 | sqgetGlobalVar('passed_ent_id',$passed_ent_id); |
| 64 | sqgetGlobalVar('send',$send); |
| 65 | |
| 66 | sqgetGlobalVar('attach',$attach); |
| 67 | |
| 68 | sqgetGlobalVar('draft',$draft); |
| 69 | sqgetGlobalVar('draft_id',$draft_id); |
| 70 | sqgetGlobalVar('ent_num',$ent_num); |
| 71 | sqgetGlobalVar('saved_draft',$saved_draft); |
| 72 | |
| 73 | if ( sqgetGlobalVar('delete_draft',$delete_draft) ) { |
| 74 | $delete_draft = (int)$delete_draft; |
| 75 | } |
| 76 | |
| 77 | if ( sqgetGlobalVar('startMessage',$startMessage) ) { |
| 78 | $startMessage = (int)$startMessage; |
| 79 | } else { |
| 80 | $startMessage = 1; |
| 81 | } |
| 82 | |
| 83 | |
| 84 | /** POST VARS */ |
| 85 | sqgetGlobalVar('sigappend', $sigappend, SQ_POST); |
| 86 | sqgetGlobalVar('from_htmladdr_search', $from_htmladdr_search, SQ_POST); |
| 87 | sqgetGlobalVar('addr_search_done', $html_addr_search_done, SQ_POST); |
| 88 | sqgetGlobalVar('addr_search_cancel', $html_addr_search_cancel, SQ_POST); |
| 89 | sqgetGlobalVar('send_to_search', $send_to_search, SQ_POST); |
| 90 | sqgetGlobalVar('do_delete', $do_delete, SQ_POST); |
| 91 | sqgetGlobalVar('delete', $delete, SQ_POST); |
| 92 | sqgetGlobalVar('restoremessages', $restoremessages, SQ_POST); |
| 93 | if ( sqgetGlobalVar('return', $temp, SQ_POST) ) { |
| 94 | $html_addr_search_done = 'Use Addresses'; |
| 95 | } |
| 96 | |
| 97 | /** GET VARS */ |
| 98 | sqgetGlobalVar('attachedmessages', $attachedmessages, SQ_GET); |
| 99 | if ( sqgetGlobalVar('account', $temp, SQ_GET) ) { |
| 100 | $iAccount = (int) $temp; |
| 101 | } else { |
| 102 | $iAccount = 0; |
| 103 | } |
| 104 | |
| 105 | |
| 106 | /** get smaction */ |
| 107 | if ( !sqgetGlobalVar('smaction',$action) ) |
| 108 | { |
| 109 | if ( sqgetGlobalVar('smaction_reply',$tmp) ) $action = 'reply'; |
| 110 | if ( sqgetGlobalVar('smaction_reply_all',$tmp) ) $action = 'reply_all'; |
| 111 | if ( sqgetGlobalVar('smaction_forward',$tmp) ) $action = 'forward'; |
| 112 | if ( sqgetGlobalVar('smaction_attache',$tmp) ) $action = 'forward_as_attachment'; |
| 113 | if ( sqgetGlobalVar('smaction_draft',$tmp) ) $action = 'draft'; |
| 114 | if ( sqgetGlobalVar('smaction_edit_new',$tmp) ) $action = 'edit_as_new'; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Here we decode the data passed in from mailto.php. |
| 119 | */ |
| 120 | if ( sqgetGlobalVar('mailtodata', $mailtodata, SQ_GET) ) { |
| 121 | $trtable = array('to' => 'send_to', |
| 122 | 'cc' => 'send_to_cc', |
| 123 | 'bcc' => 'send_to_bcc', |
| 124 | 'body' => 'body', |
| 125 | 'subject' => 'subject'); |
| 126 | $mtdata = unserialize($mailtodata); |
| 127 | |
| 128 | foreach ($trtable as $f => $t) { |
| 129 | if ( !empty($mtdata[$f]) ) { |
| 130 | $$t = $mtdata[$f]; |
| 131 | } |
| 132 | } |
| 133 | unset($mailtodata,$mtdata, $trtable); |
| 134 | } |
| 135 | |
| 136 | /* Location (For HTTP 1.1 Header("Location: ...") redirects) */ |
| 137 | $location = get_location(); |
| 138 | /* Identities (fetch only once) */ |
| 139 | $idents = get_identities(); |
| 140 | |
| 141 | /* --------------------- Specific Functions ------------------------------ */ |
| 142 | |
| 143 | function replyAllString($header) { |
| 144 | global $include_self_reply_all, $idents; |
| 145 | $excl_ar = array(); |
| 146 | /** |
| 147 | * 1) Remove the addresses we'll be sending the message 'to' |
| 148 | */ |
| 149 | if (isset($header->replyto)) { |
| 150 | $excl_ar = $header->getAddr_a('replyto'); |
| 151 | } |
| 152 | /** |
| 153 | * 2) Remove our identities from the CC list (they still can be in the |
| 154 | * TO list) only if $include_self_reply_all is turned off |
| 155 | */ |
| 156 | if (!$include_self_reply_all) { |
| 157 | foreach($idents as $id) { |
| 158 | $excl_ar[strtolower(trim($id['email_address']))] = ''; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * 3) get the addresses. |
| 164 | */ |
| 165 | $url_replytoall_ar = $header->getAddr_a(array('to','cc'), $excl_ar); |
| 166 | |
| 167 | /** |
| 168 | * 4) generate the string. |
| 169 | */ |
| 170 | $url_replytoallcc = ''; |
| 171 | foreach( $url_replytoall_ar as $email => $personal) { |
| 172 | if ($personal) { |
| 173 | // if personal name contains address separator then surround |
| 174 | // the personal name with double quotes. |
| 175 | if (strpos($personal,',') !== false) { |
| 176 | $personal = '"'.$personal.'"'; |
| 177 | } |
| 178 | $url_replytoallcc .= ", $personal <$email>"; |
| 179 | } else { |
| 180 | $url_replytoallcc .= ', '. $email; |
| 181 | } |
| 182 | } |
| 183 | $url_replytoallcc = substr($url_replytoallcc,2); |
| 184 | |
| 185 | return $url_replytoallcc; |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * creates top line in reply citations |
| 190 | * |
| 191 | * Line style depends on user preferences. |
| 192 | * $orig_date argument is available only from 1.4.3 and 1.5.1 version. |
| 193 | * @param object $orig_from From: header object. |
| 194 | * @param integer $orig_date email's timestamp |
| 195 | * @return string reply citation |
| 196 | */ |
| 197 | function getReplyCitation($orig_from, $orig_date) { |
| 198 | global $reply_citation_style, $reply_citation_start, $reply_citation_end; |
| 199 | |
| 200 | if (!is_object($orig_from)) { |
| 201 | $sOrig_from = ''; |
| 202 | } else { |
| 203 | $sOrig_from = decodeHeader($orig_from->getAddress(false),false,false,true); |
| 204 | } |
| 205 | |
| 206 | /* First, return an empty string when no citation style selected. */ |
| 207 | if (($reply_citation_style == '') || ($reply_citation_style == 'none')) { |
| 208 | return ''; |
| 209 | } |
| 210 | |
| 211 | /* Make sure our final value isn't an empty string. */ |
| 212 | if ($sOrig_from == '') { |
| 213 | return ''; |
| 214 | } |
| 215 | |
| 216 | /* Otherwise, try to select the desired citation style. */ |
| 217 | switch ($reply_citation_style) { |
| 218 | case 'author_said': |
| 219 | /** |
| 220 | * To translators: %s is for author's name |
| 221 | */ |
| 222 | $full_reply_citation = sprintf(_("%s wrote:"),$sOrig_from); |
| 223 | break; |
| 224 | case 'quote_who': |
| 225 | $start = '<quote who="'; |
| 226 | $end = '">'; |
| 227 | $full_reply_citation = $start . $sOrig_from . $end; |
| 228 | break; |
| 229 | case 'date_time_author': |
| 230 | /** |
| 231 | * To translators: |
| 232 | * first %s is for date string, second %s is for author's name. Date uses |
| 233 | * formating from "D, F j, Y g:i a" and "D, F j, Y H:i" translations. |
| 234 | * Example string: |
| 235 | * "On Sat, December 24, 2004 23:59, Santa wrote:" |
| 236 | * If you have to put author's name in front of date string, check comments about |
| 237 | * argument swapping at http://www.php.net/sprintf |
| 238 | */ |
| 239 | $full_reply_citation = sprintf(_("On %s, %s wrote:"), getLongDateString($orig_date), $sOrig_from); |
| 240 | break; |
| 241 | case 'user-defined': |
| 242 | $start = $reply_citation_start . |
| 243 | ($reply_citation_start == '' ? '' : ' '); |
| 244 | $end = $reply_citation_end; |
| 245 | $full_reply_citation = $start . $sOrig_from . $end; |
| 246 | break; |
| 247 | default: |
| 248 | return ''; |
| 249 | } |
| 250 | |
| 251 | /* Add line feed and return the citation string. */ |
| 252 | return ($full_reply_citation . "\n"); |
| 253 | } |
| 254 | |
| 255 | /** |
| 256 | * Creates header fields in forwarded email body |
| 257 | * |
| 258 | * $default_charset global must be set correctly before you call this function. |
| 259 | * @param object $orig_header |
| 260 | * @return $string |
| 261 | */ |
| 262 | function getforwardHeader($orig_header) { |
| 263 | global $editor_size, $default_charset; |
| 264 | |
| 265 | // using own strlen function in order to detect correct string length |
| 266 | $display = array( _("Subject") => sq_strlen(_("Subject"),$default_charset), |
| 267 | _("From") => sq_strlen(_("From"),$default_charset), |
| 268 | _("Date") => sq_strlen(_("Date"),$default_charset), |
| 269 | _("To") => sq_strlen(_("To"),$default_charset), |
| 270 | _("Cc") => sq_strlen(_("Cc"),$default_charset) ); |
| 271 | $maxsize = max($display); |
| 272 | $indent = str_pad('',$maxsize+2); |
| 273 | foreach($display as $key => $val) { |
| 274 | $display[$key] = $key .': '. str_pad('', $maxsize - $val); |
| 275 | } |
| 276 | $from = decodeHeader($orig_header->getAddr_s('from',"\n$indent"),false,false,true); |
| 277 | $from = str_replace(' ',' ',$from); |
| 278 | $to = decodeHeader($orig_header->getAddr_s('to',"\n$indent"),false,false,true); |
| 279 | $to = str_replace(' ',' ',$to); |
| 280 | $subject = decodeHeader($orig_header->subject,false,false,true); |
| 281 | $subject = str_replace(' ',' ',$subject); |
| 282 | |
| 283 | // using own str_pad function in order to create correct string pad |
| 284 | $bodyTop = sq_str_pad(' '._("Original Message").' ',$editor_size -2,'-',STR_PAD_BOTH,$default_charset) . |
| 285 | "\n". $display[_("Subject")] . $subject . "\n" . |
| 286 | $display[_("From")] . $from . "\n" . |
| 287 | $display[_("Date")] . getLongDateString( $orig_header->date ). "\n" . |
| 288 | $display[_("To")] . $to . "\n"; |
| 289 | if ($orig_header->cc != array() && $orig_header->cc !='') { |
| 290 | $cc = decodeHeader($orig_header->getAddr_s('cc',"\n$indent"),false,false,true); |
| 291 | $cc = str_replace(' ',' ',$cc); |
| 292 | $bodyTop .= $display[_("Cc")] .$cc . "\n"; |
| 293 | } |
| 294 | $bodyTop .= str_pad('', $editor_size -2 , '-') . |
| 295 | "\n\n"; |
| 296 | return $bodyTop; |
| 297 | } |
| 298 | /* ----------------------------------------------------------------------- */ |
| 299 | |
| 300 | /* |
| 301 | * If the session is expired during a post this restores the compose session |
| 302 | * vars. |
| 303 | */ |
| 304 | if (sqsession_is_registered('session_expired_post')) { |
| 305 | sqgetGlobalVar('session_expired_post', $session_expired_post, SQ_SESSION); |
| 306 | /* |
| 307 | * extra check for username so we don't display previous post data from |
| 308 | * another user during this session. |
| 309 | */ |
| 310 | if ($session_expired_post['username'] != $username) { |
| 311 | unset($session_expired_post); |
| 312 | sqsession_unregister('session_expired_post'); |
| 313 | session_write_close(); |
| 314 | } else { |
| 315 | // these are the vars that we can set from the expired composed session |
| 316 | $compo_var_list = array ( 'send_to', 'send_to_cc','body','startMessage', |
| 317 | 'passed_body','use_signature','signature','attachments','subject','newmail', |
| 318 | 'send_to_bcc', 'passed_id', 'mailbox', 'from_htmladdr_search', 'identity', |
| 319 | 'draft_id', 'delete_draft', 'mailprio', 'edit_as_new', 'compose_messsages', |
| 320 | 'composesession', 'request_mdn', 'request_dr'); |
| 321 | |
| 322 | foreach ($compo_var_list as $var) { |
| 323 | if ( isset($session_expired_post[$var]) && !isset($$var) ) { |
| 324 | $$var = $session_expired_post[$var]; |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | $compose_messages = unserialize(urldecode($restoremessages)); |
| 329 | sqsession_register($compose_messages,'compose_messages'); |
| 330 | sqsession_register($composesession,'composesession'); |
| 331 | if (isset($send)) { |
| 332 | unset($send); |
| 333 | } |
| 334 | $session_expired = true; |
| 335 | } |
| 336 | unset($session_expired_post); |
| 337 | sqsession_unregister('session_expired_post'); |
| 338 | session_write_close(); |
| 339 | if (!isset($mailbox)) { |
| 340 | $mailbox = ''; |
| 341 | } |
| 342 | if ($compose_new_win == '1') { |
| 343 | compose_Header($color, $mailbox); |
| 344 | } else { |
| 345 | $sHeaderJs = (isset($sHeaderJs)) ? $sHeaderJs : ''; |
| 346 | if (strpos($action, 'reply') !== false && $reply_focus) { |
| 347 | $sBodyTagJs = 'onload="checkForm(\''.$replyfocus.'\');"'; |
| 348 | } else { |
| 349 | $sBodyTagJs = 'onload="checkForm();"'; |
| 350 | } |
| 351 | displayPageHeader($color, $mailbox,$sHeaderJs,$sBodyTagJs); |
| 352 | } |
| 353 | showInputForm($session, false); |
| 354 | exit(); |
| 355 | } |
| 356 | if (!isset($composesession)) { |
| 357 | $composesession = 0; |
| 358 | sqsession_register(0,'composesession'); |
| 359 | } else { |
| 360 | $composesession = (int)$composesession; |
| 361 | } |
| 362 | |
| 363 | if (!isset($session) || (isset($newmessage) && $newmessage)) { |
| 364 | sqsession_unregister('composesession'); |
| 365 | $session = "$composesession" +1; |
| 366 | $composesession = $session; |
| 367 | sqsession_register($composesession,'composesession'); |
| 368 | } |
| 369 | if (!isset($compose_messages)) { |
| 370 | $compose_messages = array(); |
| 371 | } |
| 372 | |
| 373 | if (!isset($compose_messages[$session]) || ($compose_messages[$session] == NULL)) { |
| 374 | $composeMessage = new Message(); |
| 375 | $rfc822_header = new Rfc822Header(); |
| 376 | $composeMessage->rfc822_header = $rfc822_header; |
| 377 | $composeMessage->reply_rfc822_header = ''; |
| 378 | $compose_messages[$session] = $composeMessage; |
| 379 | |
| 380 | sqsession_register($compose_messages,'compose_messages'); |
| 381 | } else { |
| 382 | $composeMessage=$compose_messages[$session]; |
| 383 | } |
| 384 | |
| 385 | if (!isset($mailbox) || $mailbox == '' || ($mailbox == 'None')) { |
| 386 | $mailbox = 'INBOX'; |
| 387 | } |
| 388 | |
| 389 | if ($draft) { |
| 390 | /* |
| 391 | * Set $default_charset to correspond with the user's selection |
| 392 | * of language interface. |
| 393 | */ |
| 394 | set_my_charset(); |
| 395 | $composeMessage=$compose_messages[$session]; |
| 396 | if (! deliverMessage($composeMessage, true)) { |
| 397 | showInputForm($session); |
| 398 | exit(); |
| 399 | } else { |
| 400 | unset($compose_messages[$session]); |
| 401 | $draft_message = _("Draft Email Saved"); |
| 402 | /* If this is a resumed draft, then delete the original */ |
| 403 | if(isset($delete_draft)) { |
| 404 | $imap_stream = sqimap_login($username, false, $imapServerAddress, $imapPort, false); |
| 405 | sqimap_mailbox_select($imap_stream, $draft_folder); |
| 406 | // force bypass_trash=true because message should be saved when deliverMessage() returns true. |
| 407 | // in current implementation of sqimap_msgs_list_flag() single message id can |
| 408 | // be submitted as string. docs state that it should be array. |
| 409 | sqimap_msgs_list_delete($imap_stream, $draft_folder, $delete_draft, true); |
| 410 | if ($auto_expunge) { |
| 411 | sqimap_mailbox_expunge($imap_stream, $draft_folder, true); |
| 412 | } |
| 413 | sqimap_logout($imap_stream); |
| 414 | } |
| 415 | |
| 416 | $oErrorHandler->saveDelayedErrors(); |
| 417 | session_write_close(); |
| 418 | |
| 419 | if ($compose_new_win == '1') { |
| 420 | if ( !isset($pageheader_sent) || !$pageheader_sent ) { |
| 421 | Header("Location: $location/compose.php?saved_draft=yes&session=$composesession"); |
| 422 | } else { |
| 423 | echo ' <br><br><div style="text-align: center;"><a href="' . $location |
| 424 | . '/compose.php?saved_sent=yes&session=' . $composesession . '">' |
| 425 | . _("Return") . '</a></div>'; |
| 426 | } |
| 427 | exit(); |
| 428 | } else { |
| 429 | if ( !isset($pageheader_sent) || !$pageheader_sent ) { |
| 430 | Header("Location: $location/right_main.php?mailbox=" . urlencode($draft_folder) . |
| 431 | "&startMessage=1¬e=".urlencode($draft_message)); |
| 432 | } else { |
| 433 | echo ' <br><br><div style="text-align: center;"><a href="' . $location |
| 434 | . '/right_main.php?mailbox=' . urlencode($draft_folder) |
| 435 | . '&startMessage=1&note=' . urlencode($draft_message) .'">' |
| 436 | . _("Return") . '</a></div>'; |
| 437 | } |
| 438 | exit(); |
| 439 | } |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | if ($send) { |
| 444 | if (isset($_FILES['attachfile']) && |
| 445 | $_FILES['attachfile']['tmp_name'] && |
| 446 | $_FILES['attachfile']['tmp_name'] != 'none') { |
| 447 | $AttachFailure = saveAttachedFiles($session); |
| 448 | } |
| 449 | if (checkInput(false) && !isset($AttachFailure)) { |
| 450 | if ($mailbox == "All Folders") { |
| 451 | /* We entered compose via the search results page */ |
| 452 | $mailbox = 'INBOX'; /* Send 'em to INBOX, that's safe enough */ |
| 453 | } |
| 454 | $urlMailbox = urlencode (trim($mailbox)); |
| 455 | if (! isset($passed_id)) { |
| 456 | $passed_id = 0; |
| 457 | } |
| 458 | /** |
| 459 | * Set $default_charset to correspond with the user's selection |
| 460 | * of language interface. |
| 461 | */ |
| 462 | set_my_charset(); |
| 463 | /** |
| 464 | * This is to change all newlines to \n |
| 465 | * We'll change them to \r\n later (in the sendMessage function) |
| 466 | */ |
| 467 | $body = str_replace("\r\n", "\n", $body); |
| 468 | $body = str_replace("\r", "\n", $body); |
| 469 | |
| 470 | /** |
| 471 | * Rewrap $body so that no line is bigger than $editor_size |
| 472 | */ |
| 473 | $body = explode("\n", $body); |
| 474 | $newBody = ''; |
| 475 | foreach ($body as $line) { |
| 476 | if( $line <> '-- ' ) { |
| 477 | $line = rtrim($line); |
| 478 | } |
| 479 | if (sq_strlen($line,$default_charset) <= $editor_size + 1) { |
| 480 | $newBody .= $line . "\n"; |
| 481 | } else { |
| 482 | sqWordWrap($line, $editor_size,$default_charset); |
| 483 | $newBody .= $line . "\n"; |
| 484 | |
| 485 | } |
| 486 | |
| 487 | } |
| 488 | $body = $newBody; |
| 489 | |
| 490 | $composeMessage=$compose_messages[$session]; |
| 491 | |
| 492 | $Result = deliverMessage($composeMessage); |
| 493 | |
| 494 | do_hook('compose_send_after', $Result, $composeMessage); |
| 495 | if (! $Result) { |
| 496 | showInputForm($session); |
| 497 | exit(); |
| 498 | } |
| 499 | unset($compose_messages[$session]); |
| 500 | |
| 501 | /* if it is resumed draft, delete draft message */ |
| 502 | if ( isset($delete_draft)) { |
| 503 | $imap_stream = sqimap_login($username, false, $imapServerAddress, $imapPort, false); |
| 504 | sqimap_mailbox_select($imap_stream, $draft_folder); |
| 505 | // bypass_trash=true because message should be saved when deliverMessage() returns true. |
| 506 | // in current implementation of sqimap_msgs_list_flag() single message id can |
| 507 | // be submitted as string. docs state that it should be array. |
| 508 | sqimap_msgs_list_delete($imap_stream, $draft_folder, $delete_draft, true); |
| 509 | if ($auto_expunge) { |
| 510 | sqimap_mailbox_expunge($imap_stream, $draft_folder, true); |
| 511 | } |
| 512 | sqimap_logout($imap_stream); |
| 513 | } |
| 514 | /* |
| 515 | * Store the error array in the session because they will be lost on a redirect |
| 516 | */ |
| 517 | $oErrorHandler->saveDelayedErrors(); |
| 518 | session_write_close(); |
| 519 | |
| 520 | if ($compose_new_win == '1') { |
| 521 | if ( !isset($pageheader_sent) || !$pageheader_sent ) { |
| 522 | Header("Location: $location/compose.php?mail_sent=yes"); |
| 523 | } else { |
| 524 | echo ' <br><br><div style="text-align: center;"><a href="' . $location |
| 525 | . '/compose.php?mail_sent=yes">' |
| 526 | . _("Return") . '</a></div>'; |
| 527 | } |
| 528 | exit(); |
| 529 | } else { |
| 530 | if ( !isset($pageheader_sent) || !$pageheader_sent ) { |
| 531 | Header("Location: $location/right_main.php?mailbox=$urlMailbox". |
| 532 | "&startMessage=$startMessage&mail_sent=yes"); |
| 533 | } else { |
| 534 | echo ' <br><br><div style="text-align: center;"><a href="' . $location |
| 535 | . "/right_main.php?mailbox=$urlMailbox" |
| 536 | . "&startMessage=$startMessage&mail_sent=yes\">" |
| 537 | . _("Return") . '</a></div>'; |
| 538 | } |
| 539 | exit(); |
| 540 | } |
| 541 | } else { |
| 542 | if ($compose_new_win == '1') { |
| 543 | compose_Header($color, $mailbox); |
| 544 | } |
| 545 | else { |
| 546 | displayPageHeader($color, $mailbox); |
| 547 | } |
| 548 | if (isset($AttachFailure)) { |
| 549 | plain_error_message(_("Could not move/copy file. File not attached"), |
| 550 | $color); |
| 551 | } |
| 552 | checkInput(true); |
| 553 | showInputForm($session); |
| 554 | /* sqimap_logout($imapConnection); */ |
| 555 | } |
| 556 | } elseif (isset($html_addr_search_done)) { |
| 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($k, 0, 1) == 'T') { |
| 567 | if ($send_to) { |
| 568 | $send_to .= ', '; |
| 569 | } |
| 570 | $send_to .= $v; |
| 571 | } |
| 572 | elseif (substr($k, 0, 1) == 'C') { |
| 573 | if ($send_to_cc) { |
| 574 | $send_to_cc .= ', '; |
| 575 | } |
| 576 | $send_to_cc .= $v; |
| 577 | } |
| 578 | elseif (substr($k, 0, 1) == '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) && !isset($html_addr_search_cancel)) { |
| 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")); |
| 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 | if ($compose_new_win == '1') { |
| 602 | compose_Header($color, $mailbox); |
| 603 | } else { |
| 604 | displayPageHeader($color, $mailbox); |
| 605 | } |
| 606 | if (saveAttachedFiles($session)) { |
| 607 | plain_error_message(_("Could not move/copy file. File not attached")); |
| 608 | } |
| 609 | showInputForm($session); |
| 610 | } |
| 611 | elseif (isset($sigappend)) { |
| 612 | $signature = $idents[$identity]['signature']; |
| 613 | |
| 614 | $body .= "\n\n".($prefix_sig==true? "-- \n":'').$signature; |
| 615 | if ($compose_new_win == '1') { |
| 616 | compose_Header($color, $mailbox); |
| 617 | } else { |
| 618 | displayPageHeader($color, $mailbox); |
| 619 | } |
| 620 | showInputForm($session); |
| 621 | } elseif (isset($do_delete)) { |
| 622 | if ($compose_new_win == '1') { |
| 623 | compose_Header($color, $mailbox); |
| 624 | } else { |
| 625 | displayPageHeader($color, $mailbox); |
| 626 | } |
| 627 | |
| 628 | if (isset($delete) && is_array($delete)) { |
| 629 | $composeMessage = $compose_messages[$session]; |
| 630 | foreach($delete as $index) { |
| 631 | if (!empty($composeMessage->entities) && isset($composeMessage->entities[$index])) { |
| 632 | $composeMessage->entities[$index]->purgeAttachments(); |
| 633 | unset ($composeMessage->entities[$index]); |
| 634 | } |
| 635 | } |
| 636 | $new_entities = array(); |
| 637 | foreach ($composeMessage->entities as $entity) { |
| 638 | $new_entities[] = $entity; |
| 639 | } |
| 640 | $composeMessage->entities = $new_entities; |
| 641 | $compose_messages[$session] = $composeMessage; |
| 642 | sqsession_register($compose_messages, 'compose_messages'); |
| 643 | } |
| 644 | showInputForm($session); |
| 645 | } else { |
| 646 | /* |
| 647 | * This handles the default case as well as the error case |
| 648 | * (they had the same code) --> if (isset($smtpErrors)) |
| 649 | */ |
| 650 | |
| 651 | if ($compose_new_win == '1') { |
| 652 | compose_Header($color, $mailbox); |
| 653 | } else { |
| 654 | displayPageHeader($color, $mailbox); |
| 655 | } |
| 656 | |
| 657 | $newmail = true; |
| 658 | |
| 659 | if (!isset($passed_ent_id)) { |
| 660 | $passed_ent_id = ''; |
| 661 | } |
| 662 | if (!isset($passed_id)) { |
| 663 | $passed_id = ''; |
| 664 | } |
| 665 | if (!isset($mailbox)) { |
| 666 | $mailbox = ''; |
| 667 | } |
| 668 | if (!isset($action)) { |
| 669 | $action = ''; |
| 670 | } |
| 671 | |
| 672 | $values = newMail($mailbox,$passed_id,$passed_ent_id, $action, $session); |
| 673 | |
| 674 | /* in case the origin is not read_body.php */ |
| 675 | if (isset($send_to)) { |
| 676 | $values['send_to'] = $send_to; |
| 677 | } |
| 678 | if (isset($send_to_cc)) { |
| 679 | $values['send_to_cc'] = $send_to_cc; |
| 680 | } |
| 681 | if (isset($send_to_bcc)) { |
| 682 | $values['send_to_bcc'] = $send_to_bcc; |
| 683 | } |
| 684 | if (isset($subject)) { |
| 685 | $values['subject'] = $subject; |
| 686 | } |
| 687 | showInputForm($session, $values); |
| 688 | } |
| 689 | |
| 690 | exit(); |
| 691 | |
| 692 | /**************** Only function definitions go below *************/ |
| 693 | |
| 694 | function getforwardSubject($subject) |
| 695 | { |
| 696 | if ((substr(strtolower($subject), 0, 4) != 'fwd:') && |
| 697 | (substr(strtolower($subject), 0, 5) != '[fwd:') && |
| 698 | (substr(strtolower($subject), 0, 6) != '[ fwd:')) { |
| 699 | $subject = '[Fwd: ' . $subject . ']'; |
| 700 | } |
| 701 | return $subject; |
| 702 | } |
| 703 | |
| 704 | /* This function is used when not sending or adding attachments */ |
| 705 | function newMail ($mailbox='', $passed_id='', $passed_ent_id='', $action='', $session='') { |
| 706 | global $editor_size, $default_use_priority, $body, $idents, |
| 707 | $use_signature, $data_dir, $username, |
| 708 | $key, $imapServerAddress, $imapPort, $compose_messages, |
| 709 | $composeMessage, $body_quote, $request_mdn, $request_dr, |
| 710 | $default_use_mdn, $mdn_user_support; |
| 711 | global $languages, $squirrelmail_language, $default_charset; |
| 712 | |
| 713 | /* |
| 714 | * Set $default_charset to correspond with the user's selection |
| 715 | * of language interface. $default_charset global is not correct, |
| 716 | * if message is composed in new window. |
| 717 | */ |
| 718 | set_my_charset(); |
| 719 | |
| 720 | $send_to = $send_to_cc = $send_to_bcc = $subject = $identity = ''; |
| 721 | $mailprio = 3; |
| 722 | |
| 723 | if ($passed_id) { |
| 724 | $imapConnection = sqimap_login($username, false, $imapServerAddress, |
| 725 | $imapPort, 0); |
| 726 | |
| 727 | sqimap_mailbox_select($imapConnection, $mailbox); |
| 728 | $message = sqimap_get_message($imapConnection, $passed_id, $mailbox); |
| 729 | |
| 730 | $body = ''; |
| 731 | if ($passed_ent_id) { |
| 732 | /* redefine the messsage in case of message/rfc822 */ |
| 733 | $message = $message->getEntity($passed_ent_id); |
| 734 | /* message is an entity which contains the envelope and type0=message |
| 735 | * and type1=rfc822. The actual entities are childs from |
| 736 | * $message->entities[0]. That's where the encoding and is located |
| 737 | */ |
| 738 | |
| 739 | $entities = $message->entities[0]->findDisplayEntity |
| 740 | (array(), $alt_order = array('text/plain')); |
| 741 | if (!count($entities)) { |
| 742 | $entities = $message->entities[0]->findDisplayEntity |
| 743 | (array(), $alt_order = array('text/plain','html/plain')); |
| 744 | } |
| 745 | $orig_header = $message->rfc822_header; /* here is the envelope located */ |
| 746 | /* redefine the message for picking up the attachments */ |
| 747 | $message = $message->entities[0]; |
| 748 | |
| 749 | } else { |
| 750 | $entities = $message->findDisplayEntity (array(), $alt_order = array('text/plain')); |
| 751 | if (!count($entities)) { |
| 752 | $entities = $message->findDisplayEntity (array(), $alt_order = array('text/plain','html/plain')); |
| 753 | } |
| 754 | $orig_header = $message->rfc822_header; |
| 755 | } |
| 756 | |
| 757 | $type0 = $message->type0; |
| 758 | $type1 = $message->type1; |
| 759 | foreach ($entities as $ent) { |
| 760 | $msg = $message->getEntity($ent); |
| 761 | $type0 = $msg->type0; |
| 762 | $type1 = $msg->type1; |
| 763 | $unencoded_bodypart = mime_fetch_body($imapConnection, $passed_id, $ent); |
| 764 | $body_part_entity = $message->getEntity($ent); |
| 765 | $bodypart = decodeBody($unencoded_bodypart, |
| 766 | $body_part_entity->header->encoding); |
| 767 | if ($type1 == 'html') { |
| 768 | $bodypart = str_replace("\n", ' ', $bodypart); |
| 769 | $bodypart = preg_replace(array('/<\/?p>/i','/<div><\/div>/i','/<br\s*(\/)*>/i','/<\/?div>/i'), "\n", $bodypart); |
| 770 | $bodypart = str_replace(array(' ','>','<'),array(' ','>','<'),$bodypart); |
| 771 | $bodypart = strip_tags($bodypart); |
| 772 | } |
| 773 | if (isset($languages[$squirrelmail_language]['XTRA_CODE']) && |
| 774 | function_exists($languages[$squirrelmail_language]['XTRA_CODE'] . '_decode')) { |
| 775 | if (mb_detect_encoding($bodypart) != 'ASCII') { |
| 776 | $bodypart = call_user_func($languages[$squirrelmail_language]['XTRA_CODE'] . '_decode', $bodypart); |
| 777 | } |
| 778 | } |
| 779 | |
| 780 | // charset encoding in compose form stuff |
| 781 | if (isset($body_part_entity->header->parameters['charset'])) { |
| 782 | $actual = $body_part_entity->header->parameters['charset']; |
| 783 | } else { |
| 784 | $actual = 'us-ascii'; |
| 785 | } |
| 786 | |
| 787 | if ( $actual && is_conversion_safe($actual) && $actual != $default_charset){ |
| 788 | $bodypart = charset_convert($actual,$bodypart,$default_charset,false); |
| 789 | } |
| 790 | // end of charset encoding in compose |
| 791 | |
| 792 | $body .= $bodypart; |
| 793 | } |
| 794 | if ($default_use_priority) { |
| 795 | $mailprio = substr($orig_header->priority,0,1); |
| 796 | if (!$mailprio) { |
| 797 | $mailprio = 3; |
| 798 | } |
| 799 | } else { |
| 800 | $mailprio = ''; |
| 801 | } |
| 802 | |
| 803 | $identity = ''; |
| 804 | $from_o = $orig_header->from; |
| 805 | if (is_array($from_o)) { |
| 806 | if (isset($from_o[0])) { |
| 807 | $from_o = $from_o[0]; |
| 808 | } |
| 809 | } |
| 810 | if (is_object($from_o)) { |
| 811 | $orig_from = $from_o->getAddress(); |
| 812 | } else { |
| 813 | $orig_from = ''; |
| 814 | } |
| 815 | |
| 816 | $identities = array(); |
| 817 | if (count($idents) > 1) { |
| 818 | foreach($idents as $nr=>$data) { |
| 819 | $enc_from_name = '"'.$data['full_name'].'" <'. $data['email_address'].'>'; |
| 820 | if($enc_from_name == $orig_from) { |
| 821 | $identity = $nr; |
| 822 | break; |
| 823 | } |
| 824 | $identities[] = $enc_from_name; |
| 825 | } |
| 826 | |
| 827 | $identity_match = $orig_header->findAddress($identities); |
| 828 | if ($identity_match) { |
| 829 | $identity = $identity_match; |
| 830 | } |
| 831 | } |
| 832 | |
| 833 | switch ($action) { |
| 834 | case ('draft'): |
| 835 | $use_signature = FALSE; |
| 836 | $composeMessage->rfc822_header = $orig_header; |
| 837 | $send_to = decodeHeader($orig_header->getAddr_s('to'),false,false,true); |
| 838 | $send_to_cc = decodeHeader($orig_header->getAddr_s('cc'),false,false,true); |
| 839 | $send_to_bcc = decodeHeader($orig_header->getAddr_s('bcc'),false,false,true); |
| 840 | $send_from = $orig_header->getAddr_s('from'); |
| 841 | $send_from_parts = new AddressStructure(); |
| 842 | $send_from_parts = $orig_header->parseAddress($send_from); |
| 843 | $send_from_add = $send_from_parts->mailbox . '@' . $send_from_parts->host; |
| 844 | $identities = get_identities(); |
| 845 | if (count($identities) > 0) { |
| 846 | foreach($identities as $iddata) { |
| 847 | if ($send_from_add == $iddata['email_address']) { |
| 848 | $identity = $iddata['index']; |
| 849 | break; |
| 850 | } |
| 851 | } |
| 852 | } |
| 853 | $subject = decodeHeader($orig_header->subject,false,false,true); |
| 854 | |
| 855 | // Remember the receipt settings |
| 856 | $request_mdn = $mdn_user_support && !empty($orig_header->dnt) ? '1' : '0'; |
| 857 | $request_dr = $mdn_user_support && !empty($orig_header->drnt) ? '1' : '0'; |
| 858 | |
| 859 | /* remember the references and in-reply-to headers in case of an reply */ |
| 860 | $composeMessage->rfc822_header->more_headers['References'] = $orig_header->references; |
| 861 | $composeMessage->rfc822_header->more_headers['In-Reply-To'] = $orig_header->in_reply_to; |
| 862 | // rewrap the body to clean up quotations and line lengths |
| 863 | sqBodyWrap($body, $editor_size); |
| 864 | $composeMessage = getAttachments($message, $composeMessage, $passed_id, $entities, $imapConnection); |
| 865 | break; |
| 866 | case ('edit_as_new'): |
| 867 | $send_to = decodeHeader($orig_header->getAddr_s('to'),false,false,true); |
| 868 | $send_to_cc = decodeHeader($orig_header->getAddr_s('cc'),false,false,true); |
| 869 | $send_to_bcc = decodeHeader($orig_header->getAddr_s('bcc'),false,false,true); |
| 870 | $subject = decodeHeader($orig_header->subject,false,false,true); |
| 871 | $mailprio = $orig_header->priority; |
| 872 | $orig_from = ''; |
| 873 | $composeMessage = getAttachments($message, $composeMessage, $passed_id, $entities, $imapConnection); |
| 874 | // rewrap the body to clean up quotations and line lengths |
| 875 | sqBodyWrap($body, $editor_size); |
| 876 | break; |
| 877 | case ('forward'): |
| 878 | $send_to = ''; |
| 879 | $subject = getforwardSubject(decodeHeader($orig_header->subject,false,false,true)); |
| 880 | $body = getforwardHeader($orig_header) . $body; |
| 881 | // the logic for calling sqUnWordWrap here would be to allow the browser to wrap the lines |
| 882 | // forwarded message text should be as undisturbed as possible, so commenting out this call |
| 883 | // sqUnWordWrap($body); |
| 884 | $composeMessage = getAttachments($message, $composeMessage, $passed_id, $entities, $imapConnection); |
| 885 | |
| 886 | //add a blank line after the forward headers |
| 887 | $body = "\n" . $body; |
| 888 | break; |
| 889 | case ('forward_as_attachment'): |
| 890 | $subject = getforwardSubject(decodeHeader($orig_header->subject,false,false,true)); |
| 891 | $composeMessage = getMessage_RFC822_Attachment($message, $composeMessage, $passed_id, $passed_ent_id, $imapConnection); |
| 892 | $body = ''; |
| 893 | break; |
| 894 | case ('reply_all'): |
| 895 | if(isset($orig_header->mail_followup_to) && $orig_header->mail_followup_to) { |
| 896 | $send_to = $orig_header->getAddr_s('mail_followup_to'); |
| 897 | } else { |
| 898 | $send_to_cc = replyAllString($orig_header); |
| 899 | $send_to_cc = decodeHeader($send_to_cc,false,false,true); |
| 900 | } |
| 901 | case ('reply'): |
| 902 | // skip this if send_to was already set right above here |
| 903 | if(!$send_to) { |
| 904 | $send_to = $orig_header->reply_to; |
| 905 | if (is_array($send_to) && count($send_to)) { |
| 906 | $send_to = $orig_header->getAddr_s('reply_to'); |
| 907 | } else if (is_object($send_to)) { /* unneccesarry, just for failsafe purpose */ |
| 908 | $send_to = $orig_header->getAddr_s('reply_to'); |
| 909 | } else { |
| 910 | $send_to = $orig_header->getAddr_s('from'); |
| 911 | } |
| 912 | } |
| 913 | $send_to = decodeHeader($send_to,false,false,true); |
| 914 | $subject = decodeHeader($orig_header->subject,false,false,true); |
| 915 | $subject = str_replace('"', "'", $subject); |
| 916 | $subject = trim($subject); |
| 917 | if (substr(strtolower($subject), 0, 3) != 're:') { |
| 918 | $subject = 'Re: ' . $subject; |
| 919 | } |
| 920 | /* this corrects some wrapping/quoting problems on replies */ |
| 921 | $rewrap_body = explode("\n", $body); |
| 922 | $from = (is_array($orig_header->from)) ? $orig_header->from[0] : $orig_header->from; |
| 923 | $body = ''; |
| 924 | $strip_sigs = getPref($data_dir, $username, 'strip_sigs'); |
| 925 | foreach ($rewrap_body as $line) { |
| 926 | if ($strip_sigs && substr($line,0,3) == '-- ') { |
| 927 | break; |
| 928 | } |
| 929 | if (preg_match("/^(>+)/", $line, $matches)) { |
| 930 | $gt = $matches[1]; |
| 931 | $body .= $body_quote . str_replace("\n", "\n$body_quote$gt ", rtrim($line)) ."\n"; |
| 932 | } else { |
| 933 | $body .= $body_quote . (!empty($body_quote) ? ' ' : '') . str_replace("\n", "\n$body_quote" . (!empty($body_quote) ? ' ' : ''), rtrim($line)) . "\n"; |
| 934 | } |
| 935 | } |
| 936 | |
| 937 | //rewrap the body to clean up quotations and line lengths |
| 938 | $body = sqBodyWrap ($body, $editor_size); |
| 939 | |
| 940 | $body = getReplyCitation($from , $orig_header->date) . $body; |
| 941 | $composeMessage->reply_rfc822_header = $orig_header; |
| 942 | |
| 943 | break; |
| 944 | default: |
| 945 | break; |
| 946 | } |
| 947 | $compose_messages[$session] = $composeMessage; |
| 948 | sqsession_register($compose_messages, 'compose_messages'); |
| 949 | session_write_close(); |
| 950 | sqimap_logout($imapConnection); |
| 951 | } |
| 952 | $ret = array( 'send_to' => $send_to, |
| 953 | 'send_to_cc' => $send_to_cc, |
| 954 | 'send_to_bcc' => $send_to_bcc, |
| 955 | 'subject' => $subject, |
| 956 | 'mailprio' => $mailprio, |
| 957 | 'body' => $body, |
| 958 | 'identity' => $identity ); |
| 959 | |
| 960 | return ($ret); |
| 961 | } /* function newMail() */ |
| 962 | |
| 963 | /** |
| 964 | * downloads attachments from original message, stores them in attachment directory and adds |
| 965 | * them to composed message. |
| 966 | * @param object $message |
| 967 | * @param object $composeMessage |
| 968 | * @param integer $passed_id |
| 969 | * @param mixed $entities |
| 970 | * @param mixed $imapConnection |
| 971 | * @return object |
| 972 | */ |
| 973 | function getAttachments($message, &$composeMessage, $passed_id, $entities, $imapConnection) { |
| 974 | global $attachment_dir, $username, $data_dir, $squirrelmail_language, $languages; |
| 975 | $hashed_attachment_dir = getHashedDir($username, $attachment_dir); |
| 976 | if (!count($message->entities) || |
| 977 | ($message->type0 == 'message' && $message->type1 == 'rfc822')) { |
| 978 | if ( !in_array($message->entity_id, $entities) && $message->entity_id) { |
| 979 | switch ($message->type0) { |
| 980 | case 'message': |
| 981 | if ($message->type1 == 'rfc822') { |
| 982 | $filename = $message->rfc822_header->subject; |
| 983 | if ($filename == "") { |
| 984 | $filename = "untitled-".$message->entity_id; |
| 985 | } |
| 986 | $filename .= '.msg'; |
| 987 | } else { |
| 988 | $filename = $message->getFilename(); |
| 989 | } |
| 990 | break; |
| 991 | default: |
| 992 | if (!$message->mime_header) { /* temporary hack */ |
| 993 | $message->mime_header = $message->header; |
| 994 | } |
| 995 | $filename = $message->getFilename(); |
| 996 | break; |
| 997 | } |
| 998 | $filename = str_replace(' ', ' ', decodeHeader($filename)); |
| 999 | if (isset($languages[$squirrelmail_language]['XTRA_CODE']) && |
| 1000 | function_exists($languages[$squirrelmail_language]['XTRA_CODE'] . '_encode')) { |
| 1001 | $filename = call_user_func($languages[$squirrelmail_language]['XTRA_CODE'] . '_encode', $filename); |
| 1002 | } |
| 1003 | $localfilename = GenerateRandomString(32, '', 7); |
| 1004 | $full_localfilename = "$hashed_attachment_dir/$localfilename"; |
| 1005 | while (file_exists($full_localfilename)) { |
| 1006 | $localfilename = GenerateRandomString(32, '', 7); |
| 1007 | $full_localfilename = "$hashed_attachment_dir/$localfilename"; |
| 1008 | } |
| 1009 | $message->att_local_name = $full_localfilename; |
| 1010 | |
| 1011 | $composeMessage->initAttachment($message->type0.'/'.$message->type1,$filename, |
| 1012 | $full_localfilename); |
| 1013 | |
| 1014 | /* Write Attachment to file */ |
| 1015 | $fp = fopen ("$hashed_attachment_dir/$localfilename", 'wb'); |
| 1016 | mime_print_body_lines ($imapConnection, $passed_id, $message->entity_id, $message->header->encoding, $fp); |
| 1017 | fclose ($fp); |
| 1018 | } |
| 1019 | } else { |
| 1020 | for ($i=0, $entCount=count($message->entities); $i<$entCount;$i++) { |
| 1021 | $composeMessage=getAttachments($message->entities[$i], $composeMessage, $passed_id, $entities, $imapConnection); |
| 1022 | } |
| 1023 | } |
| 1024 | return $composeMessage; |
| 1025 | } |
| 1026 | |
| 1027 | function getMessage_RFC822_Attachment($message, $composeMessage, $passed_id, |
| 1028 | $passed_ent_id='', $imapConnection) { |
| 1029 | global $attachment_dir, $username, $data_dir; |
| 1030 | $hashed_attachment_dir = getHashedDir($username, $attachment_dir); |
| 1031 | if (!$passed_ent_id) { |
| 1032 | $body_a = sqimap_run_command($imapConnection, |
| 1033 | 'FETCH '.$passed_id.' RFC822', |
| 1034 | TRUE, $response, $readmessage, |
| 1035 | TRUE); |
| 1036 | } else { |
| 1037 | $body_a = sqimap_run_command($imapConnection, |
| 1038 | 'FETCH '.$passed_id.' BODY['.$passed_ent_id.']', |
| 1039 | TRUE, $response, $readmessage, TRUE); |
| 1040 | $message = $message->parent; |
| 1041 | } |
| 1042 | if ($response == 'OK') { |
| 1043 | $subject = encodeHeader($message->rfc822_header->subject); |
| 1044 | array_shift($body_a); |
| 1045 | array_pop($body_a); |
| 1046 | $body = implode('', $body_a) . "\r\n"; |
| 1047 | |
| 1048 | $localfilename = GenerateRandomString(32, 'FILE', 7); |
| 1049 | $full_localfilename = "$hashed_attachment_dir/$localfilename"; |
| 1050 | |
| 1051 | $fp = fopen($full_localfilename, 'w'); |
| 1052 | fwrite ($fp, $body); |
| 1053 | fclose($fp); |
| 1054 | $composeMessage->initAttachment('message/rfc822',$subject.'.msg', |
| 1055 | $full_localfilename); |
| 1056 | } |
| 1057 | return $composeMessage; |
| 1058 | } |
| 1059 | |
| 1060 | function showInputForm ($session, $values=false) { |
| 1061 | global $send_to, $send_to_cc, $body, $startMessage, $action, |
| 1062 | $color, $use_signature, $signature, $prefix_sig, |
| 1063 | $editor_size, $editor_height, $subject, $newmail, |
| 1064 | $use_javascript_addr_book, $send_to_bcc, $passed_id, $mailbox, |
| 1065 | $from_htmladdr_search, $location_of_buttons, $attachment_dir, |
| 1066 | $username, $data_dir, $identity, $idents, $delete_draft, |
| 1067 | $mailprio, $compose_new_win, $saved_draft, $mail_sent, $sig_first, |
| 1068 | $compose_messages, $composesession, $default_charset, |
| 1069 | $compose_onsubmit, $oTemplate, $oErrorHandler; |
| 1070 | |
| 1071 | if (checkForJavascript()) { |
| 1072 | $onfocus = ' onfocus="alreadyFocused=true;"'; |
| 1073 | $onfocus_array = array('onfocus' => 'alreadyFocused=true;'); |
| 1074 | } |
| 1075 | else { |
| 1076 | $onfocus = ''; |
| 1077 | $onfocus_array = array(); |
| 1078 | } |
| 1079 | |
| 1080 | $composeMessage = $compose_messages[$session]; |
| 1081 | if ($values) { |
| 1082 | $send_to = $values['send_to']; |
| 1083 | $send_to_cc = $values['send_to_cc']; |
| 1084 | $send_to_bcc = $values['send_to_bcc']; |
| 1085 | $subject = $values['subject']; |
| 1086 | $mailprio = $values['mailprio']; |
| 1087 | $body = $values['body']; |
| 1088 | $identity = (int) $values['identity']; |
| 1089 | } else { |
| 1090 | $send_to = decodeHeader($send_to, true, false); |
| 1091 | $send_to_cc = decodeHeader($send_to_cc, true, false); |
| 1092 | $send_to_bcc = decodeHeader($send_to_bcc, true, false); |
| 1093 | } |
| 1094 | |
| 1095 | if ($use_javascript_addr_book) { |
| 1096 | echo "\n". '<script type="text/javascript">'."\n<!--\n" . |
| 1097 | 'function open_abook() { ' . "\n" . |
| 1098 | ' var nwin = window.open("addrbook_popup.php","abookpopup",' . |
| 1099 | '"width=670,height=300,resizable=yes,scrollbars=yes");' . "\n" . |
| 1100 | ' if((!nwin.opener) && (document.windows != null))' . "\n" . |
| 1101 | ' nwin.opener = document.windows;' . "\n" . |
| 1102 | "}\n" . |
| 1103 | "// -->\n</script>\n\n"; |
| 1104 | } |
| 1105 | |
| 1106 | echo "\n" . '<form name="compose" action="compose.php" method="post" ' . |
| 1107 | 'enctype="multipart/form-data"'; |
| 1108 | |
| 1109 | $compose_onsubmit = array(); |
| 1110 | do_hook('compose_form'); |
| 1111 | |
| 1112 | // Plugins that use compose_form hook can add an array entry |
| 1113 | // to the globally scoped $compose_onsubmit; we add them up |
| 1114 | // here and format the form tag's full onsubmit handler. |
| 1115 | // Each plugin should use "return false" if they need to |
| 1116 | // stop form submission but otherwise should NOT use "return |
| 1117 | // true" to give other plugins the chance to do what they need |
| 1118 | // to do; SquirrelMail itself will add the final "return true". |
| 1119 | // Onsubmit text is enclosed inside of double quotes, so plugins |
| 1120 | // need to quote accordingly. |
| 1121 | if (checkForJavascript()) { |
| 1122 | $onsubmit_text = ' onsubmit="'; |
| 1123 | if (empty($compose_onsubmit)) |
| 1124 | $compose_onsubmit = array(); |
| 1125 | else if (!is_array($compose_onsubmit)) |
| 1126 | $compose_onsubmit = array($compose_onsubmit); |
| 1127 | |
| 1128 | foreach ($compose_onsubmit as $text) { |
| 1129 | $text = trim($text); |
| 1130 | if (substr($text, -1) != ';' && substr($text, -1) != '}') |
| 1131 | $text .= '; '; |
| 1132 | $onsubmit_text .= $text; |
| 1133 | } |
| 1134 | |
| 1135 | echo $onsubmit_text . ' return true;"'; |
| 1136 | } |
| 1137 | |
| 1138 | |
| 1139 | echo ">\n"; |
| 1140 | |
| 1141 | echo addHidden('startMessage', $startMessage); |
| 1142 | |
| 1143 | if ($action == 'draft') { |
| 1144 | echo addHidden('delete_draft', $passed_id); |
| 1145 | } |
| 1146 | if (isset($delete_draft)) { |
| 1147 | echo addHidden('delete_draft', $delete_draft); |
| 1148 | } |
| 1149 | if (isset($session)) { |
| 1150 | echo addHidden('session', $session); |
| 1151 | } |
| 1152 | |
| 1153 | if (isset($passed_id)) { |
| 1154 | echo addHidden('passed_id', $passed_id); |
| 1155 | } |
| 1156 | |
| 1157 | if ($saved_draft == 'yes') { |
| 1158 | $oTemplate->assign('note', _("Draft Saved")); |
| 1159 | $oTemplate->display('note.tpl'); |
| 1160 | } |
| 1161 | if ($mail_sent == 'yes') { |
| 1162 | $oTemplate->assign('note', _("Your message has been sent.")); |
| 1163 | $oTemplate->display('note.tpl'); |
| 1164 | } |
| 1165 | if ($compose_new_win == '1') { |
| 1166 | $oTemplate->display('compose_newwin_close.tpl'); |
| 1167 | } |
| 1168 | |
| 1169 | if ($location_of_buttons == 'top') { |
| 1170 | showComposeButtonRow(); |
| 1171 | } |
| 1172 | |
| 1173 | $identities = array(); |
| 1174 | if (count($idents) > 1) { |
| 1175 | reset($idents); |
| 1176 | foreach($idents as $id => $data) { |
| 1177 | $identities[$id] = $data['full_name'].' <'.$data['email_address'].'>'; |
| 1178 | } |
| 1179 | } |
| 1180 | |
| 1181 | $oTemplate->assign('identities', $identities); |
| 1182 | $oTemplate->assign('identity_def', $identity); |
| 1183 | $oTemplate->assign('input_onfocus', 'onfocus="'.join(' ', $onfocus_array).'"'); |
| 1184 | |
| 1185 | $oTemplate->assign('to', htmlspecialchars($send_to)); |
| 1186 | $oTemplate->assign('cc', htmlspecialchars($send_to_cc)); |
| 1187 | $oTemplate->assign('bcc', htmlspecialchars($send_to_bcc)); |
| 1188 | $oTemplate->assign('subject', htmlspecialchars($subject)); |
| 1189 | |
| 1190 | $oTemplate->display('compose_header.tpl'); |
| 1191 | |
| 1192 | if ($location_of_buttons == 'between') { |
| 1193 | showComposeButtonRow(); |
| 1194 | } |
| 1195 | |
| 1196 | $body_str = ''; |
| 1197 | if ($use_signature == true && $newmail == true && !isset($from_htmladdr_search)) { |
| 1198 | $signature = $idents[$identity]['signature']; |
| 1199 | |
| 1200 | if ($sig_first == '1') { |
| 1201 | /* |
| 1202 | * FIXME: test is specific to ja_JP translation implementation. |
| 1203 | * This test might apply incorrect conversion to other translations, but |
| 1204 | * use of 7bit iso-2022-jp charset in other translations might have other |
| 1205 | * issues too. |
| 1206 | */ |
| 1207 | if ($default_charset == 'iso-2022-jp') { |
| 1208 | $body_str = "\n\n".($prefix_sig==true? "-- \n":'').mb_convert_encoding($signature, 'EUC-JP'); |
| 1209 | } else { |
| 1210 | $body_str = "\n\n".($prefix_sig==true? "-- \n":'').decodeHeader($signature,false,false); |
| 1211 | } |
| 1212 | $body_str .= "\n\n".htmlspecialchars(decodeHeader($body,false,false)); |
| 1213 | } else { |
| 1214 | $body_str = "\n\n".htmlspecialchars(decodeHeader($body,false,false)); |
| 1215 | // FIXME: test is specific to ja_JP translation implementation. See above comments. |
| 1216 | if ($default_charset == 'iso-2022-jp') { |
| 1217 | $body_str .= "\n\n".($prefix_sig==true? "-- \n":'').mb_convert_encoding($signature, 'EUC-JP'); |
| 1218 | } else { |
| 1219 | $body_str .= "\n\n".($prefix_sig==true? "-- \n":'').decodeHeader($signature,false,false); |
| 1220 | } |
| 1221 | } |
| 1222 | } else { |
| 1223 | $body_str = htmlspecialchars(decodeHeader($body,false,false)); |
| 1224 | } |
| 1225 | |
| 1226 | $oTemplate->assign('editor_width', (int)$editor_size); |
| 1227 | $oTemplate->assign('editor_height', (int)$editor_height); |
| 1228 | $oTemplate->assign('input_onfocus', 'onfocus="'.join(' ', $onfocus_array).'"'); |
| 1229 | $oTemplate->assign('body', $body_str); |
| 1230 | $oTemplate->assign('show_bottom_send', $location_of_buttons!='bottom'); |
| 1231 | |
| 1232 | $oTemplate->display ('compose_body.tpl'); |
| 1233 | |
| 1234 | if ($location_of_buttons == 'bottom') { |
| 1235 | showComposeButtonRow(); |
| 1236 | } |
| 1237 | |
| 1238 | /* This code is for attachments */ |
| 1239 | if ((bool) ini_get('file_uploads')) { |
| 1240 | |
| 1241 | /* Calculate the max size for an uploaded file. |
| 1242 | * This is advisory for the user because we can't actually prevent |
| 1243 | * people to upload too large files. */ |
| 1244 | $sizes = array(); |
| 1245 | /* php.ini vars which influence the max for uploads */ |
| 1246 | $configvars = array('post_max_size', 'memory_limit', 'upload_max_filesize'); |
| 1247 | foreach($configvars as $var) { |
| 1248 | /* skip 0 or empty values, and -1 which means 'unlimited' */ |
| 1249 | if( $size = getByteSize(ini_get($var)) ) { |
| 1250 | if ( $size != '-1' ) { |
| 1251 | $sizes[] = $size; |
| 1252 | } |
| 1253 | } |
| 1254 | } |
| 1255 | |
| 1256 | $attach = array(); |
| 1257 | if ($composeMessage->entities) { |
| 1258 | foreach ($composeMessage->entities as $key => $attachment) { |
| 1259 | $attached_file = $attachment->att_local_name; |
| 1260 | if ($attachment->att_local_name || $attachment->body_part) { |
| 1261 | $attached_filename = decodeHeader($attachment->mime_header->getParameter('name')); |
| 1262 | $type = $attachment->mime_header->type0.'/'. |
| 1263 | $attachment->mime_header->type1; |
| 1264 | |
| 1265 | $a = array(); |
| 1266 | $a['Key'] = $key; |
| 1267 | $a['FileName'] = $attached_filename; |
| 1268 | $a['ContentType'] = $type; |
| 1269 | $a['Size'] = filesize($attached_file); |
| 1270 | $attach[$key] = $a; |
| 1271 | } |
| 1272 | } |
| 1273 | } |
| 1274 | |
| 1275 | $max = min($sizes); |
| 1276 | $oTemplate->assign('max_file_size', empty($max) ? -1 : $max); |
| 1277 | $oTemplate->assign('attachments', $attach); |
| 1278 | |
| 1279 | $oTemplate->display('compose_attachments.tpl'); |
| 1280 | } // End of file_uploads if-block |
| 1281 | /* End of attachment code */ |
| 1282 | |
| 1283 | echo addHidden('username', $username). |
| 1284 | addHidden('smaction', $action). |
| 1285 | addHidden('mailbox', $mailbox); |
| 1286 | /* |
| 1287 | store the complete ComposeMessages array in a hidden input value |
| 1288 | so we can restore them in case of a session timeout. |
| 1289 | */ |
| 1290 | sqgetGlobalVar('QUERY_STRING', $queryString, SQ_SERVER); |
| 1291 | echo addHidden('restoremessages', serialize($compose_messages)). |
| 1292 | addHidden('composesession', $composesession). |
| 1293 | addHidden('querystring', $queryString). |
| 1294 | "</form>\n"; |
| 1295 | if (!(bool) ini_get('file_uploads')) { |
| 1296 | /* File uploads are off, so we didn't show that part of the form. |
| 1297 | To avoid bogus bug reports, tell the user why. */ |
| 1298 | echo '<p style="text-align:center">' |
| 1299 | . _("Because PHP file uploads are turned off, you can not attach files to this message. Please see your system administrator for details.") |
| 1300 | . "</p>\r\n"; |
| 1301 | } |
| 1302 | |
| 1303 | do_hook('compose_bottom'); |
| 1304 | |
| 1305 | if ($compose_new_win=='1') { |
| 1306 | $oTemplate->display('compose_newwin_close.tpl'); |
| 1307 | } |
| 1308 | |
| 1309 | $oErrorHandler->setDelayedErrors(false); |
| 1310 | $oTemplate->display('footer.tpl'); |
| 1311 | } |
| 1312 | |
| 1313 | |
| 1314 | function showComposeButtonRow() { |
| 1315 | global $use_javascript_addr_book, $save_as_draft, |
| 1316 | $default_use_priority, $mailprio, $default_use_mdn, |
| 1317 | $request_mdn, $request_dr, |
| 1318 | $data_dir, $username; |
| 1319 | |
| 1320 | global $oTemplate, $buffer_hook; |
| 1321 | |
| 1322 | if ($default_use_priority) { |
| 1323 | $priorities = array('1'=>_("High"), '3'=>_("Normal"), '5'=>_("Low")); |
| 1324 | $priority = isset($mailprio) ? $mailprio : 3; |
| 1325 | } else { |
| 1326 | $priorities = array(); |
| 1327 | $priority = NULL; |
| 1328 | } |
| 1329 | |
| 1330 | $mdn_user_support=getPref($data_dir, $username, 'mdn_user_support',$default_use_mdn); |
| 1331 | |
| 1332 | if ($use_javascript_addr_book) { |
| 1333 | $addr_book = " <script type=\"text/javascript\"><!--\n document.write(\"". |
| 1334 | " <input type=button value=\\\""._("Addresses"). |
| 1335 | "\\\" onclick=\\\"javascript:open_abook();\\\" />\");". |
| 1336 | " // --></script><noscript>\n". |
| 1337 | ' <input type="submit" name="html_addr_search" value="'. |
| 1338 | _("Addresses").'" />'. |
| 1339 | " </noscript>\n"; |
| 1340 | } else { |
| 1341 | $addr_book = ' <input type="submit" name="html_addr_search" value="'. |
| 1342 | _("Addresses").'" />' . "\n"; |
| 1343 | } |
| 1344 | |
| 1345 | $oTemplate->assign('allow_priority', $default_use_priority==1); |
| 1346 | $oTemplate->assign('priority_list', $priorities); |
| 1347 | $oTemplate->assign('current_priority', $priority); |
| 1348 | |
| 1349 | $oTemplate->assign('notifications_enabled', $mdn_user_support==1); |
| 1350 | $oTemplate->assign('read_receipt', $request_mdn=='1'); |
| 1351 | $oTemplate->assign('delivery_receipt', $request_dr=='1'); |
| 1352 | |
| 1353 | $oTemplate->assign('drafts_enabled', $save_as_draft); |
| 1354 | $oTemplate->assign('address_book_button', $addr_book); |
| 1355 | |
| 1356 | $oTemplate->display('compose_buttons.tpl'); |
| 1357 | } |
| 1358 | |
| 1359 | function checkInput ($show) { |
| 1360 | /* |
| 1361 | * I implemented the $show variable because the error messages |
| 1362 | * were getting sent before the page header. So, I check once |
| 1363 | * using $show=false, and then when i'm ready to display the error |
| 1364 | * message, show=true |
| 1365 | */ |
| 1366 | global $send_to, $send_to_bcc; |
| 1367 | |
| 1368 | if ($send_to == '' && $send_to_bcc == '') { |
| 1369 | if ($show) { |
| 1370 | plain_error_message(_("You have not filled in the \"To:\" field.")); |
| 1371 | } |
| 1372 | return false; |
| 1373 | } |
| 1374 | return true; |
| 1375 | } /* function checkInput() */ |
| 1376 | |
| 1377 | |
| 1378 | /* True if FAILURE */ |
| 1379 | function saveAttachedFiles($session) { |
| 1380 | global $_FILES, $attachment_dir, $username, |
| 1381 | $data_dir, $compose_messages; |
| 1382 | |
| 1383 | /* get out of here if no file was attached at all */ |
| 1384 | if (! is_uploaded_file($_FILES['attachfile']['tmp_name']) ) { |
| 1385 | return true; |
| 1386 | } |
| 1387 | |
| 1388 | $hashed_attachment_dir = getHashedDir($username, $attachment_dir); |
| 1389 | $localfilename = GenerateRandomString(32, '', 7); |
| 1390 | $full_localfilename = "$hashed_attachment_dir/$localfilename"; |
| 1391 | while (file_exists($full_localfilename)) { |
| 1392 | $localfilename = GenerateRandomString(32, '', 7); |
| 1393 | $full_localfilename = "$hashed_attachment_dir/$localfilename"; |
| 1394 | } |
| 1395 | |
| 1396 | // m_u_f works better with restricted PHP installs (safe_mode, open_basedir), |
| 1397 | // if that doesn't work, try a simple rename. |
| 1398 | if (!@move_uploaded_file($_FILES['attachfile']['tmp_name'],$full_localfilename)) { |
| 1399 | if (!@rename($_FILES['attachfile']['tmp_name'], $full_localfilename)) { |
| 1400 | return true; |
| 1401 | } |
| 1402 | } |
| 1403 | $message = $compose_messages[$session]; |
| 1404 | $type = strtolower($_FILES['attachfile']['type']); |
| 1405 | $name = $_FILES['attachfile']['name']; |
| 1406 | $message->initAttachment($type, $name, $full_localfilename); |
| 1407 | $compose_messages[$session] = $message; |
| 1408 | sqsession_register($compose_messages , 'compose_messages'); |
| 1409 | } |
| 1410 | |
| 1411 | /* parse values like 8M and 2k into bytes */ |
| 1412 | function getByteSize($ini_size) { |
| 1413 | |
| 1414 | if(!$ini_size) { |
| 1415 | return FALSE; |
| 1416 | } |
| 1417 | |
| 1418 | $ini_size = trim($ini_size); |
| 1419 | |
| 1420 | // if there's some kind of letter at the end of the string we need to multiply. |
| 1421 | if(!is_numeric(substr($ini_size, -1))) { |
| 1422 | |
| 1423 | switch(strtoupper(substr($ini_size, -1))) { |
| 1424 | case 'G': |
| 1425 | $bytesize = 1073741824; |
| 1426 | break; |
| 1427 | case 'M': |
| 1428 | $bytesize = 1048576; |
| 1429 | break; |
| 1430 | case 'K': |
| 1431 | $bytesize = 1024; |
| 1432 | break; |
| 1433 | } |
| 1434 | |
| 1435 | return ($bytesize * (int)substr($ini_size, 0, -1)); |
| 1436 | } |
| 1437 | |
| 1438 | return $ini_size; |
| 1439 | } |
| 1440 | |
| 1441 | |
| 1442 | /** |
| 1443 | * temporary function to make use of the deliver class. |
| 1444 | * In the future the responsible backend should be automaticly loaded |
| 1445 | * and conf.pl should show a list of available backends. |
| 1446 | * The message also should be constructed by the message class. |
| 1447 | */ |
| 1448 | function deliverMessage($composeMessage, $draft=false) { |
| 1449 | global $send_to, $send_to_cc, $send_to_bcc, $mailprio, $subject, $body, |
| 1450 | $username, $popuser, $usernamedata, $identity, $idents, $data_dir, |
| 1451 | $request_mdn, $request_dr, $default_charset, $color, $useSendmail, |
| 1452 | $domain, $action, $default_move_to_sent, $move_to_sent; |
| 1453 | global $imapServerAddress, $imapPort, $sent_folder, $key; |
| 1454 | |
| 1455 | $rfc822_header = $composeMessage->rfc822_header; |
| 1456 | |
| 1457 | $abook = addressbook_init(false, true); |
| 1458 | $rfc822_header->to = $rfc822_header->parseAddress($send_to,true, array(), '', $domain, array(&$abook,'lookup')); |
| 1459 | $rfc822_header->cc = $rfc822_header->parseAddress($send_to_cc,true,array(), '',$domain, array(&$abook,'lookup')); |
| 1460 | $rfc822_header->bcc = $rfc822_header->parseAddress($send_to_bcc,true, array(), '',$domain, array(&$abook,'lookup')); |
| 1461 | $rfc822_header->priority = $mailprio; |
| 1462 | $rfc822_header->subject = $subject; |
| 1463 | |
| 1464 | $special_encoding=''; |
| 1465 | if (strtolower($default_charset) == 'iso-2022-jp') { |
| 1466 | if (mb_detect_encoding($body) == 'ASCII') { |
| 1467 | $special_encoding = '8bit'; |
| 1468 | } else { |
| 1469 | $body = mb_convert_encoding($body, 'JIS'); |
| 1470 | $special_encoding = '7bit'; |
| 1471 | } |
| 1472 | } |
| 1473 | $composeMessage->setBody($body); |
| 1474 | |
| 1475 | if (ereg("^([^@%/]+)[@%/](.+)$", $username, $usernamedata)) { |
| 1476 | $popuser = $usernamedata[1]; |
| 1477 | $domain = $usernamedata[2]; |
| 1478 | unset($usernamedata); |
| 1479 | } else { |
| 1480 | $popuser = $username; |
| 1481 | } |
| 1482 | $reply_to = ''; |
| 1483 | $from_mail = $idents[$identity]['email_address']; |
| 1484 | $full_name = $idents[$identity]['full_name']; |
| 1485 | $reply_to = $idents[$identity]['reply_to']; |
| 1486 | if (!$from_mail) { |
| 1487 | $from_mail = "$popuser@$domain"; |
| 1488 | } |
| 1489 | $rfc822_header->from = $rfc822_header->parseAddress($from_mail,true); |
| 1490 | if ($full_name) { |
| 1491 | $from = $rfc822_header->from[0]; |
| 1492 | if (!$from->host) $from->host = $domain; |
| 1493 | $full_name_encoded = encodeHeader($full_name); |
| 1494 | if ($full_name_encoded != $full_name) { |
| 1495 | $from_addr = $full_name_encoded .' <'.$from->mailbox.'@'.$from->host.'>'; |
| 1496 | } else { |
| 1497 | $from_addr = '"'.$full_name .'" <'.$from->mailbox.'@'.$from->host.'>'; |
| 1498 | } |
| 1499 | $rfc822_header->from = $rfc822_header->parseAddress($from_addr,true); |
| 1500 | } |
| 1501 | if ($reply_to) { |
| 1502 | $rfc822_header->reply_to = $rfc822_header->parseAddress($reply_to,true); |
| 1503 | } |
| 1504 | /* Receipt: On Read */ |
| 1505 | if (isset($request_mdn) && $request_mdn) { |
| 1506 | $rfc822_header->dnt = $rfc822_header->parseAddress($from_mail,true); |
| 1507 | } elseif (isset($rfc822_header->dnt)) { |
| 1508 | unset($rfc822_header->dnt); |
| 1509 | } |
| 1510 | |
| 1511 | /* Receipt: On Delivery */ |
| 1512 | if (isset($request_dr) && $request_dr) { |
| 1513 | $rfc822_header->more_headers['Return-Receipt-To'] = $from_mail; |
| 1514 | } elseif (isset($rfc822_header->more_headers['Return-Receipt-To'])) { |
| 1515 | unset($rfc822_header->more_headers['Return-Receipt-To']); |
| 1516 | } |
| 1517 | |
| 1518 | /* multipart messages */ |
| 1519 | if (count($composeMessage->entities)) { |
| 1520 | $message_body = new Message(); |
| 1521 | $message_body->body_part = $composeMessage->body_part; |
| 1522 | $composeMessage->body_part = ''; |
| 1523 | $mime_header = new MessageHeader; |
| 1524 | $mime_header->type0 = 'text'; |
| 1525 | $mime_header->type1 = 'plain'; |
| 1526 | if ($special_encoding) { |
| 1527 | $mime_header->encoding = $special_encoding; |
| 1528 | } else { |
| 1529 | $mime_header->encoding = '8bit'; |
| 1530 | } |
| 1531 | if ($default_charset) { |
| 1532 | $mime_header->parameters['charset'] = $default_charset; |
| 1533 | } |
| 1534 | $message_body->mime_header = $mime_header; |
| 1535 | array_unshift($composeMessage->entities, $message_body); |
| 1536 | $content_type = new ContentType('multipart/mixed'); |
| 1537 | } else { |
| 1538 | $content_type = new ContentType('text/plain'); |
| 1539 | if ($special_encoding) { |
| 1540 | $rfc822_header->encoding = $special_encoding; |
| 1541 | } else { |
| 1542 | $rfc822_header->encoding = '8bit'; |
| 1543 | } |
| 1544 | if ($default_charset) { |
| 1545 | $content_type->properties['charset']=$default_charset; |
| 1546 | } |
| 1547 | } |
| 1548 | |
| 1549 | $rfc822_header->content_type = $content_type; |
| 1550 | $composeMessage->rfc822_header = $rfc822_header; |
| 1551 | |
| 1552 | /* Here you can modify the message structure just before we hand |
| 1553 | it over to deliver */ |
| 1554 | $hookReturn = do_hook('compose_send', $composeMessage); |
| 1555 | /* Get any changes made by plugins to $composeMessage. */ |
| 1556 | if ( is_object($hookReturn[1]) ) { |
| 1557 | $composeMessage = $hookReturn[1]; |
| 1558 | } |
| 1559 | |
| 1560 | if (!$useSendmail && !$draft) { |
| 1561 | require_once(SM_PATH . 'class/deliver/Deliver_SMTP.class.php'); |
| 1562 | $deliver = new Deliver_SMTP(); |
| 1563 | global $smtpServerAddress, $smtpPort, $pop_before_smtp; |
| 1564 | |
| 1565 | $authPop = (isset($pop_before_smtp) && $pop_before_smtp) ? true : false; |
| 1566 | get_smtp_user($user, $pass); |
| 1567 | $stream = $deliver->initStream($composeMessage,$domain,0, |
| 1568 | $smtpServerAddress, $smtpPort, $user, $pass, $authPop); |
| 1569 | } elseif (!$draft) { |
| 1570 | require_once(SM_PATH . 'class/deliver/Deliver_SendMail.class.php'); |
| 1571 | global $sendmail_path, $sendmail_args; |
| 1572 | // Check for outdated configuration |
| 1573 | if (!isset($sendmail_args)) { |
| 1574 | if ($sendmail_path=='/var/qmail/bin/qmail-inject') { |
| 1575 | $sendmail_args = ''; |
| 1576 | } else { |
| 1577 | $sendmail_args = '-i -t'; |
| 1578 | } |
| 1579 | } |
| 1580 | $deliver = new Deliver_SendMail(array('sendmail_args'=>$sendmail_args)); |
| 1581 | $stream = $deliver->initStream($composeMessage,$sendmail_path); |
| 1582 | } elseif ($draft) { |
| 1583 | global $draft_folder; |
| 1584 | require_once(SM_PATH . 'class/deliver/Deliver_IMAP.class.php'); |
| 1585 | $imap_stream = sqimap_login($username, false, $imapServerAddress, |
| 1586 | $imapPort, 0); |
| 1587 | if (sqimap_mailbox_exists ($imap_stream, $draft_folder)) { |
| 1588 | require_once(SM_PATH . 'class/deliver/Deliver_IMAP.class.php'); |
| 1589 | $imap_deliver = new Deliver_IMAP(); |
| 1590 | $length = $imap_deliver->mail($composeMessage); |
| 1591 | sqimap_append ($imap_stream, $draft_folder, $length); |
| 1592 | $imap_deliver->mail($composeMessage, $imap_stream); |
| 1593 | sqimap_append_done ($imap_stream, $draft_folder); |
| 1594 | sqimap_logout($imap_stream); |
| 1595 | unset ($imap_deliver); |
| 1596 | $composeMessage->purgeAttachments(); |
| 1597 | return $length; |
| 1598 | } else { |
| 1599 | $msg = '<br />'.sprintf(_("Error: Draft folder %s does not exist."), htmlspecialchars($draft_folder)); |
| 1600 | plain_error_message($msg); |
| 1601 | return false; |
| 1602 | } |
| 1603 | } |
| 1604 | $success = false; |
| 1605 | if ($stream) { |
| 1606 | $length = $deliver->mail($composeMessage, $stream); |
| 1607 | $success = $deliver->finalizeStream($stream); |
| 1608 | } |
| 1609 | if (!$success) { |
| 1610 | // $deliver->dlv_server_msg is not always server's reply |
| 1611 | $msg = $deliver->dlv_msg; |
| 1612 | if (!empty($deliver->dlv_server_msg)) { |
| 1613 | // add 'server replied' part only when it is not empty. |
| 1614 | // Delivery error can be generated by delivery class itself |
| 1615 | $msg.='<br />' . |
| 1616 | _("Server replied:") . ' ' . $deliver->dlv_ret_nr . ' ' . |
| 1617 | nl2br(htmlspecialchars($deliver->dlv_server_msg)); |
| 1618 | } |
| 1619 | plain_error_message($msg); |
| 1620 | } else { |
| 1621 | unset ($deliver); |
| 1622 | $move_to_sent = getPref($data_dir,$username,'move_to_sent'); |
| 1623 | $imap_stream = sqimap_login($username, false, $imapServerAddress, $imapPort, 0); |
| 1624 | |
| 1625 | /* Move to sent code */ |
| 1626 | if (isset($default_move_to_sent) && ($default_move_to_sent != 0)) { |
| 1627 | $svr_allow_sent = true; |
| 1628 | } else { |
| 1629 | $svr_allow_sent = false; |
| 1630 | } |
| 1631 | |
| 1632 | if (isset($sent_folder) && (($sent_folder != '') || ($sent_folder != 'none')) |
| 1633 | && sqimap_mailbox_exists( $imap_stream, $sent_folder)) { |
| 1634 | $fld_sent = true; |
| 1635 | } else { |
| 1636 | $fld_sent = false; |
| 1637 | } |
| 1638 | |
| 1639 | if ((isset($move_to_sent) && ($move_to_sent != 0)) || (!isset($move_to_sent))) { |
| 1640 | $lcl_allow_sent = true; |
| 1641 | } else { |
| 1642 | $lcl_allow_sent = false; |
| 1643 | } |
| 1644 | |
| 1645 | if (($fld_sent && $svr_allow_sent && !$lcl_allow_sent) || ($fld_sent && $lcl_allow_sent)) { |
| 1646 | global $passed_id, $mailbox, $action; |
| 1647 | if ($action == 'reply' || $action == 'reply_all') { |
| 1648 | $save_reply_with_orig=getPref($data_dir,$username,'save_reply_with_orig'); |
| 1649 | if ($save_reply_with_orig) { |
| 1650 | $sent_folder = $mailbox; |
| 1651 | } |
| 1652 | } |
| 1653 | sqimap_append ($imap_stream, $sent_folder, $length); |
| 1654 | require_once(SM_PATH . 'class/deliver/Deliver_IMAP.class.php'); |
| 1655 | $imap_deliver = new Deliver_IMAP(); |
| 1656 | $imap_deliver->mail($composeMessage, $imap_stream); |
| 1657 | sqimap_append_done ($imap_stream, $sent_folder); |
| 1658 | unset ($imap_deliver); |
| 1659 | } |
| 1660 | |
| 1661 | global $passed_id, $mailbox, $action, $what, $iAccount,$startMessage; |
| 1662 | |
| 1663 | $composeMessage->purgeAttachments(); |
| 1664 | if ($action=='reply' || $action=='reply_all' || $action=='forward' || $action=='forward_as_attachment') { |
| 1665 | require(SM_PATH . 'functions/mailbox_display.php'); |
| 1666 | $aMailbox = sqm_api_mailbox_select($imap_stream, $iAccount, $mailbox,array('setindex' => $what, 'offset' => $startMessage),array()); |
| 1667 | switch($action) { |
| 1668 | case 'reply': |
| 1669 | case 'reply_all': |
| 1670 | // check if we are allowed to set the \\Answered flag |
| 1671 | if (in_array('\\answered',$aMailbox['PERMANENTFLAGS'], true)) { |
| 1672 | $aUpdatedMsgs = sqimap_toggle_flag($imap_stream, array($passed_id), '\\Answered', true, false); |
| 1673 | if (isset($aUpdatedMsgs[$passed_id]['FLAGS'])) { |
| 1674 | /** |
| 1675 | * Only update the cached headers if the header is |
| 1676 | * cached. |
| 1677 | */ |
| 1678 | if (isset($aMailbox['MSG_HEADERS'][$passed_id])) { |
| 1679 | $aMailbox['MSG_HEADERS'][$passed_id]['FLAGS'] = $aMsg['FLAGS']; |
| 1680 | } |
| 1681 | } |
| 1682 | } |
| 1683 | break; |
| 1684 | case 'forward': |
| 1685 | case 'forward_as_attachment': |
| 1686 | // check if we are allowed to set the $Forwarded flag (RFC 4550 paragraph 2.8) |
| 1687 | if (in_array('$forwarded',$aMailbox['PERMANENTFLAGS'], true) || |
| 1688 | in_array('\\*',$aMailbox['PERMANENTFLAGS'])) { |
| 1689 | |
| 1690 | $aUpdatedMsgs = sqimap_toggle_flag($imap_stream, array($passed_id), '$Forwarded', true, false); |
| 1691 | if (isset($aUpdatedMsgs[$passed_id]['FLAGS'])) { |
| 1692 | if (isset($aMailbox['MSG_HEADERS'][$passed_id])) { |
| 1693 | $aMailbox['MSG_HEADERS'][$passed_id]['FLAGS'] = $aMsg['FLAGS']; |
| 1694 | } |
| 1695 | } |
| 1696 | } |
| 1697 | break; |
| 1698 | } |
| 1699 | |
| 1700 | /** |
| 1701 | * Write mailbox with updated seen flag information back to cache. |
| 1702 | */ |
| 1703 | if(isset($aUpdatedMsgs[$passed_id])) { |
| 1704 | $mailbox_cache[$iAccount.'_'.$aMailbox['NAME']] = $aMailbox; |
| 1705 | sqsession_register($mailbox_cache,'mailbox_cache'); |
| 1706 | } |
| 1707 | |
| 1708 | sqimap_logout($imap_stream); |
| 1709 | } |
| 1710 | } |
| 1711 | return $success; |
| 1712 | } |
| 1713 | ?> |