Source for file compose.php

Documentation is available at compose.php

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

Documentation generated on 2020年1月13日 04:22:09 +0100 by phpDocumentor 1.4.3

AltStyle によって変換されたページ (->オリジナル) /